1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   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 
  28 import static jdk.nashorn.internal.runtime.Source.readFully;
  29 import static jdk.nashorn.internal.runtime.Source.sourceFor;
  30 import java.io.File;
  31 import java.io.PrintWriter;
  32 import java.io.StringWriter;
  33 import jdk.nashorn.internal.objects.Global;
  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;
  40 import org.testng.annotations.AfterClass;
  41 import org.testng.annotations.BeforeClass;
  42 import org.testng.annotations.Test;
  43 
  44 /**
  45  * Tests to check Nashorn JS compiler - just compiler and not execution of scripts.
  46  */
  47 @SuppressWarnings("javadoc")
  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");
  52     private static final String TEST_NODE_DIR  = System.getProperty("test.node.dir");
  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 
  59     private static void log(final String msg) {
  60         org.testng.Reporter.log(msg, true);
  61     }
  62 
  63     private Context context;
  64     private Global  global;
  65 
  66     @BeforeClass
  67     public void setupTest() {
  68         final Options options = new Options("nashorn");
  69         options.set("compile.only", true);
  70         options.set("print.ast", true);
  71         options.set("print.parse", true);
  72         options.set("scripting", true);
  73         options.set("const.as.var", true);
  74         options.set("verify.code", true);
  75 
  76         final ErrorManager errors = new ErrorManager() {
  77             @Override
  78             public void error(final String msg) {
  79                 log(msg);
  80             }
  81         };
  82 
  83         final StringWriter sw = new StringWriter();
  84         final PrintWriter pw = new PrintWriter(sw);
  85         this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader());
  86         this.global = context.createGlobal();
  87     }
  88 
  89     @AfterClass
  90     public void tearDownTest() {
  91         this.context = null;
  92         this.global = null;
  93     }
  94 
  95     @Test
  96     public void compileAllTests() {
  97         if (TEST262) {
  98             compileTestSet(new File(TEST262_SUITE_DIR), new TestFilter() {
  99                 @Override
 100                 public boolean exclude(final File file, final String content) {
 101                     return content != null && content.contains("@negative");
 102                 }
 103             });
 104         }
 105         compileTestSet(new File(TEST_BASIC_DIR), new TestFilter() {
 106             @Override
 107             public boolean exclude(final File file, final String content) {
 108                 return file.getName().equals("es6");
 109             }
 110         });
 111         compileTestSet(new File(TEST_NODE_DIR, "node"), null);
 112         compileTestSet(new File(TEST_NODE_DIR, "src"), null);
 113     }
 114 
 115     private void compileTestSet(final File testSetDir, final TestFilter filter) {
 116         passed = 0;
 117         failed = 0;
 118         skipped = 0;
 119         if (! testSetDir.isDirectory()) {
 120             log("WARNING: " + testSetDir + " not found or not a directory");
 121             return;
 122         }
 123         log(testSetDir.getAbsolutePath());
 124         compileJSDirectory(testSetDir, filter);
 125 
 126         log(testSetDir + " compile done!");
 127         log("compile ok: " + passed);
 128         log("compile failed: " + failed);
 129         log("compile skipped: " + skipped);
 130         if (failed != 0) {
 131             Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath());
 132         }
 133     }
 134 
 135     // number of scripts that compiled fine
 136     private int passed;
 137     // number of scripts resulting in compile failure
 138     private int failed;
 139     // scripts that were skipped - all tests with @negative are
 140     // skipped for now.
 141     private int skipped;
 142 
 143     private void compileJSDirectory(final File dir, final TestFilter filter) {
 144         if (filter != null && filter.exclude(dir, null)) {
 145             return;
 146         }
 147         for (final File f : dir.listFiles()) {
 148             if (f.isDirectory()) {
 149                 compileJSDirectory(f, filter);
 150             } else if (f.getName().endsWith(".js")) {
 151                 compileJSFile(f, filter);
 152             }
 153         }
 154     }
 155 
 156     private void compileJSFile(final File file, final TestFilter filter) {
 157         if (VERBOSE) {
 158             log("Begin compiling " + file.getAbsolutePath());
 159         }
 160 
 161         final Global oldGlobal = Context.getGlobal();
 162         final boolean globalChanged = (oldGlobal != global);
 163 
 164         try {
 165             final char[] buffer = readFully(file);
 166             boolean excluded = false;
 167 
 168             if (filter != null) {
 169                 final String content = new String(buffer);
 170                 excluded = filter.exclude(file, content);
 171             }
 172 
 173             if (excluded) {
 174                 if (VERBOSE) {
 175                     log("Skipping " + file.getAbsolutePath());
 176                 }
 177                 skipped++;
 178                 return;
 179             }
 180 
 181             if (globalChanged) {
 182                 Context.setGlobal(global);
 183             }
 184             final Source source = sourceFor(file.getAbsolutePath(), buffer);
 185             final ScriptFunction script = context.compileScript(source, global);
 186             if (script == null || context.getErrorManager().getNumberOfErrors() > 0) {
 187                 log("Compile failed: " + file.getAbsolutePath());
 188                 failed++;
 189             } else {
 190                 passed++;
 191             }
 192         } catch (final Throwable t) {
 193             log("Compile failed: " + file.getAbsolutePath() + " : " + t);
 194             if (VERBOSE) {
 195                 t.printStackTrace(System.out);
 196             }
 197             failed++;
 198         } finally {
 199             if (globalChanged) {
 200                 Context.setGlobal(oldGlobal);
 201             }
 202         }
 203 
 204         if (VERBOSE) {
 205             log("Done compiling " + file.getAbsolutePath());
 206         }
 207     }
 208 }