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().
This commit is contained in:
Andrew Kaster 2023-11-22 09:53:14 -07:00 committed by Andreas Kling
parent e30ecacb71
commit 05ec93e276
Notes: sideshowbarker 2024-07-18 05:37:06 +09:00
2 changed files with 3 additions and 9 deletions

View file

@ -18,13 +18,10 @@
namespace WebWorker {
DedicatedWorkerHost::DedicatedWorkerHost(Web::Page& page, AK::URL url, String type)
: m_worker_vm(JS::VM::create(make<Web::Bindings::WebEngineCustomData>()).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<Web::HTML::WorkerGlobalScope>(realm, m_page);
return Web::Bindings::main_thread_vm().heap().allocate_without_realm<Web::HTML::WorkerGlobalScope>(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()));

View file

@ -8,7 +8,6 @@
#include <AK/RefCounted.h>
#include <AK/URL.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Forward.h>
@ -22,7 +21,6 @@ public:
void run();
private:
NonnullRefPtr<JS::VM> m_worker_vm;
RefPtr<Web::HTML::WorkerDebugConsoleClient> m_console;
Web::Page& m_page;