Truly independent web browser
LibWeb: Port AudioWorkletProcessor to a Rust interface
Shannon Booth63b90324bccc
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_worklet_processor.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_worklet_processor.rsnew file mode 100644index 00000000000..b590491aad2--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/audio_worklet_processor.rs@@ -0,0 +1,72 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use core::ffi::c_void;+use core::ptr::NonNull;++use libjs_rust::api::{Gc, Realm, Trace};++use crate::bindings::cpp_cell::CppCell;+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi};+use crate::bindings::{CallContext, Exception, WebIdlResult};+use crate::html::message_port::MessagePort;++unsafe extern "C" {+ fn web_audio_processor_global_scope(realm: *mut c_void) -> *mut c_void;+ fn web_audio_processor_take_pending_data(scope: *mut c_void, port: *mut *mut c_void) -> bool;+ fn web_audio_processor_scope_realm(scope: *mut c_void) -> *mut c_void;+}++// https://webaudio.github.io/web-audio-api/#AudioWorkletProcessor+#[derive(Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AudioWorkletProcessor {+ global_scope: CppCell,+ port: Ref<MessagePort>,+}++impl Wrappable for AudioWorkletProcessor {+ // Ensures wrappers for this processor are created in the worklet realm, not the page realm.+ fn relevant_realm(&self) -> Option<Gc<Realm>> {+ // Pin wrapper creation to the worklet realm.+ let realm = unsafe { web_audio_processor_scope_realm(self.global_scope.as_ptr()) };+ Some(unsafe { Gc::from_non_null(NonNull::new(realm.cast::<Realm>()).unwrap()) })+ }+}++impl AudioWorkletProcessor {+ // https://webaudio.github.io/web-audio-api/#dom-audioworkletprocessor-audioworkletprocessor+ // Runs when `super()` executes in a registered processor subclass during node instantiation.+ pub(crate) fn construct(cx: &mut CallContext) -> WebIdlResult<Ref<Self>> {+ // 1. Let nodeName be the value of the current pending processor construction data's node name... (steps+ // collapsed): When the constructor is invoked outside of AudioWorkletNode instantiation, there is no+ // pending processor construction data, and a TypeError must be thrown.+ let global_scope = unsafe { web_audio_processor_global_scope(cx.realm().as_ptr().cast()) };+ if global_scope.is_null() {+ return Err(Exception::type_error(+ "AudioWorkletProcessor can only be constructed in an AudioWorkletGlobalScope",+ ));+ }+ let mut port = core::ptr::null_mut();+ if !unsafe { web_audio_processor_take_pending_data(global_scope, &raw mut port) } {+ return Err(Exception::type_error(+ "AudioWorkletProcessor cannot be constructed directly; it is instantiated by AudioWorkletNode",+ ));+ }+ let deserialized_port = unsafe { cell_from_abi::<MessagePort>(port) };++ // 2. Set this's port to the pending construction data's transferred port.+ let processor = allocate(cx.vm(), |_| Self {+ global_scope: unsafe { CppCell::from_raw(global_scope) },+ port: deserialized_port,+ });+ Ok(processor)+ }++ pub(crate) fn port(&self, _: &mut CallContext) -> WebIdlResult<Ref<MessagePort>> {+ Ok(self.port)+ }+}diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rsindex 28636401730..e94b3414064 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_audio/mod.rs@@ -9,5 +9,6 @@ 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 audio_worklet_processor;pub(crate) mod offline_audio_completion_event;pub(crate) mod periodic_wave;diff --git a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.cpp b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.cppindex 4dddc6c37c0..4bc81f48aa5 100644--- a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.cpp+++ b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.cpp@@ -4,54 +4,35 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibGC/Heap.h>#include <LibJS/Runtime/Realm.h>-#include <LibJS/Runtime/VM.h>#include <LibWeb/HTML/MessagePort.h>#include <LibWeb/WebAudio/AudioWorkletGlobalScope.h>-#include <LibWeb/WebAudio/AudioWorkletProcessor.h>namespace Web::WebAudio {-GC_DEFINE_ALLOCATOR(AudioWorkletProcessor);--// https://webaudio.github.io/web-audio-api/#dom-audioworkletprocessor-audioworkletprocessor-WebIDL::ExceptionOr<GC::Ref<AudioWorkletProcessor>> AudioWorkletProcessor::construct_impl(JS::Realm& realm)-{- // 1. Let nodeName be the value of the current pending processor construction data's node name... (steps- // collapsed): When the constructor is invoked outside of AudioWorkletNode instantiation, there is no- // pending processor construction data, and a TypeError must be thrown.- auto* global_scope = Bindings::impl_from<AudioWorkletGlobalScope>(&realm.global_object());- if (!global_scope)- return realm.vm().throw_completion<JS::TypeError>(u"AudioWorkletProcessor can only be constructed in an AudioWorkletGlobalScope"sv);-- auto pending_data = global_scope->take_pending_processor_construction_data();- if (!pending_data.has_value())- return realm.vm().throw_completion<JS::TypeError>(u"AudioWorkletProcessor cannot be constructed directly; it is instantiated by AudioWorkletNode"sv);-- // 2. Set this's port to the pending construction data's transferred port.- return GC::Heap::the().allocate<AudioWorkletProcessor>(*global_scope, pending_data->port);+extern "C" {+AudioWorkletGlobalScope* web_audio_processor_global_scope(JS::Realm*);+bool web_audio_processor_take_pending_data(AudioWorkletGlobalScope*, HTML::MessagePort**);+JS::Realm* web_audio_processor_scope_realm(AudioWorkletGlobalScope*);}-AudioWorkletProcessor::AudioWorkletProcessor(GC::Ref<AudioWorkletGlobalScope> global_scope, GC::Ref<HTML::MessagePort> port)- : m_global_scope(global_scope)- , m_port(port)+AudioWorkletGlobalScope* web_audio_processor_global_scope(JS::Realm* realm){+ return Bindings::impl_from<AudioWorkletGlobalScope>(&realm->global_object());}-AudioWorkletProcessor::~AudioWorkletProcessor() = default;--GC::Ptr<Bindings::Wrappable> AudioWorkletProcessor::relevant_global_impl() const+bool web_audio_processor_take_pending_data(AudioWorkletGlobalScope* scope, HTML::MessagePort** port){- // Pin wrapper creation to the worklet realm.- return m_global_scope;+ auto pending_data = scope->take_pending_processor_construction_data();+ if (!pending_data.has_value())+ return false;+ *port = pending_data->port.ptr();+ return true;}-void AudioWorkletProcessor::visit_edges(Cell::Visitor& visitor)+JS::Realm* web_audio_processor_scope_realm(AudioWorkletGlobalScope* scope){- Base::visit_edges(visitor);- visitor.visit(m_global_scope);- visitor.visit(m_port);+ return &scope->realm();}}diff --git a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.h b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.hindex ea95b753fb5..d507595bc5c 100644--- a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.h+++ b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.h@@ -6,37 +6,14 @@#pragma once-#include <LibWeb/Bindings/Wrappable.h>+#include <LibWeb/Bindings/AudioWorkletProcessor.h>+#include <LibWeb/Bindings/RustInterface.h>#include <LibWeb/Export.h>-#include <LibWeb/Forward.h>-#include <LibWeb/WebIDL/ExceptionOr.h>namespace Web::WebAudio {-// https://webaudio.github.io/web-audio-api/#AudioWorkletProcessor-class WEB_API AudioWorkletProcessor final : public Bindings::GCAllocatedWrappable {- WEB_WRAPPABLE(AudioWorkletProcessor, Bindings::GCAllocatedWrappable);- GC_DECLARE_ALLOCATOR(AudioWorkletProcessor);--public:- // https://webaudio.github.io/web-audio-api/#dom-audioworkletprocessor-audioworkletprocessor- // Runs when `super()` executes in a registered processor subclass during node instantiation.- static WebIDL::ExceptionOr<GC::Ref<AudioWorkletProcessor>> construct_impl(JS::Realm&);-- virtual ~AudioWorkletProcessor() override;-- GC::Ref<HTML::MessagePort> port() const { return m_port; }--private:- AudioWorkletProcessor(GC::Ref<AudioWorkletGlobalScope>, GC::Ref<HTML::MessagePort>);-- // Ensures wrappers for this processor are created in the worklet realm, not the page realm.- virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;-- virtual void visit_edges(Cell::Visitor&) override;-- GC::Ref<AudioWorkletGlobalScope> m_global_scope;- GC::Ref<HTML::MessagePort> m_port;+class WEB_API AudioWorkletProcessor final : public Bindings::RustInterfaceCell {+ WEB_RUST_INTERFACE(AudioWorkletProcessor, audio_worklet_processor)};}diff --git a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.idl b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.idlindex 15bba7bc884..7e9218df8c9 100644--- a/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.idl+++ b/Libraries/LibWeb/WebAudio/AudioWorkletProcessor.idl@@ -1,5 +1,5 @@// https://webaudio.github.io/web-audio-api/#AudioWorkletProcessor-[Exposed=AudioWorklet]+[Exposed=AudioWorklet, ImplementedIn=Rust]interface AudioWorkletProcessor {constructor();readonly attribute MessagePort port;