From 95ce5735ce298fb10f71eae836dabff101e6a641 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 19:50:29 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.event to IDL --- .../BindingsGenerator/IDLGenerators.cpp | 6 ++--- Userland/Libraries/LibWeb/HTML/Window.cpp | 25 +++++++------------ Userland/Libraries/LibWeb/HTML/Window.h | 5 ++-- Userland/Libraries/LibWeb/HTML/Window.idl | 3 +++ 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 30b98073ef..29fa652ad6 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -133,7 +133,7 @@ CppType idl_type_name_to_cpp_type(Type const& type, Interface const& interface) if (type.name() == "long" && !type.is_nullable()) return { .name = "i32", .sequence_storage_type = SequenceStorageType::Vector }; - if (type.name() == "any") + if (type.name() == "any" || type.name() == "undefined") return { .name = "JS::Value", .sequence_storage_type = SequenceStorageType::MarkedVector }; if (type.name() == "BufferSource") @@ -1600,10 +1600,10 @@ static void generate_wrap_statement(SourceGenerator& generator, DeprecatedString auto cpp_type = IDL::idl_type_name_to_cpp_type(current_union_type, interface); union_generator.set("current_type", cpp_type.name); union_generator.append(R"~~~( - [&vm, &realm](@current_type@ const& visited_union_value@recursion_depth@) -> JS::Value { + [&vm, &realm]([[maybe_unused]] @current_type@ const& visited_union_value@recursion_depth@) -> JS::Value { // These may be unused. (void)vm; - (void) realm; + (void)realm; )~~~"); // NOTE: While we are using const&, the underlying type for wrappable types in unions is (Nonnull)RefPtr, which are not references. diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 3e62021000..5db7bcd0ac 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -1101,9 +1101,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badgevalue.as_accessor(); location_accessor.set_setter(JS::NativeFunction::create(realm, location_setter, 1, "location", &realm, {}, "set"sv)); @@ -1332,6 +1329,15 @@ void Window::post_message(JS::Value message, String const&) }); } +// https://dom.spec.whatwg.org/#dom-window-event +Variant, JS::Value> Window::event() const +{ + // The event getter steps are to return this’s current event. + if (auto* current_event = this->current_event()) + return make_handle(const_cast(*current_event)); + return JS::js_undefined(); +} + static JS::ThrowCompletionOr make_timer_handler(JS::VM& vm, JS::Value handler) { if (handler.is_function()) @@ -1539,19 +1545,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::screen_setter) REPLACEABLE_PROPERTY_SETTER(Window, screen); } -JS_DEFINE_NATIVE_FUNCTION(Window::event_getter) -{ - auto* impl = TRY(impl_from(vm)); - if (!impl->current_event()) - return JS::js_undefined(); - return impl->current_event(); -} - -JS_DEFINE_NATIVE_FUNCTION(Window::event_setter) -{ - REPLACEABLE_PROPERTY_SETTER(Window, event); -} - JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 4cbd88ac51..e1bcbba0ce 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -167,6 +167,8 @@ public: void post_message(JS::Value message, String const&); + Variant, JS::Value> event() const; + private: explicit Window(JS::Realm&); @@ -235,9 +237,6 @@ private: JS_DECLARE_NATIVE_FUNCTION(screen_getter); JS_DECLARE_NATIVE_FUNCTION(screen_setter); - 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); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index a93357ba8c..6f210a67d4 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -36,6 +36,9 @@ interface Window : EventTarget { undefined postMessage(any message, USVString targetOrigin); // FIXME: undefined postMessage(any message, USVString targetOrigin, optional sequence transfer = []); // FIXME: undefined postMessage(any message, optional WindowPostMessageOptions options = {}); + + // https://dom.spec.whatwg.org/#interface-window-extensions + [Replaceable] readonly attribute (Event or undefined) event; // legacy }; Window includes GlobalEventHandlers; Window includes WindowEventHandlers;