mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 07:17:35 +00:00
LibWeb+LibJS: Make the EventTarget hierarchy (incl. DOM) GC-allocated
This is a monster patch that turns all EventTargets into GC-allocated PlatformObjects. Their C++ wrapper classes are removed, and the LibJS garbage collector is now responsible for their lifetimes. There's a fair amount of hacks and band-aids in this patch, and we'll have a lot of cleanup to do after this.
This commit is contained in:
parent
bb547ce1c4
commit
6f433c8656
445 changed files with 4797 additions and 4268 deletions
|
@ -8,18 +8,14 @@
|
|||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/IDAllocator.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibWeb/Bindings/WindowObject.h>
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
#include <LibWeb/CSS/MediaQueryList.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibWeb/Bindings/CrossOriginAbstractOperations.h>
|
||||
#include <LibWeb/CSS/Screen.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
#include <LibWeb/DOM/EventTarget.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
|
||||
#include <LibWeb/HTML/BrowsingContext.h>
|
||||
#include <LibWeb/HTML/GlobalEventHandlers.h>
|
||||
#include <LibWeb/HTML/WindowEventHandlers.h>
|
||||
|
||||
|
@ -27,24 +23,22 @@ namespace Web::HTML {
|
|||
|
||||
class IdleCallback;
|
||||
|
||||
// https://html.spec.whatwg.org/#timerhandler
|
||||
using TimerHandler = Variant<JS::Handle<Bindings::CallbackType>, String>;
|
||||
|
||||
class Window final
|
||||
: public RefCounted<Window>
|
||||
, public Weakable<Window>
|
||||
, public DOM::EventTarget
|
||||
: public DOM::EventTarget
|
||||
, public HTML::GlobalEventHandlers
|
||||
, public HTML::WindowEventHandlers {
|
||||
WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);
|
||||
|
||||
public:
|
||||
static NonnullRefPtr<Window> create();
|
||||
static NonnullRefPtr<Window> create_with_document(DOM::Document&);
|
||||
static JS::NonnullGCPtr<Window> create(JS::Realm&);
|
||||
static JS::NonnullGCPtr<Window> create_with_document(DOM::Document&);
|
||||
|
||||
~Window();
|
||||
|
||||
using RefCounted::ref;
|
||||
using RefCounted::unref;
|
||||
|
||||
virtual void ref_event_target() override { RefCounted::ref(); }
|
||||
virtual void unref_event_target() override { RefCounted::unref(); }
|
||||
virtual bool dispatch_event(DOM::Event&) override;
|
||||
virtual JS::Object* create_wrapper(JS::Realm&) override;
|
||||
|
||||
Page* page();
|
||||
Page const* page() const;
|
||||
|
@ -55,22 +49,22 @@ public:
|
|||
void set_associated_document(DOM::Document&);
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/window-object.html#window-bc
|
||||
HTML::BrowsingContext const* browsing_context() const { return m_associated_document->browsing_context(); }
|
||||
HTML::BrowsingContext* browsing_context() { return m_associated_document->browsing_context(); }
|
||||
HTML::BrowsingContext const* browsing_context() const;
|
||||
HTML::BrowsingContext* browsing_context();
|
||||
|
||||
void alert(String const&);
|
||||
bool confirm(String const&);
|
||||
String prompt(String const&, String const&);
|
||||
i32 request_animation_frame(Bindings::CallbackType& js_callback);
|
||||
void cancel_animation_frame(i32);
|
||||
void alert_impl(String const&);
|
||||
bool confirm_impl(String const&);
|
||||
String prompt_impl(String const&, String const&);
|
||||
i32 request_animation_frame_impl(Bindings::CallbackType& js_callback);
|
||||
void cancel_animation_frame_impl(i32);
|
||||
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
|
||||
|
||||
i32 set_timeout(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||
i32 set_interval(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||
void clear_timeout(i32);
|
||||
void clear_interval(i32);
|
||||
i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||
i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
|
||||
void clear_timeout_impl(i32);
|
||||
void clear_interval_impl(i32);
|
||||
|
||||
void queue_microtask(Bindings::CallbackType& callback);
|
||||
void queue_microtask_impl(Bindings::CallbackType& callback);
|
||||
|
||||
int inner_width() const;
|
||||
int inner_height() const;
|
||||
|
@ -79,25 +73,20 @@ public:
|
|||
void did_call_location_reload(Badge<Bindings::LocationObject>);
|
||||
void did_call_location_replace(Badge<Bindings::LocationObject>, String url);
|
||||
|
||||
Bindings::WindowObject* wrapper() { return m_wrapper; }
|
||||
Bindings::WindowObject const* wrapper() const { return m_wrapper; }
|
||||
|
||||
void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
|
||||
|
||||
void deallocate_timer_id(Badge<Timer>, i32);
|
||||
|
||||
HighResolutionTime::Performance& performance() { return *m_performance; }
|
||||
HighResolutionTime::Performance& performance();
|
||||
|
||||
Crypto::Crypto& crypto() { return *m_crypto; }
|
||||
|
||||
CSS::Screen& screen() { return *m_screen; }
|
||||
|
||||
DOM::Event* current_event() { return m_current_event.cell(); }
|
||||
DOM::Event const* current_event() const { return m_current_event.cell(); }
|
||||
DOM::Event* current_event() { return m_current_event.ptr(); }
|
||||
DOM::Event const* current_event() const { return m_current_event.ptr(); }
|
||||
void set_current_event(DOM::Event* event);
|
||||
|
||||
CSS::CSSStyleDeclaration* get_computed_style(DOM::Element&) const;
|
||||
NonnullRefPtr<CSS::MediaQueryList> match_media(String);
|
||||
CSS::CSSStyleDeclaration* get_computed_style_impl(DOM::Element&) const;
|
||||
JS::NonnullGCPtr<CSS::MediaQueryList> match_media_impl(String);
|
||||
Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
|
||||
|
||||
float scroll_x() const;
|
||||
|
@ -110,28 +99,31 @@ public:
|
|||
int screen_x() const;
|
||||
int screen_y() const;
|
||||
|
||||
Selection::Selection* get_selection();
|
||||
Selection::Selection* get_selection_impl();
|
||||
|
||||
RefPtr<HTML::Storage> local_storage();
|
||||
RefPtr<HTML::Storage> session_storage();
|
||||
|
||||
Window* parent();
|
||||
|
||||
DOM::ExceptionOr<void> post_message(JS::Value, String const& target_origin);
|
||||
DOM::ExceptionOr<void> post_message_impl(JS::Value, String const& target_origin);
|
||||
|
||||
String name() const;
|
||||
void set_name(String const&);
|
||||
|
||||
void start_an_idle_period();
|
||||
|
||||
u32 request_idle_callback(Bindings::CallbackType& callback);
|
||||
void cancel_idle_callback(u32);
|
||||
u32 request_idle_callback_impl(Bindings::CallbackType& callback);
|
||||
void cancel_idle_callback_impl(u32);
|
||||
|
||||
AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }
|
||||
|
||||
private:
|
||||
Window();
|
||||
explicit Window(JS::Realm&);
|
||||
explicit Window(DOM::Document&);
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
// ^HTML::GlobalEventHandlers
|
||||
virtual DOM::EventTarget& global_event_handlers_to_event_target(FlyString const&) override { return *this; }
|
||||
|
@ -143,22 +135,21 @@ private:
|
|||
Yes,
|
||||
No,
|
||||
};
|
||||
i32 run_timer_initialization_steps(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
|
||||
i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
|
||||
|
||||
void invoke_idle_callbacks();
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
|
||||
WeakPtr<DOM::Document> m_associated_document;
|
||||
JS::GCPtr<DOM::Document> m_associated_document;
|
||||
|
||||
WeakPtr<Bindings::WindowObject> m_wrapper;
|
||||
JS::GCPtr<DOM::Event> m_current_event;
|
||||
|
||||
IDAllocator m_timer_id_allocator;
|
||||
HashMap<int, NonnullRefPtr<Timer>> m_timers;
|
||||
|
||||
NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
|
||||
JS::GCPtr<HighResolutionTime::Performance> m_performance;
|
||||
NonnullRefPtr<Crypto::Crypto> m_crypto;
|
||||
NonnullOwnPtr<CSS::Screen> m_screen;
|
||||
JS::Handle<DOM::Event> m_current_event;
|
||||
|
||||
AnimationFrameCallbackDriver m_animation_frame_callback_driver;
|
||||
|
||||
|
@ -168,8 +159,130 @@ private:
|
|||
NonnullRefPtrVector<IdleCallback> m_runnable_idle_callbacks;
|
||||
// https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
|
||||
u32 m_idle_callback_identifier = 0;
|
||||
|
||||
public:
|
||||
HTML::Origin origin() const;
|
||||
|
||||
Bindings::LocationObject* location_object() { return m_location_object; }
|
||||
Bindings::LocationObject const* location_object() const { return m_location_object; }
|
||||
|
||||
JS::Object* web_prototype(String const& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
|
||||
JS::NativeFunction* web_constructor(String const& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
|
||||
|
||||
template<typename T>
|
||||
JS::Object& ensure_web_prototype(String const& class_name)
|
||||
{
|
||||
auto it = m_prototypes.find(class_name);
|
||||
if (it != m_prototypes.end())
|
||||
return *it->value;
|
||||
auto& realm = shape().realm();
|
||||
auto* prototype = heap().allocate<T>(realm, realm);
|
||||
m_prototypes.set(class_name, prototype);
|
||||
return *prototype;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
JS::NativeFunction& ensure_web_constructor(String const& class_name)
|
||||
{
|
||||
auto it = m_constructors.find(class_name);
|
||||
if (it != m_constructors.end())
|
||||
return *it->value;
|
||||
auto& realm = shape().realm();
|
||||
auto* constructor = heap().allocate<T>(realm, realm);
|
||||
m_constructors.set(class_name, constructor);
|
||||
define_direct_property(class_name, JS::Value(constructor), JS::Attribute::Writable | JS::Attribute::Configurable);
|
||||
return *constructor;
|
||||
}
|
||||
|
||||
virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
|
||||
|
||||
Bindings::CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
|
||||
Bindings::CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }
|
||||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(top_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(document_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(location_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(location_setter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(name_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(name_setter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(performance_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(performance_setter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(history_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(screen_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(event_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(event_setter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(inner_width_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(inner_height_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(parent_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(scroll_x_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(scroll_y_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(scroll);
|
||||
JS_DECLARE_NATIVE_FUNCTION(scroll_by);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(screen_x_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(screen_y_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(screen_left_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(screen_top_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(post_message);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(local_storage_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(session_storage_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(origin_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(alert);
|
||||
JS_DECLARE_NATIVE_FUNCTION(confirm);
|
||||
JS_DECLARE_NATIVE_FUNCTION(prompt);
|
||||
JS_DECLARE_NATIVE_FUNCTION(set_interval);
|
||||
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
|
||||
JS_DECLARE_NATIVE_FUNCTION(clear_interval);
|
||||
JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
|
||||
JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
|
||||
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
|
||||
JS_DECLARE_NATIVE_FUNCTION(atob);
|
||||
JS_DECLARE_NATIVE_FUNCTION(btoa);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_computed_style);
|
||||
JS_DECLARE_NATIVE_FUNCTION(match_media);
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_selection);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(queue_microtask);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(request_idle_callback);
|
||||
JS_DECLARE_NATIVE_FUNCTION(cancel_idle_callback);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(crypto_getter);
|
||||
|
||||
#define __ENUMERATE(attribute, event_name) \
|
||||
JS_DECLARE_NATIVE_FUNCTION(attribute##_getter); \
|
||||
JS_DECLARE_NATIVE_FUNCTION(attribute##_setter);
|
||||
ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE);
|
||||
ENUMERATE_WINDOW_EVENT_HANDLERS(__ENUMERATE);
|
||||
#undef __ENUMERATE
|
||||
|
||||
Bindings::LocationObject* m_location_object { nullptr };
|
||||
|
||||
HashMap<String, JS::Object*> m_prototypes;
|
||||
HashMap<String, JS::NativeFunction*> m_constructors;
|
||||
|
||||
// [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
|
||||
Bindings::CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
|
||||
};
|
||||
|
||||
void run_animation_frame_callbacks(DOM::Document&, double now);
|
||||
|
||||
}
|
||||
|
||||
WRAPPER_HACK(Window, Web::HTML)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue