From 12ddeeb9cec4fdafbc8a96f390f0cbc9c134ff71 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 29 Nov 2022 20:45:35 +0100 Subject: [PATCH] LibWeb: Add PlatformObject::implements_interface(String) This can be used to ask a PlatformObject if it implements a given IDL interface. It's implemented by a chain of virtual overrides that get inserted for each subclass by the WEB_PLATFORM_OBJECT macro. --- .../Libraries/LibWeb/Bindings/PlatformObject.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Bindings/PlatformObject.h b/Userland/Libraries/LibWeb/Bindings/PlatformObject.h index c3fd70aaf3..8f4635aa05 100644 --- a/Userland/Libraries/LibWeb/Bindings/PlatformObject.h +++ b/Userland/Libraries/LibWeb/Bindings/PlatformObject.h @@ -13,8 +13,14 @@ namespace Web::Bindings { -#define WEB_PLATFORM_OBJECT(class_, base_class) \ - JS_OBJECT(class_, base_class) +#define WEB_PLATFORM_OBJECT(class_, base_class) \ + JS_OBJECT(class_, base_class) \ + virtual bool implements_interface(String const& interface) const override \ + { \ + if (interface == #class_) \ + return true; \ + return Base::implements_interface(interface); \ + } // https://webidl.spec.whatwg.org/#dfn-platform-object class PlatformObject @@ -30,6 +36,10 @@ public: // FIXME: This should return a type that works in both window and worker contexts. HTML::Window& global_object() const; + // https://webidl.spec.whatwg.org/#implements + // This is implemented by overrides that get generated by the WEB_PLATFORM_OBJECT macro. + [[nodiscard]] virtual bool implements_interface(String const&) const { return false; } + protected: explicit PlatformObject(JS::Realm&); explicit PlatformObject(JS::Object& prototype);