diff options
-rw-r--r-- | ext/json/ext/generator/generator.c | 4 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.c | 75 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.h | 1 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.rl | 41 |
4 files changed, 78 insertions, 43 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 781e9e6..8d8e71e 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -1049,9 +1049,9 @@ static VALUE cState_init_copy(VALUE obj, VALUE orig) */ static VALUE cState_from_state_s(VALUE self, VALUE opts) { - if (rb_obj_is_kind_of(opts, self)) { + if (RTEST(rb_obj_is_kind_of(opts, self))) { return opts; - } else if (rb_obj_is_kind_of(opts, rb_cHash)) { + } else if (RTEST(rb_obj_is_kind_of(opts, rb_cHash))) { return rb_funcall(self, i_new, 1, opts); } else { if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) { diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 4ea663c..18ee58e 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -1593,6 +1593,16 @@ static VALUE convert_encoding(VALUE source) return source; } +#if defined MAGLEV +// Maglev doesn't support the mark function, keep a reference in the object +#define QUOTE(x) #x +#define PARSER_SET_REFERENCE(json, field, val) \ + (json)->field = (val); \ + rb_iv_set(json->dwrapped_parser, QUOTE(@field), (json)->field); +#else +#define PARSER_SET_REFERENCE(json, field, val) (json)->field = (val); +#endif + /* * call-seq: new(source, opts => {}) * @@ -1624,7 +1634,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) VALUE source, opts; GET_PARSER_INIT; - if (json->Vsource) { + if (RTEST(json->Vsource)) { rb_raise(rb_eTypeError, "already initialized instance"); } rb_scan_args(argc, argv, "11", &source, &opts); @@ -1672,35 +1682,35 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } tmp = ID2SYM(i_create_id); if (option_given_p(opts, tmp)) { - json->create_id = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, create_id, rb_hash_aref(opts, tmp)); } else { - json->create_id = rb_funcall(mJSON, i_create_id, 0); + PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0)); } tmp = ID2SYM(i_object_class); if (option_given_p(opts, tmp)) { - json->object_class = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, object_class, rb_hash_aref(opts, tmp)); } else { - json->object_class = Qnil; + PARSER_SET_REFERENCE(json, object_class, Qnil); } tmp = ID2SYM(i_array_class); if (option_given_p(opts, tmp)) { - json->array_class = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, array_class, rb_hash_aref(opts, tmp)); } else { - json->array_class = Qnil; + PARSER_SET_REFERENCE(json, array_class, Qnil); } tmp = ID2SYM(i_match_string); if (option_given_p(opts, tmp)) { VALUE match_string = rb_hash_aref(opts, tmp); - json->match_string = RTEST(match_string) ? match_string : Qnil; + PARSER_SET_REFERENCE(json, match_string, RTEST(match_string) ? match_string : Qnil); } else { - json->match_string = Qnil; + PARSER_SET_REFERENCE(json, match_string, Qnil); } } } else { json->max_nesting = 19; json->allow_nan = 0; json->create_additions = 1; - json->create_id = rb_funcall(mJSON, i_create_id, 0); + PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0)); json->object_class = Qnil; json->array_class = Qnil; } @@ -1710,12 +1720,12 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->current_nesting = 0; json->len = RSTRING_LEN(source); json->source = RSTRING_PTR(source);; - json->Vsource = source; + PARSER_SET_REFERENCE(json, Vsource, source); return self; } -#line 1719 "parser.c" +#line 1729 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; @@ -1723,7 +1733,7 @@ static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 726 "parser.rl" +#line 736 "parser.rl" static VALUE cParser_parse_strict(VALUE self) @@ -1734,16 +1744,16 @@ static VALUE cParser_parse_strict(VALUE self) GET_PARSER; -#line 1738 "parser.c" +#line 1748 "parser.c" { cs = JSON_start; } -#line 736 "parser.rl" +#line 746 "parser.rl" p = json->source; pe = p + json->len; -#line 1747 "parser.c" +#line 1757 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1799,7 +1809,7 @@ case 5: goto st1; goto st5; tr3: -#line 715 "parser.rl" +#line 725 "parser.rl" { char *np; json->current_nesting = 1; @@ -1808,7 +1818,7 @@ tr3: } goto st10; tr4: -#line 708 "parser.rl" +#line 718 "parser.rl" { char *np; json->current_nesting = 1; @@ -1820,7 +1830,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1824 "parser.c" +#line 1834 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1877,7 +1887,7 @@ case 9: _out: {} } -#line 739 "parser.rl" +#line 749 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -1889,7 +1899,7 @@ case 9: -#line 1893 "parser.c" +#line 1903 "parser.c" static const int JSON_quirks_mode_start = 1; static const int JSON_quirks_mode_first_final = 10; static const int JSON_quirks_mode_error = 0; @@ -1897,7 +1907,7 @@ static const int JSON_quirks_mode_error = 0; static const int JSON_quirks_mode_en_main = 1; -#line 764 "parser.rl" +#line 774 "parser.rl" static VALUE cParser_parse_quirks_mode(VALUE self) @@ -1908,16 +1918,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self) GET_PARSER; -#line 1912 "parser.c" +#line 1922 "parser.c" { cs = JSON_quirks_mode_start; } -#line 774 "parser.rl" +#line 784 "parser.rl" p = json->source; pe = p + json->len; -#line 1921 "parser.c" +#line 1931 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1951,7 +1961,7 @@ st0: cs = 0; goto _out; tr2: -#line 756 "parser.rl" +#line 766 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1961,7 +1971,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1965 "parser.c" +#line 1975 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2050,7 +2060,7 @@ case 9: _out: {} } -#line 777 "parser.rl" +#line 787 "parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2082,6 +2092,13 @@ static JSON_Parser *JSON_allocate() { JSON_Parser *json = ALLOC(JSON_Parser); MEMZERO(json, JSON_Parser, 1); + json->dwrapped_parser = Qnil; + json->Vsource = Qnil; + json->create_id = Qnil; + json->object_class = Qnil; + json->array_class = Qnil; + json->match_string = Qnil; + return json; } @@ -2102,7 +2119,7 @@ static void JSON_free(JSON_Parser *json) static VALUE cJSON_parser_s_allocate(VALUE klass) { JSON_Parser *json = JSON_allocate(); - return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json); + return json->dwrapped_parser = Data_Wrap_Struct(klass, JSON_mark, JSON_free, json); } /* diff --git a/ext/json/ext/parser/parser.h b/ext/json/ext/parser/parser.h index da6fc5c..051f79c 100644 --- a/ext/json/ext/parser/parser.h +++ b/ext/json/ext/parser/parser.h @@ -34,6 +34,7 @@ typedef unsigned char UTF8; /* typically 8 bits */ #define UNI_SUR_LOW_END (UTF32)0xDFFF typedef struct JSON_ParserStruct { + VALUE dwrapped_parser; VALUE Vsource; char *source; long len; diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index e7d47e1..2a2cc89 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -577,6 +577,16 @@ static VALUE convert_encoding(VALUE source) return source; } +#if defined MAGLEV +// Maglev doesn't support the mark function, keep a reference in the object +#define QUOTE(x) #x +#define PARSER_SET_REFERENCE(json, field, val) \ + (json)->field = (val); \ + rb_iv_set(json->dwrapped_parser, QUOTE(@field), (json)->field); +#else +#define PARSER_SET_REFERENCE(json, field, val) (json)->field = (val); +#endif + /* * call-seq: new(source, opts => {}) * @@ -608,7 +618,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) VALUE source, opts; GET_PARSER_INIT; - if (json->Vsource) { + if (RTEST(json->Vsource)) { rb_raise(rb_eTypeError, "already initialized instance"); } rb_scan_args(argc, argv, "11", &source, &opts); @@ -656,35 +666,35 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } tmp = ID2SYM(i_create_id); if (option_given_p(opts, tmp)) { - json->create_id = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, create_id, rb_hash_aref(opts, tmp)); } else { - json->create_id = rb_funcall(mJSON, i_create_id, 0); + PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0)); } tmp = ID2SYM(i_object_class); if (option_given_p(opts, tmp)) { - json->object_class = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, object_class, rb_hash_aref(opts, tmp)); } else { - json->object_class = Qnil; + PARSER_SET_REFERENCE(json, object_class, Qnil); } tmp = ID2SYM(i_array_class); if (option_given_p(opts, tmp)) { - json->array_class = rb_hash_aref(opts, tmp); + PARSER_SET_REFERENCE(json, array_class, rb_hash_aref(opts, tmp)); } else { - json->array_class = Qnil; + PARSER_SET_REFERENCE(json, array_class, Qnil); } tmp = ID2SYM(i_match_string); if (option_given_p(opts, tmp)) { VALUE match_string = rb_hash_aref(opts, tmp); - json->match_string = RTEST(match_string) ? match_string : Qnil; + PARSER_SET_REFERENCE(json, match_string, RTEST(match_string) ? match_string : Qnil); } else { - json->match_string = Qnil; + PARSER_SET_REFERENCE(json, match_string, Qnil); } } } else { json->max_nesting = 19; json->allow_nan = 0; json->create_additions = 1; - json->create_id = rb_funcall(mJSON, i_create_id, 0); + PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0)); json->object_class = Qnil; json->array_class = Qnil; } @@ -694,7 +704,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->current_nesting = 0; json->len = RSTRING_LEN(source); json->source = RSTRING_PTR(source);; - json->Vsource = source; + PARSER_SET_REFERENCE(json, Vsource, source); return self; } @@ -805,6 +815,13 @@ static JSON_Parser *JSON_allocate() { JSON_Parser *json = ALLOC(JSON_Parser); MEMZERO(json, JSON_Parser, 1); + json->dwrapped_parser = Qnil; + json->Vsource = Qnil; + json->create_id = Qnil; + json->object_class = Qnil; + json->array_class = Qnil; + json->match_string = Qnil; + return json; } @@ -825,7 +842,7 @@ static void JSON_free(JSON_Parser *json) static VALUE cJSON_parser_s_allocate(VALUE klass) { JSON_Parser *json = JSON_allocate(); - return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json); + return json->dwrapped_parser = Data_Wrap_Struct(klass, JSON_mark, JSON_free, json); } /* |