LibWeb: Use HeapFunction for Platform::Timer

This commit is contained in:
Shannon Booth 2024-10-30 21:42:05 +13:00 committed by Alexander Kalenik
parent ede3c91688
commit de1a805898
Notes: github-actions[bot] 2024-10-30 19:57:21 +00:00
8 changed files with 28 additions and 18 deletions

View file

@ -63,7 +63,7 @@ void ImageStyleValue::load_any_resources(DOM::Document& document)
if (image_data->is_animated() && image_data->frame_count() > 1) {
m_timer = Platform::Timer::create(m_document->heap());
m_timer->set_interval(image_data->frame_duration(0));
m_timer->on_timeout = [this] { animate(); };
m_timer->on_timeout = JS::create_heap_function(m_document->heap(), [this] { animate(); });
m_timer->start();
}
},

View file

@ -54,9 +54,9 @@ void EventLoop::visit_edges(Visitor& visitor)
void EventLoop::schedule()
{
if (!m_system_event_loop_timer) {
m_system_event_loop_timer = Platform::Timer::create_single_shot(heap(), 0, [this] {
m_system_event_loop_timer = Platform::Timer::create_single_shot(heap(), 0, JS::create_heap_function(heap(), [this] {
process();
});
}));
}
if (!m_system_event_loop_timer->is_active())

View file

@ -415,12 +415,12 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback
if (timeout.has_value() && timeout.value() > 0) {
auto timer = Platform::Timer::create_single_shot(m_heap, timeout.value(), nullptr);
timer->on_timeout = [timer, protocol_request, timeout_callback = move(timeout_callback)] {
timer->on_timeout = JS::create_heap_function(m_heap, [timer = JS::make_handle(timer), protocol_request, timeout_callback = move(timeout_callback)] {
(void)timer;
protocol_request->stop();
if (timeout_callback)
timeout_callback();
};
});
timer->start();
}

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/HeapFunction.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/Timer.h>
@ -11,12 +12,18 @@ namespace Web::Platform {
Timer::~Timer() = default;
void Timer::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(on_timeout);
}
JS::NonnullGCPtr<Timer> Timer::create(JS::Heap& heap)
{
return EventLoopPlugin::the().create_timer(heap);
}
JS::NonnullGCPtr<Timer> Timer::create_repeating(JS::Heap& heap, int interval_ms, JS::SafeFunction<void()>&& timeout_handler)
JS::NonnullGCPtr<Timer> Timer::create_repeating(JS::Heap& heap, int interval_ms, JS::GCPtr<JS::HeapFunction<void()>> timeout_handler)
{
auto timer = EventLoopPlugin::the().create_timer(heap);
timer->set_single_shot(false);
@ -25,7 +32,7 @@ JS::NonnullGCPtr<Timer> Timer::create_repeating(JS::Heap& heap, int interval_ms,
return timer;
}
JS::NonnullGCPtr<Timer> Timer::create_single_shot(JS::Heap& heap, int interval_ms, JS::SafeFunction<void()>&& timeout_handler)
JS::NonnullGCPtr<Timer> Timer::create_single_shot(JS::Heap& heap, int interval_ms, JS::GCPtr<JS::HeapFunction<void()>> timeout_handler)
{
auto timer = EventLoopPlugin::the().create_timer(heap);
timer->set_single_shot(true);

View file

@ -17,8 +17,8 @@ class Timer : public JS::Cell {
public:
static JS::NonnullGCPtr<Timer> create(JS::Heap&);
static JS::NonnullGCPtr<Timer> create_repeating(JS::Heap&, int interval_ms, JS::SafeFunction<void()>&& timeout_handler);
static JS::NonnullGCPtr<Timer> create_single_shot(JS::Heap&, int interval_ms, JS::SafeFunction<void()>&& timeout_handler);
static JS::NonnullGCPtr<Timer> create_repeating(JS::Heap&, int interval_ms, JS::GCPtr<JS::HeapFunction<void()>> timeout_handler);
static JS::NonnullGCPtr<Timer> create_single_shot(JS::Heap&, int interval_ms, JS::GCPtr<JS::HeapFunction<void()>> timeout_handler);
virtual ~Timer();
@ -37,7 +37,10 @@ public:
virtual bool is_single_shot() const = 0;
virtual void set_single_shot(bool) = 0;
JS::SafeFunction<void()> on_timeout;
JS::GCPtr<JS::HeapFunction<void()>> on_timeout;
protected:
virtual void visit_edges(JS::Cell::Visitor&) override;
};
}

View file

@ -5,9 +5,9 @@
*/
#include "TimerSerenity.h"
#include <AK/NonnullRefPtr.h>
#include <LibCore/Timer.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Heap/HeapFunction.h>
namespace Web::Platform {
@ -21,7 +21,7 @@ TimerSerenity::TimerSerenity()
{
m_timer->on_timeout = [this] {
if (on_timeout)
on_timeout();
on_timeout->function()();
};
}

View file

@ -879,14 +879,14 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
auto timer = Platform::Timer::create_single_shot(heap(), m_timeout, nullptr);
// NOTE: `timer` is kept alive by capturing into the lambda for the GC to see
// NOTE: `this` and `request` is kept alive by Platform::Timer using JS::SafeFunction.
timer->on_timeout = [this, request, timer]() {
// NOTE: `this` and `request` is kept alive by Platform::Timer using a Handle.
timer->on_timeout = JS::create_heap_function(heap(), [this, request, timer = JS::make_handle(timer)]() {
(void)timer;
if (!request->done()) {
m_timed_out = true;
m_fetch_controller->terminate();
}
};
});
timer->start();
}
@ -933,10 +933,10 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(Optional<DocumentOrXMLHttpRequest
auto timer = Platform::Timer::create_single_shot(heap(), m_timeout, nullptr);
// NOTE: `timer` is kept alive by capturing into the lambda for the GC to see
timer->on_timeout = [timer, &did_time_out]() {
timer->on_timeout = JS::create_heap_function(heap(), [timer = JS::make_handle(timer), &did_time_out]() {
(void)timer;
did_time_out = true;
};
});
timer->start();
}

View file

@ -58,7 +58,7 @@ ConnectionFromClient::ConnectionFromClient(JS::Heap& heap, IPC::Transport transp
, m_heap(heap)
, m_page_host(PageHost::create(*this))
{
m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(m_heap, 0, [this] { process_next_input_event(); });
m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(m_heap, 0, JS::create_heap_function(heap, [this] { process_next_input_event(); }));
}
ConnectionFromClient::~ConnectionFromClient() = default;