Skip to content

Truly independent web browser

LibGC: Let root containers hold values that are traced

Andreas Kling9b3ac4b49190
diff --git a/Libraries/LibGC/CAPI.cpp b/Libraries/LibGC/CAPI.cpp
index 1c38993c209..0479f52dd50 100644
--- a/Libraries/LibGC/CAPI.cpp
+++ b/Libraries/LibGC/CAPI.cpp
@@ -13,6 +13,7 @@
#include <LibGC/HeapRegion.h>
#include <LibGC/PrimitiveStorage.h>
#include <LibGC/Root.h>
+#include <LibGC/Rootable.h>
#include <LibGC/Weak.h>
namespace GC {
@@ -78,36 +79,6 @@ public:
}
};
-class RootGatheringVisitor final : public Cell::Visitor {
-public:
- explicit RootGatheringVisitor(HashMap<Cell*, HeapRoot>& roots)
- : m_roots(roots)
- {
- }
-
- virtual void visit_possible_values(ReadonlyBytes) override
- {
- // NB: Roots are gathered precisely.
- VERIFY_NOT_REACHED();
- }
-
-private:
- virtual void visit_impl(Cell& cell) override
- {
- m_roots.set(&cell, HeapRoot { .type = HeapRoot::Type::VM });
- }
-
- virtual void visit_impl(ReadonlySpan<NanBoxedValue> values) override
- {
- for (auto const& value : values) {
- if (value.is_cell())
- visit_impl(value.as_cell());
- }
- }
-
- HashMap<Cell*, HeapRoot>& m_roots;
-};
-
struct CAPI {
static_assert(offsetof(ForeignCell, m_word_owned_by_the_foreign_implementation) == 0);
static_assert(offsetof(ForeignCell, m_mark) == offsetof(Cell, m_mark));
@@ -237,7 +208,7 @@ GCHeap* gc_heap_create(GCGatherRootsCallback gather_roots, void* context, bool b
{
auto* heap = new Heap(
[gather_roots, context](HashMap<Cell*, HeapRoot>& roots) {
- RootGatheringVisitor visitor { roots };
+ Detail::RootGatheringVisitor visitor { roots, HeapRoot::Type::VM };
gather_roots(context, reinterpret_cast<GCVisitor*>(static_cast<Cell::Visitor*>(&visitor)));
},
become_thread_default ? Heap::BecomeThreadDefault::Yes : Heap::BecomeThreadDefault::No);
diff --git a/Libraries/LibGC/CMakeLists.txt b/Libraries/LibGC/CMakeLists.txt
index f9bd3ecb746..4f821f8e022 100644
--- a/Libraries/LibGC/CMakeLists.txt
+++ b/Libraries/LibGC/CMakeLists.txt
@@ -11,6 +11,7 @@ set(SOURCES
RootHashMap.cpp
RootHashTable.cpp
RootVector.cpp
+ Rootable.cpp
Heap.cpp
HeapGroup.cpp
HeapBlock.cpp
diff --git a/Libraries/LibGC/CaptureTraits.h b/Libraries/LibGC/CaptureTraits.h
index 87e8a0b3eeb..e43f53c9e04 100644
--- a/Libraries/LibGC/CaptureTraits.h
+++ b/Libraries/LibGC/CaptureTraits.h
@@ -159,6 +159,11 @@ struct IsTracedCapture<Vector<T, inline_capacity>> {
static constexpr bool value = IsCapturable<T>;
};
+template<typename T, size_t size>
+struct IsTracedCapture<Array<T, size>> {
+ static constexpr bool value = IsCapturable<T>;
+};
+
template<typename... Ts>
struct IsTracedCapture<Variant<Ts...>> {
static constexpr bool value = (IsCapturable<Ts> && ...);
diff --git a/Libraries/LibGC/Cell.h b/Libraries/LibGC/Cell.h
index eff84d96955..b4862c72f52 100644
--- a/Libraries/LibGC/Cell.h
+++ b/Libraries/LibGC/Cell.h
@@ -186,6 +186,13 @@ public:
visit_impl(ReadonlySpan<NanBoxedValue>(vector.span().data(), vector.size()));
}
+ template<typename T, size_t size>
+ void visit(Array<T, size> const& array)
+ requires(IsVisitable<T>::value)
+ {
+ visit(array.span());
+ }
+
template<typename T>
void visit(HashTable<T> const& table)
requires(IsVisitable<T>::value)
diff --git a/Libraries/LibGC/ConservativeHashMap.cpp b/Libraries/LibGC/ConservativeHashMap.cpp
deleted file mode 100644
index 0d5b40a0199..00000000000
--- a/Libraries/LibGC/ConservativeHashMap.cpp
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2026, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/ConservativeHashMap.h>
-#include <LibGC/Heap.h>
-
-namespace GC {
-
-ConservativeHashMapBase::ConservativeHashMapBase()
- : ConservativeHashMapBase(Heap::the())
-{
-}
-
-ConservativeHashMapBase::ConservativeHashMapBase(Heap& heap)
- : m_heap(&heap)
-{
- m_heap->did_create_conservative_hash_map({}, *this);
-}
-
-ConservativeHashMapBase::~ConservativeHashMapBase()
-{
- m_heap->did_destroy_conservative_hash_map({}, *this);
-}
-
-}
diff --git a/Libraries/LibGC/ConservativeHashMap.h b/Libraries/LibGC/ConservativeHashMap.h
deleted file mode 100644
index 281436f5c75..00000000000
--- a/Libraries/LibGC/ConservativeHashMap.h
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright (c) 2026, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Function.h>
-#include <AK/HashMap.h>
-#include <AK/IntrusiveList.h>
-#include <LibGC/Forward.h>
-
-namespace GC {
-
-class GC_API ConservativeHashMapBase {
- AK_MAKE_NONCOPYABLE(ConservativeHashMapBase);
-
-public:
- virtual void for_each_possible_value(AK::Function<void(FlatPtr)> callback) const = 0;
-
-protected:
- ConservativeHashMapBase();
- explicit ConservativeHashMapBase(Heap&);
- ~ConservativeHashMapBase();
-
- Heap* m_heap { nullptr };
- IntrusiveListNode<ConservativeHashMapBase> m_list_node;
-
-public:
- using List = IntrusiveList<&ConservativeHashMapBase::m_list_node>;
-};
-
-template<typename K, typename V, typename KeyTraits = Traits<K>, typename ValueTraits = Traits<V>, bool IsOrdered = false>
-class GC_API ConservativeHashMap final
- : public ConservativeHashMapBase
- , public HashMap<K, V, KeyTraits, ValueTraits, IsOrdered> {
-
- using HashMapBase = HashMap<K, V, KeyTraits, ValueTraits, IsOrdered>;
-
-public:
- ConservativeHashMap()
- : ConservativeHashMapBase()
- {
- }
-
- ConservativeHashMap(ConservativeHashMap const& other)
- : ConservativeHashMapBase(*other.m_heap)
- , HashMapBase(static_cast<HashMapBase const&>(other))
- {
- }
-
- ConservativeHashMap(ConservativeHashMap&& other)
- : ConservativeHashMapBase(*other.m_heap)
- , HashMapBase(move(static_cast<HashMapBase&>(other)))
- {
- }
-
- ConservativeHashMap& operator=(ConservativeHashMap const& other)
- {
- if (&other == this)
- return *this;
- HashMapBase::operator=(static_cast<HashMapBase const&>(other));
- return *this;
- }
-
- ~ConservativeHashMap() = default;
-
- virtual void for_each_possible_value(AK::Function<void(FlatPtr)> callback) const override
- {
- auto scan_bytes = [&](ReadonlyBytes bytes) {
- for (size_t i = 0; i + sizeof(FlatPtr) <= bytes.size(); i += sizeof(FlatPtr)) {
- FlatPtr value;
- memcpy(&value, bytes.offset(i), sizeof(FlatPtr));
- callback(value);
- }
- };
- for (auto& [key, value] : *this) {
- scan_bytes({ &key, sizeof(K) });
- scan_bytes({ &value, sizeof(V) });
- }
- }
-};
-
-template<typename K, typename V, typename KeyTraits = Traits<K>, typename ValueTraits = Traits<V>>
-using OrderedConservativeHashMap = ConservativeHashMap<K, V, KeyTraits, ValueTraits, true>;
-
-}
diff --git a/Libraries/LibGC/ConservativeHashTable.cpp b/Libraries/LibGC/ConservativeHashTable.cpp
deleted file mode 100644
index d407decd3e4..00000000000
--- a/Libraries/LibGC/ConservativeHashTable.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2024, Andreas Kling <[email protected]>
- * Copyright (c) 2026, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/ConservativeHashTable.h>
-#include <LibGC/Heap.h>
-
-namespace GC {
-
-ConservativeHashTableBase::ConservativeHashTableBase()
- : ConservativeHashTableBase(Heap::the())
-{
-}
-
-ConservativeHashTableBase::ConservativeHashTableBase(Heap& heap)
- : m_heap(&heap)
-{
- m_heap->did_create_conservative_hash_table({}, *this);
-}
-
-ConservativeHashTableBase::~ConservativeHashTableBase()
-{
- m_heap->did_destroy_conservative_hash_table({}, *this);
-}
-
-}
diff --git a/Libraries/LibGC/ConservativeHashTable.h b/Libraries/LibGC/ConservativeHashTable.h
deleted file mode 100644
index 8075d966e89..00000000000
--- a/Libraries/LibGC/ConservativeHashTable.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (c) 2024, Andreas Kling <[email protected]>
- * Copyright (c) 2026, Luke Wilde <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Function.h>
-#include <AK/HashTable.h>
-#include <AK/IntrusiveList.h>
-#include <LibGC/Forward.h>
-
-namespace GC {
-
-class GC_API ConservativeHashTableBase {
- AK_MAKE_NONCOPYABLE(ConservativeHashTableBase);
-
-public:
- virtual void for_each_possible_value(AK::Function<void(FlatPtr)> callback) const = 0;
-
-protected:
- ConservativeHashTableBase();
- explicit ConservativeHashTableBase(Heap&);
- ~ConservativeHashTableBase();
-
- Heap* m_heap { nullptr };
- IntrusiveListNode<ConservativeHashTableBase> m_list_node;
-
-public:
- using List = IntrusiveList<&ConservativeHashTableBase::m_list_node>;
-};
-
-template<typename T, typename TraitsForT = Traits<T>, bool IsOrdered = false>
-class GC_API ConservativeHashTable final
- : public ConservativeHashTableBase
- , public HashTable<T, TraitsForT, IsOrdered> {
-
- using HashTableBase = HashTable<T, TraitsForT, IsOrdered>;
-
-public:
- ConservativeHashTable()
- : ConservativeHashTableBase()
- {
- }
-
- ConservativeHashTable(ConservativeHashTable const& other)
- : ConservativeHashTableBase(*other.m_heap)
- , HashTableBase(static_cast<HashTableBase const&>(other))
- {
- }
-
- ConservativeHashTable(ConservativeHashTable&& other)
- : ConservativeHashTableBase(*other.m_heap)
- , HashTableBase(move(static_cast<HashTableBase&>(other)))
- {
- }
-
- ConservativeHashTable& operator=(ConservativeHashTable const& other)
- {
- if (&other == this)
- return *this;
- HashTableBase::operator=(static_cast<HashTableBase const&>(other));
- return *this;
- }
-
- ~ConservativeHashTable() = default;
-
- virtual void for_each_possible_value(AK::Function<void(FlatPtr)> callback) const override
- {
- for (auto& entry : *this) {
- auto entry_bytes = ReadonlyBytes { &entry, sizeof(T) };
- for (size_t i = 0; i + sizeof(FlatPtr) <= entry_bytes.size(); i += sizeof(FlatPtr)) {
- FlatPtr value;
- memcpy(&value, entry_bytes.offset(i), sizeof(FlatPtr));
- callback(value);
- }
- }
- }
-};
-
-template<typename T, typename TraitsForT = Traits<T>>
-using OrderedConservativeHashTable = ConservativeHashTable<T, TraitsForT, true>;
-
-}
diff --git a/Libraries/LibGC/RootHashMap.h b/Libraries/LibGC/RootHashMap.h
index 40e92fe64a7..440e7bd6a2d 100644
--- a/Libraries/LibGC/RootHashMap.h
+++ b/Libraries/LibGC/RootHashMap.h
@@ -81,10 +81,10 @@ public:
virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>& roots) const override
{
- static constexpr bool KeyIsGCType = Detail::RootableValueTraits<K>::is_rootable;
- static constexpr bool ValueIsGCType = Detail::RootableValueTraits<V>::is_rootable;
+ static constexpr bool KeyIsGCType = Detail::IsRootableValue<K>;
+ static constexpr bool ValueIsGCType = Detail::IsRootableValue<V>;
static_assert(KeyIsGCType || ValueIsGCType,
- "RootHashMap requires at least one of key or value types to be convertible to Cell const* or derive from NanBoxedValue");
+ "RootHashMap requires its key or value type to be a cell, derive from NanBoxedValue, or be a traced value");
for (auto& [key, value] : *this) {
if constexpr (KeyIsGCType)
Detail::gather_root(roots, key, HeapRoot::Type::RootHashMap);
diff --git a/Libraries/LibGC/RootHashTable.h b/Libraries/LibGC/RootHashTable.h
index ee4a2209b94..4ae0fe7e73c 100644
--- a/Libraries/LibGC/RootHashTable.h
+++ b/Libraries/LibGC/RootHashTable.h
@@ -88,8 +88,8 @@ public:
virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>& roots) const override
{
- static_assert(Detail::RootableValueTraits<T>::is_rootable,
- "RootHashTable element type must be convertible to Cell const* or derive from NanBoxedValue");
+ static_assert(Detail::IsRootableValue<T>,
+ "RootHashTable element type must be a cell, derive from NanBoxedValue, or be a traced value");
for (auto& value : *this)
Detail::gather_root(roots, value, HeapRoot::Type::RootHashTable);
}
diff --git a/Libraries/LibGC/RootVector.h b/Libraries/LibGC/RootVector.h
index 6bf53bc72e1..7080fed39d7 100644
--- a/Libraries/LibGC/RootVector.h
+++ b/Libraries/LibGC/RootVector.h
@@ -103,8 +103,8 @@ public:
virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>& roots) const override
{
- static_assert(Detail::RootableValueTraits<T>::is_rootable,
- "RootVector element type must be convertible to Cell const* or derive from NanBoxedValue");
+ static_assert(Detail::IsRootableValue<T>,
+ "RootVector element type must be a cell, derive from NanBoxedValue, or be a traced value");
for (auto& value : *this)
Detail::gather_root(roots, value, HeapRoot::Type::RootVector);
}
diff --git a/Libraries/LibGC/Rootable.cpp b/Libraries/LibGC/Rootable.cpp
new file mode 100644
index 00000000000..30196135c8d
--- /dev/null
+++ b/Libraries/LibGC/Rootable.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGC/Rootable.h>
+
+namespace GC::Detail {
+
+void RootGatheringVisitor::visit_possible_values(ReadonlyBytes)
+{
+ // NB: Roots are gathered precisely.
+ VERIFY_NOT_REACHED();
+}
+
+void RootGatheringVisitor::visit_impl(Cell& cell)
+{
+ m_roots.set(&cell, HeapRoot { .type = m_root_type });
+}
+
+void RootGatheringVisitor::visit_impl(ReadonlySpan<NanBoxedValue> values)
+{
+ for (auto const& value : values) {
+ if (value.is_cell())
+ visit_impl(value.as_cell());
+ }
+}
+
+}
diff --git a/Libraries/LibGC/Rootable.h b/Libraries/LibGC/Rootable.h
index 65435c219d0..882d3fe00d2 100644
--- a/Libraries/LibGC/Rootable.h
+++ b/Libraries/LibGC/Rootable.h
@@ -6,6 +6,8 @@
#pragma once
+#include <AK/HashMap.h>
+#include <LibGC/CaptureTraits.h>
#include <LibGC/Cell.h>
#include <LibGC/HeapRoot.h>
#include <LibGC/Ptr.h>
@@ -57,11 +59,51 @@ struct RootableValueTraits<Ptr<T>> {
}
};
+// Records every cell that a traced value visits as a root.
+class GC_API RootGatheringVisitor final : public Cell::Visitor {
+public:
+ RootGatheringVisitor(HashMap<Cell*, HeapRoot>& roots, HeapRoot::Type root_type)
+ : m_roots(roots)
+ , m_root_type(root_type)
+ {
+ }
+
+ virtual void visit_possible_values(ReadonlyBytes) override;
+
+private:
+ virtual void visit_impl(Cell&) override;
+ virtual void visit_impl(ReadonlySpan<NanBoxedValue>) override;
+
+ HashMap<Cell*, HeapRoot>& m_roots;
+ HeapRoot::Type m_root_type;
+};
+
+// A root container holds cells, values that may be cells, or values whose cells their visit_edges() names. Such a
+// value follows the rules of what a GC::Function captures: everything in it is traced or holds no cell edges, and it
+// holds no root of its own.
+template<typename T>
+inline constexpr bool IsRootableValue = RootableValueTraits<T>::is_rootable || (IsCapturable<T> && !IsUntracedCapture<T>::value);
+
template<typename T>
static void gather_root(HashMap<Cell*, GC::HeapRoot>& roots, T const& value, HeapRoot::Type root_type)
{
- if (auto* cell = RootableValueTraits<T>::cell(value))
- roots.set(cell, HeapRoot { .type = root_type });
+ if constexpr (RootableValueTraits<T>::is_rootable) {
+ if (auto* cell = RootableValueTraits<T>::cell(value))
+ roots.set(cell, HeapRoot { .type = root_type });
+ } else if constexpr (IsSpecializationOf<T, Variant>) {
+ // NB: The alternatives are rooted as they are rather than visited, which needs no type of a cell among them to
+ // be complete wherever a vector of them is.
+ value.visit([&](auto const& alternative) {
+ if constexpr (IsRootableValue<RemoveCVReference<decltype(alternative)>>)
+ gather_root(roots, alternative, root_type);
+ });
+ } else if constexpr (IsSpecializationOf<T, Optional>) {
+ if (value.has_value())
+ gather_root(roots, value.value(), root_type);
+ } else {
+ RootGatheringVisitor visitor { roots, root_type };
+ visit_capture_member(visitor, value);
+ }
}
}
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
index c0ea9f4e10f..493611e5fb0 100644
--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp
+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
@@ -1487,6 +1487,11 @@ static bool is_container_scanned_by_gc(std::string const& template_name)
return template_names.contains(template_name);
}
+static bool is_root_container(std::string const& template_name)
+{
+ return template_name == "GC::RootHashMap" || template_name == "GC::RootHashTable" || template_name == "GC::RootVector";
+}
+
static bool is_value_wrapper(std::string const& template_name)
{
static std::unordered_set<std::string> const template_names {
@@ -1560,6 +1565,9 @@ static std::optional<clang::QualType> find_container_with_unscanned_gc_pointers(
}
if (is_value_wrapper(instance->name) || is_container_scanned_by_gc(instance->name)) {
for (auto const& type_argument : instance->type_arguments) {
+ // NB: A root container traces a value with a visit_edges(), which visits what the value keeps on the heap.
+ if (is_root_container(instance->name) && type_has_visit_edges_method(type_argument->getAsCXXRecordDecl()))
+ continue;
if (auto container = find_container_with_unscanned_gc_pointers(type_argument, visited, subclasses_of_owned_types))
return container;
}
diff --git a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
index 4abb8bb7d45..41617ba3322 100644
--- a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
+++ b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
@@ -35,6 +35,12 @@ struct State {
struct DerivedState : State { };
+struct TracedState {
+ Vector<GC::Ref<GC::Cell>> cells;
+
+ void visit_edges(GC::Cell::Visitor& visitor) { visitor.visit(cells); }
+};
+
struct DerivedEntry : Entry { };
struct EntryWithArray {
@@ -166,6 +172,8 @@ void rooted_or_not_holding_gc_pointers(State& state)
GC::RootVector<GC::Ref<GC::Cell>> root_groups[2];
Vector<GC::Root<GC::Cell>> roots;
GC::ConservativeVector<Entry> conservative_entries;
+ GC::RootVector<TracedState> traced_states;
+ GC::RootHashMap<int, TracedState> traced_state_map;
GC::RootHashMap<int, GC::Ref<GC::Cell>> root_map;
GC::RootHashTable<GC::Ptr<GC::Cell>> root_table;
Vector<GC::RawPtr<GC::Cell>> raw_ptrs;
diff --git a/Tests/ClangPlugins/LibJSGCTests/root_container_non_gc_type.cpp b/Tests/ClangPlugins/LibJSGCTests/root_container_non_gc_type.cpp
index e1a054d2c3a..a7a1f1e77f2 100644
--- a/Tests/ClangPlugins/LibJSGCTests/root_container_non_gc_type.cpp
+++ b/Tests/ClangPlugins/LibJSGCTests/root_container_non_gc_type.cpp
@@ -13,7 +13,7 @@
// RootVector with a non-GC element type should fail.
void test_root_vector_non_gc_type(GC::Heap& heap)
{
- // expected-error@*{{RootVector element type must be convertible to Cell const* or derive from NanBoxedValue}}
+ // expected-error@*{{RootVector element type must be a cell, derive from NanBoxedValue, or be a traced value}}
// expected-note@+1 {{in instantiation of member function}}
GC::RootVector<int> bad_vector;
}
@@ -21,7 +21,7 @@ void test_root_vector_non_gc_type(GC::Heap& heap)
// RootHashMap where neither key nor value is a GC type should fail.
void test_root_hash_map_non_gc_types(GC::Heap& heap)
{
- // expected-error@*{{RootHashMap requires at least one of key or value types to be convertible to Cell const* or derive from NanBoxedValue}}
+ // expected-error@*{{RootHashMap requires its key or value type to be a cell, derive from NanBoxedValue, or be a traced value}}
// expected-note@+1 {{in instantiation of member function}}
GC::RootHashMap<int, int> bad_map;
}
@@ -29,7 +29,7 @@ void test_root_hash_map_non_gc_types(GC::Heap& heap)
// RootHashTable with a non-GC element type should fail.
void test_root_hash_table_non_gc_type(GC::Heap& heap)
{
- // expected-error@*{{RootHashTable element type must be convertible to Cell const* or derive from NanBoxedValue}}
+ // expected-error@*{{RootHashTable element type must be a cell, derive from NanBoxedValue, or be a traced value}}
// expected-note@+1 {{in instantiation of member function}}
GC::RootHashTable<int> bad_table;
}
diff --git a/Tests/LibGC/TestGCContainers.cpp b/Tests/LibGC/TestGCContainers.cpp
index 6d058dedbc7..fc5ae7842bf 100644
--- a/Tests/LibGC/TestGCContainers.cpp
+++ b/Tests/LibGC/TestGCContainers.cpp
@@ -289,6 +289,98 @@ TEST_CASE(root_vector_native_member_survives_relocation)
heap.collect_garbage();
}
+struct TracedRecord {
+ GC::Ref<TestCell> cell;
+ GC::Ptr<TestCell> other_cell;
+ int payload { 0 };
+
+ void visit_edges(GC::Cell::Visitor& visitor)
+ {
+ visitor.visit(cell);
+ visitor.visit(other_cell);
+ }
+};
+
+struct PlainRecord {
+ int payload { 0 };
+};
+
+static_assert(GC::Detail::IsRootableValue<GC::Ref<TestCell>>);
+static_assert(GC::Detail::IsRootableValue<TracedRecord>);
+static_assert(GC::Detail::IsRootableValue<Variant<GC::Ref<TestCell>, Empty>>);
+static_assert(GC::Detail::IsRootableValue<Optional<TracedRecord>>);
+static_assert(GC::Detail::IsRootableValue<Array<GC::Ref<TestCell>, 2>>);
+static_assert(!GC::Detail::IsRootableValue<int>);
+static_assert(!GC::Detail::IsRootableValue<PlainRecord>);
+
+TEST_CASE(root_vector_of_traced_values_reports_their_cells)
+{
+ auto& heap = test_heap();
+ GC::RootVector<TracedRecord> vector;
+
+ auto cell = heap.allocate<TestCell>();
+ auto other_cell = heap.allocate<TestCell>();
+ vector.append({ cell, other_cell, 1 });
+ vector.append({ cell, nullptr, 2 });
+
+ HashMap<GC::Cell*, GC::HeapRoot> roots;
+ vector.gather_roots(roots);
+
+ EXPECT(roots.contains(cell.ptr()));
+ EXPECT(roots.contains(other_cell.ptr()));
+ EXPECT_EQ(roots.size(), 2u);
+ EXPECT_EQ(roots.get(cell.ptr())->type, GC::HeapRoot::Type::RootVector);
+}
+
+TEST_CASE(root_vector_of_variants_reports_their_cells)
+{
+ auto& heap = test_heap();
+ GC::RootVector<Variant<GC::Ref<TestCell>, Empty>> vector;
+
+ auto cell = heap.allocate<TestCell>();
+ vector.append(cell);
+ vector.append(Empty {});
+
+ HashMap<GC::Cell*, GC::HeapRoot> roots;
+ vector.gather_roots(roots);
+
+ EXPECT(roots.contains(cell.ptr()));
+ EXPECT_EQ(roots.size(), 1u);
+}
+
+TEST_CASE(root_hash_map_traced_value_reports_its_cells)
+{
+ auto& heap = test_heap();
+ GC::RootHashMap<int, TracedRecord> map;
+
+ auto cell = heap.allocate<TestCell>();
+ map.set(1, { cell, nullptr, 1 });
+
+ HashMap<GC::Cell*, GC::HeapRoot> roots;
+ map.gather_roots(roots);
+
+ EXPECT(roots.contains(cell.ptr()));
+ EXPECT_EQ(roots.size(), 1u);
+ EXPECT_EQ(roots.get(cell.ptr())->type, GC::HeapRoot::Type::RootHashMap);
+}
+
+TEST_CASE(root_vector_of_traced_values_keeps_their_cells_alive)
+{
+ auto& heap = test_heap();
+ GC::Weak<TestCell> weak_cell;
+ GC::RootVector<TracedRecord> vector;
+
+ {
+ auto cell = heap.allocate<TestCell>();
+ weak_cell = cell.ptr();
+ vector.append({ cell, nullptr, 1 });
+ }
+
+ GC::scrub_stack_for_testing();
+ heap.collect_garbage();
+ EXPECT(weak_cell);
+}
+
TEST_CASE(conservative_vector_adopts_moved_vector)
{
auto& heap = test_heap();