8019508: Comma handling in object literal parsing is wrong

Mon, 01 Jul 2013 23:36:40 +0530

author
sundar
date
Mon, 01 Jul 2013 23:36:40 +0530
changeset 391
9165138b427c
parent 390
ab3ea5b3e507
child 392
5f9abeb0bb50

8019508: Comma handling in object literal parsing is wrong
Reviewed-by: hannesw

src/jdk/nashorn/internal/parser/Parser.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/resources/Messages.properties file | annotate | diff | comparison | revisions
test/script/basic/JDK-8019508.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8019508.js.EXPECTED file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/parser/Parser.java	Mon Jul 01 19:52:07 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java	Mon Jul 01 23:36:40 2013 +0530
     1.3 @@ -1930,7 +1930,7 @@
     1.4  
     1.5          // Object context.
     1.6          // Prepare to accumulate elements.
     1.7 -       // final List<Node> elements = new ArrayList<>();
     1.8 +        // final List<Node> elements = new ArrayList<>();
     1.9          final Map<String, PropertyNode> map = new LinkedHashMap<>();
    1.10  
    1.11          // Create a block for the object literal.
    1.12 @@ -1943,6 +1943,9 @@
    1.13                      break loop;
    1.14  
    1.15                  case COMMARIGHT:
    1.16 +                    if (commaSeen) {
    1.17 +                        throw error(AbstractParser.message("expected.property.id", type.getNameOrType()));
    1.18 +                    }
    1.19                      next();
    1.20                      commaSeen = true;
    1.21                      break;
     2.1 --- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Jul 01 19:52:07 2013 +0530
     2.2 +++ b/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Jul 01 23:36:40 2013 +0530
     2.3 @@ -42,6 +42,7 @@
     2.4  parser.error.expected.operand=Expected an operand but found {0}
     2.5  parser.error.expected.stmt=Expected statement but found {0}
     2.6  parser.error.expected.comma=Expected comma but found {0}
     2.7 +parser.error.expected.property.id=Expected property id but found {0}
     2.8  parser.error.expected=Expected {0} but found {1}
     2.9  parser.error.invalid.return=Invalid return statement
    2.10  parser.error.no.func.decl.here=Function declarations can only occur at program or function body level. You should use a function expression here instead.
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/script/basic/JDK-8019508.js	Mon Jul 01 23:36:40 2013 +0530
     3.3 @@ -0,0 +1,56 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + * 
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + * 
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + * 
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + * 
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/**
    3.28 + * JDK-8019508: Comma handling in object literal parsing is wrong
    3.29 + *
    3.30 + * @test
    3.31 + * @run
    3.32 + */
    3.33 +
    3.34 +function checkObjLiteral(str) {
    3.35 +    try {
    3.36 +        eval(str);
    3.37 +        fail("SyntaxError expected for: " + str);
    3.38 +    } catch (e) {
    3.39 +        if (! (e instanceof SyntaxError)) {
    3.40 +            fail("expected SyntaxError, got " + e);
    3.41 +        }
    3.42 +        print(e.message.replace(/\\/g, '/'));
    3.43 +    }
    3.44 +}
    3.45 +
    3.46 +// only comma
    3.47 +checkObjLiteral("({,})");
    3.48 +
    3.49 +// starting with comma
    3.50 +checkObjLiteral("({, a:2 })");
    3.51 +
    3.52 +// consecutive commas
    3.53 +checkObjLiteral("({a:3,,})");
    3.54 +
    3.55 +// missing comma
    3.56 +checkObjLiteral("({a:3 b:2}");
    3.57 +
    3.58 +// single trailing comma is okay!
    3.59 +var obj = { a: 3, };
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/script/basic/JDK-8019508.js.EXPECTED	Mon Jul 01 23:36:40 2013 +0530
     4.3 @@ -0,0 +1,12 @@
     4.4 +test/script/basic/JDK-8019508.js#33<eval>:1:2 Expected property id but found ,
     4.5 +({,})
     4.6 +  ^
     4.7 +test/script/basic/JDK-8019508.js#33<eval>:1:2 Expected property id but found ,
     4.8 +({, a:2 })
     4.9 +  ^
    4.10 +test/script/basic/JDK-8019508.js#33<eval>:1:6 Expected property id but found ,
    4.11 +({a:3,,})
    4.12 +      ^
    4.13 +test/script/basic/JDK-8019508.js#33<eval>:1:6 Expected comma but found ident
    4.14 +({a:3 b:2}
    4.15 +      ^

mercurial