Skip to content

Truly independent web browser

LibWeb: Compress the tree links and core pointers of DOM cells

Andreas Kling7ec8a4e31c26
diff --git a/Libraries/LibGC/CellTypeInfo.h b/Libraries/LibGC/CellTypeInfo.h
index 1ef7cbee662..4e422b47564 100644
--- a/Libraries/LibGC/CellTypeInfo.h
+++ b/Libraries/LibGC/CellTypeInfo.h
@@ -74,6 +74,9 @@ struct CellTypeThunks {
static constexpr CellTypeInfo info()
{
static_assert(alignof(T) <= max_cell_alignment);
+ // A free cell holds a link to the next one, so a cell that can be allocated must have room for that after its
+ // header.
+ static_assert(__is_abstract(T) || sizeof(T) >= sizeof(Cell) + sizeof(FlatPtr), "A cell must be at least as large as a free cell");
return {
.cell_size = static_cast<u32>(sizeof(T)),
.alignment = static_cast<u32>(alignof(T)),
diff --git a/Libraries/LibGC/Ptr.h b/Libraries/LibGC/Ptr.h
index 726ae48a0cd..35953fbd992 100644
--- a/Libraries/LibGC/Ptr.h
+++ b/Libraries/LibGC/Ptr.h
@@ -139,6 +139,15 @@ public:
return *this;
}
+ // NB: A member converts to a reference, a Ref and a Ptr, so assigning one would be ambiguous without this.
+ template<typename U>
+ Ref& operator=(NonnullMember<U> const& other)
+ requires(IsConvertible<U*, T*>)
+ {
+ m_ptr = static_cast<T*>(other.ptr());
+ return *this;
+ }
+
template<typename U>
Ref& operator=(Ref<U> const& foreign_cell)
requires(IsForeignCellConvertibleToCell<U, T>)
@@ -287,6 +296,23 @@ public:
return *this;
}
+ // NB: A member converts to a reference, a Ref and a Ptr, so assigning one would be ambiguous without these.
+ template<typename U>
+ Ptr& operator=(NonnullMember<U> const& other)
+ requires(IsConvertible<U*, T*>)
+ {
+ m_ptr = static_cast<T*>(other.ptr());
+ return *this;
+ }
+
+ template<typename U>
+ Ptr& operator=(Member<U> const& other)
+ requires(IsConvertible<U*, T*>)
+ {
+ m_ptr = static_cast<T*>(other.ptr());
+ return *this;
+ }
+
template<typename U>
Ptr& operator=(Ptr<U> const& foreign_cell)
requires(IsForeignCellConvertibleToCell<U, T>)
diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h
index c29976cf428..fa5a1c69695 100644
--- a/Libraries/LibJS/Runtime/Value.h
+++ b/Libraries/LibJS/Runtime/Value.h
@@ -19,6 +19,7 @@
#include <AK/Types.h>
#include <AK/Utf16String.h>
#include <AK/Utf16View.h>
+#include <LibGC/Member.h>
#include <LibGC/NanBoxedValue.h>
#include <LibGC/Ptr.h>
#include <LibGC/Root.h>
@@ -288,6 +289,24 @@ public:
template<typename T>
requires(HasForbiddenDirectJSValueConversion<T>) Value(GC::Ref<T>) = delete;
+ template<typename T>
+ requires(!HasForbiddenDirectJSValueConversion<T>) Value(GC::Member<T> const& member)
+ : Value(member.ptr())
+ {
+ }
+
+ template<typename T>
+ requires(HasForbiddenDirectJSValueConversion<T>) Value(GC::Member<T> const&) = delete;
+
+ template<typename T>
+ requires(!HasForbiddenDirectJSValueConversion<T>) Value(GC::NonnullMember<T> const& member)
+ : Value(member.ptr())
+ {
+ }
+
+ template<typename T>
+ requires(HasForbiddenDirectJSValueConversion<T>) Value(GC::NonnullMember<T> const&) = delete;
+
template<typename T>
requires(!HasForbiddenDirectJSValueConversion<T>) Value(GC::Root<T> const& ptr)
: Value(ptr.ptr())
diff --git a/Libraries/LibWeb/Bindings/RustEventTarget.cpp b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
index 0a66d54e3b9..a77d379661d 100644
--- a/Libraries/LibWeb/Bindings/RustEventTarget.cpp
+++ b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
@@ -131,7 +131,7 @@ JSValue web_bindings_event_handler_attribute(RustEventTarget* event_target, Read
auto* callback = event_target->event_handler_attribute(Utf16FlyString::from_utf8_without_validation(name));
if (!callback)
return JS::HostABI::value_to_abi(JS::js_null());
- return JS::HostABI::value_to_abi(callback->callback);
+ return JS::HostABI::value_to_abi(callback->callback.ptr());
}
// https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributes
diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
index 97f439bd25a..137405ce7d5 100644
--- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
@@ -13,6 +13,10 @@
namespace Web::CSS {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(CSSStyleDeclaration) == 40);
+#endif
+
GC_DEFINE_ALLOCATOR(CSSStyleDeclaration);
CSSStyleDeclaration::CSSStyleDeclaration(Computed computed, Readonly readonly)
@@ -81,8 +85,18 @@ void CSSStyleDeclaration::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_parent_rule);
- if (m_owner_node.has_value())
- m_owner_node->visit(visitor);
+ visitor.visit(m_owner_element);
+}
+
+void CSSStyleDeclaration::set_owner_node(Optional<DOM::AbstractElement> owner_node)
+{
+ if (!owner_node.has_value()) {
+ m_owner_element = nullptr;
+ m_owner_pseudo_element = {};
+ return;
+ }
+ m_owner_element = owner_node->element();
+ m_owner_pseudo_element = owner_node->pseudo_element();
}
}
diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
index 1d0ba7be02a..f1b0b9d0175 100644
--- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
+++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
@@ -51,11 +51,16 @@ public:
// https://drafts.csswg.org/cssom/#cssstyledeclaration-parent-css-rule
GC::Ptr<CSSRule> parent_rule() const { return m_parent_rule; }
void set_parent_rule(GC::Ptr<CSSRule> parent) { m_parent_rule = parent; }
- static constexpr size_t parent_rule_offset() { return offsetof(CSSStyleDeclaration, m_parent_rule); }
+ static constexpr GC::MemberOffset parent_rule_offset() { return GC_FIELD_OFFSET(CSSStyleDeclaration, m_parent_rule); }
// https://drafts.csswg.org/cssom/#cssstyledeclaration-owner-node
- Optional<DOM::AbstractElement> owner_node() const { return m_owner_node; }
- void set_owner_node(Optional<DOM::AbstractElement> owner_node) { m_owner_node = move(owner_node); }
+ Optional<DOM::AbstractElement> owner_node() const
+ {
+ if (!m_owner_element)
+ return {};
+ return DOM::AbstractElement { m_owner_element.as_nonnull(), m_owner_pseudo_element };
+ }
+ void set_owner_node(Optional<DOM::AbstractElement>);
// https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag
[[nodiscard]] bool is_updating() const { return m_updating; }
@@ -82,11 +87,7 @@ protected:
void update_style_attribute();
private:
- // https://drafts.csswg.org/cssom/#cssstyledeclaration-parent-css-rule
- GC::Ptr<CSSRule> m_parent_rule { nullptr };
-
- // https://drafts.csswg.org/cssom/#cssstyledeclaration-owner-node
- Optional<DOM::AbstractElement> m_owner_node {};
+ // NB: The flags come first, so that they fill the padding at the end of the wrappable's fields.
// https://drafts.csswg.org/cssom/#cssstyledeclaration-computed-flag
bool m_computed { false };
@@ -96,6 +97,15 @@ private:
// https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag
bool m_updating { false };
+
+ // https://drafts.csswg.org/cssom/#cssstyledeclaration-parent-css-rule
+ GC::Member<CSSRule> m_parent_rule;
+
+ // https://drafts.csswg.org/cssom/#cssstyledeclaration-owner-node
+ // NB: The owner node is stored as the parts of an AbstractElement, with a null element for none, which keeps the
+ // element compressed.
+ GC::Member<DOM::Element> m_owner_element;
+ Optional<PseudoElement> m_owner_pseudo_element;
};
}
diff --git a/Libraries/LibWeb/DOM/AbstractRange.h b/Libraries/LibWeb/DOM/AbstractRange.h
index 1354a8965d8..73ede18f5ce 100644
--- a/Libraries/LibWeb/DOM/AbstractRange.h
+++ b/Libraries/LibWeb/DOM/AbstractRange.h
@@ -26,8 +26,8 @@ class WEB_API AbstractRange : public Bindings::GCAllocatedWrappable {
WEB_WRAPPABLE(AbstractRange, Bindings::GCAllocatedWrappable);
public:
- static constexpr size_t start_container_offset() { return offsetof(AbstractRange, m_start_container); }
- static constexpr size_t end_container_offset() { return offsetof(AbstractRange, m_end_container); }
+ static constexpr GC::MemberOffset start_container_offset() { return GC_FIELD_OFFSET(AbstractRange, m_start_container); }
+ static constexpr GC::MemberOffset end_container_offset() { return GC_FIELD_OFFSET(AbstractRange, m_end_container); }
virtual ~AbstractRange() override;
BoundaryPoint start() const { return { m_start_container, m_start_offset }; }
@@ -58,10 +58,10 @@ protected:
virtual void visit_edges(GC::Cell::Visitor&) override;
- GC::Ref<Node> m_start_container;
+ GC::NonnullMember<Node> m_start_container;
WebIDL::UnsignedLong m_start_offset;
- GC::Ref<Node> m_end_container;
+ GC::NonnullMember<Node> m_end_container;
WebIDL::UnsignedLong m_end_offset;
};
diff --git a/Libraries/LibWeb/DOM/Attr.h b/Libraries/LibWeb/DOM/Attr.h
index 2f9885e8611..8e00c7d7593 100644
--- a/Libraries/LibWeb/DOM/Attr.h
+++ b/Libraries/LibWeb/DOM/Attr.h
@@ -43,7 +43,7 @@ public:
Element* owner_element();
Element const* owner_element() const;
void set_owner_element(Element* owner_element);
- static constexpr size_t owner_element_offset() { return offsetof(Attr, m_owner_element); }
+ static constexpr GC::MemberOffset owner_element_offset() { return GC_FIELD_OFFSET(Attr, m_owner_element); }
// Always returns true: https://dom.spec.whatwg.org/#dom-attr-specified
constexpr bool specified() const { return true; }
@@ -58,7 +58,7 @@ private:
QualifiedName m_qualified_name;
Utf16String m_value;
- GC::Ptr<Element> m_owner_element;
+ GC::Member<Element> m_owner_element;
};
template<>
diff --git a/Libraries/LibWeb/DOM/Comment.cpp b/Libraries/LibWeb/DOM/Comment.cpp
index d66f6fb4afb..991eeeaa508 100644
--- a/Libraries/LibWeb/DOM/Comment.cpp
+++ b/Libraries/LibWeb/DOM/Comment.cpp
@@ -11,6 +11,10 @@
namespace Web::DOM {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(Comment) == 112);
+#endif
+
GC_DEFINE_ALLOCATOR(Comment);
Comment::Comment(Document& document, Utf16String data)
diff --git a/Libraries/LibWeb/DOM/DOMEventListener.cpp b/Libraries/LibWeb/DOM/DOMEventListener.cpp
index ee1d701f349..f7987fb598b 100644
--- a/Libraries/LibWeb/DOM/DOMEventListener.cpp
+++ b/Libraries/LibWeb/DOM/DOMEventListener.cpp
@@ -10,6 +10,10 @@
namespace Web::DOM {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(DOMEventListener) == 32);
+#endif
+
GC_DEFINE_ALLOCATOR(DOMEventListener);
DOMEventListener::DOMEventListener() = default;
diff --git a/Libraries/LibWeb/DOM/DOMEventListener.h b/Libraries/LibWeb/DOM/DOMEventListener.h
index de1a45408bc..6d4a1a7f247 100644
--- a/Libraries/LibWeb/DOM/DOMEventListener.h
+++ b/Libraries/LibWeb/DOM/DOMEventListener.h
@@ -24,17 +24,18 @@ public:
DOMEventListener();
~DOMEventListener();
- // type (a string)
- Utf16FlyString type;
+ // NB: The capture flag and the callback come first, so that they fill the padding at the end of the cell header.
+ // capture (a boolean, initially false)
+ bool capture { false };
// callback (null or an EventListener object)
- GC::Ptr<IDLEventListener> callback;
+ GC::Member<IDLEventListener> callback;
- // signal (null or an AbortSignal object)
- GC::Ptr<DOM::AbortSignal> signal;
+ // type (a string)
+ Utf16FlyString type;
- // capture (a boolean, initially false)
- bool capture { false };
+ // signal (null or an AbortSignal object)
+ GC::Member<DOM::AbortSignal> signal;
// passive (null or a boolean, initially null)
Optional<bool> passive;
diff --git a/Libraries/LibWeb/DOM/DOMTokenList.h b/Libraries/LibWeb/DOM/DOMTokenList.h
index f1c61815827..adad054e660 100644
--- a/Libraries/LibWeb/DOM/DOMTokenList.h
+++ b/Libraries/LibWeb/DOM/DOMTokenList.h
@@ -58,7 +58,7 @@ private:
Vector<Utf16String> parse_ordered_set(Utf16View) const;
Utf16String serialize_ordered_set() const;
- GC::Ref<Element> m_associated_element;
+ GC::NonnullMember<Element> m_associated_element;
Utf16FlyString m_associated_attribute;
Vector<Utf16String> m_token_set;
bool m_is_running_update_steps { false };
diff --git a/Libraries/LibWeb/DOM/DocumentFragment.h b/Libraries/LibWeb/DOM/DocumentFragment.h
index 3013cac9fdd..96ed2e5e4b4 100644
--- a/Libraries/LibWeb/DOM/DocumentFragment.h
+++ b/Libraries/LibWeb/DOM/DocumentFragment.h
@@ -39,7 +39,7 @@ protected:
private:
// https://dom.spec.whatwg.org/#concept-documentfragment-host
- GC::Ptr<Element> m_host;
+ GC::Member<Element> m_host;
};
template<>
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 756fc911ad2..6e9c4938641 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -172,6 +172,10 @@
namespace Web::DOM {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(Element) == 400);
+#endif
+
Element::RareData::~RareData() = default;
void Element::RareData::visit_edges(Cell::Visitor& visitor)
@@ -2724,7 +2728,7 @@ WebIDL::ExceptionOr<GC::Ref<ShadowRoot>> Element::attach_shadow(ShadowRootOption
GC::Ptr<ShadowRoot> Element::open_shadow_root() const
{
// 1. Let shadow be this’s shadow root.
- auto shadow = m_shadow_root;
+ GC::Ptr<ShadowRoot> shadow = m_shadow_root;
// 2. If shadow is null or its mode is "closed", then return null.
if (shadow == nullptr || shadow->mode() == ShadowRootMode::Closed)
@@ -4334,7 +4338,7 @@ WebIDL::ExceptionOr<void> Element::insert_adjacent_html(String const& position,
void Element::request_fullscreen(GC::Ptr<WebIDL::Promise> promise, FullscreenRequester fullscreen_requester, Fullscreen::RequestType request_type)
{
// 1. Let pendingDoc be this’s node document.
- auto pending_doc = m_document;
+ GC::Ptr<Document> pending_doc = m_document;
// 3. If pendingDoc is not fully active, then reject promise with a TypeError exception and return promise.
if (!pending_doc->is_fully_active()) {
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index b4c7a74cfae..42d7b01b147 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -1089,11 +1089,9 @@ private:
OwnPtr<AttributeList> m_attributes;
u64 m_subtree_attribute_name_filter { 0 };
- GC::Ptr<CSS::CSSStyleProperties> m_inline_style;
- // NB: The declarations of a style attribute, until something asks for their CSSOM object, which takes them over.
- // At most one of the two is set.
+ // NB: The declarations of a style attribute, until something asks for their CSSOM object (m_inline_style), which
+ // takes them over. At most one of the two is set.
CSS::Parser::ValueParserFFI::DeclarationBlock* m_inline_declarations { nullptr };
- GC::Ptr<ShadowRoot> m_shadow_root;
// A consumer handle mirroring StyleEngine's authoritative style-record column. C++ consumers
// borrow the record-owned computed-values view rather than retaining one complete style per
@@ -1137,16 +1135,20 @@ private:
// Policy blocks it is decided when the element is inserted.
bool m_style_attribute_awaits_content_security_policy_check : 1 { false };
+ // https://dom.spec.whatwg.org/#concept-element-custom-element-state
+ CustomElementState m_custom_element_state { CustomElementState::Undefined };
+
// The child indices the element remembers, or zero for those not counted yet, and the generation of its parent's
// child list they were counted in; zero for none.
mutable u32 m_parent_child_index_generation { 0 };
mutable u32 m_child_index { 0 };
mutable u32 m_child_index_of_type { 0 };
- mutable Optional<Utf16String> m_lang_value;
+ // NB: These are declared next to the u32s above, so that they pack without padding.
+ GC::Member<CSS::CSSStyleProperties> m_inline_style;
+ GC::Member<ShadowRoot> m_shadow_root;
- // https://dom.spec.whatwg.org/#concept-element-custom-element-state
- CustomElementState m_custom_element_state { CustomElementState::Undefined };
+ mutable Optional<Utf16String> m_lang_value;
};
template<>
diff --git a/Libraries/LibWeb/DOM/MutationObserver.cpp b/Libraries/LibWeb/DOM/MutationObserver.cpp
index 7ec85c0a8ba..d3c60a194fa 100644
--- a/Libraries/LibWeb/DOM/MutationObserver.cpp
+++ b/Libraries/LibWeb/DOM/MutationObserver.cpp
@@ -72,7 +72,7 @@ static MutationObserverOptions to_dom_options(Bindings::MutationObserverInit con
static void invoke_mutation_observer_callback(MutationObserver& mutation_observer, Vector<GC::Root<MutationRecord>>& records)
{
auto& callback = mutation_observer.callback();
- auto& settings = callback.callback_context;
+ GC::Ref<HTML::EnvironmentSettingsObject> settings = callback.callback_context;
auto wrapped_records = MUST(JS::Array::create(settings->realm(), 0));
auto& wrapper_world = Bindings::host_defined_wrapper_world(settings->realm());
diff --git a/Libraries/LibWeb/DOM/MutationObserver.h b/Libraries/LibWeb/DOM/MutationObserver.h
index 1975324bc7e..e64c1258cc7 100644
--- a/Libraries/LibWeb/DOM/MutationObserver.h
+++ b/Libraries/LibWeb/DOM/MutationObserver.h
@@ -73,7 +73,7 @@ private:
virtual void visit_edges(GC::Cell::Visitor&) override;
// https://dom.spec.whatwg.org/#concept-mo-callback
- GC::Ptr<WebIDL::CallbackType> m_callback;
+ GC::Member<WebIDL::CallbackType> m_callback;
// https://dom.spec.whatwg.org/#mutationobserver-creation-index
u64 m_creation_index { 0 };
@@ -114,7 +114,7 @@ protected:
virtual void visit_edges(Cell::Visitor&) override;
private:
- GC::Ref<MutationObserver> m_observer;
+ GC::NonnullMember<MutationObserver> m_observer;
MutationObserverOptions m_options;
};
@@ -136,7 +136,7 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
- GC::Ref<RegisteredObserver> m_source;
+ GC::NonnullMember<RegisteredObserver> m_source;
};
template<>
diff --git a/Libraries/LibWeb/DOM/MutationRecord.cpp b/Libraries/LibWeb/DOM/MutationRecord.cpp
index cac690df476..6e93067b472 100644
--- a/Libraries/LibWeb/DOM/MutationRecord.cpp
+++ b/Libraries/LibWeb/DOM/MutationRecord.cpp
@@ -20,12 +20,12 @@ GC::Ref<MutationRecord> MutationRecord::create(Utf16FlyString const& type, Node
}
MutationRecord::MutationRecord(Utf16FlyString const& type, Node const& target, NodeList& added_nodes, NodeList& removed_nodes, Node* previous_sibling, Node* next_sibling, Optional<Utf16FlyString> const& attribute_name, Optional<Utf16FlyString> const& attribute_namespace, Optional<Utf16String> const& old_value)
- : m_type(type)
- , m_target(GC::make_root(target))
+ : m_target(&target)
+ , m_type(type)
, m_added_nodes(added_nodes)
, m_removed_nodes(removed_nodes)
- , m_previous_sibling(GC::make_root(previous_sibling))
- , m_next_sibling(GC::make_root(next_sibling))
+ , m_previous_sibling(previous_sibling)
+ , m_next_sibling(next_sibling)
, m_attribute_name(attribute_name)
, m_attribute_namespace(attribute_namespace)
, m_old_value(old_value)
diff --git a/Libraries/LibWeb/DOM/MutationRecord.h b/Libraries/LibWeb/DOM/MutationRecord.h
index e9b8efddda3..8f2cdacabb3 100644
--- a/Libraries/LibWeb/DOM/MutationRecord.h
+++ b/Libraries/LibWeb/DOM/MutationRecord.h
@@ -17,9 +17,9 @@ class MutationRecord : public Bindings::GCAllocatedWrappable {
GC_DECLARE_ALLOCATOR(MutationRecord);
public:
- static constexpr size_t target_offset() { return offsetof(MutationRecord, m_target); }
- static constexpr size_t added_nodes_offset() { return offsetof(MutationRecord, m_added_nodes); }
- static constexpr size_t removed_nodes_offset() { return offsetof(MutationRecord, m_removed_nodes); }
+ static constexpr GC::MemberOffset target_offset() { return GC_FIELD_OFFSET(MutationRecord, m_target); }
+ static constexpr GC::MemberOffset added_nodes_offset() { return GC_FIELD_OFFSET(MutationRecord, m_added_nodes); }
+ static constexpr GC::MemberOffset removed_nodes_offset() { return GC_FIELD_OFFSET(MutationRecord, m_removed_nodes); }
[[nodiscard]] static GC::Ref<MutationRecord> create(Utf16FlyString const& type, Node const& target, NodeList& added_nodes, NodeList& removed_nodes, Node* previous_sibling, Node* next_sibling, Optional<Utf16FlyString> const& attribute_name, Optional<Utf16FlyString> const& attribute_namespace, Optional<Utf16String> const& old_value);
virtual ~MutationRecord() override;
@@ -30,8 +30,8 @@ public:
NodeList const* removed_nodes() const { return m_removed_nodes.ptr(); }
Node const* previous_sibling() const { return m_previous_sibling.ptr(); }
Node const* next_sibling() const { return m_next_sibling.ptr(); }
- static constexpr size_t previous_sibling_offset() { return offsetof(MutationRecord, m_previous_sibling); }
- static constexpr size_t next_sibling_offset() { return offsetof(MutationRecord, m_next_sibling); }
+ static constexpr GC::MemberOffset previous_sibling_offset() { return GC_FIELD_OFFSET(MutationRecord, m_previous_sibling); }
+ static constexpr GC::MemberOffset next_sibling_offset() { return GC_FIELD_OFFSET(MutationRecord, m_next_sibling); }
Optional<Utf16String> attribute_name() const
{
return m_attribute_name.map([](auto const& name) { return name.to_utf16_string(); });
@@ -47,12 +47,13 @@ private:
virtual void visit_edges(GC::Cell::Visitor&) override;
+ // NB: The target comes first, so that it fills the padding at the end of the wrappable's fields.
+ GC::Member<Node const> m_target;
Utf16FlyString m_type;
- GC::Ptr<Node const> m_target;
- GC::Ptr<NodeList> m_added_nodes;
- GC::Ptr<NodeList> m_removed_nodes;
- GC::Ptr<Node> m_previous_sibling;
- GC::Ptr<Node> m_next_sibling;
+ GC::Member<NodeList> m_added_nodes;
+ GC::Member<NodeList> m_removed_nodes;
+ GC::Member<Node> m_previous_sibling;
+ GC::Member<Node> m_next_sibling;
Optional<Utf16FlyString> m_attribute_name;
Optional<Utf16FlyString> m_attribute_namespace;
Optional<Utf16String> m_old_value;
diff --git a/Libraries/LibWeb/DOM/NamedNodeMap.h b/Libraries/LibWeb/DOM/NamedNodeMap.h
index 365909bd081..3441dab4317 100644
--- a/Libraries/LibWeb/DOM/NamedNodeMap.h
+++ b/Libraries/LibWeb/DOM/NamedNodeMap.h
@@ -67,7 +67,7 @@ private:
GC::Ptr<Attr> find_attribute_node(QualifiedName const&) const;
GC::Ref<Attr> ensure_attribute_node(QualifiedName const&) const;
- GC::Ref<DOM::Element> m_element;
+ GC::NonnullMember<DOM::Element> m_element;
mutable Vector<GC::Ref<Attr>> m_attribute_nodes;
};
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index 1029dc347cd..1e2132c083e 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -116,6 +116,10 @@
namespace Web::DOM {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(Node) == 104);
+#endif
+
static bool final_direct_list_item_does_not_renumber_existing_content(Element const& list_item)
{
auto list_owner = list_item.parent_element();
diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h
index 04907ab7e05..adb9ba33df3 100644
--- a/Libraries/LibWeb/DOM/Node.h
+++ b/Libraries/LibWeb/DOM/Node.h
@@ -413,11 +413,11 @@ public:
Node* parent_node() { return parent(); }
Node const* parent_node() const { return parent(); }
- static constexpr size_t parent_node_offset() { return TreeNode<Node>::parent_offset<Node>(); }
- static constexpr size_t first_child_offset() { return TreeNode<Node>::first_child_offset<Node>(); }
- static constexpr size_t last_child_offset() { return TreeNode<Node>::last_child_offset<Node>(); }
- static constexpr size_t previous_sibling_offset() { return TreeNode<Node>::previous_sibling_offset<Node>(); }
- static constexpr size_t next_sibling_offset() { return TreeNode<Node>::next_sibling_offset<Node>(); }
+ static constexpr GC::MemberOffset parent_node_offset() { return TreeNode<Node>::parent_offset<Node>(); }
+ static constexpr GC::MemberOffset first_child_offset() { return TreeNode<Node>::first_child_offset<Node>(); }
+ static constexpr GC::MemberOffset last_child_offset() { return TreeNode<Node>::last_child_offset<Node>(); }
+ static constexpr GC::MemberOffset previous_sibling_offset() { return TreeNode<Node>::previous_sibling_offset<Node>(); }
+ static constexpr GC::MemberOffset next_sibling_offset() { return TreeNode<Node>::next_sibling_offset<Node>(); }
GC::Ptr<Element> parent_element();
GC::Ptr<Element const> parent_element() const;
@@ -674,8 +674,8 @@ protected:
// "Nodes have a strong reference to registered observers in their registered observer list." https://dom.spec.whatwg.org/#garbage-collection
OwnPtr<Vector<GC::Ref<RegisteredObserver>>> registered_observer_list;
- GC::Ptr<NodeList> child_nodes;
- GC::Ptr<HTMLCollection> children;
+ GC::Member<NodeList> child_nodes;
+ GC::Member<HTMLCollection> children;
OwnPtr<HTMLCollectionCacheRegistration::List> html_collections_with_valid_caches;
// The collections getElementsByTagName() returned, which it may return again for the same argument while the
@@ -706,8 +706,8 @@ protected:
virtual void finalize() override;
virtual size_t external_memory_size() const override;
- GC::Ptr<Document> m_document;
- GC::Ptr<Node> m_root;
+ GC::Member<Document> m_document;
+ GC::Member<Node> m_root;
NodeType m_type { NodeType::INVALID };
bool m_has_layout_box { false };
bool m_has_committed_box { false };
diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cpp
index 25082d2b1ae..be0cd38abdd 100644
--- a/Libraries/LibWeb/DOM/Range.cpp
+++ b/Libraries/LibWeb/DOM/Range.cpp
@@ -566,7 +566,7 @@ GC::Ref<Range> Range::clone_range() const
GC::Ref<Node> Range::common_ancestor_container() const
{
// 1. Let container be start node.
- auto container = m_start_container;
+ GC::Ref<Node> container = m_start_container;
// 2. While container is not an inclusive ancestor of end node, let container be container’s parent.
while (!container->is_inclusive_ancestor_of(m_end_container)) {
diff --git a/Libraries/LibWeb/DOM/Range.h b/Libraries/LibWeb/DOM/Range.h
index c176c7d64c6..595ff7026ca 100644
--- a/Libraries/LibWeb/DOM/Range.h
+++ b/Libraries/LibWeb/DOM/Range.h
@@ -180,8 +180,8 @@ private:
void update_owner_document();
- GC::Ptr<Selection::Selection> m_associated_selection;
- GC::Ptr<Document> m_owner_document;
+ GC::Member<Selection::Selection> m_associated_selection;
+ GC::Member<Document> m_owner_document;
IntrusiveListNode<Range> m_live_range_list_node;
diff --git a/Libraries/LibWeb/DOM/ShadowRoot.h b/Libraries/LibWeb/DOM/ShadowRoot.h
index b3aa60b4b28..e13702f1998 100644
--- a/Libraries/LibWeb/DOM/ShadowRoot.h
+++ b/Libraries/LibWeb/DOM/ShadowRoot.h
@@ -181,8 +181,8 @@ private:
OwnPtr<SlotRegistry> m_slot_registry;
Vector<GC::Ref<HTML::HTMLSlotElement>> m_slots_needing_assignment;
- GC::Ptr<CSS::StyleSheetList> m_style_sheets;
- mutable GC::Ptr<WebIDL::ObservableArray> m_adopted_style_sheets;
+ GC::Member<CSS::StyleSheetList> m_style_sheets;
+ mutable GC::Member<WebIDL::ObservableArray> m_adopted_style_sheets;
IntrusiveListNode<ShadowRoot> m_list_node;
IntrusiveListNode<ShadowRoot> m_slot_assignment_list_node;
@@ -193,12 +193,12 @@ private:
mutable Optional<u64> m_dom_tree_version_when_calculated_part_element_map;
// https://dom.spec.whatwg.org/#shadowroot-custom-element-registry
- GC::Ptr<HTML::CustomElementRegistry> m_custom_element_registry;
+ GC::Member<HTML::CustomElementRegistry> m_custom_element_registry;
// https://dom.spec.whatwg.org/#shadowroot-keep-custom-element-registry-null
bool m_keep_custom_element_registry_null { false };
- GC::Ptr<HTML::RadioButtonGroupRegistry> m_radio_button_group_registry;
+ GC::Member<HTML::RadioButtonGroupRegistry> m_radio_button_group_registry;
public:
using DocumentShadowRootList = IntrusiveList<&ShadowRoot::m_list_node>;
diff --git a/Libraries/LibWeb/DOM/Text.cpp b/Libraries/LibWeb/DOM/Text.cpp
index 8d05311d6fb..0e0dc9ccc13 100644
--- a/Libraries/LibWeb/DOM/Text.cpp
+++ b/Libraries/LibWeb/DOM/Text.cpp
@@ -17,6 +17,10 @@
namespace Web::DOM {
+#if ARCH(X86_64) && !defined(AK_OS_WINDOWS)
+static_assert(sizeof(Text) == 144);
+#endif
+
struct Text::RareData final
: CharacterData::RareData
, SlottableMixin::RareData {
diff --git a/Libraries/LibWeb/HTML/EventHandler.h b/Libraries/LibWeb/HTML/EventHandler.h
index 13c72d17a3d..cabc522fdc3 100644
--- a/Libraries/LibWeb/HTML/EventHandler.h
+++ b/Libraries/LibWeb/HTML/EventHandler.h
@@ -22,6 +22,10 @@ public:
explicit EventHandler(Utf16String);
explicit EventHandler(WebIDL::CallbackType&);
+ // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-listener
+ // NB: This comes first, so that it fills the padding at the end of the cell header.
+ GC::Member<DOM::DOMEventListener> listener;
+
// Either uncompiled source code or a callback.
// https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-value
// NOTE: This does not contain Empty as part of the optimization of not allocating all event handler attributes up front.
@@ -29,9 +33,6 @@ public:
// https://html.spec.whatwg.org/multipage/webappapis.html#internal-raw-uncompiled-handler
Variant<Utf16String, GC::Ptr<WebIDL::CallbackType>> value;
- // https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-listener
- GC::Ptr<DOM::DOMEventListener> listener;
-
private:
virtual void visit_edges(Cell::Visitor&) override;
};
diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
index 2ba543bd383..6fb84e67df7 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
@@ -136,7 +136,7 @@ WebIDL::ExceptionOr<void> HTMLDialogElement::show()
m_previously_focused_element = document().focused_area();
// 8. Let document be this's node document.
- auto document = m_document;
+ GC::Ptr<DOM::Document> document = m_document;
// 9. Let hideUntil be the result of running topmost popover ancestor given this, document's showing hint popover
// list, null, and false.
diff --git a/Libraries/LibWeb/TreeNode.h b/Libraries/LibWeb/TreeNode.h
index 6f466396fdf..fd3c6a8f606 100644
--- a/Libraries/LibWeb/TreeNode.h
+++ b/Libraries/LibWeb/TreeNode.h
@@ -10,6 +10,7 @@
#include <AK/IterationDecision.h>
#include <AK/TypeCasts.h>
#include <AK/Vector.h>
+#include <LibGC/Member.h>
#include <LibGC/Ptr.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/Forward.h>
@@ -51,29 +52,29 @@ TraversalDecision traverse_preorder(T root, Callback callback)
template<typename T>
class WEB_API TreeNode {
public:
- T* parent() { return m_parent; }
- T const* parent() const { return m_parent; }
-
- bool has_children() const { return m_first_child; }
- T* next_sibling() { return m_next_sibling; }
- T* previous_sibling() { return m_previous_sibling; }
- T* first_child() { return m_first_child; }
- T* last_child() { return m_last_child; }
- T const* next_sibling() const { return m_next_sibling; }
- T const* previous_sibling() const { return m_previous_sibling; }
- T const* first_child() const { return m_first_child; }
- T const* last_child() const { return m_last_child; }
+ T* parent() { return m_parent.ptr(); }
+ T const* parent() const { return m_parent.ptr(); }
+
+ bool has_children() const { return !!m_first_child; }
+ T* next_sibling() { return m_next_sibling.ptr(); }
+ T* previous_sibling() { return m_previous_sibling.ptr(); }
+ T* first_child() { return m_first_child.ptr(); }
+ T* last_child() { return m_last_child.ptr(); }
+ T const* next_sibling() const { return m_next_sibling.ptr(); }
+ T const* previous_sibling() const { return m_previous_sibling.ptr(); }
+ T const* first_child() const { return m_first_child.ptr(); }
+ T const* last_child() const { return m_last_child.ptr(); }
template<typename Derived>
- static constexpr size_t parent_offset() { return offsetof(Derived, m_parent); }
+ static constexpr auto parent_offset() { return GC_FIELD_OFFSET(Derived, m_parent); }
template<typename Derived>
- static constexpr size_t first_child_offset() { return offsetof(Derived, m_first_child); }
+ static constexpr auto first_child_offset() { return GC_FIELD_OFFSET(Derived, m_first_child); }
template<typename Derived>
- static constexpr size_t last_child_offset() { return offsetof(Derived, m_last_child); }
+ static constexpr auto last_child_offset() { return GC_FIELD_OFFSET(Derived, m_last_child); }
template<typename Derived>
- static constexpr size_t previous_sibling_offset() { return offsetof(Derived, m_previous_sibling); }
+ static constexpr auto previous_sibling_offset() { return GC_FIELD_OFFSET(Derived, m_previous_sibling); }
template<typename Derived>
- static constexpr size_t next_sibling_offset() { return offsetof(Derived, m_next_sibling); }
+ static constexpr auto next_sibling_offset() { return GC_FIELD_OFFSET(Derived, m_next_sibling); }
// https://dom.spec.whatwg.org/#concept-tree-index
size_t index() const
@@ -483,11 +484,11 @@ protected:
}
private:
- T* m_parent { nullptr };
- T* m_first_child { nullptr };
- T* m_last_child { nullptr };
- T* m_next_sibling { nullptr };
- T* m_previous_sibling { nullptr };
+ GC::Member<T> m_parent;
+ GC::Member<T> m_first_child;
+ GC::Member<T> m_last_child;
+ GC::Member<T> m_next_sibling;
+ GC::Member<T> m_previous_sibling;
};
template<typename T>
diff --git a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
index 1ca75c6038f..220a466126a 100644
--- a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
+++ b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp
@@ -192,7 +192,7 @@ JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString
this_argument = JS::js_undefined();
// 3. Let O be the ECMAScript object corresponding to value.
- auto& object = callback.callback;
+ GC::Ref<JS::Object> object = callback.callback;
// 4. Let realm be O’s associated realm.
auto& realm = callback.associated_realm();
@@ -201,7 +201,7 @@ JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString
auto& relevant_settings = HTML::principal_realm_settings_object(realm);
// 6. Let stored settings be value’s callback context.
- auto& stored_settings = callback.callback_context;
+ GC::Ref<HTML::EnvironmentSettingsObject> stored_settings = callback.callback_context;
// AD-HOC: See is_settings_object_of_inactive_window().
if (refuse_from_inactive_window == RefuseCallbackFromInactiveWindow::Yes && is_settings_object_of_inactive_window(relevant_settings))
@@ -300,7 +300,7 @@ static auto invoke_callback_impl(CallbackType& callback, Optional<JS::Value> thi
this_argument = JS::js_undefined();
// 3. Let F be the ECMAScript object corresponding to callable.
- auto& function_object = callback.callback;
+ GC::Ref<JS::Object> function_object = callback.callback;
// 5. Let realm be F’s associated realm.
auto& realm = function_object->shape().realm();
@@ -317,7 +317,7 @@ static auto invoke_callback_impl(CallbackType& callback, Optional<JS::Value> thi
}
// 7. Let stored settings be callable’s callback context.
- auto& stored_settings = callback.callback_context;
+ GC::Ref<HTML::EnvironmentSettingsObject> stored_settings = callback.callback_context;
// AD-HOC: See is_settings_object_of_inactive_window().
if (is_settings_object_of_inactive_window(relevant_settings))
@@ -431,7 +431,7 @@ JS::Completion construct(CallbackType& callable, ReadonlySpan<JS::Value> args)
JS::Completion completion;
// 2. Let F be the JavaScript object corresponding to callable.
- auto& function_object = callable.callback;
+ GC::Ref<JS::Object> function_object = callable.callback;
// 3. If IsConstructor(F) is false, throw a TypeError exception.
// 4. Let realm be F’s associated realm.
@@ -443,7 +443,7 @@ JS::Completion construct(CallbackType& callable, ReadonlySpan<JS::Value> args)
auto& relevant_settings = HTML::principal_realm_settings_object(realm);
// 6. Let stored settings be callable’s callback context.
- auto& stored_settings = callable.callback_context;
+ GC::Ref<HTML::EnvironmentSettingsObject> stored_settings = callable.callback_context;
// AD-HOC: See is_settings_object_of_inactive_window().
if (is_settings_object_of_inactive_window(relevant_settings))
diff --git a/Libraries/LibWeb/WebIDL/CallbackType.h b/Libraries/LibWeb/WebIDL/CallbackType.h
index cfe5a548993..3461dc7581a 100644
--- a/Libraries/LibWeb/WebIDL/CallbackType.h
+++ b/Libraries/LibWeb/WebIDL/CallbackType.h
@@ -29,10 +29,10 @@ public:
JS::Realm& associated_realm() const;
- GC::Ref<JS::Object> callback;
+ GC::NonnullMember<JS::Object> callback;
// https://webidl.spec.whatwg.org/#dfn-callback-context
- GC::Ref<HTML::EnvironmentSettingsObject> callback_context;
+ GC::NonnullMember<HTML::EnvironmentSettingsObject> callback_context;
// Non-standard property used to distinguish Promise-returning callbacks in callback-related AOs
OperationReturnsPromise operation_returns_promise;
diff --git a/Tests/ClangPlugins/LibJSGCTests/cell_constructed_outside_heap.cpp b/Tests/ClangPlugins/LibJSGCTests/cell_constructed_outside_heap.cpp
index 0cd073d6fdc..a56464f2fb7 100644
--- a/Tests/ClangPlugins/LibJSGCTests/cell_constructed_outside_heap.cpp
+++ b/Tests/ClangPlugins/LibJSGCTests/cell_constructed_outside_heap.cpp
@@ -16,6 +16,10 @@ class TestCell : public GC::Cell {
public:
TestCell() = default;
+
+private:
+ // A cell must be at least as large as a free cell.
+ FlatPtr m_payload { 0 };
};
GC_DEFINE_ALLOCATOR(TestCell);