1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:17:44 +00:00

LibWeb: Port {set,clear}{Timeout,Interval} to IDL

This commit is contained in:
Timothy Flynn 2023-03-14 07:28:08 -04:00 committed by Tim Flynn
parent 6c5e79ce3a
commit 0aa4466ce9
3 changed files with 11 additions and 93 deletions

View file

@ -748,13 +748,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this));
// FIXME: These should be native accessors, not properties
u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable;
define_native_function(realm, "setInterval", set_interval, 1, attr);
define_native_function(realm, "setTimeout", set_timeout, 1, attr);
define_native_function(realm, "clearInterval", clear_interval, 1, attr);
define_native_function(realm, "clearTimeout", clear_timeout, 1, attr);
// https://webidl.spec.whatwg.org/#define-the-global-property-references
// 5. For every namespace namespace that is exposed in realm:
// 1. Let id be namespaces identifier.
@ -1366,83 +1359,6 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> Window::crypto()
return JS::NonnullGCPtr { *m_crypto };
}
static JS::ThrowCompletionOr<TimerHandler> make_timer_handler(JS::VM& vm, JS::Value handler)
{
if (handler.is_function())
return JS::make_handle(vm.heap().allocate_without_realm<WebIDL::CallbackType>(handler.as_function(), incumbent_settings_object()));
return TRY(handler.to_string(vm));
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
JS_DEFINE_NATIVE_FUNCTION(Window::set_timeout)
{
auto* impl = TRY(impl_from(vm));
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
auto handler = TRY(make_timer_handler(vm, vm.argument(0)));
i32 timeout = 0;
if (vm.argument_count() >= 2)
timeout = TRY(vm.argument(1).to_i32(vm));
JS::MarkedVector<JS::Value> arguments { vm.heap() };
for (size_t i = 2; i < vm.argument_count(); ++i)
arguments.append(vm.argument(i));
auto id = static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->set_timeout(move(handler), timeout, move(arguments));
return JS::Value(id);
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
JS_DEFINE_NATIVE_FUNCTION(Window::set_interval)
{
auto* impl = TRY(impl_from(vm));
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
auto handler = TRY(make_timer_handler(vm, vm.argument(0)));
i32 timeout = 0;
if (vm.argument_count() >= 2)
timeout = TRY(vm.argument(1).to_i32(vm));
JS::MarkedVector<JS::Value> arguments { vm.heap() };
for (size_t i = 2; i < vm.argument_count(); ++i)
arguments.append(vm.argument(i));
auto id = static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->set_interval(move(handler), timeout, move(arguments));
return JS::Value(id);
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-cleartimeout
JS_DEFINE_NATIVE_FUNCTION(Window::clear_timeout)
{
auto* impl = TRY(impl_from(vm));
i32 id = 0;
if (vm.argument_count())
id = TRY(vm.argument(0).to_i32(vm));
static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->clear_timeout(id);
return JS::js_undefined();
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-clearinterval
JS_DEFINE_NATIVE_FUNCTION(Window::clear_interval)
{
auto* impl = TRY(impl_from(vm));
i32 id = 0;
if (vm.argument_count())
id = TRY(vm.argument(0).to_i32(vm));
static_cast<WindowOrWorkerGlobalScopeMixin*>(impl)->clear_interval(id);
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
{

View file

@ -56,8 +56,12 @@ public:
using WindowOrWorkerGlobalScopeMixin::atob;
using WindowOrWorkerGlobalScopeMixin::btoa;
using WindowOrWorkerGlobalScopeMixin::clear_interval;
using WindowOrWorkerGlobalScopeMixin::clear_timeout;
using WindowOrWorkerGlobalScopeMixin::fetch;
using WindowOrWorkerGlobalScopeMixin::queue_microtask;
using WindowOrWorkerGlobalScopeMixin::set_interval;
using WindowOrWorkerGlobalScopeMixin::set_timeout;
using WindowOrWorkerGlobalScopeMixin::structured_clone;
// ^DOM::EventTarget
@ -231,11 +235,6 @@ private:
CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
JS_DECLARE_NATIVE_FUNCTION(location_setter);
JS_DECLARE_NATIVE_FUNCTION(set_interval);
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(clear_interval);
JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
};
void run_animation_frame_callbacks(DOM::Document&, double now);

View file

@ -5,6 +5,9 @@
// FIXME: Support VoidFunction in the IDL parser
callback VoidFunction = undefined ();
// https://html.spec.whatwg.org/#timerhandler
typedef (DOMString or Function) TimerHandler;
// https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
interface mixin WindowOrWorkerGlobalScope {
[Replaceable] readonly attribute USVString origin;
@ -18,10 +21,10 @@ interface mixin WindowOrWorkerGlobalScope {
ByteString atob(DOMString data);
// timers
// FIXME: long setTimeout(TimerHandler handler, optional long timeout = 0, any... arguments);
// FIXME: undefined clearTimeout(optional long id = 0);
// FIXME: long setInterval(TimerHandler handler, optional long timeout = 0, any... arguments);
// FIXME: undefined clearInterval(optional long id = 0);
long setTimeout(TimerHandler handler, optional long timeout = 0, any... arguments);
undefined clearTimeout(optional long id = 0);
long setInterval(TimerHandler handler, optional long timeout = 0, any... arguments);
undefined clearInterval(optional long id = 0);
// microtask queuing
undefined queueMicrotask(VoidFunction callback);