From d8188c9f14165fa2f2a60ff1bfd569ac3dc61bbb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Apr 2025 10:31:28 +0200 Subject: [PATCH] LibJS+LibWeb: Add JS::Object::fast_is helpers for some LibWeb types These are slightly unfortunate as we're crossing the library boundary, but there's precedent with Object::is_dom_node(), and these are just knocking down a few more items that were showing up in profiles. --- Libraries/LibJS/Runtime/Object.h | 5 +++++ Libraries/LibWeb/DOM/Event.h | 5 +++++ Libraries/LibWeb/HTML/Location.h | 5 +++++ Libraries/LibWeb/HTML/Window.h | 5 +++++ Libraries/LibWeb/HTML/WindowProxy.h | 4 ++++ 5 files changed, 24 insertions(+) diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index ba41d0a351f..fb8b03dd03b 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -190,6 +190,11 @@ public: void define_native_accessor(Realm&, PropertyKey const&, ESCAPING Function(VM&)> getter, ESCAPING Function(VM&)> setter, PropertyAttributes attributes); virtual bool is_dom_node() const { return false; } + virtual bool is_dom_event() const { return false; } + virtual bool is_html_window() const { return false; } + virtual bool is_html_window_proxy() const { return false; } + virtual bool is_html_location() const { return false; } + virtual bool is_function() const { return false; } virtual bool is_promise() const { return false; } virtual bool is_error() const { return false; } diff --git a/Libraries/LibWeb/DOM/Event.h b/Libraries/LibWeb/DOM/Event.h index 20a42718274..60a16076e88 100644 --- a/Libraries/LibWeb/DOM/Event.h +++ b/Libraries/LibWeb/DOM/Event.h @@ -159,6 +159,8 @@ protected: virtual void visit_edges(Visitor&) override; private: + virtual bool is_dom_event() const final { return true; } + FlyString m_type; GC::Ptr m_target; GC::Ptr m_related_target; @@ -188,3 +190,6 @@ private: }; } + +template<> +inline bool JS::Object::fast_is() const { return is_dom_event(); } diff --git a/Libraries/LibWeb/HTML/Location.h b/Libraries/LibWeb/HTML/Location.h index 44a4f486965..07a2ebcfd66 100644 --- a/Libraries/LibWeb/HTML/Location.h +++ b/Libraries/LibWeb/HTML/Location.h @@ -71,6 +71,8 @@ public: private: explicit Location(JS::Realm&); + virtual bool is_html_location() const override { return true; } + virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; @@ -86,3 +88,6 @@ private: }; } + +template<> +inline bool JS::Object::fast_is() const { return is_html_location(); } diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.h index 6a2429ec349..e0b76d7f00e 100644 --- a/Libraries/LibWeb/HTML/Window.h +++ b/Libraries/LibWeb/HTML/Window.h @@ -274,6 +274,8 @@ private: virtual void visit_edges(Cell::Visitor&) override; virtual void finalize() override; + virtual bool is_html_window() const override { return true; } + // ^HTML::GlobalEventHandlers virtual GC::Ptr global_event_handlers_to_event_target(FlyString const&) override { return *this; } @@ -356,3 +358,6 @@ private: void run_animation_frame_callbacks(DOM::Document&, double now); } + +template<> +inline bool JS::Object::fast_is() const { return is_html_window(); } diff --git a/Libraries/LibWeb/HTML/WindowProxy.h b/Libraries/LibWeb/HTML/WindowProxy.h index 562f6b9404a..6d813c28e4a 100644 --- a/Libraries/LibWeb/HTML/WindowProxy.h +++ b/Libraries/LibWeb/HTML/WindowProxy.h @@ -40,6 +40,7 @@ public: private: explicit WindowProxy(JS::Realm&); + virtual bool is_html_window_proxy() const override { return true; } virtual void visit_edges(JS::Cell::Visitor&) override; // [[Window]], https://html.spec.whatwg.org/multipage/window-object.html#concept-windowproxy-window @@ -47,3 +48,6 @@ private: }; } + +template<> +inline bool JS::Object::fast_is() const { return is_html_window_proxy(); }