Skip to content

Commit 8068f48

Browse files
verwaestV8 LUCI CQ
authored andcommitted
[parser] Fix initializer ids w/ computed property names
Move the initializer id before the computed property name ids. Bug: 363538434 Change-Id: Ife1abf50a9348242f5e5f6c69c2911106f5a67dd Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5850479 Commit-Queue: Toon Verwaest <[email protected]> Auto-Submit: Toon Verwaest <[email protected]> Reviewed-by: Igor Sheludko <[email protected]> Cr-Commit-Position: refs/heads/main@{#96059}
1 parent ee6fa7c commit 8068f48

5 files changed

Lines changed: 47 additions & 13 deletions

File tree

src/parsing/parser-base.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -620,26 +620,32 @@ class ParserBase {
620620
return instance_members_scope != nullptr;
621621
}
622622

623-
DeclarationScope* EnsureStaticElementsScope(ParserBase* parser,
624-
int beg_pos) {
623+
DeclarationScope* EnsureStaticElementsScope(ParserBase* parser, int beg_pos,
624+
int info_id) {
625625
if (!has_static_elements()) {
626626
static_elements_scope = parser->NewFunctionScope(
627627
FunctionKind::kClassStaticInitializerFunction);
628628
static_elements_scope->SetLanguageMode(LanguageMode::kStrict);
629629
static_elements_scope->set_start_position(beg_pos);
630-
static_elements_function_id = parser->GetNextInfoId();
630+
static_elements_function_id = info_id;
631+
// Actually consume the id. The id that was passed in might be an
632+
// earlier id in case of computed property names.
633+
parser->GetNextInfoId();
631634
}
632635
return static_elements_scope;
633636
}
634637

635638
DeclarationScope* EnsureInstanceMembersScope(ParserBase* parser,
636-
int beg_pos) {
639+
int beg_pos, int info_id) {
637640
if (!has_instance_members()) {
638641
instance_members_scope = parser->NewFunctionScope(
639642
FunctionKind::kClassMembersInitializerFunction);
640643
instance_members_scope->SetLanguageMode(LanguageMode::kStrict);
641644
instance_members_scope->set_start_position(beg_pos);
642-
instance_members_function_id = parser->GetNextInfoId();
645+
instance_members_function_id = info_id;
646+
// Actually consume the id. The id that was passed in might be an
647+
// earlier id in case of computed property names.
648+
parser->GetNextInfoId();
643649
}
644650
return instance_members_scope;
645651
}
@@ -1321,7 +1327,7 @@ class ParserBase {
13211327
ParseFunctionFlags flags, bool is_static,
13221328
bool* has_seen_constructor);
13231329
ExpressionT ParseMemberInitializer(ClassInfo* class_info, int beg_pos,
1324-
bool is_static);
1330+
int info_id, bool is_static);
13251331
BlockT ParseClassStaticBlock(ClassInfo* class_info);
13261332
ObjectLiteralPropertyT ParseObjectPropertyDefinition(
13271333
ParsePropertyInfo* prop_info, bool* has_seen_proto);
@@ -2624,6 +2630,8 @@ ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
26242630
DCHECK_NOT_NULL(class_info);
26252631
DCHECK_EQ(prop_info->position, PropertyPosition::kClassLiteral);
26262632

2633+
int next_info_id = PeekNextInfoId();
2634+
26272635
Token::Value name_token = peek();
26282636
int property_beg_pos = peek_position();
26292637
int name_token_position = property_beg_pos;
@@ -2667,12 +2675,18 @@ ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
26672675
// field.
26682676
DCHECK_IMPLIES(prop_info->is_computed_name, !prop_info->is_private);
26692677

2670-
if (!prop_info->is_computed_name) {
2678+
if (prop_info->is_computed_name) {
2679+
if (next_info_id != PeekNextInfoId() &&
2680+
!(prop_info->is_static ? class_info->has_static_elements()
2681+
: class_info->has_instance_members())) {
2682+
impl()->ReindexComputedMemberName(name_expression);
2683+
}
2684+
} else {
26712685
CheckClassFieldName(prop_info->name, prop_info->is_static);
26722686
}
26732687

2674-
ExpressionT value = ParseMemberInitializer(class_info, property_beg_pos,
2675-
prop_info->is_static);
2688+
ExpressionT value = ParseMemberInitializer(
2689+
class_info, property_beg_pos, next_info_id, prop_info->is_static);
26762690
ExpectSemicolon();
26772691

26782692
ClassLiteralPropertyT result;
@@ -2786,11 +2800,12 @@ ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
27862800

27872801
template <typename Impl>
27882802
typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseMemberInitializer(
2789-
ClassInfo* class_info, int beg_pos, bool is_static) {
2803+
ClassInfo* class_info, int beg_pos, int info_id, bool is_static) {
27902804
FunctionParsingScope body_parsing_scope(impl());
27912805
DeclarationScope* initializer_scope =
2792-
is_static ? class_info->EnsureStaticElementsScope(this, beg_pos)
2793-
: class_info->EnsureInstanceMembersScope(this, beg_pos);
2806+
is_static
2807+
? class_info->EnsureStaticElementsScope(this, beg_pos, info_id)
2808+
: class_info->EnsureInstanceMembersScope(this, beg_pos, info_id);
27942809

27952810
if (Check(Token::kAssign)) {
27962811
FunctionState initializer_state(&function_state_, &scope_,
@@ -2811,7 +2826,7 @@ typename ParserBase<Impl>::BlockT ParserBase<Impl>::ParseClassStaticBlock(
28112826
Consume(Token::kStatic);
28122827

28132828
DeclarationScope* initializer_scope =
2814-
class_info->EnsureStaticElementsScope(this, position());
2829+
class_info->EnsureStaticElementsScope(this, position(), PeekNextInfoId());
28152830

28162831
FunctionState initializer_state(&function_state_, &scope_, initializer_scope);
28172832
FunctionParsingScope body_parsing_scope(impl());

src/parsing/parser.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,13 @@ void Parser::ReindexArrowFunctionFormalParameters(
27352735
}
27362736
}
27372737

2738+
void Parser::ReindexComputedMemberName(Expression* computed_name) {
2739+
// Make space for the member initializer function above the computed property
2740+
// name.
2741+
AstFunctionLiteralIdReindexer reindexer(stack_limit_, 1);
2742+
reindexer.Reindex(computed_name);
2743+
}
2744+
27382745
void Parser::PrepareGeneratorVariables() {
27392746
// Calling a generator returns a generator object. That object is stored
27402747
// in a temporary variable, a definition that is used by "yield"

src/parsing/parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
896896
}
897897

898898
void ReindexArrowFunctionFormalParameters(ParserFormalParameters* parameters);
899+
void ReindexComputedMemberName(Expression* computed_name);
899900
void DeclareArrowFunctionFormalParameters(
900901
ParserFormalParameters* parameters, Expression* params,
901902
const Scanner::Location& params_loc);

src/parsing/preparser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,8 @@ class PreParser : public ParserBase<PreParser> {
15601560

15611561
V8_INLINE void ReindexArrowFunctionFormalParameters(
15621562
PreParserFormalParameters* parameters) {}
1563+
V8_INLINE void ReindexComputedMemberName(
1564+
const PreParserExpression& expression) {}
15631565
V8_INLINE void DeclareFormalParameters(
15641566
const PreParserFormalParameters* parameters) {
15651567
if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2024 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
try {
6+
new class {
7+
static [function(){}] = [].trigger_error();
8+
}
9+
} catch (e) {}

0 commit comments

Comments
 (0)