# HG changeset patch # User sundar # Date 1359376816 -19800 # Node ID 58f6f046bb5e7d07a800e9d7168f458aa2f10e73 # Parent 3f6c0ab2597ae094560ca70cbd38c1fc4f77e418 8006983: Introduce a command line option to switch off syntactic extensions of nashorn Reviewed-by: lagergren, attila diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/src/jdk/nashorn/internal/objects/Global.java --- a/nashorn/src/jdk/nashorn/internal/objects/Global.java Fri Jan 25 17:35:31 2013 +0100 +++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java Mon Jan 28 18:10:16 2013 +0530 @@ -1456,8 +1456,8 @@ 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); diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/src/jdk/nashorn/internal/parser/Parser.java --- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java Fri Jan 25 17:35:31 2013 +0100 +++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java Mon Jan 28 18:10:16 2013 +0530 @@ -147,7 +147,7 @@ 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 @@ // 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 @@ 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 @@ // 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 @@ 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 @@ functionNode.setFirstToken(firstToken); // Nashorn extension: expression closures - if (type != LBRACE) { + if (!context._no_syntax_extensions && type != LBRACE) { /* * Example: * diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/src/jdk/nashorn/internal/runtime/Context.java --- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java Fri Jan 25 17:35:31 2013 +0100 +++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java Mon Jan 28 18:10:16 2013 +0530 @@ -229,6 +229,9 @@ /** 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 @@ 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")) { diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java --- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java Fri Jan 25 17:35:31 2013 +0100 +++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java Mon Jan 28 18:10:16 2013 +0530 @@ -44,8 +44,8 @@ /** 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 @@ * * @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) { diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties --- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties Fri Jan 25 17:35:31 2013 +0100 +++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Options.properties Mon Jan 28 18:10:16 2013 +0530 @@ -172,6 +172,14 @@ 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, \ diff -r 3f6c0ab2597a -r 58f6f046bb5e nashorn/test/script/basic/JDK-8006983.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nashorn/test/script/basic/JDK-8006983.js Mon Jan 28 18:10:16 2013 +0530 @@ -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(<