From 363c40e3f33b6b7e07eb82c1d7998574ef0695bc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 9 Mar 2020 21:29:22 +0100 Subject: [PATCH] LibJS: Make sure we mark everything reachable from the scope stack This ensures that local variables survive GC. --- Libraries/LibJS/Heap.cpp | 3 ++- Libraries/LibJS/Interpreter.cpp | 13 +++++++++++++ Libraries/LibJS/Interpreter.h | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Heap.cpp b/Libraries/LibJS/Heap.cpp index 33ce668c54..bd0ec319b8 100644 --- a/Libraries/LibJS/Heap.cpp +++ b/Libraries/LibJS/Heap.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -72,7 +73,7 @@ void Heap::collect_garbage() void Heap::collect_roots(HashTable& roots) { - roots.set(&m_interpreter.global_object()); + m_interpreter.collect_roots({}, roots); #ifdef HEAP_DEBUG dbg() << "collect_roots:"; diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 3c8f155825..701782a121 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -100,4 +101,16 @@ Value Interpreter::get_variable(const String& name) return global_object().get(name); } +void Interpreter::collect_roots(Badge, HashTable& 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()); + } + } +} + } diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index e13dc8e518..0c229c182b 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -56,6 +56,8 @@ public: void set_variable(String name, Value); void declare_variable(String name); + void collect_roots(Badge, HashTable&); + private: void enter_scope(const ScopeNode&); void exit_scope(const ScopeNode&);