mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +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:
parent
e265058768
commit
a119b61782
Notes:
sideshowbarker
2024-07-19 08:15:24 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a119b617829
6 changed files with 103 additions and 0 deletions
|
@ -33,6 +33,7 @@ class Argument;
|
|||
class Cell;
|
||||
class Expression;
|
||||
class Function;
|
||||
class HandleImpl;
|
||||
class Heap;
|
||||
class HeapBlock;
|
||||
class Interpreter;
|
||||
|
@ -42,4 +43,7 @@ class ScopeNode;
|
|||
class Value;
|
||||
enum class DeclarationType;
|
||||
|
||||
template<class T>
|
||||
class Handle;
|
||||
|
||||
}
|
||||
|
|
18
Libraries/LibJS/Heap/Handle.cpp
Normal file
18
Libraries/LibJS/Heap/Handle.cpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Runtime/Cell.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
HandleImpl::HandleImpl(Cell* cell)
|
||||
: m_cell(cell)
|
||||
{
|
||||
m_cell->heap().did_create_handle({}, *this);
|
||||
}
|
||||
|
||||
HandleImpl::~HandleImpl()
|
||||
{
|
||||
m_cell->heap().did_destroy_handle({}, *this);
|
||||
}
|
||||
|
||||
}
|
57
Libraries/LibJS/Heap/Handle.h
Normal file
57
Libraries/LibJS/Heap/Handle.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibJS/Forward.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class HandleImpl : public RefCounted<HandleImpl> {
|
||||
AK_MAKE_NONCOPYABLE(HandleImpl);
|
||||
AK_MAKE_NONMOVABLE(HandleImpl);
|
||||
|
||||
public:
|
||||
~HandleImpl();
|
||||
|
||||
Cell* cell() { return m_cell; }
|
||||
const Cell* cell() const { return m_cell; }
|
||||
|
||||
private:
|
||||
template<class T>
|
||||
friend class Handle;
|
||||
|
||||
explicit HandleImpl(Cell*);
|
||||
Cell* m_cell { nullptr };
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class Handle {
|
||||
public:
|
||||
Handle() {}
|
||||
|
||||
static Handle create(T* cell)
|
||||
{
|
||||
return Handle(adopt(*new HandleImpl(cell)));
|
||||
}
|
||||
|
||||
T* cell() { return static_cast<T*>(m_impl->cell()); }
|
||||
const T* cell() const { return static_cast<const T*>(m_impl->cell()); }
|
||||
|
||||
private:
|
||||
explicit Handle(NonnullRefPtr<HandleImpl> impl)
|
||||
: m_impl(move(impl))
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<HandleImpl> m_impl;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline Handle<T> make_handle(T* cell)
|
||||
{
|
||||
return Handle<T>::create(cell);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,11 +26,13 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibJS/Runtime/Cell.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -58,6 +60,9 @@ public:
|
|||
bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
|
||||
void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
|
||||
|
||||
void did_create_handle(Badge<HandleImpl>, HandleImpl&);
|
||||
void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
|
||||
|
||||
private:
|
||||
Cell* allocate_cell(size_t);
|
||||
|
||||
|
@ -72,6 +77,7 @@ private:
|
|||
|
||||
Interpreter& m_interpreter;
|
||||
Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
|
||||
HashTable<HandleImpl*> m_handles;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
OBJS = \
|
||||
AST.o \
|
||||
Heap/Handle.o \
|
||||
Heap/Heap.o \
|
||||
Heap/HeapBlock.o \
|
||||
Interpreter.o \
|
||||
|
|
Loading…
Add table
Reference in a new issue