Class: Java::OrgMozillaJavascript::BaseFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/rhino/rhino_ext.rb

Overview

The base class for all JavaScript function objects.

Instance Method Summary collapse

Instance Method Details

#__call__Object

Object call(Context context, Scriptable scope, Scriptable this, Object[] args)



156
# File 'lib/rhino/rhino_ext.rb', line 156

alias_method :__call__, :call

#apply(this, *args) ⇒ Object Also known as: methodcall

apply a function with the given context and (optional) arguments e.g. ‘fn.apply(obj, 1, 2)`

NOTE: That #call from Ruby does not have the same semantics as JavaScript’s Function#call but rather as Ruby’s Method#call !



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

def apply(this, *args)
  context = Rhino::JS::Context.enter; scope = current_scope(context)
  args = Rhino.args_to_javascript(args, scope)
  __call__(context, scope, Rhino.to_javascript(this), args)
rescue Rhino::JS::JavaScriptException => e
  raise Rhino::JSError.new(e)
ensure
  Rhino::JS::Context.exit
end

#bind(this, *args) ⇒ Object

bind a JavaScript function into the given (this) context



176
177
178
179
180
181
182
# File 'lib/rhino/rhino_ext.rb', line 176

def bind(this, *args)
  context = Rhino::JS::Context.enter; scope = current_scope(context)
  args = Rhino.args_to_javascript(args, scope)
  Rhino::JS::BoundFunction.new(context, scope, self, Rhino.to_javascript(this), args)
ensure
  Rhino::JS::Context.exit    
end

#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.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rhino/rhino_ext.rb', line 163

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)
  __call__(context, scope, this, Rhino.args_to_javascript(args, scope))
rescue Rhino::JS::JavaScriptException => e
  raise Rhino::JSError.new(e)
ensure
  Rhino::JS::Context.exit
end

#new(*args) ⇒ Object

use JavaScript functions constructors from Ruby as ‘fn.new`



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

def new(*args)
  context = Rhino::JS::Context.enter; scope = current_scope(context)
  construct(context, scope, Rhino.args_to_javascript(args, scope))
rescue Rhino::JS::JavaScriptException => e
  raise Rhino::JSError.new(e)
ensure
  Rhino::JS::Context.exit
end