LibWeb: Add an "internal" JS realm & window object

These will be used to host JS objects that don't belong in one of the
web-facing global objects.
This commit is contained in:
Andreas Kling 2022-08-07 12:03:41 +02:00
parent 4ae2390554
commit 0fe923e355
Notes: sideshowbarker 2024-07-17 08:43:11 +09:00
2 changed files with 24 additions and 3 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -307,12 +307,27 @@ JS::VM& main_thread_vm()
// NOTE: We push a dummy execution context onto the JS execution context stack,
// just to make sure that it's never empty.
auto& custom_data = *verify_cast<WebEngineCustomData>(vm->custom_data());
custom_data.root_execution_context = make<JS::ExecutionContext>(vm->heap());
custom_data.root_execution_context = MUST(JS::Realm::initialize_host_defined_realm(
*vm, [&](JS::Realm& realm) -> JS::GlobalObject* {
auto internal_window = HTML::Window::create();
custom_data.internal_window_object = JS::make_handle(vm->heap().allocate<Bindings::WindowObject>(realm, realm, internal_window));
return custom_data.internal_window_object.cell(); },
[](JS::Realm&) -> JS::GlobalObject* {
return nullptr;
}));
vm->push_execution_context(*custom_data.root_execution_context);
}
return *vm;
}
Bindings::WindowObject& main_thread_internal_window_object()
{
auto& vm = main_thread_vm();
auto& custom_data = verify_cast<WebEngineCustomData>(*vm.custom_data());
return *custom_data.internal_window_object;
}
// https://dom.spec.whatwg.org/#queue-a-mutation-observer-compound-microtask
void queue_mutation_observer_microtask(DOM::Document& document)
{

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -11,6 +11,7 @@
#include <LibJS/Forward.h>
#include <LibJS/Runtime/JobCallback.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/MutationObserver.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
@ -31,6 +32,10 @@ struct WebEngineCustomData final : public JS::VM::CustomData {
NonnullRefPtrVector<DOM::MutationObserver> mutation_observers;
OwnPtr<JS::ExecutionContext> root_execution_context;
// This object is used as the global object for GC-allocated objects that don't
// belong to a web-facing global object.
JS::Handle<Bindings::WindowObject> internal_window_object;
};
struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData {
@ -48,6 +53,7 @@ struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData
HTML::ClassicScript* active_script();
JS::VM& main_thread_vm();
Bindings::WindowObject& main_thread_internal_window_object();
void queue_mutation_observer_microtask(DOM::Document&);
NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, Function<JS::GlobalObject*(JS::Realm&)> create_global_object, Function<JS::GlobalObject*(JS::Realm&)> create_global_this_value);