8012191: noSuchProperty can't cope with vararg functions
Reviewed-by: jlaskey, attila
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Jul 11 18:33:33 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunction.java Thu Jul 11 22:58:37 2013 +0530
@@ -71,6 +71,8 @@
private static final MethodHandle IS_NONSTRICT_FUNCTION = findOwnMH("isNonStrictFunction", boolean.class, Object.class, Object.class, ScriptFunctionData.class);
+ private static final MethodHandle ADD_ZEROTH_ELEMENT = findOwnMH("addZerothElement", Object[].class, Object[].class, Object.class);
+
/** The parent scope. */
private final ScriptObject scope;
@@ -546,7 +548,21 @@
}
private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) {
- return bindName == null ? methodHandle : MH.insertArguments(methodHandle, 1, bindName);
+ if (bindName == null) {
+ return methodHandle;
+ } else {
+ // if it is vararg method, we need to extend argument array with
+ // a new zeroth element that is set to bindName value.
+ final MethodType methodType = methodHandle.type();
+ final int parameterCount = methodType.parameterCount();
+ final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
+
+ if (isVarArg) {
+ return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
+ } else {
+ return MH.insertArguments(methodHandle, 1, bindName);
+ }
+ }
}
/**
@@ -586,6 +602,16 @@
return self instanceof ScriptFunction && ((ScriptFunction)self).data == data && arg instanceof ScriptObject;
}
+ @SuppressWarnings("unused")
+ private static Object[] addZerothElement(final Object[] args, final Object value) {
+ // extends input array with by adding new zeroth element
+ final Object[] src = (args == null)? ScriptRuntime.EMPTY_ARRAY : args;
+ final Object[] result = new Object[src.length + 1];
+ System.arraycopy(src, 0, result, 1, src.length);
+ result[0] = value;
+ return result;
+ }
+
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
final Class<?> own = ScriptFunction.class;
final MethodType mt = MH.type(rtype, types);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8012191.js Thu Jul 11 22:58:37 2013 +0530
@@ -0,0 +1,53 @@
+/*
+ * 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-8012191: noSuchProperty can't cope with vararg functions
+ *
+ * @test
+ * @run
+ */
+
+// ClassCastException: Cannot cast java.lang.String to [Ljava.lang.Object;
+__noSuchProperty__ = function() {
+ print("obj.__noSuchProperty__ invoked for " + arguments[0]);
+}
+
+nonExistent;
+
+// related issue was seen in JSAdapter __get__, __set__ too
+var obj = new JSAdapter() {
+ __put__: function() {
+ print("JSAdapter.__put__");
+ print(arguments[0]);
+ print(arguments[1]);
+ },
+
+ __get__: function() {
+ print("JSAdapter.__get__");
+ print(arguments[0]);
+ }
+};
+
+obj.x = 343;
+obj.y
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8012191.js.EXPECTED Thu Jul 11 22:58:37 2013 +0530
@@ -0,0 +1,6 @@
+obj.__noSuchProperty__ invoked for nonExistent
+JSAdapter.__put__
+x
+343
+JSAdapter.__get__
+y