From cb516973af6e018671463af3852d48731e0c64f3 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Sun, 8 Nov 2009 23:25:25 -0500 Subject: [PATCH] to_h to_json for native object --- History.txt | 1 + lib/rhino/context.rb | 3 ++- lib/rhino/native_object.rb | 12 ++++++++++++ spec/rhino/context_spec.rb | 20 +++++++++++++------- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/History.txt b/History.txt index e97b857..5e28cb9 100644 --- a/History.txt +++ b/History.txt @@ -2,6 +2,7 @@ * N major enhancements: * easily manipulate javascript objects from ruby (NativeObject) * make NativeObject Enumerable + * to_h and to_json for NativeObject === 1.72.0 2009-09-24 diff --git a/lib/rhino/context.rb b/lib/rhino/context.rb index bf02715..2a7b280 100644 --- a/lib/rhino/context.rb +++ b/lib/rhino/context.rb @@ -36,7 +36,8 @@ module Rhino end end - def evaljs(str, scope = @native.initStandardObjects()) + def eval(str, scope = @native.initStandardObjects()) + str = str.to_s begin To.ruby @native.evaluateString(To.javascript(scope), str, "", 1, nil) rescue J::RhinoException => e diff --git a/lib/rhino/native_object.rb b/lib/rhino/native_object.rb index e8616dc..e6ae1b6 100644 --- a/lib/rhino/native_object.rb +++ b/lib/rhino/native_object.rb @@ -21,5 +21,17 @@ module Rhino yield id,@j.get(id,@j) end end + + def to_h + {}.tap do |h| + each do |k,v| + h[k] = self.class === v ? v.to_h : v + end + end + end + + def to_json(*args) + to_h.to_json(*args) + end end end \ No newline at end of file diff --git a/spec/rhino/context_spec.rb b/spec/rhino/context_spec.rb index be68864..e12d048 100644 --- a/spec/rhino/context_spec.rb +++ b/spec/rhino/context_spec.rb @@ -6,7 +6,13 @@ describe Rhino::Context do it "can evaluate some javascript" do Context.open do |cxt| - cxt.evaljs("5 + 3").should == 8 + cxt.eval("5 + 3").should == 8 + end + end + + it "treats nil and the empty string as the same thing when it comes to eval" do + Context.open do |cxt| + cxt.eval(nil).should == cxt.eval('') end end @@ -14,7 +20,7 @@ describe Rhino::Context do Context.open do |cxt| cxt.init_standard_objects.tap do |scope| scope["foo"] = "Hello World" - cxt.evaljs("foo", scope).should == "Hello World" + cxt.eval("foo", scope).should == "Hello World" end end end @@ -38,7 +44,7 @@ describe Rhino::Context do it "provides unsealed standard object by default" do Context.open do |cxt| cxt.init_standard_objects.tap do |scope| - cxt.evaljs("Object.foop = 'blort'", scope) + cxt.eval("Object.foop = 'blort'", scope) scope["Object"]['foop'].should == 'blort' end end @@ -48,11 +54,11 @@ describe Rhino::Context do Context.open do |cxt| cxt.init_standard_objects(:sealed => true).tap do |scope| lambda { - cxt.evaljs("Object.foop = 'blort'", scope) + cxt.eval("Object.foop = 'blort'", scope) }.should raise_error(Rhino::RhinoError) lambda { - cxt.evaljs("Object.prototype.toString = function() {}", scope) + cxt.eval("Object.prototype.toString = function() {}", scope) }.should raise_error(Rhino::RhinoError) end @@ -71,7 +77,7 @@ describe Rhino::Context do Context.open_std(:sealed => true, :java => true) do |cxt, scope| scope["Object"].should_not be_nil scope["java"].should_not be_nil - cxt.evaljs("new java.lang.String('foo')", scope).should == "foo" + cxt.eval("new java.lang.String('foo')", scope).should == "foo" end end end @@ -81,7 +87,7 @@ describe Rhino::Context do Context.open do |cxt| cxt.standard do |scope| scope.put("say", scope, function {|word, times| word * times}) - cxt.evaljs("say('Hello',2)", scope).should == "HelloHello" + cxt.eval("say('Hello',2)", scope).should == "HelloHello" end end end