mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 12:19:54 +00:00
LibJS: Don't directly teach the heap about the javascript VM or Realm
Instead, smuggle it in as a `void*` private data and let Javascript aware code cast out that pointer to a VM&. In order to make this split, rename JS::Cell to JS::CellImpl. Once we have a LibGC, this will become GC::Cell. CellImpl then has no specific knowledge of the VM& and Realm&. That knowledge is instead put into JS::Cell, which inherits from CellImpl. JS::Cell is responsible for JavaScript's realm initialization, as well as converting of the void* private data to what it knows should be the VM&.
This commit is contained in:
parent
ae6d105f41
commit
c2988a7dd5
Notes:
github-actions[bot]
2024-11-14 14:39:37 +00:00
Author: https://github.com/shannonbooth
Commit: c2988a7dd5
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2334
27 changed files with 346 additions and 296 deletions
|
@ -6,200 +6,17 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Heap/Internals.h>
|
||||
#include <LibJS/Heap/CellImpl.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
// This instrumentation tells analysis tooling to ignore a potentially mis-wrapped GC-allocated member variable
|
||||
// It should only be used when the lifetime of the GC-allocated member is always longer than the object
|
||||
#if defined(AK_COMPILER_CLANG)
|
||||
# define IGNORE_GC [[clang::annotate("serenity::ignore_gc")]]
|
||||
#else
|
||||
# define IGNORE_GC
|
||||
#endif
|
||||
|
||||
#define JS_CELL(class_, base_class) \
|
||||
public: \
|
||||
using Base = base_class; \
|
||||
virtual StringView class_name() const override \
|
||||
{ \
|
||||
return #class_##sv; \
|
||||
} \
|
||||
friend class JS::Heap;
|
||||
|
||||
class Cell : public Weakable<Cell> {
|
||||
AK_MAKE_NONCOPYABLE(Cell);
|
||||
AK_MAKE_NONMOVABLE(Cell);
|
||||
class Cell : public CellImpl {
|
||||
JS_CELL(Cell, CellImpl);
|
||||
|
||||
public:
|
||||
virtual void initialize(Realm&);
|
||||
virtual ~Cell() = default;
|
||||
|
||||
bool is_marked() const { return m_mark; }
|
||||
void set_marked(bool b) { m_mark = b; }
|
||||
|
||||
enum class State : bool {
|
||||
Live,
|
||||
Dead,
|
||||
};
|
||||
|
||||
State state() const { return m_state; }
|
||||
void set_state(State state) { m_state = state; }
|
||||
|
||||
virtual StringView class_name() const = 0;
|
||||
|
||||
class Visitor {
|
||||
public:
|
||||
void visit(Cell* cell)
|
||||
{
|
||||
if (cell)
|
||||
visit_impl(*cell);
|
||||
}
|
||||
|
||||
void visit(Cell& cell)
|
||||
{
|
||||
visit_impl(cell);
|
||||
}
|
||||
|
||||
void visit(Cell const* cell)
|
||||
{
|
||||
visit(const_cast<Cell*>(cell));
|
||||
}
|
||||
|
||||
void visit(Cell const& cell)
|
||||
{
|
||||
visit(const_cast<Cell&>(cell));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(GCPtr<T> cell)
|
||||
{
|
||||
if (cell)
|
||||
visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(NonnullGCPtr<T> cell)
|
||||
{
|
||||
visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(ReadonlySpan<T> span)
|
||||
{
|
||||
for (auto& value : span)
|
||||
visit(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(Span<T> span)
|
||||
{
|
||||
for (auto& value : span)
|
||||
visit(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(Vector<T> const& vector)
|
||||
{
|
||||
for (auto& value : vector)
|
||||
visit(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(HashTable<T> const& table)
|
||||
{
|
||||
for (auto& value : table)
|
||||
visit(value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void visit(OrderedHashTable<T> const& table)
|
||||
{
|
||||
for (auto& value : table)
|
||||
visit(value);
|
||||
}
|
||||
|
||||
template<typename K, typename V, typename T>
|
||||
void visit(HashMap<K, V, T> const& map)
|
||||
{
|
||||
for (auto& it : map) {
|
||||
if constexpr (requires { visit(it.key); })
|
||||
visit(it.key);
|
||||
if constexpr (requires { visit(it.value); })
|
||||
visit(it.value);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename K, typename V, typename T>
|
||||
void visit(OrderedHashMap<K, V, T> const& map)
|
||||
{
|
||||
for (auto& it : map) {
|
||||
if constexpr (requires { visit(it.key); })
|
||||
visit(it.key);
|
||||
if constexpr (requires { visit(it.value); })
|
||||
visit(it.value);
|
||||
}
|
||||
}
|
||||
|
||||
void visit(NanBoxedValue const& value);
|
||||
|
||||
// Allow explicitly ignoring a GC-allocated member in a visit_edges implementation instead
|
||||
// of just not using it.
|
||||
template<typename T>
|
||||
void ignore(T const&)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void visit_possible_values(ReadonlyBytes) = 0;
|
||||
|
||||
protected:
|
||||
virtual void visit_impl(Cell&) = 0;
|
||||
virtual ~Visitor() = default;
|
||||
};
|
||||
|
||||
virtual void visit_edges(Visitor&) { }
|
||||
|
||||
// This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
|
||||
virtual void finalize() { }
|
||||
|
||||
// This allows cells to survive GC by choice, even if nothing points to them.
|
||||
// It's used to implement special rules in the web platform.
|
||||
// NOTE: Cells must call set_overrides_must_survive_garbage_collection() for this to be honored.
|
||||
virtual bool must_survive_garbage_collection() const { return false; }
|
||||
|
||||
bool overrides_must_survive_garbage_collection(Badge<Heap>) const { return m_overrides_must_survive_garbage_collection; }
|
||||
|
||||
ALWAYS_INLINE Heap& heap() const { return HeapBlockBase::from_cell(this)->heap(); }
|
||||
ALWAYS_INLINE VM& vm() const { return bit_cast<HeapBase*>(&heap())->vm(); }
|
||||
|
||||
protected:
|
||||
Cell() = default;
|
||||
|
||||
void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; }
|
||||
|
||||
private:
|
||||
bool m_mark : 1 { false };
|
||||
bool m_overrides_must_survive_garbage_collection : 1 { false };
|
||||
State m_state : 1 { State::Live };
|
||||
ALWAYS_INLINE VM& vm() const { return *reinterpret_cast<VM*>(private_data()); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
|
||||
{
|
||||
if (!cell)
|
||||
return builder.put_string("Cell{nullptr}"sv);
|
||||
return Formatter<FormatString>::format(builder, "{}({})"sv, cell->class_name(), cell);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue