ソースを参照

LibWeb: Make DOMRect, DOMRectReadOnly and DOMRectList GC-allocated

Andreas Kling 2 年 前
コミット
57db2529cf

+ 2 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -137,7 +137,9 @@ set(SOURCES
     FontCache.cpp
     Geometry/DOMPoint.cpp
     Geometry/DOMPointReadOnly.cpp
+    Geometry/DOMRect.cpp
     Geometry/DOMRectList.cpp
+    Geometry/DOMRectReadOnly.cpp
     HTML/AttributeNames.cpp
     HTML/BrowsingContext.cpp
     HTML/BrowsingContextContainer.cpp

+ 8 - 8
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -561,7 +561,7 @@ bool Element::serializes_as_void() const
 }
 
 // https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
-NonnullRefPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
+JS::NonnullGCPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
 {
     // // NOTE: Ensure that layout is up-to-date before looking at metrics.
     const_cast<Document&>(document()).update_layout();
@@ -569,22 +569,22 @@ NonnullRefPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
     // FIXME: Support inline layout nodes as well.
     auto* paint_box = this->paint_box();
     if (!paint_box)
-        return Geometry::DOMRect::create(0, 0, 0, 0);
+        return Geometry::DOMRect::create_with_global_object(window(), 0, 0, 0, 0);
 
     VERIFY(document().browsing_context());
     auto viewport_offset = document().browsing_context()->viewport_scroll_offset();
 
-    return Geometry::DOMRect::create(paint_box->absolute_rect().translated(-viewport_offset.x(), -viewport_offset.y()));
+    return Geometry::DOMRect::create(window(), paint_box->absolute_rect().translated(-viewport_offset.x(), -viewport_offset.y()));
 }
 
 // https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
-NonnullRefPtr<Geometry::DOMRectList> Element::get_client_rects() const
+JS::NonnullGCPtr<Geometry::DOMRectList> Element::get_client_rects() const
 {
-    NonnullRefPtrVector<Geometry::DOMRect> rects;
+    Vector<JS::Handle<Geometry::DOMRect>> rects;
 
     // 1. If the element on which it was invoked does not have an associated layout box return an empty DOMRectList object and stop this algorithm.
     if (!layout_node() || !layout_node()->is_box())
-        return Geometry::DOMRectList::create(move(rects));
+        return Geometry::DOMRectList::create(window(), move(rects));
 
     // FIXME: 2. If the element has an associated SVG layout box return a DOMRectList object containing a single DOMRect object that describes
     // the bounding box of the element as defined by the SVG specification, applying the transforms that apply to the element and its ancestors.
@@ -597,8 +597,8 @@ NonnullRefPtr<Geometry::DOMRectList> Element::get_client_rects() const
     // - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes are left in the final list.
 
     auto bounding_rect = get_bounding_client_rect();
-    rects.append(bounding_rect);
-    return Geometry::DOMRectList::create(move(rects));
+    rects.append(*bounding_rect);
+    return Geometry::DOMRectList::create(window(), move(rects));
 }
 
 int Element::client_top() const

+ 2 - 2
Userland/Libraries/LibWeb/DOM/Element.h

@@ -125,8 +125,8 @@ public:
     bool is_void_element() const;
     bool serializes_as_void() const;
 
-    NonnullRefPtr<Geometry::DOMRect> get_bounding_client_rect() const;
-    NonnullRefPtr<Geometry::DOMRectList> get_client_rects() const;
+    JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
+    JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
 
     virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
 

+ 0 - 3
Userland/Libraries/LibWeb/Forward.h

