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

Mon, 28 Jan 2013 18:10:16 +0530

author
sundar
date
Mon, 28 Jan 2013 18:10:16 +0530
changeset 52
8f7a86f82376
parent 51
f52d7294536f
child 53
265c46dbcf43

8006983: Introduce a command line option to switch off syntactic extensions of nashorn
Reviewed-by: lagergren, attila

src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/parser/Parser.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/ScriptingFunctions.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/resources/Options.properties file | annotate | diff | comparison | revisions
test/script/basic/JDK-8006983.js file | annotate | diff | comparison | revisions
test/script/basic/scripting.js file | annotate | diff | comparison | revisions
test/script/basic/scripting.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Fri Jan 25 17:35:31 2013 +0100
     1.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Mon Jan 28 18:10:16 2013 +0530
     1.3 @@ -1456,8 +1456,8 @@
     1.4          value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
     1.5          addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);
     1.6  
     1.7 -        value = ScriptFunctionImpl.makeFunction("read", ScriptingFunctions.READ);
     1.8 -        addOwnProperty("read", Attribute.NOT_ENUMERABLE, value);
     1.9 +        value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    1.10 +        addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);
    1.11  
    1.12          value = ScriptFunctionImpl.makeFunction("quit", ScriptingFunctions.QUIT);
    1.13          addOwnProperty("quit", Attribute.NOT_ENUMERABLE, value);
     2.1 --- a/src/jdk/nashorn/internal/parser/Parser.java	Fri Jan 25 17:35:31 2013 +0100
     2.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java	Mon Jan 28 18:10:16 2013 +0530
     2.3 @@ -147,7 +147,7 @@
     2.4      public FunctionNode parse(final String scriptName) {
     2.5          try {
     2.6              stream = new TokenStream();
     2.7 -            lexer  = new Lexer(source, stream, context._scripting);
     2.8 +            lexer  = new Lexer(source, stream, context._scripting && !context._no_syntax_extensions);
     2.9  
    2.10              // Set up first token (skips opening EOL.)
    2.11              k = -1;
    2.12 @@ -1065,7 +1065,7 @@
    2.13  
    2.14              // Nashorn extension: for each expression.
    2.15              // iterate property values rather than property names.
    2.16 -            if (type == IDENT && "each".equals(getValue())) {
    2.17 +            if (!context._no_syntax_extensions && type == IDENT && "each".equals(getValue())) {
    2.18                  forNode.setIsForEach();
    2.19                  next();
    2.20              }
    2.21 @@ -2312,7 +2312,8 @@
    2.22              arguments = new ArrayList<>();
    2.23          }
    2.24  
    2.25 -        // This is to support the following interface impl. syntax:
    2.26 +        // Nashorn extension: This is to support the following interface implementation
    2.27 +        // syntax:
    2.28          //
    2.29          //     var r = new java.lang.Runnable() {
    2.30          //         run: function() { println("run"); }
    2.31 @@ -2321,7 +2322,7 @@
    2.32          // The object literal following the "new Constructor()" expresssion
    2.33          // is passed as an additional (last) argument to the constructor.
    2.34  
    2.35 -        if (type == LBRACE) {
    2.36 +        if (!context._no_syntax_extensions && type == LBRACE) {
    2.37              arguments.add(objectLiteral());
    2.38          }
    2.39  
    2.40 @@ -2475,8 +2476,11 @@
    2.41          if (type == IDENT || isNonStrictModeIdent()) {
    2.42              name = getIdent();
    2.43              verifyStrictIdent(name, "function name");
    2.44 -        } else if (isStatement && !context._anon_functions) {
    2.45 -            expect(IDENT);
    2.46 +        } else if (isStatement) {
    2.47 +            // Nashorn extension: anonymous function statements
    2.48 +            if (context._no_syntax_extensions || !context._anon_functions) {
    2.49 +                expect(IDENT);
    2.50 +            }
    2.51          }
    2.52  
    2.53          // name is null, generate anonymous name
    2.54 @@ -2613,7 +2617,7 @@
    2.55              functionNode.setFirstToken(firstToken);
    2.56  
    2.57              // Nashorn extension: expression closures
    2.58 -            if (type != LBRACE) {
    2.59 +            if (!context._no_syntax_extensions && type != LBRACE) {
    2.60                  /*
    2.61                   * Example:
    2.62                   *
     3.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Fri Jan 25 17:35:31 2013 +0100
     3.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Jan 28 18:10:16 2013 +0530
     3.3 @@ -229,6 +229,9 @@
     3.4      /** Create a new class loaded for each compilation */
     3.5      public final boolean _loader_per_compile;
     3.6  
     3.7 +    /** Do not support non-standard syntax extensions. */
     3.8 +    public final boolean _no_syntax_extensions;
     3.9 +
    3.10      /** Package to which generated class files are added */
    3.11      public final String  _package;
    3.12  
    3.13 @@ -341,29 +344,30 @@
    3.14          this.out       = out;
    3.15          this.err       = err;
    3.16  
    3.17 -        _anon_functions     = options.getBoolean("anon.functions");
    3.18 -        _class_cache_size   = options.getInteger("class.cache.size");
    3.19 -        _compile_only       = options.getBoolean("compile.only");
    3.20 -        _debug_lines        = options.getBoolean("debug.lines");
    3.21 -        _dest_dir           = options.getString("d");
    3.22 -        _dump_on_error      = options.getBoolean("doe");
    3.23 -        _early_lvalue_error = options.getBoolean("early.lvalue.error");
    3.24 -        _empty_statements   = options.getBoolean("empty.statements");
    3.25 -        _fullversion        = options.getBoolean("fullversion");
    3.26 -        _loader_per_compile = options.getBoolean("loader.per.compile");
    3.27 -        _package            = options.getString("package");
    3.28 -        _parse_only         = options.getBoolean("parse.only");
    3.29 -        _print_ast          = options.getBoolean("print.ast");
    3.30 -        _print_lower_ast    = options.getBoolean("print.lower.ast");
    3.31 -        _print_code         = options.getBoolean("print.code");
    3.32 -        _print_no_newline   = options.getBoolean("print.no.newline");
    3.33 -        _print_parse        = options.getBoolean("print.parse");
    3.34 -        _print_lower_parse  = options.getBoolean("print.lower.parse");
    3.35 -        _print_symbols      = options.getBoolean("print.symbols");
    3.36 -        _scripting          = options.getBoolean("scripting");
    3.37 -        _strict             = options.getBoolean("strict");
    3.38 -        _version            = options.getBoolean("version");
    3.39 -        _verify_code        = options.getBoolean("verify.code");
    3.40 +        _anon_functions       = options.getBoolean("anon.functions");
    3.41 +        _class_cache_size     = options.getInteger("class.cache.size");
    3.42 +        _compile_only         = options.getBoolean("compile.only");
    3.43 +        _debug_lines          = options.getBoolean("debug.lines");
    3.44 +        _dest_dir             = options.getString("d");
    3.45 +        _dump_on_error        = options.getBoolean("doe");
    3.46 +        _early_lvalue_error   = options.getBoolean("early.lvalue.error");
    3.47 +        _empty_statements     = options.getBoolean("empty.statements");
    3.48 +        _fullversion          = options.getBoolean("fullversion");
    3.49 +        _loader_per_compile   = options.getBoolean("loader.per.compile");
    3.50 +        _no_syntax_extensions = options.getBoolean("no.syntax.extensions");
    3.51 +        _package              = options.getString("package");
    3.52 +        _parse_only           = options.getBoolean("parse.only");
    3.53 +        _print_ast            = options.getBoolean("print.ast");
    3.54 +        _print_lower_ast      = options.getBoolean("print.lower.ast");
    3.55 +        _print_code           = options.getBoolean("print.code");
    3.56 +        _print_no_newline     = options.getBoolean("print.no.newline");
    3.57 +        _print_parse          = options.getBoolean("print.parse");
    3.58 +        _print_lower_parse    = options.getBoolean("print.lower.parse");
    3.59 +        _print_symbols        = options.getBoolean("print.symbols");
    3.60 +        _scripting            = options.getBoolean("scripting");
    3.61 +        _strict               = options.getBoolean("strict");
    3.62 +        _version              = options.getBoolean("version");
    3.63 +        _verify_code          = options.getBoolean("verify.code");
    3.64  
    3.65          int callSiteFlags = 0;
    3.66          if (options.getBoolean("profile.callsites")) {
     4.1 --- a/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Fri Jan 25 17:35:31 2013 +0100
     4.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Mon Jan 28 18:10:16 2013 +0530
     4.3 @@ -44,8 +44,8 @@
     4.4      /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
     4.5      public static final MethodHandle READLINE = findOwnMH("readLine", Object.class, Object.class);
     4.6  
     4.7 -    /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
     4.8 -    public static final MethodHandle READ = findOwnMH("read",     Object.class, Object.class, Object.class);
     4.9 +    /** Handle to implementation of {@link ScriptingFunctions#readFully} - Nashorn extension */
    4.10 +    public static final MethodHandle READFULLY = findOwnMH("readFully",     Object.class, Object.class, Object.class);
    4.11  
    4.12      /** Handle to implementation of {@link ScriptingFunctions#read} - Nashorn extension */
    4.13      public static final MethodHandle QUIT = findOwnMH("quit",     Object.class, Object.class, Object.class);
    4.14 @@ -78,7 +78,7 @@
    4.15       *
    4.16       * @throws IOException if an exception occurs
    4.17       */
    4.18 -    public static Object read(final Object self, final Object file) throws IOException {
    4.19 +    public static Object readFully(final Object self, final Object file) throws IOException {
    4.20          File f = null;
    4.21  
    4.22          if (file instanceof File) {
     5.1 --- a/src/jdk/nashorn/internal/runtime/resources/Options.properties	Fri Jan 25 17:35:31 2013 +0100
     5.2 +++ b/src/jdk/nashorn/internal/runtime/resources/Options.properties	Mon Jan 28 18:10:16 2013 +0530
     5.3 @@ -172,6 +172,14 @@
     5.4      default=true                                   \
     5.5  }
     5.6  
     5.7 +nashorn.option.no.syntax.extensions = {            \
     5.8 +    name="--no-syntax-extensions",                 \
     5.9 +    short_name="--nse",                            \
    5.10 +    is_undocumented=true,                          \
    5.11 +    desc="No non-standard syntax extensions",      \
    5.12 +    default=-anon-functions=false                  \
    5.13 +}
    5.14 +
    5.15  nashorn.option.package = {                                     \
    5.16      name="--package",                                          \
    5.17      is_undocumented=true,                                      \
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/script/basic/JDK-8006983.js	Mon Jan 28 18:10:16 2013 +0530
     6.3 @@ -0,0 +1,85 @@
     6.4 +/*
     6.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.7 + * 
     6.8 + * This code is free software; you can redistribute it and/or modify it
     6.9 + * under the terms of the GNU General Public License version 2 only, as
    6.10 + * published by the Free Software Foundation.
    6.11 + * 
    6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    6.15 + * version 2 for more details (a copy is included in the LICENSE file that
    6.16 + * accompanied this code).
    6.17 + * 
    6.18 + * You should have received a copy of the GNU General Public License version
    6.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    6.21 + * 
    6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    6.23 + * or visit www.oracle.com if you need additional information or have any
    6.24 + * questions.
    6.25 + */
    6.26 +
    6.27 +/**
    6.28 + * 8006983: Introduce a command line option to switch off syntactic extensions of nashorn
    6.29 + *
    6.30 + * @test
    6.31 + * @option -scripting
    6.32 + * @option --no-syntax-extensions
    6.33 + * @run
    6.34 + */
    6.35 +
    6.36 +try {
    6.37 +    eval("var r = new java.lang.Runnable() { run: function(){} }");
    6.38 +    fail("should have thrown error for anon-class-style new");
    6.39 +} catch (e) {
    6.40 +    if (! (e instanceof SyntaxError)) {
    6.41 +        fail("SyntaxError expected, got " + e);
    6.42 +    }
    6.43 +}
    6.44 +
    6.45 +try {
    6.46 +    eval("var sqr = function(x) x*x ");
    6.47 +    fail("should have thrown error for expression closures");
    6.48 +} catch (e) {
    6.49 +    if (! (e instanceof SyntaxError)) {
    6.50 +        fail("SyntaxError expected, got " + e);
    6.51 +    }
    6.52 +}
    6.53 +
    6.54 +try {
    6.55 +    eval("function() {};");
    6.56 +    fail("should have thrown error for anonymous function statement");
    6.57 +} catch (e) {
    6.58 +    if (! (e instanceof SyntaxError)) {
    6.59 +        fail("SyntaxError expected, got " + e);
    6.60 +    }
    6.61 +}
    6.62 +
    6.63 +try {
    6.64 +    eval("for each (i in [22, 33, 33]) { print(i) }");
    6.65 +    fail("should have thrown error for-each statement");
    6.66 +} catch (e) {
    6.67 +    if (! (e instanceof SyntaxError)) {
    6.68 +        fail("SyntaxError expected, got " + e);
    6.69 +    }
    6.70 +}
    6.71 +
    6.72 +try {
    6.73 +    eval("# shell style comment");
    6.74 +    fail("should have thrown error for shell style comment");
    6.75 +} catch (e) {
    6.76 +    if (! (e instanceof SyntaxError)) {
    6.77 +        fail("SyntaxError expected, got " + e);
    6.78 +    }
    6.79 +}
    6.80 +
    6.81 +try {
    6.82 +    eval("print(<<EOF);\nhello\nworld\nEOF\n");
    6.83 +    fail("should have thrown error heredoc");
    6.84 +} catch (e) {
    6.85 +    if (! (e instanceof SyntaxError)) {
    6.86 +        fail("SyntaxError expected, got " + e);
    6.87 +    }
    6.88 +}
     7.1 --- a/test/script/basic/scripting.js	Fri Jan 25 17:35:31 2013 +0100
     7.2 +++ b/test/script/basic/scripting.js	Mon Jan 28 18:10:16 2013 +0530
     7.3 @@ -78,4 +78,4 @@
     7.4  print(y);
     7.5  
     7.6  
     7.7 -print(read(__FILE__));
     7.8 +print(readFully(__FILE__));
     8.1 --- a/test/script/basic/scripting.js.EXPECTED	Fri Jan 25 17:35:31 2013 +0100
     8.2 +++ b/test/script/basic/scripting.js.EXPECTED	Mon Jan 28 18:10:16 2013 +0530
     8.3 @@ -99,5 +99,5 @@
     8.4  print(y);
     8.5  
     8.6  
     8.7 -print(read(__FILE__));
     8.8 +print(readFully(__FILE__));
     8.9  

mercurial