12
12
#include <mruby/throw.h>
13
13
#include <mruby/variable.h>
14
14
#include <mruby/numeric.h>
15
+ #include <mruby/internal.h>
15
16
#include "mruby/msgpack.h"
17
+ #include <mruby/string_is_utf8.h>
16
18
17
19
typedef struct {
18
20
mrb_state * mrb ;
@@ -37,6 +39,7 @@ mrb_msgpack_data_write(void* data, const char* buf, size_t len)
37
39
return 0 ;
38
40
}
39
41
}
42
+
40
43
#define pack_integer_helper_ (x , pk , self ) msgpack_pack_int##x(pk, mrb_integer(self))
41
44
#define pack_integer_helper (x , pk , self ) pack_integer_helper_(x, pk, self)
42
45
#define mrb_msgpack_pack_int (pk , self ) pack_integer_helper(MRB_INT_BIT, pk, self)
@@ -69,14 +72,18 @@ static void
69
72
mrb_msgpack_pack_string_value (mrb_state * mrb , mrb_value self , msgpack_packer * pk )
70
73
{
71
74
int rc ;
72
- if ( memchr ( RSTRING_PTR ( self ), '\0' , RSTRING_LEN ( self ))) {
73
- rc = msgpack_pack_bin ( pk , RSTRING_LEN (self ));
74
- if ( likely ( rc == 0 ))
75
- rc = msgpack_pack_bin_body ( pk , RSTRING_PTR (self ), RSTRING_LEN ( self ));
76
- } else {
75
+ #ifdef MRB_UTF8_STRING
76
+ if ( RSTRING_LEN ( self ) == mrb_utf8_strlen ( RSTRING_PTR ( self ) , RSTRING_LEN (self ))) {
77
+ #else
78
+ if ( mrb_str_is_utf8 (self )) {
79
+ #endif
77
80
rc = msgpack_pack_str (pk , RSTRING_LEN (self ));
78
81
if (likely (rc == 0 ))
79
82
rc = msgpack_pack_str_body (pk , RSTRING_PTR (self ), RSTRING_LEN (self ));
83
+ } else {
84
+ rc = msgpack_pack_bin (pk , RSTRING_LEN (self ));
85
+ if (likely (rc == 0 ))
86
+ rc = msgpack_pack_bin_body (pk , RSTRING_PTR (self ), RSTRING_LEN (self ));
80
87
}
81
88
if (unlikely (rc < 0 )) {
82
89
mrb_raise (mrb , E_MSGPACK_ERROR , "cannot pack string" );
@@ -568,8 +575,10 @@ mrb_msgpack_unpack_m_cb(mrb_state *mrb, mrb_value self_data_block_result)
568
575
msgpack_unpacked_init (result );
569
576
if (mrb_type (block ) == MRB_TT_PROC ) {
570
577
ret = msgpack_unpack_next (result , str , str_len , & off );
578
+ int arena_index = mrb_gc_arena_save (mrb );
571
579
while (ret == MSGPACK_UNPACK_SUCCESS ) {
572
580
mrb_yield (mrb , block , mrb_unpack_msgpack_obj (mrb , result -> data ));
581
+ mrb_gc_arena_restore (mrb , arena_index );
573
582
ret = msgpack_unpack_next (result , str , str_len , & off );
574
583
}
575
584
switch (ret ) {
@@ -616,8 +625,7 @@ mrb_msgpack_unpack(mrb_state *mrb, mrb_value data)
616
625
msgpack_unpacked result ;
617
626
mrb_msgpack_unpack_m_cb_data cb_data = {data , RSTRING_PTR (data ), RSTRING_LEN (data ), mrb_nil_value (), & result };
618
627
mrb_value cb_data_cptr = mrb_cptr_value (mrb , & cb_data );
619
- mrb_value unpack_return = mrb_ensure (mrb , mrb_msgpack_unpack_m_cb , cb_data_cptr , mrb_msgpack_unpack_m_ensure , cb_data_cptr );
620
- return unpack_return ;
628
+ return mrb_ensure (mrb , mrb_msgpack_unpack_m_cb , cb_data_cptr , mrb_msgpack_unpack_m_ensure , cb_data_cptr );
621
629
}
622
630
623
631
static mrb_value
0 commit comments