@@ -451,9 +451,6 @@ namespace Web::Bindings {
 class BlobWrapper;
 class CryptoWrapper;
 class DOMExceptionWrapper;
-class DOMRectListWrapper;
-class DOMRectReadOnlyWrapper;
-class DOMRectWrapper;
 class FileWrapper;
 class HeadersWrapper;
 class HeadersIteratorWrapper;

+ 30 - 0
Userland/Libraries/LibWeb/Geometry/DOMRect.cpp

@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Geometry/DOMRect.h>
+#include <LibWeb/HTML/Window.h>
+
+namespace Web::Geometry {
+
+JS::NonnullGCPtr<DOMRect> DOMRect::create_with_global_object(HTML::Window& window, double x, double y, double width, double height)
+{
+    return *window.heap().allocate<DOMRect>(window.realm(), window, x, y, width, height);
+}
+
+JS::NonnullGCPtr<DOMRect> DOMRect::create(HTML::Window& window, Gfx::FloatRect const& rect)
+{
+    return create_with_global_object(window, rect.x(), rect.y(), rect.width(), rect.height());
+}
+
+DOMRect::DOMRect(HTML::Window& window, double x, double y, double width, double height)
+    : DOMRectReadOnly(window, x, y, width, height)
+{
+    set_prototype(&window.cached_web_prototype("DOMRect"));
+}
+
+DOMRect::~DOMRect() = default;
+
+}

+ 10 - 21
Userland/Libraries/LibWeb/Geometry/DOMRect.h

@@ -11,25 +11,14 @@
 namespace Web::Geometry {
 
 // https://drafts.fxtf.org/geometry/#DOMRect
-class DOMRect final
-    : public DOMRectReadOnly {
-public:
-    using WrapperType = Bindings::DOMRectWrapper;
-
-    static NonnullRefPtr<DOMRect> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
-    {
-        return DOMRect::create(x, y, width, height);
-    }
+class DOMRect final : public DOMRectReadOnly {
+    WEB_PLATFORM_OBJECT(DOMRect, DOMRectReadOnly);
 
-    static NonnullRefPtr<DOMRect> create(double x = 0, double y = 0, double width = 0, double height = 0)
-    {
-        return adopt_ref(*new DOMRect(x, y, width, height));
-    }
+public:
+    static JS::NonnullGCPtr<DOMRect> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0);
+    static JS::NonnullGCPtr<DOMRect> create(HTML::Window&, Gfx::FloatRect const&);
 
-    static NonnullRefPtr<DOMRect> create(Gfx::FloatRect const& rect)
-    {
-        return adopt_ref(*new DOMRect(rect.x(), rect.y(), rect.width(), rect.height()));
-    }
+    virtual ~DOMRect() override;
 
     double x() const { return m_rect.x(); }
     double y() const { return m_rect.y(); }
@@ -42,9 +31,9 @@ public:
     void set_height(double height) { m_rect.set_height(height); }
 
 private:
-    DOMRect(float x, float y, float width, float height)
-        : DOMRectReadOnly(x, y, width, height)
-    {
-    }
+    DOMRect(HTML::Window&, double x, double y, double width, double height);
 };
+
 }
+
+WRAPPER_HACK(DOMRect, Web::Geometry)

+ 24 - 3
Userland/Libraries/LibWeb/Geometry/DOMRectList.cpp

@@ -4,16 +4,29 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <LibJS/Heap/Handle.h>
 #include <LibWeb/Geometry/DOMRect.h>
 #include <LibWeb/Geometry/DOMRectList.h>
+#include <LibWeb/HTML/Window.h>
 
 namespace Web::Geometry {
 
-DOMRectList::DOMRectList(NonnullRefPtrVector<DOMRect>&& rects)
-    : m_rects(move(rects))
+JS::NonnullGCPtr<DOMRectList> DOMRectList::create(HTML::Window& window, Vector<JS::Handle<DOMRect>> rect_handles)
 {
+    Vector<JS::NonnullGCPtr<DOMRect>> rects;
+    for (auto& rect : rect_handles)
+        rects.append(*rect);
+    return *window.heap().allocate<DOMRectList>(window.realm(), window, move(rects));
 }
 
+DOMRectList::DOMRectList(HTML::Window& window, Vector<JS::NonnullGCPtr<DOMRect>> rects)
+    : Bindings::LegacyPlatformObject(window.cached_web_prototype("DOMRectList"))
+    , m_rects(move(rects))
+{
+}
+
+DOMRectList::~DOMRectList() = default;
+
 // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
 u32 DOMRectList::length() const
 {
@@ -28,7 +41,7 @@ DOMRect const* DOMRectList::item(u32 index) const
     // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
     if (index >= m_rects.size())
         return nullptr;
-    return &m_rects[index];
+    return m_rects[index];
 }
 
 bool DOMRectList::is_supported_property_index(u32 index) const
@@ -36,4 +49,12 @@ bool DOMRectList::is_supported_property_index(u32 index) const
     return index < m_rects.size();
 }
 
+JS::Value DOMRectList::item_value(size_t index) const
+{
+    if (index >= m_rects.size())
+        return JS::js_undefined();
+
+    return m_rects[index].ptr();
+}
+
 }

+ 11 - 20
Userland/Libraries/LibWeb/Geometry/DOMRectList.h

@@ -6,42 +6,33 @@
 
 #pragma once
 
-#include <AK/Noncopyable.h>
-#include <AK/NonnullRefPtrVector.h>
-#include <AK/RefCounted.h>
 #include <AK/Vector.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/Forward.h>
