From 7dd7e5b4387c9c1320f0ee99bdef6047b60cde78 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 24 Apr 2025 15:58:00 +1200 Subject: [PATCH] LibJS+LibWeb: Defer initialization of the Agent after VM constructor This helps unwind a niggly depedency where the VM owns and constructs the Heap and the Agent. But the agent wants to have customized construction that depends on the heap. Solve this by defering the initialization of the Agent to after we have constructed the VM and the heap. --- Libraries/LibJS/Runtime/VM.cpp | 7 +++---- Libraries/LibJS/Runtime/VM.h | 5 +++-- Libraries/LibWeb/Bindings/MainThreadVM.cpp | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index b1739c03c3e..0340e60dd4a 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -37,12 +37,12 @@ namespace JS { -NonnullRefPtr VM::create(OwnPtr agent) +NonnullRefPtr VM::create() { ErrorMessages error_messages {}; error_messages[to_underlying(ErrorMessage::OutOfMemory)] = ErrorType::OutOfMemory.message(); - auto vm = adopt_ref(*new VM(move(agent), move(error_messages))); + auto vm = adopt_ref(*new VM(move(error_messages))); WellKnownSymbols well_known_symbols { #define __JS_ENUMERATE(SymbolName, snake_name) \ @@ -63,12 +63,11 @@ static constexpr auto make_single_ascii_character_strings(IndexSequence()); -VM::VM(OwnPtr agent, ErrorMessages error_messages) +VM::VM(ErrorMessages error_messages) : m_heap(this, [this](HashMap& roots) { gather_roots(roots); }) , m_error_messages(move(error_messages)) - , m_agent(move(agent)) { m_bytecode_interpreter = make(*this); diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h index adc87b37937..e2666dd6492 100644 --- a/Libraries/LibJS/Runtime/VM.h +++ b/Libraries/LibJS/Runtime/VM.h @@ -47,7 +47,7 @@ enum class EvalMode { class VM : public RefCounted { public: - static NonnullRefPtr create(OwnPtr = {}); + static NonnullRefPtr create(); ~VM(); GC::Heap& heap() { return m_heap; } @@ -240,6 +240,7 @@ public: Function on_promise_rejection_handled; Function on_unimplemented_property_access; + void set_agent(OwnPtr agent) { m_agent = move(agent); } Agent* agent() { return m_agent; } Agent const* agent() const { return m_agent; } @@ -290,7 +291,7 @@ private: #undef __JS_ENUMERATE }; - VM(OwnPtr, ErrorMessages); + explicit VM(ErrorMessages); void load_imported_module(ImportedModuleReferrer, ModuleRequest const&, GC::Ptr, ImportedModulePayload); ThrowCompletionOr link_and_eval_module(CyclicModule&); diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cpp index 650c9bfe149..83a9843487b 100644 --- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -76,7 +76,8 @@ void initialize_main_thread_vm(HTML::EventLoop::Type type) { VERIFY(!s_main_thread_vm); - s_main_thread_vm = JS::VM::create(make()); + s_main_thread_vm = JS::VM::create(); + s_main_thread_vm->set_agent(make()); auto& agent = as(*s_main_thread_vm->agent()); agent.event_loop = s_main_thread_vm->heap().allocate(type);