From 05ec93e2766c2dfa698f094da8b5a67eacbaf7ba Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 22 Nov 2023 09:53:14 -0700 Subject: [PATCH] WebWorker: Reuse main thread VM for DedicatedWorker realms While creating a new VM feels warm and fuzzy from an isolation perspective, having multiple JS heaps in the same process is a footgun waiting to happen. Additionally, there are still many places in LibWeb that reach for the main thread VM to check for the current realm to do things, such as Web::HTML::incumbent_settings_object(). --- Userland/Services/WebWorker/DedicatedWorkerHost.cpp | 10 +++------- Userland/Services/WebWorker/DedicatedWorkerHost.h | 2 -- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Userland/Services/WebWorker/DedicatedWorkerHost.cpp b/Userland/Services/WebWorker/DedicatedWorkerHost.cpp index 3fe9e810c1..59b8710d19 100644 --- a/Userland/Services/WebWorker/DedicatedWorkerHost.cpp +++ b/Userland/Services/WebWorker/DedicatedWorkerHost.cpp @@ -18,13 +18,10 @@ namespace WebWorker { DedicatedWorkerHost::DedicatedWorkerHost(Web::Page& page, AK::URL url, String type) - : m_worker_vm(JS::VM::create(make()).release_value_but_fixme_should_propagate_errors()) - , m_page(page) + : m_page(page) , m_url(move(url)) , m_type(move(type)) { - // FIXME: We need to attach all the HostDefined hooks from MainThreadVM onto this VM in order to load - // module scripts in Workers. } DedicatedWorkerHost::~DedicatedWorkerHost() = default; @@ -37,14 +34,14 @@ void DedicatedWorkerHost::run() // 7. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations: auto realm_execution_context = Web::Bindings::create_a_new_javascript_realm( - *m_worker_vm, + Web::Bindings::main_thread_vm(), [this](JS::Realm& realm) -> JS::Object* { // 7a. For the global object, if is shared is true, create a new SharedWorkerGlobalScope object. // 7b. Otherwise, create a new DedicatedWorkerGlobalScope object. // FIXME: Proper support for both SharedWorkerGlobalScope and DedicatedWorkerGlobalScope if (is_shared) TODO(); - return m_worker_vm->heap().allocate_without_realm(realm, m_page); + return Web::Bindings::main_thread_vm().heap().allocate_without_realm(realm, m_page); }, nullptr); @@ -55,7 +52,6 @@ void DedicatedWorkerHost::run() // 9. Set up a worker environment settings object with realm execution context, // outside settings, and unsafeWorkerCreationTime, and let inside settings be the result. auto inner_settings = Web::HTML::WorkerEnvironmentSettingsObject::setup(move(realm_execution_context)); - inner_settings->responsible_event_loop().set_vm(*m_worker_vm); auto& console_object = *inner_settings->realm().intrinsics().console_object(); m_console = adopt_ref(*new Web::HTML::WorkerDebugConsoleClient(console_object.console())); diff --git a/Userland/Services/WebWorker/DedicatedWorkerHost.h b/Userland/Services/WebWorker/DedicatedWorkerHost.h index 31623e15cd..faba0050f2 100644 --- a/Userland/Services/WebWorker/DedicatedWorkerHost.h +++ b/Userland/Services/WebWorker/DedicatedWorkerHost.h @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -22,7 +21,6 @@ public: void run(); private: - NonnullRefPtr m_worker_vm; RefPtr m_console; Web::Page& m_page;