Class: Nashorn::Context
- Inherits:
-
Object
- Object
- Nashorn::Context
- Defined in:
- lib/nashorn/context.rb
Overview
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
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Read a value from the global scope of this context.
-
#[]=(key, val) ⇒ Object
Set a value in the global scope of this context.
-
#eval(source, filename = nil, line = nil) ⇒ Object
Evaluate a String/IO of JavaScript in this context.
- #evaluate(source, filename = nil) ⇒ Object
- #factory ⇒ Object
-
#initialize(options = nil) {|_self| ... } ⇒ Context
constructor
Create a new JavaScript environment for executing JS (and Ruby) code.
-
#javascript_version ⇒ Object
(also: #version)
Get the JavaScript language version.
- #javascript_version=(version) ⇒ Object (also: #version=)
- #language_version ⇒ Object
-
#load(filename) ⇒ Object
Read the contents of
filename
and evaluate it as JavaScript. - #open ⇒ Object
Constructor Details
#initialize(options = nil) {|_self| ... } ⇒ Context
Create a new JavaScript environment for executing JS (and Ruby) code.
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( = nil) if .is_a?(Hash) factory = [:factory] with = [:with] java = [:java] version = [:javascript_version] || [:language_version] if version (args ||= []).push '--language', version.to_s end if .key?(:strict) (args ||= []).push '-strict', (!![:strict]).to_s end if .key?(:scripting) (args ||= []).push '-scripting', (!![:scripting]).to_s end elsif .is_a?(String) args = .split(' ') elsif .is_a?(Array) args = 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
#scope ⇒ Object (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, = nil) new().eval(source) end |
.open(options = nil, &block) ⇒ Object
21 22 23 |
# File 'lib/nashorn/context.rb', line 21 def open( = nil, &block) new().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 |
#factory ⇒ Object
86 |
# File 'lib/nashorn/context.rb', line 86 def factory; @native.getFactory end |
#javascript_version ⇒ Object 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_version ⇒ Object
130 131 132 |
# File 'lib/nashorn/context.rb', line 130 def language_version factory.getLanguageVersion end |
#load(filename) ⇒ Object
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 |