From 7b76438d579959edd800de14cb30c57eadb14669 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Tue, 15 Oct 2024 09:34:35 +0200 Subject: [PATCH] LibWeb: Accept JS::HeapFunction when queuing a media element task This opens up the possibility of easier memory management in future changes. --- .../LibWeb/WebAudio/AudioContext.cpp | 28 +++++++++---------- .../LibWeb/WebAudio/BaseAudioContext.cpp | 6 ++-- .../LibWeb/WebAudio/BaseAudioContext.h | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibWeb/WebAudio/AudioContext.cpp b/Userland/Libraries/LibWeb/WebAudio/AudioContext.cpp index 603efc2cc74..ed83ca8832f 100644 --- a/Userland/Libraries/LibWeb/WebAudio/AudioContext.cpp +++ b/Userland/Libraries/LibWeb/WebAudio/AudioContext.cpp @@ -71,13 +71,13 @@ AudioContext::AudioContext(JS::Realm& realm, AudioContextOptions const& context_ BaseAudioContext::set_rendering_state(Bindings::AudioContextState::Running); // 5.3: queue a media element task to execute the following steps: - queue_a_media_element_task([&realm, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, this]() { // 5.3.1: Set the state attribute of the AudioContext to "running". BaseAudioContext::set_control_state(Bindings::AudioContextState::Running); // 5.3.2: queue a media element task to fire an event named statechange at the AudioContext. this->dispatch_event(DOM::Event::create(realm, HTML::EventNames::statechange)); - }); + })); } } @@ -146,7 +146,7 @@ WebIDL::ExceptionOr> AudioContext::resume() // 7.3: Start rendering the audio graph. if (!start_rendering_audio_graph()) { // 7.4: In case of failure, queue a media element task to execute the following steps: - queue_a_media_element_task([&realm, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, this]() { // 7.4.1: Reject all promises from [[pending resume promises]] in order, then clear [[pending resume promises]]. for (auto const& promise : m_pending_resume_promises) { WebIDL::reject_promise(realm, promise, JS::js_null()); @@ -154,11 +154,11 @@ WebIDL::ExceptionOr> AudioContext::resume() m_pending_resume_promises.clear(); // FIXME: 7.4.2: Additionally, remove those promises from [[pending promises]]. - }); + })); } // 7.5: queue a media element task to execute the following steps: - queue_a_media_element_task([&realm, promise, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, promise, this]() { // 7.5.1: Resolve all promises from [[pending resume promises]] in order. for (auto const& pending_resume_promise : m_pending_resume_promises) { *pending_resume_promise->resolve(); @@ -178,11 +178,11 @@ WebIDL::ExceptionOr> AudioContext::resume() set_control_state(Bindings::AudioContextState::Running); // 7.5.4.2: queue a media element task to fire an event named statechange at the AudioContext. - queue_a_media_element_task([&realm, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, this]() { this->dispatch_event(DOM::Event::create(realm, HTML::EventNames::statechange)); - }); + })); } - }); + })); // 8. Return promise. return JS::NonnullGCPtr { verify_cast(*promise->promise()) }; @@ -226,7 +226,7 @@ WebIDL::ExceptionOr> AudioContext::suspend() set_rendering_state(Bindings::AudioContextState::Suspended); // 7.3: queue a media element task to execute the following steps: - queue_a_media_element_task([&realm, promise, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, promise, this]() { // 7.3.1: Resolve promise. *promise->resolve(); @@ -236,11 +236,11 @@ WebIDL::ExceptionOr> AudioContext::suspend() set_control_state(Bindings::AudioContextState::Suspended); // 7.3.2.2: queue a media element task to fire an event named statechange at the AudioContext. - queue_a_media_element_task([&realm, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, this]() { this->dispatch_event(DOM::Event::create(realm, HTML::EventNames::statechange)); - }); + })); } - }); + })); // 8. Return promise. return JS::NonnullGCPtr { verify_cast(*promise->promise()) }; @@ -279,7 +279,7 @@ WebIDL::ExceptionOr> AudioContext::close() // FIXME: 5.3: If this control message is being run in a reaction to the document being unloaded, abort this algorithm. // 5.4: queue a media element task to execute the following steps: - queue_a_media_element_task([&realm, promise, this]() { + queue_a_media_element_task(JS::create_heap_function(heap(), [&realm, promise, this]() { // 5.4.1: Resolve promise. *promise->resolve(); @@ -292,7 +292,7 @@ WebIDL::ExceptionOr> AudioContext::close() // 5.4.2.2: queue a media element task to fire an event named statechange at the AudioContext. // FIXME: Attempting to queue another task in here causes an assertion fail at Vector.h:148 this->dispatch_event(DOM::Event::create(realm, HTML::EventNames::statechange)); - }); + })); // 6. Return promise return JS::NonnullGCPtr { verify_cast(*promise->promise()) }; diff --git a/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp b/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp index 330f9ecddc7..5ddd954efe2 100644 --- a/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp +++ b/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp @@ -114,10 +114,10 @@ WebIDL::ExceptionOr BaseAudioContext::verify_audio_options_inside_nominal_ return {}; } -void BaseAudioContext::queue_a_media_element_task(Function steps) +void BaseAudioContext::queue_a_media_element_task(JS::NonnullGCPtr> steps) { - auto task = HTML::Task::create(vm(), m_media_element_event_task_source.source, HTML::current_settings_object().responsible_document(), JS::create_heap_function(heap(), move(steps))); - HTML::main_thread_event_loop().task_queue().add(move(task)); + auto task = HTML::Task::create(vm(), m_media_element_event_task_source.source, HTML::current_settings_object().responsible_document(), steps); + HTML::main_thread_event_loop().task_queue().add(task); } } diff --git a/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.h b/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.h index 2e0900dd571..0588ac8d2c8 100644 --- a/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.h +++ b/Userland/Libraries/LibWeb/WebAudio/BaseAudioContext.h @@ -62,7 +62,7 @@ public: protected: explicit BaseAudioContext(JS::Realm&, float m_sample_rate = 0); - void queue_a_media_element_task(Function steps); + void queue_a_media_element_task(JS::NonnullGCPtr>); virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override;