1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:17:44 +00:00

LibJS+Clients: Add JS::VM object, separate Heap from Interpreter

Taking a big step towards a world of multiple global object, this patch
adds a new JS::VM object that houses the JS::Heap.

This means that the Heap moves out of Interpreter, and the same Heap
can now be used by multiple Interpreters, and can also outlive them.

The VM keeps a stack of Interpreter pointers. We push/pop on this
stack when entering/exiting execution with a given Interpreter.
This allows us to make this change without disturbing too much of
the existing code.

There is still a 1-to-1 relationship between Interpreter and the
global object. This will change in the future.

Ultimately, the goal here is to make Interpreter a transient object
that only needs to exist while you execute some code. Getting there
will take a lot more work though. :^)

Note that in LibWeb, the global JS::VM is called main_thread_vm(),
to distinguish it from future worker VM's.
This commit is contained in:
Andreas Kling 2020-09-20 19:24:44 +02:00
parent c6ae0c41d9
commit 1c43442be4
13 changed files with 218 additions and 27 deletions

View file

@ -47,8 +47,8 @@
namespace JS {
Heap::Heap(Interpreter& interpreter)
: m_interpreter(interpreter)
Heap::Heap(VM& vm)
: m_vm(vm)
{
}
@ -57,6 +57,11 @@ Heap::~Heap()
collect_garbage(CollectionType::CollectEverything);
}
Interpreter& Heap::interpreter()
{
return vm().interpreter();
}
Cell* Heap::allocate_cell(size_t size)
{
if (should_collect_on_every_allocation()) {
@ -100,7 +105,8 @@ void Heap::collect_garbage(CollectionType collection_type, bool print_report)
void Heap::gather_roots(HashTable<Cell*>& roots)
{
m_interpreter.gather_roots({}, roots);
if (auto* interpreter = vm().interpreter_if_exists())
interpreter->gather_roots({}, roots);
gather_conservative_roots(roots);