1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 14:25:08 +00:00

LibJS: Improve function hoisting across blocks

The parser now keeps track of a scope chain so that it can hoist
function declarations to the closest function scope.
This commit is contained in:
Hendi 2021-07-04 03:15:52 +02:00 committed by Linus Groh
parent 72f8d90dc5
commit 38fd980b0c
6 changed files with 79 additions and 22 deletions

View file

@ -84,6 +84,9 @@ const GlobalObject& Interpreter::global_object() const
void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
{
ScopeGuard guard([&] {
for (auto& declaration : scope_node.hoisted_functions()) {
lexical_environment()->put_into_environment(declaration.name(), { js_undefined(), DeclarationKind::Var });
}
for (auto& declaration : scope_node.functions()) {
auto* function = OrdinaryFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), lexical_environment(), declaration.kind(), declaration.is_strict_mode());
vm().set_variable(declaration.name(), function, global_object);