# HG changeset patch # User sundar # Date 1372702000 -19800 # Node ID 9165138b427c621780f1611944d68d59f0e5dc36 # Parent ab3ea5b3e5077e3728a0685c8b0e36e5d09caafc 8019508: Comma handling in object literal parsing is wrong Reviewed-by: hannesw diff -r ab3ea5b3e507 -r 9165138b427c src/jdk/nashorn/internal/parser/Parser.java --- a/src/jdk/nashorn/internal/parser/Parser.java Mon Jul 01 19:52:07 2013 +0530 +++ b/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 elements = new ArrayList<>(); + // final List elements = new ArrayList<>(); final Map 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; diff -r ab3ea5b3e507 -r 9165138b427c src/jdk/nashorn/internal/runtime/resources/Messages.properties --- a/src/jdk/nashorn/internal/runtime/resources/Messages.properties Mon Jul 01 19:52:07 2013 +0530 +++ b/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. diff -r ab3ea5b3e507 -r 9165138b427c test/script/basic/JDK-8019508.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/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, }; diff -r ab3ea5b3e507 -r 9165138b427c test/script/basic/JDK-8019508.js.EXPECTED --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/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:1:2 Expected property id but found , +({,}) + ^ +test/script/basic/JDK-8019508.js#33:1:2 Expected property id but found , +({, a:2 }) + ^ +test/script/basic/JDK-8019508.js#33:1:6 Expected property id but found , +({a:3,,}) + ^ +test/script/basic/JDK-8019508.js#33:1:6 Expected comma but found ident +({a:3 b:2} + ^