ladybird/Libraries/LibJS/Heap/Internals.h
Shannon Booth c2988a7dd5 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&.
2024-11-14 15:38:45 +01:00

54 lines
993 B
C++

/*
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2023, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <LibJS/Forward.h>
namespace JS {
class HeapBase {
AK_MAKE_NONCOPYABLE(HeapBase);
AK_MAKE_NONMOVABLE(HeapBase);
public:
void* private_data() { return m_private_data; }
protected:
explicit HeapBase(void* private_data)
: m_private_data(private_data)
{
}
void* m_private_data;
};
class HeapBlockBase {
AK_MAKE_NONMOVABLE(HeapBlockBase);
AK_MAKE_NONCOPYABLE(HeapBlockBase);
public:
static size_t block_size;
static HeapBlockBase* from_cell(CellImpl const* cell)
{
return reinterpret_cast<HeapBlockBase*>(bit_cast<FlatPtr>(cell) & ~(HeapBlockBase::block_size - 1));
}
Heap& heap() { return m_heap; }
protected:
HeapBlockBase(Heap& heap)
: m_heap(heap)
{
}
Heap& m_heap;
};
}