author | hannesw |
Fri, 06 Jun 2014 16:51:53 +0200 | |
changeset 24879 | d316854e4249 |
parent 24206 | 40c6d45af73f |
child 27361 | 86c4ddb4797b |
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.internal.runtime; |
|
27 |
||
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
28 |
import static jdk.nashorn.internal.runtime.Source.sourceFor; |
16147 | 29 |
import static org.testng.Assert.assertEquals; |
30 |
import static org.testng.Assert.assertTrue; |
|
24879
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
31 |
import static org.testng.Assert.fail; |
16147 | 32 |
|
33 |
import java.util.Map; |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19884
diff
changeset
|
34 |
import jdk.nashorn.internal.objects.Global; |
16147 | 35 |
import jdk.nashorn.internal.runtime.options.Options; |
36 |
import org.testng.annotations.Test; |
|
37 |
||
38 |
/** |
|
39 |
* Basic Context API tests. |
|
16263
0679aaa72927
8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents:
16234
diff
changeset
|
40 |
* |
0679aaa72927
8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents:
16234
diff
changeset
|
41 |
* @test |
0679aaa72927
8008789: Enable java access and nashorn runtime tests for jtreg
sundar
parents:
16234
diff
changeset
|
42 |
* @run testng jdk.nashorn.internal.runtime.ContextTest |
16147 | 43 |
*/ |
44 |
public class ContextTest { |
|
45 |
// basic context eval test |
|
46 |
@Test |
|
47 |
public void evalTest() { |
|
48 |
final Options options = new Options(""); |
|
49 |
final ErrorManager errors = new ErrorManager(); |
|
16185 | 50 |
final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19884
diff
changeset
|
51 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 52 |
Context.setGlobal(cx.createGlobal()); |
53 |
try { |
|
54 |
String code = "22 + 10"; |
|
55 |
assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); |
|
56 |
||
57 |
code = "obj = { js: 'nashorn' }; obj.js"; |
|
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
58 |
assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); |
16147 | 59 |
} finally { |
60 |
Context.setGlobal(oldGlobal); |
|
61 |
} |
|
62 |
} |
|
63 |
||
24879
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
64 |
// Make sure trying to compile an invalid script returns null - see JDK-8046215. |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
65 |
@Test |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
66 |
public void compileErrorTest() { |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
67 |
final Options options = new Options(""); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
68 |
final ErrorManager errors = new ErrorManager(); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
69 |
final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
70 |
final Global oldGlobal = Context.getGlobal(); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
71 |
Context.setGlobal(cx.createGlobal()); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
72 |
try { |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
73 |
final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal()); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
74 |
if (script != null) { |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
75 |
fail("Invalid script compiled without errors"); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
76 |
} |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
77 |
if (errors.getNumberOfErrors() != 1) { |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
78 |
fail("Wrong number of errors: " + errors.getNumberOfErrors()); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
79 |
} |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
80 |
} finally { |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
81 |
Context.setGlobal(oldGlobal); |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
82 |
} |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
83 |
} |
d316854e4249
8046215: Running uncompilable scripts throws NullPointerException
hannesw
parents:
24206
diff
changeset
|
84 |
|
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
85 |
// basic check for JS reflection access - java.util.Map-like access on ScriptObject |
16147 | 86 |
@Test |
87 |
public void reflectionTest() { |
|
88 |
final Options options = new Options(""); |
|
89 |
final ErrorManager errors = new ErrorManager(); |
|
16185 | 90 |
final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
16263
diff
changeset
|
91 |
final boolean strict = cx.getEnv()._strict; |
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19884
diff
changeset
|
92 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 93 |
Context.setGlobal(cx.createGlobal()); |
94 |
||
95 |
try { |
|
96 |
final String code = "var obj = { x: 344, y: 42 }"; |
|
97 |
eval(cx, "<reflectionTest>", code); |
|
98 |
||
99 |
final Object obj = cx.getGlobal().get("obj"); |
|
100 |
||
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
101 |
assertTrue(obj instanceof ScriptObject); |
16147 | 102 |
|
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
103 |
final ScriptObject sobj = (ScriptObject)obj; |
16147 | 104 |
int count = 0; |
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
105 |
for (final Map.Entry<?, ?> ex : sobj.entrySet()) { |
16147 | 106 |
final Object key = ex.getKey(); |
107 |
if (key.equals("x")) { |
|
108 |
assertTrue(ex.getValue() instanceof Number); |
|
109 |
assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); |
|
110 |
||
111 |
count++; |
|
112 |
} else if (key.equals("y")) { |
|
113 |
assertTrue(ex.getValue() instanceof Number); |
|
114 |
assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); |
|
115 |
||
116 |
count++; |
|
117 |
} |
|
118 |
} |
|
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
119 |
assertEquals(count, 2); |
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
120 |
assertEquals(sobj.size(), 2); |
16147 | 121 |
|
122 |
// add property |
|
19884
1bacbaa1bfc7
8024180: Incorrect handling of expression and parent scope in 'with' statements
sundar
parents:
16263
diff
changeset
|
123 |
sobj.put("zee", "hello", strict); |
16183
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
124 |
assertEquals(sobj.get("zee"), "hello"); |
b9578bc4cd68
8006557: JDK8/Lambda build clashes on Map.replace()
sundar
parents:
16151
diff
changeset
|
125 |
assertEquals(sobj.size(), 3); |
16147 | 126 |
|
127 |
} finally { |
|
128 |
Context.setGlobal(oldGlobal); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
private Object eval(final Context cx, final String name, final String code) { |
|
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
133 |
final Source source = sourceFor(name, code); |
16147 | 134 |
final ScriptObject global = Context.getGlobal(); |
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16221
diff
changeset
|
135 |
final ScriptFunction func = cx.compileScript(source, global); |
16147 | 136 |
return func != null ? ScriptRuntime.apply(func, global) : null; |
137 |
} |
|
138 |
} |