1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:07:46 +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

@ -552,6 +552,7 @@ int main(int argc, char** argv)
bool syntax_highlight = !disable_syntax_highlight;
auto vm = JS::VM::create();
OwnPtr<JS::Interpreter> interpreter;
interrupt_interpreter = [&] {
@ -561,7 +562,7 @@ int main(int argc, char** argv)
if (script_path == nullptr) {
s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>();
interpreter = JS::Interpreter::create<ReplObject>(*vm);
ReplConsoleClient console_client(interpreter->console());
interpreter->console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
@ -842,7 +843,7 @@ int main(int argc, char** argv)
s_editor->on_tab_complete = move(complete);
repl(*interpreter);
} else {
interpreter = JS::Interpreter::create<JS::GlobalObject>();
interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm);
ReplConsoleClient console_client(interpreter->console());
interpreter->console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);