mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
Improve marshalling of Ruby hashes to JS
This commit is contained in:
parent
bdd5200619
commit
ed652c2224
2 changed files with 19 additions and 1 deletions
|
@ -20,6 +20,7 @@ module Rhino
|
|||
when String,Numeric then object
|
||||
when TrueClass,FalseClass then object
|
||||
when Array then J::NativeArray.new(object.to_java)
|
||||
when Hash then ruby_hash_to_native(object)
|
||||
when Proc,Method then RubyFunction.new(object)
|
||||
when NativeObject then object.j
|
||||
when J::Scriptable then object
|
||||
|
@ -30,7 +31,17 @@ module Rhino
|
|||
def array(native)
|
||||
native.length.times.map {|i| ruby(native.get(i,native))}
|
||||
end
|
||||
|
||||
def ruby_hash_to_native(ruby_object)
|
||||
native_object = NativeObject.new
|
||||
|
||||
ruby_object.each_pair do |k, v|
|
||||
native_object[k] = v
|
||||
end
|
||||
|
||||
native_object.j
|
||||
end
|
||||
|
||||
module_function :ruby, :javascript, :array
|
||||
module_function :ruby, :javascript, :array, :ruby_hash_to_native
|
||||
end
|
||||
end
|
||||
|
|
|
@ -97,6 +97,13 @@ describe Rhino::To do
|
|||
a.get(3,a).should be(4)
|
||||
end
|
||||
end
|
||||
|
||||
it "converts ruby hashes into native objects" do
|
||||
To.javascript({ :bare => true }).tap do |h|
|
||||
h.should be_kind_of(J::NativeObject)
|
||||
h.get("bare", h).should be(true)
|
||||
end
|
||||
end
|
||||
|
||||
it "converts procs and methods into native functions" do
|
||||
to(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
||||
|
|
Loading…
Reference in a new issue