Module: Rhino::Ruby::FunctionCall

Included in:
Java::OrgMozillaJavascript::BaseFunction
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.



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rhino/rhino_ext.rb', line 195

def call(*args)
  with_context do |context|
    begin
      # 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 = current_scope(context))
      Rhino.to_ruby __call__(context, scope, this, js_args)
    rescue Rhino::JS::JavaScriptException => e
      raise Rhino::JSError.new(e)
    end
  end
end