mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-13 04:21:54 +00:00
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:
parent
9c0b185ab8
commit
ef4e7b7945
Notes:
github-actions[bot]
2025-04-08 16:54:10 +00:00
Author: https://github.com/awesomekling
Commit: ef4e7b7945
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4283
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/trflynn89
3 changed files with 14 additions and 6 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue