author | lagergren |
Mon, 03 Nov 2014 11:47:41 +0100 | |
changeset 27361 | 86c4ddb4797b |
parent 27106 | d2d7e511c05c |
child 29407 | 3fd4ede1581e |
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.codegen; |
|
27 |
||
24769 | 28 |
import static jdk.nashorn.internal.runtime.Source.readFully; |
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
29 |
import static jdk.nashorn.internal.runtime.Source.sourceFor; |
16147 | 30 |
import java.io.File; |
31 |
import java.io.PrintWriter; |
|
32 |
import java.io.StringWriter; |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19456
diff
changeset
|
33 |
import jdk.nashorn.internal.objects.Global; |
16147 | 34 |
import jdk.nashorn.internal.runtime.Context; |
35 |
import jdk.nashorn.internal.runtime.ErrorManager; |
|
36 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
37 |
import jdk.nashorn.internal.runtime.Source; |
|
38 |
import jdk.nashorn.internal.runtime.options.Options; |
|
39 |
import org.testng.Assert; |
|
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
40 |
import org.testng.annotations.AfterClass; |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
41 |
import org.testng.annotations.BeforeClass; |
16147 | 42 |
import org.testng.annotations.Test; |
43 |
||
44 |
/** |
|
45 |
* Tests to check Nashorn JS compiler - just compiler and not execution of scripts. |
|
46 |
*/ |
|
27361 | 47 |
@SuppressWarnings("javadoc") |
16147 | 48 |
public class CompilerTest { |
49 |
private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose")); |
|
50 |
private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262")); |
|
51 |
private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
52 |
private static final String TEST_NODE_DIR = System.getProperty("test.node.dir"); |
16147 | 53 |
private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); |
54 |
||
55 |
interface TestFilter { |
|
56 |
public boolean exclude(File file, String content); |
|
57 |
} |
|
58 |
||
27361 | 59 |
private static void log(final String msg) { |
16147 | 60 |
org.testng.Reporter.log(msg, true); |
61 |
} |
|
62 |
||
63 |
private Context context; |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19456
diff
changeset
|
64 |
private Global global; |
16147 | 65 |
|
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
66 |
@BeforeClass |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
67 |
public void setupTest() { |
16147 | 68 |
final Options options = new Options("nashorn"); |
69 |
options.set("anon.functions", true); |
|
70 |
options.set("compile.only", true); |
|
71 |
options.set("print.ast", true); |
|
72 |
options.set("print.parse", true); |
|
73 |
options.set("scripting", true); |
|
24279 | 74 |
options.set("const.as.var", true); |
27106
d2d7e511c05c
8060688: Nashorn: Generated script class name fails --verify-code for names with special chars
sundar
parents:
26377
diff
changeset
|
75 |
options.set("verify.code", true); |
16147 | 76 |
|
77 |
final ErrorManager errors = new ErrorManager() { |
|
78 |
@Override |
|
79 |
public void error(final String msg) { |
|
80 |
log(msg); |
|
81 |
} |
|
82 |
}; |
|
83 |
||
84 |
final StringWriter sw = new StringWriter(); |
|
85 |
final PrintWriter pw = new PrintWriter(sw); |
|
16185 | 86 |
this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); |
16147 | 87 |
this.global = context.createGlobal(); |
88 |
} |
|
89 |
||
19456
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
90 |
@AfterClass |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
91 |
public void tearDownTest() { |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
92 |
this.context = null; |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
93 |
this.global = null; |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
94 |
} |
8cc345d620c8
8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents:
16530
diff
changeset
|
95 |
|
16147 | 96 |
@Test |
97 |
public void compileAllTests() { |
|
98 |
if (TEST262) { |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
99 |
compileTestSet(new File(TEST262_SUITE_DIR), new TestFilter() { |
16147 | 100 |
@Override |
101 |
public boolean exclude(final File file, final String content) { |
|
26377
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
102 |
return content != null && content.contains("@negative"); |
16147 | 103 |
} |
104 |
}); |
|
105 |
} |
|
26377
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
106 |
compileTestSet(new File(TEST_BASIC_DIR), new TestFilter() { |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
107 |
@Override |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
108 |
public boolean exclude(final File file, final String content) { |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
109 |
return file.getName().equals("es6"); |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
110 |
} |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
111 |
}); |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
112 |
compileTestSet(new File(TEST_NODE_DIR, "node"), null); |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
113 |
compileTestSet(new File(TEST_NODE_DIR, "src"), null); |
16147 | 114 |
} |
115 |
||
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
116 |
private void compileTestSet(final File testSetDir, final TestFilter filter) { |
16147 | 117 |
passed = 0; |
118 |
failed = 0; |
|
119 |
skipped = 0; |
|
120 |
if (! testSetDir.isDirectory()) { |
|
121 |
log("WARNING: " + testSetDir + " not found or not a directory"); |
|
122 |
return; |
|
123 |
} |
|
124 |
log(testSetDir.getAbsolutePath()); |
|
125 |
compileJSDirectory(testSetDir, filter); |
|
126 |
||
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16230
diff
changeset
|
127 |
log(testSetDir + " compile done!"); |
16147 | 128 |
log("compile ok: " + passed); |
129 |
log("compile failed: " + failed); |
|
130 |
log("compile skipped: " + skipped); |
|
131 |
if (failed != 0) { |
|
132 |
Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
// number of scripts that compiled fine |
|
137 |
private int passed; |
|
138 |
// number of scripts resulting in compile failure |
|
139 |
private int failed; |
|
140 |
// scripts that were skipped - all tests with @negative are |
|
141 |
// skipped for now. |
|
142 |
private int skipped; |
|
143 |
||
144 |
private void compileJSDirectory(final File dir, final TestFilter filter) { |
|
26377
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
145 |
if (filter != null && filter.exclude(dir, null)) { |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
146 |
return; |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
24778
diff
changeset
|
147 |
} |
16147 | 148 |
for (final File f : dir.listFiles()) { |
149 |
if (f.isDirectory()) { |
|
150 |
compileJSDirectory(f, filter); |
|
151 |
} else if (f.getName().endsWith(".js")) { |
|
152 |
compileJSFile(f, filter); |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
private void compileJSFile(final File file, final TestFilter filter) { |
|
158 |
if (VERBOSE) { |
|
159 |
log("Begin compiling " + file.getAbsolutePath()); |
|
160 |
} |
|
161 |
||
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19456
diff
changeset
|
162 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 163 |
final boolean globalChanged = (oldGlobal != global); |
164 |
||
165 |
try { |
|
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
166 |
final char[] buffer = readFully(file); |
16147 | 167 |
boolean excluded = false; |
168 |
||
169 |
if (filter != null) { |
|
170 |
final String content = new String(buffer); |
|
171 |
excluded = filter.exclude(file, content); |
|
172 |
} |
|
173 |
||
174 |
if (excluded) { |
|
175 |
if (VERBOSE) { |
|
176 |
log("Skipping " + file.getAbsolutePath()); |
|
177 |
} |
|
178 |
skipped++; |
|
179 |
return; |
|
180 |
} |
|
181 |
||
182 |
if (globalChanged) { |
|
183 |
Context.setGlobal(global); |
|
184 |
} |
|
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
185 |
final Source source = sourceFor(file.getAbsolutePath(), buffer); |
16230
c38c724d82e7
8008103: Source object should maintain URL of the script source as a private field
sundar
parents:
16185
diff
changeset
|
186 |
final ScriptFunction script = context.compileScript(source, global); |
16185 | 187 |
if (script == null || context.getErrorManager().getNumberOfErrors() > 0) { |
16147 | 188 |
log("Compile failed: " + file.getAbsolutePath()); |
189 |
failed++; |
|
190 |
} else { |
|
191 |
passed++; |
|
192 |
} |
|
193 |
} catch (final Throwable t) { |
|
194 |
log("Compile failed: " + file.getAbsolutePath() + " : " + t); |
|
195 |
if (VERBOSE) { |
|
196 |
t.printStackTrace(System.out); |
|
197 |
} |
|
198 |
failed++; |
|
199 |
} finally { |
|
200 |
if (globalChanged) { |
|
201 |
Context.setGlobal(oldGlobal); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
if (VERBOSE) { |
|
206 |
log("Done compiling " + file.getAbsolutePath()); |
|
207 |
} |
|
208 |
} |
|
209 |
} |