From 3c99c27db4b1bdf7000a8d044efe9d5fdf18dba5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 5 Apr 2020 00:24:32 +0200 Subject: [PATCH] LibJS: Clean up the anonymous wrapper block in "for" using ScopeGuard This ensures that we don't forget to pop the wrapper off of the scope stack when returning early due to an exception or some such. :^) --- Libraries/LibJS/AST.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 52d8e007ff..5305ae7bf1 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; }