diff --git a/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java b/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java index f83cfa2c954eb0fa1f5c4b3ebebe6b3400209f4c..1e899be580596e9cf6927ffd5ab877ff658240b1 100644 --- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java +++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptFunctionData.java @@ -25,14 +25,13 @@ package jdk.nashorn.internal.runtime; +import static jdk.nashorn.internal.lookup.Lookup.MH; import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; -import static jdk.nashorn.internal.lookup.Lookup.MH; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; - import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory; /** @@ -389,7 +388,9 @@ public abstract class ScriptFunctionData { boundInvoker = noArgBoundInvoker; } } else { - final Object[] boundArgs = new Object[Math.min(originalInvoker.type().parameterCount(), args.length + (isTargetBound ? 0 : (needsCallee ? 2 : 1)))]; + // If target is already bound, insert additional bound arguments after "this" argument, at position 1. + final int argInsertPos = isTargetBound ? 1 : 0; + final Object[] boundArgs = new Object[Math.min(originalInvoker.type().parameterCount() - argInsertPos, args.length + (isTargetBound ? 0 : (needsCallee ? 2 : 1)))]; int next = 0; if (!isTargetBound) { if (needsCallee) { @@ -403,7 +404,7 @@ public abstract class ScriptFunctionData { // "this" will get dropped anyway by the target invoker. We previously asserted that already bound functions // don't take a callee parameter, so we can know that the signature is (this[, args...]) therefore args // start at position 1. If the function is not bound, we start inserting arguments at position 0. - boundInvoker = MH.insertArguments(originalInvoker, isTargetBound ? 1 : 0, boundArgs); + boundInvoker = MH.insertArguments(originalInvoker, argInsertPos, boundArgs); } if (isTargetBound) { diff --git a/nashorn/test/script/basic/JDK-8011718.js b/nashorn/test/script/basic/JDK-8011718.js new file mode 100644 index 0000000000000000000000000000000000000000..7cb24f24f1092e6e731b0042c44f742065984bd7 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8011718.js @@ -0,0 +1,46 @@ +/* + * 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-8011718: binding already bound function with extra arguments fails. + * + * @test + * @run + */ + +var obj = { + hello:"From obj", +}; +var obj2 = { + hello:"From obj2", +}; + +function doit(cb){ + cb(); + var cb2 = cb.bind(obj2, "This one is not acccepted"); + cb2(); +} + +doit(function(){ + print(this.hello); + }.bind(obj)); diff --git a/nashorn/test/script/basic/JDK-8011718.js.EXPECTED b/nashorn/test/script/basic/JDK-8011718.js.EXPECTED new file mode 100644 index 0000000000000000000000000000000000000000..58a7d55629a8c7d5561c8179379fb05d7edc7906 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8011718.js.EXPECTED @@ -0,0 +1,2 @@ +From obj +From obj