Bug 1130811 - Refactor some object-literal key:property and shorthand parsing code slightly. r=efaust

This commit is contained in:
Jeff Walden 2015-02-10 14:03:53 -08:00
parent 194b1ce2ec
commit ac8593490a
3 changed files with 39 additions and 22 deletions

View File

@ -104,7 +104,7 @@ class FullParseHandler
return dn;
}
ParseNode *newIdentifier(JSAtom *atom, const TokenPos &pos) {
ParseNode *newObjectLiteralPropertyName(JSAtom *atom, const TokenPos &pos) {
return new_<NullaryNode>(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);

View File

@ -7939,7 +7939,7 @@ Parser<ParseHandler>::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<ParseHandler>::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<ParseHandler>::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<ParseHandler>::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();

View File

@ -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; }