author | sundar |
Tue, 15 Jul 2014 16:54:01 +0530 | |
changeset 25585 | 0968d8ab046a |
parent 25247 | c0f911459863 |
child 25826 | 3d2dd01a3a4a |
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 org.testng.Assert.assertEquals; |
|
29 |
import static org.testng.Assert.assertNotNull; |
|
30 |
import static org.testng.Assert.assertTrue; |
|
31 |
import static org.testng.Assert.fail; |
|
32 |
||
33 |
import java.io.StringReader; |
|
34 |
import java.io.StringWriter; |
|
22668 | 35 |
import java.lang.reflect.InvocationHandler; |
16147 | 36 |
import java.lang.reflect.Method; |
22668 | 37 |
import java.lang.reflect.Proxy; |
16147 | 38 |
import java.util.concurrent.Callable; |
39 |
import javax.script.Compilable; |
|
40 |
import javax.script.CompiledScript; |
|
20210
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
41 |
import javax.script.ScriptContext; |
16147 | 42 |
import javax.script.ScriptEngine; |
43 |
import javax.script.ScriptEngineFactory; |
|
44 |
import javax.script.ScriptEngineManager; |
|
45 |
import javax.script.ScriptException; |
|
20210
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
46 |
import javax.script.SimpleScriptContext; |
16147 | 47 |
import org.testng.annotations.Test; |
48 |
||
49 |
/** |
|
50 |
* Tests for JSR-223 script engine for Nashorn. |
|
16260
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
51 |
* |
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
52 |
* @test |
17248
60ef2a001ec2
8013295: ScriptEngineTest.java fails with compilation error when running under jtreg
sundar
parents:
17239
diff
changeset
|
53 |
* @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest |
21866
e764669804d3
8028161: nashorn: src/jdk/nashorn/api/scripting/ScriptEngineTest.java
sundar
parents:
21688
diff
changeset
|
54 |
* @run testng/othervm jdk.nashorn.api.scripting.ScriptEngineTest |
16147 | 55 |
*/ |
56 |
public class ScriptEngineTest { |
|
57 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
58 |
private void log(final String msg) { |
16147 | 59 |
org.testng.Reporter.log(msg, true); |
60 |
} |
|
61 |
||
62 |
@Test |
|
63 |
public void argumentsTest() { |
|
64 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
65 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
66 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
67 |
final String[] args = new String[] { "hello", "world" }; |
16147 | 68 |
try { |
69 |
e.put("arguments", args); |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
70 |
final Object arg0 = e.eval("arguments[0]"); |
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
71 |
final Object arg1 = e.eval("arguments[1]"); |
16147 | 72 |
assertEquals(args[0], arg0); |
73 |
assertEquals(args[1], arg1); |
|
74 |
} catch (final Exception exp) { |
|
75 |
exp.printStackTrace(); |
|
76 |
fail(exp.getMessage()); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
@Test |
|
81 |
public void argumentsWithTest() { |
|
82 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
83 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
84 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
85 |
final String[] args = new String[] { "hello", "world" }; |
16147 | 86 |
try { |
87 |
e.put("arguments", args); |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
88 |
final Object arg0 = e.eval("var imports = new JavaImporter(java.io); " + |
16147 | 89 |
" with(imports) { arguments[0] }"); |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
90 |
final Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " + |
16147 | 91 |
" with(imports) { arguments[1] }"); |
92 |
assertEquals(args[0], arg0); |
|
93 |
assertEquals(args[1], arg1); |
|
94 |
} catch (final Exception exp) { |
|
95 |
exp.printStackTrace(); |
|
96 |
fail(exp.getMessage()); |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
@Test |
|
101 |
public void argumentsEmptyTest() { |
|
102 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
103 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
104 |
||
105 |
try { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
106 |
assertEquals(e.eval("arguments instanceof Array"), true); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
107 |
assertEquals(e.eval("arguments.length == 0"), true); |
16147 | 108 |
} catch (final Exception exp) { |
109 |
exp.printStackTrace(); |
|
110 |
fail(exp.getMessage()); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
@Test |
|
115 |
public void factoryTests() { |
|
116 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
117 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
118 |
assertNotNull(e); |
|
119 |
||
120 |
final ScriptEngineFactory fac = e.getFactory(); |
|
121 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
122 |
assertEquals(fac.getLanguageName(), "ECMAScript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
123 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
124 |
assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
125 |
assertEquals(fac.getEngineName(), "Oracle Nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
126 |
assertEquals(fac.getOutputStatement("context"), "print(context)"); |
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
127 |
assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
128 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
16147 | 129 |
|
130 |
boolean seenJS = false; |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
131 |
for (final String ext : fac.getExtensions()) { |
16147 | 132 |
if (ext.equals("js")) { |
133 |
seenJS = true; |
|
134 |
} |
|
135 |
} |
|
136 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
137 |
assertEquals(seenJS, true); |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
138 |
final String str = fac.getMethodCallSyntax("obj", "foo", "x"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
139 |
assertEquals(str, "obj.foo(x)"); |
16147 | 140 |
|
141 |
boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false; |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
142 |
for (final String name : fac.getNames()) { |
16147 | 143 |
switch (name) { |
144 |
case "nashorn": seenNashorn = true; break; |
|
145 |
case "javascript": seenJavaScript = true; break; |
|
146 |
case "ECMAScript": seenECMAScript = true; break; |
|
147 |
} |
|
148 |
} |
|
149 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
150 |
assertTrue(seenNashorn); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
151 |
assertTrue(seenJavaScript); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
152 |
assertTrue(seenECMAScript); |
16147 | 153 |
|
154 |
boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false; |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
155 |
for (final String mime : fac.getMimeTypes()) { |
16147 | 156 |
switch (mime) { |
157 |
case "application/javascript": seenAppJS = true; break; |
|
158 |
case "application/ecmascript": seenAppECMA = true; break; |
|
159 |
case "text/javascript": seenTextJS = true; break; |
|
160 |
case "text/ecmascript": seenTextECMA = true; break; |
|
161 |
} |
|
162 |
} |
|
163 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
164 |
assertTrue(seenAppJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
165 |
assertTrue(seenAppECMA); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
166 |
assertTrue(seenTextJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
167 |
assertTrue(seenTextECMA); |
16147 | 168 |
} |
169 |
||
170 |
@Test |
|
171 |
public void evalTests() { |
|
172 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
173 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
174 |
e.put(ScriptEngine.FILENAME, "myfile.js"); |
|
175 |
||
176 |
try { |
|
177 |
e.eval("print('hello')"); |
|
178 |
} catch (final ScriptException se) { |
|
179 |
fail(se.getMessage()); |
|
180 |
} |
|
181 |
try { |
|
182 |
e.eval("print('hello)"); |
|
183 |
fail("script exception expected"); |
|
184 |
} catch (final ScriptException se) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
185 |
assertEquals(se.getLineNumber(), 1); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
186 |
assertEquals(se.getColumnNumber(), 13); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
187 |
assertEquals(se.getFileName(), "myfile.js"); |
16147 | 188 |
// se.printStackTrace(); |
189 |
} |
|
190 |
||
191 |
try { |
|
192 |
Object obj = e.eval("34 + 41"); |
|
193 |
assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue()); |
|
194 |
obj = e.eval("x = 5"); |
|
195 |
assertTrue(5.0 == ((Number)obj).doubleValue()); |
|
196 |
} catch (final ScriptException se) { |
|
197 |
se.printStackTrace(); |
|
198 |
fail(se.getMessage()); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
@Test |
|
203 |
public void compileTests() { |
|
204 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
205 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
206 |
CompiledScript script = null; |
|
207 |
||
208 |
try { |
|
209 |
script = ((Compilable)e).compile("print('hello')"); |
|
210 |
} catch (final ScriptException se) { |
|
211 |
fail(se.getMessage()); |
|
212 |
} |
|
213 |
||
214 |
try { |
|
215 |
script.eval(); |
|
216 |
} catch (final ScriptException | NullPointerException se) { |
|
217 |
se.printStackTrace(); |
|
218 |
fail(se.getMessage()); |
|
219 |
} |
|
220 |
||
221 |
// try to compile from a Reader |
|
222 |
try { |
|
223 |
script = ((Compilable)e).compile(new StringReader("print('world')")); |
|
224 |
} catch (final ScriptException se) { |
|
225 |
fail(se.getMessage()); |
|
226 |
} |
|
227 |
||
228 |
try { |
|
229 |
script.eval(); |
|
230 |
} catch (final ScriptException | NullPointerException se) { |
|
231 |
se.printStackTrace(); |
|
232 |
fail(se.getMessage()); |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
@Test |
|
20210
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
237 |
public void compileAndEvalInDiffContextTest() throws ScriptException { |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
238 |
final ScriptEngineManager m = new ScriptEngineManager(); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
239 |
final ScriptEngine engine = m.getEngineByName("js"); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
240 |
final Compilable compilable = (Compilable) engine; |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
241 |
final CompiledScript compiledScript = compilable.compile("foo"); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
242 |
final ScriptContext ctxt = new SimpleScriptContext(); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
243 |
ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
244 |
assertEquals(compiledScript.eval(ctxt), "hello"); |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
245 |
} |
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
246 |
|
80d652da38ac
8024973: Using a different ScriptContext with a CompiledScript results in ScriptException
sundar
parents:
19636
diff
changeset
|
247 |
@Test |
16147 | 248 |
public void accessGlobalTest() { |
249 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
250 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
251 |
||
252 |
try { |
|
253 |
e.eval("var x = 'hello'"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
254 |
assertEquals(e.get("x"), "hello"); |
16147 | 255 |
} catch (final ScriptException exp) { |
256 |
exp.printStackTrace(); |
|
257 |
fail(exp.getMessage()); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
@Test |
|
262 |
public void exposeGlobalTest() { |
|
263 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
264 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
265 |
||
266 |
try { |
|
267 |
e.put("y", "foo"); |
|
268 |
e.eval("print(y)"); |
|
269 |
} catch (final ScriptException exp) { |
|
270 |
exp.printStackTrace(); |
|
271 |
fail(exp.getMessage()); |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
@Test |
|
276 |
public void putGlobalFunctionTest() { |
|
277 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
278 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
279 |
||
280 |
e.put("callable", new Callable<String>() { |
|
281 |
@Override |
|
282 |
public String call() throws Exception { |
|
283 |
return "callable was called"; |
|
284 |
} |
|
285 |
}); |
|
286 |
||
287 |
try { |
|
288 |
e.eval("print(callable.call())"); |
|
289 |
} catch (final ScriptException exp) { |
|
290 |
exp.printStackTrace(); |
|
291 |
fail(exp.getMessage()); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
@Test |
|
296 |
public void windowAlertTest() { |
|
297 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
298 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
299 |
final Window window = new Window(); |
|
300 |
||
301 |
try { |
|
302 |
e.put("window", window); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
303 |
e.eval("print(window.alert)"); |
16147 | 304 |
e.eval("window.alert('calling window.alert...')"); |
305 |
} catch (final Exception exp) { |
|
306 |
exp.printStackTrace(); |
|
307 |
fail(exp.getMessage()); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
@Test |
|
312 |
public void windowLocationTest() { |
|
313 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
314 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
315 |
final Window window = new Window(); |
|
316 |
||
317 |
try { |
|
318 |
e.put("window", window); |
|
319 |
e.eval("print(window.location)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
320 |
final Object locationValue = e.eval("window.getLocation()"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
321 |
assertEquals(locationValue, "http://localhost:8080/window"); |
16147 | 322 |
} catch (final Exception exp) { |
323 |
exp.printStackTrace(); |
|
324 |
fail(exp.getMessage()); |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
@Test |
|
329 |
public void windowItemTest() { |
|
330 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
331 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
332 |
final Window window = new Window(); |
|
333 |
||
334 |
try { |
|
335 |
e.put("window", window); |
|
336 |
final String item1 = (String)e.eval("window.item(65535)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
337 |
assertEquals(item1, "ffff"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
338 |
final String item2 = (String)e.eval("window.item(255)"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
339 |
assertEquals(item2, "ff"); |
16147 | 340 |
} catch (final Exception exp) { |
341 |
exp.printStackTrace(); |
|
342 |
fail(exp.getMessage()); |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
@Test |
|
347 |
public void windowEventTest() { |
|
348 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
349 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
350 |
final Window window = new Window(); |
|
351 |
||
352 |
try { |
|
353 |
e.put("window", window); |
|
354 |
e.eval("window.onload = function() { print('window load event fired'); return true }"); |
|
355 |
assertTrue((Boolean)e.eval("window.onload.loaded()")); |
|
356 |
final WindowEventHandler handler = window.getOnload(); |
|
357 |
assertNotNull(handler); |
|
358 |
assertTrue(handler.loaded()); |
|
359 |
} catch (final Exception exp) { |
|
360 |
exp.printStackTrace(); |
|
361 |
fail(exp.getMessage()); |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
@Test |
|
366 |
public void throwTest() { |
|
367 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
368 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
369 |
e.put(ScriptEngine.FILENAME, "throwtest.js"); |
|
370 |
||
371 |
try { |
|
372 |
e.eval("throw 'foo'"); |
|
373 |
} catch (final ScriptException exp) { |
|
374 |
log(exp.getMessage()); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
375 |
assertEquals(exp.getMessage(), "foo in throwtest.js at line number 1 at column number 0"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
376 |
assertEquals(exp.getFileName(), "throwtest.js"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
377 |
assertEquals(exp.getLineNumber(), 1); |
16147 | 378 |
} |
379 |
} |
|
380 |
||
381 |
@Test |
|
382 |
public void setTimeoutTest() { |
|
383 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
384 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
385 |
final Window window = new Window(); |
|
386 |
||
387 |
try { |
|
388 |
final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class }; |
|
389 |
final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes); |
|
390 |
assertNotNull(setTimeout); |
|
391 |
e.put("window", window); |
|
392 |
e.eval("window.setTimeout('foo()', 100)"); |
|
393 |
||
394 |
// try to make setTimeout global |
|
395 |
e.put("setTimeout", setTimeout); |
|
396 |
// TODO: java.lang.ClassCastException: required class |
|
397 |
// java.lang.Integer but encountered class java.lang.Double |
|
398 |
// e.eval("setTimeout('foo2()', 200)"); |
|
399 |
} catch (final Exception exp) { |
|
400 |
exp.printStackTrace(); |
|
401 |
fail(exp.getMessage()); |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
@Test |
|
406 |
public void setWriterTest() { |
|
407 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
408 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
409 |
final StringWriter sw = new StringWriter(); |
|
410 |
e.getContext().setWriter(sw); |
|
411 |
||
412 |
try { |
|
413 |
e.eval("print('hello world')"); |
|
414 |
} catch (final Exception exp) { |
|
415 |
exp.printStackTrace(); |
|
416 |
fail(exp.getMessage()); |
|
417 |
} |
|
19636 | 418 |
assertEquals(sw.toString(), println("hello world")); |
16147 | 419 |
} |
420 |
||
421 |
@Test |
|
422 |
public void redefineEchoTest() { |
|
423 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
424 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
425 |
||
426 |
try { |
|
427 |
e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }"); |
|
428 |
} catch (final Exception exp) { |
|
429 |
exp.printStackTrace(); |
|
430 |
fail(exp.getMessage()); |
|
431 |
} |
|
432 |
} |
|
19101
3e6fe94f02a8
8021361: ClassCastException:.ScriptObjectMirror -> ScriptObject when getInterface called on object from different ScriptContext
sundar
parents:
19099
diff
changeset
|
433 |
@Test |
16147 | 434 |
public void noEnumerablePropertiesTest() { |
435 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
436 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
437 |
try { |
|
438 |
e.eval("for (i in this) { throw 'found property: ' + i }"); |
|
439 |
} catch (final Exception exp) { |
|
440 |
exp.printStackTrace(); |
|
441 |
fail(exp.getMessage()); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
@Test |
|
446 |
public void noRefErrorForGlobalThisAccessTest() { |
|
447 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
448 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
449 |
try { |
|
450 |
e.eval("this.foo"); |
|
451 |
} catch (final Exception exp) { |
|
452 |
exp.printStackTrace(); |
|
453 |
fail(exp.getMessage()); |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
@Test |
|
458 |
public void refErrorForUndeclaredAccessTest() { |
|
459 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
460 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
461 |
try { |
|
462 |
e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); |
|
463 |
} catch (final Exception exp) { |
|
464 |
exp.printStackTrace(); |
|
465 |
fail(exp.getMessage()); |
|
466 |
} |
|
467 |
} |
|
468 |
||
469 |
@Test |
|
470 |
public void typeErrorForGlobalThisCallTest() { |
|
471 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
472 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
473 |
try { |
|
474 |
e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); |
|
475 |
} catch (final Exception exp) { |
|
476 |
exp.printStackTrace(); |
|
477 |
fail(exp.getMessage()); |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
@Test |
|
482 |
public void refErrorForUndeclaredCallTest() { |
|
483 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
484 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
485 |
try { |
|
486 |
e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); |
|
487 |
} catch (final Exception exp) { |
|
488 |
exp.printStackTrace(); |
|
489 |
fail(exp.getMessage()); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
@Test |
|
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
494 |
// check that print function prints arg followed by newline char |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
495 |
public void printTest() { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
496 |
final ScriptEngineManager m = new ScriptEngineManager(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
497 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
498 |
final StringWriter sw = new StringWriter(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
499 |
e.getContext().setWriter(sw); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
500 |
try { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
501 |
e.eval("print('hello')"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
502 |
} catch (final Throwable t) { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
503 |
t.printStackTrace(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
504 |
fail(t.getMessage()); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
505 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
506 |
|
19636 | 507 |
assertEquals(sw.toString(), println("hello")); |
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
508 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
509 |
|
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
510 |
@Test |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
511 |
// check that print prints all arguments (more than one) |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
512 |
public void printManyTest() { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
513 |
final ScriptEngineManager m = new ScriptEngineManager(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
514 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
515 |
final StringWriter sw = new StringWriter(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
516 |
e.getContext().setWriter(sw); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
517 |
try { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
518 |
e.eval("print(34, true, 'hello')"); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
519 |
} catch (final Throwable t) { |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
520 |
t.printStackTrace(); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
521 |
fail(t.getMessage()); |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
522 |
} |
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
523 |
|
19636 | 524 |
assertEquals(sw.toString(), println("34 true hello")); |
525 |
} |
|
526 |
||
21688
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
527 |
@Test |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
528 |
public void scriptObjectAutoConversionTest() throws ScriptException { |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
529 |
final ScriptEngineManager m = new ScriptEngineManager(); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
530 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
531 |
e.eval("obj = { foo: 'hello' }"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
532 |
e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.Window")); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
533 |
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
534 |
assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
535 |
assertEquals(e.eval("Window.funcMap(obj)"), "hello"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
536 |
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello"); |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
537 |
} |
90bb8dc029c7
8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
sundar
parents:
20210
diff
changeset
|
538 |
|
22668 | 539 |
// @bug 8032948: Nashorn linkages awry |
540 |
@Test |
|
541 |
public void checkProxyAccess() throws ScriptException { |
|
542 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
543 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
544 |
final boolean[] reached = new boolean[1]; |
|
545 |
final Runnable r = (Runnable)Proxy.newProxyInstance( |
|
546 |
ScriptEngineTest.class.getClassLoader(), |
|
547 |
new Class[] { Runnable.class }, |
|
548 |
new InvocationHandler() { |
|
549 |
@Override |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
550 |
public Object invoke(final Object p, final Method m, final Object[] a) { |
22668 | 551 |
reached[0] = true; |
552 |
return null; |
|
553 |
} |
|
554 |
}); |
|
555 |
||
556 |
e.put("r", r); |
|
557 |
e.eval("r.run()"); |
|
558 |
||
559 |
assertTrue(reached[0]); |
|
560 |
} |
|
561 |
||
23076
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
562 |
// properties that can be read by any code |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
563 |
private static String[] propNames = { |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
564 |
"java.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
565 |
"java.vendor", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
566 |
"java.vendor.url", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
567 |
"java.class.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
568 |
"os.name", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
569 |
"os.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
570 |
"os.arch", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
571 |
"file.separator", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
572 |
"path.separator", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
573 |
"line.separator", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
574 |
"java.specification.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
575 |
"java.specification.vendor", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
576 |
"java.specification.name", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
577 |
"java.vm.specification.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
578 |
"java.vm.specification.vendor", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
579 |
"java.vm.specification.name", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
580 |
"java.vm.version", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
581 |
"java.vm.vendor", |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
582 |
"java.vm.name" |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
583 |
}; |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
584 |
|
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
585 |
// @bug 8033924: Default permissions are not given for eval code |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
586 |
@Test |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
587 |
public void checkPropertyReadPermissions() throws ScriptException { |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
588 |
final ScriptEngineManager m = new ScriptEngineManager(); |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
589 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
590 |
|
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
591 |
for (final String name : propNames) { |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
592 |
checkProperty(e, name); |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
593 |
} |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
594 |
} |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
595 |
|
25247
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
596 |
// @bug 8046013: TypeError: Cannot apply "with" to non script object |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
597 |
@Test |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
598 |
public void withOnMirrorTest() throws ScriptException { |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
599 |
final ScriptEngineManager m = new ScriptEngineManager(); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
600 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
601 |
|
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
602 |
final Object obj = e.eval("({ foo: 'hello'})"); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
603 |
final Object[] arr = new Object[1]; |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
604 |
arr[0] = obj; |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
605 |
e.put("arr", arr); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
606 |
final Object res = e.eval("var res; with(arr[0]) { res = foo; }; res"); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
607 |
assertEquals(res, "hello"); |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
608 |
} |
c0f911459863
8046013: TypeError: Cannot apply "with" to non script object
sundar
parents:
24778
diff
changeset
|
609 |
|
25585
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
610 |
// @bug 8050432:javax.script.filename variable should not be enumerable |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
611 |
// with nashorn engine's ENGINE_SCOPE bindings |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
612 |
@Test |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
613 |
public void enumerableGlobalsTest() throws ScriptException { |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
614 |
final ScriptEngineManager m = new ScriptEngineManager(); |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
615 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
616 |
|
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
617 |
e.put(ScriptEngine.FILENAME, "test"); |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
618 |
Object enumerable = e.eval( |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
619 |
"Object.getOwnPropertyDescriptor(this, " + |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
620 |
" 'javax.script.filename').enumerable"); |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
621 |
assertEquals(enumerable, Boolean.FALSE); |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
622 |
} |
0968d8ab046a
8050432: javax.script.filename variable should not be enumerable with nashorn engine's ENGINE_SCOPE bindings
sundar
parents:
25247
diff
changeset
|
623 |
|
23076
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
624 |
private static void checkProperty(final ScriptEngine e, final String name) |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
625 |
throws ScriptException { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
24751
diff
changeset
|
626 |
final String value = System.getProperty(name); |
23076
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
627 |
e.put("name", name); |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
628 |
assertEquals(value, e.eval("java.lang.System.getProperty(name)")); |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
629 |
} |
8660ebaaa2f2
8033924: Default permissions are not given for eval code
sundar
parents:
22668
diff
changeset
|
630 |
|
19636 | 631 |
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
632 |
||
633 |
// Returns String that would be the result of calling PrintWriter.println |
|
634 |
// of the given String. (This is to handle platform specific newline). |
|
635 |
private static String println(final String str) { |
|
636 |
return str + LINE_SEPARATOR; |
|
19107
721939a7971c
8012792: print function defined in engine.js does not handle multiple arguments
sundar
parents:
19101
diff
changeset
|
637 |
} |
16147 | 638 |
} |