Truly independent web browser
LibWeb+WebContent: Move the DevTools object table into LibWeb
sideshowbarkerdc5f8b7807db
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex 0d6b5d9e398..0bec99ebd4d 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -628,6 +628,7 @@ set(SOURCESHTML/SanitizerConfiguration.cppHTML/Scripting/Agent.cppHTML/Scripting/ClassicScript.cpp+ HTML/Scripting/DevToolsObjectTable.cppHTML/Scripting/Environments.cppHTML/Scripting/ExceptionReporter.cppHTML/Scripting/FetchClient.cppdiff --git a/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.cpp b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.cppnew file mode 100644index 00000000000..c30319d5e13--- /dev/null+++ b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.cpp@@ -0,0 +1,135 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibJS/Runtime/BigInt.h>+#include <LibJS/Runtime/FunctionObject.h>+#include <LibJS/Runtime/Object.h>+#include <LibJS/Runtime/PropertyDescriptor.h>+#include <LibJS/Runtime/ValueInlines.h>+#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>++namespace Web::HTML {++WebView::DebuggerValue DevToolsObjectTable::serialize_value(JS::Value value)+{+ WebView::DebuggerValue result;+ if (value.is_special_empty_value()) {+ result.type = WebView::DebuggerValueType::Uninitialized;+ return result;+ }+ if (value.is_undefined())+ return result;+ if (value.is_null()) {+ result.type = WebView::DebuggerValueType::Null;+ return result;+ }+ if (value.is_boolean()) {+ result.type = WebView::DebuggerValueType::Boolean;+ result.boolean_value = value.as_bool();+ return result;+ }+ if (value.is_number()) {+ result.type = WebView::DebuggerValueType::Number;+ result.number_value = value.as_double();+ return result;+ }+ if (value.is_string()) {+ result.type = WebView::DebuggerValueType::String;+ result.text = value.as_string().utf16_string();+ return result;+ }+ if (value.is_bigint()) {+ result.type = WebView::DebuggerValueType::BigInt;+ result.text = value.as_bigint().to_utf16_string();+ return result;+ }+ if (value.is_symbol()) {+ // FIXME: Represent symbols with dedicated SymbolActors instead of converting them to strings.+ result.type = WebView::DebuggerValueType::String;+ result.text = value.to_utf16_string_without_side_effects();+ return result;+ }++ VERIFY(value.is_object());+ auto& object = value.as_object();+ Optional<u64> object_id;+ if (auto existing_id = m_object_ids.get(&object); existing_id.has_value())+ object_id = *existing_id;+ if (!object_id.has_value()) {+ object_id = m_next_object_id++;+ m_object_ids.set(&object, *object_id);+ m_objects.set(*object_id, GC::make_root(object));+ }+ result.type = WebView::DebuggerValueType::Object;+ result.object_id = *object_id;+ result.object_class = object.is_function() ? "Function"_string : MUST(String::from_utf8(object.class_name()));+ return result;+}++ErrorOr<WebView::DebuggerObjectProperties> DevToolsObjectTable::properties_for_object(u64 object_id)+{+ auto object_iterator = m_objects.find(object_id);+ if (object_iterator == m_objects.end())+ return Error::from_string_literal("Unable to locate object");+ // serialize_value() may add entries to m_objects, so retain a root+ // independent of the map's storage.+ auto object = object_iterator->value;++ WebView::DebuggerObjectProperties result;+ auto prototype = object->internal_get_prototype_of();+ if (prototype.is_error())+ return Error::from_string_literal("Unable to inspect object prototype");+ if (auto* prototype_object = prototype.release_value()) {+ result.prototype = serialize_value(prototype_object);+ } else {+ WebView::DebuggerValue null_prototype;+ null_prototype.type = WebView::DebuggerValueType::Null;+ result.prototype = move(null_prototype);+ }++ auto own_property_keys = object->internal_own_property_keys();+ if (own_property_keys.is_error())+ return Error::from_string_literal("Unable to inspect object properties");++ for (auto const& key_value : own_property_keys.release_value()) {+ // FIXME: Expose symbol properties through a symbol iterator actor.+ if (!key_value.is_string())+ continue;++ auto name = key_value.as_string().utf16_string();+ auto descriptor = object->internal_get_own_property(JS::PropertyKey { name });+ if (descriptor.is_error())+ return Error::from_string_literal("Unable to inspect object property");+ if (!descriptor.value().has_value())+ continue;++ auto const& property_descriptor = descriptor.value().value();+ WebView::DebuggerProperty property;+ property.name = move(name);+ property.writable = property_descriptor.writable.value_or(false);+ property.enumerable = property_descriptor.enumerable.value_or(false);+ property.configurable = property_descriptor.configurable.value_or(false);+ if (property_descriptor.value.has_value()) {+ property.value = serialize_value(*property_descriptor.value);+ } else {+ if (property_descriptor.get.has_value())+ property.getter = serialize_value(property_descriptor.get->ptr() ? JS::Value { property_descriptor.get->ptr() } : JS::js_undefined());+ if (property_descriptor.set.has_value())+ property.setter = serialize_value(property_descriptor.set->ptr() ? JS::Value { property_descriptor.set->ptr() } : JS::js_undefined());+ }+ result.properties.append(move(property));+ }++ return result;+}++void DevToolsObjectTable::clear()+{+ m_objects.clear();+ m_object_ids.clear();+}++}diff --git a/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.h b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.hnew file mode 100644index 00000000000..185b9cc28ba--- /dev/null+++ b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.h@@ -0,0 +1,30 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/HashMap.h>+#include <LibGC/Root.h>+#include <LibJS/Forward.h>+#include <LibWeb/Export.h>+#include <LibWebCommon/WebView/Debugger.h>++namespace Web::HTML {++// The objects that DevTools refers to by ID, kept alive for as long as DevTools may ask about them.+class WEB_API DevToolsObjectTable {+public:+ WebView::DebuggerValue serialize_value(JS::Value);+ ErrorOr<WebView::DebuggerObjectProperties> properties_for_object(u64 object_id);+ void clear();++private:+ HashMap<u64, GC::Root<JS::Object>> m_objects;+ HashMap<GC::Ptr<JS::Object>, u64> m_object_ids;+ u64 m_next_object_id { 1 };+};++}diff --git a/Services/WebContent/DevToolsDebugger.cpp b/Services/WebContent/DevToolsDebugger.cppindex 034d80479b6..bc9daaadb68 100644--- a/Services/WebContent/DevToolsDebugger.cpp+++ b/Services/WebContent/DevToolsDebugger.cpp@@ -183,58 +183,7 @@ ErrorOr<void> DevToolsDebugger::remove_breakpoint(PageClient& page, WebView::DebWebView::DebuggerValue DevToolsDebugger::serialize_value(JS::Value value){- WebView::DebuggerValue result;- if (value.is_special_empty_value()) {- result.type = WebView::DebuggerValueType::Uninitialized;- return result;- }- if (value.is_undefined())- return result;- if (value.is_null()) {- result.type = WebView::DebuggerValueType::Null;- return result;- }- if (value.is_boolean()) {- result.type = WebView::DebuggerValueType::Boolean;- result.boolean_value = value.as_bool();- return result;- }- if (value.is_number()) {- result.type = WebView::DebuggerValueType::Number;- result.number_value = value.as_double();- return result;- }- if (value.is_string()) {- result.type = WebView::DebuggerValueType::String;- result.text = value.as_string().utf16_string();- return result;- }- if (value.is_bigint()) {- result.type = WebView::DebuggerValueType::BigInt;- result.text = value.as_bigint().to_utf16_string();- return result;- }- if (value.is_symbol()) {- // FIXME: Represent symbols with dedicated SymbolActors instead of converting them to strings.- result.type = WebView::DebuggerValueType::String;- result.text = value.to_utf16_string_without_side_effects();- return result;- }-- VERIFY(value.is_object());- auto& object = value.as_object();- Optional<u64> object_id;- if (auto existing_id = m_paused_object_ids.get(&object); existing_id.has_value())- object_id = *existing_id;- if (!object_id.has_value()) {- object_id = m_next_object_id++;- m_paused_object_ids.set(&object, *object_id);- m_paused_objects.set(*object_id, GC::make_root(object));- }- result.type = WebView::DebuggerValueType::Object;- result.object_id = *object_id;- result.object_class = object.is_function() ? "Function"_string : MUST(String::from_utf8(object.class_name()));- return result;+ return m_paused_object_table.serialize_value(value);}Vector<WebView::DebuggerEnvironment> DevToolsDebugger::environments_for_frame(PageClient& page, u64 frame_id)@@ -350,60 +299,7 @@ ErrorOr<WebView::DebuggerObjectProperties> DevToolsDebugger::properties_for_obje{if (m_paused_page_id != page.id())return Error::from_string_literal("Debugger is not paused for this page");-- auto object_iterator = m_paused_objects.find(object_id);- if (object_iterator == m_paused_objects.end())- return Error::from_string_literal("Unable to locate paused object");- // serialize_value() may add entries to m_paused_objects, so retain a root- // independent of the map's storage.- auto object = object_iterator->value;-- WebView::DebuggerObjectProperties result;- auto prototype = object->internal_get_prototype_of();- if (prototype.is_error())- return Error::from_string_literal("Unable to inspect object prototype");- if (auto* prototype_object = prototype.release_value()) {- result.prototype = serialize_value(prototype_object);- } else {- WebView::DebuggerValue null_prototype;- null_prototype.type = WebView::DebuggerValueType::Null;- result.prototype = move(null_prototype);- }-- auto own_property_keys = object->internal_own_property_keys();- if (own_property_keys.is_error())- return Error::from_string_literal("Unable to inspect object properties");-- for (auto const& key_value : own_property_keys.release_value()) {- // FIXME: Expose symbol properties through a symbol iterator actor.- if (!key_value.is_string())- continue;-- auto name = key_value.as_string().utf16_string();- auto descriptor = object->internal_get_own_property(JS::PropertyKey { name });- if (descriptor.is_error())- return Error::from_string_literal("Unable to inspect object property");- if (!descriptor.value().has_value())- continue;-- auto const& property_descriptor = descriptor.value().value();- WebView::DebuggerProperty property;- property.name = move(name);- property.writable = property_descriptor.writable.value_or(false);- property.enumerable = property_descriptor.enumerable.value_or(false);- property.configurable = property_descriptor.configurable.value_or(false);- if (property_descriptor.value.has_value()) {- property.value = serialize_value(*property_descriptor.value);- } else {- if (property_descriptor.get.has_value())- property.getter = serialize_value(property_descriptor.get->ptr() ? JS::Value { property_descriptor.get->ptr() } : JS::js_undefined());- if (property_descriptor.set.has_value())- property.setter = serialize_value(property_descriptor.set->ptr() ? JS::Value { property_descriptor.set->ptr() } : JS::js_undefined());- }- result.properties.append(move(property));- }-- return result;+ return m_paused_object_table.properties_for_object(object_id);}void DevToolsDebugger::resume(PageClient& page, WebView::DebuggerResumeMode mode)@@ -657,8 +553,7 @@ void DevToolsDebugger::handle_pause(JS::Debugger::PauseInfo const& pause)}m_paused_frames.clear();- m_paused_objects.clear();- m_paused_object_ids.clear();+ m_paused_object_table.clear();WebView::DebuggerPause debugger_pause {.reason = [&] {@@ -719,8 +614,7 @@ void DevToolsDebugger::handle_pause(JS::Debugger::PauseInfo const& pause)if (debugger_pause.frames.is_empty()) {m_paused_frames.clear();- m_paused_objects.clear();- m_paused_object_ids.clear();+ m_paused_object_table.clear();Web::Bindings::main_thread_vm().debugger()->continue_execution();return;}@@ -742,8 +636,7 @@ void DevToolsDebugger::handle_pause(JS::Debugger::PauseInfo const& pause)m_is_handling_pause = false;m_paused_page_id = {};m_paused_frames.clear();- m_paused_objects.clear();- m_paused_object_ids.clear();+ m_paused_object_table.clear();auto resume_mode = [&] {switch (m_resume_mode) {case WebView::DebuggerResumeMode::Continue:diff --git a/Services/WebContent/DevToolsDebugger.h b/Services/WebContent/DevToolsDebugger.hindex 6a1671410bf..e1d89cc0737 100644--- a/Services/WebContent/DevToolsDebugger.h+++ b/Services/WebContent/DevToolsDebugger.h@@ -11,6 +11,7 @@#include <AK/Weakable.h>#include <LibGC/Root.h>#include <LibJS/Debugger.h>+#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>#include <LibWebCommon/Page/PageId.h>#include <LibWebCommon/WebView/Debugger.h>#include <WebContent/Forward.h>@@ -67,11 +68,9 @@ private:HashMap<Web::PageId, Vector<BreakpointRegistration>> m_breakpoints;HashMap<Web::PageId, Vector<BlackboxedSource>> m_blackboxed_sources;HashMap<u64, JS::ExecutionContext*> m_paused_frames;- HashMap<u64, GC::Root<JS::Object>> m_paused_objects;- HashMap<GC::Ptr<JS::Object>, u64> m_paused_object_ids;+ Web::HTML::DevToolsObjectTable m_paused_object_table;u64 m_next_environment_id { 1 };u64 m_next_frame_id { 1 };- u64 m_next_object_id { 1 };Optional<Web::PageId> m_paused_page_id;WebView::DebuggerResumeMode m_resume_mode { WebView::DebuggerResumeMode::Continue };bool m_resume_requested { false };