浏览代码

LibJS: Make sure we mark everything reachable from the scope stack

This ensures that local variables survive GC.
Andreas Kling 5 年之前
父节点
当前提交
363c40e3f3
共有 3 个文件被更改,包括 17 次插入1 次删除
  1. 2 1
      Libraries/LibJS/Heap.cpp
  2. 13 0
      Libraries/LibJS/Interpreter.cpp
  3. 2 0
      Libraries/LibJS/Interpreter.h

+ 2 - 1
Libraries/LibJS/Heap.cpp

@@ -24,6 +24,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <AK/Badge.h>
 #include <AK/HashTable.h>
 #include <LibJS/Heap.h>
 #include <LibJS/HeapBlock.h>
@@ -72,7 +73,7 @@ void Heap::collect_garbage()
 
 void Heap::collect_roots(HashTable<Cell*>& roots)
 {
-    roots.set(&m_interpreter.global_object());
+    m_interpreter.collect_roots({}, roots);
 
 #ifdef HEAP_DEBUG
     dbg() << "collect_roots:";

+ 13 - 0
Libraries/LibJS/Interpreter.cpp

@@ -24,6 +24,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <AK/Badge.h>
 #include <LibJS/AST.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Object.h>
@@ -100,4 +101,16 @@ Value Interpreter::get_variable(const String& name)
     return global_object().get(name);
 }
 
+void Interpreter::collect_roots(Badge<Heap>, HashTable<Cell*>& roots)
+{
+    roots.set(m_global_object);
+
+    for (auto& scope : m_scope_stack) {
+        for (auto& it : scope.variables) {
+            if (it.value.is_object())
+                roots.set(it.value.as_object());
+        }
+    }
+}
+
 }

+ 2 - 0
Libraries/LibJS/Interpreter.h

@@ -56,6 +56,8 @@ public:
     void set_variable(String name, Value);
     void declare_variable(String name);
 
+    void collect_roots(Badge<Heap>, HashTable<Cell*>&);
+
 private:
     void enter_scope(const ScopeNode&);
     void exit_scope(const ScopeNode&);