Browse Source

LibWeb/HTML: Port Window.queueMicrotask() to IDL

Linus Groh 2 years ago
parent
commit
351e5ca917

+ 0 - 29
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -705,18 +705,6 @@ void Window::fire_a_page_transition_event(DeprecatedFlyString const& event_name,
     dispatch_event(event);
 }
 
-// https://html.spec.whatwg.org/#dom-queuemicrotask
-void Window::queue_microtask_impl(WebIDL::CallbackType& callback)
-{
-    // The queueMicrotask(callback) method must queue a microtask to invoke callback,
-    HTML::queue_a_microtask(&associated_document(), [this, &callback] {
-        auto result = WebIDL::invoke_callback(callback, {});
-        // and if callback throws an exception, report the exception.
-        if (result.is_error())
-            HTML::report_exception(result, realm());
-    });
-}
-
 // https://html.spec.whatwg.org/multipage/webstorage.html#dom-localstorage
 JS::NonnullGCPtr<HTML::Storage> Window::local_storage()
 {
@@ -883,8 +871,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
     define_native_function(realm, "clearInterval", clear_interval, 1, attr);
     define_native_function(realm, "clearTimeout", clear_timeout, 1, attr);
 
-    define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr);
-
     define_direct_property("CSS", MUST_OR_THROW_OOM(heap().allocate<Bindings::CSSNamespace>(realm, realm)), 0);
 
     // FIXME: Implement codegen for readonly properties with [PutForwards]
@@ -1564,21 +1550,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::clear_interval)
     return JS::js_undefined();
 }
 
-JS_DEFINE_NATIVE_FUNCTION(Window::queue_microtask)
-{
-    auto* impl = TRY(impl_from(vm));
-    if (!vm.argument_count())
-        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "queueMicrotask");
-    auto* callback_object = TRY(vm.argument(0).to_object(vm));
-    if (!callback_object->is_function())
-        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
-
-    auto callback = vm.heap().allocate_without_realm<WebIDL::CallbackType>(*callback_object, HTML::incumbent_settings_object());
-
-    impl->queue_microtask_impl(*callback);
-    return JS::js_undefined();
-}
-
 // https://html.spec.whatwg.org/multipage/window-object.html#number-of-document-tree-child-browsing-contexts
 size_t Window::document_tree_child_browsing_context_count() const
 {

+ 1 - 4
Userland/Libraries/LibWeb/HTML/Window.h

@@ -61,6 +61,7 @@ public:
     using WindowOrWorkerGlobalScopeMixin::atob;
     using WindowOrWorkerGlobalScopeMixin::btoa;
     using WindowOrWorkerGlobalScopeMixin::fetch;
+    using WindowOrWorkerGlobalScopeMixin::queue_microtask;
     using WindowOrWorkerGlobalScopeMixin::structured_clone;
 
     // ^DOM::EventTarget
@@ -97,8 +98,6 @@ public:
     void clear_timeout_impl(i32);
     void clear_interval_impl(i32);
 
-    void queue_microtask_impl(WebIDL::CallbackType& callback);
-
     void did_set_location_href(Badge<HTML::Location>, AK::URL const& new_href);
     void did_call_location_reload(Badge<HTML::Location>);
     void did_call_location_replace(Badge<HTML::Location>, DeprecatedString url);
@@ -255,8 +254,6 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(clear_interval);
     JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
 
-    JS_DECLARE_NATIVE_FUNCTION(queue_microtask);
-
     HTML::Location* m_location { nullptr };
 
     // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap

+ 22 - 0
Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp

@@ -12,10 +12,14 @@
 #include <LibTextCodec/Decoder.h>
 #include <LibWeb/Fetch/FetchMethod.h>
 #include <LibWeb/Forward.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
 #include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
 #include <LibWeb/HTML/StructuredSerialize.h>
+#include <LibWeb/HTML/Window.h>
 #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
 #include <LibWeb/Infra/Base64.h>
+#include <LibWeb/WebIDL/AbstractOperations.h>
 #include <LibWeb/WebIDL/DOMException.h>
 #include <LibWeb/WebIDL/ExceptionOr.h>
 
@@ -86,6 +90,24 @@ WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::atob(String const& d
     return TRY_OR_THROW_OOM(vm, decoder->to_utf8(decoded_data.value()));
 }
 
+// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
+void WindowOrWorkerGlobalScopeMixin::queue_microtask(WebIDL::CallbackType& callback)
+{
+    auto& vm = this_impl().vm();
+    auto& realm = *vm.current_realm();
+
+    JS::GCPtr<DOM::Document> document;
+    if (is<Window>(this_impl()))
+        document = &static_cast<Window&>(this_impl()).associated_document();
+
+    // The queueMicrotask(callback) method must queue a microtask to invoke callback, and if callback throws an exception, report the exception.
+    HTML::queue_a_microtask(document, [&callback, &realm] {
+        auto result = WebIDL::invoke_callback(callback, {});
+        if (result.is_error())
+            HTML::report_exception(result, realm);
+    });
+}
+
 // https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
 WebIDL::ExceptionOr<JS::Value> WindowOrWorkerGlobalScopeMixin::structured_clone(JS::Value value, StructuredSerializeOptions const& options) const
 {

+ 1 - 0
Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h

@@ -28,6 +28,7 @@ public:
     bool cross_origin_isolated() const;
     WebIDL::ExceptionOr<String> btoa(String const& data) const;
     WebIDL::ExceptionOr<String> atob(String const& data) const;
+    void queue_microtask(WebIDL::CallbackType&);
     WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Value, StructuredSerializeOptions const&) const;
     JS::NonnullGCPtr<JS::Promise> fetch(Fetch::RequestInfo const&, Fetch::RequestInit const&) const;
 };

+ 4 - 1
Userland/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl

@@ -2,6 +2,9 @@
 #import <Fetch/Response.idl>
 #import <HTML/MessagePort.idl>
 
+// FIXME: Support VoidFunction in the IDL parser
+callback VoidFunction = undefined ();
+
 // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
 interface mixin WindowOrWorkerGlobalScope {
     [Replaceable] readonly attribute USVString origin;
@@ -21,7 +24,7 @@ interface mixin WindowOrWorkerGlobalScope {
     // FIXME: undefined clearInterval(optional long id = 0);
 
     // microtask queuing
-    // FIXME: undefined queueMicrotask(VoidFunction callback);
+    undefined queueMicrotask(VoidFunction callback);
 
     // ImageBitmap
     // FIXME: Promise<ImageBitmap> createImageBitmap(ImageBitmapSource image, optional ImageBitmapOptions options = {});