LibGC+Tests: Allow overriding Heap::the() for tests

Add a test-only helper for setting the default GC heap, and use it
from LibGC tests that create their own test heap.
This commit is contained in:
Shannon Booth 2026-05-19 21:19:50 +02:00 committed by Shannon Booth
parent 8eb74bf747
commit 3897f6efb9
4 changed files with 16 additions and 0 deletions

View file

@ -273,6 +273,11 @@ Heap& Heap::the()
return *s_the;
}
void Heap::set_default_heap_for_testing(Heap& heap)
{
s_the = &heap;
}
Heap::Heap(AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> gather_embedder_roots)
: m_gather_embedder_roots(move(gather_embedder_roots))
{

View file

@ -46,6 +46,7 @@ public:
~Heap();
static Heap& the();
static void set_default_heap_for_testing(Heap&);
template<typename T, typename... Args>
Ref<T> allocate(Args&&... args)

View file

@ -33,6 +33,11 @@ static GC::Heap& test_heap()
return *heap;
}
TEST_SETUP
{
GC::Heap::set_default_heap_for_testing(test_heap());
}
class TestVisitor : public GC::Cell::Visitor {
virtual void visit_impl(GC::Cell& cell) override { visited_cells.set(&cell); }
virtual void visit_impl(ReadonlySpan<GC::NanBoxedValue>) override { }

View file

@ -33,6 +33,11 @@ static GC::Heap& test_heap()
return *heap;
}
TEST_SETUP
{
GC::Heap::set_default_heap_for_testing(test_heap());
}
class TestVisitor : public GC::Cell::Visitor {
virtual void visit_impl(GC::Cell& cell) override { visited_cells.set(&cell); }
virtual void visit_impl(ReadonlySpan<GC::NanBoxedValue> span) override { last_nan_span_size = span.size(); }