LibWeb: Make Path2D GC-allocated

This commit is contained in:
Andreas Kling 2022-09-02 23:35:06 +02:00
parent 0d2fee351a
commit 2704bcdaaa
Notes: sideshowbarker 2024-07-17 07:26:44 +09:00
5 changed files with 28 additions and 17 deletions

View file

@ -155,6 +155,8 @@ static bool impl_is_wrapper(Type const& type)
return true;
if (type.name == "WebGLRenderingContext"sv)
return true;
if (type.name == "Path2D"sv)
return true;
return false;
}

View file

@ -463,7 +463,6 @@ class IdleDeadlineWrapper;
class IntersectionObserverWrapper;
class LocationObject;
class OptionConstructor;
class Path2DWrapper;
class RangePrototype;
class ResizeObserverWrapper;
class SelectionWrapper;

View file

@ -4,13 +4,23 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Path2DPrototype.h>
#include <LibWeb/HTML/Path2D.h>
#include <LibWeb/HTML/Window.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d
Path2D::Path2D(Optional<Variant<NonnullRefPtr<Path2D>, String>> const& path)
JS::NonnullGCPtr<Path2D> Path2D::create_with_global_object(HTML::Window& window, Optional<Variant<JS::Handle<Path2D>, String>> const& path)
{
return *window.heap().allocate<Path2D>(window.realm(), window, path);
}
// https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d
Path2D::Path2D(HTML::Window& window, Optional<Variant<JS::Handle<Path2D>, String>> const& path)
: PlatformObject(window.realm())
{
set_prototype(&window.ensure_web_prototype<Bindings::Path2DPrototype>("Path2D"));
// 1. Let output be a new Path2D object.
// 2. If path is not given, then return output.
if (!path.has_value())
@ -18,8 +28,8 @@ Path2D::Path2D(Optional<Variant<NonnullRefPtr<Path2D>, String>> const& path)
// 3. If path is a Path2D object, then add all subpaths of path to output and return output.
// (In other words, it returns a copy of the argument.)
if (path->has<NonnullRefPtr<Path2D>>()) {
this->path() = path->get<NonnullRefPtr<Path2D>>()->path();
if (path->has<JS::Handle<Path2D>>()) {
this->path() = path->get<JS::Handle<Path2D>>()->path();
return;
}
@ -32,4 +42,6 @@ Path2D::Path2D(Optional<Variant<NonnullRefPtr<Path2D>, String>> const& path)
// FIXME: 8. Return output.
}
Path2D::~Path2D() = default;
}

View file

@ -8,29 +8,27 @@
#include <AK/RefCounted.h>
#include <LibGfx/Path.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/canvas.html#path2d
class Path2D
: public RefCounted<Path2D>
, public Bindings::Wrappable
class Path2D final
: public Bindings::PlatformObject
, public CanvasPath {
AK_MAKE_NONCOPYABLE(Path2D);
AK_MAKE_NONMOVABLE(Path2D);
WEB_PLATFORM_OBJECT(Path2D, Bindings::PlatformObject);
public:
using WrapperType = Bindings::Path2DWrapper;
static JS::NonnullGCPtr<Path2D> create_with_global_object(HTML::Window&, Optional<Variant<JS::Handle<Path2D>, String>> const& path);
static NonnullRefPtr<Path2D> create_with_global_object(HTML::Window&, Optional<Variant<NonnullRefPtr<Path2D>, String>> const& path) { return adopt_ref(*new Path2D(path)); }
static NonnullRefPtr<Path2D> create(Optional<Variant<NonnullRefPtr<Path2D>, String>> const& path) { return adopt_ref(*new Path2D(path)); }
~Path2D() = default;
virtual ~Path2D() override;
private:
Path2D(Optional<Variant<NonnullRefPtr<Path2D>, String>> const&);
Path2D(HTML::Window&, Optional<Variant<JS::Handle<Path2D>, String>> const&);
};
}
WRAPPER_HACK(Path2D, Web::HTML)

View file

@ -145,7 +145,7 @@ libweb_js_wrapper(HTML/MessageChannel NO_INSTANCE)
libweb_js_wrapper(HTML/MessageEvent NO_INSTANCE)
libweb_js_wrapper(HTML/MessagePort NO_INSTANCE)
libweb_js_wrapper(HTML/PageTransitionEvent NO_INSTANCE)
libweb_js_wrapper(HTML/Path2D)
libweb_js_wrapper(HTML/Path2D NO_INSTANCE)
libweb_js_wrapper(HTML/PromiseRejectionEvent NO_INSTANCE)
libweb_js_wrapper(HTML/Storage)
libweb_js_wrapper(HTML/SubmitEvent NO_INSTANCE)