From a248ec63e31a3ff4079b40bb099d5ce9b92fbea1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 26 Sep 2021 14:36:20 +0200 Subject: LibWeb: Implement window.queueMicrotask(callback) This API allows authors to schedule a serialized JS callback that will get invoked at the next spec-allowed opportunity. --- .../Libraries/LibWeb/Bindings/WindowObject.cpp | 23 ++++++++++++++++++++++ Userland/Libraries/LibWeb/Bindings/WindowObject.h | 2 ++ 2 files changed, 25 insertions(+) (limited to 'Userland/Libraries/LibWeb/Bindings') diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 0be35f2cf6..cf9ae65202 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -80,6 +80,8 @@ void WindowObject::initialize_global_object() define_native_function("atob", atob, 1, attr); define_native_function("btoa", btoa, 1, attr); + define_native_function("queueMicrotask", queue_microtask, 1, attr); + define_native_function("getComputedStyle", get_computed_style, 1, attr); define_native_function("matchMedia", match_media, 1, attr); @@ -338,6 +340,27 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame) return JS::js_undefined(); } +JS_DEFINE_NATIVE_FUNCTION(WindowObject::queue_microtask) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + if (!vm.argument_count()) { + vm.throw_exception(global_object, JS::ErrorType::BadArgCountAtLeastOne, "queueMicrotask"); + return {}; + } + auto* callback_object = vm.argument(0).to_object(global_object); + if (!callback_object) + return {}; + if (!callback_object->is_function()) { + vm.throw_exception(global_object, JS::ErrorType::NotAFunctionNoParam); + return {}; + } + + impl->queue_microtask(static_cast(*callback_object)); + return JS::js_undefined(); +} + JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob) { auto* impl = impl_from(vm, global_object); diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index b38976d9b6..a148d95ee4 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -99,6 +99,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(get_computed_style); JS_DECLARE_NATIVE_FUNCTION(match_media); + JS_DECLARE_NATIVE_FUNCTION(queue_microtask); + #define __ENUMERATE(attribute, event_name) \ JS_DECLARE_NATIVE_FUNCTION(attribute##_getter); \ JS_DECLARE_NATIVE_FUNCTION(attribute##_setter); -- cgit v1.2.3