From ae6d105f41f7cb1e7afb1cd41f16788954382980 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 14 Nov 2024 20:17:33 +1300 Subject: [PATCH] LibJS: Use a Function to indirectly let Heap visit VM's GC roots This allows the heap to mark cells that it needs to mark as roots without needing to directly reference the VM. --- Libraries/LibJS/Heap/Heap.cpp | 7 ++++--- Libraries/LibJS/Heap/Heap.h | 4 +++- Libraries/LibJS/Runtime/VM.cpp | 4 +++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index 4406060ba8f..47c92c425b7 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -19,7 +20,6 @@ #include #include #include -#include #include #ifdef HAS_ADDRESS_SANITIZER @@ -28,8 +28,9 @@ namespace JS { -Heap::Heap(VM& vm) +Heap::Heap(VM& vm, Function&)> gather_embedder_roots) : HeapBase(vm) + , m_gather_embedder_roots(move(gather_embedder_roots)) { static_assert(HeapBlock::min_possible_cell_size <= 32, "Heap Cell tracking uses too much data!"); m_size_based_cell_allocators.append(make(64)); @@ -259,7 +260,7 @@ void Heap::collect_garbage(CollectionType collection_type, bool print_report) void Heap::gather_roots(HashMap& roots) { - vm().gather_roots(roots); + m_gather_embedder_roots(roots); gather_conservative_roots(roots); for (auto& handle : m_handles) diff --git a/Libraries/LibJS/Heap/Heap.h b/Libraries/LibJS/Heap/Heap.h index 5a38dd25f9d..694a70d81a5 100644 --- a/Libraries/LibJS/Heap/Heap.h +++ b/Libraries/LibJS/Heap/Heap.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -32,7 +33,7 @@ class Heap : public HeapBase { AK_MAKE_NONMOVABLE(Heap); public: - explicit Heap(VM&); + explicit Heap(VM&, Function&)> gather_embedder_roots); ~Heap(); template @@ -145,6 +146,7 @@ private: bool m_collecting_garbage { false }; StackInfo m_stack_info; + Function&)> m_gather_embedder_roots; }; inline void Heap::did_create_handle(Badge, HandleImpl& impl) diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index 111bf8ad760..afc5538bc49 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -62,7 +62,9 @@ static constexpr auto make_single_ascii_character_strings(IndexSequence()); VM::VM(OwnPtr custom_data, ErrorMessages error_messages) - : m_heap(*this) + : m_heap(*this, [this](HashMap& roots) { + gather_roots(roots); + }) , m_error_messages(move(error_messages)) , m_custom_data(move(custom_data)) {