src/jdk/nashorn/api/scripting/NashornScriptEngine.java

Print this page

        

@@ -89,11 +89,11 @@
         final Options options = new Options("nashorn");
         options.process(args);
 
         // throw ParseException on first error from script
         final ErrorManager errors = new Context.ThrowErrorManager();
-        // create new Nashorn Context and get global object
+        // create new Nashorn Context
         this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
             @Override
             public Context run() {
                 try {
                     return new Context(options, errors);

@@ -105,11 +105,23 @@
                 }
             }
         });
 
         // create new global object
-        this.global = nashornContext.createGlobal();
+        this.global =  AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
+            @Override
+            public ScriptObject run() {
+                try {
+                    return nashornContext.createGlobal();
+                } catch (final RuntimeException e) {
+                    if (Context.DEBUG) {
+                        e.printStackTrace();
+                    }
+                    throw e;
+                }
+            }
+        });
 
         // current ScriptContext exposed as "context"
         global.addOwnProperty("context", Property.NOT_ENUMERABLE, context);
         // current ScriptEngine instance exposed as "engine". We added @SuppressWarnings("LeakingThisInConstructor") as
         // NetBeans identifies this assignment as such a leak - this is a false positive as we're setting this property

@@ -119,18 +131,12 @@
         // global script arguments
         global.addOwnProperty("arguments", Property.NOT_ENUMERABLE, UNDEFINED);
 
         // evaluate engine initial script
         try {
-            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
-                @Override
-                public Void run() throws ScriptException {
                     evalEngineScript();
-                    return null;
-                }
-            });
-        } catch (final PrivilegedActionException e) {
+        } catch (final ScriptException e) {
             if (Context.DEBUG) {
                 e.printStackTrace();
             }
             throw new RuntimeException(e);
         }

@@ -328,19 +334,24 @@
 
     private void evalEngineScript() throws ScriptException {
         evalSupportScript("resources/engine.js");
     }
 
-    private void evalSupportScript(String script) throws ScriptException {
-        final URL url = NashornScriptEngine.class.getResource(script);
+    private void evalSupportScript(final String script) throws ScriptException {
         try {
-            final InputStream is = url.openStream();
-            put(ScriptEngine.FILENAME, url);
+            final InputStream is = AccessController.doPrivileged(
+                    new PrivilegedExceptionAction<InputStream>() {
+                        public InputStream run() throws Exception {
+                            final URL url = NashornScriptEngine.class.getResource(script);
+                            return url.openStream();
+                        }
+                    });
+            put(ScriptEngine.FILENAME, "<engine>:" + script);
             try (final InputStreamReader isr = new InputStreamReader(is)) {
                 eval(isr);
             }
-        } catch (final IOException e) {
+        } catch (final PrivilegedActionException | IOException e) {
             throw new ScriptException(e);
         } finally {
             put(ScriptEngine.FILENAME, null);
         }
     }