1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:17:45 +00:00

LibWeb: Make DOMRect, DOMRectReadOnly and DOMRectList GC-allocated

This commit is contained in:
Andreas Kling 2022-09-04 01:59:58 +02:00
parent 44415af428
commit 57db2529cf
11 changed files with 125 additions and 79 deletions

View file

@ -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;
}

View file

@ -11,25 +11,14 @@
namespace Web::Geometry {
// https://drafts.fxtf.org/geometry/#DOMRect
class DOMRect final
: public DOMRectReadOnly {
class DOMRect final : public DOMRectReadOnly {
WEB_PLATFORM_OBJECT(DOMRect, DOMRectReadOnly);
public:
using WrapperType = Bindings::DOMRectWrapper;
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_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
{
return DOMRect::create(x, y, width, height);
}
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));
}
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)

View file

@ -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();
}
}

View file

@ -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)

View file

@ -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;
}

View file

@ -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 {
class DOMRectReadOnly : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
public:
using WrapperType = Bindings::DOMRectReadOnlyWrapper;
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_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
{
return DOMRectReadOnly::create(x, y, width, height);
}
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)