8046215: Running uncompilable scripts throws NullPointerException
authorhannesw
Fri, 06 Jun 2014 16:51:53 +0200
changeset 24879 d316854e4249
parent 24801 410bccbded9e
child 24880 cac5f6a0a40c
8046215: Running uncompilable scripts throws NullPointerException Reviewed-by: sundar, jlaskey
nashorn/src/jdk/nashorn/internal/runtime/Context.java
nashorn/test/src/jdk/nashorn/internal/runtime/ContextTest.java
--- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Wed Jul 05 19:44:08 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Fri Jun 06 16:51:53 2014 +0200
@@ -1014,6 +1014,9 @@
     }
 
     private static ScriptFunction getProgramFunction(final Class<?> script, final ScriptObject scope) {
+        if (script == null) {
+            return null;
+        }
         return invokeCreateProgramFunctionHandle(getCreateProgramFunctionHandle(script), scope);
     }
 
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/ContextTest.java	Wed Jul 05 19:44:08 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/ContextTest.java	Fri Jun 06 16:51:53 2014 +0200
@@ -28,6 +28,7 @@
 import static jdk.nashorn.internal.runtime.Source.sourceFor;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
 
 import java.util.Map;
 import jdk.nashorn.internal.objects.Global;
@@ -60,6 +61,27 @@
         }
     }
 
+    // Make sure trying to compile an invalid script returns null - see JDK-8046215.
+    @Test
+    public void compileErrorTest() {
+        final Options options = new Options("");
+        final ErrorManager errors = new ErrorManager();
+        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
+        final Global oldGlobal = Context.getGlobal();
+        Context.setGlobal(cx.createGlobal());
+        try {
+            final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
+            if (script != null) {
+                fail("Invalid script compiled without errors");
+            }
+            if (errors.getNumberOfErrors() != 1) {
+                fail("Wrong number of errors: " + errors.getNumberOfErrors());
+            }
+        } finally {
+            Context.setGlobal(oldGlobal);
+        }
+    }
+
     // basic check for JS reflection access - java.util.Map-like access on ScriptObject
     @Test
     public void reflectionTest() {