1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

LibJS: Add Handle<T>, a strong C++ handle for keeping GC objects alive

This is pretty heavy and unoptimized, but it will do the trick for now.
Basically, Heap now has a HashTable<HandleImpl*> and you can call
JS::make_handle(T*) to construct a Handle<T> that guarantees that the
pointee will always survive GC until the Handle<T> is destroyed.
This commit is contained in:
Andreas Kling 2020-03-18 20:03:17 +01:00
parent e265058768
commit a119b61782
6 changed files with 103 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <AK/Badge.h>
#include <AK/HashTable.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Heap/HeapBlock.h>
#include <LibJS/Interpreter.h>
@ -79,6 +80,9 @@ void Heap::gather_roots(HashTable<Cell*>& roots)
gather_conservative_roots(roots);
for (auto* handle : m_handles)
roots.set(handle->cell());
#ifdef HEAP_DEBUG
dbg() << "gather_roots:";
for (auto* root : roots) {
@ -196,4 +200,17 @@ void Heap::sweep_dead_cells()
});
}
}
void Heap::did_create_handle(Badge<HandleImpl>, HandleImpl& impl)
{
ASSERT(!m_handles.contains(&impl));
m_handles.set(&impl);
}
void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
{
ASSERT(m_handles.contains(&impl));
m_handles.remove(&impl);
}
}