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

Commit 7fc1ebd

Browse files
committed
enable calling JS code from Ruby
turns out that this "just worked" (exception handling still remains un-implemented)
1 parent 288ae8c commit 7fc1ebd

3 files changed

Lines changed: 44 additions & 40 deletions

File tree

lib/v8/conversion.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def to_ruby
111111
::V8::Function.new self
112112
end
113113
end
114+
115+
class Value
116+
def to_v8(context)
117+
self
118+
end
119+
end
114120
end
115121

116122
##

lib/v8/function.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def call(*args)
2222

2323
def new(*args)
2424
@context.enter do
25-
@context.to_ruby try {native.NewInstance(args.map {|a| @context.to_v8 a})}
25+
@context.to_ruby native.NewInstance(args.map {|a| @context.to_v8 a})
2626
end
2727
end
2828
end

spec/v8/context_spec.rb

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -654,45 +654,45 @@
654654

655655
# end
656656

657-
# describe "Calling JavaScript Code From Within Ruby", :compat => '0.1.0' do
657+
describe "Calling JavaScript Code From Within Ruby", :compat => '0.1.0' do
658658

659-
# before(:each) do
660-
# @cxt = V8::Context.new
661-
# end
659+
before(:each) do
660+
@cxt = V8::Context.new
661+
end
662662

663-
# xit "allows you to capture a reference to a javascript function and call it" do
664-
# f = @cxt.eval('(function add(lhs, rhs) {return lhs + rhs})')
665-
# f.call(1,2).should == 3
666-
# end
663+
it "allows you to capture a reference to a javascript function and call it" do
664+
f = @cxt.eval('(function add(lhs, rhs) {return lhs + rhs})')
665+
expect(f.call 1,2).to eql 3
666+
end
667667

668-
# xit "can path the 'this' object into a function as context with methodcall()" do
669-
# obj = @cxt.eval('({num: 5})')
670-
# times = @cxt.eval('(function times(num) {return this.num * num})')
671-
# times.methodcall(obj, 5).should == 25
672-
# end
668+
it "can path the 'this' object into a function as context with methodcall()" do
669+
obj = @cxt.eval('({num: 5})')
670+
times = @cxt.eval('(function times(num) {return this.num * num})')
671+
expect(times.methodcall obj, 5).to eql 25
672+
end
673673

674-
# xit "unwraps objects that are backed by javascript objects to pass their native equivalents" do
675-
# @cxt.eval('obj = {foo: "bar"}')
676-
# f = @cxt.eval('(function() {return this == obj})')
677-
# f.methodcall(@cxt['obj']).should be(true)
678-
# end
674+
it "unwraps objects that are backed by javascript objects to pass their native equivalents" do
675+
@cxt.eval('obj = {foo: "bar"}')
676+
f = @cxt.eval('(function() {return this == obj})')
677+
expect(f.methodcall @cxt['obj']).to be true
678+
end
679679

680-
# xit "can invoke a javacript constructor and return the new object reflected into ruby" do
681-
# wrapper = @cxt.eval('(function Wrapper(value) {this.value = value})')
682-
# wrapper.new(5)['value'].should == 5
683-
# end
680+
it "can invoke a javacript constructor and return the new object reflected into ruby" do
681+
wrapper = @cxt.eval('(function Wrapper(value) {this.value = value})')
682+
wrapper.new(5)['value'].should == 5
683+
end
684684

685-
# xit "can call a javascript method directly from a ruby object" do
686-
# obj = @cxt.eval('Object').new
687-
# obj.should respond_to(:toString)
688-
# obj.toString().should == '[object Object]'
689-
# end
685+
it "can call a javascript method directly from a ruby object" do
686+
obj = @cxt.eval('Object').new
687+
expect(obj).to respond_to(:toString)
688+
expect(obj.toString()).to eql '[object Object]'
689+
end
690690

691-
# xit "can access properties defined on a javascript object through ruby" do
692-
# obj = @cxt.eval('({ str: "bar", num: 5 })')
693-
# obj.str.should == "bar"
694-
# obj.num.should == 5
695-
# end
691+
it "can access properties defined on a javascript object through ruby" do
692+
obj = @cxt.eval('({ str: "bar", num: 5 })')
693+
expect(obj.str).to eql "bar"
694+
expect(obj.num).to eql 5
695+
end
696696

697697
# xit "can set properties on the javascript object via ruby setter methods" do
698698
# obj = @cxt.eval('({ str: "bar", num: 5 })')
@@ -706,13 +706,11 @@
706706
# obj.array.to_a.should == [1,2,3]
707707
# end
708708

709-
# xit "is an error to try and pass parameters to a property" do
710-
# obj = @cxt.eval('({num: 1})')
711-
# lambda {
712-
# obj.num(5)
713-
# }.should raise_error(ArgumentError)
714-
# end
715-
# end
709+
it "is an error to try and pass parameters to a property" do
710+
obj = @cxt.eval('({num: 1})')
711+
expect { obj.num(5) }.to raise_error(ArgumentError)
712+
end
713+
end
716714

717715
# describe "Setting up the Host Environment", :compat => '0.1.0' do
718716

0 commit comments

Comments
 (0)