author | sundar |
Wed, 10 Jul 2013 19:08:04 +0530 | |
changeset 18864 | c701b823ed9e |
parent 17751 | 15a93d511eb7 |
child 18865 | 8844964e5fc5 |
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; |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16221
diff
changeset
|
29 |
import static org.testng.Assert.assertFalse; |
16147 | 30 |
import static org.testng.Assert.assertNotNull; |
31 |
import static org.testng.Assert.assertTrue; |
|
32 |
import static org.testng.Assert.fail; |
|
33 |
||
34 |
import java.io.StringReader; |
|
35 |
import java.io.StringWriter; |
|
36 |
import java.lang.reflect.Method; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.Map; |
|
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
39 |
import java.util.Objects; |
16147 | 40 |
import java.util.concurrent.Callable; |
41 |
import javax.script.Bindings; |
|
42 |
import javax.script.Compilable; |
|
43 |
import javax.script.CompiledScript; |
|
44 |
import javax.script.Invocable; |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
45 |
import javax.script.ScriptContext; |
16147 | 46 |
import javax.script.ScriptEngine; |
47 |
import javax.script.ScriptEngineFactory; |
|
48 |
import javax.script.ScriptEngineManager; |
|
49 |
import javax.script.ScriptException; |
|
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
50 |
import javax.script.SimpleScriptContext; |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
51 |
import org.testng.Assert; |
16147 | 52 |
import org.testng.annotations.Test; |
53 |
||
54 |
/** |
|
55 |
* 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
|
56 |
* |
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
57 |
* @test |
17248
60ef2a001ec2
8013295: ScriptEngineTest.java fails with compilation error when running under jtreg
sundar
parents:
17239
diff
changeset
|
58 |
* @build jdk.nashorn.api.scripting.Window jdk.nashorn.api.scripting.WindowEventHandler jdk.nashorn.api.scripting.VariableArityTestInterface jdk.nashorn.api.scripting.ScriptEngineTest |
16260
3318a14ac7cb
8008729: Make sure that we can run basic jsr223 tests using jtreg
sundar
parents:
16241
diff
changeset
|
59 |
* @run testng jdk.nashorn.api.scripting.ScriptEngineTest |
16147 | 60 |
*/ |
61 |
public class ScriptEngineTest { |
|
62 |
||
63 |
private void log(String msg) { |
|
64 |
org.testng.Reporter.log(msg, true); |
|
65 |
} |
|
66 |
||
67 |
@Test |
|
68 |
public void argumentsTest() { |
|
69 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
70 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
71 |
||
72 |
String[] args = new String[] { "hello", "world" }; |
|
73 |
try { |
|
74 |
e.put("arguments", args); |
|
75 |
Object arg0 = e.eval("arguments[0]"); |
|
76 |
Object arg1 = e.eval("arguments[1]"); |
|
77 |
assertEquals(args[0], arg0); |
|
78 |
assertEquals(args[1], arg1); |
|
79 |
} catch (final Exception exp) { |
|
80 |
exp.printStackTrace(); |
|
81 |
fail(exp.getMessage()); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
@Test |
|
86 |
public void argumentsWithTest() { |
|
87 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
88 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
89 |
||
90 |
String[] args = new String[] { "hello", "world" }; |
|
91 |
try { |
|
92 |
e.put("arguments", args); |
|
93 |
Object arg0 = e.eval("var imports = new JavaImporter(java.io); " + |
|
94 |
" with(imports) { arguments[0] }"); |
|
95 |
Object arg1 = e.eval("var imports = new JavaImporter(java.util, java.io); " + |
|
96 |
" with(imports) { arguments[1] }"); |
|
97 |
assertEquals(args[0], arg0); |
|
98 |
assertEquals(args[1], arg1); |
|
99 |
} catch (final Exception exp) { |
|
100 |
exp.printStackTrace(); |
|
101 |
fail(exp.getMessage()); |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
@Test |
|
106 |
public void argumentsEmptyTest() { |
|
107 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
108 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
109 |
||
110 |
try { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
111 |
assertEquals(e.eval("arguments instanceof Array"), true); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
112 |
assertEquals(e.eval("arguments.length == 0"), true); |
16147 | 113 |
} catch (final Exception exp) { |
114 |
exp.printStackTrace(); |
|
115 |
fail(exp.getMessage()); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
@Test |
|
120 |
public void factoryTests() { |
|
121 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
122 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
123 |
assertNotNull(e); |
|
124 |
||
125 |
final ScriptEngineFactory fac = e.getFactory(); |
|
126 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
127 |
assertEquals(fac.getLanguageName(), "ECMAScript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
128 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
129 |
assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
130 |
assertEquals(fac.getEngineName(), "Oracle Nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
131 |
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
|
132 |
assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
133 |
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript"); |
16147 | 134 |
|
135 |
boolean seenJS = false; |
|
136 |
for (String ext : fac.getExtensions()) { |
|
137 |
if (ext.equals("js")) { |
|
138 |
seenJS = true; |
|
139 |
} |
|
140 |
} |
|
141 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
142 |
assertEquals(seenJS, true); |
16147 | 143 |
String str = fac.getMethodCallSyntax("obj", "foo", "x"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
144 |
assertEquals(str, "obj.foo(x)"); |
16147 | 145 |
|
146 |
boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false; |
|
147 |
for (String name : fac.getNames()) { |
|
148 |
switch (name) { |
|
149 |
case "nashorn": seenNashorn = true; break; |
|
150 |
case "javascript": seenJavaScript = true; break; |
|
151 |
case "ECMAScript": seenECMAScript = true; break; |
|
152 |
} |
|
153 |
} |
|
154 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
155 |
assertTrue(seenNashorn); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
156 |
assertTrue(seenJavaScript); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
157 |
assertTrue(seenECMAScript); |
16147 | 158 |
|
159 |
boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false; |
|
160 |
for (String mime : fac.getMimeTypes()) { |
|
161 |
switch (mime) { |
|
162 |
case "application/javascript": seenAppJS = true; break; |
|
163 |
case "application/ecmascript": seenAppECMA = true; break; |
|
164 |
case "text/javascript": seenTextJS = true; break; |
|
165 |
case "text/ecmascript": seenTextECMA = true; break; |
|
166 |
} |
|
167 |
} |
|
168 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
169 |
assertTrue(seenAppJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
170 |
assertTrue(seenAppECMA); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
171 |
assertTrue(seenTextJS); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
172 |
assertTrue(seenTextECMA); |
16147 | 173 |
} |
174 |
||
175 |
@Test |
|
176 |
public void evalTests() { |
|
177 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
178 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
179 |
e.put(ScriptEngine.FILENAME, "myfile.js"); |
|
180 |
||
181 |
try { |
|
182 |
e.eval("print('hello')"); |
|
183 |
} catch (final ScriptException se) { |
|
184 |
fail(se.getMessage()); |
|
185 |
} |
|
186 |
try { |
|
187 |
e.eval("print('hello)"); |
|
188 |
fail("script exception expected"); |
|
189 |
} catch (final ScriptException se) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
190 |
assertEquals(se.getLineNumber(), 1); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
191 |
assertEquals(se.getColumnNumber(), 13); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
192 |
assertEquals(se.getFileName(), "myfile.js"); |
16147 | 193 |
// se.printStackTrace(); |
194 |
} |
|
195 |
||
196 |
try { |
|
197 |
Object obj = e.eval("34 + 41"); |
|
198 |
assertTrue(34.0 + 41.0 == ((Number)obj).doubleValue()); |
|
199 |
obj = e.eval("x = 5"); |
|
200 |
assertTrue(5.0 == ((Number)obj).doubleValue()); |
|
201 |
} catch (final ScriptException se) { |
|
202 |
se.printStackTrace(); |
|
203 |
fail(se.getMessage()); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
@Test |
|
208 |
public void compileTests() { |
|
209 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
210 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
211 |
CompiledScript script = null; |
|
212 |
||
213 |
try { |
|
214 |
script = ((Compilable)e).compile("print('hello')"); |
|
215 |
} catch (final ScriptException se) { |
|
216 |
fail(se.getMessage()); |
|
217 |
} |
|
218 |
||
219 |
try { |
|
220 |
script.eval(); |
|
221 |
} catch (final ScriptException | NullPointerException se) { |
|
222 |
se.printStackTrace(); |
|
223 |
fail(se.getMessage()); |
|
224 |
} |
|
225 |
||
226 |
// try to compile from a Reader |
|
227 |
try { |
|
228 |
script = ((Compilable)e).compile(new StringReader("print('world')")); |
|
229 |
} catch (final ScriptException se) { |
|
230 |
fail(se.getMessage()); |
|
231 |
} |
|
232 |
||
233 |
try { |
|
234 |
script.eval(); |
|
235 |
} catch (final ScriptException | NullPointerException se) { |
|
236 |
se.printStackTrace(); |
|
237 |
fail(se.getMessage()); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
@Test |
|
242 |
public void createBindingsTest() { |
|
243 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
244 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
245 |
Bindings b = e.createBindings(); |
|
246 |
b.put("foo", 42.0); |
|
247 |
Object res = null; |
|
248 |
try { |
|
249 |
res = e.eval("foo == 42.0", b); |
|
250 |
} catch (final ScriptException | NullPointerException se) { |
|
251 |
se.printStackTrace(); |
|
252 |
fail(se.getMessage()); |
|
253 |
} |
|
254 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
255 |
assertEquals(res, Boolean.TRUE); |
16147 | 256 |
} |
257 |
||
258 |
@Test |
|
259 |
public void getInterfaceTest() { |
|
260 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
261 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
262 |
final Invocable inv = (Invocable)e; |
|
263 |
||
264 |
// try to get interface from global functions |
|
265 |
try { |
|
266 |
e.eval("function run() { print('run'); };"); |
|
267 |
final Runnable runnable = inv.getInterface(Runnable.class); |
|
268 |
runnable.run(); |
|
269 |
} catch (final Exception exp) { |
|
270 |
exp.printStackTrace(); |
|
271 |
fail(exp.getMessage()); |
|
272 |
} |
|
273 |
||
274 |
// try interface on specific script object |
|
275 |
try { |
|
276 |
e.eval("var obj = { run: function() { print('run from obj'); } };"); |
|
277 |
Object obj = e.get("obj"); |
|
278 |
final Runnable runnable = inv.getInterface(obj, Runnable.class); |
|
279 |
runnable.run(); |
|
280 |
} catch (final Exception exp) { |
|
281 |
exp.printStackTrace(); |
|
282 |
fail(exp.getMessage()); |
|
283 |
} |
|
284 |
} |
|
285 |
||
16528
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
286 |
public interface Foo { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
287 |
public void bar(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
288 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
289 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
290 |
public interface Foo2 extends Foo { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
291 |
public void bar2(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
292 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
293 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
294 |
@Test |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
295 |
public void getInterfaceMissingTest() { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
296 |
final ScriptEngineManager manager = new ScriptEngineManager(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
297 |
final ScriptEngine engine = manager.getEngineByName("nashorn"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
298 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
299 |
// don't define any function. |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
300 |
try { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
301 |
engine.eval(""); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
302 |
} catch (final Exception exp) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
303 |
exp.printStackTrace(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
304 |
fail(exp.getMessage()); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
305 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
306 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
307 |
Runnable runnable = ((Invocable)engine).getInterface(Runnable.class); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
308 |
if (runnable != null) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
309 |
fail("runnable is not null!"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
310 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
311 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
312 |
// now define "run" |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
313 |
try { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
314 |
engine.eval("function run() { print('this is run function'); }"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
315 |
} catch (final Exception exp) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
316 |
exp.printStackTrace(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
317 |
fail(exp.getMessage()); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
318 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
319 |
runnable = ((Invocable)engine).getInterface(Runnable.class); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
320 |
// should not return null now! |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
321 |
runnable.run(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
322 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
323 |
// define only one method of "Foo2" |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
324 |
try { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
325 |
engine.eval("function bar() { print('bar function'); }"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
326 |
} catch (final Exception exp) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
327 |
exp.printStackTrace(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
328 |
fail(exp.getMessage()); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
329 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
330 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
331 |
Foo2 foo2 = ((Invocable)engine).getInterface(Foo2.class); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
332 |
if (foo2 != null) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
333 |
throw new RuntimeException("foo2 is not null!"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
334 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
335 |
|
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
336 |
// now define other method of "Foo2" |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
337 |
try { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
338 |
engine.eval("function bar2() { print('bar2 function'); }"); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
339 |
} catch (final Exception exp) { |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
340 |
exp.printStackTrace(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
341 |
fail(exp.getMessage()); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
342 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
343 |
foo2 = ((Invocable)engine).getInterface(Foo2.class); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
344 |
foo2.bar(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
345 |
foo2.bar2(); |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
346 |
} |
8d20ffabe47e
8010199: javax.script.Invocable implementation for nashorn does not return null when matching functions are missing
sundar
parents:
16522
diff
changeset
|
347 |
|
16147 | 348 |
@Test |
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
349 |
/** |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
350 |
* Try passing non-interface Class object for interface implementation. |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
351 |
*/ |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
352 |
public void getNonInterfaceGetInterfaceTest() { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
353 |
final ScriptEngineManager manager = new ScriptEngineManager(); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
354 |
final ScriptEngine engine = manager.getEngineByName("nashorn"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
355 |
try { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
356 |
log(Objects.toString(((Invocable)engine).getInterface(Object.class))); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
357 |
fail("Should have thrown IllegalArgumentException"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
358 |
} catch (final Exception exp) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
359 |
if (! (exp instanceof IllegalArgumentException)) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
360 |
fail("IllegalArgumentException expected, got " + exp); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
361 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
362 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
363 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
364 |
|
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
365 |
@Test |
16147 | 366 |
public void accessGlobalTest() { |
367 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
368 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
369 |
||
370 |
try { |
|
371 |
e.eval("var x = 'hello'"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
372 |
assertEquals(e.get("x"), "hello"); |
16147 | 373 |
} catch (final ScriptException exp) { |
374 |
exp.printStackTrace(); |
|
375 |
fail(exp.getMessage()); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
@Test |
|
380 |
public void exposeGlobalTest() { |
|
381 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
382 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
383 |
||
384 |
try { |
|
385 |
e.put("y", "foo"); |
|
386 |
e.eval("print(y)"); |
|
387 |
} catch (final ScriptException exp) { |
|
388 |
exp.printStackTrace(); |
|
389 |
fail(exp.getMessage()); |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
@Test |
|
394 |
public void putGlobalFunctionTest() { |
|
395 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
396 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
397 |
||
398 |
e.put("callable", new Callable<String>() { |
|
399 |
@Override |
|
400 |
public String call() throws Exception { |
|
401 |
return "callable was called"; |
|
402 |
} |
|
403 |
}); |
|
404 |
||
405 |
try { |
|
406 |
e.eval("print(callable.call())"); |
|
407 |
} catch (final ScriptException exp) { |
|
408 |
exp.printStackTrace(); |
|
409 |
fail(exp.getMessage()); |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
@Test |
|
414 |
public void windowAlertTest() { |
|
415 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
416 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
417 |
final Window window = new Window(); |
|
418 |
||
419 |
try { |
|
420 |
e.put("window", window); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
421 |
e.eval("print(window.alert)"); |
16147 | 422 |
e.eval("window.alert('calling window.alert...')"); |
423 |
} catch (final Exception exp) { |
|
424 |
exp.printStackTrace(); |
|
425 |
fail(exp.getMessage()); |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
@Test |
|
430 |
public void windowLocationTest() { |
|
431 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
432 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
433 |
final Window window = new Window(); |
|
434 |
||
435 |
try { |
|
436 |
e.put("window", window); |
|
437 |
e.eval("print(window.location)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
438 |
final Object locationValue = e.eval("window.getLocation()"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
439 |
assertEquals(locationValue, "http://localhost:8080/window"); |
16147 | 440 |
} catch (final Exception exp) { |
441 |
exp.printStackTrace(); |
|
442 |
fail(exp.getMessage()); |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
@Test |
|
447 |
public void windowItemTest() { |
|
448 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
449 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
450 |
final Window window = new Window(); |
|
451 |
||
452 |
try { |
|
453 |
e.put("window", window); |
|
454 |
final String item1 = (String)e.eval("window.item(65535)"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
455 |
assertEquals(item1, "ffff"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
456 |
final String item2 = (String)e.eval("window.item(255)"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
457 |
assertEquals(item2, "ff"); |
16147 | 458 |
} catch (final Exception exp) { |
459 |
exp.printStackTrace(); |
|
460 |
fail(exp.getMessage()); |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
@Test |
|
465 |
public void windowEventTest() { |
|
466 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
467 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
468 |
final Window window = new Window(); |
|
469 |
||
470 |
try { |
|
471 |
e.put("window", window); |
|
472 |
e.eval("window.onload = function() { print('window load event fired'); return true }"); |
|
473 |
assertTrue((Boolean)e.eval("window.onload.loaded()")); |
|
474 |
final WindowEventHandler handler = window.getOnload(); |
|
475 |
assertNotNull(handler); |
|
476 |
assertTrue(handler.loaded()); |
|
477 |
} catch (final Exception exp) { |
|
478 |
exp.printStackTrace(); |
|
479 |
fail(exp.getMessage()); |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
@Test |
|
484 |
public void throwTest() { |
|
485 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
486 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
487 |
e.put(ScriptEngine.FILENAME, "throwtest.js"); |
|
488 |
||
489 |
try { |
|
490 |
e.eval("throw 'foo'"); |
|
491 |
} catch (final ScriptException exp) { |
|
492 |
log(exp.getMessage()); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
493 |
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
|
494 |
assertEquals(exp.getFileName(), "throwtest.js"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
495 |
assertEquals(exp.getLineNumber(), 1); |
16147 | 496 |
} |
497 |
} |
|
498 |
||
499 |
@Test |
|
500 |
public void setTimeoutTest() { |
|
501 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
502 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
503 |
final Window window = new Window(); |
|
504 |
||
505 |
try { |
|
506 |
final Class<?> setTimeoutParamTypes[] = { Window.class, String.class, int.class }; |
|
507 |
final Method setTimeout = Window.class.getDeclaredMethod("setTimeout", setTimeoutParamTypes); |
|
508 |
assertNotNull(setTimeout); |
|
509 |
e.put("window", window); |
|
510 |
e.eval("window.setTimeout('foo()', 100)"); |
|
511 |
||
512 |
// try to make setTimeout global |
|
513 |
e.put("setTimeout", setTimeout); |
|
514 |
// TODO: java.lang.ClassCastException: required class |
|
515 |
// java.lang.Integer but encountered class java.lang.Double |
|
516 |
// e.eval("setTimeout('foo2()', 200)"); |
|
517 |
} catch (final Exception exp) { |
|
518 |
exp.printStackTrace(); |
|
519 |
fail(exp.getMessage()); |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
@Test |
|
524 |
public void setWriterTest() { |
|
525 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
526 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
527 |
final StringWriter sw = new StringWriter(); |
|
528 |
e.getContext().setWriter(sw); |
|
529 |
||
530 |
try { |
|
531 |
e.eval("print('hello world')"); |
|
532 |
} catch (final Exception exp) { |
|
533 |
exp.printStackTrace(); |
|
534 |
fail(exp.getMessage()); |
|
535 |
} |
|
536 |
// dos2unix - fix line endings if running on windows |
|
537 |
assertEquals(sw.toString().replaceAll("\r", ""), "hello world\n"); |
|
538 |
} |
|
539 |
||
540 |
@SuppressWarnings("unchecked") |
|
541 |
@Test |
|
542 |
public void reflectionTest() throws ScriptException { |
|
543 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
544 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
545 |
||
546 |
e.eval("var obj = { x: 344, y: 'nashorn' }"); |
|
547 |
||
548 |
int count = 0; |
|
549 |
Map<Object, Object> map = (Map<Object, Object>)e.get("obj"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
550 |
assertFalse(map.isEmpty()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
551 |
assertTrue(map.keySet().contains("x")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
552 |
assertTrue(map.containsKey("x")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
553 |
assertTrue(map.values().contains("nashorn")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
554 |
assertTrue(map.containsValue("nashorn")); |
16147 | 555 |
for (final Map.Entry<?, ?> ex : map.entrySet()) { |
556 |
final Object key = ex.getKey(); |
|
557 |
if (key.equals("x")) { |
|
558 |
assertTrue(344 == ((Number)ex.getValue()).doubleValue()); |
|
559 |
count++; |
|
560 |
} else if (key.equals("y")) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
561 |
assertEquals(ex.getValue(), "nashorn"); |
16147 | 562 |
count++; |
563 |
} |
|
564 |
} |
|
565 |
assertEquals(2, count); |
|
566 |
assertEquals(2, map.size()); |
|
567 |
||
568 |
// add property |
|
569 |
map.put("z", "hello"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
570 |
assertEquals(e.eval("obj.z"), "hello"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
571 |
assertEquals(map.get("z"), "hello"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
572 |
assertTrue(map.keySet().contains("z")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
573 |
assertTrue(map.containsKey("z")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
574 |
assertTrue(map.values().contains("hello")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
575 |
assertTrue(map.containsValue("hello")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
576 |
assertEquals(map.size(), 3); |
16147 | 577 |
|
578 |
final Map<Object, Object> newMap = new HashMap<>(); |
|
579 |
newMap.put("foo", 23.0); |
|
580 |
newMap.put("bar", true); |
|
581 |
map.putAll(newMap); |
|
582 |
||
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
583 |
assertEquals(e.eval("obj.foo"), 23.0); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
584 |
assertEquals(e.eval("obj.bar"), true); |
16147 | 585 |
|
586 |
// remove using map method |
|
587 |
map.remove("foo"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
588 |
assertEquals(e.eval("typeof obj.foo"), "undefined"); |
16147 | 589 |
|
590 |
count = 0; |
|
591 |
e.eval("var arr = [ true, 'hello' ]"); |
|
592 |
map = (Map<Object, Object>)e.get("arr"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
593 |
assertFalse(map.isEmpty()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
594 |
assertTrue(map.containsKey("length")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
595 |
assertTrue(map.containsValue("hello")); |
16147 | 596 |
for (final Map.Entry<?, ?> ex : map.entrySet()) { |
597 |
final Object key = ex.getKey(); |
|
598 |
if (key.equals("0")) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
599 |
assertEquals(ex.getValue(), Boolean.TRUE); |
16147 | 600 |
count++; |
601 |
} else if (key.equals("1")) { |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
602 |
assertEquals(ex.getValue(), "hello"); |
16147 | 603 |
count++; |
604 |
} |
|
605 |
} |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
606 |
assertEquals(count, 2); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
607 |
assertEquals(map.size(), 2); |
16147 | 608 |
|
609 |
// add element |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
610 |
map.put("2", "world"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
611 |
assertEquals(map.get("2"), "world"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
612 |
assertEquals(map.size(), 3); |
16147 | 613 |
|
614 |
// remove all |
|
615 |
map.clear(); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
616 |
assertTrue(map.isEmpty()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
617 |
assertEquals(e.eval("typeof arr[0]"), "undefined"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
618 |
assertEquals(e.eval("typeof arr[1]"), "undefined"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
619 |
assertEquals(e.eval("typeof arr[2]"), "undefined"); |
16147 | 620 |
} |
621 |
||
622 |
@Test |
|
623 |
public void redefineEchoTest() { |
|
624 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
625 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
626 |
||
627 |
try { |
|
628 |
e.eval("var echo = {}; if (typeof echo !== 'object') { throw 'echo is a '+typeof echo; }"); |
|
629 |
} catch (final Exception exp) { |
|
630 |
exp.printStackTrace(); |
|
631 |
fail(exp.getMessage()); |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
@Test |
|
636 |
public void invokeMethodTest() { |
|
637 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
638 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
639 |
||
640 |
try { |
|
641 |
e.eval("var Example = function() { this.hello = function() { return 'Hello World!'; };}; myExample = new Example();"); |
|
642 |
final Object obj = e.get("myExample"); |
|
643 |
final Object res = ((Invocable)e).invokeMethod(obj, "hello"); |
|
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
644 |
assertEquals(res, "Hello World!"); |
16147 | 645 |
} catch (final Exception exp) { |
646 |
exp.printStackTrace(); |
|
647 |
fail(exp.getMessage()); |
|
648 |
} |
|
649 |
} |
|
650 |
||
651 |
@Test |
|
652 |
public void noEnumerablePropertiesTest() { |
|
653 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
654 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
655 |
try { |
|
656 |
e.eval("for (i in this) { throw 'found property: ' + i }"); |
|
657 |
} catch (final Exception exp) { |
|
658 |
exp.printStackTrace(); |
|
659 |
fail(exp.getMessage()); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
@Test |
|
664 |
public void noRefErrorForGlobalThisAccessTest() { |
|
665 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
666 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
667 |
try { |
|
668 |
e.eval("this.foo"); |
|
669 |
} catch (final Exception exp) { |
|
670 |
exp.printStackTrace(); |
|
671 |
fail(exp.getMessage()); |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
@Test |
|
676 |
public void refErrorForUndeclaredAccessTest() { |
|
677 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
678 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
679 |
try { |
|
680 |
e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); |
|
681 |
} catch (final Exception exp) { |
|
682 |
exp.printStackTrace(); |
|
683 |
fail(exp.getMessage()); |
|
684 |
} |
|
685 |
} |
|
686 |
||
687 |
@Test |
|
688 |
public void typeErrorForGlobalThisCallTest() { |
|
689 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
690 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
691 |
try { |
|
692 |
e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); |
|
693 |
} catch (final Exception exp) { |
|
694 |
exp.printStackTrace(); |
|
695 |
fail(exp.getMessage()); |
|
696 |
} |
|
697 |
} |
|
698 |
||
699 |
@Test |
|
700 |
public void refErrorForUndeclaredCallTest() { |
|
701 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
702 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
703 |
try { |
|
704 |
e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); |
|
705 |
} catch (final Exception exp) { |
|
706 |
exp.printStackTrace(); |
|
707 |
fail(exp.getMessage()); |
|
708 |
} |
|
709 |
} |
|
710 |
||
711 |
@Test |
|
712 |
public void jsobjectTest() { |
|
713 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
714 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
715 |
try { |
|
716 |
e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); |
|
717 |
JSObject obj = (JSObject) e.get("obj"); |
|
718 |
||
719 |
// try basic get on existing properties |
|
720 |
if (! obj.getMember("bar").equals("hello")) { |
|
721 |
fail("obj.bar != 'hello'"); |
|
722 |
} |
|
723 |
||
724 |
if (! obj.getSlot(1).equals("world")) { |
|
725 |
fail("obj[1] != 'world'"); |
|
726 |
} |
|
727 |
||
728 |
if (! obj.call("func", new Object[0]).equals("hello")) { |
|
729 |
fail("obj.call('func') != 'hello'"); |
|
730 |
} |
|
731 |
||
732 |
// try setting properties |
|
733 |
obj.setMember("bar", "new-bar"); |
|
734 |
obj.setSlot(1, "new-element-1"); |
|
735 |
if (! obj.getMember("bar").equals("new-bar")) { |
|
736 |
fail("obj.bar != 'new-bar'"); |
|
737 |
} |
|
738 |
||
739 |
if (! obj.getSlot(1).equals("new-element-1")) { |
|
740 |
fail("obj[1] != 'new-element-1'"); |
|
741 |
} |
|
742 |
||
743 |
// try adding properties |
|
744 |
obj.setMember("prop", "prop-value"); |
|
745 |
obj.setSlot(12, "element-12"); |
|
746 |
if (! obj.getMember("prop").equals("prop-value")) { |
|
747 |
fail("obj.prop != 'prop-value'"); |
|
748 |
} |
|
749 |
||
750 |
if (! obj.getSlot(12).equals("element-12")) { |
|
751 |
fail("obj[12] != 'element-12'"); |
|
752 |
} |
|
753 |
||
754 |
// delete properties |
|
755 |
obj.removeMember("prop"); |
|
756 |
if ("prop-value".equals(obj.getMember("prop"))) { |
|
757 |
fail("obj.prop is not deleted!"); |
|
758 |
} |
|
759 |
||
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
760 |
// Simple eval tests |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
761 |
assertEquals(obj.eval("typeof Object"), "function"); |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
762 |
assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); |
16147 | 763 |
} catch (final Exception exp) { |
764 |
exp.printStackTrace(); |
|
765 |
fail(exp.getMessage()); |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
@Test |
|
770 |
public void invokeFunctionExceptionTest() { |
|
771 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
772 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
773 |
try { |
|
774 |
e.eval("function func() { throw new TypeError(); }"); |
|
775 |
} catch (final Throwable t) { |
|
776 |
t.printStackTrace(); |
|
777 |
fail(t.getMessage()); |
|
778 |
} |
|
779 |
||
780 |
try { |
|
781 |
((Invocable)e).invokeFunction("func"); |
|
782 |
fail("should have thrown exception"); |
|
783 |
} catch (final ScriptException se) { |
|
784 |
// ECMA TypeError property wrapped as a ScriptException |
|
785 |
log("got " + se + " as expected"); |
|
786 |
} catch (final Throwable t) { |
|
787 |
t.printStackTrace(); |
|
788 |
fail(t.getMessage()); |
|
789 |
} |
|
790 |
} |
|
791 |
||
792 |
@Test |
|
793 |
public void invokeMethodExceptionTest() { |
|
794 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
795 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
796 |
try { |
|
797 |
e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); |
|
798 |
} catch (final Throwable t) { |
|
799 |
t.printStackTrace(); |
|
800 |
fail(t.getMessage()); |
|
801 |
} |
|
802 |
||
803 |
try { |
|
804 |
final Object sobj = e.get("sobj"); |
|
805 |
((Invocable)e).invokeMethod(sobj, "foo"); |
|
806 |
fail("should have thrown exception"); |
|
807 |
} catch (final ScriptException se) { |
|
808 |
// ECMA TypeError property wrapped as a ScriptException |
|
809 |
log("got " + se + " as expected"); |
|
810 |
} catch (final Throwable t) { |
|
811 |
t.printStackTrace(); |
|
812 |
fail(t.getMessage()); |
|
813 |
} |
|
814 |
} |
|
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
815 |
|
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
816 |
@Test |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
817 |
public void scriptObjectMirrorToStringTest() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
818 |
final ScriptEngineManager m = new ScriptEngineManager(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
819 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
820 |
try { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
821 |
Object obj = e.eval("new TypeError('wrong type')"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
822 |
assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); |
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
823 |
} catch (final Throwable t) { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
824 |
t.printStackTrace(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
825 |
fail(t.getMessage()); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
826 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
827 |
|
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
828 |
try { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
829 |
Object obj = e.eval("function func() { print('hello'); }"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
830 |
assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); |
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
831 |
} catch (final Throwable t) { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
832 |
t.printStackTrace(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
833 |
fail(t.getMessage()); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
834 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
835 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
836 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
837 |
@Test |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
838 |
public void engineScopeTest() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
839 |
final ScriptEngineManager m = new ScriptEngineManager(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
840 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
841 |
Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
842 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
843 |
// check few ECMA standard built-in global properties |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
844 |
assertNotNull(engineScope.get("Object")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
845 |
assertNotNull(engineScope.get("TypeError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
846 |
assertNotNull(engineScope.get("eval")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
847 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
848 |
// can access via ScriptEngine.get as well |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
849 |
assertNotNull(e.get("Object")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
850 |
assertNotNull(e.get("TypeError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
851 |
assertNotNull(e.get("eval")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
852 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
853 |
// Access by either way should return same object |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
854 |
assertEquals(engineScope.get("Array"), e.get("Array")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
855 |
assertEquals(engineScope.get("EvalError"), e.get("EvalError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
856 |
assertEquals(engineScope.get("undefined"), e.get("undefined")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
857 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
858 |
// try exposing a new variable from scope |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
859 |
engineScope.put("myVar", "foo"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
860 |
try { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
861 |
assertEquals(e.eval("myVar"), "foo"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
862 |
} catch (final ScriptException se) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
863 |
se.printStackTrace(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
864 |
fail(se.getMessage()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
865 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
866 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
867 |
// update "myVar" in script an check the value from scope |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
868 |
try { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
869 |
e.eval("myVar = 'nashorn';"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
870 |
} catch (final ScriptException se) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
871 |
se.printStackTrace(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
872 |
fail(se.getMessage()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
873 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
874 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
875 |
// now check modified value from scope and engine |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
876 |
assertEquals(engineScope.get("myVar"), "nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
877 |
assertEquals(e.get("myVar"), "nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
878 |
} |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
879 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
880 |
@Test |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
881 |
public void multiGlobalTest() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
882 |
final ScriptEngineManager m = new ScriptEngineManager(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
883 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
884 |
final Bindings b = e.createBindings(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
885 |
final ScriptContext newCtxt = new SimpleScriptContext(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
886 |
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
887 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
888 |
try { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
889 |
Object obj1 = e.eval("Object"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
890 |
Object obj2 = e.eval("Object", newCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
891 |
Assert.assertNotEquals(obj1, obj2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
892 |
Assert.assertNotNull(obj1); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
893 |
Assert.assertNotNull(obj2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
894 |
Assert.assertEquals(obj1.toString(), obj2.toString()); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
895 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
896 |
e.eval("x = 'hello'"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
897 |
e.eval("x = 'world'", newCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
898 |
Object x1 = e.getContext().getAttribute("x"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
899 |
Object x2 = newCtxt.getAttribute("x"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
900 |
Assert.assertNotEquals(x1, x2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
901 |
Assert.assertEquals(x1, "hello"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
902 |
Assert.assertEquals(x2, "world"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
903 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
904 |
x1 = e.eval("x"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
905 |
x2 = e.eval("x", newCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
906 |
Assert.assertNotEquals(x1, x2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
907 |
Assert.assertEquals(x1, "hello"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
908 |
Assert.assertEquals(x2, "world"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
909 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
910 |
final ScriptContext origCtxt = e.getContext(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
911 |
e.setContext(newCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
912 |
e.eval("y = new Object()"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
913 |
e.eval("y = new Object()", origCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
914 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
915 |
Object y1 = origCtxt.getAttribute("y"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
916 |
Object y2 = newCtxt.getAttribute("y"); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
917 |
Assert.assertNotEquals(y1, y2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
918 |
Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt)); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
919 |
Assert.assertEquals("[object Object]", y1.toString()); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
920 |
Assert.assertEquals("[object Object]", y2.toString()); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
921 |
} catch (final ScriptException se) { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
922 |
se.printStackTrace(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
923 |
fail(se.getMessage()); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
924 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
925 |
} |
17239
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
926 |
|
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
927 |
@Test |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
928 |
/** |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
929 |
* Tests whether invocation of a JavaScript method through a variable arity Java method will pass the vararg array. |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
930 |
* Both non-vararg and vararg JavaScript methods are tested. |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
931 |
* @throws ScriptException |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
932 |
*/ |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
933 |
public void variableArityInterfaceTest() throws ScriptException { |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
934 |
final ScriptEngineManager m = new ScriptEngineManager(); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
935 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
936 |
e.eval( |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
937 |
"function test1(i, strings) {" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
938 |
" return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
939 |
"}" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
940 |
"function test2() {" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
941 |
" return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
942 |
"}" |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
943 |
); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
944 |
final VariableArityTestInterface itf = ((Invocable)e).getInterface(VariableArityTestInterface.class); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
945 |
Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
946 |
Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
947 |
} |
18864
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
948 |
|
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
949 |
@Test |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
950 |
/** |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
951 |
* Check that script can't implement sensitive package interfaces. |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
952 |
*/ |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
953 |
public void checkSensitiveInterfaceImplTest() throws ScriptException { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
954 |
final ScriptEngineManager m = new ScriptEngineManager(); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
955 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
956 |
final Object[] holder = new Object[1]; |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
957 |
e.put("holder", holder); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
958 |
// put an empty script object into array |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
959 |
e.eval("holder[0] = {}"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
960 |
// holder[0] is an object of some subclass of ScriptObject |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
961 |
Class ScriptObjectClass = holder[0].getClass().getSuperclass(); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
962 |
Class PropertyAccessClass = ScriptObjectClass.getInterfaces()[0]; |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
963 |
// implementation methods for PropertyAccess class |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
964 |
e.eval("function set() {}; function get() {}; function getInt(){} " + |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
965 |
"function getDouble(){}; function getLong() {}; " + |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
966 |
"this.delete = function () {}; function has() {}; " + |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
967 |
"function hasOwnProperty() {}"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
968 |
|
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
969 |
// get implementation of a restricted package interface |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
970 |
try { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
971 |
log(Objects.toString(((Invocable)e).getInterface(PropertyAccessClass))); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
972 |
fail("should have thrown SecurityException"); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
973 |
} catch (final Exception exp) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
974 |
if (! (exp instanceof SecurityException)) { |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
975 |
fail("SecurityException expected, got " + exp); |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
976 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
977 |
} |
c701b823ed9e
8020276: interface checks in Invocable.getInterface implementation
sundar
parents:
17751
diff
changeset
|
978 |
} |
16147 | 979 |
} |