From 48440cb88cc89a162611dda4ed6b3cfc5c80f09e Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 20 Dec 2011 06:47:07 -0600 Subject: Minor tweaks to Java ext for performance. * Avoid full newInstance logic if constructing normal Array or Hash * Skip intermediate RubyString for float and integer parsing * Avoid toString for comparison with "-Infinity" * Avoid repeated class.getName().equals() calls --- java/src/json/ext/Parser.java | 202 ++++++++++++++++++++++-------------------- java/src/json/ext/Parser.rl | 42 +++++---- 2 files changed, 128 insertions(+), 116 deletions(-) diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 0b59c8f..2755a04 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -28,6 +28,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; +import org.jruby.util.ConvertBytes; +import static org.jruby.util.ConvertDouble.DoubleConverter; /** * The JSON::Ext::Parser class. @@ -58,7 +60,7 @@ public class Parser extends RubyObject { private static final int DEFAULT_MAX_NESTING = 19; - private static final String JSON_MINUS_INFINITY = "-Infinity"; + private static final ByteList JSON_MINUS_INFINITY = new ByteList(ByteList.plain("-Infinity")); // constant names in the JSON module containing those values private static final String CONST_NAN = "NaN"; private static final String CONST_INFINITY = "Infinity"; @@ -306,6 +308,7 @@ public class Parser extends RubyObject { private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; + private final DoubleConverter dc; // initialization value for all state variables. // no idea about the origins of this value, ask Flori ;) @@ -317,6 +320,7 @@ public class Parser extends RubyObject { this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); this.decoder = new StringDecoder(context); + this.dc = new DoubleConverter(); } private RaiseException unexpectedToken(int absStart, int absEnd) { @@ -331,11 +335,11 @@ public class Parser extends RubyObject { } -// line 357 "Parser.rl" +// line 361 "Parser.rl" -// line 339 "Parser.java" +// line 343 "Parser.java" private static byte[] init__JSON_value_actions_0() { return new byte [] { @@ -449,7 +453,7 @@ static final int JSON_value_error = 0; static final int JSON_value_en_main = 1; -// line 463 "Parser.rl" +// line 467 "Parser.rl" ParserResult parseValue(int p, int pe) { @@ -457,14 +461,14 @@ static final int JSON_value_en_main = 1; IRubyObject result = null; -// line 461 "Parser.java" +// line 465 "Parser.java" { cs = JSON_value_start; } -// line 470 "Parser.rl" +// line 474 "Parser.rl" -// line 468 "Parser.java" +// line 472 "Parser.java" { int _klen; int _trans = 0; @@ -490,13 +494,13 @@ case 1: while ( _nacts-- > 0 ) { switch ( _JSON_value_actions[_acts++] ) { case 9: -// line 448 "Parser.rl" +// line 452 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 500 "Parser.java" +// line 504 "Parser.java" } } @@ -559,25 +563,25 @@ case 1: switch ( _JSON_value_actions[_acts++] ) { case 0: -// line 365 "Parser.rl" +// line 369 "Parser.rl" { result = getRuntime().getNil(); } break; case 1: -// line 368 "Parser.rl" +// line 372 "Parser.rl" { result = getRuntime().getFalse(); } break; case 2: -// line 371 "Parser.rl" +// line 375 "Parser.rl" { result = getRuntime().getTrue(); } break; case 3: -// line 374 "Parser.rl" +// line 378 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_NAN); @@ -587,7 +591,7 @@ case 1: } break; case 4: -// line 381 "Parser.rl" +// line 385 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_INFINITY); @@ -597,10 +601,10 @@ case 1: } break; case 5: -// line 388 "Parser.rl" +// line 392 "Parser.rl" { if (pe > p + 9 - (parser.quirksMode ? 1 : 0) && - absSubSequence(p, p + 9).toString().equals(JSON_MINUS_INFINITY)) { + absSubSequence(p, p + 9).equals(JSON_MINUS_INFINITY)) { if (parser.allowNaN) { result = getConstant(CONST_MINUS_INFINITY); @@ -626,7 +630,7 @@ case 1: } break; case 6: -// line 414 "Parser.rl" +// line 418 "Parser.rl" { ParserResult res = parseString(p, pe); if (res == null) { @@ -639,7 +643,7 @@ case 1: } break; case 7: -// line 424 "Parser.rl" +// line 428 "Parser.rl" { currentNesting++; ParserResult res = parseArray(p, pe); @@ -654,7 +658,7 @@ case 1: } break; case 8: -// line 436 "Parser.rl" +// line 440 "Parser.rl" { currentNesting++; ParserResult res = parseObject(p, pe); @@ -668,7 +672,7 @@ case 1: } } break; -// line 672 "Parser.java" +// line 676 "Parser.java" } } } @@ -688,7 +692,7 @@ case 5: break; } } -// line 471 "Parser.rl" +// line 475 "Parser.rl" if (cs >= JSON_value_first_final && result != null) { return new ParserResult(result, p); @@ -698,7 +702,7 @@ case 5: } -// line 702 "Parser.java" +// line 706 "Parser.java" private static byte[] init__JSON_integer_actions_0() { return new byte [] { @@ -797,22 +801,22 @@ static final int JSON_integer_error = 0; static final int JSON_integer_en_main = 1; -// line 490 "Parser.rl" +// line 494 "Parser.rl" ParserResult parseInteger(int p, int pe) { int cs = EVIL; -// line 808 "Parser.java" +// line 812 "Parser.java" { cs = JSON_integer_start; } -// line 496 "Parser.rl" +// line 500 "Parser.rl" int memo = p; -// line 816 "Parser.java" +// line 820 "Parser.java" { int _klen; int _trans = 0; @@ -893,13 +897,13 @@ case 1: switch ( _JSON_integer_actions[_acts++] ) { case 0: -// line 484 "Parser.rl" +// line 488 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 903 "Parser.java" +// line 907 "Parser.java" } } } @@ -919,22 +923,19 @@ case 5: break; } } -// line 498 "Parser.rl" +// line 502 "Parser.rl" if (cs < JSON_integer_first_final) { return null; } ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyInteger number = RubyNumeric.str2inum(getRuntime(), expr, 10, true); + RubyInteger number = ConvertBytes.byteListToInum(getRuntime(), num, 10, true); return new ParserResult(number, p + 1); } -// line 938 "Parser.java" +// line 939 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1036,22 +1037,22 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 526 "Parser.rl" +// line 527 "Parser.rl" ParserResult parseFloat(int p, int pe) { int cs = EVIL; -// line 1047 "Parser.java" +// line 1048 "Parser.java" { cs = JSON_float_start; } -// line 532 "Parser.rl" +// line 533 "Parser.rl" int memo = p; -// line 1055 "Parser.java" +// line 1056 "Parser.java" { int _klen; int _trans = 0; @@ -1132,13 +1133,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 517 "Parser.rl" +// line 518 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1142 "Parser.java" +// line 1143 "Parser.java" } } } @@ -1158,22 +1159,19 @@ case 5: break; } } -// line 534 "Parser.rl" +// line 535 "Parser.rl" if (cs < JSON_float_first_final) { return null; } ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyFloat number = RubyNumeric.str2fnum(getRuntime(), expr, true); + RubyFloat number = RubyFloat.newFloat(getRuntime(), dc.parse(num, true, getRuntime().is1_9())); return new ParserResult(number, p + 1); } -// line 1177 "Parser.java" +// line 1175 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1275,7 +1273,7 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 578 "Parser.rl" +// line 576 "Parser.rl" ParserResult parseString(int p, int pe) { @@ -1283,15 +1281,15 @@ static final int JSON_string_en_main = 1; IRubyObject result = null; -// line 1287 "Parser.java" +// line 1285 "Parser.java" { cs = JSON_string_start; } -// line 585 "Parser.rl" +// line 583 "Parser.rl" int memo = p; -// line 1295 "Parser.java" +// line 1293 "Parser.java" { int _klen; int _trans = 0; @@ -1372,7 +1370,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 553 "Parser.rl" +// line 551 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1387,13 +1385,13 @@ case 1: } break; case 1: -// line 566 "Parser.rl" +// line 564 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1397 "Parser.java" +// line 1395 "Parser.java" } } } @@ -1413,7 +1411,7 @@ case 5: break; } } -// line 587 "Parser.rl" +// line 585 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1448,7 +1446,7 @@ case 5: } -// line 1452 "Parser.java" +// line 1450 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1561,7 +1559,7 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 657 "Parser.rl" +// line 655 "Parser.rl" ParserResult parseArray(int p, int pe) { @@ -1574,19 +1572,23 @@ static final int JSON_array_en_main = 1; // this is guaranteed to be a RubyArray due to the earlier // allocator test at OptionsReader#getClass - RubyArray result = - (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + RubyArray result; + if (parser.arrayClass != getRuntime().getArray()) { + result = (RubyArray)parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } else { + result = RubyArray.newArray(getRuntime()); + } -// line 1583 "Parser.java" +// line 1585 "Parser.java" { cs = JSON_array_start; } -// line 674 "Parser.rl" +// line 676 "Parser.rl" -// line 1590 "Parser.java" +// line 1592 "Parser.java" { int _klen; int _trans = 0; @@ -1667,14 +1669,14 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 626 "Parser.rl" +// line 624 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (!parser.arrayClass.getName().equals("Array")) { + if (parser.arrayClass != getRuntime().getArray()) { result.callMethod(context, "<<", res.result); } else { result.append(res.result); @@ -1684,13 +1686,13 @@ case 1: } break; case 1: -// line 641 "Parser.rl" +// line 639 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1694 "Parser.java" +// line 1696 "Parser.java" } } } @@ -1710,7 +1712,7 @@ case 5: break; } } -// line 675 "Parser.rl" +// line 677 "Parser.rl" if (cs >= JSON_array_first_final) { return new ParserResult(result, p + 1); @@ -1720,7 +1722,7 @@ case 5: } -// line 1724 "Parser.java" +// line 1726 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1843,7 +1845,7 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 734 "Parser.rl" +// line 736 "Parser.rl" ParserResult parseObject(int p, int pe) { @@ -1857,19 +1859,23 @@ static final int JSON_object_en_main = 1; // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result = - (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + RubyHash result; + if (parser.objectClass != getRuntime().getHash()) { + result = (RubyHash)parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } else { + result = RubyHash.newHash(getRuntime()); + } -// line 1866 "Parser.java" +// line 1872 "Parser.java" { cs = JSON_object_start; } -// line 752 "Parser.rl" +// line 758 "Parser.rl" -// line 1873 "Parser.java" +// line 1879 "Parser.java" { int _klen; int _trans = 0; @@ -1950,14 +1956,14 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 689 "Parser.rl" +// line 691 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (!parser.objectClass.getName().equals("Hash")) { + if (parser.objectClass != getRuntime().getHash()) { result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } else { result.op_aset(context, lastName, res.result); @@ -1967,7 +1973,7 @@ case 1: } break; case 1: -// line 704 "Parser.rl" +// line 706 "Parser.rl" { ParserResult res = parseString(p, pe); if (res == null) { @@ -1987,13 +1993,13 @@ case 1: } break; case 2: -// line 722 "Parser.rl" +// line 724 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1997 "Parser.java" +// line 2003 "Parser.java" } } } @@ -2013,7 +2019,7 @@ case 5: break; } } -// line 753 "Parser.rl" +// line 759 "Parser.rl" if (cs < JSON_object_first_final) { return null; @@ -2039,7 +2045,7 @@ case 5: } -// line 2043 "Parser.java" +// line 2049 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2143,7 +2149,7 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 811 "Parser.rl" +// line 817 "Parser.rl" public IRubyObject parseStrict() { @@ -2152,16 +2158,16 @@ static final int JSON_en_main = 1; IRubyObject result = null; -// line 2156 "Parser.java" +// line 2162 "Parser.java" { cs = JSON_start; } -// line 819 "Parser.rl" +// line 825 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2165 "Parser.java" +// line 2171 "Parser.java" { int _klen; int _trans = 0; @@ -2242,7 +2248,7 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 783 "Parser.rl" +// line 789 "Parser.rl" { currentNesting = 1; ParserResult res = parseObject(p, pe); @@ -2256,7 +2262,7 @@ case 1: } break; case 1: -// line 795 "Parser.rl" +// line 801 "Parser.rl" { currentNesting = 1; ParserResult res = parseArray(p, pe); @@ -2269,7 +2275,7 @@ case 1: } } break; -// line 2273 "Parser.java" +// line 2279 "Parser.java" } } } @@ -2289,7 +2295,7 @@ case 5: break; } } -// line 822 "Parser.rl" +// line 828 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2299,7 +2305,7 @@ case 5: } -// line 2303 "Parser.java" +// line 2309 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2402,7 +2408,7 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 850 "Parser.rl" +// line 856 "Parser.rl" public IRubyObject parseQuirksMode() { @@ -2411,16 +2417,16 @@ static final int JSON_quirks_mode_en_main = 1; IRubyObject result = null; -// line 2415 "Parser.java" +// line 2421 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 858 "Parser.rl" +// line 864 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2424 "Parser.java" +// line 2430 "Parser.java" { int _klen; int _trans = 0; @@ -2501,7 +2507,7 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 836 "Parser.rl" +// line 842 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { @@ -2513,7 +2519,7 @@ case 1: } } break; -// line 2517 "Parser.java" +// line 2523 "Parser.java" } } } @@ -2533,7 +2539,7 @@ case 5: break; } } -// line 861 "Parser.rl" +// line 867 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index e8cd874..b850b71 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -26,6 +26,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; +import org.jruby.util.ConvertBytes; +import static org.jruby.util.ConvertDouble.DoubleConverter; /** * The JSON::Ext::Parser class. @@ -56,7 +58,7 @@ public class Parser extends RubyObject { private static final int DEFAULT_MAX_NESTING = 19; - private static final String JSON_MINUS_INFINITY = "-Infinity"; + private static final ByteList JSON_MINUS_INFINITY = new ByteList(ByteList.plain("-Infinity")); // constant names in the JSON module containing those values private static final String CONST_NAN = "NaN"; private static final String CONST_INFINITY = "Infinity"; @@ -304,6 +306,7 @@ public class Parser extends RubyObject { private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; + private final DoubleConverter dc; // initialization value for all state variables. // no idea about the origins of this value, ask Flori ;) @@ -315,6 +318,7 @@ public class Parser extends RubyObject { this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); this.decoder = new StringDecoder(context); + this.dc = new DoubleConverter(); } private RaiseException unexpectedToken(int absStart, int absEnd) { @@ -387,7 +391,7 @@ public class Parser extends RubyObject { } action parse_number { if (pe > fpc + 9 - (parser.quirksMode ? 1 : 0) && - absSubSequence(fpc, fpc + 9).toString().equals(JSON_MINUS_INFINITY)) { + absSubSequence(fpc, fpc + 9).equals(JSON_MINUS_INFINITY)) { if (parser.allowNaN) { result = getConstant(CONST_MINUS_INFINITY); @@ -501,10 +505,7 @@ public class Parser extends RubyObject { } ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyInteger number = RubyNumeric.str2inum(getRuntime(), expr, 10, true); + RubyInteger number = ConvertBytes.byteListToInum(getRuntime(), num, 10, true); return new ParserResult(number, p + 1); } @@ -537,10 +538,7 @@ public class Parser extends RubyObject { } ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyFloat number = RubyNumeric.str2fnum(getRuntime(), expr, true); + RubyFloat number = RubyFloat.newFloat(getRuntime(), dc.parse(num, true, getRuntime().is1_9())); return new ParserResult(number, p + 1); } @@ -629,7 +627,7 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - if (!parser.arrayClass.getName().equals("Array")) { + if (parser.arrayClass != getRuntime().getArray()) { result.callMethod(context, "<<", res.result); } else { result.append(res.result); @@ -666,9 +664,13 @@ public class Parser extends RubyObject { // this is guaranteed to be a RubyArray due to the earlier // allocator test at OptionsReader#getClass - RubyArray result = - (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + RubyArray result; + if (parser.arrayClass != getRuntime().getArray()) { + result = (RubyArray)parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } else { + result = RubyArray.newArray(getRuntime()); + } %% write init; %% write exec; @@ -692,7 +694,7 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - if (!parser.objectClass.getName().equals("Hash")) { + if (parser.objectClass != getRuntime().getHash()) { result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } else { result.op_aset(context, lastName, res.result); @@ -744,9 +746,13 @@ public class Parser extends RubyObject { // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result = - (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + RubyHash result; + if (parser.objectClass != getRuntime().getHash()) { + result = (RubyHash)parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } else { + result = RubyHash.newHash(getRuntime()); + } %% write init; %% write exec; -- cgit v1.2.1 From 7e72fc62e670f59edc5ca74f339c1bc264214034 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 20 Dec 2011 07:00:10 -0600 Subject: Use appropriate byteListToInum for Ruby version. --- java/src/json/ext/Parser.rl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index b850b71..02056f8 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -505,7 +505,9 @@ public class Parser extends RubyObject { } ByteList num = absSubSequence(memo, p); - RubyInteger number = ConvertBytes.byteListToInum(getRuntime(), num, 10, true); + RubyInteger number = getRuntime().is1_9() ? + ConvertBytes.byteListToInum19(getRuntime(), num, 10, true) : + ConvertBytes.byteListToInum(getRuntime(), num, 10, true); return new ParserResult(number, p + 1); } -- cgit v1.2.1 From d44d02c2e2f84ce0c57be7819c925c822ce614f3 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 20 Dec 2011 08:30:33 -0600 Subject: Better ByteList subsequence and break up some parse methods. --- java/src/json/ext/Parser.java | 162 ++++++++++++++++++++++++------------------ java/src/json/ext/Parser.rl | 48 +++++++++---- 2 files changed, 126 insertions(+), 84 deletions(-) diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 2755a04..adb227f 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -805,18 +805,25 @@ static final int JSON_integer_en_main = 1; ParserResult parseInteger(int p, int pe) { + int new_p = parseIntegerInternal(p, pe); + if (new_p == -1) return null; + RubyInteger number = createInteger(p, new_p); + return new ParserResult(number, new_p + 1); + } + + int parseIntegerInternal(int p, int pe) { int cs = EVIL; -// line 812 "Parser.java" +// line 819 "Parser.java" { cs = JSON_integer_start; } -// line 500 "Parser.rl" +// line 507 "Parser.rl" int memo = p; -// line 820 "Parser.java" +// line 827 "Parser.java" { int _klen; int _trans = 0; @@ -903,7 +910,7 @@ case 1: { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 907 "Parser.java" +// line 914 "Parser.java" } } } @@ -923,19 +930,25 @@ case 5: break; } } -// line 502 "Parser.rl" +// line 509 "Parser.rl" if (cs < JSON_integer_first_final) { - return null; + return -1; } - ByteList num = absSubSequence(memo, p); - RubyInteger number = ConvertBytes.byteListToInum(getRuntime(), num, 10, true); - return new ParserResult(number, p + 1); + return p; + } + + RubyInteger createInteger(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return runtime.is1_9() ? + ConvertBytes.byteListToInum19(runtime, num, 10, true) : + ConvertBytes.byteListToInum(runtime, num, 10, true); } -// line 939 "Parser.java" +// line 952 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1037,22 +1050,29 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 527 "Parser.rl" +// line 540 "Parser.rl" ParserResult parseFloat(int p, int pe) { + int new_p = parseFloatInternal(p, pe); + if (new_p == -1) return null; + RubyFloat number = createFloat(p, new_p); + return new ParserResult(number, new_p + 1); + } + + int parseFloatInternal(int p, int pe) { int cs = EVIL; -// line 1048 "Parser.java" +// line 1068 "Parser.java" { cs = JSON_float_start; } -// line 533 "Parser.rl" +// line 553 "Parser.rl" int memo = p; -// line 1056 "Parser.java" +// line 1076 "Parser.java" { int _klen; int _trans = 0; @@ -1133,13 +1153,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 518 "Parser.rl" +// line 531 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1143 "Parser.java" +// line 1163 "Parser.java" } } } @@ -1159,19 +1179,23 @@ case 5: break; } } -// line 535 "Parser.rl" +// line 555 "Parser.rl" if (cs < JSON_float_first_final) { - return null; + return -1; } - - ByteList num = absSubSequence(memo, p); - RubyFloat number = RubyFloat.newFloat(getRuntime(), dc.parse(num, true, getRuntime().is1_9())); - return new ParserResult(number, p + 1); + + return p; + } + + RubyFloat createFloat(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return RubyFloat.newFloat(runtime, dc.parse(num, true, runtime.is1_9())); } -// line 1175 "Parser.java" +// line 1199 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1273,7 +1297,7 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 576 "Parser.rl" +// line 600 "Parser.rl" ParserResult parseString(int p, int pe) { @@ -1281,15 +1305,15 @@ static final int JSON_string_en_main = 1; IRubyObject result = null; -// line 1285 "Parser.java" +// line 1309 "Parser.java" { cs = JSON_string_start; } -// line 583 "Parser.rl" +// line 607 "Parser.rl" int memo = p; -// line 1293 "Parser.java" +// line 1317 "Parser.java" { int _klen; int _trans = 0; @@ -1370,7 +1394,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 551 "Parser.rl" +// line 575 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1385,13 +1409,13 @@ case 1: } break; case 1: -// line 564 "Parser.rl" +// line 588 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1395 "Parser.java" +// line 1419 "Parser.java" } } } @@ -1411,7 +1435,7 @@ case 5: break; } } -// line 585 "Parser.rl" +// line 609 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1446,7 +1470,7 @@ case 5: } -// line 1450 "Parser.java" +// line 1474 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1559,7 +1583,7 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 655 "Parser.rl" +// line 679 "Parser.rl" ParserResult parseArray(int p, int pe) { @@ -1581,14 +1605,14 @@ static final int JSON_array_en_main = 1; } -// line 1585 "Parser.java" +// line 1609 "Parser.java" { cs = JSON_array_start; } -// line 676 "Parser.rl" +// line 700 "Parser.rl" -// line 1592 "Parser.java" +// line 1616 "Parser.java" { int _klen; int _trans = 0; @@ -1669,7 +1693,7 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 624 "Parser.rl" +// line 648 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { @@ -1686,13 +1710,13 @@ case 1: } break; case 1: -// line 639 "Parser.rl" +// line 663 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1696 "Parser.java" +// line 1720 "Parser.java" } } } @@ -1712,7 +1736,7 @@ case 5: break; } } -// line 677 "Parser.rl" +// line 701 "Parser.rl" if (cs >= JSON_array_first_final) { return new ParserResult(result, p + 1); @@ -1722,7 +1746,7 @@ case 5: } -// line 1726 "Parser.java" +// line 1750 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1845,7 +1869,7 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 736 "Parser.rl" +// line 760 "Parser.rl" ParserResult parseObject(int p, int pe) { @@ -1868,14 +1892,14 @@ static final int JSON_object_en_main = 1; } -// line 1872 "Parser.java" +// line 1896 "Parser.java" { cs = JSON_object_start; } -// line 758 "Parser.rl" +// line 782 "Parser.rl" -// line 1879 "Parser.java" +// line 1903 "Parser.java" { int _klen; int _trans = 0; @@ -1956,7 +1980,7 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 691 "Parser.rl" +// line 715 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { @@ -1973,7 +1997,7 @@ case 1: } break; case 1: -// line 706 "Parser.rl" +// line 730 "Parser.rl" { ParserResult res = parseString(p, pe); if (res == null) { @@ -1993,13 +2017,13 @@ case 1: } break; case 2: -// line 724 "Parser.rl" +// line 748 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 2003 "Parser.java" +// line 2027 "Parser.java" } } } @@ -2019,7 +2043,7 @@ case 5: break; } } -// line 759 "Parser.rl" +// line 783 "Parser.rl" if (cs < JSON_object_first_final) { return null; @@ -2045,7 +2069,7 @@ case 5: } -// line 2049 "Parser.java" +// line 2073 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2149,7 +2173,7 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 817 "Parser.rl" +// line 841 "Parser.rl" public IRubyObject parseStrict() { @@ -2158,16 +2182,16 @@ static final int JSON_en_main = 1; IRubyObject result = null; -// line 2162 "Parser.java" +// line 2186 "Parser.java" { cs = JSON_start; } -// line 825 "Parser.rl" +// line 849 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2171 "Parser.java" +// line 2195 "Parser.java" { int _klen; int _trans = 0; @@ -2248,7 +2272,7 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 789 "Parser.rl" +// line 813 "Parser.rl" { currentNesting = 1; ParserResult res = parseObject(p, pe); @@ -2262,7 +2286,7 @@ case 1: } break; case 1: -// line 801 "Parser.rl" +// line 825 "Parser.rl" { currentNesting = 1; ParserResult res = parseArray(p, pe); @@ -2275,7 +2299,7 @@ case 1: } } break; -// line 2279 "Parser.java" +// line 2303 "Parser.java" } } } @@ -2295,7 +2319,7 @@ case 5: break; } } -// line 828 "Parser.rl" +// line 852 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2305,7 +2329,7 @@ case 5: } -// line 2309 "Parser.java" +// line 2333 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2408,7 +2432,7 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 856 "Parser.rl" +// line 880 "Parser.rl" public IRubyObject parseQuirksMode() { @@ -2417,16 +2441,16 @@ static final int JSON_quirks_mode_en_main = 1; IRubyObject result = null; -// line 2421 "Parser.java" +// line 2445 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 864 "Parser.rl" +// line 888 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2430 "Parser.java" +// line 2454 "Parser.java" { int _klen; int _trans = 0; @@ -2507,7 +2531,7 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 842 "Parser.rl" +// line 866 "Parser.rl" { ParserResult res = parseValue(p, pe); if (res == null) { @@ -2519,7 +2543,7 @@ case 1: } } break; -// line 2523 "Parser.java" +// line 2547 "Parser.java" } } } @@ -2539,7 +2563,7 @@ case 5: break; } } -// line 867 "Parser.rl" +// line 891 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2565,9 +2589,7 @@ case 5: * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - int offset = byteList.begin(); - return (ByteList)byteList.subSequence(absStart - offset, - absEnd - offset); + return new ByteList(byteList.unsafeBytes(), absStart, absEnd - absStart, false); } /** diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index 02056f8..9555138 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -494,6 +494,13 @@ public class Parser extends RubyObject { }%% ParserResult parseInteger(int p, int pe) { + int new_p = parseIntegerInternal(p, pe); + if (new_p == -1) return null; + RubyInteger number = createInteger(p, new_p); + return new ParserResult(number, new_p + 1); + } + + int parseIntegerInternal(int p, int pe) { int cs = EVIL; %% write init; @@ -501,14 +508,18 @@ public class Parser extends RubyObject { %% write exec; if (cs < JSON_integer_first_final) { - return null; + return -1; } - ByteList num = absSubSequence(memo, p); - RubyInteger number = getRuntime().is1_9() ? - ConvertBytes.byteListToInum19(getRuntime(), num, 10, true) : - ConvertBytes.byteListToInum(getRuntime(), num, 10, true); - return new ParserResult(number, p + 1); + return p; + } + + RubyInteger createInteger(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return runtime.is1_9() ? + ConvertBytes.byteListToInum19(runtime, num, 10, true) : + ConvertBytes.byteListToInum(runtime, num, 10, true); } %%{ @@ -529,6 +540,13 @@ public class Parser extends RubyObject { }%% ParserResult parseFloat(int p, int pe) { + int new_p = parseFloatInternal(p, pe); + if (new_p == -1) return null; + RubyFloat number = createFloat(p, new_p); + return new ParserResult(number, new_p + 1); + } + + int parseFloatInternal(int p, int pe) { int cs = EVIL; %% write init; @@ -536,12 +554,16 @@ public class Parser extends RubyObject { %% write exec; if (cs < JSON_float_first_final) { - return null; + return -1; } - - ByteList num = absSubSequence(memo, p); - RubyFloat number = RubyFloat.newFloat(getRuntime(), dc.parse(num, true, getRuntime().is1_9())); - return new ParserResult(number, p + 1); + + return p; + } + + RubyFloat createFloat(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return RubyFloat.newFloat(runtime, dc.parse(num, true, runtime.is1_9())); } %%{ @@ -891,9 +913,7 @@ public class Parser extends RubyObject { * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - int offset = byteList.begin(); - return (ByteList)byteList.subSequence(absStart - offset, - absEnd - offset); + return new ByteList(byteList.unsafeBytes(), absStart, absEnd - absStart, false); } /** -- cgit v1.2.1 From 3c7c29215d18d25fff3d3af2d599f90894c52c72 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 20 Dec 2011 09:11:53 -0600 Subject: Only allocate a single ParserResult, and pass it along the stack. --- java/src/json/ext/Parser.java | 215 ++++++++++++++++++++++-------------------- java/src/json/ext/Parser.rl | 96 +++++++++++-------- 2 files changed, 170 insertions(+), 141 deletions(-) diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index adb227f..7178c16 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -84,13 +84,13 @@ public class Parser extends RubyObject { * The result of the successful parsing. Should never be * null. */ - final IRubyObject result; + IRubyObject result; /** * The point where the parser returned. */ - final int p; + int p; - ParserResult(IRubyObject result, int p) { + void update(IRubyObject result, int p) { this.result = result; this.p = p; } @@ -456,7 +456,7 @@ static final int JSON_value_en_main = 1; // line 467 "Parser.rl" - ParserResult parseValue(int p, int pe) { + void parseValue(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; @@ -615,13 +615,13 @@ case 1: throw unexpectedToken(p, pe); } } - ParserResult res = parseFloat(p, pe); - if (res != null) { + parseFloat(res, p, pe); + if (res.result != null) { result = res.result; {p = (( res.p))-1;} } - res = parseInteger(p, pe); - if (res != null) { + parseInteger(res, p, pe); + if (res.result != null) { result = res.result; {p = (( res.p))-1;} } @@ -632,8 +632,8 @@ case 1: case 6: // line 418 "Parser.rl" { - ParserResult res = parseString(p, pe); - if (res == null) { + parseString(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -646,9 +646,9 @@ case 1: // line 428 "Parser.rl" { currentNesting++; - ParserResult res = parseArray(p, pe); + parseArray(res, p, pe); currentNesting--; - if (res == null) { + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -661,9 +661,9 @@ case 1: // line 440 "Parser.rl" { currentNesting++; - ParserResult res = parseObject(p, pe); + parseObject(res, p, pe); currentNesting--; - if (res == null) { + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -695,9 +695,9 @@ case 5: // line 475 "Parser.rl" if (cs >= JSON_value_first_final && result != null) { - return new ParserResult(result, p); + res.update(result, p); } else { - return null; + res.update(null, p); } } @@ -804,26 +804,30 @@ static final int JSON_integer_en_main = 1; // line 494 "Parser.rl" - ParserResult parseInteger(int p, int pe) { + void parseInteger(ParserResult res, int p, int pe) { int new_p = parseIntegerInternal(p, pe); - if (new_p == -1) return null; + if (new_p == -1) { + res.update(null, p); + return; + } RubyInteger number = createInteger(p, new_p); - return new ParserResult(number, new_p + 1); + res.update(number, new_p + 1); + return; } int parseIntegerInternal(int p, int pe) { int cs = EVIL; -// line 819 "Parser.java" +// line 823 "Parser.java" { cs = JSON_integer_start; } -// line 507 "Parser.rl" +// line 511 "Parser.rl" int memo = p; -// line 827 "Parser.java" +// line 831 "Parser.java" { int _klen; int _trans = 0; @@ -910,7 +914,7 @@ case 1: { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 914 "Parser.java" +// line 918 "Parser.java" } } } @@ -930,7 +934,7 @@ case 5: break; } } -// line 509 "Parser.rl" +// line 513 "Parser.rl" if (cs < JSON_integer_first_final) { return -1; @@ -942,13 +946,17 @@ case 5: RubyInteger createInteger(int p, int new_p) { Ruby runtime = getRuntime(); ByteList num = absSubSequence(p, new_p); + return bytesToInum(runtime, num); + } + + RubyInteger bytesToInum(Ruby runtime, ByteList num) { return runtime.is1_9() ? ConvertBytes.byteListToInum19(runtime, num, 10, true) : ConvertBytes.byteListToInum(runtime, num, 10, true); } -// line 952 "Parser.java" +// line 960 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1050,29 +1058,33 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 540 "Parser.rl" +// line 548 "Parser.rl" - ParserResult parseFloat(int p, int pe) { + void parseFloat(ParserResult res, int p, int pe) { int new_p = parseFloatInternal(p, pe); - if (new_p == -1) return null; + if (new_p == -1) { + res.update(null, p); + return; + } RubyFloat number = createFloat(p, new_p); - return new ParserResult(number, new_p + 1); + res.update(number, new_p + 1); + return; } int parseFloatInternal(int p, int pe) { int cs = EVIL; -// line 1068 "Parser.java" +// line 1080 "Parser.java" { cs = JSON_float_start; } -// line 553 "Parser.rl" +// line 565 "Parser.rl" int memo = p; -// line 1076 "Parser.java" +// line 1088 "Parser.java" { int _klen; int _trans = 0; @@ -1153,13 +1165,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 531 "Parser.rl" +// line 539 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1163 "Parser.java" +// line 1175 "Parser.java" } } } @@ -1179,7 +1191,7 @@ case 5: break; } } -// line 555 "Parser.rl" +// line 567 "Parser.rl" if (cs < JSON_float_first_final) { return -1; @@ -1195,7 +1207,7 @@ case 5: } -// line 1199 "Parser.java" +// line 1211 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1297,23 +1309,23 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 600 "Parser.rl" +// line 612 "Parser.rl" - ParserResult parseString(int p, int pe) { + void parseString(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; -// line 1309 "Parser.java" +// line 1321 "Parser.java" { cs = JSON_string_start; } -// line 607 "Parser.rl" +// line 619 "Parser.rl" int memo = p; -// line 1317 "Parser.java" +// line 1329 "Parser.java" { int _klen; int _trans = 0; @@ -1394,7 +1406,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 575 "Parser.rl" +// line 587 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1409,13 +1421,13 @@ case 1: } break; case 1: -// line 588 "Parser.rl" +// line 600 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1419 "Parser.java" +// line 1431 "Parser.java" } } } @@ -1435,7 +1447,7 @@ case 5: break; } } -// line 609 "Parser.rl" +// line 621 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1463,14 +1475,14 @@ case 5: } if (cs >= JSON_string_first_final && result != null) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { - return null; + res.update(null, p + 1); } } -// line 1474 "Parser.java" +// line 1486 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1583,10 +1595,10 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 679 "Parser.rl" +// line 691 "Parser.rl" - ParserResult parseArray(int p, int pe) { + void parseArray(ParserResult res, int p, int pe) { int cs = EVIL; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { @@ -1605,14 +1617,14 @@ static final int JSON_array_en_main = 1; } -// line 1609 "Parser.java" +// line 1621 "Parser.java" { cs = JSON_array_start; } -// line 700 "Parser.rl" +// line 712 "Parser.rl" -// line 1616 "Parser.java" +// line 1628 "Parser.java" { int _klen; int _trans = 0; @@ -1693,10 +1705,10 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 648 "Parser.rl" +// line 660 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -1710,13 +1722,13 @@ case 1: } break; case 1: -// line 663 "Parser.rl" +// line 675 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1720 "Parser.java" +// line 1732 "Parser.java" } } } @@ -1736,17 +1748,17 @@ case 5: break; } } -// line 701 "Parser.rl" +// line 713 "Parser.rl" if (cs >= JSON_array_first_final) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { throw unexpectedToken(p, pe); } } -// line 1750 "Parser.java" +// line 1762 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1869,10 +1881,10 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 760 "Parser.rl" +// line 772 "Parser.rl" - ParserResult parseObject(int p, int pe) { + void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; @@ -1892,14 +1904,14 @@ static final int JSON_object_en_main = 1; } -// line 1896 "Parser.java" +// line 1908 "Parser.java" { cs = JSON_object_start; } -// line 782 "Parser.rl" +// line 794 "Parser.rl" -// line 1903 "Parser.java" +// line 1915 "Parser.java" { int _klen; int _trans = 0; @@ -1980,10 +1992,10 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 715 "Parser.rl" +// line 727 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -1997,10 +2009,10 @@ case 1: } break; case 1: -// line 730 "Parser.rl" +// line 742 "Parser.rl" { - ParserResult res = parseString(p, pe); - if (res == null) { + parseString(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2017,13 +2029,13 @@ case 1: } break; case 2: -// line 748 "Parser.rl" +// line 760 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 2027 "Parser.java" +// line 2039 "Parser.java" } } } @@ -2043,10 +2055,11 @@ case 5: break; } } -// line 783 "Parser.rl" +// line 795 "Parser.rl" if (cs < JSON_object_first_final) { - return null; + res.update(null, p + 1); + return; } IRubyObject returnedResult = result; @@ -2065,11 +2078,11 @@ case 5: } } } - return new ParserResult(returnedResult, p + 1); + res.update(returnedResult, p + 1); } -// line 2073 "Parser.java" +// line 2085 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2173,25 +2186,26 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 841 "Parser.rl" +// line 853 "Parser.rl" public IRubyObject parseStrict() { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); -// line 2186 "Parser.java" +// line 2199 "Parser.java" { cs = JSON_start; } -// line 849 "Parser.rl" +// line 862 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2195 "Parser.java" +// line 2208 "Parser.java" { int _klen; int _trans = 0; @@ -2272,11 +2286,11 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 813 "Parser.rl" +// line 825 "Parser.rl" { currentNesting = 1; - ParserResult res = parseObject(p, pe); - if (res == null) { + parseObject(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2286,11 +2300,11 @@ case 1: } break; case 1: -// line 825 "Parser.rl" +// line 837 "Parser.rl" { currentNesting = 1; - ParserResult res = parseArray(p, pe); - if (res == null) { + parseArray(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2299,7 +2313,7 @@ case 1: } } break; -// line 2303 "Parser.java" +// line 2316 "Parser.java" } } } @@ -2319,7 +2333,7 @@ case 5: break; } } -// line 852 "Parser.rl" +// line 865 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2329,7 +2343,7 @@ case 5: } -// line 2333 "Parser.java" +// line 2346 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2432,25 +2446,26 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 880 "Parser.rl" +// line 893 "Parser.rl" public IRubyObject parseQuirksMode() { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); -// line 2445 "Parser.java" +// line 2459 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 888 "Parser.rl" +// line 902 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2454 "Parser.java" +// line 2468 "Parser.java" { int _klen; int _trans = 0; @@ -2531,10 +2546,10 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 866 "Parser.rl" +// line 879 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2543,7 +2558,7 @@ case 1: } } break; -// line 2547 "Parser.java" +// line 2561 "Parser.java" } } } @@ -2563,7 +2578,7 @@ case 5: break; } } -// line 891 "Parser.rl" +// line 905 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index 9555138..bee52c8 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -82,13 +82,13 @@ public class Parser extends RubyObject { * The result of the successful parsing. Should never be * null. */ - final IRubyObject result; + IRubyObject result; /** * The point where the parser returned. */ - final int p; + int p; - ParserResult(IRubyObject result, int p) { + void update(IRubyObject result, int p) { this.result = result; this.p = p; } @@ -402,13 +402,13 @@ public class Parser extends RubyObject { throw unexpectedToken(p, pe); } } - ParserResult res = parseFloat(fpc, pe); - if (res != null) { + parseFloat(res, fpc, pe); + if (res.result != null) { result = res.result; fexec res.p; } - res = parseInteger(fpc, pe); - if (res != null) { + parseInteger(res, fpc, pe); + if (res.result != null) { result = res.result; fexec res.p; } @@ -416,8 +416,8 @@ public class Parser extends RubyObject { fbreak; } action parse_string { - ParserResult res = parseString(fpc, pe); - if (res == null) { + parseString(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -427,9 +427,9 @@ public class Parser extends RubyObject { } action parse_array { currentNesting++; - ParserResult res = parseArray(fpc, pe); + parseArray(res, fpc, pe); currentNesting--; - if (res == null) { + if (res.result == null) { fhold; fbreak; } else { @@ -439,9 +439,9 @@ public class Parser extends RubyObject { } action parse_object { currentNesting++; - ParserResult res = parseObject(fpc, pe); + parseObject(res, fpc, pe); currentNesting--; - if (res == null) { + if (res.result == null) { fhold; fbreak; } else { @@ -466,7 +466,7 @@ public class Parser extends RubyObject { ) %*exit; }%% - ParserResult parseValue(int p, int pe) { + void parseValue(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; @@ -474,9 +474,9 @@ public class Parser extends RubyObject { %% write exec; if (cs >= JSON_value_first_final && result != null) { - return new ParserResult(result, p); + res.update(result, p); } else { - return null; + res.update(null, p); } } @@ -493,11 +493,15 @@ public class Parser extends RubyObject { main := '-'? ( '0' | [1-9][0-9]* ) ( ^[0-9]? @exit ); }%% - ParserResult parseInteger(int p, int pe) { + void parseInteger(ParserResult res, int p, int pe) { int new_p = parseIntegerInternal(p, pe); - if (new_p == -1) return null; + if (new_p == -1) { + res.update(null, p); + return; + } RubyInteger number = createInteger(p, new_p); - return new ParserResult(number, new_p + 1); + res.update(number, new_p + 1); + return; } int parseIntegerInternal(int p, int pe) { @@ -517,6 +521,10 @@ public class Parser extends RubyObject { RubyInteger createInteger(int p, int new_p) { Ruby runtime = getRuntime(); ByteList num = absSubSequence(p, new_p); + return bytesToInum(runtime, num); + } + + RubyInteger bytesToInum(Ruby runtime, ByteList num) { return runtime.is1_9() ? ConvertBytes.byteListToInum19(runtime, num, 10, true) : ConvertBytes.byteListToInum(runtime, num, 10, true); @@ -539,11 +547,15 @@ public class Parser extends RubyObject { ( ^[0-9Ee.\-]? @exit ); }%% - ParserResult parseFloat(int p, int pe) { + void parseFloat(ParserResult res, int p, int pe) { int new_p = parseFloatInternal(p, pe); - if (new_p == -1) return null; + if (new_p == -1) { + res.update(null, p); + return; + } RubyFloat number = createFloat(p, new_p); - return new ParserResult(number, new_p + 1); + res.update(number, new_p + 1); + return; } int parseFloatInternal(int p, int pe) { @@ -599,7 +611,7 @@ public class Parser extends RubyObject { ) '"' @exit; }%% - ParserResult parseString(int p, int pe) { + void parseString(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; @@ -633,9 +645,9 @@ public class Parser extends RubyObject { } if (cs >= JSON_string_first_final && result != null) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { - return null; + res.update(null, p + 1); } } @@ -646,8 +658,8 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -678,7 +690,7 @@ public class Parser extends RubyObject { end_array @exit; }%% - ParserResult parseArray(int p, int pe) { + void parseArray(ParserResult res, int p, int pe) { int cs = EVIL; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { @@ -700,7 +712,7 @@ public class Parser extends RubyObject { %% write exec; if (cs >= JSON_array_first_final) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { throw unexpectedToken(p, pe); } @@ -713,8 +725,8 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -728,8 +740,8 @@ public class Parser extends RubyObject { } action parse_name { - ParserResult res = parseString(fpc, pe); - if (res == null) { + parseString(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -759,7 +771,7 @@ public class Parser extends RubyObject { ) @exit; }%% - ParserResult parseObject(int p, int pe) { + void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; @@ -801,7 +813,7 @@ public class Parser extends RubyObject { } } } - return new ParserResult(returnedResult, p + 1); + res.update(returnedResult, p + 1); } %%{ @@ -812,8 +824,8 @@ public class Parser extends RubyObject { action parse_object { currentNesting = 1; - ParserResult res = parseObject(fpc, pe); - if (res == null) { + parseObject(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -824,8 +836,8 @@ public class Parser extends RubyObject { action parse_array { currentNesting = 1; - ParserResult res = parseArray(fpc, pe); - if (res == null) { + parseArray(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -844,6 +856,7 @@ public class Parser extends RubyObject { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); %% write init; p = byteList.begin(); @@ -864,8 +877,8 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -883,6 +896,7 @@ public class Parser extends RubyObject { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); %% write init; p = byteList.begin(); -- cgit v1.2.1 From eacb6564c05ff7428bcbb3c34f72655b1b5d6ca3 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Dec 2011 20:14:10 +0100 Subject: Support object class with duck type hash --- ext/json/ext/generator/extconf.rb | 6 +- ext/json/ext/parser/extconf.rb | 6 +- ext/json/ext/parser/parser.c | 148 ++++++++++++++++++++------------------ ext/json/ext/parser/parser.rl | 10 ++- tests/test_json.rb | 25 ++++++- 5 files changed, 115 insertions(+), 80 deletions(-) diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index a9eae14..32aed7f 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -5,9 +5,9 @@ unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3') end if CONFIG['CC'] =~ /gcc/ $CFLAGS << ' -Wall' - #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') - # $CFLAGS << ' -O0 -ggdb' - #end + unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') + $CFLAGS << ' -O0 -ggdb' + end end create_makefile 'json/ext/generator' diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb index b09f122..4791829 100644 --- a/ext/json/ext/parser/extconf.rb +++ b/ext/json/ext/parser/extconf.rb @@ -5,9 +5,9 @@ unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3') end if CONFIG['CC'] =~ /gcc/ $CFLAGS << ' -Wall' - #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') - # $CFLAGS << ' -O0 -ggdb' - #end + if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') + $CFLAGS << ' -O0 -ggdb' + end end create_makefile 'json/ext/parser' diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 7d6092f..e262f09 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -81,7 +81,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode, i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_leftshift; + i_match_string, i_aset, i_aref, i_leftshift; #line 110 "parser.rl" @@ -445,7 +445,12 @@ case 26: if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname = rb_hash_aref(*result, json->create_id); + VALUE klassname; + if (NIL_P(json->object_class)) { + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { @@ -461,7 +466,7 @@ case 26: -#line 465 "parser.c" +#line 470 "parser.c" static const int JSON_value_start = 1; static const int JSON_value_first_final = 21; static const int JSON_value_error = 0; @@ -469,7 +474,7 @@ static const int JSON_value_error = 0; static const int JSON_value_en_main = 1; -#line 266 "parser.rl" +#line 271 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -477,14 +482,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 481 "parser.c" +#line 486 "parser.c" { cs = JSON_value_start; } -#line 273 "parser.rl" +#line 278 "parser.rl" -#line 488 "parser.c" +#line 493 "parser.c" { if ( p == pe ) goto _test_eof; @@ -509,14 +514,14 @@ st0: cs = 0; goto _out; tr0: -#line 214 "parser.rl" +#line 219 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr2: -#line 219 "parser.rl" +#line 224 "parser.rl" { char *np; if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) { @@ -536,7 +541,7 @@ tr2: } goto st21; tr5: -#line 237 "parser.rl" +#line 242 "parser.rl" { char *np; json->current_nesting++; @@ -546,7 +551,7 @@ tr5: } goto st21; tr9: -#line 245 "parser.rl" +#line 250 "parser.rl" { char *np; json->current_nesting++; @@ -556,7 +561,7 @@ tr9: } goto st21; tr16: -#line 207 "parser.rl" +#line 212 "parser.rl" { if (json->allow_nan) { *result = CInfinity; @@ -566,7 +571,7 @@ tr16: } goto st21; tr18: -#line 200 "parser.rl" +#line 205 "parser.rl" { if (json->allow_nan) { *result = CNaN; @@ -576,19 +581,19 @@ tr18: } goto st21; tr22: -#line 194 "parser.rl" +#line 199 "parser.rl" { *result = Qfalse; } goto st21; tr25: -#line 191 "parser.rl" +#line 196 "parser.rl" { *result = Qnil; } goto st21; tr28: -#line 197 "parser.rl" +#line 202 "parser.rl" { *result = Qtrue; } @@ -597,9 +602,9 @@ st21: if ( ++p == pe ) goto _test_eof21; case 21: -#line 253 "parser.rl" +#line 258 "parser.rl" { p--; {p++; cs = 21; goto _out;} } -#line 603 "parser.c" +#line 608 "parser.c" goto st0; st2: if ( ++p == pe ) @@ -760,7 +765,7 @@ case 20: _out: {} } -#line 274 "parser.rl" +#line 279 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -770,7 +775,7 @@ case 20: } -#line 774 "parser.c" +#line 779 "parser.c" static const int JSON_integer_start = 1; static const int JSON_integer_first_final = 3; static const int JSON_integer_error = 0; @@ -778,7 +783,7 @@ static const int JSON_integer_error = 0; static const int JSON_integer_en_main = 1; -#line 290 "parser.rl" +#line 295 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -786,15 +791,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 790 "parser.c" +#line 795 "parser.c" { cs = JSON_integer_start; } -#line 297 "parser.rl" +#line 302 "parser.rl" json->memo = p; -#line 798 "parser.c" +#line 803 "parser.c" { if ( p == pe ) goto _test_eof; @@ -828,14 +833,14 @@ case 3: goto st0; goto tr4; tr4: -#line 287 "parser.rl" +#line 292 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 839 "parser.c" +#line 844 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -854,7 +859,7 @@ case 5: _out: {} } -#line 299 "parser.rl" +#line 304 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -869,7 +874,7 @@ case 5: } -#line 873 "parser.c" +#line 878 "parser.c" static const int JSON_float_start = 1; static const int JSON_float_first_final = 8; static const int JSON_float_error = 0; @@ -877,7 +882,7 @@ static const int JSON_float_error = 0; static const int JSON_float_en_main = 1; -#line 324 "parser.rl" +#line 329 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -885,15 +890,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 889 "parser.c" +#line 894 "parser.c" { cs = JSON_float_start; } -#line 331 "parser.rl" +#line 336 "parser.rl" json->memo = p; -#line 897 "parser.c" +#line 902 "parser.c" { if ( p == pe ) goto _test_eof; @@ -951,14 +956,14 @@ case 8: goto st0; goto tr9; tr9: -#line 318 "parser.rl" +#line 323 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 962 "parser.c" +#line 967 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1019,7 +1024,7 @@ case 7: _out: {} } -#line 333 "parser.rl" +#line 338 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1035,7 +1040,7 @@ case 7: -#line 1039 "parser.c" +#line 1044 "parser.c" static const int JSON_array_start = 1; static const int JSON_array_first_final = 17; static const int JSON_array_error = 0; @@ -1043,7 +1048,7 @@ static const int JSON_array_error = 0; static const int JSON_array_en_main = 1; -#line 376 "parser.rl" +#line 381 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -1057,14 +1062,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 1061 "parser.c" +#line 1066 "parser.c" { cs = JSON_array_start; } -#line 389 "parser.rl" +#line 394 "parser.rl" -#line 1068 "parser.c" +#line 1073 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1103,7 +1108,7 @@ case 2: goto st2; goto st0; tr2: -#line 353 "parser.rl" +#line 358 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); @@ -1123,7 +1128,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1127 "parser.c" +#line 1132 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1223,14 +1228,14 @@ case 12: goto st3; goto st12; tr4: -#line 368 "parser.rl" +#line 373 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1234 "parser.c" +#line 1239 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1286,7 +1291,7 @@ case 16: _out: {} } -#line 390 "parser.rl" +#line 395 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1367,7 +1372,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1371 "parser.c" +#line 1376 "parser.c" static const int JSON_string_start = 1; static const int JSON_string_first_final = 8; static const int JSON_string_error = 0; @@ -1375,7 +1380,7 @@ static const int JSON_string_error = 0; static const int JSON_string_en_main = 1; -#line 489 "parser.rl" +#line 494 "parser.rl" static int @@ -1397,15 +1402,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1401 "parser.c" +#line 1406 "parser.c" { cs = JSON_string_start; } -#line 510 "parser.rl" +#line 515 "parser.rl" json->memo = p; -#line 1409 "parser.c" +#line 1414 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1430,7 +1435,7 @@ case 2: goto st0; goto st2; tr2: -#line 475 "parser.rl" +#line 480 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1441,14 +1446,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 486 "parser.rl" +#line 491 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1452 "parser.c" +#line 1457 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1524,7 +1529,7 @@ case 7: _out: {} } -#line 512 "parser.rl" +#line 517 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1723,7 +1728,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1727 "parser.c" +#line 1732 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; @@ -1731,7 +1736,7 @@ static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 734 "parser.rl" +#line 739 "parser.rl" static VALUE cParser_parse_strict(VALUE self) @@ -1742,16 +1747,16 @@ static VALUE cParser_parse_strict(VALUE self) GET_PARSER; -#line 1746 "parser.c" +#line 1751 "parser.c" { cs = JSON_start; } -#line 744 "parser.rl" +#line 749 "parser.rl" p = json->source; pe = p + json->len; -#line 1755 "parser.c" +#line 1760 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1807,7 +1812,7 @@ case 5: goto st1; goto st5; tr3: -#line 723 "parser.rl" +#line 728 "parser.rl" { char *np; json->current_nesting = 1; @@ -1816,7 +1821,7 @@ tr3: } goto st10; tr4: -#line 716 "parser.rl" +#line 721 "parser.rl" { char *np; json->current_nesting = 1; @@ -1828,7 +1833,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1832 "parser.c" +#line 1837 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1885,7 +1890,7 @@ case 9: _out: {} } -#line 747 "parser.rl" +#line 752 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -1897,7 +1902,7 @@ case 9: -#line 1901 "parser.c" +#line 1906 "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; @@ -1905,7 +1910,7 @@ static const int JSON_quirks_mode_error = 0; static const int JSON_quirks_mode_en_main = 1; -#line 772 "parser.rl" +#line 777 "parser.rl" static VALUE cParser_parse_quirks_mode(VALUE self) @@ -1916,16 +1921,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self) GET_PARSER; -#line 1920 "parser.c" +#line 1925 "parser.c" { cs = JSON_quirks_mode_start; } -#line 782 "parser.rl" +#line 787 "parser.rl" p = json->source; pe = p + json->len; -#line 1929 "parser.c" +#line 1934 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1959,7 +1964,7 @@ st0: cs = 0; goto _out; tr2: -#line 764 "parser.rl" +#line 769 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1969,7 +1974,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1973 "parser.c" +#line 1978 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2058,7 +2063,7 @@ case 9: _out: {} } -#line 785 "parser.rl" +#line 790 "parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2173,6 +2178,7 @@ void Init_parser() i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); i_aset = rb_intern("[]="); + i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index c91b4b0..1bee5bd 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode, i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_leftshift; + i_match_string, i_aset, i_aref, i_leftshift; %%{ machine JSON_common; @@ -167,7 +167,12 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname = rb_hash_aref(*result, json->create_id); + VALUE klassname; + if (NIL_P(json->object_class)) { + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { @@ -896,6 +901,7 @@ void Init_parser() i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); i_aset = rb_intern("[]="); + i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); diff --git a/tests/test_json.rb b/tests/test_json.rb index c308933..fe21475 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -5,6 +5,7 @@ require 'test/unit' require File.join(File.dirname(__FILE__), 'setup_variant') require 'stringio' require 'tempfile' +require 'ostruct' unless Array.method_defined?(:permutation) begin @@ -256,13 +257,35 @@ class TC_JSON < Test::Unit::TestCase end end - def test_parse_object_custom_class + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_hash_derived_class res = parse('{"foo":"bar"}', :object_class => SubHash) assert_equal({"foo" => "bar"}, res) assert_equal(SubHash, res.class) assert res.item_set? end + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + def test_generation_of_core_subclasses_with_new_to_json obj = SubHash2["foo" => SubHash2["bar" => true]] obj_json = JSON(obj) -- cgit v1.2.1 From 43460e877f3759af36bc541433ddb6b32d510116 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Dec 2011 20:17:51 +0100 Subject: only master --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 072b43d..039d759 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,4 +1,4 @@ -= JSON implementation for Ruby http://travis-ci.org/flori/json.png += JSON implementation for Ruby http://travis-ci.org/flori/json.png?branch=master == Description -- cgit v1.2.1 From dec1286737e8d9c3d61ec9f6726a88d65592f48e Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Dec 2011 22:28:12 +0100 Subject: Support hash ducktype objects on JRuby as well --- ext/json/ext/parser/parser.rl | 10 ++--- java/src/json/ext/OptionsReader.java | 8 +--- java/src/json/ext/Parser.java | 71 ++++++++++++++++++++---------------- java/src/json/ext/Parser.rl | 28 +++++++++----- 4 files changed, 63 insertions(+), 54 deletions(-) diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index 1bee5bd..5838dac 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -167,12 +167,12 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname; + VALUE klassname; if (NIL_P(json->object_class)) { - klassname = rb_hash_aref(*result, json->create_id); - } else { - klassname = rb_funcall(*result, i_aref, 1, json->create_id); - } + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { diff --git a/java/src/json/ext/OptionsReader.java b/java/src/json/ext/OptionsReader.java index a0b76b1..6835735 100644 --- a/java/src/json/ext/OptionsReader.java +++ b/java/src/json/ext/OptionsReader.java @@ -102,13 +102,7 @@ final class OptionsReader { IRubyObject value = get(key); if (value == null || value.isNil()) return defaultValue; - - if (value instanceof RubyClass && - ((RubyClass)value).getAllocator() == defaultValue.getAllocator()) { - return (RubyClass)value; - } - throw runtime.newTypeError(key + " option must be a subclass of " - + defaultValue); + return (RubyClass)value; } public RubyHash getHash(String key) { diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 7178c16..1ff25a6 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -1887,6 +1887,7 @@ static final int JSON_object_en_main = 1; void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; + boolean objectDefault = true; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { throw newException(Utils.M_NESTING_ERROR, @@ -1895,23 +1896,24 @@ static final int JSON_object_en_main = 1; // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result; - if (parser.objectClass != getRuntime().getHash()) { - result = (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); - } else { + IRubyObject result; + if (parser.objectClass == getRuntime().getHash()) { result = RubyHash.newHash(getRuntime()); + } else { + objectDefault = false; + result = parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); } -// line 1908 "Parser.java" +// line 1910 "Parser.java" { cs = JSON_object_start; } -// line 794 "Parser.rl" +// line 796 "Parser.rl" -// line 1915 "Parser.java" +// line 1917 "Parser.java" { int _klen; int _trans = 0; @@ -1999,10 +2001,10 @@ case 1: p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (parser.objectClass != getRuntime().getHash()) { - result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + if (parser.objectClass == getRuntime().getHash()) { + ((RubyHash)result).op_aset(context, lastName, res.result); } else { - result.op_aset(context, lastName, res.result); + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } {p = (( res.p))-1;} } @@ -2035,7 +2037,7 @@ case 1: { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 2039 "Parser.java" +// line 2041 "Parser.java" } } } @@ -2055,10 +2057,9 @@ case 5: break; } } -// line 795 "Parser.rl" +// line 797 "Parser.rl" if (cs < JSON_object_first_final) { - res.update(null, p + 1); return; } @@ -2066,7 +2067,13 @@ case 5: // attempt to de-serialize object if (parser.createAdditions) { - IRubyObject vKlassName = result.op_aref(context, parser.createId); + IRubyObject vKlassName; + if (objectDefault) { + vKlassName = ((RubyHash)result).op_aref(context, parser.createId); + } else { + vKlassName = result.callMethod(context, "[]", parser.createId); + } + if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate IRubyObject klass = parser.info.jsonModule.get(). @@ -2082,7 +2089,7 @@ case 5: } -// line 2085 "Parser.java" +// line 2093 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2186,7 +2193,7 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 853 "Parser.rl" +// line 861 "Parser.rl" public IRubyObject parseStrict() { @@ -2196,16 +2203,16 @@ static final int JSON_en_main = 1; ParserResult res = new ParserResult(); -// line 2199 "Parser.java" +// line 2207 "Parser.java" { cs = JSON_start; } -// line 862 "Parser.rl" +// line 870 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2208 "Parser.java" +// line 2216 "Parser.java" { int _klen; int _trans = 0; @@ -2286,7 +2293,7 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 825 "Parser.rl" +// line 833 "Parser.rl" { currentNesting = 1; parseObject(res, p, pe); @@ -2300,7 +2307,7 @@ case 1: } break; case 1: -// line 837 "Parser.rl" +// line 845 "Parser.rl" { currentNesting = 1; parseArray(res, p, pe); @@ -2313,7 +2320,7 @@ case 1: } } break; -// line 2316 "Parser.java" +// line 2324 "Parser.java" } } } @@ -2333,7 +2340,7 @@ case 5: break; } } -// line 865 "Parser.rl" +// line 873 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2343,7 +2350,7 @@ case 5: } -// line 2346 "Parser.java" +// line 2354 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2446,7 +2453,7 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 893 "Parser.rl" +// line 901 "Parser.rl" public IRubyObject parseQuirksMode() { @@ -2456,16 +2463,16 @@ static final int JSON_quirks_mode_en_main = 1; ParserResult res = new ParserResult(); -// line 2459 "Parser.java" +// line 2467 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 902 "Parser.rl" +// line 910 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2468 "Parser.java" +// line 2476 "Parser.java" { int _klen; int _trans = 0; @@ -2546,7 +2553,7 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 879 "Parser.rl" +// line 887 "Parser.rl" { parseValue(res, p, pe); if (res.result == null) { @@ -2558,7 +2565,7 @@ case 1: } } break; -// line 2561 "Parser.java" +// line 2569 "Parser.java" } } } @@ -2578,7 +2585,7 @@ case 5: break; } } -// line 905 "Parser.rl" +// line 913 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index bee52c8..bb6a38f 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -730,10 +730,10 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - if (parser.objectClass != getRuntime().getHash()) { - result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + if (parser.objectClass == getRuntime().getHash()) { + ((RubyHash)result).op_aset(context, lastName, res.result); } else { - result.op_aset(context, lastName, res.result); + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } fexec res.p; } @@ -774,6 +774,7 @@ public class Parser extends RubyObject { void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; + boolean objectDefault = true; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { throw newException(Utils.M_NESTING_ERROR, @@ -782,26 +783,33 @@ public class Parser extends RubyObject { // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result; - if (parser.objectClass != getRuntime().getHash()) { - result = (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); - } else { + IRubyObject result; + if (parser.objectClass == getRuntime().getHash()) { result = RubyHash.newHash(getRuntime()); + } else { + objectDefault = false; + result = parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); } %% write init; %% write exec; if (cs < JSON_object_first_final) { - return null; + return; } IRubyObject returnedResult = result; // attempt to de-serialize object if (parser.createAdditions) { - IRubyObject vKlassName = result.op_aref(context, parser.createId); + IRubyObject vKlassName; + if (objectDefault) { + vKlassName = ((RubyHash)result).op_aref(context, parser.createId); + } else { + vKlassName = result.callMethod(context, "[]", parser.createId); + } + if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate IRubyObject klass = parser.info.jsonModule.get(). -- cgit v1.2.1 From aa7feb07b68e13ce2e024669873a8a6a89f304dc Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Dec 2011 23:05:15 +0100 Subject: Support duck typed ruby array in JRuby as well --- ext/json/ext/parser/parser.c | 10 +++++----- java/src/json/ext/Parser.java | 16 ++++++++-------- java/src/json/ext/Parser.rl | 16 ++++++++-------- tests/test_json.rb | 30 +++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index e262f09..c572a95 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -445,12 +445,12 @@ case 26: if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname; + VALUE klassname; if (NIL_P(json->object_class)) { - klassname = rb_hash_aref(*result, json->create_id); - } else { - klassname = rb_funcall(*result, i_aref, 1, json->create_id); - } + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 1ff25a6..579b047 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -1608,12 +1608,12 @@ static final int JSON_array_en_main = 1; // this is guaranteed to be a RubyArray due to the earlier // allocator test at OptionsReader#getClass - RubyArray result; - if (parser.arrayClass != getRuntime().getArray()) { - result = (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); - } else { + IRubyObject result; + if (parser.arrayClass == getRuntime().getArray()) { result = RubyArray.newArray(getRuntime()); + } else { + result = parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); } @@ -1712,10 +1712,10 @@ case 1: p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (parser.arrayClass != getRuntime().getArray()) { - result.callMethod(context, "<<", res.result); + if (parser.arrayClass == getRuntime().getArray()) { + ((RubyArray)result).append(res.result); } else { - result.append(res.result); + result.callMethod(context, "<<", res.result); } {p = (( res.p))-1;} } diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index bb6a38f..fd759f5 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -663,10 +663,10 @@ public class Parser extends RubyObject { fhold; fbreak; } else { - if (parser.arrayClass != getRuntime().getArray()) { - result.callMethod(context, "<<", res.result); + if (parser.arrayClass == getRuntime().getArray()) { + ((RubyArray)result).append(res.result); } else { - result.append(res.result); + result.callMethod(context, "<<", res.result); } fexec res.p; } @@ -700,12 +700,12 @@ public class Parser extends RubyObject { // this is guaranteed to be a RubyArray due to the earlier // allocator test at OptionsReader#getClass - RubyArray result; - if (parser.arrayClass != getRuntime().getArray()) { - result = (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); - } else { + IRubyObject result; + if (parser.arrayClass == getRuntime().getArray()) { result = RubyArray.newArray(getRuntime()); + } else { + result = parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); } %% write init; diff --git a/tests/test_json.rb b/tests/test_json.rb index fe21475..c06fd19 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -219,13 +219,41 @@ class TC_JSON < Test::Unit::TestCase end end - def test_parse_array_custom_class + class SubArrayWrapper + def initialize + @data = [] + end + + attr_reader :data + + def [](index) + @data[index] + end + + def <<(value) + @data << value + @shifted = true + end + + def shifted? + @shifted + end + end + + def test_parse_array_custom_array_derived_class res = parse('[1,2]', :array_class => SubArray) assert_equal([1,2], res) assert_equal(SubArray, res.class) assert res.shifted? end + def test_parse_array_custom_non_array_derived_class + res = parse('[1,2]', :array_class => SubArrayWrapper) + assert_equal([1,2], res.data) + assert_equal(SubArrayWrapper, res.class) + assert res.shifted? + end + def test_parse_object assert_equal({}, parse('{}')) assert_equal({}, parse(' { } ')) -- cgit v1.2.1 From 6506aa32d5b7c56dc478cba2158e7ac7c2364be7 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Tue, 20 Dec 2011 23:39:32 +0100 Subject: Fix #109 --- java/lib/bytelist-1.0.6.jar | Bin 10493 -> 0 bytes java/lib/jcodings.jar | Bin 242327 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 java/lib/bytelist-1.0.6.jar delete mode 100644 java/lib/jcodings.jar diff --git a/java/lib/bytelist-1.0.6.jar b/java/lib/bytelist-1.0.6.jar deleted file mode 100644 index 7918e7c..0000000 Binary files a/java/lib/bytelist-1.0.6.jar and /dev/null differ diff --git a/java/lib/jcodings.jar b/java/lib/jcodings.jar deleted file mode 100644 index e33fc99..0000000 Binary files a/java/lib/jcodings.jar and /dev/null differ -- cgit v1.2.1 From 52e55eaf3e7c28167e990310625645a3bb64467a Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 20 Dec 2011 15:20:57 -0600 Subject: Only allocate a single "view" bytelist, and update it as needed. Conflicts: java/src/json/ext/Parser.java java/src/json/ext/Parser.rl --- java/src/json/ext/Parser.java | 171 +++++++++++++++++++++--------------------- java/src/json/ext/Parser.rl | 11 ++- 2 files changed, 94 insertions(+), 88 deletions(-) diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 579b047..2eddd8f 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -305,6 +305,7 @@ public class Parser extends RubyObject { private final Parser parser; private final ThreadContext context; private final ByteList byteList; + private final ByteList view; private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; @@ -319,6 +320,7 @@ public class Parser extends RubyObject { this.context = context; this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); + this.view = new ByteList(data, false); this.decoder = new StringDecoder(context); this.dc = new DoubleConverter(); } @@ -335,11 +337,11 @@ public class Parser extends RubyObject { } -// line 361 "Parser.rl" +// line 363 "Parser.rl" -// line 343 "Parser.java" +// line 345 "Parser.java" private static byte[] init__JSON_value_actions_0() { return new byte [] { @@ -453,7 +455,7 @@ static final int JSON_value_error = 0; static final int JSON_value_en_main = 1; -// line 467 "Parser.rl" +// line 469 "Parser.rl" void parseValue(ParserResult res, int p, int pe) { @@ -461,14 +463,14 @@ static final int JSON_value_en_main = 1; IRubyObject result = null; -// line 465 "Parser.java" +// line 467 "Parser.java" { cs = JSON_value_start; } -// line 474 "Parser.rl" +// line 476 "Parser.rl" -// line 472 "Parser.java" +// line 474 "Parser.java" { int _klen; int _trans = 0; @@ -494,13 +496,13 @@ case 1: while ( _nacts-- > 0 ) { switch ( _JSON_value_actions[_acts++] ) { case 9: -// line 452 "Parser.rl" +// line 454 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 504 "Parser.java" +// line 506 "Parser.java" } } @@ -563,25 +565,25 @@ case 1: switch ( _JSON_value_actions[_acts++] ) { case 0: -// line 369 "Parser.rl" +// line 371 "Parser.rl" { result = getRuntime().getNil(); } break; case 1: -// line 372 "Parser.rl" +// line 374 "Parser.rl" { result = getRuntime().getFalse(); } break; case 2: -// line 375 "Parser.rl" +// line 377 "Parser.rl" { result = getRuntime().getTrue(); } break; case 3: -// line 378 "Parser.rl" +// line 380 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_NAN); @@ -591,7 +593,7 @@ case 1: } break; case 4: -// line 385 "Parser.rl" +// line 387 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_INFINITY); @@ -601,7 +603,7 @@ case 1: } break; case 5: -// line 392 "Parser.rl" +// line 394 "Parser.rl" { if (pe > p + 9 - (parser.quirksMode ? 1 : 0) && absSubSequence(p, p + 9).equals(JSON_MINUS_INFINITY)) { @@ -630,7 +632,7 @@ case 1: } break; case 6: -// line 418 "Parser.rl" +// line 420 "Parser.rl" { parseString(res, p, pe); if (res.result == null) { @@ -643,7 +645,7 @@ case 1: } break; case 7: -// line 428 "Parser.rl" +// line 430 "Parser.rl" { currentNesting++; parseArray(res, p, pe); @@ -658,7 +660,7 @@ case 1: } break; case 8: -// line 440 "Parser.rl" +// line 442 "Parser.rl" { currentNesting++; parseObject(res, p, pe); @@ -672,7 +674,7 @@ case 1: } } break; -// line 676 "Parser.java" +// line 678 "Parser.java" } } } @@ -692,7 +694,7 @@ case 5: break; } } -// line 475 "Parser.rl" +// line 477 "Parser.rl" if (cs >= JSON_value_first_final && result != null) { res.update(result, p); @@ -702,7 +704,7 @@ case 5: } -// line 706 "Parser.java" +// line 708 "Parser.java" private static byte[] init__JSON_integer_actions_0() { return new byte [] { @@ -801,7 +803,7 @@ static final int JSON_integer_error = 0; static final int JSON_integer_en_main = 1; -// line 494 "Parser.rl" +// line 496 "Parser.rl" void parseInteger(ParserResult res, int p, int pe) { @@ -819,15 +821,15 @@ static final int JSON_integer_en_main = 1; int cs = EVIL; -// line 823 "Parser.java" +// line 825 "Parser.java" { cs = JSON_integer_start; } -// line 511 "Parser.rl" +// line 513 "Parser.rl" int memo = p; -// line 831 "Parser.java" +// line 833 "Parser.java" { int _klen; int _trans = 0; @@ -908,13 +910,13 @@ case 1: switch ( _JSON_integer_actions[_acts++] ) { case 0: -// line 488 "Parser.rl" +// line 490 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 918 "Parser.java" +// line 920 "Parser.java" } } } @@ -934,7 +936,7 @@ case 5: break; } } -// line 513 "Parser.rl" +// line 515 "Parser.rl" if (cs < JSON_integer_first_final) { return -1; @@ -956,7 +958,7 @@ case 5: } -// line 960 "Parser.java" +// line 962 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1058,7 +1060,7 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 548 "Parser.rl" +// line 550 "Parser.rl" void parseFloat(ParserResult res, int p, int pe) { @@ -1076,15 +1078,15 @@ static final int JSON_float_en_main = 1; int cs = EVIL; -// line 1080 "Parser.java" +// line 1082 "Parser.java" { cs = JSON_float_start; } -// line 565 "Parser.rl" +// line 567 "Parser.rl" int memo = p; -// line 1088 "Parser.java" +// line 1090 "Parser.java" { int _klen; int _trans = 0; @@ -1165,13 +1167,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 539 "Parser.rl" +// line 541 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1175 "Parser.java" +// line 1177 "Parser.java" } } } @@ -1191,7 +1193,7 @@ case 5: break; } } -// line 567 "Parser.rl" +// line 569 "Parser.rl" if (cs < JSON_float_first_final) { return -1; @@ -1207,7 +1209,7 @@ case 5: } -// line 1211 "Parser.java" +// line 1213 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1309,7 +1311,7 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 612 "Parser.rl" +// line 614 "Parser.rl" void parseString(ParserResult res, int p, int pe) { @@ -1317,15 +1319,15 @@ static final int JSON_string_en_main = 1; IRubyObject result = null; -// line 1321 "Parser.java" +// line 1323 "Parser.java" { cs = JSON_string_start; } -// line 619 "Parser.rl" +// line 621 "Parser.rl" int memo = p; -// line 1329 "Parser.java" +// line 1331 "Parser.java" { int _klen; int _trans = 0; @@ -1406,7 +1408,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 587 "Parser.rl" +// line 589 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1421,13 +1423,13 @@ case 1: } break; case 1: -// line 600 "Parser.rl" +// line 602 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1431 "Parser.java" +// line 1433 "Parser.java" } } } @@ -1447,7 +1449,7 @@ case 5: break; } } -// line 621 "Parser.rl" +// line 623 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1482,7 +1484,7 @@ case 5: } -// line 1486 "Parser.java" +// line 1488 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1595,7 +1597,7 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 691 "Parser.rl" +// line 693 "Parser.rl" void parseArray(ParserResult res, int p, int pe) { @@ -1617,14 +1619,14 @@ static final int JSON_array_en_main = 1; } -// line 1621 "Parser.java" +// line 1623 "Parser.java" { cs = JSON_array_start; } -// line 712 "Parser.rl" +// line 714 "Parser.rl" -// line 1628 "Parser.java" +// line 1630 "Parser.java" { int _klen; int _trans = 0; @@ -1705,7 +1707,7 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 660 "Parser.rl" +// line 662 "Parser.rl" { parseValue(res, p, pe); if (res.result == null) { @@ -1722,13 +1724,13 @@ case 1: } break; case 1: -// line 675 "Parser.rl" +// line 677 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1732 "Parser.java" +// line 1734 "Parser.java" } } } @@ -1748,7 +1750,7 @@ case 5: break; } } -// line 713 "Parser.rl" +// line 715 "Parser.rl" if (cs >= JSON_array_first_final) { res.update(result, p + 1); @@ -1758,7 +1760,7 @@ case 5: } -// line 1762 "Parser.java" +// line 1764 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1881,7 +1883,7 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 772 "Parser.rl" +// line 774 "Parser.rl" void parseObject(ParserResult res, int p, int pe) { @@ -1906,14 +1908,14 @@ static final int JSON_object_en_main = 1; } -// line 1910 "Parser.java" +// line 1912 "Parser.java" { cs = JSON_object_start; } -// line 796 "Parser.rl" +// line 798 "Parser.rl" -// line 1917 "Parser.java" +// line 1919 "Parser.java" { int _klen; int _trans = 0; @@ -1994,7 +1996,7 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 727 "Parser.rl" +// line 729 "Parser.rl" { parseValue(res, p, pe); if (res.result == null) { @@ -2011,7 +2013,7 @@ case 1: } break; case 1: -// line 742 "Parser.rl" +// line 744 "Parser.rl" { parseString(res, p, pe); if (res.result == null) { @@ -2031,13 +2033,13 @@ case 1: } break; case 2: -// line 760 "Parser.rl" +// line 762 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 2041 "Parser.java" +// line 2043 "Parser.java" } } } @@ -2057,9 +2059,10 @@ case 5: break; } } -// line 797 "Parser.rl" +// line 799 "Parser.rl" if (cs < JSON_object_first_final) { + res.update(null, p + 1); return; } @@ -2089,7 +2092,7 @@ case 5: } -// line 2093 "Parser.java" +// line 2096 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2193,7 +2196,7 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 861 "Parser.rl" +// line 864 "Parser.rl" public IRubyObject parseStrict() { @@ -2203,16 +2206,16 @@ static final int JSON_en_main = 1; ParserResult res = new ParserResult(); -// line 2207 "Parser.java" +// line 2210 "Parser.java" { cs = JSON_start; } -// line 870 "Parser.rl" +// line 873 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2216 "Parser.java" +// line 2219 "Parser.java" { int _klen; int _trans = 0; @@ -2293,7 +2296,7 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 833 "Parser.rl" +// line 836 "Parser.rl" { currentNesting = 1; parseObject(res, p, pe); @@ -2307,7 +2310,7 @@ case 1: } break; case 1: -// line 845 "Parser.rl" +// line 848 "Parser.rl" { currentNesting = 1; parseArray(res, p, pe); @@ -2320,7 +2323,7 @@ case 1: } } break; -// line 2324 "Parser.java" +// line 2327 "Parser.java" } } } @@ -2340,7 +2343,7 @@ case 5: break; } } -// line 873 "Parser.rl" +// line 876 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2350,7 +2353,7 @@ case 5: } -// line 2354 "Parser.java" +// line 2357 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2453,7 +2456,7 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 901 "Parser.rl" +// line 904 "Parser.rl" public IRubyObject parseQuirksMode() { @@ -2463,16 +2466,16 @@ static final int JSON_quirks_mode_en_main = 1; ParserResult res = new ParserResult(); -// line 2467 "Parser.java" +// line 2470 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 910 "Parser.rl" +// line 913 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2476 "Parser.java" +// line 2479 "Parser.java" { int _klen; int _trans = 0; @@ -2553,7 +2556,7 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 887 "Parser.rl" +// line 890 "Parser.rl" { parseValue(res, p, pe); if (res.result == null) { @@ -2565,7 +2568,7 @@ case 1: } } break; -// line 2569 "Parser.java" +// line 2572 "Parser.java" } } } @@ -2585,7 +2588,7 @@ case 5: break; } } -// line 913 "Parser.rl" +// line 916 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2604,14 +2607,14 @@ case 5: } /** - * Returns a subsequence of the source ByteList, based on source - * array byte offsets (i.e., the ByteList's own begin offset is not - * automatically added). + * Updates the "view" bytelist with the new offsets and returns it. * @param start * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - return new ByteList(byteList.unsafeBytes(), absStart, absEnd - absStart, false); + view.setBegin(absStart); + view.setRealSize(absEnd - absStart); + return view; } /** diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index fd759f5..0dbc9c1 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -303,6 +303,7 @@ public class Parser extends RubyObject { private final Parser parser; private final ThreadContext context; private final ByteList byteList; + private final ByteList view; private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; @@ -317,6 +318,7 @@ public class Parser extends RubyObject { this.context = context; this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); + this.view = new ByteList(data, false); this.decoder = new StringDecoder(context); this.dc = new DoubleConverter(); } @@ -796,6 +798,7 @@ public class Parser extends RubyObject { %% write exec; if (cs < JSON_object_first_final) { + res.update(null, p + 1); return; } @@ -928,14 +931,14 @@ public class Parser extends RubyObject { } /** - * Returns a subsequence of the source ByteList, based on source - * array byte offsets (i.e., the ByteList's own begin offset is not - * automatically added). + * Updates the "view" bytelist with the new offsets and returns it. * @param start * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - return new ByteList(byteList.unsafeBytes(), absStart, absEnd - absStart, false); + view.setBegin(absStart); + view.setRealSize(absEnd - absStart); + return view; } /** -- cgit v1.2.1 From 0eda56f86ff4d04ddabf15721ad0cc39c2218a6d Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Wed, 21 Dec 2011 10:54:39 +0100 Subject: Remove superflous comment --- java/src/json/ext/Parser.java | 2 -- java/src/json/ext/Parser.rl | 2 -- 2 files changed, 4 deletions(-) diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 579b047..b0573f4 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -1606,8 +1606,6 @@ static final int JSON_array_en_main = 1; "nesting of " + currentNesting + " is too deep"); } - // this is guaranteed to be a RubyArray due to the earlier - // allocator test at OptionsReader#getClass IRubyObject result; if (parser.arrayClass == getRuntime().getArray()) { result = RubyArray.newArray(getRuntime()); diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index fd759f5..1c0222c 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -698,8 +698,6 @@ public class Parser extends RubyObject { "nesting of " + currentNesting + " is too deep"); } - // this is guaranteed to be a RubyArray due to the earlier - // allocator test at OptionsReader#getClass IRubyObject result; if (parser.arrayClass == getRuntime().getArray()) { result = RubyArray.newArray(getRuntime()); -- cgit v1.2.1