Class: Nashorn::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/nashorn/context.rb

Overview

Note:

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

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

Constant Summary collapse

ENGINE_SCOPE =
javax.script.ScriptContext.ENGINE_SCOPE
NashornScriptEngineFactory =
JS::NashornScriptEngineFactory
NASHORN_GLOBAL =
JS::NashornScriptEngine::NASHORN_GLOBAL.to_java
SimpleScriptContext =
javax.script.SimpleScriptContext
FILENAME =
javax.script.ScriptEngine.FILENAME
ScriptException =
javax.script.ScriptException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) {|_self| ... } ⇒ Context

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

Yields:

  • (_self)

Yield Parameters:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



88
89
90
# File 'lib/nashorn/context.rb', line 88

def scope
  @scope
end

Class Method Details

.eval(source, options = nil) ⇒ Object



25
26
27
# File 'lib/nashorn/context.rb', line 25

def eval(source, options = nil)
  new(options).eval(source)
end

.open(options = nil, &block) ⇒ Object



21
22
23
# File 'lib/nashorn/context.rb', line 21

def open(options = nil, &block)
  new(options).open(&block)
end

Instance Method Details

#[](key) ⇒ Object

Read a value from the global scope of this context



91
92
93
# File 'lib/nashorn/context.rb', line 91

def [](key)
  @scope[key]
end

#[]=(key, val) ⇒ Object

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.



97
98
99
# File 'lib/nashorn/context.rb', line 97

def []=(key, val)
  @scope[key] = val
end

#eval(source, filename = nil, line = nil) ⇒ Object

Evaluate a String/IO of JavaScript in this context.



105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



117
# File 'lib/nashorn/context.rb', line 117

def evaluate(source, filename = nil); eval(source, filename) end

#factoryObject



86
# File 'lib/nashorn/context.rb', line 86

def factory; @native.getFactory end

#javascript_versionObject Also known as: version

Get the JavaScript language version.



136
137
138
139
140
141
142
143
# 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

#javascript_version=(version) ⇒ Object Also known as: version=



147
148
149
# 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

#language_versionObject



130
131
132
# File 'lib/nashorn/context.rb', line 130

def language_version
  factory.getLanguageVersion
end

#load(filename) ⇒ Object

Read the contents of filename and evaluate it as JavaScript.

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

Returns:

  • the result of evaluating the JavaScript



124
125
126
127
128
# File 'lib/nashorn/context.rb', line 124

def load(filename)
  File.open(filename) do |file|
    eval file, filename
  end
end

#openObject



155
156
157
158
159
160
161
# 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