Truly independent web browser
LibWeb: Give environment settings objects an address
Luke Wilde114596064dcd
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex 3a7be33a2f1..65546f044e0 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -1628,7 +1628,9 @@ void EventLoop::perform_a_microtask_checkpoint()}// 4. For each environment settings object settingsObject whose responsible event loop is this event loop, notify about rejected promises given settingsObject's global object.- auto environments = GC::RootVector { m_related_environment_settings_objects };+ GC::RootVector<GC::Ref<EnvironmentSettingsObject>> environments;+ for (auto const& entry : m_related_environment_settings_objects)+ environments.append(*entry.value);for (auto& environment_settings_object : environments) {auto& global_object = environment_settings_object->global_object();if (auto* worklet_global_scope = Bindings::impl_from<WorkletGlobalScope>(&global_object)) {@@ -1769,15 +1771,21 @@ EnvironmentSettingsObject& EventLoop::top_of_backup_incumbent_realm_stack()void EventLoop::register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object){- m_related_environment_settings_objects.append(&environment_settings_object);+ auto result = m_related_environment_settings_objects.set(environment_settings_object.address(), &environment_settings_object);+ VERIFY(result == HashSetResult::InsertedNewEntry);}void EventLoop::unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object){- bool did_remove = m_related_environment_settings_objects.remove_first_matching([&](auto& entry) { return entry == &environment_settings_object; });+ bool did_remove = m_related_environment_settings_objects.remove(environment_settings_object.address());VERIFY(did_remove);}+GC::Ptr<EnvironmentSettingsObject> EventLoop::environment_settings_object_at(EnvironmentSettingsObjectAddress address) const+{+ return m_related_environment_settings_objects.get(address).value_or(nullptr);+}+// https://html.spec.whatwg.org/multipage/webappapis.html#same-loop-windowsVector<GC::Root<HTML::Window>> EventLoop::same_loop_windows() const{diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.hindex 9c3772ac621..e5358f400e1 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h@@ -8,6 +8,7 @@#include <AK/Forward.h>#include <AK/Function.h>+#include <AK/HashMap.h>#include <AK/Noncopyable.h>#include <AK/OwnPtr.h>#include <AK/Queue.h>@@ -17,6 +18,7 @@#include <LibJS/Forward.h>#include <LibWeb/Export.h>#include <LibWeb/HTML/EventLoop/TaskQueue.h>+#include <LibWeb/HTML/Scripting/EnvironmentSettingsObjectAddress.h>#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>namespace Web::Layout::RustFFI {@@ -150,6 +152,7 @@ public:void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&);+ GC::Ptr<EnvironmentSettingsObject> environment_settings_object_at(EnvironmentSettingsObjectAddress) const;double compute_deadline() const;@@ -253,7 +256,7 @@ private:// Used to implement step 4 of "perform a microtask checkpoint".// NOTE: These are weak references! ESO registers and unregisters itself from the event loop manually.- Vector<RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;+ OrderedHashMap<EnvironmentSettingsObjectAddress, RawPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects;// https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stackVector<GC::Ref<EnvironmentSettingsObject>> m_backup_incumbent_realm_stack;diff --git a/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsObjectAddress.h b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsObjectAddress.hnew file mode 100644index 00000000000..6ffc3085a86--- /dev/null+++ b/Libraries/LibWeb/HTML/Scripting/EnvironmentSettingsObjectAddress.h@@ -0,0 +1,16 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/DistinctNumeric.h>++namespace Web::HTML {++// Names one environment settings object in this process; no other settings object ever has the same address.+AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, EnvironmentSettingsObjectAddress);++}diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.cpp b/Libraries/LibWeb/HTML/Scripting/Environments.cppindex 0cc613cd1e4..17a011c15b4 100644--- a/Libraries/LibWeb/HTML/Scripting/Environments.cpp+++ b/Libraries/LibWeb/HTML/Scripting/Environments.cpp@@ -7,6 +7,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Atomic.h>#include <LibGC/Heap.h>#include <LibGC/WeakInlines.h>#include <LibURL/InternalURLs.h>@@ -61,8 +62,15 @@ Environment::Environment(EnvironmentId id, URL::URL creation_url, Optional<URL::Environment::~Environment() = default;+static EnvironmentSettingsObjectAddress allocate_an_address()+{+ static Atomic<u64> s_next_address { 1 };+ return EnvironmentSettingsObjectAddress { s_next_address.fetch_add(1) };+}+EnvironmentSettingsObject::EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext> realm_execution_context): m_realm_execution_context(move(realm_execution_context))+ , m_address(allocate_an_address()), m_keepalive_quota_accountant(Fetch::Infrastructure::KeepaliveQuotaAccountant::create()){m_module_map = GC::Heap::the().allocate<ModuleMap>();@@ -400,6 +408,11 @@ JS::Realm& incumbent_realm()return incumbent_settings_object().realm();}+GC::Ptr<EnvironmentSettingsObject> environment_settings_object_at(EnvironmentSettingsObjectAddress address)+{+ return main_thread_event_loop().environment_settings_object_at(address);+}+// https://html.spec.whatwg.org/multipage/webappapis.html#incumbent-settings-objectEnvironmentSettingsObject& incumbent_settings_object(){diff --git a/Libraries/LibWeb/HTML/Scripting/Environments.h b/Libraries/LibWeb/HTML/Scripting/Environments.hindex 74205900d69..4fca160bf7a 100644--- a/Libraries/LibWeb/HTML/Scripting/Environments.h+++ b/Libraries/LibWeb/HTML/Scripting/Environments.h@@ -19,6 +19,7 @@#include <LibWeb/Fetch/Infrastructure/KeepaliveQuotaAccountant.h>#include <LibWeb/Forward.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>+#include <LibWeb/HTML/Scripting/EnvironmentSettingsObjectAddress.h>#include <LibWeb/HTML/Scripting/ModuleMap.h>#include <LibWeb/HighResolutionTime/TimeOrigin.h>#include <LibWeb/ServiceWorker/Registration.h>@@ -84,6 +85,8 @@ struct WEB_API EnvironmentSettingsObject : public Environment {public:virtual void finalize() override;+ EnvironmentSettingsObjectAddress address() const { return m_address; }+// https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-contextJS::ExecutionContext& realm_execution_context();JS::ExecutionContext const& realm_execution_context() const;@@ -182,6 +185,7 @@ protected:private:NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;+ EnvironmentSettingsObjectAddress m_address;GC::Ptr<ModuleMap> m_module_map;GC::Ptr<EventLoop> m_responsible_event_loop;@@ -227,6 +231,8 @@ WEB_API bool module_type_allowed(EnvironmentSettingsObject const&, Utf16View modWEB_API void add_module_to_resolved_module_set(EnvironmentSettingsObject&, Utf16View serialized_base_url, Utf16View normalized_specifier, Optional<URL::URL> const& as_url);+WEB_API GC::Ptr<EnvironmentSettingsObject> environment_settings_object_at(EnvironmentSettingsObjectAddress);+WEB_API EnvironmentSettingsObject& incumbent_settings_object();WEB_API JS::Realm& incumbent_realm();