Class: Java::OrgMozillaJavascript::BaseFunction
- Defined in:
- lib/rhino/rhino_ext.rb
Overview
The base class for all JavaScript function objects.
Instance Method Summary collapse
-
#__call__ ⇒ Object
Object call(Context context, Scriptable scope, Scriptable this, Object[] args).
-
#apply(this, *args) ⇒ Object
(also: #methodcall)
apply a function with the given context and (optional) arguments e.g.
-
#bind(this, *args) ⇒ Object
bind a JavaScript function into the given (this) context.
-
#call(*args) ⇒ Object
make JavaScript functions callable Ruby style e.g.
-
#new(*args) ⇒ Object
use JavaScript functions constructors from Ruby as ‘fn.new`.
Instance Method Details
#__call__ ⇒ Object
Object call(Context context, Scriptable scope, Scriptable this, Object[] args)
180 |
# File 'lib/rhino/rhino_ext.rb', line 180 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 !
224 225 226 227 228 229 230 231 232 |
# File 'lib/rhino/rhino_ext.rb', line 224 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
201 202 203 204 205 206 207 |
# File 'lib/rhino/rhino_ext.rb', line 201 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.
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rhino/rhino_ext.rb', line 187 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) js_args = Rhino.args_to_javascript(args, scope) Rhino.to_ruby __call__(context, scope, this, js_args) 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`
210 211 212 213 214 215 216 217 |
# File 'lib/rhino/rhino_ext.rb', line 210 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 |