8006983: Introduce a command line option to switch off syntactic extensions of nashorn

Reviewed-by: lagergren, attila
This commit is contained in:
Athijegannathan Sundararajan 2013-01-28 18:10:16 +05:30
parent 327a6cf1b7
commit daaeb20eff
8 changed files with 138 additions and 37 deletions

View File

@ -1456,8 +1456,8 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);
value = ScriptFunctionImpl.makeFunction("read", ScriptingFunctions.READ);
addOwnProperty("read", Attribute.NOT_ENUMERABLE, value);
value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);
value = ScriptFunctionImpl.makeFunction("quit", ScriptingFunctions.QUIT);
addOwnProperty("quit", Attribute.NOT_ENUMERABLE, value);

View File

@ -147,7 +147,7 @@ public class Parser extends AbstractParser {
public FunctionNode parse(final String scriptName) {
try {
stream = new TokenStream();
lexer = new Lexer(source, stream, context._scripting);
lexer = new Lexer(source, stream, context._scripting && !context._no_syntax_extensions);
// Set up first token (skips opening EOL.)
k = -1;
@ -1065,7 +1065,7 @@ loop:
// Nashorn extension: for each expression.
// iterate property values rather than property names.
if (type == IDENT && "each".equals(getValue())) {
if (!context._no_syntax_extensions && type == IDENT && "each".equals(getValue())) {
forNode.setIsForEach();
next();
}
@ -2312,7 +2312,8 @@ loop:
arguments = new ArrayList<>();
}
// This is to support the following interface impl. syntax:
// Nashorn extension: This is to support the following interface implementation
// syntax:
//
// var r = new java.lang.Runnable() {
// run: function() { println("run"); }
@ -2321,7 +2322,7 @@ loop:
// The object literal following the "new Constructor()" expresssion
// is passed as an additional (last) argument to the constructor.
if (type == LBRACE) {
if (!context._no_syntax_extensions && type == LBRACE) {
arguments.add(objectLiteral());
}
@ -2475,8 +2476,11 @@ loop:
if (type == IDENT || isNonStrictModeIdent()) {
name = getIdent();
verifyStrictIdent(name, "function name");
} else if (isStatement && !context._anon_functions) {
expect(IDENT);
} else if (isStatement) {
// Nashorn extension: anonymous function statements
if (context._no_syntax_extensions || !context._anon_functions) {
expect(IDENT);
}
}
// name is null, generate anonymous name
@ -2613,7 +2617,7 @@ loop:
functionNode.setFirstToken(firstToken);
// Nashorn extension: expression closures
if (type != LBRACE) {
if (!context._no_syntax_extensions && type != LBRACE) {
/*
* Example:
*

View File

@ -229,6 +229,9 @@ public final class Context {
/** Create a new class loaded for each compilation */
public final boolean _loader_per_compile;
/** Do not support non-standard syntax extensions. */
public final boolean _no_syntax_extensions;
/** Package to which generated class files are added */
public final String _package;
@ -341,29 +344,30 @@ public final class Context {
this.out = out;
this.err = err;
_anon_functions = options.getBoolean("anon.functions");
_class_cache_size = options.getInteger("class.cache.size");
_compile_only = options.getBoolean("compile.only");
_debug_lines = options.getBoolean("debug.lines");
_dest_dir = options.getString("d");
_dump_on_error = options.getBoolean("doe");
_early_lvalue_error = options.getBoolean("early.lvalue.error");
_empty_statements = options.getBoolean("empty.statements");
_fullversion = options.getBoolean("fullversion");
_loader_per_compile = options.getBoolean("loader.per.compile");
_package = options.getString("package");
_parse_only = options.getBoolean("parse.only");
_print_ast = options.getBoolean("print.ast");
_print_lower_ast = options.getBoolean("print.lower.ast");
_print_code = options.getBoolean("print.code");
_print_no_newline = options.getBoolean("print.no.newline");
_print_parse = options.getBoolean("print.parse");
_print_lower_parse = options.getBoolean("print.lower.parse");
_print_symbols = options.getBoolean("print.symbols");
_scripting = options.getBoolean("scripting");
_strict = options.getBoolean("strict");
_version = options.getBoolean("version");
_verify_code = options.getBoolean("verify.code");
_anon_functions = options.getBoolean("anon.functions");
_class_cache_size = options.getInteger("class.cache.size");
_compile_only = options.getBoolean("compile.only");
_debug_lines = options.getBoolean("debug.lines");
_dest_dir = options.getString("d");
_dump_on_error = options.getBoolean("doe");
_early_lvalue_error = options.getBoolean("early.lvalue.error");
_empty_statements = options.getBoolean("empty.statements");
_fullversion = options.getBoolean("fullversion");
_loader_per_compile = options.getBoolean("loader.per.compile");
_no_syntax_extensions = options.getBoolean("no.syntax.extensions");
_package = options.getString("package");
_parse_only = options.getBoolean("parse.only");
_print_ast = options.getBoolean("print.ast");
_print_lower_ast = options.getBoolean("print.lower.ast");
_print_code = options.getBoolean("print.code");
_print_no_newline = options.getBoolean("print.no.newline");
_print_parse = options.getBoolean("print.parse");
_print_lower_parse = options.getBoolean("print.lower.parse");
_print_symbols = options.getBoolean("print.symbols");
_scripting = options.getBoolean("scripting");
_strict = options.getBoolean("strict");
_version = options.getBoolean("version");
_verify_code = options.getBoolean("verify.code");
int callSiteFlags = 0;
if (options.getBoolean("profile.callsites")) {

View File

@ -44,8 +44,8 @@ public class ScriptingFunctions {
/** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class);
/** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
public static final MethodHandle READ = findOwnMH("read", Object.class, Object.class, Object.class);
/** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
public static final MethodHandle READFULLY = findOwnMH("readFully", Object.class, Object.class, Object.class);
/** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
public static final MethodHandle QUIT = findOwnMH("quit", Object.class, Object.class, Object.class);
@ -78,7 +78,7 @@ public class ScriptingFunctions {
*
* @throws IOException if an exception occurs
*/
public static Object read(final Object self, final Object file) throws IOException {
public static Object readFully(final Object self, final Object file) throws IOException {
File f = null;
if (file instanceof File) {

View File

@ -172,6 +172,14 @@ nashorn.option.loader.per.compile = { \
default=true \
}
nashorn.option.no.syntax.extensions = { \
name="--no-syntax-extensions", \
short_name="--nse", \
is_undocumented=true, \
desc="No non-standard syntax extensions", \
default=-anon-functions=false \
}
nashorn.option.package = { \
name="--package", \
is_undocumented=true, \

View File

@ -0,0 +1,85 @@
/*
* 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.
*/
/**
* 8006983: Introduce a command line option to switch off syntactic extensions of nashorn
*
* @test
* @option -scripting
* @option --no-syntax-extensions
* @run
*/
try {
eval("var r = new java.lang.Runnable() { run: function(){} }");
fail("should have thrown error for anon-class-style new");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}
try {
eval("var sqr = function(x) x*x ");
fail("should have thrown error for expression closures");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}
try {
eval("function() {};");
fail("should have thrown error for anonymous function statement");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}
try {
eval("for each (i in [22, 33, 33]) { print(i) }");
fail("should have thrown error for-each statement");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}
try {
eval("# shell style comment");
fail("should have thrown error for shell style comment");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}
try {
eval("print(<<EOF);\nhello\nworld\nEOF\n");
fail("should have thrown error heredoc");
} catch (e) {
if (! (e instanceof SyntaxError)) {
fail("SyntaxError expected, got " + e);
}
}

View File

@ -78,4 +78,4 @@ EOD y = "No we don't";
print(y);
print(read(__FILE__));
print(readFully(__FILE__));

View File

@ -99,5 +99,5 @@ EOD y = "No we don't";
print(y);
print(read(__FILE__));
print(readFully(__FILE__));