diff options
32 files changed, 263 insertions, 189 deletions
@@ -156,6 +156,7 @@ task :version do puts m File.open(File.join('lib', 'json', 'version.rb'), 'w') do |v| v.puts <<EOT +# frozen_string_literal: false module JSON # JSON version VERSION = '#{PKG_VERSION}' @@ -361,6 +362,7 @@ else sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2" end src = File.read("parser.c").gsub(/[ \t]+$/, '') + src.gsub!(/^static const int (JSON_.*=.*);$/, 'enum {\1};') File.open("parser.c", "w") {|f| f.print src} end end diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb index ae4f861..2addc53 100644 --- a/ext/json/ext/parser/extconf.rb +++ b/ext/json/ext/parser/extconf.rb @@ -1,3 +1,5 @@ require 'mkmf' +have_func("rb_enc_raise", "ruby.h") + create_makefile 'json/ext/parser' diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 5b2e61c..71292e4 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -3,6 +3,28 @@ #include "../fbuffer/fbuffer.h" #include "parser.h" +#if defined HAVE_RUBY_ENCODING_H +# define EXC_ENCODING rb_utf8_encoding(), +# ifndef HAVE_RB_ENC_RAISE +static void +enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...) +{ + va_list args; + VALUE mesg; + + va_start(args, fmt); + mesg = rb_enc_vsprintf(enc, fmt, args); + va_end(args); + + rb_exc_raise(rb_exc_new3(exc, mesg)); +} +# define rb_enc_raise enc_raise +# endif +#else +# define EXC_ENCODING /* nothing */ +# define rb_enc_raise rb_raise +#endif + /* unicode */ static const char digit_values[256] = { @@ -70,8 +92,6 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) #ifdef HAVE_RUBY_ENCODING_H static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8; static ID i_encoding, i_encode; -#else -static ID i_iconv; #endif static VALUE mJSON, mExt, cParser, eParserError, eNestingError; @@ -83,19 +103,19 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_match_string, i_aset, i_aref, i_leftshift; -#line 109 "parser.rl" +#line 129 "parser.rl" -#line 91 "parser.c" -static const int JSON_object_start = 1; -static const int JSON_object_first_final = 27; -static const int JSON_object_error = 0; +#line 111 "parser.c" +enum {JSON_object_start = 1}; +enum {JSON_object_first_final = 27}; +enum {JSON_object_error = 0}; -static const int JSON_object_en_main = 1; +enum {JSON_object_en_main = 1}; -#line 150 "parser.rl" +#line 170 "parser.rl" static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -111,14 +131,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class); -#line 115 "parser.c" +#line 135 "parser.c" { cs = JSON_object_start; } -#line 165 "parser.rl" +#line 185 "parser.rl" -#line 122 "parser.c" +#line 142 "parser.c" { if ( p == pe ) goto _test_eof; @@ -146,7 +166,7 @@ case 2: goto st2; goto st0; tr2: -#line 132 "parser.rl" +#line 152 "parser.rl" { char *np; json->parsing_name = 1; @@ -159,7 +179,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 163 "parser.c" +#line 183 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -226,7 +246,7 @@ case 8: goto st8; goto st0; tr11: -#line 117 "parser.rl" +#line 137 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); @@ -246,7 +266,7 @@ st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 250 "parser.c" +#line 270 "parser.c" switch( (*p) ) { case 13: goto st9; case 32: goto st9; @@ -335,14 +355,14 @@ case 18: goto st9; goto st18; tr4: -#line 140 "parser.rl" +#line 160 "parser.rl" { p--; {p++; cs = 27; goto _out;} } goto st27; st27: if ( ++p == pe ) goto _test_eof27; case 27: -#line 346 "parser.c" +#line 366 "parser.c" goto st0; st19: if ( ++p == pe ) @@ -440,7 +460,7 @@ case 26: _out: {} } -#line 166 "parser.rl" +#line 186 "parser.rl" if (cs >= JSON_object_first_final) { if (json->create_additions) { @@ -465,15 +485,15 @@ case 26: -#line 469 "parser.c" -static const int JSON_value_start = 1; -static const int JSON_value_first_final = 29; -static const int JSON_value_error = 0; +#line 489 "parser.c" +enum {JSON_value_start = 1}; +enum {JSON_value_first_final = 29}; +enum {JSON_value_error = 0}; -static const int JSON_value_en_main = 1; +enum {JSON_value_en_main = 1}; -#line 270 "parser.rl" +#line 290 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -481,14 +501,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 485 "parser.c" +#line 505 "parser.c" { cs = JSON_value_start; } -#line 277 "parser.rl" +#line 297 "parser.rl" -#line 492 "parser.c" +#line 512 "parser.c" { if ( p == pe ) goto _test_eof; @@ -522,14 +542,14 @@ st0: cs = 0; goto _out; tr2: -#line 218 "parser.rl" +#line 238 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;} } goto st29; tr3: -#line 223 "parser.rl" +#line 243 "parser.rl" { char *np; if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) { @@ -538,7 +558,7 @@ tr3: {p = (( p + 10))-1;} p--; {p++; cs = 29; goto _out;} } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } np = JSON_parse_float(json, p, pe, result); @@ -549,7 +569,7 @@ tr3: } goto st29; tr7: -#line 241 "parser.rl" +#line 261 "parser.rl" { char *np; json->current_nesting++; @@ -559,7 +579,7 @@ tr7: } goto st29; tr11: -#line 249 "parser.rl" +#line 269 "parser.rl" { char *np; json->current_nesting++; @@ -569,39 +589,39 @@ tr11: } goto st29; tr25: -#line 211 "parser.rl" +#line 231 "parser.rl" { if (json->allow_nan) { *result = CInfinity; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); } } goto st29; tr27: -#line 204 "parser.rl" +#line 224 "parser.rl" { if (json->allow_nan) { *result = CNaN; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); } } goto st29; tr31: -#line 198 "parser.rl" +#line 218 "parser.rl" { *result = Qfalse; } goto st29; tr34: -#line 195 "parser.rl" +#line 215 "parser.rl" { *result = Qnil; } goto st29; tr37: -#line 201 "parser.rl" +#line 221 "parser.rl" { *result = Qtrue; } @@ -610,9 +630,9 @@ st29: if ( ++p == pe ) goto _test_eof29; case 29: -#line 257 "parser.rl" +#line 277 "parser.rl" { p--; {p++; cs = 29; goto _out;} } -#line 616 "parser.c" +#line 636 "parser.c" switch( (*p) ) { case 13: goto st29; case 32: goto st29; @@ -853,7 +873,7 @@ case 28: _out: {} } -#line 278 "parser.rl" +#line 298 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -863,15 +883,15 @@ case 28: } -#line 867 "parser.c" -static const int JSON_integer_start = 1; -static const int JSON_integer_first_final = 3; -static const int JSON_integer_error = 0; +#line 887 "parser.c" +enum {JSON_integer_start = 1}; +enum {JSON_integer_first_final = 3}; +enum {JSON_integer_error = 0}; -static const int JSON_integer_en_main = 1; +enum {JSON_integer_en_main = 1}; -#line 294 "parser.rl" +#line 314 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -879,15 +899,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 883 "parser.c" +#line 903 "parser.c" { cs = JSON_integer_start; } -#line 301 "parser.rl" +#line 321 "parser.rl" json->memo = p; -#line 891 "parser.c" +#line 911 "parser.c" { if ( p == pe ) goto _test_eof; @@ -921,14 +941,14 @@ case 3: goto st0; goto tr4; tr4: -#line 291 "parser.rl" +#line 311 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 932 "parser.c" +#line 952 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -947,7 +967,7 @@ case 5: _out: {} } -#line 303 "parser.rl" +#line 323 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -962,15 +982,15 @@ case 5: } -#line 966 "parser.c" -static const int JSON_float_start = 1; -static const int JSON_float_first_final = 8; -static const int JSON_float_error = 0; +#line 986 "parser.c" +enum {JSON_float_start = 1}; +enum {JSON_float_first_final = 8}; +enum {JSON_float_error = 0}; -static const int JSON_float_en_main = 1; +enum {JSON_float_en_main = 1}; -#line 328 "parser.rl" +#line 348 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -978,15 +998,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 982 "parser.c" +#line 1002 "parser.c" { cs = JSON_float_start; } -#line 335 "parser.rl" +#line 355 "parser.rl" json->memo = p; -#line 990 "parser.c" +#line 1010 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1044,14 +1064,14 @@ case 8: goto st0; goto tr9; tr9: -#line 322 "parser.rl" +#line 342 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 1055 "parser.c" +#line 1075 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1112,7 +1132,7 @@ case 7: _out: {} } -#line 337 "parser.rl" +#line 357 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1128,15 +1148,15 @@ case 7: -#line 1132 "parser.c" -static const int JSON_array_start = 1; -static const int JSON_array_first_final = 17; -static const int JSON_array_error = 0; +#line 1152 "parser.c" +enum {JSON_array_start = 1}; +enum {JSON_array_first_final = 17}; +enum {JSON_array_error = 0}; -static const int JSON_array_en_main = 1; +enum {JSON_array_en_main = 1}; -#line 380 "parser.rl" +#line 400 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -1150,14 +1170,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class); -#line 1154 "parser.c" +#line 1174 "parser.c" { cs = JSON_array_start; } -#line 393 "parser.rl" +#line 413 "parser.rl" -#line 1161 "parser.c" +#line 1181 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1196,7 +1216,7 @@ case 2: goto st2; goto st0; tr2: -#line 357 "parser.rl" +#line 377 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); @@ -1216,7 +1236,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1220 "parser.c" +#line 1240 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1316,14 +1336,14 @@ case 12: goto st3; goto st12; tr4: -#line 372 "parser.rl" +#line 392 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1327 "parser.c" +#line 1347 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1379,12 +1399,12 @@ case 16: _out: {} } -#line 394 "parser.rl" +#line 414 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); return NULL; } } @@ -1460,15 +1480,15 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1464 "parser.c" -static const int JSON_string_start = 1; -static const int JSON_string_first_final = 8; -static const int JSON_string_error = 0; +#line 1484 "parser.c" +enum {JSON_string_start = 1}; +enum {JSON_string_first_final = 8}; +enum {JSON_string_error = 0}; -static const int JSON_string_en_main = 1; +enum {JSON_string_en_main = 1}; -#line 493 "parser.rl" +#line 513 "parser.rl" static int @@ -1490,15 +1510,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1494 "parser.c" +#line 1514 "parser.c" { cs = JSON_string_start; } -#line 514 "parser.rl" +#line 534 "parser.rl" json->memo = p; -#line 1502 "parser.c" +#line 1522 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1523,7 +1543,7 @@ case 2: goto st0; goto st2; tr2: -#line 479 "parser.rl" +#line 499 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1534,14 +1554,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 490 "parser.rl" +#line 510 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1545 "parser.c" +#line 1565 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1617,7 +1637,7 @@ case 7: _out: {} } -#line 516 "parser.rl" +#line 536 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1655,12 +1675,7 @@ case 7: static VALUE convert_encoding(VALUE source) { #ifdef HAVE_RUBY_ENCODING_H - VALUE encoding = rb_funcall(source, i_encoding, 0); - if (encoding == CEncoding_ASCII_8BIT) { - FORCE_UTF8(source); - } else { - source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8); - } + source = rb_str_conv_enc(source, NULL, rb_utf8_encoding()); #endif return source; } @@ -1700,12 +1715,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) if (json->Vsource) { rb_raise(rb_eTypeError, "already initialized instance"); } +#ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH + rb_scan_args(argc, argv, "1:", &source, &opts); +#else rb_scan_args(argc, argv, "11", &source, &opts); +#endif if (!NIL_P(opts)) { +#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash"); if (NIL_P(opts)) { rb_raise(rb_eArgError, "opts needs to be like a hash"); } else { +#endif VALUE tmp = ID2SYM(i_max_nesting); if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); @@ -1766,7 +1787,9 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->match_string = Qnil; } +#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH } +#endif } else { json->max_nesting = 100; json->allow_nan = 0; @@ -1775,7 +1798,6 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->object_class = Qnil; json->array_class = Qnil; } - source = rb_convert_type(source, T_STRING, "String", "to_str"); source = convert_encoding(StringValue(source)); json->current_nesting = 0; StringValue(source); @@ -1786,15 +1808,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1790 "parser.c" -static const int JSON_start = 1; -static const int JSON_first_final = 10; -static const int JSON_error = 0; +#line 1812 "parser.c" +enum {JSON_start = 1}; +enum {JSON_first_final = 10}; +enum {JSON_error = 0}; -static const int JSON_en_main = 1; +enum {JSON_en_main = 1}; -#line 698 "parser.rl" +#line 720 "parser.rl" /* @@ -1811,16 +1833,16 @@ static VALUE cParser_parse(VALUE self) GET_PARSER; -#line 1815 "parser.c" +#line 1837 "parser.c" { cs = JSON_start; } -#line 714 "parser.rl" +#line 736 "parser.rl" p = json->source; pe = p + json->len; -#line 1824 "parser.c" +#line 1846 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1854,7 +1876,7 @@ st0: cs = 0; goto _out; tr2: -#line 690 "parser.rl" +#line 712 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1864,7 +1886,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1868 "parser.c" +#line 1890 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1953,12 +1975,12 @@ case 9: _out: {} } -#line 717 "parser.rl" +#line 739 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); return Qnil; } } @@ -2056,8 +2078,6 @@ void Init_parser(void) CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit")); i_encoding = rb_intern("encoding"); i_encode = rb_intern("encode"); -#else - i_iconv = rb_intern("iconv"); #endif } diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index f3933cb..e8ad426 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -1,6 +1,28 @@ #include "../fbuffer/fbuffer.h" #include "parser.h" +#if defined HAVE_RUBY_ENCODING_H +# define EXC_ENCODING rb_utf8_encoding(), +# ifndef HAVE_RB_ENC_RAISE +static void +enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...) +{ + va_list args; + VALUE mesg; + + va_start(args, fmt); + mesg = rb_enc_vsprintf(enc, fmt, args); + va_end(args); + + rb_exc_raise(rb_exc_new3(exc, mesg)); +} +# define rb_enc_raise enc_raise +# endif +#else +# define EXC_ENCODING /* nothing */ +# define rb_enc_raise rb_raise +#endif + /* unicode */ static const char digit_values[256] = { @@ -68,8 +90,6 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch) #ifdef HAVE_RUBY_ENCODING_H static VALUE CEncoding_ASCII_8BIT, CEncoding_UTF_8; static ID i_encoding, i_encode; -#else -static ID i_iconv; #endif static VALUE mJSON, mExt, cParser, eParserError, eNestingError; @@ -205,14 +225,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (json->allow_nan) { *result = CNaN; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2); } } action parse_infinity { if (json->allow_nan) { *result = CInfinity; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8); } } action parse_string { @@ -228,7 +248,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu fexec p + 10; fhold; fbreak; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); } } np = JSON_parse_float(json, fpc, pe, result); @@ -395,7 +415,7 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul if(cs >= JSON_array_first_final) { return p + 1; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); return NULL; } } @@ -550,12 +570,7 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu static VALUE convert_encoding(VALUE source) { #ifdef HAVE_RUBY_ENCODING_H - VALUE encoding = rb_funcall(source, i_encoding, 0); - if (encoding == CEncoding_ASCII_8BIT) { - FORCE_UTF8(source); - } else { - source = rb_funcall(source, i_encode, 1, CEncoding_UTF_8); - } + source = rb_str_conv_enc(source, NULL, rb_utf8_encoding()); #endif return source; } @@ -595,12 +610,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) if (json->Vsource) { rb_raise(rb_eTypeError, "already initialized instance"); } +#ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH + rb_scan_args(argc, argv, "1:", &source, &opts); +#else rb_scan_args(argc, argv, "11", &source, &opts); +#endif if (!NIL_P(opts)) { +#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash"); if (NIL_P(opts)) { rb_raise(rb_eArgError, "opts needs to be like a hash"); } else { +#endif VALUE tmp = ID2SYM(i_max_nesting); if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); @@ -661,7 +682,9 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } else { json->match_string = Qnil; } +#ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH } +#endif } else { json->max_nesting = 100; json->allow_nan = 0; @@ -670,7 +693,6 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->object_class = Qnil; json->array_class = Qnil; } - source = rb_convert_type(source, T_STRING, "String", "to_str"); source = convert_encoding(StringValue(source)); json->current_nesting = 0; StringValue(source); @@ -718,7 +740,7 @@ static VALUE cParser_parse(VALUE self) if (cs >= JSON_first_final && p == pe) { return result; } else { - rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p); + rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p); return Qnil; } } @@ -816,8 +838,6 @@ void Init_parser(void) CEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit")); i_encoding = rb_intern("encoding"); i_encode = rb_intern("encode"); -#else - i_iconv = rb_intern("iconv"); #endif } diff --git a/lib/json.rb b/lib/json.rb index 24aa385..b5a6912 100644 --- a/lib/json.rb +++ b/lib/json.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'json/common' ## diff --git a/lib/json/add/bigdecimal.rb b/lib/json/add/bigdecimal.rb index 0ef69f1..539daee 100644 --- a/lib/json/add/bigdecimal.rb +++ b/lib/json/add/bigdecimal.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/complex.rb b/lib/json/add/complex.rb index 2723f60..28ef734 100644 --- a/lib/json/add/complex.rb +++ b/lib/json/add/complex.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end @@ -25,4 +26,4 @@ class Complex def to_json(*) as_json.to_json end -end
\ No newline at end of file +end diff --git a/lib/json/add/core.rb b/lib/json/add/core.rb index 77d9dc0..bfb017c 100644 --- a/lib/json/add/core.rb +++ b/lib/json/add/core.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false # This file requires the implementations of ruby core's custom objects for # serialisation/deserialisation. diff --git a/lib/json/add/date.rb b/lib/json/add/date.rb index 4288237..6a9f16e 100644 --- a/lib/json/add/date.rb +++ b/lib/json/add/date.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/date_time.rb b/lib/json/add/date_time.rb index 5ea42ea..0edeeda 100644 --- a/lib/json/add/date_time.rb +++ b/lib/json/add/date_time.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/exception.rb b/lib/json/add/exception.rb index e6ad257..d6ecfed 100644 --- a/lib/json/add/exception.rb +++ b/lib/json/add/exception.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/ostruct.rb b/lib/json/add/ostruct.rb index da81e10..b8bf8ed 100644 --- a/lib/json/add/ostruct.rb +++ b/lib/json/add/ostruct.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/range.rb b/lib/json/add/range.rb index e61e553..d9c97dd 100644 --- a/lib/json/add/range.rb +++ b/lib/json/add/range.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/rational.rb b/lib/json/add/rational.rb index ee39c20..356940b 100644 --- a/lib/json/add/rational.rb +++ b/lib/json/add/rational.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/regexp.rb b/lib/json/add/regexp.rb index 2fcbb6f..df945ba 100644 --- a/lib/json/add/regexp.rb +++ b/lib/json/add/regexp.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/struct.rb b/lib/json/add/struct.rb index 6847cde..2180207 100644 --- a/lib/json/add/struct.rb +++ b/lib/json/add/struct.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/symbol.rb b/lib/json/add/symbol.rb index 03dc9a5..1f07bf7 100644 --- a/lib/json/add/symbol.rb +++ b/lib/json/add/symbol.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/add/time.rb b/lib/json/add/time.rb index d983467..d0cb97d 100644 --- a/lib/json/add/time.rb +++ b/lib/json/add/time.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end diff --git a/lib/json/common.rb b/lib/json/common.rb index 55908f8..b53041b 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'json/version' require 'json/generic_object' diff --git a/lib/json/generic_object.rb b/lib/json/generic_object.rb index 8b8fd53..6c8f039 100644 --- a/lib/json/generic_object.rb +++ b/lib/json/generic_object.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'ostruct' module JSON @@ -47,14 +48,6 @@ module JSON table end - def [](name) - table[name.to_sym] - end - - def []=(name, value) - __send__ "#{name}=", value - end - def |(other) self.class[other.to_hash.merge(to_hash)] end diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 98bc369..ccb6fe4 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false module JSON MAP = { "\x0" => '\u0000', diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index f02f408..b907236 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'strscan' module JSON diff --git a/lib/json/version.rb b/lib/json/version.rb index b91ccd7..520a8ad 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false module JSON # JSON version VERSION = '2.0.0' diff --git a/tests/json_addition_test.rb b/tests/json_addition_test.rb index 128f34a..a028e0f 100644 --- a/tests/json_addition_test.rb +++ b/tests/json_addition_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' require 'json/add/core' require 'json/add/complex' @@ -108,7 +109,7 @@ class JSONAdditionTest < Test::Unit::TestCase c = C.new assert !C.json_creatable? json = generate(c) - assert_raises(ArgumentError, NameError) { parse(json, :create_additions => true) } + assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) } end def test_raw_strings @@ -147,7 +148,7 @@ class JSONAdditionTest < Test::Unit::TestCase assert_equal s, JSON(JSON(s), :create_additions => true) struct = Struct.new :foo, :bar s = struct.new 4711, 'foot' - assert_raises(JSONError) { JSON(s) } + assert_raise(JSONError) { JSON(s) } begin raise TypeError, "test me" rescue TypeError => e diff --git a/tests/json_common_interface_test.rb b/tests/json_common_interface_test.rb index 38136fa..b2051d4 100644 --- a/tests/json_common_interface_test.rb +++ b/tests/json_common_interface_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' require 'stringio' require 'tempfile' @@ -100,8 +101,8 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' assert_equal too_deep, dump(eval(too_deep)) assert_kind_of String, Marshal.dump(eval(too_deep)) - assert_raises(ArgumentError) { dump(eval(too_deep), 100) } - assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 100) } + assert_raise(ArgumentError) { dump(eval(too_deep), 100) } + assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) } assert_equal too_deep, dump(eval(too_deep), 101) assert_kind_of String, Marshal.dump(eval(too_deep), 101) output = StringIO.new diff --git a/tests/json_encoding_test.rb b/tests/json_encoding_test.rb index d983e12..86c4df8 100644 --- a/tests/json_encoding_test.rb +++ b/tests/json_encoding_test.rb @@ -1,4 +1,6 @@ # encoding: utf-8 +#frozen_string_literal: false + require 'test_helper' class JSONEncodingTest < Test::Unit::TestCase diff --git a/tests/json_ext_parser_test.rb b/tests/json_ext_parser_test.rb index ade9784..c5a030e 100644 --- a/tests/json_ext_parser_test.rb +++ b/tests/json_ext_parser_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' class JSONExtParserTest < Test::Unit::TestCase diff --git a/tests/json_fixtures_test.rb b/tests/json_fixtures_test.rb index 6681b8d..01954fe 100644 --- a/tests/json_fixtures_test.rb +++ b/tests/json_fixtures_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' class JSONFixturesTest < Test::Unit::TestCase @@ -22,7 +23,7 @@ class JSONFixturesTest < Test::Unit::TestCase def test_failing for name, source in @failed - assert_raises(JSON::ParserError, JSON::NestingError, + assert_raise(JSON::ParserError, JSON::NestingError, "Did not fail for fixture '#{name}': #{source.inspect}") do JSON.parse(source) end diff --git a/tests/json_generator_test.rb b/tests/json_generator_test.rb index 7087471..18b0833 100644 --- a/tests/json_generator_test.rb +++ b/tests/json_generator_test.rb @@ -118,12 +118,12 @@ EOT assert s[:check_circular?] h = { 1=>2 } h[3] = h - assert_raises(JSON::NestingError) { generate(h) } - assert_raises(JSON::NestingError) { generate(h, s) } + assert_raise(JSON::NestingError) { generate(h) } + assert_raise(JSON::NestingError) { generate(h, s) } s = JSON.state.new a = [ 1, 2 ] a << a - assert_raises(JSON::NestingError) { generate(a, s) } + assert_raise(JSON::NestingError) { generate(a, s) } assert s.check_circular? assert s[:check_circular?] end @@ -177,34 +177,34 @@ EOT end def test_allow_nan - assert_raises(GeneratorError) { generate([JSON::NaN]) } + assert_raise(GeneratorError) { generate([JSON::NaN]) } assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) - assert_raises(GeneratorError) { fast_generate([JSON::NaN]) } - assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) } + assert_raise(GeneratorError) { fast_generate([JSON::NaN]) } + assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) } assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) - assert_raises(GeneratorError) { generate([JSON::Infinity]) } + assert_raise(GeneratorError) { generate([JSON::Infinity]) } assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) - assert_raises(GeneratorError) { fast_generate([JSON::Infinity]) } - assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) } + assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) } assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) - assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) } + assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) } assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) - assert_raises(GeneratorError) { fast_generate([JSON::MinusInfinity]) } - assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } + assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) } + assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) end def test_depth ary = []; ary << ary assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth - assert_raises(JSON::NestingError) { generate(ary) } + assert_raise(JSON::NestingError) { generate(ary) } assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth - assert_raises(JSON::NestingError) { JSON.pretty_generate(ary) } + assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) } assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth s = JSON.state.new assert_equal 0, s.depth - assert_raises(JSON::NestingError) { ary.to_json(s) } + assert_raise(JSON::NestingError) { ary.to_json(s) } assert_equal 100, s.depth end @@ -330,8 +330,8 @@ EOT def test_nesting too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' too_deep_ary = eval too_deep - assert_raises(JSON::NestingError) { generate too_deep_ary } - assert_raises(JSON::NestingError) { generate too_deep_ary, :max_nesting => 100 } + assert_raise(JSON::NestingError) { generate too_deep_ary } + assert_raise(JSON::NestingError) { generate too_deep_ary, :max_nesting => 100 } ok = generate too_deep_ary, :max_nesting => 101 assert_equal too_deep, ok ok = generate too_deep_ary, :max_nesting => nil diff --git a/tests/json_generic_object_test.rb b/tests/json_generic_object_test.rb index 171fdb8..82742dc 100644 --- a/tests/json_generic_object_test.rb +++ b/tests/json_generic_object_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' class JSONGenericObjectTest < Test::Unit::TestCase diff --git a/tests/json_parser_test.rb b/tests/json_parser_test.rb index a5793f9..5120f09 100644 --- a/tests/json_parser_test.rb +++ b/tests/json_parser_test.rb @@ -1,4 +1,5 @@ # encoding: utf-8 +# frozen_string_literal: false require 'test_helper' require 'stringio' @@ -19,6 +20,16 @@ class JSONParserTest < Test::Unit::TestCase assert_equal Encoding::UTF_16, source.encoding end if defined?(Encoding::UTF_16) + def test_error_message_encoding + bug10705 = '[ruby-core:67386] [Bug #10705]' + json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8) + e = assert_raise(JSON::ParserError) { + JSON::Ext::Parser.new(json).parse + } + assert_equal(Encoding::UTF_8, e.message.encoding, bug10705) + assert_include(e.message, json, bug10705) + end if defined?(Encoding::UTF_8) and defined?(JSON::Ext::Parser) + def test_parsing parser = JSON::Parser.new('"test"') assert_equal 'test', parser.parse @@ -52,7 +63,7 @@ class JSONParserTest < Test::Unit::TestCase assert_equal({ "a" => nil }, parse('{"a":null}')) assert_equal({ "a" => false }, parse('{ "a" : false } ')) assert_equal({ "a" => false }, parse('{"a":false}')) - assert_raises(JSON::ParserError) { parse('{false}') } + assert_raise(JSON::ParserError) { parse('{false}') } assert_equal({ "a" => true }, parse('{"a":true}')) assert_equal({ "a" => true }, parse(' { "a" : true } ')) assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) @@ -64,10 +75,10 @@ class JSONParserTest < Test::Unit::TestCase end def test_parse_numbers - assert_raises(JSON::ParserError) { parse('+23.2') } - assert_raises(JSON::ParserError) { parse('+23') } - assert_raises(JSON::ParserError) { parse('.23') } - assert_raises(JSON::ParserError) { parse('023') } + assert_raise(JSON::ParserError) { parse('+23.2') } + assert_raise(JSON::ParserError) { parse('+23') } + assert_raise(JSON::ParserError) { parse('.23') } + assert_raise(JSON::ParserError) { parse('023') } assert_equal 23, parse('23') assert_equal -23, parse('-23') assert_equal_float 3.141, parse('3.141') @@ -78,11 +89,11 @@ class JSONParserTest < Test::Unit::TestCase assert_equal_float 3.141, parse('3141.0E-3') assert_equal_float -3.141, parse('-3141.0e-3') assert_equal_float -3.141, parse('-3141e-3') - assert_raises(ParserError) { parse('NaN') } + assert_raise(ParserError) { parse('NaN') } assert parse('NaN', :allow_nan => true).nan? - assert_raises(ParserError) { parse('Infinity') } + assert_raise(ParserError) { parse('Infinity') } assert_equal 1.0/0, parse('Infinity', :allow_nan => true) - assert_raises(ParserError) { parse('-Infinity') } + assert_raise(ParserError) { parse('-Infinity') } assert_equal -1.0/0, parse('-Infinity', :allow_nan => true) end @@ -165,19 +176,19 @@ class JSONParserTest < Test::Unit::TestCase end def test_some_wrong_inputs - assert_raises(ParserError) { parse('[] bla') } - assert_raises(ParserError) { parse('[] 1') } - assert_raises(ParserError) { parse('[] []') } - assert_raises(ParserError) { parse('[] {}') } - assert_raises(ParserError) { parse('{} []') } - assert_raises(ParserError) { parse('{} {}') } - assert_raises(ParserError) { parse('[NULL]') } - assert_raises(ParserError) { parse('[FALSE]') } - assert_raises(ParserError) { parse('[TRUE]') } - assert_raises(ParserError) { parse('[07] ') } - assert_raises(ParserError) { parse('[0a]') } - assert_raises(ParserError) { parse('[1.]') } - assert_raises(ParserError) { parse(' ') } + assert_raise(ParserError) { parse('[] bla') } + assert_raise(ParserError) { parse('[] 1') } + assert_raise(ParserError) { parse('[] []') } + assert_raise(ParserError) { parse('[] {}') } + assert_raise(ParserError) { parse('{} []') } + assert_raise(ParserError) { parse('{} {}') } + assert_raise(ParserError) { parse('[NULL]') } + assert_raise(ParserError) { parse('[FALSE]') } + assert_raise(ParserError) { parse('[TRUE]') } + assert_raise(ParserError) { parse('[07] ') } + assert_raise(ParserError) { parse('[0a]') } + assert_raise(ParserError) { parse('[1.]') } + assert_raise(ParserError) { parse(' ') } end def test_symbolize_names @@ -212,7 +223,7 @@ EOT * comment */ } EOT - assert_raises(ParserError) { parse(json) } + assert_raise(ParserError) { parse(json) } json = <<EOT { "key1":"value1" /* multi line @@ -221,7 +232,7 @@ EOT and again, throw an Error */ } EOT - assert_raises(ParserError) { parse(json) } + assert_raise(ParserError) { parse(json) } json = <<EOT { "key1":"value1" /*/*/ @@ -233,8 +244,8 @@ EOT def test_nesting too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' too_deep_ary = eval too_deep - assert_raises(JSON::NestingError) { parse too_deep } - assert_raises(JSON::NestingError) { parse too_deep, :max_nesting => 100 } + assert_raise(JSON::NestingError) { parse too_deep } + assert_raise(JSON::NestingError) { parse too_deep, :max_nesting => 100 } ok = parse too_deep, :max_nesting => 101 assert_equal too_deep_ary, ok ok = parse too_deep, :max_nesting => nil diff --git a/tests/json_string_matching_test.rb b/tests/json_string_matching_test.rb index 7fec841..5d55dc3 100644 --- a/tests/json_string_matching_test.rb +++ b/tests/json_string_matching_test.rb @@ -1,3 +1,4 @@ +#frozen_string_literal: false require 'test_helper' require 'time' |