summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/json/ext/generator/generator.c4
-rw-r--r--ext/json/ext/parser/parser.c75
-rw-r--r--ext/json/ext/parser/parser.h1
-rw-r--r--ext/json/ext/parser/parser.rl41
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 4bbc0c7..b44b396 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;
}
@@ -1711,12 +1721,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 1720 "parser.c"
+#line 1730 "parser.c"
static const int JSON_start = 1;
static const int JSON_first_final = 10;
static const int JSON_error = 0;
@@ -1724,7 +1734,7 @@ static const int JSON_error = 0;
static const int JSON_en_main = 1;
-#line 727 "parser.rl"
+#line 737 "parser.rl"
static VALUE cParser_parse_strict(VALUE self)
@@ -1735,16 +1745,16 @@ static VALUE cParser_parse_strict(VALUE self)
GET_PARSER;
-#line 1739 "parser.c"
+#line 1749 "parser.c"
{
cs = JSON_start;
}
-#line 737 "parser.rl"
+#line 747 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1748 "parser.c"
+#line 1758 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1800,7 +1810,7 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 716 "parser.rl"
+#line 726 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1809,7 +1819,7 @@ tr3:
}
goto st10;
tr4:
-#line 709 "parser.rl"
+#line 719 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1821,7 +1831,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1825 "parser.c"
+#line 1835 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1878,7 +1888,7 @@ case 9:
_out: {}
}
-#line 740 "parser.rl"
+#line 750 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1890,7 +1900,7 @@ case 9:
-#line 1894 "parser.c"
+#line 1904 "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;
@@ -1898,7 +1908,7 @@ static const int JSON_quirks_mode_error = 0;
static const int JSON_quirks_mode_en_main = 1;
-#line 765 "parser.rl"
+#line 775 "parser.rl"
static VALUE cParser_parse_quirks_mode(VALUE self)
@@ -1909,16 +1919,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
GET_PARSER;
-#line 1913 "parser.c"
+#line 1923 "parser.c"
{
cs = JSON_quirks_mode_start;
}
-#line 775 "parser.rl"
+#line 785 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1922 "parser.c"
+#line 1932 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1952,7 +1962,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 757 "parser.rl"
+#line 767 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1962,7 +1972,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1966 "parser.c"
+#line 1976 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2051,7 +2061,7 @@ case 9:
_out: {}
}
-#line 778 "parser.rl"
+#line 788 "parser.rl"
if (cs >= JSON_quirks_mode_first_final && p == pe) {
return result;
@@ -2083,6 +2093,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;
}
@@ -2103,7 +2120,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 8c4681d..56e1a11 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;
}
@@ -695,7 +705,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;
}
@@ -806,6 +816,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;
}
@@ -826,7 +843,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);
}
/*