1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-29 22:15:10 +00:00
serenity/Userland/Libraries/LibWeb/DOM/Timer.cpp
Timothy Flynn 18b9d02edd 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.
2022-03-04 19:51:26 +01:00

38 lines
817 B
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/Timer.h>
#include <LibWeb/DOM/Timer.h>
#include <LibWeb/DOM/Window.h>
namespace Web::DOM {
NonnullRefPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
{
return adopt_ref(*new Timer(window, milliseconds, move(callback), id));
}
Timer::Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
: m_window(window)
, m_id(id)
{
m_timer = Core::Timer::create_single_shot(milliseconds, [this, callback = move(callback)] {
NonnullRefPtr strong_timer { *this };
callback();
});
}
Timer::~Timer()
{
m_window.deallocate_timer_id({}, m_id);
}
void Timer::start()
{
m_timer->start();
}
}