changeset 887:d3cc5b704bfe

8046215: Running uncompilable scripts throws NullPointerException Reviewed-by: sundar, jlaskey
author hannesw
date Fri, 06 Jun 2014 16:51:53 +0200
parents 4a47b7cfecdf
children 96f475bfb917
files src/jdk/nashorn/internal/runtime/Context.java test/src/jdk/nashorn/internal/runtime/ContextTest.java
diffstat 2 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/runtime/Context.java	Thu Jun 05 19:38:45 2014 -0700
+++ b/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/test/src/jdk/nashorn/internal/runtime/ContextTest.java	Thu Jun 05 19:38:45 2014 -0700
+++ b/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() {