module Nashorn

Constants

DEEP_UNMIRROR
RubyConstructor
RubyFunction
RubyObject

@private

ScriptObject
ScriptObjectMirror
VERSION

Public Class Methods

args_to_js(args, to_java = false) click to toggle source
# File lib/nashorn/convert.rb, line 47
def args_to_js(args, to_java = false)
  args = args.map { |arg| to_js(arg) }
  to_java ? args.to_java : args
end
args_to_rb(args) click to toggle source
# File lib/nashorn/convert.rb, line 43
def args_to_rb(args)
  args.map { |arg| to_rb(arg) }
end
eval(source, options = nil)
Alias for: eval_js
eval_js(source, options = nil) click to toggle source
# File lib/nashorn.rb, line 21
def eval_js(source, options = nil)
  factory = JS::NashornScriptEngineFactory.new
  factory.getScriptEngine.eval(source)
end
Also aliased as: eval
js_mirror_to_rb(object, deep = DEEP_UNMIRROR) click to toggle source
# File lib/nashorn/convert.rb, line 56
def js_mirror_to_rb(object, deep = DEEP_UNMIRROR)
  if object.is_a?(Nashorn::JS::JSObject)
    if object.isArray
      return object.raw_values.to_a unless deep
      object.raw_values.map { |obj| to_rb(obj, true) }
    end
    return object if object.isFunction # TODO CallableHash < Hash?
    # Nashorn::JS::ScriptObjectMirror is already a Map but still :
    hash = {}
    for key in object.keySet
      hash[key] = deep ? to_rb(object[key], true) : object[key]
    end
    hash
  else
    object
  end
end
to_javascript(object)
Alias for: to_js
to_js(object) click to toggle source
# File lib/nashorn/convert.rb, line 26
def to_js(object)
  case object
  when NilClass              then object
  when String, Numeric       then object.to_java
  when TrueClass, FalseClass then object.to_java
  when Nashorn::JS::JSObject then object
  #when Array                 then array_to_js(object)
  #when Hash                  then hash_to_js(object)
  when Time                  then object.to_java
  when Method, UnboundMethod then Nashorn::Ruby::Function.wrap(object)
  when Proc                  then Nashorn::Ruby::Function.wrap(object)
  when Class                 then Nashorn::Ruby::Constructor.wrap(object)
  else Nashorn::Ruby::Object.wrap(object)
  end
end
Also aliased as: to_javascript
to_rb(object, unmirror = false) click to toggle source
# File lib/nashorn/convert.rb, line 8
def to_rb(object, unmirror = false)
  # ConsString for optimized String + operations :
  return object.toString if object.is_a?(Java::JavaLang::CharSequence)
  return object.unwrap if object.is_a?(Nashorn::RubyObject)
  return object.unwrap if object.is_a?(Nashorn::RubyFunction)
  return nil if ScriptObjectMirror.isUndefined(object)
  # NOTE: "correct" Nashorn leaking-out internals :
  if ScriptObject && object.is_a?(ScriptObject)
    # BUGY: Java::JavaLang::ClassCastException:
    #   jdk.nashorn.internal.scripts.JO4 cannot be cast to jdk.nashorn.api.scripting.ScriptObjectMirror
    #   jdk.nashorn.api.scripting.ScriptUtils.wrap(jdk/nashorn/api/scripting/ScriptUtils.java:92)
    # object = Nashorn::JS::ScriptUtils.wrap(object)
  end
  return js_mirror_to_rb(object) if unmirror
  object
end
Also aliased as: to_ruby
to_ruby(object, unmirror = false)
Alias for: to_rb

Private Instance Methods

eval_js(source, options = nil) click to toggle source
# File lib/nashorn.rb, line 21
def eval_js(source, options = nil)
  factory = JS::NashornScriptEngineFactory.new
  factory.getScriptEngine.eval(source)
end
Also aliased as: eval