Преглед на файлове

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. :^)
Andreas Kling преди 5 години
родител
ревизия
3c99c27db4
променени са 1 файла, в които са добавени 6 реда и са изтрити 3 реда
  1. 6 3
      Libraries/LibJS/AST.cpp

+ 6 - 3
Libraries/LibJS/AST.cpp

@@ -26,6 +26,7 @@
 
 
 #include <AK/Function.h>
 #include <AK/Function.h>
 #include <AK/HashMap.h>
 #include <AK/HashMap.h>
+#include <AK/ScopeGuard.h>
 #include <AK/StringBuilder.h>
 #include <AK/StringBuilder.h>
 #include <LibJS/AST.h>
 #include <LibJS/AST.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Interpreter.h>
@@ -200,6 +201,11 @@ Value ForStatement::execute(Interpreter& interpreter) const
         interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
         interpreter.enter_scope(*wrapper, {}, ScopeType::Block);
     }
     }
 
 
+    auto wrapper_cleanup = ScopeGuard([&] {
+        if (wrapper)
+            interpreter.exit_scope(*wrapper);
+    });
+
     Value last_value = js_undefined();
     Value last_value = js_undefined();
 
 
     if (m_init) {
     if (m_init) {
@@ -254,9 +260,6 @@ Value ForStatement::execute(Interpreter& interpreter) const
         }
         }
     }
     }
 
 
-    if (wrapper)
-        interpreter.exit_scope(*wrapper);
-
     return last_value;
     return last_value;
 }
 }