Module: Rhino::Ruby::FunctionCall

Defined in:
lib/rhino/rhino_ext.rb

Instance Method Summary collapse

Instance Method Details

#call(*args) ⇒ Object

make JavaScript functions callable Ruby style e.g. ‘fn.call(’42’)‘

NOTE: That invoking #call does not have the same semantics as JavaScript’s Function#call but rather as Ruby’s Method#call ! Use #apply or #bind before calling to achieve the same effect.



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rhino/rhino_ext.rb', line 185

def call(*args)
  context = Rhino::JS::Context.enter; scope = current_scope(context)
  # 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)
  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
  Rhino::JS::Context.exit
end