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
28 import java.io.File;
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31 import jdk.nashorn.internal.runtime.Context;
32 import jdk.nashorn.internal.runtime.ErrorManager;
33 import jdk.nashorn.internal.runtime.ScriptFunction;
34 import jdk.nashorn.internal.runtime.ScriptObject;
35 import jdk.nashorn.internal.runtime.Source;
36 import jdk.nashorn.internal.runtime.options.Options;
37 import org.testng.Assert;
38 import org.testng.annotations.AfterClass;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 /**
43 * Tests to check Nashorn JS compiler - just compiler and not execution of scripts.
44 */
45 public class CompilerTest {
46 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose"));
47 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262"));
48 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir");
49 private static final String TEST_NODE_DIR = System.getProperty("test.node.dir");
50 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir");
51
52 interface TestFilter {
53 public boolean exclude(File file, String content);
54 }
55
56 private void log(String msg) {
57 org.testng.Reporter.log(msg, true);
58 }
59
60 private Context context;
61 private ScriptObject global;
62
63 @BeforeClass
64 public void setupTest() {
65 final Options options = new Options("nashorn");
66 options.set("anon.functions", true);
67 options.set("compile.only", true);
68 options.set("print.ast", true);
69 options.set("print.parse", true);
70 options.set("scripting", true);
71
72 final ErrorManager errors = new ErrorManager() {
73 @Override
74 public void error(final String msg) {
75 log(msg);
76 }
77 };
78
79 final StringWriter sw = new StringWriter();
80 final PrintWriter pw = new PrintWriter(sw);
81 this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader());
129 private int failed;
130 // scripts that were skipped - all tests with @negative are
131 // skipped for now.
132 private int skipped;
133
134 private void compileJSDirectory(final File dir, final TestFilter filter) {
135 for (final File f : dir.listFiles()) {
136 if (f.isDirectory()) {
137 compileJSDirectory(f, filter);
138 } else if (f.getName().endsWith(".js")) {
139 compileJSFile(f, filter);
140 }
141 }
142 }
143
144 private void compileJSFile(final File file, final TestFilter filter) {
145 if (VERBOSE) {
146 log("Begin compiling " + file.getAbsolutePath());
147 }
148
149 final ScriptObject oldGlobal = Context.getGlobal();
150 final boolean globalChanged = (oldGlobal != global);
151
152 try {
153 final char[] buffer = Source.readFully(file);
154 boolean excluded = false;
155
156 if (filter != null) {
157 final String content = new String(buffer);
158 excluded = filter.exclude(file, content);
159 }
160
161 if (excluded) {
162 if (VERBOSE) {
163 log("Skipping " + file.getAbsolutePath());
164 }
165 skipped++;
166 return;
167 }
168
169 if (globalChanged) {
|
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
28 import java.io.File;
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31 import jdk.nashorn.internal.objects.Global;
32 import jdk.nashorn.internal.runtime.Context;
33 import jdk.nashorn.internal.runtime.ErrorManager;
34 import jdk.nashorn.internal.runtime.ScriptFunction;
35 import jdk.nashorn.internal.runtime.ScriptObject;
36 import jdk.nashorn.internal.runtime.Source;
37 import jdk.nashorn.internal.runtime.options.Options;
38 import org.testng.Assert;
39 import org.testng.annotations.AfterClass;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.Test;
42
43 /**
44 * Tests to check Nashorn JS compiler - just compiler and not execution of scripts.
45 */
46 public class CompilerTest {
47 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose"));
48 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262"));
49 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir");
50 private static final String TEST_NODE_DIR = System.getProperty("test.node.dir");
51 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir");
52
53 interface TestFilter {
54 public boolean exclude(File file, String content);
55 }
56
57 private void log(String msg) {
58 org.testng.Reporter.log(msg, true);
59 }
60
61 private Context context;
62 private Global global;
63
64 @BeforeClass
65 public void setupTest() {
66 final Options options = new Options("nashorn");
67 options.set("anon.functions", true);
68 options.set("compile.only", true);
69 options.set("print.ast", true);
70 options.set("print.parse", true);
71 options.set("scripting", true);
72
73 final ErrorManager errors = new ErrorManager() {
74 @Override
75 public void error(final String msg) {
76 log(msg);
77 }
78 };
79
80 final StringWriter sw = new StringWriter();
81 final PrintWriter pw = new PrintWriter(sw);
82 this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader());
130 private int failed;
131 // scripts that were skipped - all tests with @negative are
132 // skipped for now.
133 private int skipped;
134
135 private void compileJSDirectory(final File dir, final TestFilter filter) {
136 for (final File f : dir.listFiles()) {
137 if (f.isDirectory()) {
138 compileJSDirectory(f, filter);
139 } else if (f.getName().endsWith(".js")) {
140 compileJSFile(f, filter);
141 }
142 }
143 }
144
145 private void compileJSFile(final File file, final TestFilter filter) {
146 if (VERBOSE) {
147 log("Begin compiling " + file.getAbsolutePath());
148 }
149
150 final Global oldGlobal = Context.getGlobal();
151 final boolean globalChanged = (oldGlobal != global);
152
153 try {
154 final char[] buffer = Source.readFully(file);
155 boolean excluded = false;
156
157 if (filter != null) {
158 final String content = new String(buffer);
159 excluded = filter.exclude(file, content);
160 }
161
162 if (excluded) {
163 if (VERBOSE) {
164 log("Skipping " + file.getAbsolutePath());
165 }
166 skipped++;
167 return;
168 }
169
170 if (globalChanged) {
|