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

delegate to aliased JS::Function#call when invoking a function property (thus it works happily besides Ruby's #call); changed this from current scope to null when invoking fn from Ruby

This commit is contained in:
kares 2012-01-10 10:43:55 +01:00
parent bc3ca955cc
commit 07f7d5a5b2
2 changed files with 15 additions and 10 deletions

View file

@ -96,8 +96,9 @@ class Java::OrgMozillaJavascript::ScriptableObject
if property.is_a?(Rhino::JS::Function)
begin
context = Rhino::JS::Context.enter
scope = current_scope(context)
js_args = Rhino.args_to_javascript(args, self) # scope == self
Rhino.to_ruby property.call(context, self, s_name, js_args)
Rhino.to_ruby property.__call__(context, scope, self, js_args)
ensure
Rhino::JS::Context.exit
end
@ -113,6 +114,12 @@ class Java::OrgMozillaJavascript::ScriptableObject
end
end
protected
def current_scope(context)
getParentScope || context.initStandardObjects
end
end
class Java::OrgMozillaJavascript::NativeObject
@ -139,29 +146,27 @@ end
# The base class for all JavaScript function objects.
class Java::OrgMozillaJavascript::BaseFunction
alias_method :__call__, :call # Rhino's Function#call(a1, a2, a3, a4)
# Object call(Context context, Scriptable scope, Scriptable this, Object[] args)
alias_method :__call__, :call
# make JavaScript functions callable Ruby style e.g. `fn.call('42')`
def call(*args)
context = Rhino::JS::Context.enter
scope = getParentScope || context.initStandardObjects
__call__(context, scope, scope, Rhino.args_to_javascript(args, scope))
context = Rhino::JS::Context.enter; scope = current_scope(context)
__call__(context, scope, nil, Rhino.args_to_javascript(args, scope))
ensure
Rhino::JS::Context.exit
end
# use JavaScript functions constructors from Ruby as `fn.new`
def new(*args)
context = Rhino::JS::Context.enter
scope = getParentScope || context.initStandardObjects
context = Rhino::JS::Context.enter; scope = current_scope(context)
construct(context, scope, Rhino.args_to_javascript(args, scope))
ensure
Rhino::JS::Context.exit
end
def methodcall(this, *args)
context = Rhino::JS::Context.enter
scope = getParentScope || context.initStandardObjects
context = Rhino::JS::Context.enter; scope = current_scope(context)
__call__(context, scope, Rhino.to_javascript(this), Rhino.args_to_javascript(args, scope))
ensure
Rhino::JS::Context.exit

View file

@ -17,11 +17,11 @@ module Rhino
when NilClass then object
when String, Numeric then object
when TrueClass, FalseClass then object
when JS::Scriptable then object
when Array then array_to_javascript(object, scope)
when Hash then hash_to_javascript(object, scope)
when Time then time_to_javascript(object, scope)
when Proc, Method then RubyFunction.wrap(object, scope)
when JS::Scriptable then object
when Class then RubyConstructor.wrap(object, scope)
else RubyObject.wrap(object, scope)
end