From ac8593490acb7da296dc7f5b40b5ecec6f7a0f68 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Tue, 10 Feb 2015 14:03:53 -0800 Subject: [PATCH] Bug 1130811 - Refactor some object-literal key:property and shorthand parsing code slightly. r=efaust --- js/src/frontend/FullParseHandler.h | 31 +++++++++++++++++++++------- js/src/frontend/Parser.cpp | 22 ++++++++------------ js/src/frontend/SyntaxParseHandler.h | 8 +++++-- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/js/src/frontend/FullParseHandler.h b/js/src/frontend/FullParseHandler.h index a146e4ace6a..9ad2789d9bb 100644 --- a/js/src/frontend/FullParseHandler.h +++ b/js/src/frontend/FullParseHandler.h @@ -104,7 +104,7 @@ class FullParseHandler return dn; } - ParseNode *newIdentifier(JSAtom *atom, const TokenPos &pos) { + ParseNode *newObjectLiteralPropertyName(JSAtom *atom, const TokenPos &pos) { return new_(PNK_NAME, JSOP_NOP, pos, atom); } @@ -293,13 +293,30 @@ class FullParseHandler return true; } - bool addPropertyDefinition(ParseNode *literal, ParseNode *name, ParseNode *expr, - bool isShorthand = false) { + bool addPropertyDefinition(ParseNode *literal, ParseNode *key, ParseNode *val) { + MOZ_ASSERT(literal->isKind(PNK_OBJECT)); MOZ_ASSERT(literal->isArity(PN_LIST)); - ParseNode *propdef = newBinary(isShorthand ? PNK_SHORTHAND : PNK_COLON, name, expr, - JSOP_INITPROP); - if (isShorthand) - literal->pn_xflags |= PNX_NONCONST; + MOZ_ASSERT(key->isKind(PNK_NUMBER) || + key->isKind(PNK_NAME) || + key->isKind(PNK_STRING) || + key->isKind(PNK_COMPUTED_NAME)); + + ParseNode *propdef = newBinary(PNK_COLON, key, val, JSOP_INITPROP); + if (!propdef) + return false; + literal->append(propdef); + return true; + } + + bool addShorthand(ParseNode *literal, ParseNode *name, ParseNode *expr) { + MOZ_ASSERT(literal->isKind(PNK_OBJECT)); + MOZ_ASSERT(literal->isArity(PN_LIST)); + MOZ_ASSERT(name->isKind(PNK_NAME)); + MOZ_ASSERT(expr->isKind(PNK_NAME)); + MOZ_ASSERT(name->pn_atom == expr->pn_atom); + + setListFlag(literal, PNX_NONCONST); + ParseNode *propdef = newBinary(PNK_SHORTHAND, name, expr, JSOP_INITPROP); if (!propdef) return false; literal->append(propdef); diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index 4ec41d6e86b..80fcc0450f5 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -7939,7 +7939,7 @@ Parser::objectLiteral() op = atom == context->names().get ? JSOP_INITPROP_GETTER : JSOP_INITPROP_SETTER; } else { - propname = handler.newIdentifier(atom, pos()); + propname = handler.newObjectLiteralPropertyName(atom, pos()); if (!propname) return null(); break; @@ -7952,7 +7952,7 @@ Parser::objectLiteral() return null(); if (tt == TOK_NAME) { atom = tokenStream.currentName(); - propname = newName(atom->asPropertyName()); + propname = handler.newObjectLiteralPropertyName(atom, pos()); if (!propname) return null(); } else if (tt == TOK_STRING) { @@ -7985,7 +7985,7 @@ Parser::objectLiteral() } else { // Not an accessor property after all. tokenStream.ungetToken(); - propname = handler.newIdentifier(atom, pos()); + propname = handler.newObjectLiteralPropertyName(atom, pos()); if (!propname) return null(); op = JSOP_INITPROP; @@ -8064,20 +8064,16 @@ Parser::objectLiteral() report(ParseError, false, null(), JSMSG_BAD_PROP_ID); return null(); } - if (!abortIfSyntaxParser()) - return null(); + tokenStream.ungetToken(); if (!tokenStream.checkForKeyword(atom, nullptr)) return null(); - PropertyName *name = handler.isName(propname); - MOZ_ASSERT(atom); - propname = newName(name); - if (!propname) + + Node nameExpr = identifierName(); + if (!nameExpr) return null(); - Node ident = identifierName(); - if (!ident) - return null(); - if (!handler.addPropertyDefinition(literal, propname, ident, true)) + + if (!handler.addShorthand(literal, propname, nameExpr)) return null(); } else if (tt == TOK_LP) { tokenStream.ungetToken(); diff --git a/js/src/frontend/SyntaxParseHandler.h b/js/src/frontend/SyntaxParseHandler.h index 6656e75faa1..61a2e768f90 100644 --- a/js/src/frontend/SyntaxParseHandler.h +++ b/js/src/frontend/SyntaxParseHandler.h @@ -120,7 +120,10 @@ class SyntaxParseHandler return Definition::PLACEHOLDER; } - Node newIdentifier(JSAtom *atom, const TokenPos &pos) { return NodeName; } + Node newObjectLiteralPropertyName(JSAtom *atom, const TokenPos &pos) { + return NodeName; + } + Node newNumber(double value, DecimalPoint decimalPoint, const TokenPos &pos) { return NodeGeneric; } Node newBooleanLiteral(bool cond, const TokenPos &pos) { return NodeGeneric; } @@ -184,7 +187,8 @@ class SyntaxParseHandler Node newObjectLiteral(uint32_t begin) { return NodeGeneric; } bool addPrototypeMutation(Node literal, uint32_t begin, Node expr) { return true; } - bool addPropertyDefinition(Node literal, Node name, Node expr, bool isShorthand = false) { return true; } + bool addPropertyDefinition(Node literal, Node name, Node expr) { return true; } + bool addShorthand(Node literal, Node name, Node expr) { return true; } bool addMethodDefinition(Node literal, Node name, Node fn, JSOp op) { return true; } Node newYieldExpression(uint32_t begin, Node value, Node gen) { return NodeUnparenthesizedYieldExpr; } Node newYieldStarExpression(uint32_t begin, Node value, Node gen) { return NodeGeneric; }