LibWeb: Make HTML::Timer GC-allocated

These are the timers used internally by setTimeout() and setInterval().
This commit is contained in:
Andreas Kling 2022-09-01 12:21:47 +02:00
parent c569c88e63
commit c7ac82ec33
Notes: sideshowbarker 2024-07-17 09:47:09 +09:00
4 changed files with 26 additions and 11 deletions

View File

@ -10,24 +10,29 @@
namespace Web::HTML {
NonnullRefPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
JS::NonnullGCPtr<Timer> Timer::create(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
{
return adopt_ref(*new Timer(window, milliseconds, move(callback), id));
return *window.heap().allocate_without_realm<Timer>(window, milliseconds, move(callback), id);
}
Timer::Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id)
: m_window(window)
, m_callback(move(callback))
, m_id(id)
{
m_timer = Core::Timer::create_single_shot(milliseconds, [this, callback = move(callback)] {
NonnullRefPtr strong_timer { *this };
callback();
m_timer = Core::Timer::create_single_shot(milliseconds, [this] {
m_callback();
});
}
void Timer::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_window.ptr());
}
Timer::~Timer()
{
m_window.deallocate_timer_id({}, m_id);
}
void Timer::start()

View File

@ -8,23 +8,31 @@
#include <AK/Forward.h>
#include <AK/Function.h>
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
class Timer final : public RefCounted<Timer> {
class Timer final : public JS::Cell {
JS_CELL(Timer, JS::Cell);
public:
static NonnullRefPtr<Timer> create(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
~Timer();
static JS::NonnullGCPtr<Timer> create(Window&, i32 milliseconds, Function<void()> callback, i32 id);
virtual ~Timer() override;
void start();
private:
Timer(Window& window, i32 milliseconds, Function<void()> callback, i32 id);
virtual void visit_edges(Cell::Visitor&) override;
RefPtr<Core::Timer> m_timer;
Window& m_window;
JS::NonnullGCPtr<Window> m_window;
Function<void()> m_callback;
i32 m_id { 0 };
};

View File

@ -118,6 +118,8 @@ void Window::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(it.value);
for (auto& it : m_constructors)
visitor.visit(it.value);
for (auto& it : m_timers)
visitor.visit(it.value.ptr());
}
Window::~Window() = default;

View File

@ -144,7 +144,7 @@ private:
JS::GCPtr<DOM::Event> m_current_event;
IDAllocator m_timer_id_allocator;
HashMap<int, NonnullRefPtr<Timer>> m_timers;
HashMap<int, JS::NonnullGCPtr<Timer>> m_timers;
JS::GCPtr<HighResolutionTime::Performance> m_performance;
NonnullRefPtr<Crypto::Crypto> m_crypto;