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)



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

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 !



188
189
190
191
192
193
194
# File 'lib/rhino/rhino_ext.rb', line 188

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)
ensure
  Rhino::JS::Context.exit
end

#bind(this, *args) ⇒ Object

bind a JavaScript function into the given (this) context



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

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.



156
157
158
159
160
161
162
163
164
# File 'lib/rhino/rhino_ext.rb', line 156

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))
ensure
  Rhino::JS::Context.exit
end

#new(*args) ⇒ Object

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



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

def new(*args)
  context = Rhino::JS::Context.enter; scope = current_scope(context)
  construct(context, scope, Rhino.args_to_javascript(args, scope))
ensure
  Rhino::JS::Context.exit
end