From 0a38ab3f0af81b95ef9e176e7d69cad61f777ea4 Mon Sep 17 00:00:00 2001 From: kares Date: Sat, 8 Sep 2012 15:38:01 +0200 Subject: [PATCH] function's return value should be converted to Ruby (when invoked Ruby #call style) --- lib/rhino/rhino_ext.rb | 3 ++- spec/rhino/rhino_ext_spec.rb | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/rhino/rhino_ext.rb b/lib/rhino/rhino_ext.rb index ab33d6f..860f677 100644 --- a/lib/rhino/rhino_ext.rb +++ b/lib/rhino/rhino_ext.rb @@ -165,7 +165,8 @@ class Java::OrgMozillaJavascript::BaseFunction # calling as a (var) stored function - no this === undefined "use strict" # TODO can't pass Undefined.instance as this - it's not a Scriptable !? this = Rhino::JS::ScriptRuntime.getGlobal(context) - __call__(context, scope, this, Rhino.args_to_javascript(args, scope)) + js_args = Rhino.args_to_javascript(args, scope) + Rhino.to_ruby __call__(context, scope, this, js_args) rescue Rhino::JS::JavaScriptException => e raise Rhino::JSError.new(e) ensure diff --git a/spec/rhino/rhino_ext_spec.rb b/spec/rhino/rhino_ext_spec.rb index 10935c7..fd7dac4 100644 --- a/spec/rhino/rhino_ext_spec.rb +++ b/spec/rhino/rhino_ext_spec.rb @@ -158,10 +158,22 @@ describe "NativeFunction" do it_should_behave_like 'ScriptableObject' - it 'is callable' do + it 'is (Ruby) callable' do # NOTE: no implicit or bound this thus this === global @object.call.should == '[object global]' end + + it 'is (Ruby) callable passing arguments' do + js = "( function foo(arg) { return 'foo' + arg; } )" + foo = @context.evaluateString(@scope, js, '', 0, nil) + foo.call(42).should == 'foo42' + end + + it 'is (Ruby) callable converting result' do + js = "( function foo(arg) { return [ 1, 2, arg ]; } )" + foo = @context.evaluateString(@scope, js, '', 0, nil) + foo.call('x').should == [ 1, 2, 'x' ] + end it 'might be bind and called' do @object.bind(@object).should be_a(Rhino::JS::Function)