+#include <LibWeb/Bindings/LegacyPlatformObject.h>
 #include <LibWeb/Geometry/DOMRect.h>
 
 namespace Web::Geometry {
 
 // https://drafts.fxtf.org/geometry-1/#DOMRectList
-class DOMRectList final
-    : public RefCounted<DOMRectList>
-    , public Bindings::Wrappable {
-    AK_MAKE_NONCOPYABLE(DOMRectList);
-    AK_MAKE_NONMOVABLE(DOMRectList);
+class DOMRectList final : public Bindings::LegacyPlatformObject {
+    WEB_PLATFORM_OBJECT(DOMRectList, Bindings::LegacyPlatformObject);
 
 public:
-    using WrapperType = Bindings::DOMRectListWrapper;
+    static JS::NonnullGCPtr<DOMRectList> create(HTML::Window&, Vector<JS::Handle<DOMRect>>);
 
-    static NonnullRefPtr<DOMRectList> create(NonnullRefPtrVector<DOMRect>&& rects)
-    {
-        return adopt_ref(*new DOMRectList(move(rects)));
-    }
-
-    ~DOMRectList() = default;
+    virtual ~DOMRectList() override;
 
     u32 length() const;
     DOMRect const* item(u32 index) const;
 
-    bool is_supported_property_index(u32) const;
+    virtual bool is_supported_property_index(u32) const override;
+    virtual JS::Value item_value(size_t index) const override;
 
 private:
-    DOMRectList(NonnullRefPtrVector<DOMRect>&& rects);
+    DOMRectList(HTML::Window&, Vector<JS::NonnullGCPtr<DOMRect>>);
 
-    NonnullRefPtrVector<DOMRect> m_rects;
+    Vector<JS::NonnullGCPtr<DOMRect>> m_rects;
 };
 
 }
+
+WRAPPER_HACK(DOMRectList, Web::Geometry)

+ 26 - 0
Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp

@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Geometry/DOMRectReadOnly.h>
+#include <LibWeb/HTML/Window.h>
+
+namespace Web::Geometry {
+
+JS::NonnullGCPtr<DOMRectReadOnly> DOMRectReadOnly::create_with_global_object(HTML::Window& window, double x, double y, double width, double height)
+{
+    return *window.heap().allocate<DOMRectReadOnly>(window.realm(), window, x, y, width, height);
+}
+
+DOMRectReadOnly::DOMRectReadOnly(HTML::Window& window, double x, double y, double width, double height)
+    : PlatformObject(window.realm())
+    , m_rect(x, y, width, height)
+{
+    set_prototype(&window.cached_web_prototype("DOMRectReadOnly"));
+}
+
+DOMRectReadOnly::~DOMRectReadOnly() = default;
+
+}

+ 9 - 19
Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h

@@ -6,29 +6,20 @@
 
 #pragma once
 
-#include <AK/RefCounted.h>
 #include <LibGfx/Rect.h>
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/PlatformObject.h>
 #include <LibWeb/Forward.h>
 
 namespace Web::Geometry {
 
 // https://drafts.fxtf.org/geometry/#domrectreadonly
-class DOMRectReadOnly
-    : public RefCounted<DOMRectReadOnly>
-    , public Bindings::Wrappable {
-public:
-    using WrapperType = Bindings::DOMRectReadOnlyWrapper;
+class DOMRectReadOnly : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
 
-    static NonnullRefPtr<DOMRectReadOnly> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
-    {
-        return DOMRectReadOnly::create(x, y, width, height);
-    }
+public:
+    static JS::NonnullGCPtr<DOMRectReadOnly> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0);
 
-    static NonnullRefPtr<DOMRectReadOnly> create(double x = 0, double y = 0, double width = 0, double height = 0)
-    {
-        return adopt_ref(*new DOMRectReadOnly(x, y, width, height));
-    }
+    virtual ~DOMRectReadOnly() override;
 
     double x() const { return m_rect.x(); }
     double y() const { return m_rect.y(); }
@@ -41,11 +32,10 @@ public:
     double left() const { return min(x(), x() + width()); }
 
 protected:
-    DOMRectReadOnly(float x, float y, float width, float height)
-        : m_rect(x, y, width, height)
-    {
-    }
+    DOMRectReadOnly(HTML::Window&, double x, double y, double width, double height);
 
     Gfx::FloatRect m_rect;
 };
 }
+
+WRAPPER_HACK(DOMRectReadOnly, Web::Geometry)

+ 3 - 3
Userland/Libraries/LibWeb/idl_files.cmake

@@ -58,9 +58,9 @@ libweb_js_wrapper(FileAPI/Blob)
 libweb_js_wrapper(FileAPI/File)
 libweb_js_wrapper(Geometry/DOMPoint NO_INSTANCE)
 libweb_js_wrapper(Geometry/DOMPointReadOnly NO_INSTANCE)
-libweb_js_wrapper(Geometry/DOMRect)
-libweb_js_wrapper(Geometry/DOMRectList)
-libweb_js_wrapper(Geometry/DOMRectReadOnly)
+libweb_js_wrapper(Geometry/DOMRect NO_INSTANCE)
+libweb_js_wrapper(Geometry/DOMRectList NO_INSTANCE)
+libweb_js_wrapper(Geometry/DOMRectReadOnly NO_INSTANCE)
 libweb_js_wrapper(HTML/CanvasGradient NO_INSTANCE)
 libweb_js_wrapper(HTML/CanvasRenderingContext2D NO_INSTANCE)
 libweb_js_wrapper(HTML/CloseEvent NO_INSTANCE)