Wed, 05 Jun 2013 12:08:49 +0530
8015945: loadWithNewGlobal return value has to be properly wrapped
Reviewed-by: lagergren, hannesw
1.1 --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Tue Jun 04 22:31:48 2013 +0530 1.2 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Wed Jun 05 12:08:49 2013 +0530 1.3 @@ -342,20 +342,28 @@ 1.4 }); 1.5 } 1.6 1.7 - // package-privates below this. 1.8 - ScriptObject getScriptObject() { 1.9 - return sobj; 1.10 - } 1.11 1.12 - static Object translateUndefined(Object obj) { 1.13 - return (obj == ScriptRuntime.UNDEFINED)? null : obj; 1.14 - } 1.15 + // These are public only so that Context can access these. 1.16 1.17 - static Object wrap(final Object obj, final ScriptObject homeGlobal) { 1.18 + /** 1.19 + * Make a script object mirror on given object if needed. 1.20 + * 1.21 + * @param obj object to be wrapped 1.22 + * @param homeGlobal global to which this object belongs 1.23 + * @return wrapped object 1.24 + */ 1.25 + public static Object wrap(final Object obj, final ScriptObject homeGlobal) { 1.26 return (obj instanceof ScriptObject) ? new ScriptObjectMirror((ScriptObject)obj, homeGlobal) : obj; 1.27 } 1.28 1.29 - static Object unwrap(final Object obj, final ScriptObject homeGlobal) { 1.30 + /** 1.31 + * Unwrap a script object mirror if needed. 1.32 + * 1.33 + * @param obj object to be unwrapped 1.34 + * @param homeGlobal global to which this object belongs 1.35 + * @return unwrapped object 1.36 + */ 1.37 + public static Object unwrap(final Object obj, final ScriptObject homeGlobal) { 1.38 if (obj instanceof ScriptObjectMirror) { 1.39 final ScriptObjectMirror mirror = (ScriptObjectMirror)obj; 1.40 return (mirror.global == homeGlobal)? mirror.sobj : obj; 1.41 @@ -364,7 +372,14 @@ 1.42 return obj; 1.43 } 1.44 1.45 - static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) { 1.46 + /** 1.47 + * Wrap an array of object to script object mirrors if needed. 1.48 + * 1.49 + * @param args array to be unwrapped 1.50 + * @param homeGlobal global to which this object belongs 1.51 + * @return wrapped array 1.52 + */ 1.53 + public static Object[] wrapArray(final Object[] args, final ScriptObject homeGlobal) { 1.54 if (args == null || args.length == 0) { 1.55 return args; 1.56 } 1.57 @@ -378,7 +393,14 @@ 1.58 return newArgs; 1.59 } 1.60 1.61 - static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) { 1.62 + /** 1.63 + * Unwrap an array of script object mirrors if needed. 1.64 + * 1.65 + * @param args array to be unwrapped 1.66 + * @param homeGlobal global to which this object belongs 1.67 + * @return unwrapped array 1.68 + */ 1.69 + public static Object[] unwrapArray(final Object[] args, final ScriptObject homeGlobal) { 1.70 if (args == null || args.length == 0) { 1.71 return args; 1.72 } 1.73 @@ -391,4 +413,13 @@ 1.74 } 1.75 return newArgs; 1.76 } 1.77 + 1.78 + // package-privates below this. 1.79 + ScriptObject getScriptObject() { 1.80 + return sobj; 1.81 + } 1.82 + 1.83 + static Object translateUndefined(Object obj) { 1.84 + return (obj == ScriptRuntime.UNDEFINED)? null : obj; 1.85 + } 1.86 }
2.1 --- a/src/jdk/nashorn/internal/runtime/Context.java Tue Jun 04 22:31:48 2013 +0530 2.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java Wed Jun 05 12:08:49 2013 +0530 2.3 @@ -48,6 +48,7 @@ 2.4 import java.security.ProtectionDomain; 2.5 import jdk.internal.org.objectweb.asm.ClassReader; 2.6 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter; 2.7 +import jdk.nashorn.api.scripting.ScriptObjectMirror; 2.8 import jdk.nashorn.internal.codegen.Compiler; 2.9 import jdk.nashorn.internal.codegen.ObjectClassGenerator; 2.10 import jdk.nashorn.internal.ir.FunctionNode; 2.11 @@ -518,7 +519,7 @@ 2.12 setGlobalTrusted(newGlobal); 2.13 2.14 try { 2.15 - return load(newGlobal, from); 2.16 + return ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal); 2.17 } finally { 2.18 setGlobalTrusted(oldGlobal); 2.19 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8015945.js Wed Jun 05 12:08:49 2013 +0530 3.3 @@ -0,0 +1,55 @@ 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-8015945: loadWithNewGlobal return value has to be properly wrapped 3.29 + * 3.30 + * @test 3.31 + * @option -scripting 3.32 + * @run 3.33 + */ 3.34 + 3.35 +var global = loadWithNewGlobal({ name: "<code>", 3.36 + script: <<EOF 3.37 + 3.38 +function squares() { 3.39 + var res = new Array(arguments.length); 3.40 + for (var i in arguments) { 3.41 + res[i] = arguments[i]*arguments[i] 3.42 + } 3.43 + return res; 3.44 +} 3.45 + 3.46 +this; 3.47 + 3.48 +EOF 3.49 +}) 3.50 + 3.51 +print("global an Object? " + (global instanceof Object)); 3.52 +var res = global.squares(2, 3, 4, 5); 3.53 +print("global.squares returns Array? " + (res instanceof Array)); 3.54 +// still can access array index properties and length 3.55 +print("result length " + res.length); 3.56 +for (var i in res) { 3.57 + print(i + " = " + res[i]); 3.58 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/script/basic/JDK-8015945.js.EXPECTED Wed Jun 05 12:08:49 2013 +0530 4.3 @@ -0,0 +1,7 @@ 4.4 +global an Object? false 4.5 +global.squares returns Array? false 4.6 +result length 4 4.7 +0 = 4 4.8 +1 = 9 4.9 +2 = 16 4.10 +3 = 25