summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Felgentreff <timfelgentreff@gmail.com>2011-09-27 11:15:56 -0700
committerTim Felgentreff <timfelgentreff@gmail.com>2011-09-27 16:33:43 -0700
commit6b7e16bc6aa453507dadc6959e158372cab641b9 (patch)
tree7b6a143ac0ffdc9574b0d96f0653ab405dd7e88f
parent65dd51142e4f3e9a6f5d5956a72b8a95a29639b0 (diff)
downloadjson-6b7e16bc6aa453507dadc6959e158372cab641b9.tar.gz
change source so that it'll work on all impls
-rw-r--r--ext/json/ext/parser/parser.c135
-rw-r--r--ext/json/ext/parser/parser.h18
-rw-r--r--ext/json/ext/parser/parser.rl103
3 files changed, 131 insertions, 125 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index b7d0b68..8e4753a 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -98,7 +98,7 @@ static const int JSON_object_en_main = 1;
#line 150 "parser.rl"
-static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
VALUE last_name = Qnil;
@@ -148,7 +148,7 @@ case 2:
tr2:
#line 132 "parser.rl"
{
- const char *np;
+ char *np;
json->parsing_name = 1;
np = JSON_parse_string(json, p, pe, &last_name);
json->parsing_name = 0;
@@ -229,7 +229,7 @@ tr11:
#line 117 "parser.rl"
{
VALUE v = Qnil;
- const char *np = JSON_parse_value(json, p, pe, &v);
+ char *np = JSON_parse_value(json, p, pe, &v);
if (np == NULL) {
p--; {p++; cs = 9; goto _out;}
} else {
@@ -471,7 +471,7 @@ static const int JSON_value_en_main = 1;
#line 265 "parser.rl"
-static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -510,7 +510,7 @@ cs = 0;
tr0:
#line 213 "parser.rl"
{
- const char *np = JSON_parse_string(json, p, pe, result);
+ char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
}
goto st21;
@@ -537,7 +537,7 @@ tr2:
tr5:
#line 236 "parser.rl"
{
- const char *np;
+ char *np;
json->current_nesting++;
np = JSON_parse_array(json, p, pe, result);
json->current_nesting--;
@@ -547,7 +547,7 @@ tr5:
tr9:
#line 244 "parser.rl"
{
- const char *np;
+ char *np;
json->current_nesting++;
np = JSON_parse_object(json, p, pe, result);
json->current_nesting--;
@@ -780,7 +780,7 @@ static const int JSON_integer_en_main = 1;
#line 289 "parser.rl"
-static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -876,7 +876,7 @@ static const int JSON_float_en_main = 1;
#line 320 "parser.rl"
-static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -1039,7 +1039,7 @@ static const int JSON_array_en_main = 1;
#line 369 "parser.rl"
-static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
VALUE array_class = json->array_class;
@@ -1099,7 +1099,7 @@ tr2:
#line 346 "parser.rl"
{
VALUE v = Qnil;
- const char *np = JSON_parse_value(json, p, pe, &v);
+ char *np = JSON_parse_value(json, p, pe, &v);
if (np == NULL) {
p--; {p++; cs = 3; goto _out;}
} else {
@@ -1289,9 +1289,9 @@ case 16:
}
}
-static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd)
+static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
{
- const char *p = string, *pe = string, *unescape;
+ char *p = string, *pe = string, *unescape;
int unescape_len;
while (pe < stringEnd) {
@@ -1383,9 +1383,11 @@ match_i(VALUE regexp, VALUE klass, VALUE memo)
return ST_CONTINUE;
}
-static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
+ VALUE match_string;
+
*result = rb_str_buf_new(0);
#line 1394 "parser.c"
@@ -1517,9 +1519,7 @@ case 7:
#line 505 "parser.rl"
- if (json->create_additions) {
- VALUE match_string = json->match_string;
- if (RTEST(match_string)) {
+ if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
VALUE memo = rb_ary_new2(2);
rb_ary_push(memo, *result);
@@ -1528,7 +1528,6 @@ case 7:
if (RTEST(klass)) {
*result = rb_funcall(klass, i_json_create, 1, *result);
}
- }
}
if (json->symbolize_names && json->parsing_name) {
@@ -1555,7 +1554,7 @@ case 7:
static VALUE convert_encoding(VALUE source)
{
- const char *ptr = RSTRING_PTR(source);
+ char *ptr = RSTRING_PTR(source);
long len = RSTRING_LEN(source);
if (len < 2) {
rb_raise(eParserError, "A JSON text must at least contain two octets!");
@@ -1594,12 +1593,15 @@ static VALUE convert_encoding(VALUE source)
return source;
}
-static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v)
-{
- // store reference to v in a Ruby instVar to keep v alive
- // without using a gc_mark function in Data_Wrap_Struct calls
- rb_iv_set(json->dwrapped_parser, iv_name, v);
-}
+#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 => {})
@@ -1630,20 +1632,14 @@ static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v
* parsing single JSON values instead of documents is possible.
*
*/
-
-static int init_count = 0;
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE source, opts;
-
GET_PARSER_INIT;
- init_count += 1;
-#ifndef MAGLEV
- if (json->Vsource) {
+ if (RTEST(json->Vsource)) {
rb_raise(rb_eTypeError, "already initialized instance");
}
-#endif
rb_scan_args(argc, argv, "11", &source, &opts);
if (!NIL_P(opts)) {
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
@@ -1689,34 +1685,37 @@ 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));
}
- parser_iv_set(json, "@create_id", json->create_id);
tmp = ID2SYM(i_object_class);
if (option_given_p(opts, tmp)) {
- json->object_class = rb_hash_aref(opts, tmp);
- parser_iv_set(json, "@object_class", json->object_class);
+ PARSER_SET_REFERENCE(json, object_class, rb_hash_aref(opts, tmp));
+ } else {
+ 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_iv_set(json, "@array_class", json->array_class);
+ PARSER_SET_REFERENCE(json, array_class, rb_hash_aref(opts, tmp));
+ } else {
+ 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_iv_set(json, "@match_string", json->match_string);
+ PARSER_SET_REFERENCE(json, match_string, RTEST(match_string) ? match_string : Qnil);
+ } else {
+ 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_iv_set(json, "@create_id", json->create_id);
+ PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0));
+ json->object_class = Qnil;
+ json->array_class = Qnil;
}
if (!json->quirks_mode) {
source = convert_encoding(StringValue(source));
@@ -1724,13 +1723,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_iv_set(json, "@vsource", json->Vsource);
+ 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;
@@ -1738,12 +1736,12 @@ 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)
{
- const char *p, *pe;
+ char *p, *pe;
int cs = EVIL;
VALUE result = Qnil;
GET_PARSER;
@@ -1754,7 +1752,7 @@ static VALUE cParser_parse_strict(VALUE self)
cs = JSON_start;
}
-#line 736 "parser.rl"
+#line 746 "parser.rl"
p = json->source;
pe = p + json->len;
@@ -1814,18 +1812,18 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 715 "parser.rl"
+#line 725 "parser.rl"
{
- const char *np;
+ char *np;
json->current_nesting = 1;
np = JSON_parse_array(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
}
goto st10;
tr4:
-#line 708 "parser.rl"
+#line 718 "parser.rl"
{
- const char *np;
+ char *np;
json->current_nesting = 1;
np = JSON_parse_object(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1892,7 +1890,7 @@ case 9:
_out: {}
}
-#line 739 "parser.rl"
+#line 749 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1904,7 +1902,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;
@@ -1912,7 +1910,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)
@@ -1923,16 +1921,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;
@@ -1966,7 +1964,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;}
@@ -1976,7 +1974,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;
@@ -2065,7 +2063,7 @@ case 9:
_out: {}
}
-#line 777 "parser.rl"
+#line 787 "parser.rl"
if (cs >= JSON_quirks_mode_first_final && p == pe) {
return result;
@@ -2107,7 +2105,14 @@ static JSON_Parser *JSON_allocate()
return json;
}
-/* deleted JSON_mark function */
+static void JSON_mark(JSON_Parser *json)
+{
+ rb_gc_mark_maybe(json->Vsource);
+ rb_gc_mark_maybe(json->create_id);
+ rb_gc_mark_maybe(json->object_class);
+ rb_gc_mark_maybe(json->array_class);
+ rb_gc_mark_maybe(json->match_string);
+}
static void JSON_free(JSON_Parser *json)
{
@@ -2117,9 +2122,7 @@ static void JSON_free(JSON_Parser *json)
static VALUE cJSON_parser_s_allocate(VALUE klass)
{
JSON_Parser *json = JSON_allocate();
- VALUE data_obj = Data_Wrap_Struct(klass, NULL, JSON_free, json);
- json->dwrapped_parser = data_obj;
- return data_obj;
+ 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 cf3e558..fd61a79 100644
--- a/ext/json/ext/parser/parser.h
+++ b/ext/json/ext/parser/parser.h
@@ -36,9 +36,9 @@ typedef unsigned char UTF8; /* typically 8 bits */
typedef struct JSON_ParserStruct {
VALUE dwrapped_parser;
VALUE Vsource;
- const char *source;
+ char *source;
long len;
- const char *memo;
+ char *memo;
VALUE create_id;
int max_nesting;
int current_nesting;
@@ -64,13 +64,13 @@ typedef struct JSON_ParserStruct {
static UTF32 unescape_unicode(const unsigned char *p);
static int convert_UTF32_to_UTF8(char *buf, UTF32 ch);
-static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
-static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
-static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
-static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
-static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
-static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd);
-static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result);
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd);
+static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
static VALUE convert_encoding(VALUE source);
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
static VALUE cParser_parse(VALUE self);
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index 5de6118..2a2cc89 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -116,7 +116,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
action parse_value {
VALUE v = Qnil;
- const char *np = JSON_parse_value(json, fpc, pe, &v);
+ char *np = JSON_parse_value(json, fpc, pe, &v);
if (np == NULL) {
fhold; fbreak;
} else {
@@ -130,7 +130,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
}
action parse_name {
- const char *np;
+ char *np;
json->parsing_name = 1;
np = JSON_parse_string(json, fpc, pe, &last_name);
json->parsing_name = 0;
@@ -149,7 +149,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
) @exit;
}%%
-static const char *JSON_parse_object(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
VALUE last_name = Qnil;
@@ -211,7 +211,7 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha
}
}
action parse_string {
- const char *np = JSON_parse_string(json, fpc, pe, result);
+ char *np = JSON_parse_string(json, fpc, pe, result);
if (np == NULL) { fhold; fbreak; } else fexec np;
}
@@ -234,7 +234,7 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha
}
action parse_array {
- const char *np;
+ char *np;
json->current_nesting++;
np = JSON_parse_array(json, fpc, pe, result);
json->current_nesting--;
@@ -242,7 +242,7 @@ static const char *JSON_parse_object(JSON_Parser *json, const char *p, const cha
}
action parse_object {
- const char *np;
+ char *np;
json->current_nesting++;
np = JSON_parse_object(json, fpc, pe, result);
json->current_nesting--;
@@ -264,7 +264,7 @@ main := (
) %*exit;
}%%
-static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -288,7 +288,7 @@ static const char *JSON_parse_value(JSON_Parser *json, const char *p, const char
main := '-'? ('0' | [1-9][0-9]*) (^[0-9]? @exit);
}%%
-static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -319,7 +319,7 @@ static const char *JSON_parse_integer(JSON_Parser *json, const char *p, const ch
) (^[0-9Ee.\-]? @exit );
}%%
-static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
@@ -345,7 +345,7 @@ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char
action parse_value {
VALUE v = Qnil;
- const char *np = JSON_parse_value(json, fpc, pe, &v);
+ char *np = JSON_parse_value(json, fpc, pe, &v);
if (np == NULL) {
fhold; fbreak;
} else {
@@ -368,7 +368,7 @@ static const char *JSON_parse_float(JSON_Parser *json, const char *p, const char
end_array @exit;
}%%
-static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
VALUE array_class = json->array_class;
@@ -389,9 +389,9 @@ static const char *JSON_parse_array(JSON_Parser *json, const char *p, const char
}
}
-static VALUE json_string_unescape(VALUE result, const char *string, const char *stringEnd)
+static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
{
- const char *p = string, *pe = string, *unescape;
+ char *p = string, *pe = string, *unescape;
int unescape_len;
while (pe < stringEnd) {
@@ -493,17 +493,17 @@ match_i(VALUE regexp, VALUE klass, VALUE memo)
return ST_CONTINUE;
}
-static const char *JSON_parse_string(JSON_Parser *json, const char *p, const char *pe, VALUE *result)
+static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
+ VALUE match_string;
+
*result = rb_str_buf_new(0);
%% write init;
json->memo = p;
%% write exec;
- if (json->create_additions) {
- VALUE match_string = json->match_string;
- if (RTEST(match_string)) {
+ if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
VALUE memo = rb_ary_new2(2);
rb_ary_push(memo, *result);
@@ -512,7 +512,6 @@ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const cha
if (RTEST(klass)) {
*result = rb_funcall(klass, i_json_create, 1, *result);
}
- }
}
if (json->symbolize_names && json->parsing_name) {
@@ -539,7 +538,7 @@ static const char *JSON_parse_string(JSON_Parser *json, const char *p, const cha
static VALUE convert_encoding(VALUE source)
{
- const char *ptr = RSTRING_PTR(source);
+ char *ptr = RSTRING_PTR(source);
long len = RSTRING_LEN(source);
if (len < 2) {
rb_raise(eParserError, "A JSON text must at least contain two octets!");
@@ -578,12 +577,15 @@ static VALUE convert_encoding(VALUE source)
return source;
}
-static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v)
-{
- // store reference to v in a Ruby instVar to keep v alive
- // without using a gc_mark function in Data_Wrap_Struct calls
- rb_iv_set(json->dwrapped_parser, iv_name, v);
-}
+#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 => {})
@@ -611,20 +613,14 @@ static inline void parser_iv_set(JSON_Parser *json, const char* iv_name, VALUE v
* * *object_class*: Defaults to Hash
* * *array_class*: Defaults to Array
*/
-
-static int init_count = 0;
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE source, opts;
-
GET_PARSER_INIT;
- init_count += 1;
-#ifndef MAGLEV
- if (json->Vsource) {
+ if (RTEST(json->Vsource)) {
rb_raise(rb_eTypeError, "already initialized instance");
}
-#endif
rb_scan_args(argc, argv, "11", &source, &opts);
if (!NIL_P(opts)) {
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
@@ -670,34 +666,37 @@ 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));
}
- parser_iv_set(json, "@create_id", json->create_id);
tmp = ID2SYM(i_object_class);
if (option_given_p(opts, tmp)) {
- json->object_class = rb_hash_aref(opts, tmp);
- parser_iv_set(json, "@object_class", json->object_class);
+ PARSER_SET_REFERENCE(json, object_class, rb_hash_aref(opts, tmp));
+ } else {
+ 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_iv_set(json, "@array_class", json->array_class);
+ PARSER_SET_REFERENCE(json, array_class, rb_hash_aref(opts, tmp));
+ } else {
+ 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_iv_set(json, "@match_string", json->match_string);
+ PARSER_SET_REFERENCE(json, match_string, RTEST(match_string) ? match_string : Qnil);
+ } else {
+ 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_iv_set(json, "@create_id", json->create_id);
+ PARSER_SET_REFERENCE(json, create_id, rb_funcall(mJSON, i_create_id, 0));
+ json->object_class = Qnil;
+ json->array_class = Qnil;
}
if (!json->quirks_mode) {
source = convert_encoding(StringValue(source));
@@ -705,8 +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_iv_set(json, "@vsource", json->Vsource);
+ PARSER_SET_REFERENCE(json, Vsource, source);
return self;
}
@@ -739,7 +737,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
static VALUE cParser_parse_strict(VALUE self)
{
- const char *p, *pe;
+ char *p, *pe;
int cs = EVIL;
VALUE result = Qnil;
GET_PARSER;
@@ -827,7 +825,14 @@ static JSON_Parser *JSON_allocate()
return json;
}
-/* deleted JSON_mark function */
+static void JSON_mark(JSON_Parser *json)
+{
+ rb_gc_mark_maybe(json->Vsource);
+ rb_gc_mark_maybe(json->create_id);
+ rb_gc_mark_maybe(json->object_class);
+ rb_gc_mark_maybe(json->array_class);
+ rb_gc_mark_maybe(json->match_string);
+}
static void JSON_free(JSON_Parser *json)
{
@@ -837,9 +842,7 @@ static void JSON_free(JSON_Parser *json)
static VALUE cJSON_parser_s_allocate(VALUE klass)
{
JSON_Parser *json = JSON_allocate();
- VALUE data_obj = Data_Wrap_Struct(klass, NULL, JSON_free, json);
- json->dwrapped_parser = data_obj;
- return data_obj;
+ return json->dwrapped_parser = Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
}
/*