mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 18:05:07 +00:00
LibJS: Evaluate for statements in their own scope if necessary
We now evaluate for loops in their own scope if their init statement is a lexical declaration. Evaluating for loops in their own scope allow us to obtain expected behaviour, which means for example, that the block-scoped variables declared in a for statement will be limited to the scope of the for loop's body and statement and not to that of the current scope (i.e the one where the for statement was made)
This commit is contained in:
parent
0659d07241
commit
644b4f4201
4 changed files with 17 additions and 3 deletions
|
@ -95,6 +95,13 @@ Value WhileStatement::execute(Interpreter& interpreter) const
|
|||
|
||||
Value ForStatement::execute(Interpreter& interpreter) const
|
||||
{
|
||||
OwnPtr<BlockStatement> wrapper;
|
||||
|
||||
if (m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) {
|
||||
wrapper = make<BlockStatement>();
|
||||
interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
|
||||
}
|
||||
|
||||
Value last_value = js_undefined();
|
||||
|
||||
if (m_init)
|
||||
|
@ -114,6 +121,9 @@ Value ForStatement::execute(Interpreter& interpreter) const
|
|||
}
|
||||
}
|
||||
|
||||
if (wrapper)
|
||||
interpreter.exit_scope(*wrapper);
|
||||
|
||||
return last_value;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue