8016239: loadWithNewGlobal should support user supplied arguments from the caller
authorsundar
Mon, 10 Jun 2013 19:54:07 +0530
changeset 18321 b2ceadf211cb
parent 18320 9cb04632b21a
child 18322 7d5c1fa9c82c
8016239: loadWithNewGlobal should support user supplied arguments from the caller Reviewed-by: lagergren, attila, jlaskey
nashorn/src/jdk/nashorn/internal/objects/Global.java
nashorn/src/jdk/nashorn/internal/runtime/Context.java
nashorn/test/script/basic/JDK-8016239.js
nashorn/test/script/basic/JDK-8016239.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 10 13:27:07 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 10 19:54:07 2013 +0530
@@ -372,7 +372,7 @@
     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 LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object.class, Object[].class);
     private static final MethodHandle EXIT              = findOwnMH("exit",              Object.class, Object.class, Object.class);
 
     private final Context context;
@@ -752,14 +752,15 @@
      *
      * @param self    scope
      * @param source  source to load
+     * @param args (optional) arguments to be passed to the loaded script
      *
      * @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 {
+    public static Object loadWithNewGlobal(final Object self, final Object source, final Object...args) throws IOException {
         final Global global = Global.instance();
-        return global.context.loadWithNewGlobal(source);
+        return global.context.loadWithNewGlobal(source, args);
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 10 13:27:07 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 10 19:54:07 2013 +0530
@@ -496,12 +496,13 @@
      * expression, after creating a new global scope.
      *
      * @param from source expression for script
+     * @param args (optional) arguments to be passed to the loaded 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 {
+    public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
         final ScriptObject oldGlobal = getGlobalTrusted();
         final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
            @Override
@@ -518,6 +519,9 @@
         });
         setGlobalTrusted(newGlobal);
 
+        final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, newGlobal);
+        newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped));
+
         try {
             return ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal);
         } finally {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8016239.js	Mon Jun 10 19:54:07 2013 +0530
@@ -0,0 +1,43 @@
+/*
+ * 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-8016239: loadWithNewGlobal should support user supplied arguments from the caller
+ *
+ * @test
+ * @run
+ */
+
+var jmap = new java.util.HashMap();
+jmap.put("foo", "bar");
+
+loadWithNewGlobal({
+   name: "<code>",
+   script:
+        " print(arguments[0]); "+
+        " print(arguments[1]); " +
+        " print(arguments[2].foo); " +
+        " print(arguments[3])"
+   },
+  "hello", 23, { foo: 33}, jmap
+);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8016239.js.EXPECTED	Mon Jun 10 19:54:07 2013 +0530
@@ -0,0 +1,4 @@
+hello
+23
+33
+{foo=bar}