1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 19:15:06 +00:00

LibJS: Replace for_each_bound_name with for_each_bound_identifier

Preparation before deleting for_each_bound_name.
This commit is contained in:
Aliaksandr Kalenik 2023-07-20 16:40:14 +02:00 committed by Andreas Kling
parent 608a730bd6
commit 348e43b36d
4 changed files with 64 additions and 47 deletions

View file

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

View file

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

View file

@ -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. // Note: This is handled by for_each_var_scoped_variable_declaration.
// i. For each String vn of the BoundNames of d, do // 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 // 1. If vn is not an element of declaredFunctionNames, then
if (!declared_function_names.contains(name)) { if (!declared_function_names.contains(name)) {
// a. If varEnv is a global Environment Record, then // 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. // a. NOTE: Lexically declared names are only instantiated here but not initialized.
// b. For each element dn of the BoundNames of d, do // 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 // i. If IsConstantDeclaration of d is true, then
if (declaration.is_constant_declaration()) { if (declaration.is_constant_declaration()) {
// 1. Perform ? lexEnv.CreateImmutableBinding(dn, true). // 1. Perform ? lexEnv.CreateImmutableBinding(dn, true).

View file

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