1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

LibJS/Bytecode: Leave FunctionDeclarationInstantantiation in C++

Instead of trying to implement this AO in bytecode, we can just let it
be a C++ thing. Once we implement fast uncaptured locals, we won't even
be calling it super often.
This commit is contained in:
Andreas Kling 2023-06-15 09:14:25 +02:00
parent 17fe2c4822
commit 872d798951
2 changed files with 11 additions and 50 deletions

View file

@ -247,44 +247,7 @@ Bytecode::CodeGenerationErrorOr<void> ScopeNode::generate_bytecode(Bytecode::Gen
generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Var, false, true);
}
} else {
// Perform the steps of FunctionDeclarationInstantiation.
generator.begin_variable_scope(Bytecode::Generator::BindingMode::Var, Bytecode::Generator::SurroundingScopeKind::Function);
pushed_scope_count++;
if (has_lexical_declarations()) {
generator.begin_variable_scope(Bytecode::Generator::BindingMode::Lexical, Bytecode::Generator::SurroundingScopeKind::Function);
pushed_scope_count++;
}
// FIXME: Implement this boi correctly.
(void)for_each_lexically_scoped_declaration([&](Declaration const& declaration) -> ThrowCompletionOr<void> {
auto is_constant_declaration = declaration.is_constant_declaration();
// NOTE: Nothing in the callback throws an exception.
MUST(declaration.for_each_bound_name([&](auto const& name) {
auto index = generator.intern_identifier(name);
if (is_constant_declaration || !generator.has_binding(index)) {
generator.register_binding(index);
generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, is_constant_declaration);
}
}));
if (is<FunctionDeclaration>(declaration)) {
auto& function_declaration = static_cast<FunctionDeclaration const&>(declaration);
if (auto result = function_declaration.generate_bytecode(generator); result.is_error()) {
maybe_error = result.release_error();
// To make `for_each_lexically_scoped_declaration` happy.
return failing_completion;
}
auto const& name = function_declaration.name();
auto index = generator.intern_identifier(name);
if (!generator.has_binding(index)) {
generator.register_binding(index);
generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, false);
}
generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::InitializeOrSet);
}
return {};
});
// FunctionDeclarationInstantiation is handled by the C++ AO.
}
if (maybe_error.has_value())

View file

@ -573,7 +573,6 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
if (!scope_body)
return {};
if (!Bytecode::Interpreter::current()) {
// NOTE: Due to the use of MUST in the callback, an exception should not result from `for_each_lexically_scoped_declaration`.
MUST(scope_body->for_each_lexically_scoped_declaration([&](Declaration const& declaration) {
// NOTE: Due to the use of MUST with `create_immutable_binding` and `create_mutable_binding` below,
@ -585,7 +584,6 @@ ThrowCompletionOr<void> ECMAScriptFunctionObject::function_declaration_instantia
MUST(lex_environment->create_mutable_binding(vm, name, false));
}));
}));
}
auto private_environment = callee_context.private_environment;
for (auto& declaration : functions_to_initialize) {