diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 52d8e007ff5..5305ae7bf1e 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -200,6 +201,11 @@ Value ForStatement::execute(Interpreter& interpreter) const interpreter.enter_scope(*wrapper, {}, ScopeType::Block); } + auto wrapper_cleanup = ScopeGuard([&] { + if (wrapper) + interpreter.exit_scope(*wrapper); + }); + Value last_value = js_undefined(); if (m_init) { @@ -254,9 +260,6 @@ Value ForStatement::execute(Interpreter& interpreter) const } } - if (wrapper) - interpreter.exit_scope(*wrapper); - return last_value; }