mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-26 19:29:16 +00:00
LibJS: Limit scope of 'for' loop variables
This required 2 changes: 1. In the parser, create a new variable scope, so the variable is declared in it instead of the scope in which the 'for' is found. 2. On execute, push the variable into the newly created block. Existing code created an empty block (no variables, no arguments) which allows Interpreter::enter_scope() to skip the creation of a new environment, therefore when the variable initializer is executed, it sets the variable to the outer scope. By attaching the variable to the new block, the block gets a new environment. This is only needed for 'let' / 'const' declarations, since 'var' declarations are expected to leak. Fixes: #2103
This commit is contained in:
committed by
Andreas Kling
parent
ab652fa1ee
commit
b184f12aaf
@@ -251,6 +251,9 @@ Value ForStatement::execute(Interpreter& interpreter) const
|
||||
|
||||
if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) {
|
||||
wrapper = create_ast_node<BlockStatement>();
|
||||
NonnullRefPtrVector<VariableDeclaration> decls;
|
||||
decls.append(*static_cast<const VariableDeclaration*>(m_init.ptr()));
|
||||
wrapper->add_variables(decls);
|
||||
interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user