From 25daa14a054a1866afbe881fe4d055c07e1d70c4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 4 Sep 2022 14:14:22 +0200 Subject: [PATCH] LibWeb: Make IntersectionObserver GC-allocated --- Userland/Libraries/LibWeb/Forward.h | 1 - .../IntersectionObserver.cpp | 13 +++++++++-- .../IntersectionObserver.h | 22 ++++++++++--------- Userland/Libraries/LibWeb/idl_files.cmake | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 2ce0367fe5..bc711fdb32 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -449,7 +449,6 @@ class URLSearchParamsIterator; namespace Web::Bindings { class DOMExceptionWrapper; -class IntersectionObserverWrapper; class LocationObject; class OptionConstructor; class RangePrototype; diff --git a/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp b/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp index 6880ff33cf..7bd00dc103 100644 --- a/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp +++ b/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.cpp @@ -5,20 +5,29 @@ */ #include +#include #include namespace Web::IntersectionObserver { // https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-intersectionobserver -NonnullRefPtr IntersectionObserver::create_with_global_object(HTML::Window&, Bindings::CallbackType* callback, IntersectionObserverInit const& options) +JS::NonnullGCPtr IntersectionObserver::create_with_global_object(HTML::Window& window, Bindings::CallbackType* callback, IntersectionObserverInit const& options) { // FIXME: Implement (void)callback; (void)options; - return adopt_ref(*new IntersectionObserver); + return *window.heap().allocate(window.realm(), window); } +IntersectionObserver::IntersectionObserver(HTML::Window& window) + : PlatformObject(window.realm()) +{ + set_prototype(&window.cached_web_prototype("IntersectionObserver")); +} + +IntersectionObserver::~IntersectionObserver() = default; + // https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-observe void IntersectionObserver::observe(DOM::Element& target) { diff --git a/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h b/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h index 97f775cb2b..859babd244 100644 --- a/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h +++ b/Userland/Libraries/LibWeb/IntersectionObserver/IntersectionObserver.h @@ -6,10 +6,7 @@ #pragma once -#include -#include -#include -#include +#include namespace Web::IntersectionObserver { @@ -20,17 +17,22 @@ struct IntersectionObserverInit { }; // https://w3c.github.io/IntersectionObserver/#intersection-observer-interface -class IntersectionObserver - : public RefCounted - , public Bindings::Wrappable { -public: - using WrapperType = Bindings::IntersectionObserverWrapper; +class IntersectionObserver : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(IntersectionObserver, Bindings::PlatformObject); - static NonnullRefPtr create_with_global_object(HTML::Window&, Bindings::CallbackType* callback, IntersectionObserverInit const& options = {}); +public: + static JS::NonnullGCPtr create_with_global_object(HTML::Window&, Bindings::CallbackType* callback, IntersectionObserverInit const& options = {}); + + virtual ~IntersectionObserver() override; void observe(DOM::Element& target); void unobserve(DOM::Element& target); void disconnect(); + +private: + explicit IntersectionObserver(HTML::Window&); }; } + +WRAPPER_HACK(IntersectionObserver, Web::IntersectionObserver) diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index ec5bd20db9..20db0f4104 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -155,7 +155,7 @@ libweb_js_wrapper(HTML/WorkerGlobalScope NO_INSTANCE) libweb_js_wrapper(HTML/WorkerLocation) libweb_js_wrapper(HTML/WorkerNavigator) libweb_js_wrapper(HighResolutionTime/Performance NO_INSTANCE) -libweb_js_wrapper(IntersectionObserver/IntersectionObserver) +libweb_js_wrapper(IntersectionObserver/IntersectionObserver NO_INSTANCE) libweb_js_wrapper(NavigationTiming/PerformanceTiming NO_INSTANCE) libweb_js_wrapper(RequestIdleCallback/IdleDeadline NO_INSTANCE) libweb_js_wrapper(ResizeObserver/ResizeObserver NO_INSTANCE)