mirror of
https://github.com/RGBCube/serenity
synced 2025-07-03 00:42:14 +00:00
LibJS: Ensure function declarations don't leak outside function scopes
When using VM::set_variable() to put the created ScriptFunction onto a ScopeObject, we would previously unexpectedly reach the global object as set_variable() checks each traversed scope for an existing Variable with the given name - which would cause a leak of the inner function past the outer function (we even had a test expecting that behaviour!). Now we first declare functions (as DeclarationKind::Var) before setting them. This will need some more work to make hoisting across non-lexical scopes work, but it fixes this specific issue for now. Fixes #6766.
This commit is contained in:
parent
b221cad659
commit
a92dc4e30d
3 changed files with 31 additions and 10 deletions
|
@ -1,9 +1,11 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/AST.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
|
@ -80,13 +82,17 @@ const GlobalObject& Interpreter::global_object() const
|
|||
|
||||
void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
|
||||
{
|
||||
for (auto& declaration : scope_node.functions()) {
|
||||
auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
|
||||
vm().set_variable(declaration.name(), function, global_object);
|
||||
}
|
||||
ScopeGuard guard([&] {
|
||||
for (auto& declaration : scope_node.functions()) {
|
||||
auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
|
||||
vm().set_variable(declaration.name(), function, global_object);
|
||||
}
|
||||
});
|
||||
|
||||
if (scope_type == ScopeType::Function) {
|
||||
push_scope({ scope_type, scope_node, false });
|
||||
for (auto& declaration : scope_node.functions())
|
||||
current_scope()->put_to_scope(declaration.name(), { js_undefined(), DeclarationKind::Var });
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue