LibJS: Make JS parser emit accurate this insights for constructors

This way we don't have to handle it when instantiating the constructor.
This commit is contained in:
Andreas Kling 2025-04-07 21:04:45 +02:00 committed by Andreas Kling
parent 9c0b185ab8
commit ef4e7b7945
Notes: github-actions[bot] 2025-04-08 16:54:10 +00:00
3 changed files with 14 additions and 6 deletions

View file

@ -346,9 +346,6 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::create_class_const
// FIXME: Step 14.a is done in the parser. By using a synthetic super(...args) which does not call @@iterator of %Array.prototype%
auto const& constructor = *m_constructor;
auto parsing_insights = constructor.parsing_insights();
parsing_insights.uses_this_from_environment = true;
parsing_insights.uses_this = true;
auto class_constructor = ECMAScriptFunctionObject::create(
realm,
constructor.name(),
@ -361,7 +358,7 @@ ThrowCompletionOr<ECMAScriptFunctionObject*> ClassExpression::create_class_const
vm.running_execution_context().private_environment,
constructor.kind(),
constructor.is_strict_mode(),
parsing_insights,
constructor.parsing_insights(),
constructor.is_arrow_function());
class_constructor->set_name(class_name);

View file

@ -1565,7 +1565,7 @@ NonnullRefPtr<ClassExpression const> Parser::parse_class_expression(bool expect_
}
if (match(TokenType::ParenOpen)) {
u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
u16 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
if (!super_class.is_null() && !is_static && is_constructor)
parse_options |= FunctionNodeParseOptions::AllowSuperConstructorCall;
if (method_kind == ClassMethod::Kind::Getter)
@ -1576,6 +1576,8 @@ NonnullRefPtr<ClassExpression const> Parser::parse_class_expression(bool expect_
parse_options |= FunctionNodeParseOptions::IsGeneratorFunction;
if (is_async)
parse_options |= FunctionNodeParseOptions::IsAsyncFunction;
if (is_constructor)
parse_options |= FunctionNodeParseOptions::IsConstructor;
auto function = parse_function_node<FunctionExpression>(parse_options, function_start);
if (is_constructor) {
constructor = move(function);
@ -1635,12 +1637,16 @@ NonnullRefPtr<ClassExpression const> Parser::parse_class_expression(bool expect_
constructor_body->append(create_ast_node<ReturnStatement>({ m_source_code, rule_start.position(), position() }, move(super_call)));
FunctionParsingInsights parsing_insights;
parsing_insights.uses_this_from_environment = true;
parsing_insights.uses_this = true;
constructor = create_ast_node<FunctionExpression>(
{ m_source_code, rule_start.position(), position() }, class_name, "",
move(constructor_body), FunctionParameters::create(Vector { FunctionParameter { move(argument_name), nullptr, true } }), 0, FunctionKind::Normal,
/* is_strict_mode */ true, parsing_insights, /* local_variables_names */ Vector<FlyString> {});
} else {
FunctionParsingInsights parsing_insights;
parsing_insights.uses_this_from_environment = true;
parsing_insights.uses_this = true;
constructor = create_ast_node<FunctionExpression>(
{ m_source_code, rule_start.position(), position() }, class_name, "",
move(constructor_body), FunctionParameters::empty(), 0, FunctionKind::Normal,
@ -2073,7 +2079,7 @@ NonnullRefPtr<ObjectExpression const> Parser::parse_object_expression()
invalid_object_literal_property_range = expression->source_range();
} else if (match(TokenType::ParenOpen)) {
VERIFY(property_key);
u8 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
u16 parse_options = FunctionNodeParseOptions::AllowSuperPropertyLookup;
if (property_type == ObjectProperty::Type::Getter)
parse_options |= FunctionNodeParseOptions::IsGetterFunction;
if (property_type == ObjectProperty::Type::Setter)
@ -2992,6 +2998,10 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u16 parse_options, O
auto function_end_offset = position().offset - m_state.current_token.trivia().length();
auto source_text = ByteString { m_state.lexer.source().substring_view(function_start_offset, function_end_offset - function_start_offset) };
parsing_insights.might_need_arguments_object = m_state.function_might_need_arguments_object;
if (parse_options & FunctionNodeParseOptions::IsConstructor) {
parsing_insights.uses_this = true;
parsing_insights.uses_this_from_environment = true;
}
return create_ast_node<FunctionNodeType>(
{ m_source_code, rule_start.position(), position() },
name, move(source_text), move(body), parameters.release_nonnull(), function_length,

View file

@ -39,6 +39,7 @@ struct FunctionNodeParseOptions {
IsGeneratorFunction = 1 << 6,
IsAsyncFunction = 1 << 7,
HasDefaultExportName = 1 << 8,
IsConstructor = 1 << 9,
};
};