From ae11d70b0c5cacf1b10481f3991fcaeaf11f859b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 8 Aug 2022 14:49:54 +0200 Subject: LibWeb: Make DOMStringMap GC-allocated --- Userland/Libraries/LibWeb/Forward.h | 1 - Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp | 14 ++++++++++-- Userland/Libraries/LibWeb/HTML/DOMStringMap.h | 29 ++++++++++++------------- Userland/Libraries/LibWeb/HTML/HTMLElement.cpp | 2 +- Userland/Libraries/LibWeb/HTML/HTMLElement.h | 5 +++-- Userland/Libraries/LibWeb/SVG/SVGElement.cpp | 2 +- Userland/Libraries/LibWeb/SVG/SVGElement.h | 5 +++-- Userland/Libraries/LibWeb/idl_files.cmake | 2 +- 8 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index eb06ef7f75..dd01406f82 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -471,7 +471,6 @@ class DOMPointReadOnlyWrapper; class DOMRectListWrapper; class DOMRectReadOnlyWrapper; class DOMRectWrapper; -class DOMStringMapWrapper; class DOMTokenListWrapper; class ElementWrapper; class ErrorEventWrapper; diff --git a/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp b/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp index bf9ef0b5e6..3e276996ed 100644 --- a/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp +++ b/Userland/Libraries/LibWeb/HTML/DOMStringMap.cpp @@ -5,13 +5,23 @@ */ #include +#include +#include +#include #include #include namespace Web::HTML { -DOMStringMap::DOMStringMap(DOM::Element& associated_element) - : m_associated_element(associated_element) +DOMStringMap* DOMStringMap::create(DOM::Element& element) +{ + auto& realm = element.document().preferred_window_object().realm(); + return realm.heap().allocate(realm, element); +} + +DOMStringMap::DOMStringMap(DOM::Element& element) + : PlatformObject(element.document().preferred_window_object().ensure_web_prototype("DOMStringMap")) + , m_associated_element(element) { } diff --git a/Userland/Libraries/LibWeb/HTML/DOMStringMap.h b/Userland/Libraries/LibWeb/HTML/DOMStringMap.h index a29c85faa4..902037a2db 100644 --- a/Userland/Libraries/LibWeb/HTML/DOMStringMap.h +++ b/Userland/Libraries/LibWeb/HTML/DOMStringMap.h @@ -1,33 +1,29 @@ /* * Copyright (c) 2021, Luke Wilde + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include -#include -#include +#include #include namespace Web::HTML { // https://html.spec.whatwg.org/multipage/dom.html#domstringmap -class DOMStringMap final - : public RefCounted - , public Weakable - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::DOMStringMapWrapper; +class DOMStringMap final : public Bindings::PlatformObject { + JS_OBJECT(DOMStringMap, Bindings::PlatformObject); - static NonnullRefPtr create(DOM::Element& associated_element) - { - return adopt_ref(*new DOMStringMap(associated_element)); - } +public: + static DOMStringMap* create(DOM::Element&); + explicit DOMStringMap(DOM::Element&); virtual ~DOMStringMap() override; + DOMStringMap& impl() { return *this; } + Vector supported_property_names() const; String determine_value_of_named_property(String const&) const; @@ -38,8 +34,6 @@ public: bool delete_existing_named_property(String const&); private: - DOMStringMap(DOM::Element&); - struct NameValuePair { String name; String value; @@ -52,3 +46,8 @@ private: }; } + +namespace Web::Bindings { +inline JS::Object* wrap(JS::Realm&, Web::HTML::DOMStringMap& object) { return &object; } +using DOMStringMapWrapper = Web::HTML::DOMStringMap; +} diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp index 935b45d32d..36fd97e8c6 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.cpp @@ -30,7 +30,7 @@ namespace Web::HTML { HTMLElement::HTMLElement(DOM::Document& document, DOM::QualifiedName qualified_name) : Element(document, move(qualified_name)) - , m_dataset(DOMStringMap::create(*this)) + , m_dataset(JS::make_handle(DOMStringMap::create(*this))) { } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLElement.h b/Userland/Libraries/LibWeb/HTML/HTMLElement.h index 9baa00569a..1c0376668e 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLElement.h @@ -38,7 +38,8 @@ public: bool cannot_navigate() const; - NonnullRefPtr dataset() const { return m_dataset; } + DOMStringMap* dataset() { return m_dataset.cell(); } + DOMStringMap const* dataset() const { return m_dataset.cell(); } void focus(); @@ -65,7 +66,7 @@ private: }; ContentEditableState content_editable_state() const; - NonnullRefPtr m_dataset; + JS::Handle m_dataset; // https://html.spec.whatwg.org/multipage/interaction.html#locked-for-focus bool m_locked_for_focus { false }; diff --git a/Userland/Libraries/LibWeb/SVG/SVGElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGElement.cpp index 4119706dd5..301231367a 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGElement.cpp @@ -10,7 +10,7 @@ namespace Web::SVG { SVGElement::SVGElement(DOM::Document& document, DOM::QualifiedName qualified_name) : Element(document, move(qualified_name)) - , m_dataset(HTML::DOMStringMap::create(*this)) + , m_dataset(JS::make_handle(HTML::DOMStringMap::create(*this))) { } diff --git a/Userland/Libraries/LibWeb/SVG/SVGElement.h b/Userland/Libraries/LibWeb/SVG/SVGElement.h index 0cd75788fe..af47434fb4 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGElement.h +++ b/Userland/Libraries/LibWeb/SVG/SVGElement.h @@ -17,12 +17,13 @@ public: virtual bool requires_svg_container() const override { return true; } - NonnullRefPtr dataset() const { return m_dataset; } + HTML::DOMStringMap* dataset() { return m_dataset.cell(); } + HTML::DOMStringMap const* dataset() const { return m_dataset.cell(); } protected: SVGElement(DOM::Document&, DOM::QualifiedName); - NonnullRefPtr m_dataset; + JS::Handle m_dataset; }; } diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 2cd19c0df9..62c83c4fbe 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -65,7 +65,7 @@ libweb_js_wrapper(HTML/CanvasGradient) libweb_js_wrapper(HTML/CanvasRenderingContext2D) libweb_js_wrapper(HTML/CloseEvent) libweb_js_wrapper(HTML/DOMParser) -libweb_js_wrapper(HTML/DOMStringMap) +libweb_js_wrapper(HTML/DOMStringMap NO_INSTANCE) libweb_js_wrapper(HTML/ErrorEvent) libweb_js_wrapper(HTML/History) libweb_js_wrapper(HTML/HTMLAnchorElement) -- cgit v1.2.3