[class] Ban #constructor as a private field name

Bug: v8:5368
Change-Id: Idcb13e039614167da8b5be879e5644dbcb9df271
Reviewed-on: https://chromium-review.googlesource.com/899881
Reviewed-by: Georg Neis <neis@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51128}
This commit is contained in:
Sathya Gunasekaran 2018-02-06 10:23:39 -08:00 committed by Commit Bot
parent 134cd88243
commit ec434d6767
5 changed files with 15 additions and 6 deletions

View File

@ -1454,6 +1454,10 @@ class ParserBase {
return this->scanner()->CurrentMatchesContextualEscaped(
Token::CONSTRUCTOR);
}
bool IsPrivateConstructor() {
return this->scanner()->CurrentMatchesContextualEscaped(
Token::PRIVATE_CONSTRUCTOR);
}
bool IsPrototype() {
return this->scanner()->CurrentMatchesContextualEscaped(Token::PROTOTYPE);
}
@ -2345,7 +2349,7 @@ ParserBase<Impl>::ParseClassPropertyDefinition(
*ok = false;
return impl()->NullLiteralProperty();
}
if (!*is_computed_name && name_token != Token::PRIVATE_NAME) {
if (!*is_computed_name) {
checker->CheckClassFieldName(*is_static,
CHECK_OK_CUSTOM(NullLiteralProperty));
}
@ -6223,7 +6227,7 @@ void ParserBase<Impl>::ClassLiteralChecker::CheckClassFieldName(bool is_static,
return;
}
if (IsConstructor()) {
if (IsConstructor() || IsPrivateConstructor()) {
this->parser()->ReportMessage(MessageTemplate::kConstructorClassField);
*ok = false;
return;

View File

@ -1510,13 +1510,15 @@ uc32 Scanner::ScanUnicodeEscape() {
KEYWORD_GROUP('y') \
KEYWORD("yield", Token::YIELD) \
KEYWORD_GROUP('_') \
KEYWORD("__proto__", Token::PROTO_UNDERSCORED)
KEYWORD("__proto__", Token::PROTO_UNDERSCORED) \
KEYWORD_GROUP('#') \
KEYWORD("#constructor", Token::PRIVATE_CONSTRUCTOR)
static Token::Value KeywordOrIdentifierToken(const uint8_t* input,
int input_length) {
DCHECK_GE(input_length, 1);
const int kMinLength = 2;
const int kMaxLength = 11;
const int kMaxLength = 12;
if (input_length < kMinLength || input_length > kMaxLength) {
return Token::IDENTIFIER;
}

View File

@ -195,6 +195,7 @@ namespace internal {
C(NAME, "name", 0) \
C(PROTO_UNDERSCORED, "__proto__", 0) \
C(CONSTRUCTOR, "constructor", 0) \
C(PRIVATE_CONSTRUCTOR, "#constructor", 0) \
C(PROTOTYPE, "prototype", 0) \
C(EVAL, "eval", 0) \
C(ARGUMENTS, "arguments", 0) \

View File

@ -5129,6 +5129,9 @@ TEST(PrivateClassFieldsErrors) {
"#async a = 0",
"#async a",
"#constructor",
"#constructor = function() {}",
"# a = 0",
"#a() { }",
"get #a() { }",

View File

@ -11,8 +11,7 @@
// (a) check private field access on proxies.
// (b) throw reference error on missing private field access.
// (c) throw when private fields are set without being declared.
// (d) check that private field isn't called 'constructor'.
// (e) tests involving eval
// (d) tests involving eval
{
class C {
#a;