module Nashorn::JS

@private

Constants

JavaScriptException
RhinoException
ScriptObject

include_package 'jdk.nashorn.internal.runtime'

Scriptable

Public Instance Methods

[](key) click to toggle source
# File lib/nashorn/ext.rb, line 11
def [](key)
  Nashorn.to_rb key.is_a?(Fixnum) ? getSlot(key) : getMember(key.to_s)
end
[]=(key, value) click to toggle source
# 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(this, *args) click to toggle source

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
call(*args) click to toggle source

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
delete(key) click to toggle source
# File lib/nashorn/ext.rb, line 28
def delete(key); removeMember(key) end
each() { |key, to_rb| ... } click to toggle source

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
each_key() { |key| ... } click to toggle source
# File lib/nashorn/ext.rb, line 46
def each_key
  each_raw { |key, val| yield key }
end
each_raw() { |id, getMember(id)| ... } click to toggle source
# File lib/nashorn/ext.rb, line 54
def each_raw
  for id in keySet do
    yield id, getMember(id)
  end
end
each_value() { |to_rb| ... } click to toggle source
# File lib/nashorn/ext.rb, line 50
def each_value
  each_raw { |key, val| yield Nashorn.to_rb(val) }
end
has_key?(key) click to toggle source
# File lib/nashorn/ext.rb, line 21
def has_key?(key); hasMember(key) end
has_value?(val) click to toggle source
# File lib/nashorn/ext.rb, line 25
def has_value?(val); raw_values.include?(val) end
keys() click to toggle source
# File lib/nashorn/ext.rb, line 60
def keys
  keySet.to_a
end
length() click to toggle source
# File lib/nashorn/ext.rb, line 30
def length; keySet.size end
method_missing(name, *args) click to toggle source

Delegate methods to JS object if possible when called from Ruby.

Calls superclass method
# 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
new(*args) click to toggle source

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
to_h() click to toggle source

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
to_json(*args) click to toggle source

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
values() click to toggle source
# File lib/nashorn/ext.rb, line 64
def values
  raw_values.map { |val| Nashorn.to_rb(val) }
end