Class: Rhino::NativeFunction

Inherits:
NativeObject show all
Defined in:
lib/rhino/native_function.rb

Overview

Wraps a function that has been defined in Javascript so that it can be referenced and called from javascript. e.g.

plus = Rhino::Context.open do |cx|
  cx.eval('function(lhs, rhs) {return lhs + rhs}')
end
plus.call(5,4) # => 9

Instance Attribute Summary

Attributes inherited from NativeObject

#j

Instance Method Summary collapse

Methods inherited from NativeObject

#[], #[]=, #each, #initialize, #to_h, #to_json

Constructor Details

This class inherits a constructor from Rhino::NativeObject

Instance Method Details

#call(*args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rhino/native_function.rb', line 13

def call(*args)
  cxt = J::Context.enter()
  scope = @j.getParentScope() || cxt.initStandardObjects()
  @j.call(cxt, scope, scope, args.map {|o| To.javascript(o)})
ensure
  J::Context.exit()
end

#methodcall(this, *args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rhino/native_function.rb', line 21

def methodcall(this, *args)
  cxt = J::Context.enter()
  scope = @j.getParentScope() || cxt.initStandardObjects()
  @j.call(cxt, scope, To.javascript(this), args.map {|o| To.javascript(o)})
ensure
  J::Context.exit()
end