8016239: loadWithNewGlobal should support user supplied arguments from the caller

Reviewed-by: lagergren, attila, jlaskey
This commit is contained in:
Athijegannathan Sundararajan 2013-06-10 19:54:07 +05:30
parent 2d2e7fe029
commit be9816a81e
4 changed files with 56 additions and 4 deletions

View File

@ -372,7 +372,7 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
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 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
*
* @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);
}
/**

View File

@ -496,12 +496,13 @@ public final class Context {
* 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 @@ public final class Context {
});
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 {

View File

@ -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
);

View File

@ -0,0 +1,4 @@
hello
23
33
{foo=bar}