Skip to content

Truly independent web browser

LibWeb: Port WebAudio events to Rust

Shannon Booth26fc50293df3
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
index bb6f3c96705..6ac2e3d57a8 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
@@ -221,6 +221,12 @@ impl CppEventTarget {
pub(crate) struct CppEvent(CppCell);
impl CppEvent {
+ /// # Safety
+ /// `event` must be a live C++ Event that the returned handle keeps alive.
+ pub(crate) unsafe fn from_raw(event: *mut c_void) -> Self {
+ Self(unsafe { CppCell::from_raw(event) })
+ }
+
pub(crate) fn create(realm: Gc<Realm>, r#type: &str, bubbles: bool, cancelable: bool, composed: bool) -> Self {
// SAFETY: The realm is live, the type is lent for the call, and LibWeb returns a live event.
let event = Self(unsafe {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_processing_event.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_processing_event.rs
new file mode 100644
index 00000000000..ca62d476051
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_processing_event.rs
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::{Trace, Vm};
+
+use super::audio_buffer::AudioBuffer;
+use crate::bindings::event_target::CppEvent;
+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::dom::event::EventInit;
+
+pub(crate) struct AudioProcessingEventInit {
+ pub(crate) parent: EventInit,
+ pub(crate) input_buffer: Ref<AudioBuffer>,
+ pub(crate) output_buffer: Ref<AudioBuffer>,
+ pub(crate) playback_time: f64,
+}
+
+// https://webaudio.github.io/web-audio-api/#AudioProcessingEvent
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct AudioProcessingEvent {
+ event: CppEvent,
+ playback_time: f64,
+ input_buffer: Ref<AudioBuffer>,
+ output_buffer: Ref<AudioBuffer>,
+}
+
+impl Wrappable for AudioProcessingEvent {
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event.as_cpp_parent()
+ }
+}
+
+impl AudioProcessingEvent {
+ pub(crate) fn create(
+ vm: &Vm,
+ event: CppEvent,
+ playback_time: f64,
+ input_buffer: Ref<AudioBuffer>,
+ output_buffer: Ref<AudioBuffer>,
+ ) -> Ref<Self> {
+ let object = allocate(vm, |_| Self {
+ event,
+ playback_time,
+ input_buffer,
+ output_buffer,
+ });
+ object.event.set_owner(object);
+ object
+ }
+
+ pub(crate) fn construct(
+ cx: &mut CallContext,
+ r#type: Utf16String,
+ event_init_dict: AudioProcessingEventInit,
+ ) -> WebIdlResult<Ref<Self>> {
+ let event = CppEvent::create_for_owner(
+ cx.realm(),
+ &r#type,
+ event_init_dict.parent.bubbles,
+ event_init_dict.parent.cancelable,
+ event_init_dict.parent.composed,
+ );
+ Ok(Self::create(
+ cx.vm(),
+ event,
+ event_init_dict.playback_time,
+ event_init_dict.input_buffer,
+ event_init_dict.output_buffer,
+ ))
+ }
+
+ pub(crate) fn playback_time(&self, _: &mut CallContext) -> WebIdlResult<f64> {
+ Ok(self.playback_time)
+ }
+
+ pub(crate) fn input_buffer(&self, _: &mut CallContext) -> WebIdlResult<Ref<AudioBuffer>> {
+ Ok(self.input_buffer)
+ }
+
+ pub(crate) fn output_buffer(&self, _: &mut CallContext) -> WebIdlResult<Ref<AudioBuffer>> {
+ Ok(self.output_buffer)
+ }
+}
+
+#[unsafe(no_mangle)]
+pub(crate) unsafe extern "C" fn web_rust_audio_processing_event_create(
+ vm: &Vm,
+ event: *mut c_void,
+ playback_time: f64,
+ input: *mut c_void,
+ output: *mut c_void,
+) -> *mut c_void {
+ let event = unsafe { CppEvent::from_raw(event) };
+ let input: Ref<AudioBuffer> = unsafe { cell_from_abi(input) };
+ let output: Ref<AudioBuffer> = unsafe { cell_from_abi(output) };
+ AudioProcessingEvent::create(vm, event, playback_time, input, output)
+ .event
+ .as_ptr()
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs
index 04c435c6494..28636401730 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs
@@ -8,4 +8,6 @@ pub(crate) mod audio_buffer;
pub(crate) mod audio_listener;
pub(crate) mod audio_param;
pub(crate) mod audio_param_map;
+pub(crate) mod audio_processing_event;
+pub(crate) mod offline_audio_completion_event;
pub(crate) mod periodic_wave;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_completion_event.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_completion_event.rs
new file mode 100644
index 00000000000..20a1cf42f8f
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/offline_audio_completion_event.rs
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::{Trace, Vm};
+
+use super::audio_buffer::AudioBuffer;
+use crate::bindings::event_target::CppEvent;
+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::dom::event::EventInit;
+
+pub(crate) struct OfflineAudioCompletionEventInit {
+ pub(crate) parent: EventInit,
+ pub(crate) rendered_buffer: Ref<AudioBuffer>,
+}
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct OfflineAudioCompletionEvent {
+ event: CppEvent,
+ rendered_buffer: Option<Ref<AudioBuffer>>,
+}
+
+impl Wrappable for OfflineAudioCompletionEvent {
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event.as_cpp_parent()
+ }
+}
+
+impl OfflineAudioCompletionEvent {
+ pub(crate) fn create(vm: &Vm, event: CppEvent, rendered_buffer: Ref<AudioBuffer>) -> Ref<Self> {
+ let object = allocate(vm, |_| Self {
+ event,
+ rendered_buffer: Some(rendered_buffer),
+ });
+ object.event.set_owner(object);
+ object
+ }
+
+ pub(crate) fn construct(
+ cx: &mut CallContext,
+ r#type: Utf16String,
+ event_init_dict: OfflineAudioCompletionEventInit,
+ ) -> WebIdlResult<Ref<Self>> {
+ let event = CppEvent::create_for_owner(
+ cx.realm(),
+ &r#type,
+ event_init_dict.parent.bubbles,
+ event_init_dict.parent.cancelable,
+ event_init_dict.parent.composed,
+ );
+ Ok(Self::create(cx.vm(), event, event_init_dict.rendered_buffer))
+ }
+
+ pub(crate) fn rendered_buffer(&self, _: &mut CallContext) -> WebIdlResult<Ref<AudioBuffer>> {
+ Ok(self
+ .rendered_buffer
+ .expect("the event initializer supplies its required buffer"))
+ }
+}
+
+#[unsafe(no_mangle)]
+pub(crate) unsafe extern "C" fn web_rust_offline_audio_completion_event_create(
+ vm: &Vm,
+ event: *mut c_void,
+ buffer: *mut c_void,
+) -> *mut c_void {
+ let event = unsafe { CppEvent::from_raw(event) };
+ let buffer: Ref<AudioBuffer> = unsafe { cell_from_abi(buffer) };
+ OfflineAudioCompletionEvent::create(vm, event, buffer).event.as_ptr()
+}
diff --git a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.cpp b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.cpp
index f0991f82df5..86ae285d1d4 100644
--- a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.cpp
+++ b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.cpp
@@ -1,36 +1,23 @@
/*
* Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGC/Heap.h>
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
+#include <LibWeb/Bindings/RustEventTarget.h>
#include <LibWeb/WebAudio/AudioProcessingEvent.h>
namespace Web::WebAudio {
-GC_DEFINE_ALLOCATOR(AudioProcessingEvent);
+extern "C" DOM::Event* web_rust_audio_processing_event_create(JSVM*, void* event, double playback_time, void* input, void* output);
-GC::Ref<AudioProcessingEvent> AudioProcessingEvent::create(Utf16FlyString const& event_name, AudioProcessingEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
+GC::Ref<DOM::Event> AudioProcessingEvent::create(Utf16FlyString const& event_name, AudioProcessingEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
{
- return GC::Heap::the().allocate<AudioProcessingEvent>(event_name, event_init, time_stamp);
-}
-
-AudioProcessingEvent::AudioProcessingEvent(Utf16FlyString const& event_name, AudioProcessingEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : DOM::Event(event_name, event_init, time_stamp)
- , m_playback_time(event_init.playback_time)
- , m_input_buffer(event_init.input_buffer)
- , m_output_buffer(event_init.output_buffer)
-{
-}
-
-AudioProcessingEvent::~AudioProcessingEvent() = default;
-
-void AudioProcessingEvent::visit_edges(GC::Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_input_buffer);
- visitor.visit(m_output_buffer);
+ auto event = Bindings::RustEvent::create(event_name, event_init, time_stamp);
+ return *web_rust_audio_processing_event_create(JS::HostABI::vm_to_abi(JS::VM::the()), event.ptr(), event_init.playback_time, event_init.input_buffer.ptr(), event_init.output_buffer.ptr());
}
}
diff --git a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.h b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.h
index cd85661b337..c1c683f2210 100644
--- a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.h
+++ b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.h
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2026, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -7,36 +8,23 @@
#pragma once
#include <LibWeb/Bindings/AudioProcessingEvent.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/WebAudio/AudioBuffer.h>
-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>
namespace Web::WebAudio {
-using AudioProcessingEventInit = Bindings::AudioProcessingEventInit;
+struct AudioProcessingEventInit : DOM::EventInit {
+ GC::Ref<AudioBuffer> input_buffer;
+ GC::Ref<AudioBuffer> output_buffer;
+ double playback_time;
+};
-// https://webaudio.github.io/web-audio-api/#AudioProcessingEvent
-class AudioProcessingEvent final : public DOM::Event {
- WEB_WRAPPABLE(AudioProcessingEvent, DOM::Event);
- GC_DECLARE_ALLOCATOR(AudioProcessingEvent);
+class AudioProcessingEvent final : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(AudioProcessingEvent, audio_processing_event)
public:
- static GC::Ref<AudioProcessingEvent> create(Utf16FlyString const& event_name, AudioProcessingEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual ~AudioProcessingEvent() override;
-
- double playback_time() const { return m_playback_time; }
- GC::Ref<AudioBuffer> input_buffer() const { return m_input_buffer; }
- GC::Ref<AudioBuffer> output_buffer() const { return m_output_buffer; }
-
-private:
- AudioProcessingEvent(Utf16FlyString const& event_name, AudioProcessingEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual void visit_edges(GC::Cell::Visitor&) override;
-
- double m_playback_time { 0 };
- GC::Ref<AudioBuffer> m_input_buffer;
- GC::Ref<AudioBuffer> m_output_buffer;
+ static GC::Ref<DOM::Event> create(Utf16FlyString const& event_name, AudioProcessingEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
};
}
diff --git a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.idl b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.idl
index b149f00cfa6..ebe55180937 100644
--- a/Libraries/LibWeb/WebAudio/AudioProcessingEvent.idl
+++ b/Libraries/LibWeb/WebAudio/AudioProcessingEvent.idl
@@ -1,5 +1,5 @@
// https://webaudio.github.io/web-audio-api/#AudioProcessingEvent
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface AudioProcessingEvent : Event {
constructor(DOMString type, AudioProcessingEventInit eventInitDict);
readonly attribute double playbackTime;
diff --git a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.cpp b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.cpp
index 745d5db977f..3b597238cc7 100644
--- a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.cpp
+++ b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.cpp
@@ -1,33 +1,23 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGC/Heap.h>
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
+#include <LibWeb/Bindings/RustEventTarget.h>
#include <LibWeb/WebAudio/OfflineAudioCompletionEvent.h>
namespace Web::WebAudio {
-GC_DEFINE_ALLOCATOR(OfflineAudioCompletionEvent);
+extern "C" DOM::Event* web_rust_offline_audio_completion_event_create(JSVM*, void* event, void* buffer);
-GC::Ref<OfflineAudioCompletionEvent> OfflineAudioCompletionEvent::create(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
+GC::Ref<DOM::Event> OfflineAudioCompletionEvent::create(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
{
- return GC::Heap::the().allocate<OfflineAudioCompletionEvent>(event_name, event_init, time_stamp);
-}
-
-OfflineAudioCompletionEvent::OfflineAudioCompletionEvent(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : DOM::Event(event_name, event_init, time_stamp)
- , m_rendered_buffer(event_init.rendered_buffer)
-{
-}
-
-OfflineAudioCompletionEvent::~OfflineAudioCompletionEvent() = default;
-
-void OfflineAudioCompletionEvent::visit_edges(GC::Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_rendered_buffer);
+ auto event = Bindings::RustEvent::create(event_name, event_init, time_stamp);
+ return *web_rust_offline_audio_completion_event_create(JS::HostABI::vm_to_abi(JS::VM::the()), event.ptr(), event_init.rendered_buffer.ptr());
}
}
diff --git a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.h b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.h
index 6e4ee431ad7..5afeebf78cc 100644
--- a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.h
+++ b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.h
@@ -1,45 +1,28 @@
/*
* Copyright (c) 2025, Luke Wilde <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
-#include <LibJS/Forward.h>
#include <LibWeb/Bindings/OfflineAudioCompletionEvent.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/WebAudio/AudioBuffer.h>
-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>
-
-namespace Web::HTML {
-
-class Window;
-
-}
namespace Web::WebAudio {
-using OfflineAudioCompletionEventInit = Bindings::OfflineAudioCompletionEventInit;
+struct OfflineAudioCompletionEventInit : DOM::EventInit {
+ GC::Ref<AudioBuffer> rendered_buffer;
+};
-class OfflineAudioCompletionEvent final : public DOM::Event {
- WEB_WRAPPABLE(OfflineAudioCompletionEvent, DOM::Event);
- GC_DECLARE_ALLOCATOR(OfflineAudioCompletionEvent);
+class OfflineAudioCompletionEvent final : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(OfflineAudioCompletionEvent, offline_audio_completion_event)
public:
- static constexpr size_t rendered_buffer_offset() { return offsetof(OfflineAudioCompletionEvent, m_rendered_buffer); }
- static GC::Ref<OfflineAudioCompletionEvent> create(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual ~OfflineAudioCompletionEvent() override;
-
- GC::Ptr<AudioBuffer> rendered_buffer() const { return m_rendered_buffer; }
-
-private:
- OfflineAudioCompletionEvent(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual void visit_edges(GC::Cell::Visitor&) override;
-
- GC::Ptr<AudioBuffer> m_rendered_buffer;
+ static GC::Ref<DOM::Event> create(Utf16FlyString const& event_name, OfflineAudioCompletionEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
};
}
diff --git a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.idl b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.idl
index 00f82cacd28..9630fb14f58 100644
--- a/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.idl
+++ b/Libraries/LibWeb/WebAudio/OfflineAudioCompletionEvent.idl
@@ -1,5 +1,5 @@
// https://webaudio.github.io/web-audio-api/#OfflineAudioCompletionEvent
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface OfflineAudioCompletionEvent : Event {
constructor(DOMString type, OfflineAudioCompletionEventInit eventInitDict);
readonly attribute AudioBuffer renderedBuffer;