Thu, 11 Jul 2013 22:58:37 +0530
8012191: noSuchProperty can't cope with vararg functions
Reviewed-by: jlaskey, attila
1.1 --- a/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Jul 11 18:33:33 2013 +0200 1.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Jul 11 22:58:37 2013 +0530 1.3 @@ -71,6 +71,8 @@ 1.4 1.5 private static final MethodHandle IS_NONSTRICT_FUNCTION = findOwnMH("isNonStrictFunction", boolean.class, Object.class, Object.class, ScriptFunctionData.class); 1.6 1.7 + private static final MethodHandle ADD_ZEROTH_ELEMENT = findOwnMH("addZerothElement", Object[].class, Object[].class, Object.class); 1.8 + 1.9 /** The parent scope. */ 1.10 private final ScriptObject scope; 1.11 1.12 @@ -546,7 +548,21 @@ 1.13 } 1.14 1.15 private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) { 1.16 - return bindName == null ? methodHandle : MH.insertArguments(methodHandle, 1, bindName); 1.17 + if (bindName == null) { 1.18 + return methodHandle; 1.19 + } else { 1.20 + // if it is vararg method, we need to extend argument array with 1.21 + // a new zeroth element that is set to bindName value. 1.22 + final MethodType methodType = methodHandle.type(); 1.23 + final int parameterCount = methodType.parameterCount(); 1.24 + final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray(); 1.25 + 1.26 + if (isVarArg) { 1.27 + return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName)); 1.28 + } else { 1.29 + return MH.insertArguments(methodHandle, 1, bindName); 1.30 + } 1.31 + } 1.32 } 1.33 1.34 /** 1.35 @@ -586,6 +602,16 @@ 1.36 return self instanceof ScriptFunction && ((ScriptFunction)self).data == data && arg instanceof ScriptObject; 1.37 } 1.38 1.39 + @SuppressWarnings("unused") 1.40 + private static Object[] addZerothElement(final Object[] args, final Object value) { 1.41 + // extends input array with by adding new zeroth element 1.42 + final Object[] src = (args == null)? ScriptRuntime.EMPTY_ARRAY : args; 1.43 + final Object[] result = new Object[src.length + 1]; 1.44 + System.arraycopy(src, 0, result, 1, src.length); 1.45 + result[0] = value; 1.46 + return result; 1.47 + } 1.48 + 1.49 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 1.50 final Class<?> own = ScriptFunction.class; 1.51 final MethodType mt = MH.type(rtype, types);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/script/basic/JDK-8012191.js Thu Jul 11 22:58:37 2013 +0530 2.3 @@ -0,0 +1,53 @@ 2.4 +/* 2.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/** 2.28 + * JDK-8012191: noSuchProperty can't cope with vararg functions 2.29 + * 2.30 + * @test 2.31 + * @run 2.32 + */ 2.33 + 2.34 +// ClassCastException: Cannot cast java.lang.String to [Ljava.lang.Object; 2.35 +__noSuchProperty__ = function() { 2.36 + print("obj.__noSuchProperty__ invoked for " + arguments[0]); 2.37 +} 2.38 + 2.39 +nonExistent; 2.40 + 2.41 +// related issue was seen in JSAdapter __get__, __set__ too 2.42 +var obj = new JSAdapter() { 2.43 + __put__: function() { 2.44 + print("JSAdapter.__put__"); 2.45 + print(arguments[0]); 2.46 + print(arguments[1]); 2.47 + }, 2.48 + 2.49 + __get__: function() { 2.50 + print("JSAdapter.__get__"); 2.51 + print(arguments[0]); 2.52 + } 2.53 +}; 2.54 + 2.55 +obj.x = 343; 2.56 +obj.y
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8012191.js.EXPECTED Thu Jul 11 22:58:37 2013 +0530 3.3 @@ -0,0 +1,6 @@ 3.4 +obj.__noSuchProperty__ invoked for nonExistent 3.5 +JSAdapter.__put__ 3.6 +x 3.7 +343 3.8 +JSAdapter.__get__ 3.9 +y