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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(klass) ⇒ Object



210
211
212
213
214
215
# File 'lib/rhino/rhino_ext.rb', line 210

def self.inherited(klass)
  # NOTE: in JRuby < 9.3 inherited won't be called for a Java class
  # ... so this happens pretty much only for `Rhino::Ruby::Function`
  klass.send(:include, Rhino::Ruby::FunctionCall)
  super(klass)
end

Instance Method Details

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



241
242
243
244
245
246
247
248
249
# File 'lib/rhino/rhino_ext.rb', line 241

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



218
219
220
221
222
223
224
# File 'lib/rhino/rhino_ext.rb', line 218

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

#new(*args) ⇒ Object

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



227
228
229
230
231
232
233
234
# File 'lib/rhino/rhino_ext.rb', line 227

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