diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-26 14:36:20 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-26 14:39:14 +0200 |
commit | a248ec63e31a3ff4079b40bb099d5ce9b92fbea1 (patch) | |
tree | 48bdf368f4e1db5661f14f2aeeb1eda46fd1dca4 /Userland/Libraries/LibWeb/Bindings | |
parent | 831fdcaabc95817d4bff9c8bd8a6741227a0b371 (diff) | |
download | serenity-a248ec63e31a3ff4079b40bb099d5ce9b92fbea1.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibWeb/Bindings')
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/WindowObject.cpp | 23 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/WindowObject.h | 2 |
2 files changed, 25 insertions, 0 deletions
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<JS::TypeError>(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<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam); + return {}; + } + + impl->queue_microtask(static_cast<JS::FunctionObject&>(*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); |