changeset 24279:33b0fbd03872

8027933: Add --const-as-var option Reviewed-by: jlaskey, hannesw
author sundar
date Fri, 02 May 2014 19:15:59 +0530
parents 0d1f816217dc
children 4420eb3f86a4
files nashorn/src/jdk/nashorn/internal/parser/Parser.java nashorn/src/jdk/nashorn/internal/parser/TokenType.java nashorn/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties nashorn/test/script/basic/JDK-8008448.js nashorn/test/script/basic/JDK-8027933.js nashorn/test/script/basic/JDK-8027933.js.EXPECTED nashorn/test/script/error/JDK-8027933.js nashorn/test/script/error/JDK-8027933.js.EXPECTED nashorn/test/src/jdk/nashorn/internal/codegen/CompilerTest.java nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java nashorn/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java
diffstat 12 files changed, 116 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Fri May 02 19:15:59 2014 +0530
@@ -33,6 +33,7 @@
 import static jdk.nashorn.internal.parser.TokenType.CATCH;
 import static jdk.nashorn.internal.parser.TokenType.COLON;
 import static jdk.nashorn.internal.parser.TokenType.COMMARIGHT;
+import static jdk.nashorn.internal.parser.TokenType.CONST;
 import static jdk.nashorn.internal.parser.TokenType.DECPOSTFIX;
 import static jdk.nashorn.internal.parser.TokenType.DECPREFIX;
 import static jdk.nashorn.internal.parser.TokenType.ELSE;
@@ -849,6 +850,11 @@
             expect(SEMICOLON);
             break;
         default:
+            if (env._const_as_var && type == CONST) {
+                variableStatement(true);
+                break;
+            }
+
             if (type == IDENT || isNonStrictModeIdent()) {
                 if (T(k + 1) == COLON) {
                     labelStatement();
@@ -1110,6 +1116,12 @@
             case SEMICOLON:
                 break;
             default:
+                if (env._const_as_var && type == CONST) {
+                    // Var statements captured in for outer block.
+                    vars = variableStatement(false);
+                    break;
+                }
+
                 final Expression expression = expression(unaryExpression(), COMMARIGHT.getPrecedence(), true);
                 forNode = forNode.setInit(lc, expression);
                 break;
--- a/nashorn/src/jdk/nashorn/internal/parser/TokenType.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/parser/TokenType.java	Fri May 02 19:15:59 2014 +0530
@@ -111,7 +111,7 @@
     CATCH          (KEYWORD,  "catch"),
 //  CHAR           (FUTURE,   "char"),
     CLASS          (FUTURE,   "class"),
-    CONST          (FUTURE,  "const"),
+    CONST          (KEYWORD,  "const"),
     CONTINUE       (KEYWORD,  "continue"),
     DEBUGGER       (KEYWORD,  "debugger"),
     DEFAULT        (KEYWORD,  "default"),
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java	Fri May 02 19:15:59 2014 +0530
@@ -62,6 +62,9 @@
     /** Only compile script, do not run it or generate other ScriptObjects */
     public final boolean _compile_only;
 
+    /** Accept "const" keyword and treat it as variable. Interim feature */
+    public final boolean _const_as_var;
+
     /** Accumulated callsite flags that will be used when bootstrapping script callsites */
     public final int     _callsite_flags;
 
@@ -200,6 +203,7 @@
 
         _class_cache_size     = options.getInteger("class.cache.size");
         _compile_only         = options.getBoolean("compile.only");
+        _const_as_var         = options.getBoolean("const.as.var");
         _debug_lines          = options.getBoolean("debug.lines");
         _dest_dir             = options.getString("d");
         _dump_on_error        = options.getBoolean("doe");
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties	Fri May 02 19:15:59 2014 +0530
@@ -102,6 +102,13 @@
     type=Boolean                      \
 }
 
+nashorn.option.const.as.var = {          \
+    name="--const-as-var",               \
+    is_undocumented=true,                \
+    desc="Replace 'const' with 'var'.",  \
+    type=Boolean                         \
+}
+
 nashorn.option.d = {                                             \
     name="--dump-debug-dir",                                     \
     short_name="-d",                                             \
--- a/nashorn/test/script/basic/JDK-8008448.js	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/test/script/basic/JDK-8008448.js	Fri May 02 19:15:59 2014 +0530
@@ -26,6 +26,7 @@
  * Ensure that all parseable files can be parsed using parser API.
  *
  * @test
+ * @option --const-as-var
  * @option -scripting
  * @run
  */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8027933.js	Fri May 02 19:15:59 2014 +0530
@@ -0,0 +1,38 @@
+/*
+ * 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-8027933: Add const.as.var option
+ *
+ * @test
+ * @option --const-as-var
+ * @run
+ */
+
+const THE_ANSWER = 42;
+print("Answer to all questions: " + THE_ANSWER);
+
+print((function () {
+    const FORTY_TWO = 42;
+    return FORTY_TWO
+})())
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8027933.js.EXPECTED	Fri May 02 19:15:59 2014 +0530
@@ -0,0 +1,2 @@
+Answer to all questions: 42
+42
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/error/JDK-8027933.js	Fri May 02 19:15:59 2014 +0530
@@ -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-8027933: Add const.as.var option
+ *
+ * @test/compile-error
+ */
+
+// without --const-as-var the following should fail to compile
+const THE_ANSWER = 42;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/error/JDK-8027933.js.EXPECTED	Fri May 02 19:15:59 2014 +0530
@@ -0,0 +1,3 @@
+test/script/error/JDK-8027933.js:31:0 Expected an operand but found const
+const THE_ANSWER = 42;
+^
--- a/nashorn/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/codegen/CompilerTest.java	Fri May 02 19:15:59 2014 +0530
@@ -71,6 +71,7 @@
         options.set("print.ast", true);
         options.set("print.parse", true);
         options.set("scripting", true);
+        options.set("const.as.var", true);
 
         final ErrorManager errors = new ErrorManager() {
             @Override
--- a/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Fri May 02 19:15:59 2014 +0530
@@ -65,6 +65,7 @@
         options.set("anon.functions", true);
         options.set("parse.only", true);
         options.set("scripting", true);
+        options.set("const.as.var", true);
 
         ErrorManager errors = new ErrorManager();
         this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader());
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java	Wed Jul 05 19:39:35 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java	Fri May 02 19:15:59 2014 +0530
@@ -220,4 +220,19 @@
         // bar should be visible in default context
         assertTrue(e.eval("typeof bar").equals("function"));
     }
+
+
+    @Test public void nashornSwallowsConstKeyword() throws Exception {
+        final NashornScriptEngineFactory f = new NashornScriptEngineFactory();
+        final String[] args = new String[] { "--const-as-var" };
+        final ScriptEngine engine = f.getScriptEngine(args);
+
+        final Object ret = engine.eval(""
+            + "(function() {\n"
+            + "  const x = 10;\n"
+            + "  return x;\n"
+            + "})();"
+        );
+        assertEquals(ret, 10, "Parsed and executed OK");
+    }
 }