From feb34a66ca3da1f0cc13960c8f7860c7aa592fa0 Mon Sep 17 00:00:00 2001 From: sundar Date: Fri, 2 May 2014 19:15:59 +0530 Subject: [PATCH] 8027933: Add --const-as-var option Reviewed-by: jlaskey, hannesw --- src/jdk/nashorn/internal/parser/Parser.java | 12 ++++++ .../nashorn/internal/parser/TokenType.java | 2 +- .../internal/runtime/ScriptEnvironment.java | 4 ++ .../runtime/resources/Options.properties | 7 ++++ test/script/basic/JDK-8008448.js | 1 + test/script/basic/JDK-8027933.js | 38 +++++++++++++++++++ test/script/basic/JDK-8027933.js.EXPECTED | 2 + test/script/error/JDK-8027933.js | 31 +++++++++++++++ test/script/error/JDK-8027933.js.EXPECTED | 3 ++ .../internal/codegen/CompilerTest.java | 1 + .../nashorn/internal/parser/ParserTest.java | 1 + .../runtime/TrustedScriptEngineTest.java | 15 ++++++++ 12 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 test/script/basic/JDK-8027933.js create mode 100644 test/script/basic/JDK-8027933.js.EXPECTED create mode 100644 test/script/error/JDK-8027933.js create mode 100644 test/script/error/JDK-8027933.js.EXPECTED diff --git a/src/jdk/nashorn/internal/parser/Parser.java b/src/jdk/nashorn/internal/parser/Parser.java index 08ff725a..6ad60b58 100644 --- a/src/jdk/nashorn/internal/parser/Parser.java +++ b/src/jdk/nashorn/internal/parser/Parser.java @@ -33,6 +33,7 @@ import static jdk.nashorn.internal.parser.TokenType.CASE; 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 @@ loop: 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 @@ loop: 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; diff --git a/src/jdk/nashorn/internal/parser/TokenType.java b/src/jdk/nashorn/internal/parser/TokenType.java index 5c696cb9..59005077 100644 --- a/src/jdk/nashorn/internal/parser/TokenType.java +++ b/src/jdk/nashorn/internal/parser/TokenType.java @@ -111,7 +111,7 @@ public enum TokenType { CATCH (KEYWORD, "catch"), // CHAR (FUTURE, "char"), CLASS (FUTURE, "class"), - CONST (FUTURE, "const"), + CONST (KEYWORD, "const"), CONTINUE (KEYWORD, "continue"), DEBUGGER (KEYWORD, "debugger"), DEFAULT (KEYWORD, "default"), diff --git a/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java b/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java index 96982011..445106d7 100644 --- a/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java +++ b/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java @@ -62,6 +62,9 @@ public final class ScriptEnvironment { /** 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 @@ public final class ScriptEnvironment { _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"); diff --git a/src/jdk/nashorn/internal/runtime/resources/Options.properties b/src/jdk/nashorn/internal/runtime/resources/Options.properties index 033fcc25..9262a401 100644 --- a/src/jdk/nashorn/internal/runtime/resources/Options.properties +++ b/src/jdk/nashorn/internal/runtime/resources/Options.properties @@ -102,6 +102,13 @@ nashorn.option.compile.only = { \ 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", \ diff --git a/test/script/basic/JDK-8008448.js b/test/script/basic/JDK-8008448.js index b30e3417..240efd61 100644 --- a/test/script/basic/JDK-8008448.js +++ b/test/script/basic/JDK-8008448.js @@ -26,6 +26,7 @@ * Ensure that all parseable files can be parsed using parser API. * * @test + * @option --const-as-var * @option -scripting * @run */ diff --git a/test/script/basic/JDK-8027933.js b/test/script/basic/JDK-8027933.js new file mode 100644 index 00000000..506abc23 --- /dev/null +++ b/test/script/basic/JDK-8027933.js @@ -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 +})()) diff --git a/test/script/basic/JDK-8027933.js.EXPECTED b/test/script/basic/JDK-8027933.js.EXPECTED new file mode 100644 index 00000000..cb920ab6 --- /dev/null +++ b/test/script/basic/JDK-8027933.js.EXPECTED @@ -0,0 +1,2 @@ +Answer to all questions: 42 +42 diff --git a/test/script/error/JDK-8027933.js b/test/script/error/JDK-8027933.js new file mode 100644 index 00000000..9b398fa5 --- /dev/null +++ b/test/script/error/JDK-8027933.js @@ -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; diff --git a/test/script/error/JDK-8027933.js.EXPECTED b/test/script/error/JDK-8027933.js.EXPECTED new file mode 100644 index 00000000..9389a5fc --- /dev/null +++ b/test/script/error/JDK-8027933.js.EXPECTED @@ -0,0 +1,3 @@ +test/script/error/JDK-8027933.js:31:0 Expected an operand but found const +const THE_ANSWER = 42; +^ diff --git a/test/src/jdk/nashorn/internal/codegen/CompilerTest.java b/test/src/jdk/nashorn/internal/codegen/CompilerTest.java index ac438101..14a9cb9c 100644 --- a/test/src/jdk/nashorn/internal/codegen/CompilerTest.java +++ b/test/src/jdk/nashorn/internal/codegen/CompilerTest.java @@ -69,6 +69,7 @@ public class CompilerTest { 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 diff --git a/test/src/jdk/nashorn/internal/parser/ParserTest.java b/test/src/jdk/nashorn/internal/parser/ParserTest.java index 3e10a89e..e47ccee3 100644 --- a/test/src/jdk/nashorn/internal/parser/ParserTest.java +++ b/test/src/jdk/nashorn/internal/parser/ParserTest.java @@ -62,6 +62,7 @@ public class ParserTest { 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()); diff --git a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java index 469d890e..f6da6f82 100644 --- a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java +++ b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java @@ -220,4 +220,19 @@ public class TrustedScriptEngineTest { // 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"); + } } -- GitLab