changeset 1148:0972880cbb97

8066221: anonymous function statement name clashes with another symbol Reviewed-by: lagergren, sundar
author attila
date Wed, 10 Dec 2014 18:28:41 +0100
parents 8cb808c0db80
children fef78bb8752b
files src/jdk/nashorn/internal/parser/Parser.java test/script/basic/JDK-8066221.js
diffstat 2 files changed, 55 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/parser/Parser.java	Wed Dec 10 12:30:48 2014 +0100
+++ b/src/jdk/nashorn/internal/parser/Parser.java	Wed Dec 10 18:28:41 2014 +0100
@@ -2644,7 +2644,7 @@
         // name is null, generate anonymous name
         boolean isAnonymous = false;
         if (name == null) {
-            final String tmpName = getDefaultValidFunctionName(functionLine);
+            final String tmpName = getDefaultValidFunctionName(functionLine, isStatement);
             name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
             isAnonymous = true;
         }
@@ -2653,7 +2653,15 @@
         final List<IdentNode> parameters = formalParameterList();
         expect(RPAREN);
 
-        FunctionNode functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL, functionLine);
+        FunctionNode functionNode;
+        // Hide the current default name across function boundaries. E.g. "x3 = function x1() { function() {}}"
+        // If we didn't hide the current default name, then the innermost anonymous function would receive "x3".
+        hideDefaultName();
+        try {
+            functionNode = functionBody(functionToken, name, parameters, FunctionNode.Kind.NORMAL, functionLine);
+        } finally {
+            defaultNames.pop();
+        }
 
         if (isStatement) {
             if (topLevel || useBlockScope()) {
@@ -2722,9 +2730,17 @@
         return functionNode;
     }
 
-    private String getDefaultValidFunctionName(final int functionLine) {
+    private String getDefaultValidFunctionName(final int functionLine, final boolean isStatement) {
         final String defaultFunctionName = getDefaultFunctionName();
-        return isValidIdentifier(defaultFunctionName) ? defaultFunctionName : ANON_FUNCTION_PREFIX.symbolName() + functionLine;
+        if (isValidIdentifier(defaultFunctionName)) {
+            if (isStatement) {
+                // The name will be used as the LHS of a symbol assignment. We add the anonymous function
+                // prefix to ensure that it can't clash with another variable.
+                return ANON_FUNCTION_PREFIX.symbolName() + defaultFunctionName;
+            }
+            return defaultFunctionName;
+        }
+        return ANON_FUNCTION_PREFIX.symbolName() + functionLine;
     }
 
     private static boolean isValidIdentifier(final String name) {
@@ -2758,6 +2774,10 @@
 
     private void markDefaultNameUsed() {
         defaultNames.pop();
+        hideDefaultName();
+    }
+
+    private void hideDefaultName() {
         // Can be any value as long as getDefaultFunctionName doesn't recognize it as something it can extract a value
         // from. Can't be null
         defaultNames.push("");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8066221.js	Wed Dec 10 18:28:41 2014 +0100
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2014 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-8066221: anonymous function statement name clashes with another symbol
+ * (compile-only test)
+ *
+ * @test
+ */
+
+x3 = function x1(x3) { function (){} };