module Nashorn::JS
@private
Constants
- JavaScriptException
- RhinoException
- ScriptObject
include_package 'jdk.nashorn.internal.runtime'
- Scriptable
Public Instance Methods
# File lib/nashorn/ext.rb, line 11 def [](key) Nashorn.to_rb key.is_a?(Fixnum) ? getSlot(key) : getMember(key.to_s) end
# File lib/nashorn/ext.rb, line 15 def []=(key, value) js_val = Nashorn.to_js value key.is_a?(Fixnum) ? setSlot(key, js_val) : setMember(key.to_s, js_val) js_val end
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 !
# File lib/nashorn/ext.rb, line 132 def apply(this, *args) __call__ Nashorn.to_js(this), *Nashorn.args_to_js(args) rescue JS::NashornException => e raise Nashorn::JSError.new(e) end
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.
# File lib/nashorn/ext.rb, line 121 def call(*args) Nashorn.to_rb __call__ nil, *Nashorn.args_to_js(args) # this = nil rescue JS::NashornException => e raise Nashorn::JSError.new(e) end
# File lib/nashorn/ext.rb, line 28 def delete(key); removeMember(key) end
enumerate the key value pairs contained in this javascript object. e.g.
eval_js("{foo: 'bar', baz: 'bang'}").each do |key,value| puts "#{key} -> #{value} " end
outputs foo -> bar baz -> bang
# File lib/nashorn/ext.rb, line 41 def each each_raw { |key, val| yield key, Nashorn.to_rb(val) } end
# File lib/nashorn/ext.rb, line 46 def each_key each_raw { |key, val| yield key } end
# File lib/nashorn/ext.rb, line 54 def each_raw for id in keySet do yield id, getMember(id) end end
# File lib/nashorn/ext.rb, line 50 def each_value each_raw { |key, val| yield Nashorn.to_rb(val) } end
# File lib/nashorn/ext.rb, line 21 def has_key?(key); hasMember(key) end
# File lib/nashorn/ext.rb, line 25 def has_value?(val); raw_values.include?(val) end
# File lib/nashorn/ext.rb, line 60 def keys keySet.to_a end
# File lib/nashorn/ext.rb, line 30 def length; keySet.size end
Delegate methods to JS
object if possible when called from Ruby
.
# File lib/nashorn/ext.rb, line 92 def method_missing(name, *args) name_str = name.to_s if name_str.end_with?('=') && args.size == 1 # writer -> JS put self[ name_str[0...-1] ] = args[0] else if hasMember(name_str) && property = getMember(name_str) if property.is_a?(JSObject) && property.isFunction Nashorn.to_rb property.__call__(self, *Nashorn.args_to_js(args)) else if args.size > 0 raise ArgumentError, "can't call '#{name_str}' with args: #{args.inspect} as it's a property" end Nashorn.to_rb property end else super end end end
use JavaScript functions constructors from Ruby
as `fn.new`
# File lib/nashorn/ext.rb, line 146 def new(*args) newObject *Nashorn.args_to_js(args) rescue JS::NashornException => e raise Nashorn::JSError.new(e) end
Converts the native object to a hash. This isn't really a stretch since it's pretty much a hash in the first place.
# File lib/nashorn/ext.rb, line 70 def to_h hash = {} each do |key, val| hash[key] = val.is_a?(JSObject) && ! val.equal?(self) ? val.to_h : val end hash end
Convert this javascript object into a json string.
# File lib/nashorn/ext.rb, line 87 def to_json(*args) to_h.to_json(*args) end
# File lib/nashorn/ext.rb, line 64 def values raw_values.map { |val| Nashorn.to_rb(val) } end