1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:58:11 +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

@ -44,8 +44,8 @@
namespace JS {
Interpreter::Interpreter()
: m_heap(*this)
Interpreter::Interpreter(VM& vm)
: m_vm(vm)
, m_console(*this)
{
#define __JS_ENUMERATE(SymbolName, snake_name) \
@ -60,6 +60,8 @@ Interpreter::~Interpreter()
Value Interpreter::run(GlobalObject& global_object, const Program& program)
{
VM::InterpreterScope scope(*this);
ASSERT(!exception());
if (m_call_stack.is_empty()) {
@ -223,7 +225,6 @@ Symbol* Interpreter::get_global_symbol(const String& description)
void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
{
roots.set(m_global_object);
roots.set(m_exception);
if (m_last_value.is_cell())
@ -252,6 +253,8 @@ Value Interpreter::call_internal(Function& function, Value this_value, Optional<
{
ASSERT(!exception());
VM::InterpreterScope scope(*this);
auto& call_frame = push_call_frame();
call_frame.function_name = function.name();
call_frame.this_value = function.bound_this().value_or(this_value);
@ -348,12 +351,12 @@ void Interpreter::throw_exception(Exception* exception)
GlobalObject& Interpreter::global_object()
{
return static_cast<GlobalObject&>(*m_global_object);
return static_cast<GlobalObject&>(*m_global_object.cell());
}
const GlobalObject& Interpreter::global_object() const
{
return static_cast<const GlobalObject&>(*m_global_object);
return static_cast<const GlobalObject&>(*m_global_object.cell());
}
String Interpreter::join_arguments() const