Wed, 10 Dec 2014 18:28:41 +0100
8066221: anonymous function statement name clashes with another symbol
Reviewed-by: lagergren, sundar
src/jdk/nashorn/internal/parser/Parser.java | file | annotate | diff | comparison | revisions | |
test/script/basic/JDK-8066221.js | file | annotate | diff | comparison | revisions |
1.1 --- a/src/jdk/nashorn/internal/parser/Parser.java Wed Dec 10 12:30:48 2014 +0100 1.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java Wed Dec 10 18:28:41 2014 +0100 1.3 @@ -2644,7 +2644,7 @@ 1.4 // name is null, generate anonymous name 1.5 boolean isAnonymous = false; 1.6 if (name == null) { 1.7 - final String tmpName = getDefaultValidFunctionName(functionLine); 1.8 + final String tmpName = getDefaultValidFunctionName(functionLine, isStatement); 1.9 name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName); 1.10 isAnonymous = true; 1.11 } 1.12 @@ -2653,7 +2653,15 @@ 1.13 final List<IdentNode> parameters = formalParameterList(); 1.14 expect(RPAREN); 1.15 1.16 - FunctionNode functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL, functionLine); 1.17 + FunctionNode functionNode; 1.18 + // Hide the current default name across function boundaries. E.g. "x3 = function x1() { function() {}}" 1.19 + // If we didn't hide the current default name, then the innermost anonymous function would receive "x3". 1.20 + hideDefaultName(); 1.21 + try { 1.22 + functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL, functionLine); 1.23 + } finally { 1.24 + defaultNames.pop(); 1.25 + } 1.26 1.27 if (isStatement) { 1.28 if (topLevel || useBlockScope()) { 1.29 @@ -2722,9 +2730,17 @@ 1.30 return functionNode; 1.31 } 1.32 1.33 - private String getDefaultValidFunctionName(final int functionLine) { 1.34 + private String getDefaultValidFunctionName(final int functionLine, final boolean isStatement) { 1.35 final String defaultFunctionName = getDefaultFunctionName(); 1.36 - return isValidIdentifier(defaultFunctionName) ? defaultFunctionName : ANON_FUNCTION_PREFIX.symbolName() + functionLine; 1.37 + if (isValidIdentifier(defaultFunctionName)) { 1.38 + if (isStatement) { 1.39 + // The name will be used as the LHS of a symbol assignment. We add the anonymous function 1.40 + // prefix to ensure that it can't clash with another variable. 1.41 + return ANON_FUNCTION_PREFIX.symbolName() + defaultFunctionName; 1.42 + } 1.43 + return defaultFunctionName; 1.44 + } 1.45 + return ANON_FUNCTION_PREFIX.symbolName() + functionLine; 1.46 } 1.47 1.48 private static boolean isValidIdentifier(final String name) { 1.49 @@ -2758,6 +2774,10 @@ 1.50 1.51 private void markDefaultNameUsed() { 1.52 defaultNames.pop(); 1.53 + hideDefaultName(); 1.54 + } 1.55 + 1.56 + private void hideDefaultName() { 1.57 // Can be any value as long as getDefaultFunctionName doesn't recognize it as something it can extract a value 1.58 // from. Can't be null 1.59 defaultNames.push("");
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/script/basic/JDK-8066221.js Wed Dec 10 18:28:41 2014 +0100 2.3 @@ -0,0 +1,31 @@ 2.4 +/* 2.5 + * Copyright (c) 2014 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-8066221: anonymous function statement name clashes with another symbol 2.29 + * (compile-only test) 2.30 + * 2.31 + * @test 2.32 + */ 2.33 + 2.34 +x3 = function x1(x3) { function (){} };