8019508: Comma handling in object literal parsing is wrong
authorsundar
Mon, 01 Jul 2013 23:36:40 +0530
changeset 18629 6b0b6aab4280
parent 18628 012cd852f881
child 18630 48e3dc4bf0f6
8019508: Comma handling in object literal parsing is wrong Reviewed-by: hannesw
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties
nashorn/test/script/basic/JDK-8019508.js
nashorn/test/script/basic/JDK-8019508.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Mon Jul 01 19:52:07 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Mon Jul 01 23:36:40 2013 +0530
@@ -1930,7 +1930,7 @@
 
         // Object context.
         // Prepare to accumulate elements.
-       // final List<Node> elements = new ArrayList<>();
+        // final List<Node> elements = new ArrayList<>();
         final Map<String, PropertyNode> map = new LinkedHashMap<>();
 
         // Create a block for the object literal.
@@ -1943,6 +1943,9 @@
                     break loop;
 
                 case COMMARIGHT:
+                    if (commaSeen) {
+                        throw error(AbstractParser.message("expected.property.id", type.getNameOrType()));
+                    }
                     next();
                     commaSeen = true;
                     break;
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Jul 01 19:52:07 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Jul 01 23:36:40 2013 +0530
@@ -42,6 +42,7 @@
 parser.error.expected.operand=Expected an operand but found {0}
 parser.error.expected.stmt=Expected statement but found {0}
 parser.error.expected.comma=Expected comma but found {0}
+parser.error.expected.property.id=Expected property id but found {0}
 parser.error.expected=Expected {0} but found {1}
 parser.error.invalid.return=Invalid return statement
 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.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019508.js	Mon Jul 01 23:36:40 2013 +0530
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+/**
+ * JDK-8019508: Comma handling in object literal parsing is wrong
+ *
+ * @test
+ * @run
+ */
+
+function checkObjLiteral(str) {
+    try {
+        eval(str);
+        fail("SyntaxError expected for: " + str);
+    } catch (e) {
+        if (! (e instanceof SyntaxError)) {
+            fail("expected SyntaxError, got " + e);
+        }
+        print(e.message.replace(/\\/g, '/'));
+    }
+}
+
+// only comma
+checkObjLiteral("({,})");
+
+// starting with comma
+checkObjLiteral("({, a:2 })");
+
+// consecutive commas
+checkObjLiteral("({a:3,,})");
+
+// missing comma
+checkObjLiteral("({a:3 b:2}");
+
+// single trailing comma is okay!
+var obj = { a: 3, };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8019508.js.EXPECTED	Mon Jul 01 23:36:40 2013 +0530
@@ -0,0 +1,12 @@
+test/script/basic/JDK-8019508.js#33<eval>:1:2 Expected property id but found ,
+({,})
+  ^
+test/script/basic/JDK-8019508.js#33<eval>:1:2 Expected property id but found ,
+({, a:2 })
+  ^
+test/script/basic/JDK-8019508.js#33<eval>:1:6 Expected property id but found ,
+({a:3,,})
+      ^
+test/script/basic/JDK-8019508.js#33<eval>:1:6 Expected comma but found ident
+({a:3 b:2}
+      ^