1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

implement passing objects from JS -> Ruby

This commit is contained in:
Charles Lowell 2015-07-29 00:23:50 +03:00
parent 78c841c6b6
commit 277926fc7f
4 changed files with 22 additions and 17 deletions

View file

@ -25,7 +25,6 @@ require 'v8/conversion'
# require 'v8/access/indices' # require 'v8/access/indices'
# require 'v8/access/invocation' # require 'v8/access/invocation'
# require 'v8/access' # require 'v8/access'
# require 'v8/context' require 'v8/object'
# require 'v8/object'
# require 'v8/array' # require 'v8/array'
# require 'v8/function' # require 'v8/function'

View file

@ -31,6 +31,12 @@ module V8::C
IsTrue() IsTrue()
end end
end end
class Object
def to_ruby
::V8::Object.new self
end
end
end end
class String class String

View file

@ -6,12 +6,12 @@ class V8::Object
def initialize(native = nil) def initialize(native = nil)
@context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context" @context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
@native = block_given? ? yield : native || V8::C::Object::New() @native = block_given? ? yield : native || V8::C::Object::New()
@context.link self, @native #@context.link self, @native
end end
def [](key) def [](key)
@context.enter do @context.enter do
@context.to_ruby @native.Get(@context.to_v8(key)) @context.to_ruby @native.Get(@context.native, @context.to_v8(key)).FromJust()
end end
end end

View file

@ -71,18 +71,18 @@ describe "V8::Context" do
# end # end
# end # end
# xit "can pass objects back to ruby" do it "can pass objects back to ruby" do
# @cxt.eval("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})").tap do |object| @cxt.eval("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})").tap do |object|
# object.should_not be_nil object.should_not be_nil
# object['foo'].should == 'bar' object['foo'].should == 'bar'
# object['baz'].should == 'bang' object['baz'].should == 'bang'
# object['5'].should == 5 object['5'].should == 5
# object['embedded'].tap do |embedded| object['embedded'].tap do |embedded|
# embedded.should_not be_nil embedded.should_not be_nil
# embedded['badda'].should == 'bing' embedded['badda'].should == 'bing'
# end end
# end end
# end end
# xit "can pass int properties to ruby", :compat => '0.2.1' do # xit "can pass int properties to ruby", :compat => '0.2.1' do
# @cxt.eval("({ 4: '4', 5: 5, '6': true })").tap do |object| # @cxt.eval("({ 4: '4', 5: 5, '6': true })").tap do |object|