8015741: Need a global.load function that starts with a new global scope.
authorjlaskey
Mon, 03 Jun 2013 08:34:29 -0300
changeset 17974 a2818f17324e
parent 17973 d220c8157f25
child 17975 2fd9facd0169
8015741: Need a global.load function that starts with a new global scope. Reviewed-by: sundar, lagergren Contributed-by: james.laskey@oracle.com
nashorn/src/jdk/nashorn/internal/objects/Global.java
nashorn/src/jdk/nashorn/internal/runtime/Context.java
nashorn/test/script/basic/JDK-8015741.js
nashorn/test/script/basic/JDK-8015741.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 03 15:58:14 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 03 08:34:29 2013 -0300
@@ -119,6 +119,10 @@
     @Property(attributes = Attribute.NOT_ENUMERABLE)
     public Object load;
 
+    /** Nashorn extension: global.loadWithNewGlobal */
+    @Property(attributes = Attribute.NOT_ENUMERABLE)
+    public Object loadWithNewGlobal;
+
     /** Nashorn extension: global.exit */
     @Property(attributes = Attribute.NOT_ENUMERABLE)
     public Object exit;
@@ -364,11 +368,12 @@
     // Used to store the last RegExp result to support deprecated RegExp constructor properties
     private RegExpResult lastRegExpResult;
 
-    private static final MethodHandle EVAL    = findOwnMH("eval",    Object.class, Object.class, Object.class);
-    private static final MethodHandle PRINT   = findOwnMH("print",   Object.class, Object.class, Object[].class);
-    private static final MethodHandle PRINTLN = findOwnMH("println", Object.class, Object.class, Object[].class);
-    private static final MethodHandle LOAD    = findOwnMH("load",    Object.class, Object.class, Object.class);
-    private static final MethodHandle EXIT    = findOwnMH("exit",    Object.class, Object.class, Object.class);
+    private static final MethodHandle EVAL              = findOwnMH("eval",              Object.class, Object.class, Object.class);
+    private static final MethodHandle PRINT             = findOwnMH("print",             Object.class, Object.class, Object[].class);
+    private static final MethodHandle PRINTLN           = findOwnMH("println",           Object.class, Object.class, Object[].class);
+    private static final MethodHandle LOAD              = findOwnMH("load",              Object.class, Object.class, Object.class);
+    private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object.class);
+    private static final MethodHandle EXIT              = findOwnMH("exit",              Object.class, Object.class, Object.class);
 
     private final Context context;
 
@@ -743,6 +748,21 @@
     }
 
     /**
+     * Global loadWithNewGlobal implementation - Nashorn extension
+     *
+     * @param self    scope
+     * @param source  source to load
+     *
+     * @return result of load (undefined)
+     *
+     * @throws IOException if source could not be read
+     */
+    public static Object loadWithNewGlobal(final Object self, final Object source) throws IOException {
+        final Global global = Global.instance();
+        return global.context.loadWithNewGlobal(source);
+    }
+
+    /**
      * Global exit and quit implementation - Nashorn extension: perform a {@code System.exit} call from the script
      *
      * @param self  self reference
@@ -1387,6 +1407,7 @@
         this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
         this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
         this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
+        this.loadWithNewGlobal  = ScriptFunctionImpl.makeFunction("loadWithNewGlobal", LOADWITHNEWGLOBAL);
         this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
         this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 03 15:58:14 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 03 08:34:29 2013 -0300
@@ -491,6 +491,28 @@
     }
 
     /**
+     * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
+     * expression, after creating a new global scope.
+     *
+     * @param from source expression for script
+     *
+     * @return return value for load call (undefined)
+     *
+     * @throws IOException if source cannot be found or loaded
+     */
+    public Object loadWithNewGlobal(final Object from) throws IOException {
+        final ScriptObject oldGlobal = getGlobalTrusted();
+        final ScriptObject newGlobal = createGlobal();
+        setGlobalTrusted(newGlobal);
+
+        try {
+            return load(newGlobal, from);
+        } finally {
+            setGlobalTrusted(oldGlobal);
+        }
+    }
+
+    /**
      * Load or get a structure class. Structure class names are based on the number of parameter fields
      * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects
      *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8015741.js	Mon Jun 03 08:34:29 2013 -0300
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8015741 : Need a global.load function that starts with a new global scope.
+ *
+ * @test
+ * @run
+ */
+
+var Thread = java.lang.Thread;
+
+myGlobal = "#0";
+var script1 = {name: "script 1", script: 'myGlobal = "#1"; print(myGlobal);'};
+var script2 = {name: "script 2", script: 'myGlobal = "#2"; print(myGlobal);'};
+var script3 = {name: "script 3", script: 'myGlobal = "#3"; print(myGlobal);'};
+var script4 = {name: "script 4", script: 'myGlobal = "#4"; print(myGlobal);'};
+
+print(myGlobal);
+load(script1);
+print(myGlobal);
+
+print(myGlobal);
+var thread1 = new Thread(function() { load(script2); });
+thread1.start();
+thread1.join();
+print(myGlobal);
+
+print(myGlobal);
+loadWithNewGlobal(script3);
+print(myGlobal);
+
+print(myGlobal);
+var thread2 = new Thread(function() { loadWithNewGlobal(script4); });
+thread2.start();
+thread2.join();
+print(myGlobal);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8015741.js.EXPECTED	Mon Jun 03 08:34:29 2013 -0300
@@ -0,0 +1,12 @@
+#0
+#1
+#1
+#1
+#2
+#2
+#2
+#3
+#2
+#2
+#4
+#2