1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibWeb: Use JS::HeapFunction for callbacks in FetchController

If a function that captures a GC-allocated object is owned by another
GC-allocated object, it is more preferable to use JS::HeapFunction.
This is because JS::HeapFunction is visited, unlike introducing a new
heap root as JS::SafeFunction does.
This commit is contained in:
Aliaksandr Kalenik 2023-09-25 18:04:10 +02:00 committed by Andreas Kling
parent df86e52d75
commit baf37af09c
2 changed files with 22 additions and 8 deletions

View file

@ -24,27 +24,39 @@ void FetchController::visit_edges(JS::Cell::Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
visitor.visit(m_full_timing_info); visitor.visit(m_full_timing_info);
visitor.visit(m_report_timing_steps);
visitor.visit(m_next_manual_redirect_steps);
visitor.visit(m_fetch_params); visitor.visit(m_fetch_params);
} }
void FetchController::set_report_timing_steps(Function<void(JS::Object const&)> report_timing_steps)
{
m_report_timing_steps = JS::create_heap_function(vm().heap(), move(report_timing_steps));
}
void FetchController::set_next_manual_redirect_steps(Function<void()> next_manual_redirect_steps)
{
m_next_manual_redirect_steps = JS::create_heap_function(vm().heap(), move(next_manual_redirect_steps));
}
// https://fetch.spec.whatwg.org/#finalize-and-report-timing // https://fetch.spec.whatwg.org/#finalize-and-report-timing
void FetchController::report_timing(JS::Object const& global) const void FetchController::report_timing(JS::Object const& global) const
{ {
// 1. Assert: thiss report timing steps is not null. // 1. Assert: thiss report timing steps is not null.
VERIFY(m_report_timing_steps.has_value()); VERIFY(m_report_timing_steps);
// 2. Call thiss report timing steps with global. // 2. Call thiss report timing steps with global.
(*m_report_timing_steps)(global); m_report_timing_steps->function()(global);
} }
// https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect // https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect
void FetchController::process_next_manual_redirect() const void FetchController::process_next_manual_redirect() const
{ {
// 1. Assert: controllers next manual redirect steps are not null. // 1. Assert: controllers next manual redirect steps are not null.
VERIFY(m_next_manual_redirect_steps.has_value()); VERIFY(m_next_manual_redirect_steps);
// 2. Call controllers next manual redirect steps. // 2. Call controllers next manual redirect steps.
(*m_next_manual_redirect_steps)(); m_next_manual_redirect_steps->function()();
} }
// https://fetch.spec.whatwg.org/#extract-full-timing-info // https://fetch.spec.whatwg.org/#extract-full-timing-info

View file

@ -10,6 +10,8 @@
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h> #include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/GCPtr.h> #include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/HeapFunction.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/SafeFunction.h> #include <LibJS/SafeFunction.h>
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h> #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
@ -30,8 +32,8 @@ public:
[[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&); [[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&);
void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; } void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; }
void set_report_timing_steps(JS::SafeFunction<void(JS::Object const&)> report_timing_steps) { m_report_timing_steps = move(report_timing_steps); } void set_report_timing_steps(Function<void(JS::Object const&)> report_timing_steps);
void set_next_manual_redirect_steps(JS::SafeFunction<void()> next_manual_redirect_steps) { m_next_manual_redirect_steps = move(next_manual_redirect_steps); } void set_next_manual_redirect_steps(Function<void()> next_manual_redirect_steps);
[[nodiscard]] State state() const { return m_state; } [[nodiscard]] State state() const { return m_state; }
@ -63,7 +65,7 @@ private:
// https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
// report timing steps (default null) // report timing steps (default null)
// Null or an algorithm accepting a global object. // Null or an algorithm accepting a global object.
Optional<JS::SafeFunction<void(JS::Object const&)>> m_report_timing_steps; JS::GCPtr<JS::HeapFunction<void(JS::Object const&)>> m_report_timing_steps;
// https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
// FIXME: serialized abort reason (default null) // FIXME: serialized abort reason (default null)
@ -72,7 +74,7 @@ private:
// https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps // https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
// next manual redirect steps (default null) // next manual redirect steps (default null)
// Null or an algorithm accepting nothing. // Null or an algorithm accepting nothing.
Optional<JS::SafeFunction<void()>> m_next_manual_redirect_steps; JS::GCPtr<JS::HeapFunction<void()>> m_next_manual_redirect_steps;
JS::GCPtr<FetchParams> m_fetch_params; JS::GCPtr<FetchParams> m_fetch_params;
}; };