Skip to content

Commit

Permalink
function's return value should be converted to Ruby
Browse files Browse the repository at this point in the history
(when invoked Ruby #call style)
  • Loading branch information
kares committed Sep 8, 2012
1 parent fc7b183 commit 0a38ab3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/rhino/rhino_ext.rb
Expand Up @@ -165,7 +165,8 @@ def call(*args)
# 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
Expand Down
14 changes: 13 additions & 1 deletion spec/rhino/rhino_ext_spec.rb
Expand Up @@ -158,10 +158,22 @@ def to_h_properties

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, '<eval>', 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, '<eval>', 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)
Expand Down

0 comments on commit 0a38ab3

Please sign in to comment.