提交 feb34a66 编写于 作者: S sundar

8027933: Add --const-as-var option

Reviewed-by: jlaskey, hannesw
上级 a7a5b7fb
......@@ -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;
......
......@@ -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"),
......
......@@ -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");
......
......@@ -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", \
......
......@@ -26,6 +26,7 @@
* Ensure that all parseable files can be parsed using parser API.
*
* @test
* @option --const-as-var
* @option -scripting
* @run
*/
......
/*
* 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
})())
/*
* 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;
test/script/error/JDK-8027933.js:31:0 Expected an operand but found const
const THE_ANSWER = 42;
^
......@@ -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
......
......@@ -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());
......
......@@ -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");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册