Browse Source

LibJS: Replace for_each_bound_name with for_each_bound_identifier

Preparation before deleting for_each_bound_name.
Aliaksandr Kalenik 2 years ago
parent
commit
348e43b36d

+ 24 - 20
Userland/Libraries/LibJS/AST.cpp

@@ -783,8 +783,9 @@ Completion ForStatement::loop_evaluation(Interpreter& interpreter, Vector<Deprec
             loop_env = new_declarative_environment(*old_environment);
             auto is_const = declaration->is_constant_declaration();
             // NOTE: Due to the use of MUST with `create_immutable_binding` and `create_mutable_binding` below,
-            //       an exception should not result from `for_each_bound_name`.
-            MUST(declaration->for_each_bound_name([&](auto const& name) {
+            //       an exception should not result from `for_each_bound_identifier`.
+            MUST(declaration->for_each_bound_identifier([&](auto const& identifier) {
+                auto const& name = identifier.string();
                 if (is_const) {
                     MUST(loop_env->create_immutable_binding(vm, name, true));
                 } else {
@@ -985,8 +986,9 @@ struct ForInOfHeadState {
             // 14.7.5.4 Runtime Semantics: ForDeclarationBindingInstantiation, https://tc39.es/ecma262/#sec-runtime-semantics-fordeclarationbindinginstantiation
             // 1. For each element name of the BoundNames of ForBinding, do
             // NOTE: Due to the use of MUST with `create_immutable_binding` and `create_mutable_binding` below,
-            //       an exception should not result from `for_each_bound_name`.
-            MUST(for_declaration.for_each_bound_name([&](auto const& name) {
+            //       an exception should not result from `for_each_bound_identifier`.
+            MUST(for_declaration.for_each_bound_identifier([&](auto const& identifier) {
+                auto const& name = identifier.string();
                 if (first_name.is_empty())
                     first_name = name;
 
@@ -1081,9 +1083,9 @@ static ThrowCompletionOr<ForInOfHeadState> for_in_of_head_execute(Interpreter& i
             } else {
                 state.lhs_kind = ForInOfHeadState::LexicalBinding;
                 new_environment = new_declarative_environment(*interpreter.lexical_environment());
-                // NOTE: Due to the use of MUST with `create_mutable_binding` below, an exception should not result from `for_each_bound_name`.
-                MUST(variable_declaration.for_each_bound_name([&](auto const& name) {
-                    MUST(new_environment->create_mutable_binding(vm, name, false));
+                // NOTE: Due to the use of MUST with `create_mutable_binding` below, an exception should not result from `for_each_bound_identifier.
+                MUST(variable_declaration.for_each_bound_identifier([&](auto const& identifier) {
+                    MUST(new_environment->create_mutable_binding(vm, identifier.string(), false));
                 }));
             }
         } else {
@@ -1091,9 +1093,9 @@ static ThrowCompletionOr<ForInOfHeadState> for_in_of_head_execute(Interpreter& i
             auto& declaration = static_cast<UsingDeclaration const&>(*(*ast_ptr));
             state.lhs_kind = ForInOfHeadState::LexicalBinding;
             new_environment = new_declarative_environment(*interpreter.lexical_environment());
-            // NOTE: Due to the use of MUST with `create_mutable_binding` below, an exception should not result from `for_each_bound_name`.
-            MUST(declaration.for_each_bound_name([&](auto const& name) {
-                MUST(new_environment->create_mutable_binding(vm, name, false));
+            // NOTE: Due to the use of MUST with `create_mutable_binding` below, an exception should not result from `for_each_bound_identifier.
+            MUST(declaration.for_each_bound_identifier([&](auto const& identifier) {
+                MUST(new_environment->create_mutable_binding(vm, identifier.string(), false));
             }));
         }
 
@@ -3121,8 +3123,8 @@ ThrowCompletionOr<void> VariableDeclaration::for_each_bound_name(ThrowCompletion
                 return callback(id->string());
             },
             [&](NonnullRefPtr<BindingPattern const> const& binding) {
-                return binding->for_each_bound_name([&](auto const& name) {
-                    return callback(name);
+                return binding->for_each_bound_identifier([&](auto const& identifier) {
+                    return callback(identifier.string());
                 });
             }));
     }
@@ -3933,9 +3935,9 @@ Completion TryStatement::execute(Interpreter& interpreter) const
             [&](NonnullRefPtr<BindingPattern const> const& pattern) {
                 // 3. For each element argName of the BoundNames of CatchParameter, do
                 // NOTE: Due to the use of MUST with `create_mutable_binding` below, an exception should not result from `for_each_bound_name`.
-                MUST(pattern->for_each_bound_name([&](auto& name) {
+                MUST(pattern->for_each_bound_identifier([&](auto& identifier) {
                     // a. Perform ! catchEnv.CreateMutableBinding(argName, false).
-                    MUST(catch_environment->create_mutable_binding(vm, name, false));
+                    MUST(catch_environment->create_mutable_binding(vm, identifier.string(), false));
                 }));
             });
 
@@ -4427,8 +4429,8 @@ ThrowCompletionOr<void> ScopeNode::for_each_lexically_scoped_declaration(ThrowCo
 ThrowCompletionOr<void> ScopeNode::for_each_lexically_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const
 {
     for (auto const& declaration : m_lexical_declarations) {
-        TRY(declaration->for_each_bound_name([&](auto const& name) {
-            return callback(name);
+        TRY(declaration->for_each_bound_identifier([&](auto const& identifier) {
+            return callback(identifier.string());
         }));
     }
     return {};
@@ -4447,8 +4449,8 @@ ThrowCompletionOr<void> ScopeNode::for_each_lexically_declared_identifier(ThrowC
 ThrowCompletionOr<void> ScopeNode::for_each_var_declared_name(ThrowCompletionOrVoidCallback<DeprecatedFlyString const&>&& callback) const
 {
     for (auto& declaration : m_var_declarations) {
-        TRY(declaration->for_each_bound_name([&](auto const& name) {
-            return callback(name);
+        TRY(declaration->for_each_bound_identifier([&](auto const& identifier) {
+            return callback(identifier.string());
         }));
     }
     return {};
@@ -4813,7 +4815,8 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(VM& vm, Global
         // Note: This is done in for_each_var_scoped_variable_declaration.
 
         // i. For each String vn of the BoundNames of d, do
-        return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
+        return declaration.for_each_bound_identifier([&](auto const& identifier) -> ThrowCompletionOr<void> {
+            auto const& name = identifier.string();
             // 1. If vn is not an element of declaredFunctionNames, then
             if (declared_function_names.contains(name))
                 return {};
@@ -4893,7 +4896,8 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(VM& vm, Global
     TRY(for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
         // a. NOTE: Lexically declared names are only instantiated here but not initialized.
         // b. For each element dn of the BoundNames of d, do
-        return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
+        return declaration.for_each_bound_identifier([&](auto const& identifier) -> ThrowCompletionOr<void> {
+            auto const& name = identifier.string();
             // i. If IsConstantDeclaration of d is true, then
             if (declaration.is_constant_declaration()) {
                 // 1. Perform ? env.CreateImmutableBinding(dn, true).

+ 31 - 23
Userland/Libraries/LibJS/Parser.cpp

@@ -110,9 +110,9 @@ public:
         ScopePusher scope_pusher(parser, nullptr, ScopeLevel::NotTopLevel, ScopeType::Catch);
         if (pattern) {
             // NOTE: Nothing in the callback throws an exception.
-            MUST(pattern->for_each_bound_name([&](auto const& name) {
-                scope_pusher.m_forbidden_var_names.set(name);
-                scope_pusher.m_bound_names.set(name);
+            MUST(pattern->for_each_bound_identifier([&](auto const& identifier) {
+                scope_pusher.m_forbidden_var_names.set(identifier.string());
+                scope_pusher.m_bound_names.set(identifier.string());
             }));
         } else if (!parameter.is_empty()) {
             scope_pusher.m_var_names.set(parameter);
@@ -149,7 +149,8 @@ public:
     {
         if (declaration->is_lexical_declaration()) {
             // NOTE: Nothing in the callback throws an exception.
-            MUST(declaration->for_each_bound_name([&](auto const& name) {
+            MUST(declaration->for_each_bound_identifier([&](auto const& identifier) {
+                auto const& name = identifier.string();
                 if (m_var_names.contains(name) || m_forbidden_lexical_names.contains(name) || m_function_names.contains(name))
                     throw_identifier_declared(name, declaration);
 
@@ -160,7 +161,8 @@ public:
             m_node->add_lexical_declaration(move(declaration));
         } else if (!declaration->is_function_declaration()) {
             // NOTE: Nothing in the callback throws an exception.
-            MUST(declaration->for_each_bound_name([&](auto const& name) {
+            MUST(declaration->for_each_bound_identifier([&](auto const& identifier) {
+                auto const& name = identifier.string();
                 ScopePusher* pusher = this;
                 while (true) {
                     if (pusher->m_lexical_names.contains(name)
@@ -185,8 +187,8 @@ public:
             if (m_scope_level != ScopeLevel::NotTopLevel && m_scope_level != ScopeLevel::ModuleTopLevel) {
                 // Only non-top levels and Module don't var declare the top functions
                 // NOTE: Nothing in the callback throws an exception.
-                MUST(declaration->for_each_bound_name([&](auto const& name) {
-                    m_var_names.set(name);
+                MUST(declaration->for_each_bound_identifier([&](auto const& identifier) {
+                    m_var_names.set(identifier.string());
                 }));
                 m_node->add_var_scoped_declaration(move(declaration));
             } else {
@@ -257,8 +259,8 @@ public:
                 },
                 [&](NonnullRefPtr<BindingPattern const> const& binding_pattern) {
                     // NOTE: Nothing in the callback throws an exception.
-                    MUST(binding_pattern->for_each_bound_name([&](auto const& name) {
-                        m_forbidden_lexical_names.set(name);
+                    MUST(binding_pattern->for_each_bound_identifier([&](auto const& identifier) {
+                        m_forbidden_lexical_names.set(identifier.string());
                     }));
                 });
         }
@@ -2754,7 +2756,9 @@ NonnullRefPtr<FunctionBody const> Parser::parse_function_body(Vector<FunctionPar
                 },
                 [&](NonnullRefPtr<BindingPattern const> const& binding) {
                     // NOTE: Nothing in the callback throws an exception.
-                    MUST(binding->for_each_bound_name([&](auto& bound_name) {
+                    MUST(binding->for_each_bound_identifier([&](auto& bound_identifier) {
+                        auto const& bound_name = bound_identifier.string();
+
                         if (function_kind == FunctionKind::Generator && bound_name == "yield"sv)
                             syntax_error("Parameter name 'yield' not allowed in this context");
 
@@ -2929,8 +2933,8 @@ Vector<FunctionParameter> Parser::parse_formal_parameters(int& function_length,
                 [&](NonnullRefPtr<BindingPattern const> const& bindings) {
                     bool found_duplicate = false;
                     // NOTE: Nothing in the callback throws an exception.
-                    MUST(bindings->for_each_bound_name([&](auto& bound_name) {
-                        if (bound_name == parameter_name)
+                    MUST(bindings->for_each_bound_identifier([&](auto& bound_identifier) {
+                        if (bound_identifier.string() == parameter_name)
                             found_duplicate = true;
                     }));
                     return found_duplicate;
@@ -3183,7 +3187,8 @@ RefPtr<BindingPattern const> Parser::parse_binding_pattern(Parser::AllowDuplicat
 
     Vector<StringView> bound_names;
     // NOTE: Nothing in the callback throws an exception.
-    MUST(pattern->for_each_bound_name([&](auto& name) {
+    MUST(pattern->for_each_bound_identifier([&](auto& identifier) {
+        auto const& name = identifier.string();
         if (allow_duplicates == AllowDuplicates::No) {
             if (bound_names.contains_slow(name))
                 syntax_error("Duplicate parameter names in bindings");
@@ -3244,8 +3249,8 @@ NonnullRefPtr<VariableDeclaration const> Parser::parse_variable_declaration(IsFo
         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)) {
                 // NOTE: Nothing in the callback throws an exception.
-                MUST(pattern->for_each_bound_name([this](auto& name) {
-                    if (name == "let"sv)
+                MUST(pattern->for_each_bound_identifier([this](auto& identifier) {
+                    if (identifier.string() == "let"sv)
                         syntax_error("Lexical binding may not be called 'let'");
                 }));
             }
@@ -3685,10 +3690,10 @@ NonnullRefPtr<CatchClause const> Parser::parse_catch_clause()
 
     if (pattern_parameter) {
         // NOTE: Nothing in the callback throws an exception.
-        MUST(pattern_parameter->for_each_bound_name(
-            [&](auto& name) {
-                check_identifier_name_for_assignment_validity(name);
-                bound_names.set(name);
+        MUST(pattern_parameter->for_each_bound_identifier(
+            [&](auto& identifier) {
+                check_identifier_name_for_assignment_validity(identifier.string());
+                bound_names.set(identifier.string());
             }));
     }
 
@@ -4888,7 +4893,8 @@ NonnullRefPtr<ExportStatement const> Parser::parse_export_statement(Program& pro
                         },
                         [&](NonnullRefPtr<BindingPattern const> const& binding) {
                             // NOTE: Nothing in the callback throws an exception.
-                            MUST(binding->for_each_bound_name([&](auto& name) {
+                            MUST(binding->for_each_bound_identifier([&](auto& identifier) {
+                                auto const& name = identifier.string();
                                 entries_with_location.append({ ExportEntry::named_export(name, name), decl_position });
                             }));
                         });
@@ -4906,9 +4912,11 @@ NonnullRefPtr<ExportStatement const> Parser::parse_export_statement(Program& pro
                     },
                     [&](NonnullRefPtr<BindingPattern const> const& binding) {
                         // NOTE: Nothing in the callback throws an exception.
-                        MUST(binding->for_each_bound_name([&](auto& name) {
-                            entries_with_location.append({ ExportEntry::named_export(name, name), variable_position });
-                        }));
+                        MUST(binding->for_each_bound_identifier(
+                            [&](auto& identifier) {
+                                auto const& name = identifier.string();
+                                entries_with_location.append({ ExportEntry::named_export(name, name), variable_position });
+                            }));
                     });
             }
             expression = variable_declaration;

+ 6 - 2
Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp

@@ -936,7 +936,9 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& pr
         // Note: This is handled by for_each_var_scoped_variable_declaration.
 
         // i. For each String vn of the BoundNames of d, do
-        return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
+        return declaration.for_each_bound_identifier([&](auto const& identifier) -> ThrowCompletionOr<void> {
+            auto const& name = identifier.string();
+
             // 1. If vn is not an element of declaredFunctionNames, then
             if (!declared_function_names.contains(name)) {
                 // a. If varEnv is a global Environment Record, then
@@ -965,7 +967,9 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& pr
         // a. NOTE: Lexically declared names are only instantiated here but not initialized.
 
         // b. For each element dn of the BoundNames of d, do
-        return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
+        return declaration.for_each_bound_identifier([&](auto const& identifier) -> ThrowCompletionOr<void> {
+            auto const& name = identifier.string();
+
             // i. If IsConstantDeclaration of d is true, then
             if (declaration.is_constant_declaration()) {
                 // 1. Perform ? lexEnv.CreateImmutableBinding(dn, true).

+ 3 - 2
Userland/Libraries/LibJS/SourceTextModule.cpp

@@ -464,8 +464,9 @@ ThrowCompletionOr<void> SourceTextModule::initialize_environment(VM& vm)
     MUST(m_ecmascript_code->for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
         // a. For each element dn of the BoundNames of d, do
         // NOTE: Due to the use of MUST with `create_immutable_binding`, `create_mutable_binding` and `initialize_binding` below,
-        //       an exception should not result from `for_each_bound_name`.
-        MUST(declaration.for_each_bound_name([&](DeprecatedFlyString const& name) {
+        //       an exception should not result from `for_each_bound_identifier`.
+        MUST(declaration.for_each_bound_identifier([&](auto const& identifier) {
+            auto const& name = identifier.string();
             // i. If IsConstantDeclaration of d is true, then
             if (declaration.is_constant_declaration()) {
                 // 1. Perform ! env.CreateImmutableBinding(dn, true).