1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +00:00

LibWeb: Convert DOM::AbortSignal to use JS::SafeFunction

This protects the captured GC pointers automatically instead of
manually.
This commit is contained in:
Luke Wilde 2022-10-26 18:09:24 +01:00 committed by Linus Groh
parent 67e396d931
commit 07e3bb729d
3 changed files with 6 additions and 5 deletions

View file

@ -24,7 +24,7 @@ AbortSignal::AbortSignal(JS::Realm& realm)
} }
// https://dom.spec.whatwg.org/#abortsignal-add // https://dom.spec.whatwg.org/#abortsignal-add
void AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm) void AbortSignal::add_abort_algorithm(JS::SafeFunction<void()> abort_algorithm)
{ {
// 1. If signal is aborted, then return. // 1. If signal is aborted, then return.
if (aborted()) if (aborted())

View file

@ -22,7 +22,7 @@ public:
virtual ~AbortSignal() override = default; virtual ~AbortSignal() override = default;
void add_abort_algorithm(Function<void()>); void add_abort_algorithm(JS::SafeFunction<void()>);
// https://dom.spec.whatwg.org/#dom-abortsignal-aborted // https://dom.spec.whatwg.org/#dom-abortsignal-aborted
// An AbortSignal object is aborted when its abort reason is not undefined. // An AbortSignal object is aborted when its abort reason is not undefined.
@ -49,7 +49,7 @@ private:
// https://dom.spec.whatwg.org/#abortsignal-abort-algorithms // https://dom.spec.whatwg.org/#abortsignal-abort-algorithms
// FIXME: This should be a set. // FIXME: This should be a set.
Vector<Function<void()>> m_abort_algorithms; Vector<JS::SafeFunction<void()>> m_abort_algorithms;
}; };
} }

View file

@ -170,9 +170,10 @@ void EventTarget::add_an_event_listener(DOMEventListener& listener)
// 5. If listeners signal is not null, then add the following abort steps to it: // 5. If listeners signal is not null, then add the following abort steps to it:
if (listener.signal) { if (listener.signal) {
listener.signal->add_abort_algorithm([strong_event_target = JS::make_handle(*this), listener = JS::make_handle(&listener)]() mutable { // NOTE: `this` and `listener` are protected by AbortSignal using JS::SafeFunction.
listener.signal->add_abort_algorithm([this, &listener]() mutable {
// 1. Remove an event listener with eventTarget and listener. // 1. Remove an event listener with eventTarget and listener.
strong_event_target->remove_an_event_listener(*listener); remove_an_event_listener(listener);
}); });
} }
} }