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

Merge pull request #375 from cowboyd/4.5/call-js-code-from-ruby

4.5/call js code from ruby
This commit is contained in:
Charles Lowell 2015-08-01 00:01:43 +03:00
commit bafd35c278
3 changed files with 44 additions and 40 deletions

View file

@ -111,6 +111,12 @@ module V8::C
::V8::Function.new self ::V8::Function.new self
end end
end end
class Value
def to_v8(context)
self
end
end
end end
## ##

View file

@ -22,7 +22,7 @@ class V8::Function < V8::Object
def new(*args) def new(*args)
@context.enter do @context.enter do
@context.to_ruby try {native.NewInstance(args.map {|a| @context.to_v8 a})} @context.to_ruby native.NewInstance(args.map {|a| @context.to_v8 a})
end end
end end
end end

View file

@ -654,45 +654,45 @@ describe "V8::Context" do
# end # end
# describe "Calling JavaScript Code From Within Ruby", :compat => '0.1.0' do describe "Calling JavaScript Code From Within Ruby", :compat => '0.1.0' do
# before(:each) do before(:each) do
# @cxt = V8::Context.new @cxt = V8::Context.new
# end end
# xit "allows you to capture a reference to a javascript function and call it" do it "allows you to capture a reference to a javascript function and call it" do
# f = @cxt.eval('(function add(lhs, rhs) {return lhs + rhs})') f = @cxt.eval('(function add(lhs, rhs) {return lhs + rhs})')
# f.call(1,2).should == 3 expect(f.call 1,2).to eql 3
# end end
# xit "can path the 'this' object into a function as context with methodcall()" do it "can path the 'this' object into a function as context with methodcall()" do
# obj = @cxt.eval('({num: 5})') obj = @cxt.eval('({num: 5})')
# times = @cxt.eval('(function times(num) {return this.num * num})') times = @cxt.eval('(function times(num) {return this.num * num})')
# times.methodcall(obj, 5).should == 25 expect(times.methodcall obj, 5).to eql 25
# end end
# xit "unwraps objects that are backed by javascript objects to pass their native equivalents" do it "unwraps objects that are backed by javascript objects to pass their native equivalents" do
# @cxt.eval('obj = {foo: "bar"}') @cxt.eval('obj = {foo: "bar"}')
# f = @cxt.eval('(function() {return this == obj})') f = @cxt.eval('(function() {return this == obj})')
# f.methodcall(@cxt['obj']).should be(true) expect(f.methodcall @cxt['obj']).to be true
# end end
# xit "can invoke a javacript constructor and return the new object reflected into ruby" do it "can invoke a javacript constructor and return the new object reflected into ruby" do
# wrapper = @cxt.eval('(function Wrapper(value) {this.value = value})') wrapper = @cxt.eval('(function Wrapper(value) {this.value = value})')
# wrapper.new(5)['value'].should == 5 wrapper.new(5)['value'].should == 5
# end end
# xit "can call a javascript method directly from a ruby object" do it "can call a javascript method directly from a ruby object" do
# obj = @cxt.eval('Object').new obj = @cxt.eval('Object').new
# obj.should respond_to(:toString) expect(obj).to respond_to(:toString)
# obj.toString().should == '[object Object]' expect(obj.toString()).to eql '[object Object]'
# end end
# xit "can access properties defined on a javascript object through ruby" do it "can access properties defined on a javascript object through ruby" do
# obj = @cxt.eval('({ str: "bar", num: 5 })') obj = @cxt.eval('({ str: "bar", num: 5 })')
# obj.str.should == "bar" expect(obj.str).to eql "bar"
# obj.num.should == 5 expect(obj.num).to eql 5
# end end
# xit "can set properties on the javascript object via ruby setter methods" do # xit "can set properties on the javascript object via ruby setter methods" do
# obj = @cxt.eval('({ str: "bar", num: 5 })') # obj = @cxt.eval('({ str: "bar", num: 5 })')
@ -706,13 +706,11 @@ describe "V8::Context" do
# obj.array.to_a.should == [1,2,3] # obj.array.to_a.should == [1,2,3]
# end # end
# xit "is an error to try and pass parameters to a property" do it "is an error to try and pass parameters to a property" do
# obj = @cxt.eval('({num: 1})') obj = @cxt.eval('({num: 1})')
# lambda { expect { obj.num(5) }.to raise_error(ArgumentError)
# obj.num(5) end
# }.should raise_error(ArgumentError) end
# end
# end
# describe "Setting up the Host Environment", :compat => '0.1.0' do # describe "Setting up the Host Environment", :compat => '0.1.0' do