8016239: loadWithNewGlobal should support user supplied arguments from the caller

Mon, 10 Jun 2013 19:54:07 +0530

author
sundar
date
Mon, 10 Jun 2013 19:54:07 +0530
changeset 337
966868ef75ee
parent 336
a6f8ea57f048
child 338
1a5d67424e83

8016239: loadWithNewGlobal should support user supplied arguments from the caller
Reviewed-by: lagergren, attila, jlaskey

src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8016239.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8016239.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 10 13:27:07 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Mon Jun 10 19:54:07 2013 +0530
     1.3 @@ -372,7 +372,7 @@
     1.4      private static final MethodHandle PRINT             = findOwnMH("print",             Object.class, Object.class, Object[].class);
     1.5      private static final MethodHandle PRINTLN           = findOwnMH("println",           Object.class, Object.class, Object[].class);
     1.6      private static final MethodHandle LOAD              = findOwnMH("load",              Object.class, Object.class, Object.class);
     1.7 -    private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object.class);
     1.8 +    private static final MethodHandle LOADWITHNEWGLOBAL = findOwnMH("loadWithNewGlobal", Object.class, Object.class, Object.class, Object[].class);
     1.9      private static final MethodHandle EXIT              = findOwnMH("exit",              Object.class, Object.class, Object.class);
    1.10  
    1.11      private final Context context;
    1.12 @@ -752,14 +752,15 @@
    1.13       *
    1.14       * @param self    scope
    1.15       * @param source  source to load
    1.16 +     * @param args (optional) arguments to be passed to the loaded script
    1.17       *
    1.18       * @return result of load (undefined)
    1.19       *
    1.20       * @throws IOException if source could not be read
    1.21       */
    1.22 -    public static Object loadWithNewGlobal(final Object self, final Object source) throws IOException {
    1.23 +    public static Object loadWithNewGlobal(final Object self, final Object source, final Object...args) throws IOException {
    1.24          final Global global = Global.instance();
    1.25 -        return global.context.loadWithNewGlobal(source);
    1.26 +        return global.context.loadWithNewGlobal(source, args);
    1.27      }
    1.28  
    1.29      /**
     2.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 10 13:27:07 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Jun 10 19:54:07 2013 +0530
     2.3 @@ -496,12 +496,13 @@
     2.4       * expression, after creating a new global scope.
     2.5       *
     2.6       * @param from source expression for script
     2.7 +     * @param args (optional) arguments to be passed to the loaded script
     2.8       *
     2.9       * @return return value for load call (undefined)
    2.10       *
    2.11       * @throws IOException if source cannot be found or loaded
    2.12       */
    2.13 -    public Object loadWithNewGlobal(final Object from) throws IOException {
    2.14 +    public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    2.15          final ScriptObject oldGlobal = getGlobalTrusted();
    2.16          final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
    2.17             @Override
    2.18 @@ -518,6 +519,9 @@
    2.19          });
    2.20          setGlobalTrusted(newGlobal);
    2.21  
    2.22 +        final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, newGlobal);
    2.23 +        newGlobal.put("arguments", ((GlobalObject)newGlobal).wrapAsObject(wrapped));
    2.24 +
    2.25          try {
    2.26              return ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal);
    2.27          } finally {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8016239.js	Mon Jun 10 19:54:07 2013 +0530
     3.3 @@ -0,0 +1,43 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8016239: loadWithNewGlobal should support user supplied arguments from the caller
    3.29 + *
    3.30 + * @test
    3.31 + * @run
    3.32 + */
    3.33 +
    3.34 +var jmap = new java.util.HashMap();
    3.35 +jmap.put("foo", "bar");
    3.36 +
    3.37 +loadWithNewGlobal({
    3.38 +   name: "<code>",
    3.39 +   script:
    3.40 +        " print(arguments[0]); "+
    3.41 +        " print(arguments[1]); " +
    3.42 +        " print(arguments[2].foo); " +
    3.43 +        " print(arguments[3])"
    3.44 +   },
    3.45 +  "hello", 23, { foo: 33}, jmap
    3.46 +);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/JDK-8016239.js.EXPECTED	Mon Jun 10 19:54:07 2013 +0530
     4.3 @@ -0,0 +1,4 @@
     4.4 +hello
     4.5 +23
     4.6 +33
     4.7 +{foo=bar}

mercurial