Navigation Menu

Skip to content

Commit

Permalink
traverse the prototype chain when accessing javascript properties fro…
Browse files Browse the repository at this point in the history
…m ruby
  • Loading branch information
cowboyd committed Nov 20, 2009
1 parent 3335adc commit f6eb4ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rhino/native_object.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(j) # :nodoc:
# jsobject['foo'] # => 'bar'
# jsobject['Take me to'] # => 'a funky town'
def [](k)
To.ruby @j.get(k.to_s, @j)
To.ruby J::ScriptableObject.getProperty(@j,k.to_s)
end

# set a property on the javascript object, where +k+ is a string or symbol corresponding
Expand All @@ -36,7 +36,8 @@ def [](k)
# end

def []=(k,v)
@j.put(k.to_s,@j,To.javascript(v))
#@j.put(k.to_s,@j,To.javascript(v))
J::ScriptableObject.putProperty(@j, k.to_s, To.javascript(v))
end

# enumerate the key value pairs contained in this javascript object. e.g.
Expand Down
15 changes: 15 additions & 0 deletions spec/rhino/native_object_spec.rb
Expand Up @@ -27,6 +27,18 @@
it "returns nil when the value is null, null, or not defined" do
@o[:foo].should be_nil
end


it "traverses the prototype chain when hash accessing properties from the ruby object" do
Rhino::Context.open do |cxt|
cxt.eval(<<EOJS)['bar'].should == "baz"
function Foo() {}
Foo.prototype.bar = 'baz'
new Foo()
EOJS
end
end


describe Enumerable do
it "enumerates according to native keys and values" do
Expand All @@ -35,6 +47,9 @@
@j.put(5, @j, 'flip')
@o.inject({}) {|i,p| k,v = p; i.tap {i[k] = v}}.should == {"foo" => 'bar', "bang" => 'baz', 5 => 'flip'}
end

it "should traverse prototype chain when enumerating keys and values"

end

end

0 comments on commit f6eb4ff

Please sign in to comment.