Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 2e59b4e

Browse files
committed
Merge pull request #372 from cowboyd/4.5/js-objects-to-high-level-api
implement passing objects from JS -> Ruby
2 parents f014899 + 277926f commit 2e59b4e

4 files changed

Lines changed: 22 additions & 17 deletions

File tree

lib/v8.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
# require 'v8/access/indices'
2626
# require 'v8/access/invocation'
2727
# require 'v8/access'
28-
# require 'v8/context'
29-
# require 'v8/object'
28+
require 'v8/object'
3029
# require 'v8/array'
3130
# require 'v8/function'

lib/v8/conversion.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def to_ruby
3131
IsTrue()
3232
end
3333
end
34+
35+
class Object
36+
def to_ruby
37+
::V8::Object.new self
38+
end
39+
end
3440
end
3541

3642
class String

lib/v8/object.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class V8::Object
66
def initialize(native = nil)
77
@context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
88
@native = block_given? ? yield : native || V8::C::Object::New()
9-
@context.link self, @native
9+
#@context.link self, @native
1010
end
1111

1212
def [](key)
1313
@context.enter do
14-
@context.to_ruby @native.Get(@context.to_v8(key))
14+
@context.to_ruby @native.Get(@context.native, @context.to_v8(key)).FromJust()
1515
end
1616
end
1717

@@ -76,4 +76,4 @@ def method_missing(name, *args, &block)
7676
raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
7777
end
7878
end
79-
end
79+
end

spec/v8/context_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@
7171
# end
7272
# end
7373

74-
# xit "can pass objects back to ruby" do
75-
# @cxt.eval("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})").tap do |object|
76-
# object.should_not be_nil
77-
# object['foo'].should == 'bar'
78-
# object['baz'].should == 'bang'
79-
# object['5'].should == 5
80-
# object['embedded'].tap do |embedded|
81-
# embedded.should_not be_nil
82-
# embedded['badda'].should == 'bing'
83-
# end
84-
# end
85-
# end
74+
it "can pass objects back to ruby" do
75+
@cxt.eval("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})").tap do |object|
76+
object.should_not be_nil
77+
object['foo'].should == 'bar'
78+
object['baz'].should == 'bang'
79+
object['5'].should == 5
80+
object['embedded'].tap do |embedded|
81+
embedded.should_not be_nil
82+
embedded['badda'].should == 'bing'
83+
end
84+
end
85+
end
8686

8787
# xit "can pass int properties to ruby", :compat => '0.2.1' do
8888
# @cxt.eval("({ 4: '4', 5: 5, '6': true })").tap do |object|

0 commit comments

Comments
 (0)