Class: Java::OrgMozillaJavascript::BaseFunction

Inherits:
Object
  • Object
show all
Includes:
Rhino::Ruby::FunctionCall
Defined in:
lib/rhino/rhino_ext.rb

Overview

The base class for all JavaScript function objects.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rhino::Ruby::FunctionCall

#call

Class Method Details

.inherited(klass) ⇒ Object



222
223
224
225
226
227
# File 'lib/rhino/rhino_ext.rb', line 222

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 !



254
255
256
257
258
259
260
261
262
263
# File 'lib/rhino/rhino_ext.rb', line 254

def apply(this, *args)
  with_context do |context|
    begin
      args = Rhino.args_to_javascript(args, scope = current_scope(context))
      __call__(context, scope, Rhino.to_javascript(this), args)
    rescue Rhino::JS::JavaScriptException => e
      raise Rhino::JSError.new(e)
    end
  end
end

#bind(this, *args) ⇒ Object

bind a JavaScript function into the given (this) context



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

def bind(this, *args)
  with_context do |context|
    args = Rhino.args_to_javascript(args, scope = current_scope(context))
    Rhino::JS::BoundFunction.new(context, scope, self, Rhino.to_javascript(this), args)
  end
end

#new(*args) ⇒ Object

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



238
239
240
241
242
243
244
245
246
247
# File 'lib/rhino/rhino_ext.rb', line 238

def new(*args)
  with_context do |context|
    begin
      scope = current_scope(context)
      construct(context, scope, Rhino.args_to_javascript(args, scope))
    rescue Rhino::JS::JavaScriptException => e
      raise Rhino::JSError.new(e)
    end
  end
end