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

Print this page

        

@@ -325,11 +325,15 @@
 
         return UNDEFINED;
     }
 
     private void evalEngineScript() throws ScriptException {
-        final URL url = NashornScriptEngine.class.getResource("resources/engine.js");
+        evalSupportScript("resources/engine.js");
+    }
+    
+    private void evalSupportScript(String script) throws ScriptException {
+        final URL url = NashornScriptEngine.class.getResource(script);
         try {
             final InputStream is = url.openStream();
             put(ScriptEngine.FILENAME, url);
             try (final InputStreamReader isr = new InputStreamReader(is)) {
                 eval(isr);

@@ -433,10 +437,19 @@
             if (globalChanged) {
                 setNashornGlobal(global);
             }
 
             setContextVariables(ctxt);
+            final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
+            final String fileName = (val != null) ? val.toString() : "<eval>";
+            
+            // if this is jrunscript's init.js, we want to run the replacement
+            if ("<system-init>".equals(fileName)) {
+                evalSupportScript("resources/init.js");
+                return null;
+            }
+            
             Object res = ScriptRuntime.apply(script, global);
             res = ScriptObjectMirror.wrap(res, global);
             return (res == UNDEFINED) ? null : res;
         } catch (final Exception e) {
             throwAsScriptException(e);

@@ -484,15 +497,10 @@
         final boolean globalChanged = (oldGlobal != global);
         try {
             final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
             final String fileName = (val != null) ? val.toString() : "<eval>";
 
-            // !!HACK!! do not evaluate "init.js" from jrunscript tool!!
-            if ("<system-init>".equals(fileName)) {
-                return null;
-            }
-
             final Source source = new Source(fileName, buf);
             if (globalChanged) {
                 setNashornGlobal(global);
             }