diff options
author | Florian Frank <flori@ping.de> | 2011-07-08 16:15:49 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2011-07-08 16:15:49 +0200 |
commit | dcfa15b6a1179a29d4ee7b3ff16807fa9b1f96a4 (patch) | |
tree | 3842c92001b242bc3252a8767da907e815cb4e68 /java/src/json/ext/Parser.rl | |
parent | 8f4e1c79a3afd54ed91d47b6667db6d9590adcf7 (diff) | |
download | json-dcfa15b6a1179a29d4ee7b3ff16807fa9b1f96a4.tar.gz |
Ported fixes from fix_memory_leak into Parser.rl
Ported semantic of the desired behaviour in ruby-core:35079
to java as well.
Diffstat (limited to 'java/src/json/ext/Parser.rl')
-rw-r--r-- | java/src/json/ext/Parser.rl | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index b8734e1..779d3f3 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -142,7 +142,10 @@ public class Parser extends RubyObject { @JRubyMethod(required = 1, optional = 1, visibility = Visibility.PRIVATE) public IRubyObject initialize(ThreadContext context, IRubyObject[] args) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.getRuntime(); + if (this.vSource != null) { + throw runtime.newTypeError("already initialized instance"); + } RubyString source = convertEncoding(context, args[0].convertToString()); OptionsReader opts = new OptionsReader(context, args.length > 1 ? args[1] : null); @@ -174,8 +177,8 @@ public class Parser extends RubyObject { if (info.encodingsSupported()) { RubyEncoding encoding = (RubyEncoding)source.encoding(context); - if (encoding != info.ascii8bit) { - return (RubyString)source.encode(context, info.utf8); + if (encoding != info.ascii8bit.get()) { + return (RubyString)source.encode(context, info.utf8.get()); } String sniffedEncoding = sniffByteList(bl); @@ -186,7 +189,7 @@ public class Parser extends RubyObject { String sniffedEncoding = sniffByteList(bl); if (sniffedEncoding == null) return source; // assume UTF-8 Ruby runtime = context.getRuntime(); - return (RubyString)info.jsonModule. + return (RubyString)info.jsonModule.get(). callMethod(context, "iconv", new IRubyObject[] { runtime.newString("utf-8"), @@ -216,7 +219,7 @@ public class Parser extends RubyObject { private RubyString reinterpretEncoding(ThreadContext context, RubyString str, String sniffedEncoding) { RubyEncoding actualEncoding = info.getEncoding(context, sniffedEncoding); - RubyEncoding targetEncoding = info.utf8; + RubyEncoding targetEncoding = info.utf8.get(); RubyString dup = (RubyString)str.dup(); dup.force_encoding(context, actualEncoding); return (RubyString)dup.encode_bang(context, targetEncoding); @@ -241,7 +244,15 @@ public class Parser extends RubyObject { */ @JRubyMethod(name = "source") public IRubyObject source_get() { - return vSource.dup(); + return checkAndGetSource().dup(); + } + + public RubyString checkAndGetSource() { + if (vSource != null) { + return vSource; + } else { + throw getRuntime().newTypeError("uninitialized instance"); + } } /** @@ -249,7 +260,7 @@ public class Parser extends RubyObject { * set to <code>nil</code> or <code>false</code>, and a String if not. */ private RubyString getCreateId(ThreadContext context) { - IRubyObject v = info.jsonModule.callMethod(context, "create_id"); + IRubyObject v = info.jsonModule.get().callMethod(context, "create_id"); return v.isTrue() ? v.convertToString() : null; } @@ -279,7 +290,7 @@ public class Parser extends RubyObject { private ParserSession(Parser parser, ThreadContext context) { this.parser = parser; this.context = context; - this.byteList = parser.vSource.getByteList(); + this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); this.decoder = new StringDecoder(context); } @@ -730,7 +741,7 @@ public class Parser extends RubyObject { IRubyObject vKlassName = result.op_aref(context, parser.createId); if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate - IRubyObject klass = parser.info.jsonModule. + IRubyObject klass = parser.info.jsonModule.get(). callMethod(context, "deep_const_get", vKlassName); if (klass.respondsTo("json_creatable?") && klass.callMethod(context, "json_creatable?").isTrue()) { @@ -813,7 +824,7 @@ public class Parser extends RubyObject { * @param name The constant name */ private IRubyObject getConstant(String name) { - return parser.info.jsonModule.getConstant(name); + return parser.info.jsonModule.get().getConstant(name); } private RaiseException newException(String className, String message) { |