diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-09 21:29:22 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-09 21:49:20 +0100 |
commit | 363c40e3f33b6b7e07eb82c1d7998574ef0695bc (patch) | |
tree | 8b5c9f3841e76c4cf3670c20b6132b01f9955593 | |
parent | 26165cd92a1e934cceea4c76323974dd5de44327 (diff) | |
download | serenity-363c40e3f33b6b7e07eb82c1d7998574ef0695bc.zip |
LibJS: Make sure we mark everything reachable from the scope stack
This ensures that local variables survive GC.
-rw-r--r-- | Libraries/LibJS/Heap.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.h | 2 |
3 files changed, 17 insertions, 1 deletions
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 <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:"; 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 <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()); + } + } +} + } 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<Heap>, HashTable<Cell*>&); + private: void enter_scope(const ScopeNode&); void exit_scope(const ScopeNode&); |