8026039: future strict names are allowed as function name and argument name of a strict function
authorsundar
Tue, 08 Oct 2013 14:57:31 +0200
changeset 20571 f52b0abf5d88
parent 20570 b1c8d1e8013a
child 20572 7bceef30eefa
8026039: future strict names are allowed as function name and argument name of a strict function Reviewed-by: hannesw, jlaskey
nashorn/src/jdk/nashorn/internal/ir/IdentNode.java
nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/test/script/error/JDK-8026039.js
nashorn/test/script/error/JDK-8026039.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/ir/IdentNode.java	Tue Oct 08 13:11:15 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/ir/IdentNode.java	Tue Oct 08 14:57:31 2013 +0200
@@ -40,9 +40,10 @@
  */
 @Immutable
 public final class IdentNode extends Expression implements PropertyKey, TypeOverride<IdentNode>, FunctionCall {
-    private static final int PROPERTY_NAME    = 1 << 0;
-    private static final int INITIALIZED_HERE = 1 << 1;
-    private static final int FUNCTION         = 1 << 2;
+    private static final int PROPERTY_NAME     = 1 << 0;
+    private static final int INITIALIZED_HERE  = 1 << 1;
+    private static final int FUNCTION          = 1 << 2;
+    private static final int FUTURESTRICT_NAME = 1 << 3;
 
     /** Identifier. */
     private final String name;
@@ -197,6 +198,25 @@
     }
 
     /**
+     * Check if this IdentNode is a future strict name
+     * @return true if this is a future strict name
+     */
+    public boolean isFutureStrictName() {
+        return (flags & FUTURESTRICT_NAME) != 0;
+    }
+
+    /**
+     * Flag this IdentNode as a future strict name
+     * @return a node equivalent to this one except for the requested change.
+     */
+    public IdentNode setIsFutureStrictName() {
+        if (isFutureStrictName()) {
+            return this;
+        }
+        return new IdentNode(this, name, callSiteType, flags | FUTURESTRICT_NAME);
+    }
+
+    /**
      * Helper function for local def analysis.
      * @return true if IdentNode is initialized on creation
      */
--- a/nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java	Tue Oct 08 13:11:15 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java	Tue Oct 08 14:57:31 2013 +0200
@@ -378,7 +378,7 @@
             next();
 
             // Create IDENT node.
-            return new IdentNode(identToken, finish, ident);
+            return new IdentNode(identToken, finish, ident).setIsFutureStrictName();
         }
 
         // Get IDENT.
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Tue Oct 08 13:11:15 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Tue Oct 08 14:57:31 2013 +0200
@@ -909,6 +909,10 @@
             default:
                 break;
             }
+
+            if (ident.isFutureStrictName()) {
+                throw error(AbstractParser.message("strict.name", ident.getName(), contextString), ident.getToken());
+            }
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/error/JDK-8026039.js	Tue Oct 08 14:57:31 2013 +0200
@@ -0,0 +1,32 @@
+/*
+ * 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-8026039: future strict names are allowed as function name and argument name of a strict function
+ *
+ * @test/compile-error
+ */
+
+function public() {"use strict"}
+
+function f(public) {"use strict"} 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/error/JDK-8026039.js.EXPECTED	Tue Oct 08 14:57:31 2013 +0200
@@ -0,0 +1,9 @@
+test/script/error/JDK-8026039.js:30:9 "public" cannot be used as function name in strict mode
+function public() {"use strict"}
+         ^
+test/script/error/JDK-8026039.js:32:11 Expected ident but found public
+function f(public) {"use strict"} 
+           ^
+test/script/error/JDK-8026039.js:33:0 Expected } but found eof
+
+^