author | lagergren |
Mon, 04 Feb 2013 16:20:05 +0100 | |
changeset 16210 | 8ad1381b69d0 |
parent 16205 | 93fda2507e35 |
child 16211 | 41e031a45186 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.api.scripting; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
30 |
||
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.InputStreamReader; |
|
34 |
import java.io.Reader; |
|
35 |
import java.net.URL; |
|
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedAction; |
|
38 |
import java.security.PrivilegedActionException; |
|
39 |
import java.security.PrivilegedExceptionAction; |
|
40 |
import javax.script.AbstractScriptEngine; |
|
41 |
import javax.script.Bindings; |
|
42 |
import javax.script.Compilable; |
|
43 |
import javax.script.CompiledScript; |
|
44 |
import javax.script.Invocable; |
|
45 |
import javax.script.ScriptContext; |
|
46 |
import javax.script.ScriptEngine; |
|
47 |
import javax.script.ScriptEngineFactory; |
|
48 |
import javax.script.ScriptException; |
|
49 |
import jdk.nashorn.internal.runtime.Context; |
|
50 |
import jdk.nashorn.internal.runtime.ErrorManager; |
|
51 |
import jdk.nashorn.internal.runtime.GlobalObject; |
|
52 |
import jdk.nashorn.internal.runtime.Property; |
|
53 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
54 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
55 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
56 |
import jdk.nashorn.internal.runtime.Source; |
|
57 |
import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; |
|
58 |
import jdk.nashorn.internal.runtime.options.Options; |
|
59 |
||
60 |
/** |
|
61 |
* JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through |
|
62 |
* {@link NashornScriptEngineFactory#getScriptEngine()}. Note that this engine implements the {@link Compilable} and |
|
63 |
* {@link Invocable} interfaces, allowing for efficient precompilation and repeated execution of scripts. |
|
64 |
* @see NashornScriptEngineFactory |
|
65 |
*/ |
|
66 |
||
67 |
public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { |
|
68 |
||
69 |
private final ScriptEngineFactory factory; |
|
70 |
private final Context nashornContext; |
|
71 |
private final ScriptObject global; |
|
72 |
||
73 |
// default options passed to Nashorn Options object |
|
74 |
private static final String[] DEFAULT_OPTIONS = new String[] { "-scripting", "-af", "-doe" }; |
|
75 |
||
16197
1a5414cce91a
8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents:
16189
diff
changeset
|
76 |
NashornScriptEngine(final NashornScriptEngineFactory factory, final ClassLoader appLoader) { |
1a5414cce91a
8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents:
16189
diff
changeset
|
77 |
this(factory, DEFAULT_OPTIONS, appLoader); |
16147 | 78 |
} |
79 |
||
80 |
@SuppressWarnings("LeakingThisInConstructor") |
|
16197
1a5414cce91a
8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents:
16189
diff
changeset
|
81 |
NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) { |
16147 | 82 |
this.factory = factory; |
83 |
final Options options = new Options("nashorn"); |
|
84 |
options.process(args); |
|
85 |
||
86 |
// throw ParseException on first error from script |
|
16185 | 87 |
final ErrorManager errMgr = new Context.ThrowErrorManager(); |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
88 |
// create new Nashorn Context |
16147 | 89 |
this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() { |
90 |
@Override |
|
91 |
public Context run() { |
|
92 |
try { |
|
16185 | 93 |
return new Context(options, errMgr, appLoader); |
16147 | 94 |
} catch (final RuntimeException e) { |
95 |
if (Context.DEBUG) { |
|
96 |
e.printStackTrace(); |
|
97 |
} |
|
98 |
throw e; |
|
99 |
} |
|
100 |
} |
|
101 |
}); |
|
102 |
||
103 |
// create new global object |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
104 |
this.global = createNashornGlobal(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
105 |
// set the default engine scope for the default context |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
106 |
context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE); |
16147 | 107 |
|
108 |
// evaluate engine initial script |
|
109 |
try { |
|
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
110 |
evalEngineScript(); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
111 |
} catch (final ScriptException e) { |
16147 | 112 |
if (Context.DEBUG) { |
113 |
e.printStackTrace(); |
|
114 |
} |
|
115 |
throw new RuntimeException(e); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException { |
|
121 |
try { |
|
122 |
return evalImpl(Source.readFully(reader), ctxt); |
|
123 |
} catch (final IOException e) { |
|
124 |
throw new ScriptException(e); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
@Override |
|
129 |
public Object eval(final String script, final ScriptContext ctxt) throws ScriptException { |
|
130 |
return evalImpl(script.toCharArray(), ctxt); |
|
131 |
} |
|
132 |
||
133 |
@Override |
|
134 |
public ScriptEngineFactory getFactory() { |
|
135 |
return factory; |
|
136 |
} |
|
137 |
||
138 |
@Override |
|
139 |
public Bindings createBindings() { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
140 |
final ScriptObject newGlobal = createNashornGlobal(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
141 |
return new ScriptObjectMirror(newGlobal, newGlobal); |
16147 | 142 |
} |
143 |
||
144 |
// Compilable methods |
|
145 |
||
146 |
@Override |
|
147 |
public CompiledScript compile(final Reader reader) throws ScriptException { |
|
148 |
try { |
|
149 |
return asCompiledScript(compileImpl(Source.readFully(reader), context)); |
|
150 |
} catch (final IOException e) { |
|
151 |
throw new ScriptException(e); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
@Override |
|
156 |
public CompiledScript compile(final String str) throws ScriptException { |
|
157 |
return asCompiledScript(compileImpl(str.toCharArray(), context)); |
|
158 |
} |
|
159 |
||
160 |
// Invocable methods |
|
161 |
||
162 |
@Override |
|
163 |
public Object invokeFunction(final String name, final Object... args) |
|
164 |
throws ScriptException, NoSuchMethodException { |
|
165 |
return invokeImpl(null, name, args); |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
169 |
public Object invokeMethod(final Object self, final String name, final Object... args) |
|
170 |
throws ScriptException, NoSuchMethodException { |
|
171 |
if (self == null) { |
|
172 |
throw new IllegalArgumentException("script object can not be null"); |
|
173 |
} |
|
174 |
return invokeImpl(self, name, args); |
|
175 |
} |
|
176 |
||
177 |
private <T> T getInterfaceInner(final Object self, final Class<T> clazz) { |
|
178 |
final Object realSelf; |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
179 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(context); |
16147 | 180 |
if(self == null) { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
181 |
realSelf = ctxtGlobal; |
16147 | 182 |
} else if (!(self instanceof ScriptObject)) { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
183 |
realSelf = ScriptObjectMirror.unwrap(self, ctxtGlobal); |
16147 | 184 |
} else { |
185 |
realSelf = self; |
|
186 |
} |
|
187 |
try { |
|
16188 | 188 |
final ScriptObject oldGlobal = getNashornGlobal(); |
16147 | 189 |
try { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
190 |
if(oldGlobal != ctxtGlobal) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
191 |
setNashornGlobal(ctxtGlobal); |
16147 | 192 |
} |
193 |
return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz).invoke(realSelf)); |
|
194 |
} finally { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
195 |
if(oldGlobal != ctxtGlobal) { |
16147 | 196 |
setNashornGlobal(oldGlobal); |
197 |
} |
|
198 |
} |
|
199 |
} catch(final RuntimeException|Error e) { |
|
200 |
throw e; |
|
201 |
} catch(final Throwable t) { |
|
202 |
throw new RuntimeException(t); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public <T> T getInterface(final Class<T> clazz) { |
|
208 |
return getInterfaceInner(null, clazz); |
|
209 |
} |
|
210 |
||
211 |
@Override |
|
212 |
public <T> T getInterface(final Object self, final Class<T> clazz) { |
|
213 |
if (self == null) { |
|
214 |
throw new IllegalArgumentException("script object can not be null"); |
|
215 |
} |
|
216 |
return getInterfaceInner(self, clazz); |
|
217 |
} |
|
218 |
||
219 |
// These are called from the "engine.js" script |
|
220 |
||
221 |
/** |
|
222 |
* This hook is used to search js global variables exposed from Java code. |
|
223 |
* |
|
224 |
* @param self 'this' passed from the script |
|
225 |
* @param ctxt current ScriptContext in which name is searched |
|
226 |
* @param name name of the variable searched |
|
227 |
* @return the value of the named variable |
|
228 |
*/ |
|
229 |
public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) { |
|
230 |
final int scope = ctxt.getAttributesScope(name); |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
231 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
16147 | 232 |
if (scope != -1) { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
233 |
return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal); |
16147 | 234 |
} |
235 |
||
236 |
if (self == UNDEFINED) { |
|
237 |
// scope access and so throw ReferenceError |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
238 |
referenceError(ctxtGlobal, "not.defined", name); |
16147 | 239 |
} |
240 |
||
241 |
return UNDEFINED; |
|
242 |
} |
|
243 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
244 |
private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
245 |
final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
246 |
if (bindings instanceof ScriptObjectMirror) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
247 |
ScriptObject sobj = ((ScriptObjectMirror)bindings).getScriptObject(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
248 |
if (sobj instanceof GlobalObject) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
249 |
return sobj; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
250 |
} |
16147 | 251 |
} |
252 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
253 |
// didn't find global object from context given - return the engine-wide global |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
254 |
return global; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
255 |
} |
16147 | 256 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
257 |
private ScriptObject createNashornGlobal() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
258 |
final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
259 |
@Override |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
260 |
public ScriptObject run() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
261 |
try { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
262 |
return nashornContext.createGlobal(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
263 |
} catch (final RuntimeException e) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
264 |
if (Context.DEBUG) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
265 |
e.printStackTrace(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
266 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
267 |
throw e; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
268 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
269 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
270 |
}); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
271 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
272 |
// current ScriptContext exposed as "context" |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
273 |
newGlobal.addOwnProperty("context", Property.NOT_ENUMERABLE, UNDEFINED); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
274 |
// current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
275 |
// NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
276 |
// in the Global of a Context we just created - both the Context and the Global were just created and can not be |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
277 |
// seen from another thread outside of this constructor. |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
278 |
newGlobal.addOwnProperty("engine", Property.NOT_ENUMERABLE, this); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
279 |
// global script arguments with undefined value |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
280 |
newGlobal.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
281 |
// file name default is null |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
282 |
newGlobal.addOwnProperty(ScriptEngine.FILENAME, Property.NOT_ENUMERABLE, null); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
283 |
return newGlobal; |
16147 | 284 |
} |
285 |
||
286 |
private void evalEngineScript() throws ScriptException { |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
287 |
evalSupportScript("resources/engine.js", NashornException.ENGINE_SCRIPT_SOURCE_NAME); |
16172
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
288 |
} |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
289 |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
290 |
private void evalSupportScript(final String script, final String name) throws ScriptException { |
16147 | 291 |
try { |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
292 |
final InputStream is = AccessController.doPrivileged( |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
293 |
new PrivilegedExceptionAction<InputStream>() { |
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16197
diff
changeset
|
294 |
@Override |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
295 |
public InputStream run() throws Exception { |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
296 |
final URL url = NashornScriptEngine.class.getResource(script); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
297 |
return url.openStream(); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
298 |
} |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
299 |
}); |
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
300 |
put(ScriptEngine.FILENAME, name); |
16147 | 301 |
try (final InputStreamReader isr = new InputStreamReader(is)) { |
302 |
eval(isr); |
|
303 |
} |
|
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
304 |
} catch (final PrivilegedActionException | IOException e) { |
16147 | 305 |
throw new ScriptException(e); |
306 |
} finally { |
|
307 |
put(ScriptEngine.FILENAME, null); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
// scripts should see "context" and "engine" as variables |
|
312 |
private void setContextVariables(final ScriptContext ctxt) { |
|
313 |
ctxt.setAttribute("context", ctxt, ScriptContext.ENGINE_SCOPE); |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
314 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
315 |
ctxtGlobal.set("context", ctxt, false); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
316 |
Object args = ScriptObjectMirror.unwrap(ctxt.getAttribute("arguments"), ctxtGlobal); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
317 |
if (args == null || args == UNDEFINED) { |
16147 | 318 |
args = ScriptRuntime.EMPTY_ARRAY; |
319 |
} |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
320 |
// if no arguments passed, expose it |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
321 |
args = ((GlobalObject)ctxtGlobal).wrapAsObject(args); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
322 |
ctxtGlobal.set("arguments", args, false); |
16147 | 323 |
} |
324 |
||
325 |
private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException { |
|
16188 | 326 |
final ScriptObject oldGlobal = getNashornGlobal(); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
327 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(context); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
328 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 329 |
|
330 |
Object self = selfObject; |
|
331 |
||
332 |
try { |
|
333 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
334 |
setNashornGlobal(ctxtGlobal); |
16147 | 335 |
} |
336 |
||
337 |
ScriptObject sobj; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
338 |
Object value = null; |
16147 | 339 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
340 |
self = ScriptObjectMirror.unwrap(self, ctxtGlobal); |
16147 | 341 |
|
342 |
// FIXME: should convert when self is not ScriptObject |
|
343 |
if (self instanceof ScriptObject) { |
|
344 |
sobj = (ScriptObject)self; |
|
345 |
value = sobj.get(name); |
|
346 |
} else if (self == null) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
347 |
self = ctxtGlobal; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
348 |
sobj = ctxtGlobal; |
16147 | 349 |
value = sobj.get(name); |
350 |
} |
|
351 |
||
352 |
if (value instanceof ScriptFunction) { |
|
353 |
final Object res; |
|
354 |
try { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
355 |
res = ScriptRuntime.apply((ScriptFunction)value, self, ScriptObjectMirror.unwrapArray(args, ctxtGlobal)); |
16147 | 356 |
} catch (final Exception e) { |
357 |
throwAsScriptException(e); |
|
358 |
throw new AssertionError("should not reach here"); |
|
359 |
} |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
360 |
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(res, ctxtGlobal)); |
16147 | 361 |
} |
362 |
||
363 |
throw new NoSuchMethodException(name); |
|
364 |
} finally { |
|
365 |
if (globalChanged) { |
|
366 |
setNashornGlobal(oldGlobal); |
|
367 |
} |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
private Object evalImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException { |
|
372 |
return evalImpl(compileImpl(buf, ctxt), ctxt); |
|
373 |
} |
|
374 |
||
375 |
private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException { |
|
376 |
if (script == null) { |
|
377 |
return null; |
|
378 |
} |
|
16188 | 379 |
final ScriptObject oldGlobal = getNashornGlobal(); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
380 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
381 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 382 |
try { |
383 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
384 |
setNashornGlobal(ctxtGlobal); |
16147 | 385 |
} |
386 |
||
387 |
setContextVariables(ctxt); |
|
16172
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
388 |
final Object val = ctxt.getAttribute(ScriptEngine.FILENAME); |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
389 |
final String fileName = (val != null) ? val.toString() : "<eval>"; |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
390 |
|
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
391 |
// NOTE: FIXME: If this is jrunscript's init.js, we want to run the replacement. |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
392 |
// This should go away once we fix jrunscript's copy of init.js. |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
393 |
if ("<system-init>".equals(fileName)) { |
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
394 |
evalSupportScript("resources/init.js", "nashorn:engine/resources/init.js"); |
16172
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
395 |
return null; |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
396 |
} |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
397 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
398 |
Object res = ScriptRuntime.apply(script, ctxtGlobal); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
399 |
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(res, ctxtGlobal)); |
16147 | 400 |
} catch (final Exception e) { |
401 |
throwAsScriptException(e); |
|
402 |
throw new AssertionError("should not reach here"); |
|
403 |
} finally { |
|
404 |
if (globalChanged) { |
|
405 |
setNashornGlobal(oldGlobal); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
private static void throwAsScriptException(final Exception e) throws ScriptException { |
|
411 |
if (e instanceof ScriptException) { |
|
412 |
throw (ScriptException)e; |
|
413 |
} else if (e instanceof NashornException) { |
|
414 |
final NashornException ne = (NashornException)e; |
|
415 |
final ScriptException se = new ScriptException( |
|
416 |
ne.getMessage(), ne.getFileName(), |
|
417 |
ne.getLineNumber(), ne.getColumnNumber()); |
|
418 |
se.initCause(e); |
|
419 |
throw se; |
|
420 |
} else if (e instanceof RuntimeException) { |
|
421 |
throw (RuntimeException)e; |
|
422 |
} else { |
|
423 |
// wrap any other exception as ScriptException |
|
424 |
throw new ScriptException(e); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
private CompiledScript asCompiledScript(final ScriptFunction script) { |
|
429 |
return new CompiledScript() { |
|
430 |
@Override |
|
431 |
public Object eval(final ScriptContext ctxt) throws ScriptException { |
|
432 |
return evalImpl(script, ctxt); |
|
433 |
} |
|
434 |
@Override |
|
435 |
public ScriptEngine getEngine() { |
|
436 |
return NashornScriptEngine.this; |
|
437 |
} |
|
438 |
}; |
|
439 |
} |
|
440 |
||
441 |
private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException { |
|
16188 | 442 |
final ScriptObject oldGlobal = getNashornGlobal(); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
443 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
444 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 445 |
try { |
446 |
final Object val = ctxt.getAttribute(ScriptEngine.FILENAME); |
|
447 |
final String fileName = (val != null) ? val.toString() : "<eval>"; |
|
448 |
||
449 |
final Source source = new Source(fileName, buf); |
|
450 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
451 |
setNashornGlobal(ctxtGlobal); |
16147 | 452 |
} |
453 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
454 |
return nashornContext.compileScript(source, ctxtGlobal, nashornContext._strict); |
16147 | 455 |
} catch (final Exception e) { |
456 |
throwAsScriptException(e); |
|
457 |
throw new AssertionError("should not reach here"); |
|
458 |
} finally { |
|
459 |
if (globalChanged) { |
|
460 |
setNashornGlobal(oldGlobal); |
|
461 |
} |
|
462 |
} |
|
463 |
} |
|
464 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
465 |
// don't make this public!! |
16188 | 466 |
static ScriptObject getNashornGlobal() { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
467 |
return Context.getGlobal(); |
16188 | 468 |
} |
469 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
470 |
static void setNashornGlobal(final ScriptObject newGlobal) { |
16147 | 471 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
472 |
@Override |
|
473 |
public Void run() { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
474 |
Context.setGlobal(newGlobal); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
475 |
return null; |
16147 | 476 |
} |
477 |
}); |
|
478 |
} |
|
479 |
} |