From eb2b5771d2d89346f8e4e152ab14dd104c3b168b Mon Sep 17 00:00:00 2001 From: Athijegannathan Sundararajan Date: Mon, 31 Aug 2015 17:51:02 +0530 Subject: [PATCH] 8134731: Function.prototype.apply interacts incorrectly with arguments Reviewed-by: attila, hannesw --- .../internal/codegen/ApplySpecialization.java | 16 +++---- .../jdk/nashorn/internal/ir/FunctionNode.java | 8 ++++ nashorn/test/script/basic/JDK-8134731.js | 46 +++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 nashorn/test/script/basic/JDK-8134731.js diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java index 62e58f46767..e7bd4f65570 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java @@ -283,17 +283,13 @@ public final class ApplySpecialization extends NodeVisitor imple start++; } - start++; //we always uses this + start++; // we always use this - final List params = functionNode.getParameters(); + assert functionNode.getNumOfParams() == 0 : "apply2call on function with named paramaters!"; final List newParams = new ArrayList<>(); - final long to = Math.max(params.size(), actualCallSiteType.parameterCount() - start); + final long to = actualCallSiteType.parameterCount() - start; for (int i = 0; i < to; i++) { - if (i >= params.size()) { - newParams.add(new IdentNode(functionNode.getToken(), functionNode.getFinish(), EXPLODED_ARGUMENT_PREFIX.symbolName() + (i))); - } else { - newParams.add(params.get(i)); - } + newParams.add(new IdentNode(functionNode.getToken(), functionNode.getFinish(), EXPLODED_ARGUMENT_PREFIX.symbolName() + (i))); } callSiteTypes.push(actualCallSiteType); @@ -316,6 +312,10 @@ public final class ApplySpecialization extends NodeVisitor imple return false; } + if (functionNode.getNumOfParams() != 0) { + return false; + } + if (functionNode.hasEval()) { return false; } diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/FunctionNode.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/FunctionNode.java index 783043f520f..a00f8241de8 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/FunctionNode.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/FunctionNode.java @@ -1025,6 +1025,14 @@ public final class FunctionNode extends LexicalContextExpression implements Flag return Collections.unmodifiableList(parameters); } + /** + * Return the number of parameters to this function + * @return the number of parameters + */ + public int getNumOfParams() { + return parameters.size(); + } + /** * Returns the identifier for a named parameter at the specified position in this function's parameter list. * @param index the parameter's position. diff --git a/nashorn/test/script/basic/JDK-8134731.js b/nashorn/test/script/basic/JDK-8134731.js new file mode 100644 index 00000000000..e5cd3397928 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8134731.js @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015, 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-8134731: `Function.prototype.apply` interacts incorrectly with `arguments` + * + * @test + * @run + */ + +function func() { + return (function(f){ + return function(a1, a2, a3, a4){ + return (f.apply(this, arguments)); + } + })(function(){ + return arguments.length; + }) +} + +Assert.assertTrue(func()() == 0); +Assert.assertTrue(func()(33) == 1); +Assert.assertTrue(func()(33, true) == 2); +Assert.assertTrue(func()(33, true, "hello") == 3); +Assert.assertTrue(func()(33, true, "hello", "world") == 4); +Assert.assertTrue(func()(33, true, "hello", "world", 42) == 5);