diff --git a/Tests/LibJS/test262-runner.cpp b/Tests/LibJS/test262-runner.cpp index 95d939520a9..a01fef2d9af 100644 --- a/Tests/LibJS/test262-runner.cpp +++ b/Tests/LibJS/test262-runner.cpp @@ -87,7 +87,7 @@ static Result run_program(InterpreterT& interpreter, ScriptOrMo }); } else { auto program_node = program.visit( - [](auto& visitor) -> NonnullRefPtr { + [](auto& visitor) -> NonnullRefPtr { return visitor->parse_node(); }); diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 89401762b4a..50cb9ecb15e 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2021-2022, David Tuin * @@ -914,21 +914,21 @@ Completion ForStatement::for_body_evaluation(JS::Interpreter& interpreter, Vecto } struct ForInOfHeadState { - explicit ForInOfHeadState(Variant, NonnullRefPtr> lhs) + explicit ForInOfHeadState(Variant, NonnullRefPtr> lhs) { lhs.visit( - [&](NonnullRefPtr& ast_node) { + [&](NonnullRefPtr& ast_node) { expression_lhs = ast_node.ptr(); }, - [&](NonnullRefPtr& pattern) { + [&](NonnullRefPtr& pattern) { pattern_lhs = pattern.ptr(); destructuring = true; lhs_kind = Assignment; }); } - ASTNode* expression_lhs = nullptr; - BindingPattern* pattern_lhs = nullptr; + ASTNode const* expression_lhs = nullptr; + BindingPattern const* pattern_lhs = nullptr; enum LhsKind { Assignment, VarBinding, @@ -956,12 +956,12 @@ struct ForInOfHeadState { VERIFY(expression_lhs); if (is(*expression_lhs)) { auto& declaration = static_cast(*expression_lhs); - VERIFY(declaration.declarations().first().target().has>()); - lhs_reference = TRY(declaration.declarations().first().target().get>()->to_reference(interpreter)); + VERIFY(declaration.declarations().first().target().has>()); + lhs_reference = TRY(declaration.declarations().first().target().get>()->to_reference(interpreter)); } else if (is(*expression_lhs)) { auto& declaration = static_cast(*expression_lhs); - VERIFY(declaration.declarations().first().target().has>()); - lhs_reference = TRY(declaration.declarations().first().target().get>()->to_reference(interpreter)); + VERIFY(declaration.declarations().first().target().has>()); + lhs_reference = TRY(declaration.declarations().first().target().get>()->to_reference(interpreter)); } else { VERIFY(is(*expression_lhs) || is(*expression_lhs) || is(*expression_lhs)); auto& expression = static_cast(*expression_lhs); @@ -1028,7 +1028,7 @@ struct ForInOfHeadState { } VERIFY(expression_lhs && is(*expression_lhs)); auto& for_declaration = static_cast(*expression_lhs); - auto& binding_pattern = for_declaration.declarations().first().target().get>(); + auto& binding_pattern = for_declaration.declarations().first().target().get>(); VERIFY(lhs_kind == VarBinding || iteration_environment); // At this point iteration_environment is undefined if lhs_kind == VarBinding which means this does both @@ -1041,12 +1041,12 @@ struct ForInOfHeadState { // 14.7.5.6 ForIn/OfHeadEvaluation ( uninitializedBoundNames, expr, iterationKind ), https://tc39.es/ecma262/#sec-runtime-semantics-forinofheadevaluation // This method combines ForInOfLoopEvaluation and ForIn/OfHeadEvaluation for similar reason as ForIn/OfBodyEvaluation, to prevent code duplication. // For the same reason we also skip step 6 and 7 of ForIn/OfHeadEvaluation as this is done by the appropriate for loop type. -static ThrowCompletionOr for_in_of_head_execute(Interpreter& interpreter, Variant, NonnullRefPtr> lhs, Expression const& rhs) +static ThrowCompletionOr for_in_of_head_execute(Interpreter& interpreter, Variant, NonnullRefPtr> lhs, Expression const& rhs) { auto& vm = interpreter.vm(); ForInOfHeadState state(lhs); - if (auto* ast_ptr = lhs.get_pointer>(); ast_ptr && is(ast_ptr->ptr())) { + if (auto* ast_ptr = lhs.get_pointer>(); ast_ptr && is(ast_ptr->ptr())) { // Runtime Semantics: ForInOfLoopEvaluation, for any of: // ForInOfStatement : for ( var ForBinding in Expression ) Statement // ForInOfStatement : for ( ForDeclaration in Expression ) Statement @@ -1059,14 +1059,14 @@ static ThrowCompletionOr for_in_of_head_execute(Interpreter& i if (is(ast_ptr->ptr())) { auto& variable_declaration = static_cast(*(*ast_ptr)); VERIFY(variable_declaration.declarations().size() == 1); - state.destructuring = variable_declaration.declarations().first().target().has>(); + state.destructuring = variable_declaration.declarations().first().target().has>(); if (variable_declaration.declaration_kind() == DeclarationKind::Var) { state.lhs_kind = ForInOfHeadState::VarBinding; auto& variable = variable_declaration.declarations().first(); // B.3.5 Initializers in ForIn Statement Heads, https://tc39.es/ecma262/#sec-initializers-in-forin-statement-heads if (variable.init()) { - VERIFY(variable.target().has>()); - auto& binding_id = variable.target().get>()->string(); + VERIFY(variable.target().has>()); + auto& binding_id = variable.target().get>()->string(); auto reference = TRY(interpreter.vm().resolve_binding(binding_id)); auto result = TRY(interpreter.vm().named_evaluation_if_anonymous_function(*variable.init(), binding_id)); TRY(reference.put_value(vm, result)); @@ -1742,7 +1742,7 @@ ThrowCompletionOr ClassMethod::class_element_evaluatio // 10.2.1.3 Runtime Semantics: EvaluateBody, https://tc39.es/ecma262/#sec-runtime-semantics-evaluatebody class ClassFieldInitializerStatement : public Statement { public: - ClassFieldInitializerStatement(SourceRange source_range, NonnullRefPtr expression, DeprecatedFlyString field_name) + ClassFieldInitializerStatement(SourceRange source_range, NonnullRefPtr expression, DeprecatedFlyString field_name) : Statement(source_range) , m_expression(move(expression)) , m_class_field_identifier_name(move(field_name)) @@ -1775,7 +1775,7 @@ public: } private: - NonnullRefPtr m_expression; + NonnullRefPtr m_expression; DeprecatedFlyString m_class_field_identifier_name; // [[ClassFieldIdentifierName]] }; @@ -2414,7 +2414,7 @@ bool BindingPattern::contains_expression() const for (auto& entry : entries) { if (entry.initializer) return true; - if (auto binding_ptr = entry.alias.get_pointer>(); binding_ptr && (*binding_ptr)->contains_expression()) + if (auto binding_ptr = entry.alias.get_pointer>(); binding_ptr && (*binding_ptr)->contains_expression()) return true; } return false; @@ -2424,14 +2424,14 @@ ThrowCompletionOr BindingPattern::for_each_bound_name(ThrowCompletionOrVoi { for (auto const& entry : entries) { auto const& alias = entry.alias; - if (alias.has>()) { - TRY(callback(alias.get>()->string())); - } else if (alias.has>()) { - TRY(alias.get>()->for_each_bound_name(forward(callback))); + if (alias.has>()) { + TRY(callback(alias.get>()->string())); + } else if (alias.has>()) { + TRY(alias.get>()->for_each_bound_name(forward(callback))); } else { auto const& name = entry.name; - if (name.has>()) - TRY(callback(name.get>()->string())); + if (name.has>()) + TRY(callback(name.get>()->string())); } } return {}; @@ -2449,10 +2449,10 @@ void BindingPattern::dump(int indent) const if (kind == Kind::Object) { print_indent(indent + 2); outln("(Identifier)"); - if (entry.name.has>()) { - entry.name.get>()->dump(indent + 3); + if (entry.name.has>()) { + entry.name.get>()->dump(indent + 3); } else { - entry.name.get>()->dump(indent + 3); + entry.name.get>()->dump(indent + 3); } } else if (entry.is_elision()) { print_indent(indent + 2); @@ -2462,12 +2462,12 @@ void BindingPattern::dump(int indent) const print_indent(indent + 2); outln("(Pattern{})", entry.is_rest ? " rest=true" : ""); - if (entry.alias.has>()) { - entry.alias.get>()->dump(indent + 3); - } else if (entry.alias.has>()) { - entry.alias.get>()->dump(indent + 3); - } else if (entry.alias.has>()) { - entry.alias.get>()->dump(indent + 3); + if (entry.alias.has>()) { + entry.alias.get>()->dump(indent + 3); + } else if (entry.alias.has>()) { + entry.alias.get>()->dump(indent + 3); + } else if (entry.alias.has>()) { + entry.alias.get>()->dump(indent + 3); } else { print_indent(indent + 3); outln(""); @@ -2718,7 +2718,7 @@ Completion AssignmentExpression::execute(Interpreter& interpreter) const // AssignmentExpression : LeftHandSideExpression = AssignmentExpression return m_lhs.visit( // 1. If LeftHandSideExpression is neither an ObjectLiteral nor an ArrayLiteral, then - [&](NonnullRefPtr const& lhs) -> ThrowCompletionOr { + [&](NonnullRefPtr const& lhs) -> ThrowCompletionOr { // a. Let lref be the result of evaluating LeftHandSideExpression. // b. ReturnIfAbrupt(lref). auto reference = TRY(lhs->to_reference(interpreter)); @@ -2745,7 +2745,7 @@ Completion AssignmentExpression::execute(Interpreter& interpreter) const return rhs_result; }, // 2. Let assignmentPattern be the AssignmentPattern that is covered by LeftHandSideExpression. - [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { + [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { // 3. Let rref be the result of evaluating AssignmentExpression. // 4. Let rval be ? GetValue(rref). auto rhs_result = TRY(m_rhs->execute(interpreter)).release_value(); @@ -2757,10 +2757,10 @@ Completion AssignmentExpression::execute(Interpreter& interpreter) const return rhs_result; }); } - VERIFY(m_lhs.has>()); + VERIFY(m_lhs.has>()); // 1. Let lref be the result of evaluating LeftHandSideExpression. - auto& lhs_expression = *m_lhs.get>(); + auto& lhs_expression = *m_lhs.get>(); auto reference = TRY(lhs_expression.to_reference(interpreter)); // 2. Let lval be ? GetValue(lref). @@ -3029,7 +3029,7 @@ Completion VariableDeclaration::execute(Interpreter& interpreter) const for (auto& declarator : m_declarations) { if (auto* init = declarator.init()) { TRY(declarator.target().visit( - [&](NonnullRefPtr const& id) -> ThrowCompletionOr { + [&](NonnullRefPtr const& id) -> ThrowCompletionOr { auto reference = TRY(id->to_reference(interpreter)); auto initializer_result = TRY(interpreter.vm().named_evaluation_if_anonymous_function(*init, id->string())); VERIFY(!initializer_result.is_empty()); @@ -3039,7 +3039,7 @@ Completion VariableDeclaration::execute(Interpreter& interpreter) const else return reference.initialize_referenced_binding(vm, initializer_result); }, - [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { + [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { auto initializer_result = TRY(init->execute(interpreter)).release_value(); Environment* environment = m_declaration_kind == DeclarationKind::Var ? nullptr : interpreter.lexical_environment(); @@ -3047,8 +3047,8 @@ Completion VariableDeclaration::execute(Interpreter& interpreter) const return vm.binding_initialization(pattern, initializer_result, environment); })); } else if (m_declaration_kind != DeclarationKind::Var) { - VERIFY(declarator.target().has>()); - auto& identifier = declarator.target().get>(); + VERIFY(declarator.target().has>()); + auto& identifier = declarator.target().get>(); auto reference = TRY(identifier->to_reference(interpreter)); TRY(reference.initialize_referenced_binding(vm, js_undefined())); } @@ -3068,10 +3068,10 @@ ThrowCompletionOr VariableDeclaration::for_each_bound_name(ThrowCompletion { for (auto const& entry : declarations()) { TRY(entry.target().visit( - [&](NonnullRefPtr const& id) { + [&](NonnullRefPtr const& id) { return callback(id->string()); }, - [&](NonnullRefPtr const& binding) { + [&](NonnullRefPtr const& binding) { return binding->for_each_bound_name([&](auto const& name) { return callback(name); }); @@ -3112,10 +3112,10 @@ Completion UsingDeclaration::execute(Interpreter& interpreter) const auto& vm = interpreter.vm(); for (auto& declarator : m_declarations) { - VERIFY(declarator.target().has>()); + VERIFY(declarator.target().has>()); VERIFY(declarator.init()); - auto& id = declarator.target().get>(); + auto& id = declarator.target().get>(); // 2. ReturnIfAbrupt(next). auto reference = TRY(id->to_reference(interpreter)); @@ -3131,8 +3131,8 @@ Completion UsingDeclaration::execute(Interpreter& interpreter) const ThrowCompletionOr UsingDeclaration::for_each_bound_name(ThrowCompletionOrVoidCallback&& callback) const { for (auto const& entry : m_declarations) { - VERIFY(entry.target().has>()); - TRY(callback(entry.target().get>()->string())); + VERIFY(entry.target().has>()); + TRY(callback(entry.target().get>()->string())); } return {}; @@ -3349,24 +3349,24 @@ ThrowCompletionOr OptionalChain::to_reference_ return ReferenceAndValue { {}, js_undefined() }; auto expression = reference.visit( - [&](Call const& call) -> NonnullRefPtr { + [&](Call const& call) -> NonnullRefPtr { return CallExpression::create(source_range(), create_ast_node(source_range(), base_reference, base), call.arguments); }, - [&](ComputedReference const& ref) -> NonnullRefPtr { + [&](ComputedReference const& ref) -> NonnullRefPtr { return create_ast_node(source_range(), create_ast_node(source_range(), base_reference, base), ref.expression, true); }, - [&](MemberReference const& ref) -> NonnullRefPtr { + [&](MemberReference const& ref) -> NonnullRefPtr { return create_ast_node(source_range(), create_ast_node(source_range(), base_reference, base), ref.identifier, false); }, - [&](PrivateMemberReference const& ref) -> NonnullRefPtr { + [&](PrivateMemberReference const& ref) -> NonnullRefPtr { return create_ast_node(source_range(), create_ast_node(source_range(), base_reference, base), ref.private_identifier, @@ -3945,7 +3945,7 @@ void CatchClause::dump(int indent) const else outln("CatchClause ({})", parameter); }, - [&](NonnullRefPtr const& pattern) { + [&](NonnullRefPtr const& pattern) { outln("CatchClause"); print_indent(indent); outln("(Parameter)"); @@ -3981,7 +3981,7 @@ Completion TryStatement::execute(Interpreter& interpreter) const // a. Perform ! catchEnv.CreateMutableBinding(argName, false). MUST(catch_environment->create_mutable_binding(vm, parameter, false)); }, - [&](NonnullRefPtr const& pattern) { + [&](NonnullRefPtr const& pattern) { // 3. For each element argName of the BoundNames of CatchParameter, do pattern->for_each_bound_name([&](auto& name) { // a. Perform ! catchEnv.CreateMutableBinding(argName, false). @@ -3997,7 +3997,7 @@ Completion TryStatement::execute(Interpreter& interpreter) const [&](DeprecatedFlyString const& parameter) { return catch_environment->initialize_binding(vm, parameter, thrown_value, Environment::InitializeBindingHint::Normal); }, - [&](NonnullRefPtr const& pattern) { + [&](NonnullRefPtr const& pattern) { return vm.binding_initialization(pattern, thrown_value, catch_environment); }); @@ -4119,9 +4119,9 @@ Completion SwitchStatement::execute_impl(Interpreter& interpreter) const return js_undefined(); } - NonnullRefPtrVector case_clauses_1; - NonnullRefPtrVector case_clauses_2; - RefPtr default_clause; + NonnullRefPtrVector case_clauses_1; + NonnullRefPtrVector case_clauses_2; + RefPtr default_clause; for (auto const& switch_case : m_cases) { if (!switch_case.test()) default_clause = switch_case; @@ -4524,17 +4524,17 @@ ThrowCompletionOr ScopeNode::for_each_function_hoistable_with_annexB_exten return {}; } -void ScopeNode::add_lexical_declaration(NonnullRefPtr declaration) +void ScopeNode::add_lexical_declaration(NonnullRefPtr declaration) { m_lexical_declarations.append(move(declaration)); } -void ScopeNode::add_var_scoped_declaration(NonnullRefPtr declaration) +void ScopeNode::add_var_scoped_declaration(NonnullRefPtr declaration) { m_var_declarations.append(move(declaration)); } -void ScopeNode::add_hoisted_function(NonnullRefPtr declaration) +void ScopeNode::add_hoisted_function(NonnullRefPtr declaration) { m_functions_hoistable_with_annexB_extension.append(move(declaration)); } @@ -4967,12 +4967,12 @@ DeprecatedString SourceRange::filename() const return code->filename().to_deprecated_string(); } -NonnullRefPtr CallExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) +NonnullRefPtr CallExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) { return ASTNodeWithTailArray::create(arguments.size(), move(source_range), move(callee), arguments); } -NonnullRefPtr NewExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) +NonnullRefPtr NewExpression::create(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) { return ASTNodeWithTailArray::create(arguments.size(), move(source_range), move(callee), arguments); } diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index a620205f7fb..e73999e84aa 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2020-2022, Linus Groh * Copyright (c) 2021-2022, David Tuin * @@ -97,7 +97,7 @@ private: // NOTE: These members are carefully ordered so that `m_start_offset` is packed with the padding after RefCounted::m_ref_count. // This creates a 4-byte padding hole after `m_end_offset` which is used to pack subclasses better. u32 m_start_offset { 0 }; - RefPtr m_source_code; + RefPtr m_source_code; u32 m_end_offset { 0 }; }; @@ -152,7 +152,7 @@ public: // 14.13 Labelled Statements, https://tc39.es/ecma262/#sec-labelled-statements class LabelledStatement : public Statement { public: - LabelledStatement(SourceRange source_range, DeprecatedFlyString label, NonnullRefPtr labelled_item) + LabelledStatement(SourceRange source_range, DeprecatedFlyString label, NonnullRefPtr labelled_item) : Statement(source_range) , m_label(move(label)) , m_labelled_item(move(labelled_item)) @@ -166,14 +166,13 @@ public: DeprecatedFlyString const& label() const { return m_label; } DeprecatedFlyString& label() { return m_label; } - NonnullRefPtr const& labelled_item() const { return m_labelled_item; } - NonnullRefPtr& labelled_item() { return m_labelled_item; } + NonnullRefPtr const& labelled_item() const { return m_labelled_item; } private: virtual bool is_labelled_statement() const final { return true; } DeprecatedFlyString m_label; - NonnullRefPtr m_labelled_item; + NonnullRefPtr m_labelled_item; }; class LabelableStatement : public Statement { @@ -219,7 +218,7 @@ public: class ExpressionStatement final : public Statement { public: - ExpressionStatement(SourceRange source_range, NonnullRefPtr expression) + ExpressionStatement(SourceRange source_range, NonnullRefPtr expression) : Statement(source_range) , m_expression(move(expression)) { @@ -234,7 +233,7 @@ public: private: virtual bool is_expression_statement() const override { return true; } - NonnullRefPtr m_expression; + NonnullRefPtr m_expression; }; template @@ -275,7 +274,7 @@ public: m_children.append(move(child)); return static_cast(m_children.last()); } - void append(NonnullRefPtr child) + void append(NonnullRefPtr child) { m_children.append(move(child)); } @@ -288,15 +287,15 @@ public: m_functions_hoistable_with_annexB_extension.shrink_to_fit(); } - NonnullRefPtrVector const& children() const { return m_children; } + NonnullRefPtrVector const& children() const { return m_children; } virtual void dump(int indent) const override; virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; Completion evaluate_statements(Interpreter&) const; - void add_var_scoped_declaration(NonnullRefPtr variables); - void add_lexical_declaration(NonnullRefPtr variables); - void add_hoisted_function(NonnullRefPtr declaration); + void add_var_scoped_declaration(NonnullRefPtr variables); + void add_lexical_declaration(NonnullRefPtr variables); + void add_hoisted_function(NonnullRefPtr declaration); [[nodiscard]] bool has_lexical_declarations() const { return !m_lexical_declarations.is_empty(); } [[nodiscard]] bool has_var_declarations() const { return !m_var_declarations.is_empty(); } @@ -325,11 +324,11 @@ protected: private: virtual bool is_scope_node() const final { return true; } - NonnullRefPtrVector m_children; - NonnullRefPtrVector m_lexical_declarations; - NonnullRefPtrVector m_var_declarations; + NonnullRefPtrVector m_children; + NonnullRefPtrVector m_lexical_declarations; + NonnullRefPtrVector m_var_declarations; - NonnullRefPtrVector m_functions_hoistable_with_annexB_extension; + NonnullRefPtrVector m_functions_hoistable_with_annexB_extension; }; // ImportEntry Record, https://tc39.es/ecma262/#table-importentry-record-fields @@ -375,7 +374,6 @@ public: bool has_bound_name(DeprecatedFlyString const& name) const; Vector const& entries() const { return m_entries; } ModuleRequest const& module_request() const { return m_module_request; } - ModuleRequest& module_request() { return m_module_request; } private: ModuleRequest m_module_request; @@ -453,7 +451,7 @@ class ExportStatement final : public Statement { public: static DeprecatedFlyString local_name_for_default; - ExportStatement(SourceRange source_range, RefPtr statement, Vector entries, bool is_default_export, ModuleRequest module_request) + ExportStatement(SourceRange source_range, RefPtr statement, Vector entries, bool is_default_export, ModuleRequest module_request) : Statement(source_range) , m_statement(move(statement)) , m_entries(move(entries)) @@ -483,14 +481,14 @@ public: return *m_statement; } - ModuleRequest& module_request() + ModuleRequest const& module_request() const { VERIFY(!m_module_request.module_specifier.is_null()); return m_module_request; } private: - RefPtr m_statement; + RefPtr m_statement; Vector m_entries; bool m_is_default_export { false }; ModuleRequest m_module_request; @@ -516,23 +514,23 @@ public: Type type() const { return m_type; } - void append_import(NonnullRefPtr import_statement) + void append_import(NonnullRefPtr import_statement) { m_imports.append(import_statement); append(import_statement); } - void append_export(NonnullRefPtr export_statement) + void append_export(NonnullRefPtr export_statement) { m_exports.append(export_statement); append(export_statement); } - NonnullRefPtrVector const& imports() const { return m_imports; } - NonnullRefPtrVector const& exports() const { return m_exports; } + NonnullRefPtrVector const& imports() const { return m_imports; } + NonnullRefPtrVector const& exports() const { return m_exports; } - NonnullRefPtrVector& imports() { return m_imports; } - NonnullRefPtrVector& exports() { return m_exports; } + NonnullRefPtrVector& imports() { return m_imports; } + NonnullRefPtrVector& exports() { return m_exports; } bool has_top_level_await() const { return m_has_top_level_await; } void set_has_top_level_await() { m_has_top_level_await = true; } @@ -545,8 +543,8 @@ private: bool m_is_strict_mode { false }; Type m_type { Type::Script }; - NonnullRefPtrVector m_imports; - NonnullRefPtrVector m_exports; + NonnullRefPtrVector m_imports; + NonnullRefPtrVector m_exports; bool m_has_top_level_await { false }; }; @@ -618,9 +616,9 @@ struct BindingPattern : RefCounted { // This covers both BindingProperty and BindingElement, hence the more generic name struct BindingEntry { // If this entry represents a BindingElement, then name will be Empty - Variant, NonnullRefPtr, Empty> name {}; - Variant, NonnullRefPtr, NonnullRefPtr, Empty> alias {}; - RefPtr initializer {}; + Variant, NonnullRefPtr, Empty> name {}; + Variant, NonnullRefPtr, NonnullRefPtr, Empty> alias {}; + RefPtr initializer {}; bool is_rest { false }; bool is_elision() const { return name.has() && alias.has(); } @@ -642,8 +640,8 @@ struct BindingPattern : RefCounted { }; struct FunctionParameter { - Variant> binding; - RefPtr default_value; + Variant> binding; + RefPtr default_value; bool is_rest { false }; }; @@ -661,7 +659,7 @@ public: FunctionKind kind() const { return m_kind; } protected: - FunctionNode(DeprecatedFlyString name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function) + FunctionNode(DeprecatedFlyString name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function) : m_name(move(name)) , m_source_text(move(source_text)) , m_body(move(body)) @@ -682,7 +680,7 @@ protected: private: DeprecatedFlyString m_name; DeprecatedString m_source_text; - NonnullRefPtr m_body; + NonnullRefPtr m_body; Vector const m_parameters; const i32 m_function_length; FunctionKind m_kind; @@ -698,7 +696,7 @@ class FunctionDeclaration final public: static bool must_have_name() { return true; } - FunctionDeclaration(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval) + FunctionDeclaration(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval) : Declaration(source_range) , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, false) { @@ -724,7 +722,7 @@ class FunctionExpression final public: static bool must_have_name() { return false; } - FunctionExpression(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function = false) + FunctionExpression(SourceRange source_range, DeprecatedFlyString const& name, DeprecatedString source_text, NonnullRefPtr body, Vector parameters, i32 function_length, FunctionKind kind, bool is_strict_mode, bool might_need_arguments_object, bool contains_direct_call_to_eval, bool is_arrow_function = false) : Expression(source_range) , FunctionNode(name, move(source_text), move(body), move(parameters), function_length, kind, is_strict_mode, might_need_arguments_object, contains_direct_call_to_eval, is_arrow_function) { @@ -755,7 +753,7 @@ public: class YieldExpression final : public Expression { public: - explicit YieldExpression(SourceRange source_range, RefPtr argument, bool is_yield_from) + explicit YieldExpression(SourceRange source_range, RefPtr argument, bool is_yield_from) : Expression(source_range) , m_argument(move(argument)) , m_is_yield_from(is_yield_from) @@ -770,13 +768,13 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - RefPtr m_argument; + RefPtr m_argument; bool m_is_yield_from { false }; }; class AwaitExpression final : public Expression { public: - explicit AwaitExpression(SourceRange source_range, NonnullRefPtr argument) + explicit AwaitExpression(SourceRange source_range, NonnullRefPtr argument) : Expression(source_range) , m_argument(move(argument)) { @@ -787,12 +785,12 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_argument; + NonnullRefPtr m_argument; }; class ReturnStatement final : public Statement { public: - explicit ReturnStatement(SourceRange source_range, RefPtr argument) + explicit ReturnStatement(SourceRange source_range, RefPtr argument) : Statement(source_range) , m_argument(move(argument)) { @@ -805,12 +803,12 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - RefPtr m_argument; + RefPtr m_argument; }; class IfStatement final : public Statement { public: - IfStatement(SourceRange source_range, NonnullRefPtr predicate, NonnullRefPtr consequent, RefPtr alternate) + IfStatement(SourceRange source_range, NonnullRefPtr predicate, NonnullRefPtr consequent, RefPtr alternate) : Statement(source_range) , m_predicate(move(predicate)) , m_consequent(move(consequent)) @@ -827,14 +825,14 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_predicate; - NonnullRefPtr m_consequent; - RefPtr m_alternate; + NonnullRefPtr m_predicate; + NonnullRefPtr m_consequent; + RefPtr m_alternate; }; class WhileStatement final : public IterationStatement { public: - WhileStatement(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr body) + WhileStatement(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr body) : IterationStatement(source_range) , m_test(move(test)) , m_body(move(body)) @@ -851,13 +849,13 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_labelled_evaluation(Bytecode::Generator&, Vector const&) const override; private: - NonnullRefPtr m_test; - NonnullRefPtr m_body; + NonnullRefPtr m_test; + NonnullRefPtr m_body; }; class DoWhileStatement final : public IterationStatement { public: - DoWhileStatement(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr body) + DoWhileStatement(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr body) : IterationStatement(source_range) , m_test(move(test)) , m_body(move(body)) @@ -874,13 +872,13 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_labelled_evaluation(Bytecode::Generator&, Vector const&) const override; private: - NonnullRefPtr m_test; - NonnullRefPtr m_body; + NonnullRefPtr m_test; + NonnullRefPtr m_body; }; class WithStatement final : public Statement { public: - WithStatement(SourceRange source_range, NonnullRefPtr object, NonnullRefPtr body) + WithStatement(SourceRange source_range, NonnullRefPtr object, NonnullRefPtr body) : Statement(source_range) , m_object(move(object)) , m_body(move(body)) @@ -895,13 +893,13 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_object; - NonnullRefPtr m_body; + NonnullRefPtr m_object; + NonnullRefPtr m_body; }; class ForStatement final : public IterationStatement { public: - ForStatement(SourceRange source_range, RefPtr init, RefPtr test, RefPtr update, NonnullRefPtr body) + ForStatement(SourceRange source_range, RefPtr init, RefPtr test, RefPtr update, NonnullRefPtr body) : IterationStatement(source_range) , m_init(move(init)) , m_test(move(test)) @@ -924,15 +922,15 @@ public: private: Completion for_body_evaluation(Interpreter&, Vector const&, size_t per_iteration_bindings_size) const; - RefPtr m_init; - RefPtr m_test; - RefPtr m_update; - NonnullRefPtr m_body; + RefPtr m_init; + RefPtr m_test; + RefPtr m_update; + NonnullRefPtr m_body; }; class ForInStatement final : public IterationStatement { public: - ForInStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) + ForInStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) : IterationStatement(source_range) , m_lhs(move(lhs)) , m_rhs(move(rhs)) @@ -951,14 +949,14 @@ public: virtual void dump(int indent) const override; private: - Variant, NonnullRefPtr> m_lhs; - NonnullRefPtr m_rhs; - NonnullRefPtr m_body; + Variant, NonnullRefPtr> m_lhs; + NonnullRefPtr m_rhs; + NonnullRefPtr m_body; }; class ForOfStatement final : public IterationStatement { public: - ForOfStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) + ForOfStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) : IterationStatement(source_range) , m_lhs(move(lhs)) , m_rhs(move(rhs)) @@ -977,14 +975,14 @@ public: virtual void dump(int indent) const override; private: - Variant, NonnullRefPtr> m_lhs; - NonnullRefPtr m_rhs; - NonnullRefPtr m_body; + Variant, NonnullRefPtr> m_lhs; + NonnullRefPtr m_rhs; + NonnullRefPtr m_body; }; class ForAwaitOfStatement final : public IterationStatement { public: - ForAwaitOfStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) + ForAwaitOfStatement(SourceRange source_range, Variant, NonnullRefPtr> lhs, NonnullRefPtr rhs, NonnullRefPtr body) : IterationStatement(source_range) , m_lhs(move(lhs)) , m_rhs(move(rhs)) @@ -997,9 +995,9 @@ public: virtual void dump(int indent) const override; private: - Variant, NonnullRefPtr> m_lhs; - NonnullRefPtr m_rhs; - NonnullRefPtr m_body; + Variant, NonnullRefPtr> m_lhs; + NonnullRefPtr m_rhs; + NonnullRefPtr m_body; }; enum class BinaryOp { @@ -1029,7 +1027,7 @@ enum class BinaryOp { class BinaryExpression final : public Expression { public: - BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) + BinaryExpression(SourceRange source_range, BinaryOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) : Expression(source_range) , m_op(op) , m_lhs(move(lhs)) @@ -1043,8 +1041,8 @@ public: private: BinaryOp m_op; - NonnullRefPtr m_lhs; - NonnullRefPtr m_rhs; + NonnullRefPtr m_lhs; + NonnullRefPtr m_rhs; }; enum class LogicalOp { @@ -1055,7 +1053,7 @@ enum class LogicalOp { class LogicalExpression final : public Expression { public: - LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) + LogicalExpression(SourceRange source_range, LogicalOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) : Expression(source_range) , m_op(op) , m_lhs(move(lhs)) @@ -1069,8 +1067,8 @@ public: private: LogicalOp m_op; - NonnullRefPtr m_lhs; - NonnullRefPtr m_rhs; + NonnullRefPtr m_lhs; + NonnullRefPtr m_rhs; }; enum class UnaryOp { @@ -1085,7 +1083,7 @@ enum class UnaryOp { class UnaryExpression final : public Expression { public: - UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr lhs) + UnaryExpression(SourceRange source_range, UnaryOp op, NonnullRefPtr lhs) : Expression(source_range) , m_op(op) , m_lhs(move(lhs)) @@ -1098,12 +1096,12 @@ public: private: UnaryOp m_op; - NonnullRefPtr m_lhs; + NonnullRefPtr m_lhs; }; class SequenceExpression final : public Expression { public: - SequenceExpression(SourceRange source_range, NonnullRefPtrVector expressions) + SequenceExpression(SourceRange source_range, NonnullRefPtrVector expressions) : Expression(source_range) , m_expressions(move(expressions)) { @@ -1115,7 +1113,7 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtrVector m_expressions; + NonnullRefPtrVector m_expressions; }; class Literal : public Expression { @@ -1314,7 +1312,7 @@ public: Setter, }; - ClassMethod(SourceRange source_range, NonnullRefPtr key, NonnullRefPtr function, Kind kind, bool is_static) + ClassMethod(SourceRange source_range, NonnullRefPtr key, NonnullRefPtr function, Kind kind, bool is_static) : ClassElement(source_range, is_static) , m_key(move(key)) , m_function(move(function)) @@ -1332,14 +1330,14 @@ public: private: virtual bool is_class_method() const override { return true; } - NonnullRefPtr m_key; - NonnullRefPtr m_function; + NonnullRefPtr m_key; + NonnullRefPtr m_function; Kind m_kind; }; class ClassField final : public ClassElement { public: - ClassField(SourceRange source_range, NonnullRefPtr key, RefPtr init, bool contains_direct_call_to_eval, bool is_static) + ClassField(SourceRange source_range, NonnullRefPtr key, RefPtr init, bool contains_direct_call_to_eval, bool is_static) : ClassElement(source_range, is_static) , m_key(move(key)) , m_initializer(move(init)) @@ -1348,8 +1346,8 @@ public: } Expression const& key() const { return *m_key; } - RefPtr const& initializer() const { return m_initializer; } - RefPtr& initializer() { return m_initializer; } + RefPtr const& initializer() const { return m_initializer; } + RefPtr& initializer() { return m_initializer; } virtual ElementKind class_element_kind() const override { return ElementKind::Field; } @@ -1358,8 +1356,8 @@ public: virtual Optional private_bound_identifier() const override; private: - NonnullRefPtr m_key; - RefPtr m_initializer; + NonnullRefPtr m_key; + RefPtr m_initializer; bool m_contains_direct_call_to_eval { false }; }; @@ -1397,7 +1395,7 @@ public: class ClassExpression final : public Expression { public: - ClassExpression(SourceRange source_range, DeprecatedString name, DeprecatedString source_text, RefPtr constructor, RefPtr super_class, NonnullRefPtrVector elements) + ClassExpression(SourceRange source_range, DeprecatedString name, DeprecatedString source_text, RefPtr constructor, RefPtr super_class, NonnullRefPtrVector elements) : Expression(source_range) , m_name(move(name)) , m_source_text(move(source_text)) @@ -1409,7 +1407,7 @@ public: StringView name() const { return m_name; } DeprecatedString const& source_text() const { return m_source_text; } - RefPtr constructor() const { return m_constructor; } + RefPtr constructor() const { return m_constructor; } virtual Completion execute(Interpreter&) const override; virtual void dump(int indent) const override; @@ -1424,14 +1422,14 @@ private: DeprecatedString m_name; DeprecatedString m_source_text; - RefPtr m_constructor; - RefPtr m_super_class; - NonnullRefPtrVector m_elements; + RefPtr m_constructor; + RefPtr m_super_class; + NonnullRefPtrVector m_elements; }; class ClassDeclaration final : public Declaration { public: - ClassDeclaration(SourceRange source_range, NonnullRefPtr class_expression) + ClassDeclaration(SourceRange source_range, NonnullRefPtr class_expression) : Declaration(source_range) , m_class_expression(move(class_expression)) { @@ -1452,12 +1450,12 @@ private: friend ExportStatement; - NonnullRefPtr m_class_expression; + NonnullRefPtr m_class_expression; }; class SpreadExpression final : public Expression { public: - explicit SpreadExpression(SourceRange source_range, NonnullRefPtr target) + explicit SpreadExpression(SourceRange source_range, NonnullRefPtr target) : Expression(source_range) , m_target(move(target)) { @@ -1468,7 +1466,7 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_target; + NonnullRefPtr m_target; }; class ThisExpression final : public Expression { @@ -1483,7 +1481,7 @@ public: }; struct CallExpressionArgument { - NonnullRefPtr value; + NonnullRefPtr value; bool is_spread; }; @@ -1493,7 +1491,7 @@ class CallExpression : public ASTNodeWithTailArray create(SourceRange, NonnullRefPtr callee, ReadonlySpan arguments); + static NonnullRefPtr create(SourceRange, NonnullRefPtr callee, ReadonlySpan arguments); virtual Completion execute(Interpreter&) const override; virtual void dump(int indent) const override; @@ -1504,7 +1502,7 @@ public: ReadonlySpan arguments() const { return tail_span(); } protected: - CallExpression(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) + CallExpression(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) : ASTNodeWithTailArray(move(source_range), arguments) , m_callee(move(callee)) { @@ -1523,21 +1521,21 @@ protected: Completion throw_type_error_for_callee(Interpreter&, Value callee_value, StringView call_type) const; Optional expression_string() const; - NonnullRefPtr m_callee; + NonnullRefPtr m_callee; }; class NewExpression final : public CallExpression { friend class ASTNodeWithTailArray; public: - static NonnullRefPtr create(SourceRange, NonnullRefPtr callee, ReadonlySpan arguments); + static NonnullRefPtr create(SourceRange, NonnullRefPtr callee, ReadonlySpan arguments); virtual Completion execute(Interpreter&) const override; virtual bool is_new_expression() const override { return true; } private: - NewExpression(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) + NewExpression(SourceRange source_range, NonnullRefPtr callee, ReadonlySpan arguments) : CallExpression(move(source_range), move(callee), arguments) { } @@ -1599,7 +1597,7 @@ enum class AssignmentOp { class AssignmentExpression final : public Expression { public: - AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) + AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) : Expression(source_range) , m_op(op) , m_lhs(move(lhs)) @@ -1607,7 +1605,7 @@ public: { } - AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) + AssignmentExpression(SourceRange source_range, AssignmentOp op, NonnullRefPtr lhs, NonnullRefPtr rhs) : Expression(source_range) , m_op(op) , m_lhs(move(lhs)) @@ -1621,8 +1619,8 @@ public: private: AssignmentOp m_op; - Variant, NonnullRefPtr> m_lhs; - NonnullRefPtr m_rhs; + Variant, NonnullRefPtr> m_lhs; + NonnullRefPtr m_rhs; }; enum class UpdateOp { @@ -1632,7 +1630,7 @@ enum class UpdateOp { class UpdateExpression final : public Expression { public: - UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr argument, bool prefixed = false) + UpdateExpression(SourceRange source_range, UpdateOp op, NonnullRefPtr argument, bool prefixed = false) : Expression(source_range) , m_op(op) , m_argument(move(argument)) @@ -1648,7 +1646,7 @@ private: virtual bool is_update_expression() const override { return true; } UpdateOp m_op; - NonnullRefPtr m_argument; + NonnullRefPtr m_argument; bool m_prefixed; }; @@ -1660,20 +1658,20 @@ enum class DeclarationKind { class VariableDeclarator final : public ASTNode { public: - VariableDeclarator(SourceRange source_range, NonnullRefPtr id) + VariableDeclarator(SourceRange source_range, NonnullRefPtr id) : ASTNode(source_range) , m_target(move(id)) { } - VariableDeclarator(SourceRange source_range, NonnullRefPtr target, RefPtr init) + VariableDeclarator(SourceRange source_range, NonnullRefPtr target, RefPtr init) : ASTNode(source_range) , m_target(move(target)) , m_init(move(init)) { } - VariableDeclarator(SourceRange source_range, Variant, NonnullRefPtr> target, RefPtr init) + VariableDeclarator(SourceRange source_range, Variant, NonnullRefPtr> target, RefPtr init) : ASTNode(source_range) , m_target(move(target)) , m_init(move(init)) @@ -1687,13 +1685,13 @@ public: virtual void dump(int indent) const override; private: - Variant, NonnullRefPtr> m_target; - RefPtr m_init; + Variant, NonnullRefPtr> m_target; + RefPtr m_init; }; class VariableDeclaration final : public Declaration { public: - VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, NonnullRefPtrVector declarations) + VariableDeclaration(SourceRange source_range, DeclarationKind declaration_kind, NonnullRefPtrVector declarations) : Declaration(source_range) , m_declaration_kind(declaration_kind) , m_declarations(move(declarations)) @@ -1706,7 +1704,7 @@ public: virtual void dump(int indent) const override; virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; - NonnullRefPtrVector const& declarations() const { return m_declarations; } + NonnullRefPtrVector const& declarations() const { return m_declarations; } virtual ThrowCompletionOr for_each_bound_name(ThrowCompletionOrVoidCallback&& callback) const override; @@ -1718,12 +1716,12 @@ private: virtual bool is_variable_declaration() const override { return true; } DeclarationKind m_declaration_kind; - NonnullRefPtrVector m_declarations; + NonnullRefPtrVector m_declarations; }; class UsingDeclaration final : public Declaration { public: - UsingDeclaration(SourceRange source_range, NonnullRefPtrVector declarations) + UsingDeclaration(SourceRange source_range, NonnullRefPtrVector declarations) : Declaration(move(source_range)) , m_declarations(move(declarations)) { @@ -1738,10 +1736,10 @@ public: virtual bool is_lexical_declaration() const override { return true; } - NonnullRefPtrVector const& declarations() const { return m_declarations; } + NonnullRefPtrVector const& declarations() const { return m_declarations; } private: - NonnullRefPtrVector m_declarations; + NonnullRefPtrVector m_declarations; }; class ObjectProperty final : public ASTNode { @@ -1754,7 +1752,7 @@ public: ProtoSetter, }; - ObjectProperty(SourceRange source_range, NonnullRefPtr key, RefPtr value, Type property_type, bool is_method) + ObjectProperty(SourceRange source_range, NonnullRefPtr key, RefPtr value, Type property_type, bool is_method) : ASTNode(source_range) , m_property_type(property_type) , m_is_method(is_method) @@ -1779,8 +1777,8 @@ public: private: Type m_property_type; bool m_is_method { false }; - NonnullRefPtr m_key; - RefPtr m_value; + NonnullRefPtr m_key; + RefPtr m_value; }; class ObjectExpression final : public Expression { @@ -1803,13 +1801,13 @@ private: class ArrayExpression final : public Expression { public: - ArrayExpression(SourceRange source_range, Vector> elements) + ArrayExpression(SourceRange source_range, Vector> elements) : Expression(source_range) , m_elements(move(elements)) { } - Vector> const& elements() const { return m_elements; } + Vector> const& elements() const { return m_elements; } virtual Completion execute(Interpreter&) const override; virtual void dump(int indent) const override; @@ -1818,18 +1816,18 @@ public: private: virtual bool is_array_expression() const override { return true; } - Vector> m_elements; + Vector> m_elements; }; class TemplateLiteral final : public Expression { public: - TemplateLiteral(SourceRange source_range, NonnullRefPtrVector expressions) + TemplateLiteral(SourceRange source_range, NonnullRefPtrVector expressions) : Expression(source_range) , m_expressions(move(expressions)) { } - TemplateLiteral(SourceRange source_range, NonnullRefPtrVector expressions, NonnullRefPtrVector raw_strings) + TemplateLiteral(SourceRange source_range, NonnullRefPtrVector expressions, NonnullRefPtrVector raw_strings) : Expression(source_range) , m_expressions(move(expressions)) , m_raw_strings(move(raw_strings)) @@ -1840,17 +1838,17 @@ public: virtual void dump(int indent) const override; virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; - NonnullRefPtrVector const& expressions() const { return m_expressions; } - NonnullRefPtrVector const& raw_strings() const { return m_raw_strings; } + NonnullRefPtrVector const& expressions() const { return m_expressions; } + NonnullRefPtrVector const& raw_strings() const { return m_raw_strings; } private: - NonnullRefPtrVector const m_expressions; - NonnullRefPtrVector const m_raw_strings; + NonnullRefPtrVector const m_expressions; + NonnullRefPtrVector const m_raw_strings; }; class TaggedTemplateLiteral final : public Expression { public: - TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr tag, NonnullRefPtr template_literal) + TaggedTemplateLiteral(SourceRange source_range, NonnullRefPtr tag, NonnullRefPtr template_literal) : Expression(source_range) , m_tag(move(tag)) , m_template_literal(move(template_literal)) @@ -1864,14 +1862,14 @@ public: ThrowCompletionOr get_template_object(Interpreter&) const; private: - NonnullRefPtr const m_tag; - NonnullRefPtr const m_template_literal; + NonnullRefPtr const m_tag; + NonnullRefPtr const m_template_literal; mutable HashMap> m_cached_values; }; class MemberExpression final : public Expression { public: - MemberExpression(SourceRange source_range, NonnullRefPtr object, NonnullRefPtr property, bool computed = false) + MemberExpression(SourceRange source_range, NonnullRefPtr object, NonnullRefPtr property, bool computed = false) : Expression(source_range) , m_computed(computed) , m_object(move(object)) @@ -1896,8 +1894,8 @@ private: virtual bool is_member_expression() const override { return true; } bool m_computed { false }; - NonnullRefPtr m_object; - NonnullRefPtr m_property; + NonnullRefPtr m_object; + NonnullRefPtr m_property; }; class OptionalChain final : public Expression { @@ -1912,21 +1910,21 @@ public: Mode mode; }; struct ComputedReference { - NonnullRefPtr expression; + NonnullRefPtr expression; Mode mode; }; struct MemberReference { - NonnullRefPtr identifier; + NonnullRefPtr identifier; Mode mode; }; struct PrivateMemberReference { - NonnullRefPtr private_identifier; + NonnullRefPtr private_identifier; Mode mode; }; using Reference = Variant; - OptionalChain(SourceRange source_range, NonnullRefPtr base, Vector references) + OptionalChain(SourceRange source_range, NonnullRefPtr base, Vector references) : Expression(source_range) , m_base(move(base)) , m_references(move(references)) @@ -1944,7 +1942,7 @@ private: }; ThrowCompletionOr to_reference_and_value(Interpreter&) const; - NonnullRefPtr m_base; + NonnullRefPtr m_base; Vector m_references; }; @@ -1971,7 +1969,7 @@ private: class ImportCall final : public Expression { public: - ImportCall(SourceRange source_range, NonnullRefPtr specifier, RefPtr options) + ImportCall(SourceRange source_range, NonnullRefPtr specifier, RefPtr options) : Expression(source_range) , m_specifier(move(specifier)) , m_options(move(options)) @@ -1984,13 +1982,13 @@ public: private: virtual bool is_import_call() const override { return true; } - NonnullRefPtr m_specifier; - RefPtr m_options; + NonnullRefPtr m_specifier; + RefPtr m_options; }; class ConditionalExpression final : public Expression { public: - ConditionalExpression(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr consequent, NonnullRefPtr alternate) + ConditionalExpression(SourceRange source_range, NonnullRefPtr test, NonnullRefPtr consequent, NonnullRefPtr alternate) : Expression(source_range) , m_test(move(test)) , m_consequent(move(consequent)) @@ -2003,21 +2001,21 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_test; - NonnullRefPtr m_consequent; - NonnullRefPtr m_alternate; + NonnullRefPtr m_test; + NonnullRefPtr m_consequent; + NonnullRefPtr m_alternate; }; class CatchClause final : public ASTNode { public: - CatchClause(SourceRange source_range, DeprecatedFlyString parameter, NonnullRefPtr body) + CatchClause(SourceRange source_range, DeprecatedFlyString parameter, NonnullRefPtr body) : ASTNode(source_range) , m_parameter(move(parameter)) , m_body(move(body)) { } - CatchClause(SourceRange source_range, NonnullRefPtr parameter, NonnullRefPtr body) + CatchClause(SourceRange source_range, NonnullRefPtr parameter, NonnullRefPtr body) : ASTNode(source_range) , m_parameter(move(parameter)) , m_body(move(body)) @@ -2031,13 +2029,13 @@ public: virtual Completion execute(Interpreter&) const override; private: - Variant> m_parameter; - NonnullRefPtr m_body; + Variant> m_parameter; + NonnullRefPtr m_body; }; class TryStatement final : public Statement { public: - TryStatement(SourceRange source_range, NonnullRefPtr block, RefPtr handler, RefPtr finalizer) + TryStatement(SourceRange source_range, NonnullRefPtr block, RefPtr handler, RefPtr finalizer) : Statement(source_range) , m_block(move(block)) , m_handler(move(handler)) @@ -2054,14 +2052,14 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_block; - RefPtr m_handler; - RefPtr m_finalizer; + NonnullRefPtr m_block; + RefPtr m_handler; + RefPtr m_finalizer; }; class ThrowStatement final : public Statement { public: - explicit ThrowStatement(SourceRange source_range, NonnullRefPtr argument) + explicit ThrowStatement(SourceRange source_range, NonnullRefPtr argument) : Statement(source_range) , m_argument(move(argument)) { @@ -2074,12 +2072,12 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; private: - NonnullRefPtr m_argument; + NonnullRefPtr m_argument; }; class SwitchCase final : public ScopeNode { public: - SwitchCase(SourceRange source_range, RefPtr test) + SwitchCase(SourceRange source_range, RefPtr test) : ScopeNode(source_range) , m_test(move(test)) { @@ -2091,12 +2089,12 @@ public: virtual Completion execute(Interpreter&) const override; private: - RefPtr m_test; + RefPtr m_test; }; class SwitchStatement final : public ScopeNode { public: - SwitchStatement(SourceRange source_range, NonnullRefPtr discriminant) + SwitchStatement(SourceRange source_range, NonnullRefPtr discriminant) : ScopeNode(source_range) , m_discriminant(move(discriminant)) { @@ -2108,11 +2106,11 @@ public: virtual Bytecode::CodeGenerationErrorOr generate_labelled_evaluation(Bytecode::Generator&, Vector const&) const; Completion execute_impl(Interpreter&) const; - void add_case(NonnullRefPtr switch_case) { m_cases.append(move(switch_case)); } + void add_case(NonnullRefPtr switch_case) { m_cases.append(move(switch_case)); } private: - NonnullRefPtr m_discriminant; - NonnullRefPtrVector m_cases; + NonnullRefPtr m_discriminant; + NonnullRefPtrVector m_cases; }; class BreakStatement final : public Statement { diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 8d7fa11fd18..0e7646704ae 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2023, Andreas Kling * Copyright (c) 2021, Linus Groh * Copyright (c) 2021, Gunnar Beutner * Copyright (c) 2021, Marcin Gasperowicz @@ -603,7 +603,7 @@ Bytecode::CodeGenerationErrorOr AssignmentExpression::generate_bytecode(By // AssignmentExpression : LeftHandSideExpression = AssignmentExpression return m_lhs.visit( // 1. If LeftHandSideExpression is neither an ObjectLiteral nor an ArrayLiteral, then - [&](NonnullRefPtr const& lhs) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& lhs) -> Bytecode::CodeGenerationErrorOr { // a. Let lref be the result of evaluating LeftHandSideExpression. // b. ReturnIfAbrupt(lref). Optional base_object_register; @@ -675,7 +675,7 @@ Bytecode::CodeGenerationErrorOr AssignmentExpression::generate_bytecode(By return {}; }, // 2. Let assignmentPattern be the AssignmentPattern that is covered by LeftHandSideExpression. - [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { // 3. Let rref be the result of evaluating AssignmentExpression. // 4. Let rval be ? GetValue(rref). TRY(m_rhs->generate_bytecode(generator)); @@ -691,8 +691,8 @@ Bytecode::CodeGenerationErrorOr AssignmentExpression::generate_bytecode(By }); } - VERIFY(m_lhs.has>()); - auto& lhs = m_lhs.get>(); + VERIFY(m_lhs.has>()); + auto& lhs = m_lhs.get>(); TRY(generator.emit_load_from_reference(lhs)); @@ -1219,11 +1219,11 @@ static Bytecode::CodeGenerationErrorOr generate_object_binding_pattern_byt for (auto& [name, alias, initializer, is_rest] : pattern.entries) { if (is_rest) { - VERIFY(name.has>()); + VERIFY(name.has>()); VERIFY(alias.has()); VERIFY(!initializer); - auto identifier = name.get>()->string(); + auto identifier = name.get>()->string(); auto interned_identifier = generator.intern_identifier(identifier); generator.emit_with_extra_register_slots(excluded_property_names.size(), value_reg, excluded_property_names); @@ -1234,8 +1234,8 @@ static Bytecode::CodeGenerationErrorOr generate_object_binding_pattern_byt Bytecode::StringTableIndex name_index; - if (name.has>()) { - auto identifier = name.get>()->string(); + if (name.has>()) { + auto identifier = name.get>()->string(); name_index = generator.intern_string(identifier); if (has_rest) { @@ -1248,7 +1248,7 @@ static Bytecode::CodeGenerationErrorOr generate_object_binding_pattern_byt generator.emit(value_reg); generator.emit(generator.intern_identifier(identifier)); } else { - auto expression = name.get>(); + auto expression = name.get>(); TRY(expression->generate_bytecode(generator)); if (has_rest) { @@ -1277,24 +1277,24 @@ static Bytecode::CodeGenerationErrorOr generate_object_binding_pattern_byt generator.switch_to_basic_block(if_not_undefined_block); } - if (alias.has>()) { - auto& binding_pattern = *alias.get>(); + if (alias.has>()) { + auto& binding_pattern = *alias.get>(); auto nested_value_reg = generator.allocate_register(); generator.emit(nested_value_reg); TRY(generate_binding_pattern_bytecode(generator, binding_pattern, initialization_mode, nested_value_reg)); } else if (alias.has()) { - if (name.has>()) { + if (name.has>()) { // This needs some sort of SetVariableByValue opcode, as it's a runtime binding return Bytecode::CodeGenerationError { - name.get>().ptr(), + name.get>().ptr(), "Unimplemented name/alias pair: Empty/Expression"sv, }; } - auto& identifier = name.get>()->string(); + auto& identifier = name.get>()->string(); generator.emit(generator.intern_identifier(identifier), initialization_mode); } else { - auto& identifier = alias.get>()->string(); + auto& identifier = alias.get>()->string(); generator.emit(generator.intern_identifier(identifier), initialization_mode); } } @@ -1340,18 +1340,18 @@ static Bytecode::CodeGenerationErrorOr generate_array_binding_pattern_byte // This element is an elision return {}; }, - [&](NonnullRefPtr const& identifier) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& identifier) -> Bytecode::CodeGenerationErrorOr { auto interned_index = generator.intern_identifier(identifier->string()); generator.emit(interned_index, initialization_mode); return {}; }, - [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { // Store the accumulator value in a permanent register auto target_reg = generator.allocate_register(); generator.emit(target_reg); return generate_binding_pattern_bytecode(generator, pattern, initialization_mode, target_reg); }, - [&](NonnullRefPtr const& expr) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& expr) -> Bytecode::CodeGenerationErrorOr { return generator.emit_store_to_reference(*expr); }); }; @@ -1487,11 +1487,11 @@ static Bytecode::CodeGenerationErrorOr assign_accumulator_to_variable_decl auto environment_mode = declaration.is_lexical_declaration() ? Bytecode::Op::EnvironmentMode::Lexical : Bytecode::Op::EnvironmentMode::Var; return declarator.target().visit( - [&](NonnullRefPtr const& id) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& id) -> Bytecode::CodeGenerationErrorOr { generator.emit(generator.intern_identifier(id->string()), initialization_mode, environment_mode); return {}; }, - [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const& pattern) -> Bytecode::CodeGenerationErrorOr { auto value_register = generator.allocate_register(); generator.emit(value_register); return generate_binding_pattern_bytecode(generator, pattern, initialization_mode, value_register); @@ -2224,7 +2224,7 @@ Bytecode::CodeGenerationErrorOr TryStatement::generate_bytecode(Bytecode:: } return {}; }, - [&](NonnullRefPtr const&) -> Bytecode::CodeGenerationErrorOr { + [&](NonnullRefPtr const&) -> Bytecode::CodeGenerationErrorOr { // FIXME: Implement this path when the above DeclarativeEnvironment issue is dealt with. return Bytecode::CodeGenerationError { this, @@ -2422,12 +2422,12 @@ struct ForInOfHeadEvaluationResult { bool is_destructuring { false }; LHSKind lhs_kind { LHSKind::Assignment }; }; -static Bytecode::CodeGenerationErrorOr for_in_of_head_evaluation(Bytecode::Generator& generator, IterationKind iteration_kind, Variant, NonnullRefPtr> const& lhs, NonnullRefPtr const& rhs) +static Bytecode::CodeGenerationErrorOr for_in_of_head_evaluation(Bytecode::Generator& generator, IterationKind iteration_kind, Variant, NonnullRefPtr> const& lhs, NonnullRefPtr const& rhs) { ForInOfHeadEvaluationResult result {}; bool entered_lexical_scope = false; - if (auto* ast_ptr = lhs.get_pointer>(); ast_ptr && is(**ast_ptr)) { + if (auto* ast_ptr = lhs.get_pointer>(); ast_ptr && is(**ast_ptr)) { // Runtime Semantics: ForInOfLoopEvaluation, for any of: // ForInOfStatement : for ( var ForBinding in Expression ) Statement // ForInOfStatement : for ( ForDeclaration in Expression ) Statement @@ -2435,7 +2435,7 @@ static Bytecode::CodeGenerationErrorOr for_in_of_he // ForInOfStatement : for ( ForDeclaration of AssignmentExpression ) Statement auto& variable_declaration = static_cast(**ast_ptr); - result.is_destructuring = variable_declaration.declarations().first().target().has>(); + result.is_destructuring = variable_declaration.declarations().first().target().has>(); result.lhs_kind = variable_declaration.is_lexical_declaration() ? LHSKind::LexicalBinding : LHSKind::VarBinding; // 1. Let oldEnv be the running execution context's LexicalEnvironment. @@ -2516,7 +2516,7 @@ static Bytecode::CodeGenerationErrorOr for_in_of_he } // 14.7.5.7 ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet [ , iteratorKind ] ), https://tc39.es/ecma262/#sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset -static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode::Generator& generator, ASTNode const& node, Variant, NonnullRefPtr> const& lhs, ASTNode const& body, ForInOfHeadEvaluationResult const& head_result, Vector const& label_set, Bytecode::BasicBlock& loop_end, Bytecode::BasicBlock& loop_update) +static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode::Generator& generator, ASTNode const& node, Variant, NonnullRefPtr> const& lhs, ASTNode const& body, ForInOfHeadEvaluationResult const& head_result, Vector const& label_set, Bytecode::BasicBlock& loop_end, Bytecode::BasicBlock& loop_update) { auto iterator_register = generator.allocate_register(); generator.emit(iterator_register); @@ -2579,14 +2579,14 @@ static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode: // 1. Let lhsRef be the result of evaluating lhs. (It may be evaluated repeatedly.) // NOTE: We're skipping all the completion stuff that the spec does, as the unwinding mechanism will take case of doing that. if (head_result.lhs_kind == LHSKind::VarBinding) { - auto& declaration = static_cast(*lhs.get>()); + auto& declaration = static_cast(*lhs.get>()); VERIFY(declaration.declarations().size() == 1); TRY(assign_accumulator_to_variable_declarator(generator, declaration.declarations().first(), declaration)); } else { - if (auto ptr = lhs.get_pointer>()) { + if (auto ptr = lhs.get_pointer>()) { TRY(generator.emit_store_to_reference(**ptr)); } else { - auto& binding_pattern = lhs.get>(); + auto& binding_pattern = lhs.get>(); auto value_register = generator.allocate_register(); generator.emit(value_register); TRY(generate_binding_pattern_bytecode(generator, *binding_pattern, Bytecode::Op::SetVariable::InitializationMode::Set, value_register)); @@ -2607,7 +2607,7 @@ static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode: // 14.7.5.4 Runtime Semantics: ForDeclarationBindingInstantiation, https://tc39.es/ecma262/#sec-runtime-semantics-fordeclarationbindinginstantiation // 1. Assert: environment is a declarative Environment Record. // NOTE: We just made it. - auto& variable_declaration = static_cast(*lhs.get>()); + auto& variable_declaration = static_cast(*lhs.get>()); // 2. For each element name of the BoundNames of ForBinding, do variable_declaration.for_each_bound_name([&](auto const& name) { auto identifier = generator.intern_identifier(name); @@ -2630,7 +2630,7 @@ static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode: if (!destructuring) { // 1. Assert: lhs binds a single name. // 2. Let lhsName be the sole element of BoundNames of lhs. - auto lhs_name = variable_declaration.declarations().first().target().get>()->string(); + auto lhs_name = variable_declaration.declarations().first().target().get>()->string(); // 3. Let lhsRef be ! ResolveBinding(lhsName). // NOTE: We're skipping all the completion stuff that the spec does, as the unwinding mechanism will take case of doing that. auto identifier = generator.intern_identifier(lhs_name); @@ -2660,9 +2660,9 @@ static Bytecode::CodeGenerationErrorOr for_in_of_body_evaluation(Bytecode: // 2. Assert: lhs is a ForDeclaration. // 3. Let status be Completion(ForDeclarationBindingInitialization of lhs with arguments nextValue and iterationEnv). if (head_result.lhs_kind == LHSKind::VarBinding || head_result.lhs_kind == LHSKind::LexicalBinding) { - auto& declaration = static_cast(*lhs.get>()); + auto& declaration = static_cast(*lhs.get>()); VERIFY(declaration.declarations().size() == 1); - auto& binding_pattern = declaration.declarations().first().target().get>(); + auto& binding_pattern = declaration.declarations().first().target().get>(); auto value_register = generator.allocate_register(); generator.emit(value_register); diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 2a9c0c4b75a..503048b651b 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -4,6 +4,7 @@ * Copyright (c) 2021-2022, David Tuin * Copyright (c) 2021, Ali Mohammad Pur * Copyright (c) 2021, Idan Horowitz + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -68,7 +69,7 @@ public: [&](DeprecatedFlyString const& name) { scope_pusher.m_forbidden_lexical_names.set(name); }, - [&](NonnullRefPtr const& binding_pattern) { + [&](NonnullRefPtr const& binding_pattern) { binding_pattern->for_each_bound_name([&](auto const& name) { scope_pusher.m_forbidden_lexical_names.set(name); }); @@ -87,7 +88,7 @@ public: return ScopePusher(parser, &node, ScopeLevel::NotTopLevel); } - static ScopePusher for_loop_scope(Parser& parser, RefPtr const& init) + static ScopePusher for_loop_scope(Parser& parser, RefPtr const& init) { ScopePusher scope_pusher(parser, nullptr, ScopeLevel::NotTopLevel); if (init && is(*init)) { @@ -102,7 +103,7 @@ public: return scope_pusher; } - static ScopePusher catch_scope(Parser& parser, RefPtr const& pattern, DeprecatedFlyString const& parameter) + static ScopePusher catch_scope(Parser& parser, RefPtr const& pattern, DeprecatedFlyString const& parameter) { ScopePusher scope_pusher(parser, nullptr, ScopeLevel::NotTopLevel); if (pattern) { @@ -125,7 +126,7 @@ public: return ScopePusher(parser, nullptr, ScopeLevel::NotTopLevel); } - void add_declaration(NonnullRefPtr declaration) + void add_declaration(NonnullRefPtr declaration) { if (declaration->is_lexical_declaration()) { declaration->for_each_bound_name([&](auto const& name) { @@ -184,7 +185,7 @@ public: m_function_names.set(function_name); if (!m_lexical_names.contains(function_name)) - m_functions_to_hoist.append(static_ptr_cast(declaration)); + m_functions_to_hoist.append(static_ptr_cast(declaration)); m_node->add_lexical_declaration(move(declaration)); } @@ -227,10 +228,10 @@ public: if (m_lexical_names.contains(function_declaration.name()) || m_forbidden_var_names.contains(function_declaration.name())) continue; if (is_top_level()) { - m_node->add_hoisted_function(move(m_functions_to_hoist[i])); + m_node->add_hoisted_function(move(m_functions_to_hoist.ptr_at(i))); } else { if (!m_parent_scope->m_lexical_names.contains(function_declaration.name()) && !m_parent_scope->m_function_names.contains(function_declaration.name())) - m_parent_scope->m_functions_to_hoist.append(move(m_functions_to_hoist[i])); + m_parent_scope->m_functions_to_hoist.append(move(m_functions_to_hoist.ptr_at(i))); } } @@ -259,7 +260,7 @@ public: } private: - void throw_identifier_declared(DeprecatedFlyString const& name, NonnullRefPtr const& declaration) + void throw_identifier_declared(DeprecatedFlyString const& name, NonnullRefPtr const& declaration) { m_parser.syntax_error(DeprecatedString::formatted("Identifier '{}' already declared", name), declaration->source_range().start); } @@ -277,7 +278,7 @@ private: HashTable m_forbidden_lexical_names; HashTable m_forbidden_var_names; - NonnullRefPtrVector m_functions_to_hoist; + NonnullRefPtrVector m_functions_to_hoist; Optional> m_function_parameters; @@ -589,7 +590,7 @@ void Parser::parse_module(Program& program) } } -NonnullRefPtr Parser::parse_declaration() +NonnullRefPtr Parser::parse_declaration() { auto rule_start = push_start(); if (m_state.current_token.type() == TokenType::Async && next_token().type() == TokenType::Function) @@ -617,7 +618,7 @@ NonnullRefPtr Parser::parse_declaration() } } -NonnullRefPtr Parser::parse_statement(AllowLabelledFunction allow_labelled_function) +NonnullRefPtr Parser::parse_statement(AllowLabelledFunction allow_labelled_function) { auto rule_start = push_start(); auto type = m_state.current_token.type(); @@ -724,7 +725,7 @@ static bool is_simple_parameter_list(Vector const& parameters }); } -RefPtr Parser::try_parse_arrow_function_expression(bool expect_parens, bool is_async) +RefPtr Parser::try_parse_arrow_function_expression(bool expect_parens, bool is_async) { if (is_async) VERIFY(match(TokenType::Async)); @@ -815,7 +816,7 @@ RefPtr Parser::try_parse_arrow_function_expression(bool expe bool contains_direct_call_to_eval = false; - auto function_body_result = [&]() -> RefPtr { + auto function_body_result = [&]() -> RefPtr { TemporaryChange change(m_state.in_arrow_function_context, true); TemporaryChange async_context_change(m_state.await_expression_is_valid, is_async); TemporaryChange in_class_static_init_block_change(m_state.in_class_static_init_block, false); @@ -837,9 +838,9 @@ RefPtr Parser::try_parse_arrow_function_expression(bool expe auto return_block = create_ast_node({ m_source_code, rule_start.position(), position() }); ScopePusher function_scope = ScopePusher::function_scope(*this, return_block, parameters); auto return_expression = parse_expression(2); - return_block->append({ m_source_code, rule_start.position(), position() }, move(return_expression)); + return_block->append({ m_source_code, rule_start.position(), position() }, move(return_expression)); if (m_state.strict_mode) - return_block->set_strict_mode(); + const_cast(*return_block).set_strict_mode(); contains_direct_call_to_eval = function_scope.contains_direct_call_to_eval(); return return_block; } @@ -873,7 +874,7 @@ RefPtr Parser::try_parse_arrow_function_expression(bool expe /* might_need_arguments_object */ false, contains_direct_call_to_eval, /* is_arrow_function */ true); } -RefPtr Parser::try_parse_labelled_statement(AllowLabelledFunction allow_function) +RefPtr Parser::try_parse_labelled_statement(AllowLabelledFunction allow_function) { { // NOTE: This is a fast path where we try to fail early to avoid the expensive save_state+load_state. @@ -924,7 +925,7 @@ RefPtr Parser::try_parse_labelled_statement(AllowLabelledFunc if (m_state.labels_in_scope.contains(identifier)) syntax_error(DeprecatedString::formatted("Label '{}' has already been declared", identifier)); - RefPtr labelled_item; + RefPtr labelled_item; auto is_iteration_statement = false; @@ -945,7 +946,7 @@ RefPtr Parser::try_parse_labelled_statement(AllowLabelledFunc // Extract the innermost statement from a potentially nested chain of LabelledStatements. auto statement = labelled_item; while (is(*statement)) - statement = static_cast(*statement).labelled_item(); + statement = static_cast(*statement).labelled_item(); if (is(*statement)) is_iteration_statement = true; } @@ -960,7 +961,7 @@ RefPtr Parser::try_parse_labelled_statement(AllowLabelledFunc return create_ast_node({ m_source_code, rule_start.position(), position() }, identifier, labelled_item.release_nonnull()); } -RefPtr Parser::try_parse_new_target_expression() +RefPtr Parser::try_parse_new_target_expression() { // Optimization which skips the save/load state. if (next_token().type() != TokenType::Period) @@ -985,7 +986,7 @@ RefPtr Parser::try_parse_new_target_expression() return create_ast_node({ m_source_code, rule_start.position(), position() }, MetaProperty::Type::NewTarget); } -RefPtr Parser::try_parse_import_meta_expression() +RefPtr Parser::try_parse_import_meta_expression() { // Optimization which skips the save/load state. if (next_token().type() != TokenType::Period) @@ -1010,7 +1011,7 @@ RefPtr Parser::try_parse_import_meta_expression() return create_ast_node({ m_source_code, rule_start.position(), position() }, MetaProperty::Type::ImportMeta); } -NonnullRefPtr Parser::parse_import_call() +NonnullRefPtr Parser::parse_import_call() { auto rule_start = push_start(); @@ -1024,7 +1025,7 @@ NonnullRefPtr Parser::parse_import_call() consume(TokenType::ParenOpen); auto argument = parse_expression(2); - RefPtr options; + RefPtr options; if (match(TokenType::Comma)) { consume(TokenType::Comma); @@ -1042,13 +1043,13 @@ NonnullRefPtr Parser::parse_import_call() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(argument), move(options)); } -NonnullRefPtr Parser::parse_class_declaration() +NonnullRefPtr Parser::parse_class_declaration() { auto rule_start = push_start(); return create_ast_node({ m_source_code, rule_start.position(), position() }, parse_class_expression(true)); } -NonnullRefPtr Parser::parse_class_expression(bool expect_class_name) +NonnullRefPtr Parser::parse_class_expression(bool expect_class_name) { auto rule_start = push_start(); // Classes are always in strict mode. @@ -1056,9 +1057,9 @@ NonnullRefPtr Parser::parse_class_expression(bool expect_class_ consume(TokenType::Class); - NonnullRefPtrVector elements; - RefPtr super_class; - RefPtr constructor; + NonnullRefPtrVector elements; + RefPtr super_class; + RefPtr constructor; HashTable found_private_names; DeprecatedFlyString class_name = expect_class_name || match_identifier() || match(TokenType::Yield) || match(TokenType::Await) @@ -1102,7 +1103,7 @@ NonnullRefPtr Parser::parse_class_expression(bool expect_class_ }; while (!done() && !match(TokenType::CurlyClose)) { - RefPtr property_key; + RefPtr property_key; bool is_static = false; bool is_constructor = false; bool is_generator = false; @@ -1313,7 +1314,7 @@ NonnullRefPtr Parser::parse_class_expression(bool expect_class_ if (name == "constructor"sv) syntax_error("Class cannot have field named 'constructor'"); - RefPtr initializer; + RefPtr initializer; bool contains_direct_call_to_eval = false; if (match(TokenType::Equals)) { @@ -1389,7 +1390,7 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression() if (match_unary_prefixed_expression()) return { parse_unary_prefixed_expression() }; - auto try_arrow_function_parse_or_fail = [this](Position const& position, bool expect_paren, bool is_async = false) -> RefPtr { + auto try_arrow_function_parse_or_fail = [this](Position const& position, bool expect_paren, bool is_async = false) -> RefPtr { if (try_parse_arrow_function_expression_failed_at_position(position)) return nullptr; auto arrow_function = try_parse_arrow_function_expression(expect_paren, is_async); @@ -1411,7 +1412,7 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression() auto expression = parse_expression(0); consume(TokenType::ParenClose); if (is(*expression)) { - auto& function = static_cast(*expression); + auto& function = static_cast(*expression); if (function.kind() == FunctionKind::Generator && function.name() == "yield"sv) syntax_error("function is not allowed to be called 'yield' in this context", function.source_range().start); if (function.kind() == FunctionKind::Async && function.name() == "await"sv) @@ -1534,7 +1535,7 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression() return { create_ast_node({ m_source_code, rule_start.position(), position() }) }; } -NonnullRefPtr Parser::parse_regexp_literal() +NonnullRefPtr Parser::parse_regexp_literal() { auto rule_start = push_start(); auto pattern = consume().value(); @@ -1577,7 +1578,7 @@ static bool is_simple_assignment_target(Expression const& expression, bool allow return is(expression) || is(expression) || (allow_web_reality_call_expression && is(expression)); } -NonnullRefPtr Parser::parse_unary_prefixed_expression() +NonnullRefPtr Parser::parse_unary_prefixed_expression() { auto rule_start = push_start(); auto precedence = g_operator_precedence.get(m_state.current_token.type()); @@ -1591,7 +1592,7 @@ NonnullRefPtr Parser::parse_unary_prefixed_expression() syntax_error(DeprecatedString::formatted("Right-hand side of prefix increment operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start); if (m_state.strict_mode && is(*rhs)) { - auto& identifier = static_cast(*rhs); + auto& identifier = static_cast(*rhs); auto& name = identifier.string(); check_identifier_name_for_assignment_validity(name); } @@ -1606,7 +1607,7 @@ NonnullRefPtr Parser::parse_unary_prefixed_expression() syntax_error(DeprecatedString::formatted("Right-hand side of prefix decrement operator must be identifier or member expression, got {}", rhs->class_name()), rhs_start); if (m_state.strict_mode && is(*rhs)) { - auto& identifier = static_cast(*rhs); + auto& identifier = static_cast(*rhs); auto& name = identifier.string(); check_identifier_name_for_assignment_validity(name); } @@ -1655,7 +1656,7 @@ NonnullRefPtr Parser::parse_unary_prefixed_expression() } } -NonnullRefPtr Parser::parse_property_key() +NonnullRefPtr Parser::parse_property_key() { auto rule_start = push_start(); if (match(TokenType::StringLiteral)) { @@ -1676,7 +1677,7 @@ NonnullRefPtr Parser::parse_property_key() } } -NonnullRefPtr Parser::parse_object_expression() +NonnullRefPtr Parser::parse_object_expression() { auto rule_start = push_start(); consume(TokenType::CurlyOpen); @@ -1697,8 +1698,8 @@ NonnullRefPtr Parser::parse_object_expression() while (!done() && !match(TokenType::CurlyClose)) { property_type = ObjectProperty::Type::KeyValue; - RefPtr property_key; - RefPtr property_value; + RefPtr property_key; + RefPtr property_value; FunctionKind function_kind { FunctionKind::Normal }; if (match(TokenType::TripleDot)) { @@ -1829,14 +1830,14 @@ NonnullRefPtr Parser::parse_object_expression() move(properties)); } -NonnullRefPtr Parser::parse_array_expression() +NonnullRefPtr Parser::parse_array_expression() { auto rule_start = push_start(); consume(TokenType::BracketOpen); - Vector> elements; + Vector> elements; while (match_expression() || match(TokenType::TripleDot) || match(TokenType::Comma)) { - RefPtr expression; + RefPtr expression; if (match(TokenType::TripleDot)) { consume(TokenType::TripleDot); @@ -1857,7 +1858,7 @@ NonnullRefPtr Parser::parse_array_expression() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(elements)); } -NonnullRefPtr Parser::parse_string_literal(Token const& token, StringLiteralType string_literal_type, bool* contains_invalid_escape) +NonnullRefPtr Parser::parse_string_literal(Token const& token, StringLiteralType string_literal_type, bool* contains_invalid_escape) { auto rule_start = push_start(); auto status = Token::StringValueStatus::Ok; @@ -1894,13 +1895,13 @@ NonnullRefPtr Parser::parse_string_literal(Token const& token, St return create_ast_node({ m_source_code, rule_start.position(), position() }, string); } -NonnullRefPtr Parser::parse_template_literal(bool is_tagged) +NonnullRefPtr Parser::parse_template_literal(bool is_tagged) { auto rule_start = push_start(); consume(TokenType::TemplateLiteralStart); - NonnullRefPtrVector expressions; - NonnullRefPtrVector raw_strings; + NonnullRefPtrVector expressions; + NonnullRefPtrVector raw_strings; auto append_empty_string = [this, &rule_start, &expressions, &raw_strings, is_tagged]() { auto string_literal = create_ast_node({ m_source_code, rule_start.position(), position() }, ""); @@ -1959,7 +1960,7 @@ NonnullRefPtr Parser::parse_template_literal(bool is_tagged) return create_ast_node({ m_source_code, rule_start.position(), position() }, expressions); } -NonnullRefPtr Parser::parse_expression(int min_precedence, Associativity associativity, ForbiddenTokens forbidden) +NonnullRefPtr Parser::parse_expression(int min_precedence, Associativity associativity, ForbiddenTokens forbidden) { auto rule_start = push_start(); auto [expression, should_continue_parsing] = parse_primary_expression(); @@ -1970,7 +1971,7 @@ NonnullRefPtr Parser::parse_expression(int min_precedence, Associati } }; if (is(*expression) && m_state.current_scope_pusher) { - auto identifier_instance = static_ptr_cast(expression); + auto identifier_instance = static_ptr_cast(expression); auto function_scope = m_state.current_scope_pusher->last_function_scope(); auto function_parent_scope = function_scope ? function_scope->parent_scope() : nullptr; bool has_not_been_declared_as_variable = true; @@ -2017,7 +2018,7 @@ NonnullRefPtr Parser::parse_expression(int min_precedence, Associati check_for_invalid_object_property(expression); if (is(*expression) && m_state.current_scope_pusher) { - auto& callee = static_ptr_cast(expression)->callee(); + auto& callee = static_ptr_cast(expression)->callee(); if (is(callee)) { auto& identifier_instance = static_cast(callee); if (identifier_instance.string() == "eval"sv) { @@ -2035,7 +2036,7 @@ NonnullRefPtr Parser::parse_expression(int min_precedence, Associati } if (match(TokenType::Comma) && min_precedence <= 1) { - NonnullRefPtrVector expressions; + NonnullRefPtrVector expressions; expressions.append(expression); while (match(TokenType::Comma)) { consume(); @@ -2047,7 +2048,7 @@ NonnullRefPtr Parser::parse_expression(int min_precedence, Associati return expression; } -Parser::ExpressionResult Parser::parse_secondary_expression(NonnullRefPtr lhs, int min_precedence, Associativity associativity, ForbiddenTokens forbidden) +Parser::ExpressionResult Parser::parse_secondary_expression(NonnullRefPtr lhs, int min_precedence, Associativity associativity, ForbiddenTokens forbidden) { auto rule_start = push_start(); switch (m_state.current_token.type()) { @@ -2170,7 +2171,7 @@ Parser::ExpressionResult Parser::parse_secondary_expression(NonnullRefPtrclass_name())); if (m_state.strict_mode && is(*lhs)) { - auto& identifier = static_cast(*lhs); + auto& identifier = static_cast(*lhs); auto& name = identifier.string(); check_identifier_name_for_assignment_validity(name); } @@ -2182,7 +2183,7 @@ Parser::ExpressionResult Parser::parse_secondary_expression(NonnullRefPtrclass_name())); if (m_state.strict_mode && is(*lhs)) { - auto& identifier = static_cast(*lhs); + auto& identifier = static_cast(*lhs); auto& name = identifier.string(); check_identifier_name_for_assignment_validity(name); } @@ -2238,7 +2239,7 @@ bool Parser::is_private_identifier_valid() const return true; } -RefPtr Parser::synthesize_binding_pattern(Expression const& expression) +RefPtr Parser::synthesize_binding_pattern(Expression const& expression) { VERIFY(is(expression) || is(expression)); // Clear any syntax error that has occurred in the range that 'expression' spans. @@ -2276,7 +2277,7 @@ RefPtr Parser::synthesize_binding_pattern(Expression const& expr return result; } -NonnullRefPtr Parser::parse_assignment_expression(AssignmentOp assignment_op, NonnullRefPtr lhs, int min_precedence, Associativity associativity, ForbiddenTokens forbidden) +NonnullRefPtr Parser::parse_assignment_expression(AssignmentOp assignment_op, NonnullRefPtr lhs, int min_precedence, Associativity associativity, ForbiddenTokens forbidden) { auto rule_start = push_start(); VERIFY(match(TokenType::Equals) @@ -2327,7 +2328,7 @@ NonnullRefPtr Parser::parse_assignment_expression(Assignme return create_ast_node({ m_source_code, rule_start.position(), position() }, assignment_op, move(lhs), move(rhs)); } -NonnullRefPtr Parser::parse_identifier() +NonnullRefPtr Parser::parse_identifier() { auto identifier_start = position(); auto token = consume_identifier(); @@ -2359,7 +2360,7 @@ Vector Parser::parse_arguments() return arguments; } -NonnullRefPtr Parser::parse_call_expression(NonnullRefPtr lhs) +NonnullRefPtr Parser::parse_call_expression(NonnullRefPtr lhs) { auto rule_start = push_start(); if (!m_state.allow_super_constructor_call && is(*lhs)) @@ -2373,7 +2374,7 @@ NonnullRefPtr Parser::parse_call_expression(NonnullRefPtr Parser::parse_new_expression() +NonnullRefPtr Parser::parse_new_expression() { auto rule_start = push_start(); consume(TokenType::New); @@ -2403,7 +2404,7 @@ NonnullRefPtr Parser::parse_new_expression() return NewExpression::create({ m_source_code, rule_start.position(), position() }, move(callee), move(arguments)); } -NonnullRefPtr Parser::parse_yield_expression() +NonnullRefPtr Parser::parse_yield_expression() { auto rule_start = push_start(); @@ -2411,7 +2412,7 @@ NonnullRefPtr Parser::parse_yield_expression() syntax_error("'Yield' expression is not allowed in formal parameters of generator function"); consume(TokenType::Yield); - RefPtr argument; + RefPtr argument; bool yield_from = false; if (!m_state.current_token.trivia_contains_line_terminator()) { @@ -2427,7 +2428,7 @@ NonnullRefPtr Parser::parse_yield_expression() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(argument), yield_from); } -NonnullRefPtr Parser::parse_await_expression() +NonnullRefPtr Parser::parse_await_expression() { auto rule_start = push_start(); @@ -2445,7 +2446,7 @@ NonnullRefPtr Parser::parse_await_expression() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(argument)); } -NonnullRefPtr Parser::parse_return_statement() +NonnullRefPtr Parser::parse_return_statement() { auto rule_start = push_start(); if (!m_state.in_function_context && !m_state.in_arrow_function_context) @@ -2486,7 +2487,7 @@ void Parser::parse_statement_list(ScopeNode& output_node, AllowLabelledFunction } // FunctionBody, https://tc39.es/ecma262/#prod-FunctionBody -NonnullRefPtr Parser::parse_function_body(Vector const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval) +NonnullRefPtr Parser::parse_function_body(Vector const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval) { auto rule_start = push_start(); auto function_body = create_ast_node({ m_source_code, rule_start.position(), position() }); @@ -2530,7 +2531,7 @@ NonnullRefPtr Parser::parse_function_body(Vector const& binding) { + [&](NonnullRefPtr const& binding) { binding->for_each_bound_name([&](auto& bound_name) { if (function_kind == FunctionKind::Generator && bound_name == "yield"sv) syntax_error("Parameter name 'yield' not allowed in this context"); @@ -2555,7 +2556,7 @@ NonnullRefPtr Parser::parse_function_body(Vector Parser::parse_block_statement() +NonnullRefPtr Parser::parse_block_statement() { auto rule_start = push_start(); auto block = create_ast_node({ m_source_code, rule_start.position(), position() }); @@ -2669,7 +2670,7 @@ Vector Parser::parse_formal_parameters(int& function_length, Vector parameters; - auto consume_identifier_or_binding_pattern = [&]() -> Variant> { + auto consume_identifier_or_binding_pattern = [&]() -> Variant> { if (auto pattern = parse_binding_pattern(AllowDuplicates::No, AllowMemberExpressions::No)) return pattern.release_nonnull(); @@ -2683,7 +2684,7 @@ Vector Parser::parse_formal_parameters(int& function_length, [&](DeprecatedFlyString const& name) { return name == parameter_name; }, - [&](NonnullRefPtr const& bindings) { + [&](NonnullRefPtr const& bindings) { bool found_duplicate = false; bindings->for_each_bound_name([&](auto& bound_name) { if (bound_name == parameter_name) @@ -2724,7 +2725,7 @@ Vector Parser::parse_formal_parameters(int& function_length, is_rest = true; } auto parameter = consume_identifier_or_binding_pattern(); - RefPtr default_value; + RefPtr default_value; if (match(TokenType::Equals)) { consume(); @@ -2737,7 +2738,7 @@ Vector Parser::parse_formal_parameters(int& function_length, default_value = parse_expression(2); bool is_generator = parse_options & FunctionNodeParseOptions::IsGeneratorFunction; - if ((is_generator || m_state.strict_mode) && default_value && default_value->fast_is() && static_cast(*default_value).string() == "yield"sv) + if ((is_generator || m_state.strict_mode) && default_value && default_value->fast_is() && static_cast(*default_value).string() == "yield"sv) syntax_error("Generator function parameter initializer cannot contain a reference to an identifier named \"yield\""); } parameters.append({ move(parameter), default_value, is_rest }); @@ -2758,7 +2759,7 @@ Vector Parser::parse_formal_parameters(int& function_length, static AK::Array s_reserved_words = { "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with" }; -RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates allow_duplicates, Parser::AllowMemberExpressions allow_member_expressions) +RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates allow_duplicates, Parser::AllowMemberExpressions allow_member_expressions) { auto rule_start = push_start(); @@ -2794,7 +2795,7 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all decltype(BindingPattern::BindingEntry::name) name = Empty {}; decltype(BindingPattern::BindingEntry::alias) alias = Empty {}; - RefPtr initializer = {}; + RefPtr initializer = {}; if (is_object) { bool needs_alias = false; @@ -2802,9 +2803,9 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all auto expression_position = position(); auto expression = parse_expression(2, Associativity::Right, { TokenType::Equals }); if (is(*expression)) - alias = static_ptr_cast(expression); + alias = static_ptr_cast(expression); else if (is(*expression)) - name = static_ptr_cast(expression); + name = static_ptr_cast(expression); else syntax_error("Invalid destructuring assignment target", expression_position); } else if (match_identifier_name() || match(TokenType::StringLiteral) || match(TokenType::NumericLiteral) || match(TokenType::BigIntLiteral)) { @@ -2815,17 +2816,17 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all auto token = consume(TokenType::StringLiteral); auto string_literal = parse_string_literal(token); - name = create_ast_node( + name = create_ast_node( { m_source_code, rule_start.position(), position() }, string_literal->value()); } else if (match(TokenType::BigIntLiteral)) { auto string_value = consume().DeprecatedFlyString_value(); VERIFY(string_value.ends_with("n"sv)); - name = create_ast_node( + name = create_ast_node( { m_source_code, rule_start.position(), position() }, DeprecatedFlyString(string_value.view().substring_view(0, string_value.length() - 1))); } else { - name = create_ast_node( + name = create_ast_node( { m_source_code, rule_start.position(), position() }, consume().DeprecatedFlyString_value()); } @@ -2851,9 +2852,9 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all else syntax_error("Invalid destructuring assignment target", expression_position); } else if (is(*expression)) { - alias = static_ptr_cast(expression); + alias = static_ptr_cast(expression); } else if (is(*expression)) { - alias = static_ptr_cast(expression); + alias = static_ptr_cast(expression); } else { syntax_error("Invalid destructuring assignment target", expression_position); } @@ -2863,7 +2864,7 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all return {}; alias = binding_pattern.release_nonnull(); } else if (match_identifier_name()) { - alias = create_ast_node( + alias = create_ast_node( { m_source_code, rule_start.position(), position() }, consume().DeprecatedFlyString_value()); @@ -2886,9 +2887,9 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all else syntax_error("Invalid destructuring assignment target", expression_position); } else if (is(*expression)) { - alias = static_ptr_cast(expression); + alias = static_ptr_cast(expression); } else if (is(*expression)) { - alias = static_ptr_cast(expression); + alias = static_ptr_cast(expression); } else { syntax_error("Invalid destructuring assignment target", expression_position); } @@ -2902,7 +2903,7 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all } else if (match_identifier_name()) { // BindingElement must always have an Empty name field auto identifier_name = consume_identifier().DeprecatedFlyString_value(); - alias = create_ast_node( + alias = create_ast_node( { m_source_code, rule_start.position(), position() }, identifier_name); } else { @@ -2962,7 +2963,7 @@ RefPtr Parser::parse_binding_pattern(Parser::AllowDuplicates all return pattern; } -RefPtr Parser::parse_lexical_binding() +RefPtr Parser::parse_lexical_binding() { auto binding_start = push_start(); @@ -2992,7 +2993,7 @@ RefPtr Parser::parse_lexical_binding() return {}; } -NonnullRefPtr Parser::parse_variable_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration) +NonnullRefPtr Parser::parse_variable_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration) { auto rule_start = push_start(); DeclarationKind declaration_kind; @@ -3012,9 +3013,9 @@ NonnullRefPtr Parser::parse_variable_declaration(IsForLoopV } consume(); - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (;;) { - Variant, NonnullRefPtr, Empty> target {}; + Variant, NonnullRefPtr, Empty> target {}; if (auto pattern = parse_binding_pattern(declaration_kind != DeclarationKind::Var ? AllowDuplicates::No : AllowDuplicates::Yes, AllowMemberExpressions::No)) { if ((declaration_kind == DeclarationKind::Let || declaration_kind == DeclarationKind::Const)) { pattern->for_each_bound_name([this](auto& name) { @@ -3041,7 +3042,7 @@ NonnullRefPtr Parser::parse_variable_declaration(IsForLoopV break; } - RefPtr init; + RefPtr init; if (match(TokenType::Equals)) { consume(); // In a for loop 'in' can be ambiguous so we do not allow it @@ -3052,13 +3053,13 @@ NonnullRefPtr Parser::parse_variable_declaration(IsForLoopV init = parse_expression(2); } else if (is_for_loop_variable_declaration == IsForLoopVariableDeclaration::No && declaration_kind == DeclarationKind::Const) { syntax_error("Missing initializer in 'const' variable declaration"); - } else if (is_for_loop_variable_declaration == IsForLoopVariableDeclaration::No && target.has>()) { + } else if (is_for_loop_variable_declaration == IsForLoopVariableDeclaration::No && target.has>()) { syntax_error("Missing initializer in destructuring assignment"); } declarations.append(create_ast_node( { m_source_code, rule_start.position(), position() }, - move(target).downcast, NonnullRefPtr>(), + move(target).downcast, NonnullRefPtr>(), move(init))); if (match(TokenType::Comma)) { @@ -3076,14 +3077,14 @@ NonnullRefPtr Parser::parse_variable_declaration(IsForLoopV return declaration; } -NonnullRefPtr Parser::parse_using_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration) +NonnullRefPtr Parser::parse_using_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration) { // using [no LineTerminator here] BindingList[?In, ?Yield, ?Await, +Using] ; auto rule_start = push_start(); VERIFY(m_state.current_token.original_value() == "using"sv); consume(TokenType::Identifier); VERIFY(!m_state.current_token.trivia_contains_line_terminator()); - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (;;) { auto lexical_binding = parse_lexical_binding(); @@ -3096,7 +3097,7 @@ NonnullRefPtr Parser::parse_using_declaration(IsForLoopVariabl if (lexical_binding->string() == "let"sv) syntax_error("Lexical binding may not be called 'let'"); - RefPtr initializer; + RefPtr initializer; if (match(TokenType::Equals)) { consume(); @@ -3125,7 +3126,7 @@ NonnullRefPtr Parser::parse_using_declaration(IsForLoopVariabl return create_ast_node({ m_source_code, rule_start.position(), position() }, move(declarations)); } -NonnullRefPtr Parser::parse_throw_statement() +NonnullRefPtr Parser::parse_throw_statement() { auto rule_start = push_start(); consume(TokenType::Throw); @@ -3141,7 +3142,7 @@ NonnullRefPtr Parser::parse_throw_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(expression)); } -NonnullRefPtr Parser::parse_break_statement() +NonnullRefPtr Parser::parse_break_statement() { auto rule_start = push_start(); consume(TokenType::Break); @@ -3165,7 +3166,7 @@ NonnullRefPtr Parser::parse_break_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, target_label); } -NonnullRefPtr Parser::parse_continue_statement() +NonnullRefPtr Parser::parse_continue_statement() { auto rule_start = push_start(); if (!m_state.in_continue_context) @@ -3191,7 +3192,7 @@ NonnullRefPtr Parser::parse_continue_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, target_label); } -NonnullRefPtr Parser::parse_conditional_expression(NonnullRefPtr test, ForbiddenTokens forbidden) +NonnullRefPtr Parser::parse_conditional_expression(NonnullRefPtr test, ForbiddenTokens forbidden) { auto rule_start = push_start(); consume(TokenType::QuestionMark); @@ -3201,7 +3202,7 @@ NonnullRefPtr Parser::parse_conditional_expression(Nonnul return create_ast_node({ m_source_code, rule_start.position(), position() }, move(test), move(consequent), move(alternate)); } -NonnullRefPtr Parser::parse_optional_chain(NonnullRefPtr base) +NonnullRefPtr Parser::parse_optional_chain(NonnullRefPtr base) { auto rule_start = push_start(); Vector chain; @@ -3296,18 +3297,18 @@ NonnullRefPtr Parser::parse_optional_chain(NonnullRefPtr Parser::parse_try_statement() +NonnullRefPtr Parser::parse_try_statement() { auto rule_start = push_start(); consume(TokenType::Try); auto block = parse_block_statement(); - RefPtr handler; + RefPtr handler; if (match(TokenType::Catch)) handler = parse_catch_clause(); - RefPtr finalizer; + RefPtr finalizer; if (match(TokenType::Finally)) { consume(); finalizer = parse_block_statement(); @@ -3319,12 +3320,12 @@ NonnullRefPtr Parser::parse_try_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(block), move(handler), move(finalizer)); } -NonnullRefPtr Parser::parse_do_while_statement() +NonnullRefPtr Parser::parse_do_while_statement() { auto rule_start = push_start(); consume(TokenType::Do); - auto body = [&]() -> NonnullRefPtr { + auto body = [&]() -> NonnullRefPtr { TemporaryChange break_change(m_state.in_break_context, true); TemporaryChange continue_change(m_state.in_continue_context, true); return parse_statement(); @@ -3344,7 +3345,7 @@ NonnullRefPtr Parser::parse_do_while_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(test), move(body)); } -NonnullRefPtr Parser::parse_while_statement() +NonnullRefPtr Parser::parse_while_statement() { auto rule_start = push_start(); consume(TokenType::While); @@ -3361,7 +3362,7 @@ NonnullRefPtr Parser::parse_while_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(test), move(body)); } -NonnullRefPtr Parser::parse_switch_statement() +NonnullRefPtr Parser::parse_switch_statement() { auto rule_start = push_start(); consume(TokenType::Switch); @@ -3393,7 +3394,7 @@ NonnullRefPtr Parser::parse_switch_statement() return switch_statement; } -NonnullRefPtr Parser::parse_with_statement() +NonnullRefPtr Parser::parse_with_statement() { auto rule_start = push_start(); consume(TokenType::With); @@ -3407,10 +3408,10 @@ NonnullRefPtr Parser::parse_with_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(object), move(body)); } -NonnullRefPtr Parser::parse_switch_case() +NonnullRefPtr Parser::parse_switch_case() { auto rule_start = push_start(); - RefPtr test; + RefPtr test; if (consume().type() == TokenType::Case) { test = parse_expression(0); @@ -3426,13 +3427,13 @@ NonnullRefPtr Parser::parse_switch_case() return switch_case; } -NonnullRefPtr Parser::parse_catch_clause() +NonnullRefPtr Parser::parse_catch_clause() { auto rule_start = push_start(); consume(TokenType::Catch); DeprecatedFlyString parameter; - RefPtr pattern_parameter; + RefPtr pattern_parameter; auto should_expect_parameter = false; if (match(TokenType::ParenOpen)) { should_expect_parameter = true; @@ -3486,7 +3487,7 @@ NonnullRefPtr Parser::parse_catch_clause() move(body)); } -NonnullRefPtr Parser::parse_if_statement() +NonnullRefPtr Parser::parse_if_statement() { auto rule_start = push_start(); auto parse_function_declaration_as_block_statement = [&] { @@ -3519,13 +3520,13 @@ NonnullRefPtr Parser::parse_if_statement() auto predicate = parse_expression(0); consume(TokenType::ParenClose); - RefPtr consequent; + RefPtr consequent; if (!m_state.strict_mode && match(TokenType::Function)) consequent = parse_function_declaration_as_block_statement(); else consequent = parse_statement(); - RefPtr alternate; + RefPtr alternate; if (match(TokenType::Else)) { consume(); if (!m_state.strict_mode && match(TokenType::Function)) @@ -3536,7 +3537,7 @@ NonnullRefPtr Parser::parse_if_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(predicate), move(*consequent), move(alternate)); } -NonnullRefPtr Parser::parse_for_statement() +NonnullRefPtr Parser::parse_for_statement() { auto rule_start = push_start(); auto is_await_loop = IsForAwaitLoop::No; @@ -3571,7 +3572,7 @@ NonnullRefPtr Parser::parse_for_statement() Optional scope_pusher; - RefPtr init; + RefPtr init; if (!match(TokenType::Semicolon)) { auto match_for_using_declaration = [&] { @@ -3650,13 +3651,13 @@ NonnullRefPtr Parser::parse_for_statement() } consume(TokenType::Semicolon); - RefPtr test; + RefPtr test; if (!match(TokenType::Semicolon)) test = parse_expression(0); consume(TokenType::Semicolon); - RefPtr update; + RefPtr update; if (!match(TokenType::ParenClose)) update = parse_expression(0); @@ -3670,21 +3671,21 @@ NonnullRefPtr Parser::parse_for_statement() return create_ast_node({ m_source_code, rule_start.position(), position() }, move(init), move(test), move(update), move(body)); } -NonnullRefPtr Parser::parse_for_in_of_statement(NonnullRefPtr lhs, IsForAwaitLoop is_for_await_loop) +NonnullRefPtr Parser::parse_for_in_of_statement(NonnullRefPtr lhs, IsForAwaitLoop is_for_await_loop) { - Variant, NonnullRefPtr> for_declaration = lhs; + Variant, NonnullRefPtr> for_declaration = lhs; auto rule_start = push_start(); auto has_annexB_for_in_init_extension = false; if (is(*lhs)) { - auto& declaration = static_cast(*lhs); + auto& declaration = static_cast(*lhs); // Syntax errors for wrong amounts of declaration should have already been hit. if (!declaration.declarations().is_empty()) { // AnnexB extension B.3.5 Initializers in ForIn Statement Heads, https://tc39.es/ecma262/#sec-initializers-in-forin-statement-heads auto& variable = declaration.declarations().first(); if (variable.init()) { - if (m_state.strict_mode || declaration.declaration_kind() != DeclarationKind::Var || !variable.target().has>()) + if (m_state.strict_mode || declaration.declaration_kind() != DeclarationKind::Var || !variable.target().has>()) syntax_error("Variable initializer not allowed in for..in/of"); else has_annexB_for_in_init_extension = true; @@ -3729,7 +3730,7 @@ NonnullRefPtr Parser::parse_for_in_of_statement(NonnullRefPtr({ m_source_code, rule_start.position(), position() }, move(for_declaration), move(rhs), move(body)); } -NonnullRefPtr Parser::parse_debugger_statement() +NonnullRefPtr Parser::parse_debugger_statement() { auto rule_start = push_start(); consume(TokenType::Debugger); @@ -4280,7 +4281,7 @@ ModuleRequest Parser::parse_module_request() static DeprecatedFlyString default_string_value = "default"; -NonnullRefPtr Parser::parse_import_statement(Program& program) +NonnullRefPtr Parser::parse_import_statement(Program& program) { // We use the extended syntax which adds: // ImportDeclaration: @@ -4440,7 +4441,7 @@ NonnullRefPtr Parser::parse_import_statement(Program& program) return create_ast_node({ m_source_code, rule_start.position(), position() }, move(module_request), move(entries)); } -NonnullRefPtr Parser::parse_export_statement(Program& program) +NonnullRefPtr Parser::parse_export_statement(Program& program) { // We use the extended syntax which adds: // ExportDeclaration: @@ -4472,7 +4473,7 @@ NonnullRefPtr Parser::parse_export_statement(Program& program) Vector entries_with_location; - RefPtr expression = {}; + RefPtr expression = {}; bool is_default = false; ModuleRequest from_specifier; @@ -4575,7 +4576,7 @@ NonnullRefPtr Parser::parse_export_statement(Program& program) consume_or_insert_semicolon(); if (is(*expression)) { - auto const& class_expression = static_cast(*expression); + auto const& class_expression = static_cast(*expression); if (class_expression.has_name()) local_name = class_expression.name(); } @@ -4634,21 +4635,21 @@ NonnullRefPtr Parser::parse_export_statement(Program& program) auto declaration = parse_declaration(); m_state.current_scope_pusher->add_declaration(declaration); if (is(*declaration)) { - auto& func = static_cast(*declaration); + auto& func = static_cast(*declaration); entries_with_location.append({ ExportEntry::named_export(func.name(), func.name()), func.source_range().start }); } else if (is(*declaration)) { - auto& class_declaration = static_cast(*declaration); + auto& class_declaration = static_cast(*declaration); entries_with_location.append({ ExportEntry::named_export(class_declaration.name(), class_declaration.name()), class_declaration.source_range().start }); } else { VERIFY(is(*declaration)); - auto& variables = static_cast(*declaration); + auto& variables = static_cast(*declaration); VERIFY(variables.is_lexical_declaration()); for (auto& decl : variables.declarations()) { decl.target().visit( - [&](NonnullRefPtr const& identifier) { + [&](NonnullRefPtr const& identifier) { entries_with_location.append({ ExportEntry::named_export(identifier->string(), identifier->string()), identifier->source_range().start }); }, - [&](NonnullRefPtr const& binding) { + [&](NonnullRefPtr const& binding) { binding->for_each_bound_name([&](auto& name) { entries_with_location.append({ ExportEntry::named_export(name, name), decl_position }); }); @@ -4662,10 +4663,10 @@ NonnullRefPtr Parser::parse_export_statement(Program& program) m_state.current_scope_pusher->add_declaration(variable_declaration); for (auto& decl : variable_declaration->declarations()) { decl.target().visit( - [&](NonnullRefPtr const& identifier) { + [&](NonnullRefPtr const& identifier) { entries_with_location.append({ ExportEntry::named_export(identifier->string(), identifier->string()), identifier->source_range().start }); }, - [&](NonnullRefPtr const& binding) { + [&](NonnullRefPtr const& binding) { binding->for_each_bound_name([&](auto& name) { entries_with_location.append({ ExportEntry::named_export(name, name), variable_position }); }); diff --git a/Userland/Libraries/LibJS/Parser.h b/Userland/Libraries/LibJS/Parser.h index 0e6692fc229..0ee6add634e 100644 --- a/Userland/Libraries/LibJS/Parser.h +++ b/Userland/Libraries/LibJS/Parser.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2020, Stephan Unverwerth * Copyright (c) 2021-2022, David Tuin + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -70,34 +71,34 @@ public: No }; - RefPtr parse_binding_pattern(AllowDuplicates is_var_declaration = AllowDuplicates::No, AllowMemberExpressions allow_member_expressions = AllowMemberExpressions::No); + RefPtr parse_binding_pattern(AllowDuplicates is_var_declaration = AllowDuplicates::No, AllowMemberExpressions allow_member_expressions = AllowMemberExpressions::No); struct PrimaryExpressionParseResult { - NonnullRefPtr result; + NonnullRefPtr result; bool should_continue_parsing_as_expression { true }; }; - NonnullRefPtr parse_declaration(); + NonnullRefPtr parse_declaration(); enum class AllowLabelledFunction { No, Yes }; - NonnullRefPtr parse_statement(AllowLabelledFunction allow_labelled_function = AllowLabelledFunction::No); - NonnullRefPtr parse_block_statement(); - NonnullRefPtr parse_function_body(Vector const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval); - NonnullRefPtr parse_return_statement(); + NonnullRefPtr parse_statement(AllowLabelledFunction allow_labelled_function = AllowLabelledFunction::No); + NonnullRefPtr parse_block_statement(); + NonnullRefPtr parse_function_body(Vector const& parameters, FunctionKind function_kind, bool& contains_direct_call_to_eval); + NonnullRefPtr parse_return_statement(); enum class IsForLoopVariableDeclaration { No, Yes }; - NonnullRefPtr parse_variable_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration = IsForLoopVariableDeclaration::No); - RefPtr parse_lexical_binding(); - NonnullRefPtr parse_using_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration = IsForLoopVariableDeclaration::No); - NonnullRefPtr parse_for_statement(); + NonnullRefPtr parse_variable_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration = IsForLoopVariableDeclaration::No); + RefPtr parse_lexical_binding(); + NonnullRefPtr parse_using_declaration(IsForLoopVariableDeclaration is_for_loop_variable_declaration = IsForLoopVariableDeclaration::No); + NonnullRefPtr parse_for_statement(); enum class IsForAwaitLoop { No, @@ -122,36 +123,43 @@ public: struct ExpressionResult { template - ExpressionResult(NonnullRefPtr expression, ForbiddenTokens forbidden = {}) - : expression(expression) + ExpressionResult(NonnullRefPtr expression, ForbiddenTokens forbidden = {}) + : expression(move(expression)) , forbidden(forbidden) { } - NonnullRefPtr expression; + + template + ExpressionResult(NonnullRefPtr expression, ForbiddenTokens forbidden = {}) + : expression(move(expression)) + , forbidden(forbidden) + { + } + NonnullRefPtr expression; ForbiddenTokens forbidden; }; - NonnullRefPtr parse_for_in_of_statement(NonnullRefPtr lhs, IsForAwaitLoop is_await); - NonnullRefPtr parse_if_statement(); - NonnullRefPtr parse_throw_statement(); - NonnullRefPtr parse_try_statement(); - NonnullRefPtr parse_catch_clause(); - NonnullRefPtr parse_switch_statement(); - NonnullRefPtr parse_switch_case(); - NonnullRefPtr parse_break_statement(); - NonnullRefPtr parse_continue_statement(); - NonnullRefPtr parse_do_while_statement(); - NonnullRefPtr parse_while_statement(); - NonnullRefPtr parse_with_statement(); - NonnullRefPtr parse_debugger_statement(); - NonnullRefPtr parse_conditional_expression(NonnullRefPtr test, ForbiddenTokens); - NonnullRefPtr parse_optional_chain(NonnullRefPtr base); - NonnullRefPtr parse_expression(int min_precedence, Associativity associate = Associativity::Right, ForbiddenTokens forbidden = {}); + NonnullRefPtr parse_for_in_of_statement(NonnullRefPtr lhs, IsForAwaitLoop is_await); + NonnullRefPtr parse_if_statement(); + NonnullRefPtr parse_throw_statement(); + NonnullRefPtr parse_try_statement(); + NonnullRefPtr parse_catch_clause(); + NonnullRefPtr parse_switch_statement(); + NonnullRefPtr parse_switch_case(); + NonnullRefPtr parse_break_statement(); + NonnullRefPtr parse_continue_statement(); + NonnullRefPtr parse_do_while_statement(); + NonnullRefPtr parse_while_statement(); + NonnullRefPtr parse_with_statement(); + NonnullRefPtr parse_debugger_statement(); + NonnullRefPtr parse_conditional_expression(NonnullRefPtr test, ForbiddenTokens); + NonnullRefPtr parse_optional_chain(NonnullRefPtr base); + NonnullRefPtr parse_expression(int min_precedence, Associativity associate = Associativity::Right, ForbiddenTokens forbidden = {}); PrimaryExpressionParseResult parse_primary_expression(); - NonnullRefPtr parse_unary_prefixed_expression(); - NonnullRefPtr parse_regexp_literal(); - NonnullRefPtr parse_object_expression(); - NonnullRefPtr parse_array_expression(); + NonnullRefPtr parse_unary_prefixed_expression(); + NonnullRefPtr parse_regexp_literal(); + NonnullRefPtr parse_object_expression(); + NonnullRefPtr parse_array_expression(); enum class StringLiteralType { Normal, @@ -159,26 +167,26 @@ public: TaggedTemplate }; - NonnullRefPtr parse_string_literal(Token const& token, StringLiteralType string_literal_type = StringLiteralType::Normal, bool* contains_invalid_escape = nullptr); - NonnullRefPtr parse_template_literal(bool is_tagged); - ExpressionResult parse_secondary_expression(NonnullRefPtr, int min_precedence, Associativity associate = Associativity::Right, ForbiddenTokens forbidden = {}); - NonnullRefPtr parse_call_expression(NonnullRefPtr); - NonnullRefPtr parse_new_expression(); - NonnullRefPtr parse_class_declaration(); - NonnullRefPtr parse_class_expression(bool expect_class_name); - NonnullRefPtr parse_yield_expression(); - NonnullRefPtr parse_await_expression(); - NonnullRefPtr parse_property_key(); - NonnullRefPtr parse_assignment_expression(AssignmentOp, NonnullRefPtr lhs, int min_precedence, Associativity, ForbiddenTokens forbidden = {}); - NonnullRefPtr parse_identifier(); - NonnullRefPtr parse_import_statement(Program& program); - NonnullRefPtr parse_export_statement(Program& program); + NonnullRefPtr parse_string_literal(Token const& token, StringLiteralType string_literal_type = StringLiteralType::Normal, bool* contains_invalid_escape = nullptr); + NonnullRefPtr parse_template_literal(bool is_tagged); + ExpressionResult parse_secondary_expression(NonnullRefPtr, int min_precedence, Associativity associate = Associativity::Right, ForbiddenTokens forbidden = {}); + NonnullRefPtr parse_call_expression(NonnullRefPtr); + NonnullRefPtr parse_new_expression(); + NonnullRefPtr parse_class_declaration(); + NonnullRefPtr parse_class_expression(bool expect_class_name); + NonnullRefPtr parse_yield_expression(); + NonnullRefPtr parse_await_expression(); + NonnullRefPtr parse_property_key(); + NonnullRefPtr parse_assignment_expression(AssignmentOp, NonnullRefPtr lhs, int min_precedence, Associativity, ForbiddenTokens forbidden = {}); + NonnullRefPtr parse_identifier(); + NonnullRefPtr parse_import_statement(Program& program); + NonnullRefPtr parse_export_statement(Program& program); - RefPtr try_parse_arrow_function_expression(bool expect_parens, bool is_async = false); - RefPtr try_parse_labelled_statement(AllowLabelledFunction allow_function); - RefPtr try_parse_new_target_expression(); - RefPtr try_parse_import_meta_expression(); - NonnullRefPtr parse_import_call(); + RefPtr try_parse_arrow_function_expression(bool expect_parens, bool is_async = false); + RefPtr try_parse_labelled_statement(AllowLabelledFunction allow_function); + RefPtr try_parse_new_target_expression(); + RefPtr try_parse_import_meta_expression(); + NonnullRefPtr parse_import_call(); Vector parse_arguments(); @@ -246,7 +254,7 @@ private: void discard_saved_state(); Position position() const; - RefPtr synthesize_binding_pattern(Expression const& expression); + RefPtr synthesize_binding_pattern(Expression const& expression); Token next_token(size_t steps = 1) const; @@ -333,7 +341,7 @@ private: } }; - NonnullRefPtr m_source_code; + NonnullRefPtr m_source_code; Vector m_rule_starts; ParserState m_state; DeprecatedFlyString m_filename; diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index b6be28e1e90..375cde65178 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2020, Stephan Unverwerth * Copyright (c) 2020-2022, Linus Groh + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -350,7 +351,7 @@ ThrowCompletionOr ECMAScriptFunctionObject::function_declaration_instantia if (parameter_names.set(name) != AK::HashSetResult::InsertedNewEntry) has_duplicates = true; }, - [&](NonnullRefPtr const& pattern) { + [&](NonnullRefPtr const& pattern) { if (pattern->contains_expression()) has_parameter_expressions = true; @@ -472,7 +473,8 @@ ThrowCompletionOr ECMAScriptFunctionObject::function_declaration_instantia return reference.put_value(vm, argument_value); else return reference.initialize_referenced_binding(vm, argument_value); - } else if (IsSame const&, decltype(param)>) { + } + if constexpr (IsSame const&, decltype(param)>) { // Here the difference from hasDuplicates is important return vm.binding_initialization(param, argument_value, used_environment); } @@ -728,7 +730,7 @@ void ECMAScriptFunctionObject::async_function_start(PromiseCapability const& pro } // 27.7.5.2 AsyncBlockStart ( promiseCapability, asyncBody, asyncContext ), https://tc39.es/ecma262/#sec-asyncblockstart -void async_block_start(VM& vm, NonnullRefPtr const& async_body, PromiseCapability const& promise_capability, ExecutionContext& async_context) +void async_block_start(VM& vm, NonnullRefPtr const& async_body, PromiseCapability const& promise_capability, ExecutionContext& async_context) { auto& realm = *vm.current_realm(); diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h index 101a08aa53a..978234d852e 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2020-2021, Andreas Kling * Copyright (c) 2020-2022, Linus Groh + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,7 +15,7 @@ namespace JS { -void async_block_start(VM&, NonnullRefPtr const& parse_node, PromiseCapability const&, ExecutionContext&); +void async_block_start(VM&, NonnullRefPtr const& parse_node, PromiseCapability const&, ExecutionContext&); // 10.2 ECMAScript Function Objects, https://tc39.es/ecma262/#sec-ecmascript-function-objects class ECMAScriptFunctionObject final : public FunctionObject { @@ -114,7 +115,7 @@ private: Environment* m_environment { nullptr }; // [[Environment]] PrivateEnvironment* m_private_environment { nullptr }; // [[PrivateEnvironment]] Vector const m_formal_parameters; // [[FormalParameters]] - NonnullRefPtr m_ecmascript_code; // [[ECMAScriptCode]] + NonnullRefPtr m_ecmascript_code; // [[ECMAScriptCode]] Realm* m_realm { nullptr }; // [[Realm]] ScriptOrModule m_script_or_module; // [[ScriptOrModule]] Object* m_home_object { nullptr }; // [[HomeObject]] diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 8291c936d24..8d49b298adb 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2021-2022, David Tuin * @@ -275,7 +275,7 @@ ThrowCompletionOr VM::named_evaluation_if_anonymous_function(ASTNode cons } // 13.15.5.2 Runtime Semantics: DestructuringAssignmentEvaluation, https://tc39.es/ecma262/#sec-runtime-semantics-destructuringassignmentevaluation -ThrowCompletionOr VM::destructuring_assignment_evaluation(NonnullRefPtr const& target, Value value) +ThrowCompletionOr VM::destructuring_assignment_evaluation(NonnullRefPtr const& target, Value value) { // Note: DestructuringAssignmentEvaluation is just like BindingInitialization without an environment // And it allows member expressions. We thus trust the parser to disallow member expressions @@ -292,7 +292,7 @@ ThrowCompletionOr VM::binding_initialization(DeprecatedFlyString const& ta } // 8.5.2 Runtime Semantics: BindingInitialization, https://tc39.es/ecma262/#sec-runtime-semantics-bindinginitialization -ThrowCompletionOr VM::binding_initialization(NonnullRefPtr const& target, Value value, Environment* environment) +ThrowCompletionOr VM::binding_initialization(NonnullRefPtr const& target, Value value, Environment* environment) { auto& vm = *this; @@ -348,9 +348,9 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const if (property.is_rest) { Reference assignment_target; - if (auto identifier_ptr = property.name.get_pointer>()) { + if (auto identifier_ptr = property.name.get_pointer>()) { assignment_target = TRY(resolve_binding((*identifier_ptr)->string(), environment)); - } else if (auto member_ptr = property.alias.get_pointer>()) { + } else if (auto member_ptr = property.alias.get_pointer>()) { assignment_target = TRY((*member_ptr)->to_reference(interpreter())); } else { VERIFY_NOT_REACHED(); @@ -368,19 +368,19 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const auto name = TRY(property.name.visit( [&](Empty) -> ThrowCompletionOr { VERIFY_NOT_REACHED(); }, - [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr { + [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr { return identifier->string(); }, - [&](NonnullRefPtr const& expression) -> ThrowCompletionOr { + [&](NonnullRefPtr const& expression) -> ThrowCompletionOr { auto result = TRY(expression->execute(interpreter())).release_value(); return result.to_property_key(vm); })); seen_names.set(name); - if (property.name.has>() && property.alias.has()) { + if (property.name.has>() && property.alias.has()) { // FIXME: this branch and not taking this have a lot in common we might want to unify it more (like it was before). - auto& identifier = *property.name.get>(); + auto& identifier = *property.name.get>(); auto reference = TRY(resolve_binding(identifier.string(), environment)); auto value_to_assign = TRY(object->get(name)); @@ -397,23 +397,23 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const auto reference_to_assign_to = TRY(property.alias.visit( [&](Empty) -> ThrowCompletionOr> { return Optional {}; }, - [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr> { + [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr> { return TRY(resolve_binding(identifier->string(), environment)); }, - [&](NonnullRefPtr const&) -> ThrowCompletionOr> { return Optional {}; }, - [&](NonnullRefPtr const& member_expression) -> ThrowCompletionOr> { + [&](NonnullRefPtr const&) -> ThrowCompletionOr> { return Optional {}; }, + [&](NonnullRefPtr const& member_expression) -> ThrowCompletionOr> { return TRY(member_expression->to_reference(interpreter())); })); auto value_to_assign = TRY(object->get(name)); if (property.initializer && value_to_assign.is_undefined()) { - if (auto* identifier_ptr = property.alias.get_pointer>()) + if (auto* identifier_ptr = property.alias.get_pointer>()) value_to_assign = TRY(named_evaluation_if_anonymous_function(*property.initializer, (*identifier_ptr)->string())); else value_to_assign = TRY(property.initializer->execute(interpreter())).release_value(); } - if (auto* binding_ptr = property.alias.get_pointer>()) { + if (auto* binding_ptr = property.alias.get_pointer>()) { TRY(binding_initialization(*binding_ptr, value_to_assign, environment)); } else { VERIFY(reference_to_assign_to.has_value()); @@ -441,11 +441,11 @@ ThrowCompletionOr VM::iterator_binding_initialization(BindingPattern const auto assignment_target = TRY(entry.alias.visit( [&](Empty) -> ThrowCompletionOr> { return Optional {}; }, - [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr> { + [&](NonnullRefPtr const& identifier) -> ThrowCompletionOr> { return TRY(resolve_binding(identifier->string(), environment)); }, - [&](NonnullRefPtr const&) -> ThrowCompletionOr> { return Optional {}; }, - [&](NonnullRefPtr const& member_expression) -> ThrowCompletionOr> { + [&](NonnullRefPtr const&) -> ThrowCompletionOr> { return Optional {}; }, + [&](NonnullRefPtr const& member_expression) -> ThrowCompletionOr> { return TRY(member_expression->to_reference(interpreter())); })); @@ -544,13 +544,13 @@ ThrowCompletionOr VM::iterator_binding_initialization(BindingPattern const if (value.is_undefined() && entry.initializer) { VERIFY(!entry.is_rest); - if (auto* identifier_ptr = entry.alias.get_pointer>()) + if (auto* identifier_ptr = entry.alias.get_pointer>()) value = TRY(named_evaluation_if_anonymous_function(*entry.initializer, (*identifier_ptr)->string())); else value = TRY(entry.initializer->execute(interpreter())).release_value(); } - if (auto* binding_ptr = entry.alias.get_pointer>()) { + if (auto* binding_ptr = entry.alias.get_pointer>()) { TRY(binding_initialization(*binding_ptr, value, environment)); } else if (!entry.alias.has()) { VERIFY(assignment_target.has_value()); diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index ea762770622..c82c487d82d 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2023, Andreas Kling * Copyright (c) 2020-2023, Linus Groh * Copyright (c) 2021-2022, David Tuin * @@ -235,9 +235,9 @@ public: CustomData* custom_data() { return m_custom_data; } - ThrowCompletionOr destructuring_assignment_evaluation(NonnullRefPtr const& target, Value value); + ThrowCompletionOr destructuring_assignment_evaluation(NonnullRefPtr const& target, Value value); ThrowCompletionOr binding_initialization(DeprecatedFlyString const& target, Value value, Environment* environment); - ThrowCompletionOr binding_initialization(NonnullRefPtr const& target, Value value, Environment* environment); + ThrowCompletionOr binding_initialization(NonnullRefPtr const& target, Value value, Environment* environment); ThrowCompletionOr named_evaluation_if_anonymous_function(ASTNode const& expression, DeprecatedFlyString const& name); diff --git a/Userland/Libraries/LibJS/SourceCode.cpp b/Userland/Libraries/LibJS/SourceCode.cpp index 3df5758ab9e..060623ca504 100644 --- a/Userland/Libraries/LibJS/SourceCode.cpp +++ b/Userland/Libraries/LibJS/SourceCode.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2022-2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,7 +12,7 @@ namespace JS { -NonnullRefPtr SourceCode::create(String filename, String code) +NonnullRefPtr SourceCode::create(String filename, String code) { return adopt_ref(*new SourceCode(move(filename), move(code))); } diff --git a/Userland/Libraries/LibJS/SourceCode.h b/Userland/Libraries/LibJS/SourceCode.h index 1550c50c89b..f48e3bdda9e 100644 --- a/Userland/Libraries/LibJS/SourceCode.h +++ b/Userland/Libraries/LibJS/SourceCode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2022-2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,7 +14,7 @@ namespace JS { class SourceCode : public RefCounted { public: - static NonnullRefPtr create(String filename, String code); + static NonnullRefPtr create(String filename, String code); String const& filename() const; String const& code() const; diff --git a/Userland/Libraries/LibJS/SourceRange.h b/Userland/Libraries/LibJS/SourceRange.h index 71f3d2ba52f..67e23225b1d 100644 --- a/Userland/Libraries/LibJS/SourceRange.h +++ b/Userland/Libraries/LibJS/SourceRange.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -21,7 +22,7 @@ struct Position { struct SourceRange { [[nodiscard]] bool contains(Position const& position) const { return position.offset <= end.offset && position.offset >= start.offset; } - NonnullRefPtr code; + NonnullRefPtr code; Position start; Position end; diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp index 7c56722f6ac..89a30312b27 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.cpp +++ b/Userland/Libraries/LibJS/SourceTextModule.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2023, Andreas Kling * Copyright (c) 2022, David Tuin * * SPDX-License-Identifier: BSD-2-Clause @@ -49,7 +49,7 @@ static Vector module_requests(Program& program, Vector requested_modules_with_indices; @@ -89,7 +89,7 @@ static Vector module_requests(Program& program, Vectorassertions, supported_import_assertions); // Note: We have to modify the assertions in place because else it might keep non supported ones - module.module_request->assertions = move(assertions); + const_cast(module.module_request)->assertions = move(assertions); // 3. Return a ModuleRequest Record { [[Specifer]]: specifier, [[Assertions]]: assertions }. requested_modules_in_source_order.empend(module.module_request->module_specifier, module.module_request->assertions); @@ -102,7 +102,7 @@ static Vector module_requests(Program& program, Vector body, Vector requested_modules, Vector import_entries, Vector local_export_entries, Vector indirect_export_entries, Vector star_export_entries, - RefPtr default_export) + RefPtr default_export) : CyclicModule(realm, filename, has_top_level_await, move(requested_modules), host_defined) , m_ecmascript_code(move(body)) , m_execution_context(realm.heap()) @@ -157,7 +157,7 @@ Result, Vector> SourceTextModule::pa Vector star_export_entries; // Note: Not in the spec but makes it easier to find the default. - RefPtr default_export; + RefPtr default_export; // 9. Let exportEntries be ExportEntries of body. // 10. For each ExportEntry Record ee of exportEntries, do diff --git a/Userland/Libraries/LibJS/SourceTextModule.h b/Userland/Libraries/LibJS/SourceTextModule.h index 28dea01f9b7..c79f122bfad 100644 --- a/Userland/Libraries/LibJS/SourceTextModule.h +++ b/Userland/Libraries/LibJS/SourceTextModule.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2023, Andreas Kling * Copyright (c) 2022, David Tuin * * SPDX-License-Identifier: BSD-2-Clause @@ -36,7 +36,7 @@ private: SourceTextModule(Realm&, StringView filename, Script::HostDefined* host_defined, bool has_top_level_await, NonnullRefPtr body, Vector requested_modules, Vector import_entries, Vector local_export_entries, Vector indirect_export_entries, Vector star_export_entries, - RefPtr default_export); + RefPtr default_export); virtual void visit_edges(Cell::Visitor&) override; @@ -48,7 +48,7 @@ private: Vector m_indirect_export_entries; // [[IndirectExportEntries]] Vector m_star_export_entries; // [[StarExportEntries]] - RefPtr m_default_export; // Note: Not from the spec + RefPtr m_default_export; // Note: Not from the spec }; }