author | sundar |
Thu, 25 Jul 2013 14:05:03 +0530 | |
changeset 19099 | 30230d3febb8 |
parent 18874 | 8ba96bd382d3 |
child 19101 | 3e6fe94f02a8 |
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 |
|
19099
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
652 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
653 |
* Check that we can call invokeMethod on an object that we got by evaluating |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
654 |
* script with different Context set. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
655 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
656 |
public void invokeMethodDifferentContextTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
657 |
ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
658 |
ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
659 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
660 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
661 |
// define an object with method on it |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
662 |
Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
663 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
664 |
final ScriptContext ctxt = new SimpleScriptContext(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
665 |
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
666 |
e.setContext(ctxt); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
667 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
668 |
// invoke 'func' on obj - but with current script context changed |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
669 |
final Object res = ((Invocable)e).invokeMethod(obj, "hello"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
670 |
assertEquals(res, "Hello World!"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
671 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
672 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
673 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
674 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
675 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
676 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
677 |
@Test |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
678 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
679 |
* Check that invokeMethod throws NPE on null method name. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
680 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
681 |
public void invokeMethodNullNameTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
682 |
final ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
683 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
684 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
685 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
686 |
final Object obj = e.eval("({})"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
687 |
final Object res = ((Invocable)e).invokeMethod(obj, null); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
688 |
fail("should have thrown NPE"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
689 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
690 |
if (! (exp instanceof NullPointerException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
691 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
692 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
693 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
694 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
695 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
696 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
697 |
@Test |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
698 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
699 |
* Check that invokeMethod throws NoSuchMethodException on missing method. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
700 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
701 |
public void invokeMethodMissingTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
702 |
final ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
703 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
704 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
705 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
706 |
final Object obj = e.eval("({})"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
707 |
final Object res = ((Invocable)e).invokeMethod(obj, "nonExistentMethod"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
708 |
fail("should have thrown NoSuchMethodException"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
709 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
710 |
if (! (exp instanceof NoSuchMethodException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
711 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
712 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
713 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
714 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
715 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
716 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
717 |
@Test |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
718 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
719 |
* Check that calling method on non-script object 'thiz' results in IllegalArgumentException. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
720 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
721 |
public void invokeMethodNonScriptObjectThizTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
722 |
final ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
723 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
724 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
725 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
726 |
((Invocable)e).invokeMethod(new Object(), "toString"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
727 |
fail("should have thrown IllegalArgumentException"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
728 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
729 |
if (! (exp instanceof IllegalArgumentException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
730 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
731 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
732 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
733 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
734 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
735 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
736 |
@Test |
16147 | 737 |
public void noEnumerablePropertiesTest() { |
738 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
739 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
740 |
try { |
|
741 |
e.eval("for (i in this) { throw 'found property: ' + i }"); |
|
742 |
} catch (final Exception exp) { |
|
743 |
exp.printStackTrace(); |
|
744 |
fail(exp.getMessage()); |
|
745 |
} |
|
746 |
} |
|
747 |
||
748 |
@Test |
|
749 |
public void noRefErrorForGlobalThisAccessTest() { |
|
750 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
751 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
752 |
try { |
|
753 |
e.eval("this.foo"); |
|
754 |
} catch (final Exception exp) { |
|
755 |
exp.printStackTrace(); |
|
756 |
fail(exp.getMessage()); |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
@Test |
|
761 |
public void refErrorForUndeclaredAccessTest() { |
|
762 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
763 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
764 |
try { |
|
765 |
e.eval("try { print(foo); throw 'no ref error' } catch (e) { if (!(e instanceof ReferenceError)) throw e; }"); |
|
766 |
} catch (final Exception exp) { |
|
767 |
exp.printStackTrace(); |
|
768 |
fail(exp.getMessage()); |
|
769 |
} |
|
770 |
} |
|
771 |
||
772 |
@Test |
|
773 |
public void typeErrorForGlobalThisCallTest() { |
|
774 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
775 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
776 |
try { |
|
777 |
e.eval("try { this.foo() } catch(e) { if (! (e instanceof TypeError)) throw 'no type error' }"); |
|
778 |
} catch (final Exception exp) { |
|
779 |
exp.printStackTrace(); |
|
780 |
fail(exp.getMessage()); |
|
781 |
} |
|
782 |
} |
|
783 |
||
784 |
@Test |
|
785 |
public void refErrorForUndeclaredCallTest() { |
|
786 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
787 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
788 |
try { |
|
789 |
e.eval("try { foo() } catch(e) { if (! (e instanceof ReferenceError)) throw 'no ref error' }"); |
|
790 |
} catch (final Exception exp) { |
|
791 |
exp.printStackTrace(); |
|
792 |
fail(exp.getMessage()); |
|
793 |
} |
|
794 |
} |
|
795 |
||
796 |
@Test |
|
797 |
public void jsobjectTest() { |
|
798 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
799 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
800 |
try { |
|
801 |
e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }"); |
|
802 |
JSObject obj = (JSObject) e.get("obj"); |
|
803 |
||
804 |
// try basic get on existing properties |
|
805 |
if (! obj.getMember("bar").equals("hello")) { |
|
806 |
fail("obj.bar != 'hello'"); |
|
807 |
} |
|
808 |
||
809 |
if (! obj.getSlot(1).equals("world")) { |
|
810 |
fail("obj[1] != 'world'"); |
|
811 |
} |
|
812 |
||
813 |
if (! obj.call("func", new Object[0]).equals("hello")) { |
|
814 |
fail("obj.call('func') != 'hello'"); |
|
815 |
} |
|
816 |
||
817 |
// try setting properties |
|
818 |
obj.setMember("bar", "new-bar"); |
|
819 |
obj.setSlot(1, "new-element-1"); |
|
820 |
if (! obj.getMember("bar").equals("new-bar")) { |
|
821 |
fail("obj.bar != 'new-bar'"); |
|
822 |
} |
|
823 |
||
824 |
if (! obj.getSlot(1).equals("new-element-1")) { |
|
825 |
fail("obj[1] != 'new-element-1'"); |
|
826 |
} |
|
827 |
||
828 |
// try adding properties |
|
829 |
obj.setMember("prop", "prop-value"); |
|
830 |
obj.setSlot(12, "element-12"); |
|
831 |
if (! obj.getMember("prop").equals("prop-value")) { |
|
832 |
fail("obj.prop != 'prop-value'"); |
|
833 |
} |
|
834 |
||
835 |
if (! obj.getSlot(12).equals("element-12")) { |
|
836 |
fail("obj[12] != 'element-12'"); |
|
837 |
} |
|
838 |
||
839 |
// delete properties |
|
840 |
obj.removeMember("prop"); |
|
841 |
if ("prop-value".equals(obj.getMember("prop"))) { |
|
842 |
fail("obj.prop is not deleted!"); |
|
843 |
} |
|
844 |
||
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
845 |
// Simple eval tests |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16234
diff
changeset
|
846 |
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
|
847 |
assertEquals(obj.eval("'nashorn'.substring(3)"), "horn"); |
16147 | 848 |
} catch (final Exception exp) { |
849 |
exp.printStackTrace(); |
|
850 |
fail(exp.getMessage()); |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
@Test |
|
19099
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
855 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
856 |
* check that null function name results in NPE. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
857 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
858 |
public void invokeFunctionNullNameTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
859 |
final ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
860 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
861 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
862 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
863 |
final Object res = ((Invocable)e).invokeFunction(null); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
864 |
fail("should have thrown NPE"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
865 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
866 |
if (! (exp instanceof NullPointerException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
867 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
868 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
869 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
870 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
871 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
872 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
873 |
@Test |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
874 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
875 |
* Check that attempt to call missing function results in NoSuchMethodException. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
876 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
877 |
public void invokeFunctionMissingTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
878 |
final ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
879 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
880 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
881 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
882 |
final Object res = ((Invocable)e).invokeFunction("NonExistentFunc"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
883 |
fail("should have thrown NoSuchMethodException"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
884 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
885 |
if (! (exp instanceof NoSuchMethodException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
886 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
887 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
888 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
889 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
890 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
891 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
892 |
@Test |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
893 |
/** |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
894 |
* Check that invokeFunction calls functions only from current context's Bindings. |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
895 |
*/ |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
896 |
public void invokeFunctionDifferentContextTest() { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
897 |
ScriptEngineManager m = new ScriptEngineManager(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
898 |
ScriptEngine e = m.getEngineByName("nashorn"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
899 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
900 |
try { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
901 |
// define an object with method on it |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
902 |
Object obj = e.eval("function hello() { return 'Hello World!'; }"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
903 |
final ScriptContext ctxt = new SimpleScriptContext(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
904 |
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
905 |
// change engine's current context |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
906 |
e.setContext(ctxt); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
907 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
908 |
((Invocable)e).invokeFunction("hello"); // no 'hello' in new context! |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
909 |
fail("should have thrown NoSuchMethodException"); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
910 |
} catch (final Exception exp) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
911 |
if (! (exp instanceof NoSuchMethodException)) { |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
912 |
exp.printStackTrace(); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
913 |
fail(exp.getMessage()); |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
914 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
915 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
916 |
} |
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
917 |
|
30230d3febb8
8021252: invokeMethod throws NoSuchMethodException when script object is from different script context
sundar
parents:
18874
diff
changeset
|
918 |
@Test |
16147 | 919 |
public void invokeFunctionExceptionTest() { |
920 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
921 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
922 |
try { |
|
923 |
e.eval("function func() { throw new TypeError(); }"); |
|
924 |
} catch (final Throwable t) { |
|
925 |
t.printStackTrace(); |
|
926 |
fail(t.getMessage()); |
|
927 |
} |
|
928 |
||
929 |
try { |
|
930 |
((Invocable)e).invokeFunction("func"); |
|
931 |
fail("should have thrown exception"); |
|
932 |
} catch (final ScriptException se) { |
|
933 |
// ECMA TypeError property wrapped as a ScriptException |
|
934 |
log("got " + se + " as expected"); |
|
935 |
} catch (final Throwable t) { |
|
936 |
t.printStackTrace(); |
|
937 |
fail(t.getMessage()); |
|
938 |
} |
|
939 |
} |
|
940 |
||
941 |
@Test |
|
942 |
public void invokeMethodExceptionTest() { |
|
943 |
final ScriptEngineManager m = new ScriptEngineManager(); |
|
944 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
|
945 |
try { |
|
946 |
e.eval("var sobj = {}; sobj.foo = function func() { throw new TypeError(); }"); |
|
947 |
} catch (final Throwable t) { |
|
948 |
t.printStackTrace(); |
|
949 |
fail(t.getMessage()); |
|
950 |
} |
|
951 |
||
952 |
try { |
|
953 |
final Object sobj = e.get("sobj"); |
|
954 |
((Invocable)e).invokeMethod(sobj, "foo"); |
|
955 |
fail("should have thrown exception"); |
|
956 |
} catch (final ScriptException se) { |
|
957 |
// ECMA TypeError property wrapped as a ScriptException |
|
958 |
log("got " + se + " as expected"); |
|
959 |
} catch (final Throwable t) { |
|
960 |
t.printStackTrace(); |
|
961 |
fail(t.getMessage()); |
|
962 |
} |
|
963 |
} |
|
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
964 |
|
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
965 |
@Test |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
966 |
public void scriptObjectMirrorToStringTest() { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
967 |
final ScriptEngineManager m = new ScriptEngineManager(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
968 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
969 |
try { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
970 |
Object obj = e.eval("new TypeError('wrong type')"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
971 |
assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); |
16177
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
972 |
} catch (final Throwable t) { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
973 |
t.printStackTrace(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
974 |
fail(t.getMessage()); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
975 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
976 |
|
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
977 |
try { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
978 |
Object obj = e.eval("function func() { print('hello'); }"); |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
979 |
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
|
980 |
} catch (final Throwable t) { |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
981 |
t.printStackTrace(); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
982 |
fail(t.getMessage()); |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
983 |
} |
e6464f96bdb2
8006412: Improve toString method of ScriptObjectMirror class
sundar
parents:
16151
diff
changeset
|
984 |
} |
16182
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
985 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
986 |
@Test |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
987 |
public void engineScopeTest() { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
988 |
final ScriptEngineManager m = new ScriptEngineManager(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
989 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
990 |
Bindings engineScope = e.getBindings(ScriptContext.ENGINE_SCOPE); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
991 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
992 |
// check few ECMA standard built-in global properties |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
993 |
assertNotNull(engineScope.get("Object")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
994 |
assertNotNull(engineScope.get("TypeError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
995 |
assertNotNull(engineScope.get("eval")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
996 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
997 |
// can access via ScriptEngine.get as well |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
998 |
assertNotNull(e.get("Object")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
999 |
assertNotNull(e.get("TypeError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1000 |
assertNotNull(e.get("eval")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1001 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1002 |
// Access by either way should return same object |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1003 |
assertEquals(engineScope.get("Array"), e.get("Array")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1004 |
assertEquals(engineScope.get("EvalError"), e.get("EvalError")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1005 |
assertEquals(engineScope.get("undefined"), e.get("undefined")); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1006 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1007 |
// try exposing a new variable from scope |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1008 |
engineScope.put("myVar", "foo"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1009 |
try { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1010 |
assertEquals(e.eval("myVar"), "foo"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1011 |
} catch (final ScriptException se) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1012 |
se.printStackTrace(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1013 |
fail(se.getMessage()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1014 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1015 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1016 |
// update "myVar" in script an check the value from scope |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1017 |
try { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1018 |
e.eval("myVar = 'nashorn';"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1019 |
} catch (final ScriptException se) { |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1020 |
se.printStackTrace(); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1021 |
fail(se.getMessage()); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1022 |
} |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1023 |
|
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1024 |
// now check modified value from scope and engine |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1025 |
assertEquals(engineScope.get("myVar"), "nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1026 |
assertEquals(e.get("myVar"), "nashorn"); |
06e8c712f6a3
8006584: improve variable handling in NashornScriptEngine
sundar
parents:
16177
diff
changeset
|
1027 |
} |
16189
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1028 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1029 |
@Test |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1030 |
public void multiGlobalTest() { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1031 |
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
|
1032 |
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
|
1033 |
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
|
1034 |
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
|
1035 |
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
|
1036 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1037 |
try { |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1038 |
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
|
1039 |
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
|
1040 |
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
|
1041 |
Assert.assertNotNull(obj1); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1042 |
Assert.assertNotNull(obj2); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1043 |
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
|
1044 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1045 |
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
|
1046 |
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
|
1047 |
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
|
1048 |
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
|
1049 |
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
|
1050 |
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
|
1051 |
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
|
1052 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1053 |
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
|
1054 |
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
|
1055 |
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
|
1056 |
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
|
1057 |
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
|
1058 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1059 |
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
|
1060 |
e.setContext(newCtxt); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1061 |
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
|
1062 |
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
|
1063 |
|
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1064 |
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
|
1065 |
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
|
1066 |
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
|
1067 |
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
|
1068 |
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
|
1069 |
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
|
1070 |
} 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
|
1071 |
se.printStackTrace(); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1072 |
fail(se.getMessage()); |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1073 |
} |
38eaf01bf6d6
8006736: nashorn script engine should support the usage multiple global objects with same engine instance
sundar
parents:
16182
diff
changeset
|
1074 |
} |
17239
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1075 |
|
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1076 |
@Test |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1077 |
/** |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1078 |
* 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
|
1079 |
* 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
|
1080 |
* @throws ScriptException |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1081 |
*/ |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1082 |
public void variableArityInterfaceTest() throws ScriptException { |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1083 |
final ScriptEngineManager m = new ScriptEngineManager(); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1084 |
final ScriptEngine e = m.getEngineByName("nashorn"); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1085 |
e.eval( |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1086 |
"function test1(i, strings) {" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1087 |
" 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
|
1088 |
"}" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1089 |
"function test2() {" + |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1090 |
" 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
|
1091 |
"}" |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1092 |
); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1093 |
final VariableArityTestInterface itf = ((Invocable)e).getInterface(VariableArityTestInterface.class); |
6dd68632cdcd
8011065: Problems when script implements an interface with variadic methods
attila
parents:
16528
diff
changeset
|
1094 |
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
|
1095 |
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
|
1096 |
} |
16147 | 1097 |
} |