class Nashorn::Context

JavaScript gets executed in a context which represents the execution environment in which scripts run. The environment consists of the standard JavaScript objects and functions like `Object`, `parseInt()` or `null`, as well as any objects or functions which have been defined in it. e.g.

Context.open do |js|
  js['answer'] = 22
  js['compute'] = lambda { |t| 10 * t }
  js.eval('num + compute(2)') #=> 42
end

@note Context are isolated, multiple context do not share any JS objects!

Constants

ENGINE_SCOPE

@private

FILENAME

@private

NASHORN_GLOBAL

@private

NashornScriptEngineFactory

@private

ScriptException

@private

SimpleScriptContext

@private

Attributes

scope[R]

Public Class Methods

eval(source, options = nil) click to toggle source
# File lib/nashorn/context.rb, line 25
def eval(source, options = nil)
  new(options).eval(source)
end
new(options = nil) { |self| ... } click to toggle source

Create a new JavaScript environment for executing JS (and Ruby) code.

# File lib/nashorn/context.rb, line 41
def initialize(options = nil)
  if options.is_a?(Hash)
    factory = options[:factory]
    with = options[:with]
    java = options[:java]
    version = options[:javascript_version] || options[:language_version]
    if version
      (args ||= []).push '--language', version.to_s
    end
    if options.key?(:strict)
      (args ||= []).push '-strict', (!!options[:strict]).to_s
    end
    if options.key?(:scripting)
      (args ||= []).push '-scripting', (!!options[:scripting]).to_s
    end
  elsif options.is_a?(String)
    args = options.split(' ')
  elsif options.is_a?(Array)
    args = options
  end
  factory ||= NashornScriptEngineFactory.new
  with ||= nil
  java = true if java.nil?

  @native = args ? factory.getScriptEngine(args.to_java) : factory.getScriptEngine

  #simple_context = SimpleScriptContext.new
  #bindings = @native.getBindings(ENGINE_SCOPE)
  #global = bindings.get(NASHORN_GLOBAL)

  @scope = global = @native.eval('this')

  if with
    #bindings.set(NASHORN_GLOBAL, @scope = Nashorn.to_js(with))
    object = @native.eval('Object')
    @native.invokeMethod(object, 'bindProperties', global, Nashorn.to_js(with))
  end
  unless java
    [ 'java', 'javax', 'org', 'com', 'Packages', 'Java' ].each do |name|
      global.removeMember(name)
    end
  end
  yield(self) if block_given?
end
open(options = nil, &block) click to toggle source
# File lib/nashorn/context.rb, line 21
def open(options = nil, &block)
  new(options).open(&block)
end

Public Instance Methods

[](key) click to toggle source

Read a value from the global scope of this context

# File lib/nashorn/context.rb, line 91
def [](key)
  @scope[key]
end
[]=(key, val) click to toggle source

Set a value in the global scope of this context. This value will be visible to all the javascript that is executed in this context.

# File lib/nashorn/context.rb, line 97
def []=(key, val)
  @scope[key] = val
end
eval(source, filename = nil, line = nil) click to toggle source

Evaluate a String/IO of JavaScript in this context.

# File lib/nashorn/context.rb, line 105
def eval(source, filename = nil, line = nil)
  open do
    if IO === source || StringIO === source
      source = IOReader.new(source)
    else
      source = source.to_s
    end
    @native.put(FILENAME, filename) if filename
    Nashorn.to_rb @native.eval(source, @scope)
  end
end
evaluate(source, filename = nil) click to toggle source
# File lib/nashorn/context.rb, line 117
def evaluate(source, filename = nil); eval(source, filename) end
factory() click to toggle source
# File lib/nashorn/context.rb, line 86
def factory; @native.getFactory end
javascript_version() click to toggle source

Get the JavaScript language version. @private

# File lib/nashorn/context.rb, line 136
def javascript_version
  case version = language_version
    when nil, '' then nil
    #when 'es5'   then 1.5 # default
    #when 'es6'   then 1.6
    else version
  end
end
Also aliased as: version
javascript_version=(version) click to toggle source

@private

# File lib/nashorn/context.rb, line 147
def javascript_version=(version)
  warn "#{self}#javascript_version = not supported, use open(javascript_version: #{version.inspect}) instead"
end
Also aliased as: version=
language_version() click to toggle source
# File lib/nashorn/context.rb, line 130
def language_version
  factory.getLanguageVersion
end
load(filename) click to toggle source

Read the contents of filename and evaluate it as JavaScript.

Context.open { |js_env| js_env.load("path/to/some/lib.js") }

@return the result of evaluating the JavaScript

# File lib/nashorn/context.rb, line 124
def load(filename)
  File.open(filename) do |file|
    eval file, filename
  end
end
open() { |self| ... } click to toggle source
# File lib/nashorn/context.rb, line 155
def open
  yield self
rescue ScriptException => e
  raise JSError.new(e)
rescue JS::NashornException => e
  raise JSError.new(e)
end
version()
Alias for: javascript_version
version=(version)
Alias for: javascript_version=