author | sundar |
Thu, 11 Jul 2013 16:34:55 +0530 | |
changeset 18865 | 8844964e5fc5 |
parent 18864 | c701b823ed9e |
child 19085 | 066c9e5afd79 |
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; |
|
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
35 |
import java.lang.reflect.Method; |
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
36 |
import java.lang.reflect.Modifier; |
16147 | 37 |
import java.net.URL; |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
18335
diff
changeset
|
38 |
import java.nio.charset.Charset; |
16147 | 39 |
import java.security.AccessController; |
40 |
import java.security.PrivilegedAction; |
|
41 |
import java.security.PrivilegedActionException; |
|
42 |
import java.security.PrivilegedExceptionAction; |
|
43 |
import javax.script.AbstractScriptEngine; |
|
44 |
import javax.script.Bindings; |
|
45 |
import javax.script.Compilable; |
|
46 |
import javax.script.CompiledScript; |
|
47 |
import javax.script.Invocable; |
|
48 |
import javax.script.ScriptContext; |
|
49 |
import javax.script.ScriptEngine; |
|
50 |
import javax.script.ScriptEngineFactory; |
|
51 |
import javax.script.ScriptException; |
|
52 |
import jdk.nashorn.internal.runtime.Context; |
|
53 |
import jdk.nashorn.internal.runtime.ErrorManager; |
|
54 |
import jdk.nashorn.internal.runtime.GlobalObject; |
|
55 |
import jdk.nashorn.internal.runtime.Property; |
|
56 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
57 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
58 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
59 |
import jdk.nashorn.internal.runtime.Source; |
|
60 |
import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; |
|
61 |
import jdk.nashorn.internal.runtime.options.Options; |
|
62 |
||
63 |
/** |
|
64 |
* JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned through |
|
65 |
* {@link NashornScriptEngineFactory#getScriptEngine()}. Note that this engine implements the {@link Compilable} and |
|
66 |
* {@link Invocable} interfaces, allowing for efficient precompilation and repeated execution of scripts. |
|
67 |
* @see NashornScriptEngineFactory |
|
68 |
*/ |
|
69 |
||
70 |
public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { |
|
71 |
||
72 |
private final ScriptEngineFactory factory; |
|
73 |
private final Context nashornContext; |
|
74 |
private final ScriptObject global; |
|
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
75 |
// initialized bit late to be made 'final'. Property object for "context" |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
76 |
// property of global object |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
77 |
private Property contextProperty; |
16147 | 78 |
|
79 |
// default options passed to Nashorn Options object |
|
18335
1b5fdae617cf
8016550: nashorn.option.no.syntax.extensions has the wrong default
sundar
parents:
17523
diff
changeset
|
80 |
private static final String[] DEFAULT_OPTIONS = new String[] { "-scripting", "-doe" }; |
16147 | 81 |
|
16197
1a5414cce91a
8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents:
16189
diff
changeset
|
82 |
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
|
83 |
this(factory, DEFAULT_OPTIONS, appLoader); |
16147 | 84 |
} |
85 |
||
16197
1a5414cce91a
8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents:
16189
diff
changeset
|
86 |
NashornScriptEngine(final NashornScriptEngineFactory factory, final String[] args, final ClassLoader appLoader) { |
16147 | 87 |
this.factory = factory; |
88 |
final Options options = new Options("nashorn"); |
|
89 |
options.process(args); |
|
90 |
||
91 |
// throw ParseException on first error from script |
|
16185 | 92 |
final ErrorManager errMgr = new Context.ThrowErrorManager(); |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
93 |
// create new Nashorn Context |
16147 | 94 |
this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() { |
95 |
@Override |
|
96 |
public Context run() { |
|
97 |
try { |
|
16185 | 98 |
return new Context(options, errMgr, appLoader); |
16147 | 99 |
} catch (final RuntimeException e) { |
100 |
if (Context.DEBUG) { |
|
101 |
e.printStackTrace(); |
|
102 |
} |
|
103 |
throw e; |
|
104 |
} |
|
105 |
} |
|
106 |
}); |
|
107 |
||
108 |
// create new global object |
|
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17233
diff
changeset
|
109 |
this.global = createNashornGlobal(); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
110 |
// 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
|
111 |
context.setBindings(new ScriptObjectMirror(global, global), ScriptContext.ENGINE_SCOPE); |
16147 | 112 |
|
113 |
// evaluate engine initial script |
|
114 |
try { |
|
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
115 |
evalEngineScript(); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
116 |
} catch (final ScriptException e) { |
16147 | 117 |
if (Context.DEBUG) { |
118 |
e.printStackTrace(); |
|
119 |
} |
|
120 |
throw new RuntimeException(e); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public Object eval(final Reader reader, final ScriptContext ctxt) throws ScriptException { |
|
126 |
try { |
|
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
127 |
if (reader instanceof URLReader) { |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
128 |
final URL url = ((URLReader)reader).getURL(); |
18337
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
18335
diff
changeset
|
129 |
final Charset cs = ((URLReader)reader).getCharset(); |
effcb00fcf58
8008915: URLReader constructor should allow specifying encoding
sundar
parents:
18335
diff
changeset
|
130 |
return evalImpl(compileImpl(new Source(url.toString(), url, cs), ctxt), ctxt); |
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
131 |
} |
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
132 |
return evalImpl(Source.readFully(reader), ctxt); |
16147 | 133 |
} catch (final IOException e) { |
134 |
throw new ScriptException(e); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
@Override |
|
139 |
public Object eval(final String script, final ScriptContext ctxt) throws ScriptException { |
|
140 |
return evalImpl(script.toCharArray(), ctxt); |
|
141 |
} |
|
142 |
||
143 |
@Override |
|
144 |
public ScriptEngineFactory getFactory() { |
|
145 |
return factory; |
|
146 |
} |
|
147 |
||
148 |
@Override |
|
149 |
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
|
150 |
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
|
151 |
return new ScriptObjectMirror(newGlobal, newGlobal); |
16147 | 152 |
} |
153 |
||
154 |
// Compilable methods |
|
155 |
||
156 |
@Override |
|
157 |
public CompiledScript compile(final Reader reader) throws ScriptException { |
|
158 |
try { |
|
159 |
return asCompiledScript(compileImpl(Source.readFully(reader), context)); |
|
160 |
} catch (final IOException e) { |
|
161 |
throw new ScriptException(e); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
166 |
public CompiledScript compile(final String str) throws ScriptException { |
|
167 |
return asCompiledScript(compileImpl(str.toCharArray(), context)); |
|
168 |
} |
|
169 |
||
170 |
// Invocable methods |
|
171 |
||
172 |
@Override |
|
173 |
public Object invokeFunction(final String name, final Object... args) |
|
174 |
throws ScriptException, NoSuchMethodException { |
|
175 |
return invokeImpl(null, name, args); |
|
176 |
} |
|
177 |
||
178 |
@Override |
|
179 |
public Object invokeMethod(final Object self, final String name, final Object... args) |
|
180 |
throws ScriptException, NoSuchMethodException { |
|
181 |
if (self == null) { |
|
182 |
throw new IllegalArgumentException("script object can not be null"); |
|
183 |
} |
|
184 |
return invokeImpl(self, name, args); |
|
185 |
} |
|
186 |
||
187 |
private <T> T getInterfaceInner(final Object self, final Class<T> clazz) { |
|
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
188 |
if (clazz == null || !clazz.isInterface()) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
189 |
throw new IllegalArgumentException("interface Class expected"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
190 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
191 |
|
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
192 |
// perform security access check as early as possible |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
193 |
final SecurityManager sm = System.getSecurityManager(); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
194 |
if (sm != null) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
195 |
if (! Modifier.isPublic(clazz.getModifiers())) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
196 |
throw new SecurityException("attempt to implement non-public interfce: " + clazz); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
197 |
} |
18865
8844964e5fc5
8020325: static property does not work on accessible, public classes
sundar
parents:
18864
diff
changeset
|
198 |
Context.checkPackageAccess(clazz.getName()); |
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
199 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
200 |
|
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
201 |
final ScriptObject realSelf; |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
202 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(context); |
16147 | 203 |
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
|
204 |
realSelf = ctxtGlobal; |
16147 | 205 |
} else if (!(self instanceof ScriptObject)) { |
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
206 |
realSelf = (ScriptObject)ScriptObjectMirror.unwrap(self, ctxtGlobal); |
16147 | 207 |
} else { |
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
208 |
realSelf = (ScriptObject)self; |
16147 | 209 |
} |
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
18615
diff
changeset
|
210 |
|
16147 | 211 |
try { |
16188 | 212 |
final ScriptObject oldGlobal = getNashornGlobal(); |
16147 | 213 |
try { |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
214 |
if(oldGlobal != ctxtGlobal) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
215 |
setNashornGlobal(ctxtGlobal); |
16147 | 216 |
} |
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
217 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
218 |
if (! isInterfaceImplemented(clazz, realSelf)) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
219 |
return null; |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
220 |
} |
16147 | 221 |
return clazz.cast(JavaAdapterFactory.getConstructor(realSelf.getClass(), clazz).invoke(realSelf)); |
222 |
} finally { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
223 |
if(oldGlobal != ctxtGlobal) { |
16147 | 224 |
setNashornGlobal(oldGlobal); |
225 |
} |
|
226 |
} |
|
227 |
} catch(final RuntimeException|Error e) { |
|
228 |
throw e; |
|
229 |
} catch(final Throwable t) { |
|
230 |
throw new RuntimeException(t); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public <T> T getInterface(final Class<T> clazz) { |
|
236 |
return getInterfaceInner(null, clazz); |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public <T> T getInterface(final Object self, final Class<T> clazz) { |
|
241 |
if (self == null) { |
|
242 |
throw new IllegalArgumentException("script object can not be null"); |
|
243 |
} |
|
244 |
return getInterfaceInner(self, clazz); |
|
245 |
} |
|
246 |
||
247 |
// These are called from the "engine.js" script |
|
248 |
||
249 |
/** |
|
250 |
* This hook is used to search js global variables exposed from Java code. |
|
251 |
* |
|
252 |
* @param self 'this' passed from the script |
|
253 |
* @param ctxt current ScriptContext in which name is searched |
|
254 |
* @param name name of the variable searched |
|
255 |
* @return the value of the named variable |
|
256 |
*/ |
|
257 |
public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) { |
|
258 |
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
|
259 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
16147 | 260 |
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
|
261 |
return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal); |
16147 | 262 |
} |
263 |
||
264 |
if (self == UNDEFINED) { |
|
265 |
// scope access and so throw ReferenceError |
|
16256
f2d9a0c49914
8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents:
16243
diff
changeset
|
266 |
throw referenceError(ctxtGlobal, "not.defined", name); |
16147 | 267 |
} |
268 |
||
269 |
return UNDEFINED; |
|
270 |
} |
|
271 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
return sobj; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
278 |
} |
16147 | 279 |
} |
280 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
281 |
// 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
|
282 |
return global; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
283 |
} |
16147 | 284 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
285 |
private ScriptObject createNashornGlobal() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
286 |
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
|
287 |
@Override |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
288 |
public ScriptObject run() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
289 |
try { |
16211 | 290 |
return nashornContext.newGlobal(); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
291 |
} 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
|
292 |
if (Context.DEBUG) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
293 |
e.printStackTrace(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
294 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
295 |
throw e; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
296 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
297 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
298 |
}); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
299 |
|
16211 | 300 |
nashornContext.initGlobal(newGlobal); |
301 |
||
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
302 |
final int NON_ENUMERABLE_CONSTANT = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE | Property.NOT_WRITABLE; |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
303 |
// current ScriptContext exposed as "context" |
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
304 |
// "context" is non-writable from script - but script engine still |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
305 |
// needs to set it and so save the context Property object |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
306 |
contextProperty = newGlobal.addOwnProperty("context", NON_ENUMERABLE_CONSTANT, UNDEFINED); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
307 |
// 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
|
308 |
// 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
|
309 |
// 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
|
310 |
// seen from another thread outside of this constructor. |
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
311 |
newGlobal.addOwnProperty("engine", NON_ENUMERABLE_CONSTANT, this); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
312 |
// 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
|
313 |
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
|
314 |
// 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
|
315 |
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
|
316 |
return newGlobal; |
16147 | 317 |
} |
318 |
||
319 |
private void evalEngineScript() throws ScriptException { |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
320 |
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
|
321 |
} |
25c8da53438f
8006181: nashorn script engine does not run jrunscript's initialization script
sundar
parents:
16151
diff
changeset
|
322 |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
323 |
private void evalSupportScript(final String script, final String name) throws ScriptException { |
16147 | 324 |
try { |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
325 |
final InputStream is = AccessController.doPrivileged( |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
326 |
new PrivilegedExceptionAction<InputStream>() { |
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16197
diff
changeset
|
327 |
@Override |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
328 |
public InputStream run() throws Exception { |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
329 |
final URL url = NashornScriptEngine.class.getResource(script); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
330 |
return url.openStream(); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
331 |
} |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
332 |
}); |
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16201
diff
changeset
|
333 |
put(ScriptEngine.FILENAME, name); |
16147 | 334 |
try (final InputStreamReader isr = new InputStreamReader(is)) { |
335 |
eval(isr); |
|
336 |
} |
|
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16172
diff
changeset
|
337 |
} catch (final PrivilegedActionException | IOException e) { |
16147 | 338 |
throw new ScriptException(e); |
339 |
} finally { |
|
340 |
put(ScriptEngine.FILENAME, null); |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
// scripts should see "context" and "engine" as variables |
|
345 |
private void setContextVariables(final ScriptContext ctxt) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
346 |
final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt); |
18615
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
347 |
// set "context" global variable via contextProperty - because this |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
348 |
// property is non-writable |
3f6e6adcbc1a
8015969: Needs to enforce and document that global "context" and "engine" can't be modified when running via jsr223
sundar
parents:
18337
diff
changeset
|
349 |
contextProperty.setObjectValue(ctxtGlobal, ctxtGlobal, ctxt, false); |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
350 |
Object args = ScriptObjectMirror.unwrap(ctxt.getAttribute("arguments"), ctxtGlobal); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
351 |
if (args == null || args == UNDEFINED) { |
16147 | 352 |
args = ScriptRuntime.EMPTY_ARRAY; |
353 |
} |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
354 |
// 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
|
355 |
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
|
356 |
ctxtGlobal.set("arguments", args, false); |
16147 | 357 |
} |
358 |
||
359 |
private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException { |
|
16188 | 360 |
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
|
361 |
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
|
362 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 363 |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16230
diff
changeset
|
364 |
Object self = globalChanged? ScriptObjectMirror.wrap(selfObject, oldGlobal) : selfObject; |
16147 | 365 |
|
366 |
try { |
|
367 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
368 |
setNashornGlobal(ctxtGlobal); |
16147 | 369 |
} |
370 |
||
371 |
ScriptObject sobj; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16180
diff
changeset
|
372 |
Object value = null; |
16147 | 373 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
374 |
self = ScriptObjectMirror.unwrap(self, ctxtGlobal); |
16147 | 375 |
|
376 |
// FIXME: should convert when self is not ScriptObject |
|
377 |
if (self instanceof ScriptObject) { |
|
378 |
sobj = (ScriptObject)self; |
|
379 |
value = sobj.get(name); |
|
380 |
} 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
|
381 |
self = ctxtGlobal; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
382 |
sobj = ctxtGlobal; |
16147 | 383 |
value = sobj.get(name); |
384 |
} |
|
385 |
||
386 |
if (value instanceof ScriptFunction) { |
|
387 |
final Object res; |
|
388 |
try { |
|
16232
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16230
diff
changeset
|
389 |
final Object[] modArgs = globalChanged? ScriptObjectMirror.wrapArray(args, oldGlobal) : args; |
efd57dd90de6
8008197: Cross script engine function calls do not work as expected
sundar
parents:
16230
diff
changeset
|
390 |
res = ScriptRuntime.checkAndApply((ScriptFunction)value, self, ScriptObjectMirror.unwrapArray(modArgs, ctxtGlobal)); |
16147 | 391 |
} catch (final Exception e) { |
392 |
throwAsScriptException(e); |
|
393 |
throw new AssertionError("should not reach here"); |
|
394 |
} |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
395 |
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(res, ctxtGlobal)); |
16147 | 396 |
} |
397 |
||
398 |
throw new NoSuchMethodException(name); |
|
399 |
} finally { |
|
400 |
if (globalChanged) { |
|
401 |
setNashornGlobal(oldGlobal); |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
||
406 |
private Object evalImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException { |
|
407 |
return evalImpl(compileImpl(buf, ctxt), ctxt); |
|
408 |
} |
|
409 |
||
410 |
private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException { |
|
411 |
if (script == null) { |
|
412 |
return null; |
|
413 |
} |
|
16188 | 414 |
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
|
415 |
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
|
416 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 417 |
try { |
418 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
419 |
setNashornGlobal(ctxtGlobal); |
16147 | 420 |
} |
421 |
||
422 |
setContextVariables(ctxt); |
|
17233 | 423 |
return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal)); |
16147 | 424 |
} catch (final Exception e) { |
425 |
throwAsScriptException(e); |
|
426 |
throw new AssertionError("should not reach here"); |
|
427 |
} finally { |
|
428 |
if (globalChanged) { |
|
429 |
setNashornGlobal(oldGlobal); |
|
430 |
} |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
private static void throwAsScriptException(final Exception e) throws ScriptException { |
|
435 |
if (e instanceof ScriptException) { |
|
436 |
throw (ScriptException)e; |
|
437 |
} else if (e instanceof NashornException) { |
|
438 |
final NashornException ne = (NashornException)e; |
|
439 |
final ScriptException se = new ScriptException( |
|
440 |
ne.getMessage(), ne.getFileName(), |
|
441 |
ne.getLineNumber(), ne.getColumnNumber()); |
|
442 |
se.initCause(e); |
|
443 |
throw se; |
|
444 |
} else if (e instanceof RuntimeException) { |
|
445 |
throw (RuntimeException)e; |
|
446 |
} else { |
|
447 |
// wrap any other exception as ScriptException |
|
448 |
throw new ScriptException(e); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
private CompiledScript asCompiledScript(final ScriptFunction script) { |
|
453 |
return new CompiledScript() { |
|
454 |
@Override |
|
455 |
public Object eval(final ScriptContext ctxt) throws ScriptException { |
|
456 |
return evalImpl(script, ctxt); |
|
457 |
} |
|
458 |
@Override |
|
459 |
public ScriptEngine getEngine() { |
|
460 |
return NashornScriptEngine.this; |
|
461 |
} |
|
462 |
}; |
|
463 |
} |
|
464 |
||
465 |
private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException { |
|
16243
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
466 |
final Object val = ctxt.getAttribute(ScriptEngine.FILENAME); |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
467 |
final String fileName = (val != null) ? val.toString() : "<eval>"; |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
468 |
return compileImpl(new Source(fileName, buf), ctxt); |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
469 |
} |
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
470 |
|
f5fdf89e1583
8008305: ScriptEngine.eval should offer the ability to provide a codebase
sundar
parents:
16232
diff
changeset
|
471 |
private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException { |
16188 | 472 |
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
|
473 |
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
|
474 |
final boolean globalChanged = (oldGlobal != ctxtGlobal); |
16147 | 475 |
try { |
476 |
if (globalChanged) { |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
477 |
setNashornGlobal(ctxtGlobal); |
16147 | 478 |
} |
479 |
||
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16211
diff
changeset
|
480 |
return nashornContext.compileScript(source, ctxtGlobal); |
16147 | 481 |
} catch (final Exception e) { |
482 |
throwAsScriptException(e); |
|
483 |
throw new AssertionError("should not reach here"); |
|
484 |
} finally { |
|
485 |
if (globalChanged) { |
|
486 |
setNashornGlobal(oldGlobal); |
|
487 |
} |
|
488 |
} |
|
489 |
} |
|
490 |
||
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
491 |
private static boolean isInterfaceImplemented(final Class<?> iface, final ScriptObject sobj) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
492 |
for (final Method method : iface.getMethods()) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
493 |
// ignore methods of java.lang.Object class |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
494 |
if (method.getDeclaringClass() == Object.class) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
495 |
continue; |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
496 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
497 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
498 |
Object obj = sobj.get(method.getName()); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
499 |
if (! (obj instanceof ScriptFunction)) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
500 |
return false; |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
501 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
502 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
503 |
return true; |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
504 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16527
diff
changeset
|
505 |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
506 |
// don't make this public!! |
16188 | 507 |
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
|
508 |
return Context.getGlobal(); |
16188 | 509 |
} |
510 |
||
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
511 |
static void setNashornGlobal(final ScriptObject newGlobal) { |
16147 | 512 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
513 |
@Override |
|
514 |
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
|
515 |
Context.setGlobal(newGlobal); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16188
diff
changeset
|
516 |
return null; |
16147 | 517 |
} |
518 |
}); |
|
519 |
} |
|
520 |
} |