1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:47:46 +00:00

LibWeb: Implement setTimeout/setInterval with ESO according to the spec

Our setInterval implementation currently crashes on DuckDuckGo when it's
invoked with a string argument. In this path, we were creating a native
function to evaluate and execute that string. That evaluation was always
returning a Completion, but NativeFunction expects ThrowCompletionOr.
The conversion from Completion to ThrowCompletionOr would fail a VERIFY
because that conversion is only valid if the Completion is an error; but
we would trigger this conversion even on success.

This change re-implements setTimeout & setInterval in direct accordance
with the spec. So we avoid making that NativeFunction altogether, and
DDG can progress past its invocation to the timer. With this change, we
also have other features we did not previously support, such as passing
any number of arguments to the timers. This does not implement handling
of nesting levels yet.
This commit is contained in:
Timothy Flynn 2022-03-04 10:41:12 -05:00 committed by Andreas Kling
parent 8156ec5da8
commit 18b9d02edd
6 changed files with 184 additions and 138 deletions

View file

@ -50,8 +50,8 @@ public:
i32 request_animation_frame(NonnullOwnPtr<Bindings::CallbackType> js_callback);
void cancel_animation_frame(i32);
i32 set_timeout(NonnullOwnPtr<Bindings::CallbackType> callback, i32);
i32 set_interval(NonnullOwnPtr<Bindings::CallbackType> callback, i32);
i32 set_timeout(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
i32 set_interval(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments);
void clear_timeout(i32);
void clear_interval(i32);
@ -69,9 +69,7 @@ public:
void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
i32 allocate_timer_id(Badge<Timer>);
void deallocate_timer_id(Badge<Timer>, i32);
void timer_did_fire(Badge<Timer>, Timer&);
HighResolutionTime::Performance& performance() { return *m_performance; }
@ -110,6 +108,12 @@ private:
// ^HTML::GlobalEventHandlers
virtual DOM::EventTarget& global_event_handlers_to_event_target() override { return *this; }
enum class Repeat {
Yes,
No,
};
i32 run_timer_initialization_steps(Bindings::TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
// https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
WeakPtr<Document> m_associated_document;