Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(web-audio): expose shared factories and analyser data methods

ldm02a5690485d67
diff --git a/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs b/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs
index 4fe8c9752..1d812561a 100644
--- a/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/assets/prototype_bindings.rs
@@ -53,8 +53,7 @@ use super::super::{
touch_runtime::install_touch_template_bindings,
view_transition_runtime::install_view_transition_template_bindings,
web_audio_runtime::{
- audio_buffer_get_channel_data_callback, install_offline_audio_context_bindings,
- install_web_audio_template_bindings,
+ audio_buffer_get_channel_data_callback, install_web_audio_template_bindings,
},
webrtc::install_webrtc_template_bindings,
websocket::{install_websocket_bindings, install_websocket_stream_bindings},
@@ -564,9 +563,6 @@ pub(super) fn install_constructor_template_bindings<'s>(
"Navigation" => {
install_navigation_bindings(scope, template);
}
- "OfflineAudioContext" => {
- install_offline_audio_context_bindings(scope, template);
- }
"AudioBuffer" => {
let proto = template.prototype_template(scope);
AudioBufferTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
index 3adeff5c2..b3ac57e5e 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
@@ -4,6 +4,7 @@ use super::media_queries::{
simple_event_target_remove_event_listener_callback,
};
use super::*;
+use crate::native_bridge::throw_dom_exception;
use crate::util::{
array_push_value, call_object_method, get_private_value, object_string_property,
set_private_value, set_symbol_to_string_tag,
@@ -30,7 +31,7 @@ const OFFLINE_AUDIO_COMPLETE_CONTEXT_SLOT: &str = "__moliOfflineAudioCompleteCon
const OFFLINE_AUDIO_COMPLETE_BUFFER_SLOT: &str = "__moliOfflineAudioCompleteBuffer";
const OFFLINE_AUDIO_COMPRESSORS_SLOT: &str = "__moliOfflineAudioCompressors";
const DYNAMICS_COMPRESSOR_REDUCTION_SLOT: &str = "__moliDynamicsCompressorReduction";
-const MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH: u32 = 1 << 24;
+const ANALYSER_FFT_SIZE_SLOT: &str = "__moliAnalyserFftSize";
#[derive(WebApiObject)]
#[webapi(interface = "AudioContext")]
@@ -203,10 +204,8 @@ struct DynamicsCompressorNodePrototypeDeclaration {
#[derive(WebApiObject)]
#[webapi(interface = "AnalyserNode")]
struct AnalyserNodeObjectDeclaration {
- #[webapi(data_property = "fftSize")]
+ #[webapi(slot = ANALYSER_FFT_SIZE_SLOT)]
fft_size: f64,
- #[webapi(data_property = "frequencyBinCount")]
- frequency_bin_count: f64,
#[webapi(data_property = "minDecibels")]
min_decibels: f64,
#[webapi(data_property = "maxDecibels")]
@@ -217,6 +216,15 @@ struct AnalyserNodeObjectDeclaration {
connect: (),
#[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
disconnect: (),
+}
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "AnalyserNode", enumerable)]
+struct AnalyserNodePrototypeDeclaration {
+ #[webapi(accessor_property = "fftSize", getter = analyser_fft_size_getter_callback, setter = analyser_fft_size_setter_callback)]
+ fft_size: (),
+ #[webapi(accessor_property = "frequencyBinCount", getter = analyser_frequency_bin_count_getter_callback)]
+ frequency_bin_count: (),
#[webapi(method, length = 1, callback = analyser_get_float_frequency_data_callback)]
get_float_frequency_data: (),
#[webapi(method, length = 1, callback = analyser_get_float_time_domain_data_callback)]
@@ -370,6 +378,13 @@ struct AudioParamSetValueAtTimeArgs {
start_time: f64,
}
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AnalyserNode.fftSize")]
+struct AnalyserFftSizeArgs {
+ #[webidl(required)]
+ value: u32,
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(
name = "AudioContext",
@@ -397,29 +412,33 @@ struct AudioWorkletNodeTemplateDeclaration {
}
#[derive(WebApiFunctionTemplate)]
-#[webapi(name = "OfflineAudioContext", enumerable)]
-struct OfflineAudioContextTemplateMethodsDeclaration {
+#[webapi(name = "BaseAudioContext", enumerable)]
+struct BaseAudioContextPrototypeDeclaration {
#[webapi(
method = "createOscillator",
length = 0,
- callback = offline_audio_context_create_oscillator_callback
+ callback = audio_context_create_oscillator_callback
)]
create_oscillator: (),
#[webapi(
method = "createDynamicsCompressor",
length = 0,
- callback = offline_audio_context_create_dynamics_compressor_callback
+ callback = audio_context_create_dynamics_compressor_callback
)]
create_dynamics_compressor: (),
#[webapi(
method = "createAnalyser",
length = 0,
- callback = offline_audio_context_create_analyser_callback
+ callback = audio_context_create_analyser_callback
)]
create_analyser: (),
+}
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "OfflineAudioContext", enumerable)]
+struct OfflineAudioContextPrototypeDeclaration {
#[webapi(
method = "startRendering",
length = 0,
@@ -428,14 +447,6 @@ struct OfflineAudioContextTemplateMethodsDeclaration {
start_rendering: (),
}
-pub(super) fn install_offline_audio_context_bindings<'s>(
- scope: &mut v8::PinScope<'s, '_, ()>,
- template: v8::Local<'s, v8::FunctionTemplate>,
-) {
- let proto = template.prototype_template(scope);
- OfflineAudioContextTemplateMethodsDeclaration::initialize_prototype_template(scope, proto);
-}
-
pub(in crate::context_bootstrap) fn build_audio_context_constructor_template<'s>(
scope: &mut v8::PinScope<'s, '_, ()>,
) -> v8::Local<'s, v8::FunctionTemplate> {
@@ -453,11 +464,32 @@ pub(in crate::context_bootstrap) fn install_web_audio_template_bindings<'s>(
template: v8::Local<'s, v8::FunctionTemplate>,
interface_name: &str,
) {
- if interface_name == "DynamicsCompressorNode" {
- DynamicsCompressorNodePrototypeDeclaration::initialize_prototype_template(
- scope,
- template.prototype_template(scope),
- );
+ match interface_name {
+ "BaseAudioContext" => {
+ BaseAudioContextPrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+ }
+ "OfflineAudioContext" => {
+ OfflineAudioContextPrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+ }
+ "AnalyserNode" => {
+ AnalyserNodePrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+ }
+ "DynamicsCompressorNode" => {
+ DynamicsCompressorNodePrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+ }
+ _ => {}
}
}
@@ -569,6 +601,7 @@ fn audio_context_close_callback<'s>(
AUDIO_CONTEXT_PROCESSORS_SLOT,
processors.into(),
);
+ define_non_enumerable_string_property(scope, context, "state", "closed");
if let Some(promise) = resolved_undefined_promise(scope) {
rv.set(promise.into());
@@ -1258,11 +1291,27 @@ pub(in crate::context_bootstrap) fn audio_buffer_get_channel_data_callback<'s>(
rv.set(data.into());
}
-fn offline_audio_context_create_oscillator_callback<'s>(
+fn require_base_audio_context<'s>(
scope: &mut v8::PinScope<'s, '_>,
- _args: v8::FunctionCallbackArguments<'s>,
+ object: v8::Local<'s, v8::Object>,
+) -> bool {
+ if is_audio_context_object(scope, object)
+ || get_private_value(scope, object, OFFLINE_AUDIO_LENGTH_SLOT).is_some()
+ {
+ return true;
+ }
+ throw_type_error(scope, "Illegal invocation: expected a BaseAudioContext.");
+ false
+}
+
+fn audio_context_create_oscillator_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
+ if !require_base_audio_context(scope, args.this()) {
+ return;
+ }
let frequency = audio_param(scope, 440.0);
let node = OscillatorNodeObjectDeclaration::new("sine", frequency)
.bind(scope)
@@ -1270,11 +1319,14 @@ fn offline_audio_context_create_oscillator_callback<'s>(
rv.set(node.into());
}
-fn offline_audio_context_create_dynamics_compressor_callback<'s>(
+fn audio_context_create_dynamics_compressor_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
+ if !require_base_audio_context(scope, args.this()) {
+ return;
+ }
let threshold = audio_param(scope, -24.0);
let knee = audio_param(scope, 30.0);
let ratio = audio_param(scope, 12.0);
@@ -1288,12 +1340,15 @@ fn offline_audio_context_create_dynamics_compressor_callback<'s>(
rv.set(node.into());
}
-fn offline_audio_context_create_analyser_callback<'s>(
+fn audio_context_create_analyser_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
- _args: v8::FunctionCallbackArguments<'s>,
+ args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
- let node = AnalyserNodeObjectDeclaration::new(2048.0, 1024.0, -100.0, -30.0, 0.8)
+ if !require_base_audio_context(scope, args.this()) {
+ return;
+ }
+ let node = AnalyserNodeObjectDeclaration::new(2048.0, -100.0, -30.0, 0.8)
.bind(scope)
.expect("AnalyserNode declaration should bind");
rv.set(node.into());
@@ -1430,92 +1485,168 @@ fn audio_node_disconnect_callback<'s>(
fn analyser_get_float_frequency_data_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- fill_numeric_array_like_with_profile(
- scope,
- args.get(0),
- SYNTHETIC_ANALYSER_FREQUENCY_BINS,
- *SYNTHETIC_ANALYSER_FREQUENCY_BINS.last().unwrap_or(&-100.0),
- );
- rv.set(v8::undefined(scope).into());
+ copy_analyser_data(scope, &args, AnalyserData::FloatFrequency);
}
fn analyser_get_float_time_domain_data_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- fill_numeric_array_like(scope, args.get(0), 0.0);
- rv.set(v8::undefined(scope).into());
+ copy_analyser_data(scope, &args, AnalyserData::FloatTimeDomain);
}
fn analyser_get_byte_frequency_data_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- fill_numeric_array_like(scope, args.get(0), 0.0);
- rv.set(v8::undefined(scope).into());
+ copy_analyser_data(scope, &args, AnalyserData::ByteFrequency);
}
fn analyser_get_byte_time_domain_data_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ copy_analyser_data(scope, &args, AnalyserData::ByteTimeDomain);
+}
+
+fn analyser_fft_size<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ object: v8::Local<'s, v8::Object>,
+) -> Option<u32> {
+ let Some(size) = web_audio_number_slot(scope, object, ANALYSER_FFT_SIZE_SLOT) else {
+ throw_type_error(scope, "Illegal invocation: expected an AnalyserNode.");
+ return None;
+ };
+ Some(size as u32)
+}
+
+fn analyser_fft_size_getter_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
- fill_numeric_array_like(scope, args.get(0), 128.0);
- rv.set(v8::undefined(scope).into());
+ if let Some(size) = analyser_fft_size(scope, args.this()) {
+ rv.set_uint32(size);
+ }
}
-fn fill_numeric_array_like(
- scope: &mut v8::PinScope<'_, '_>,
- value: v8::Local<'_, v8::Value>,
- fill: f64,
+fn analyser_fft_size_setter_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- let Ok(array_like) = v8::Local::<v8::Object>::try_from(value) else {
+ if analyser_fft_size(scope, args.this()).is_none() {
return;
- };
- let Some(length_value) = array_like.get(scope, v8str(scope, "length").into()) else {
+ }
+ let Some(parsed) = webidl::parse_args::<AnalyserFftSizeArgs>(scope, &args) else {
return;
};
- let Some(length) = length_value.uint32_value(scope) else {
+ if !(32..=32768).contains(&parsed.value) || !parsed.value.is_power_of_two() {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "AnalyserNode.fftSize must be a power of two between 32 and 32768.",
+ );
return;
- };
- let length = capped_numeric_array_like_length(length);
- let value = v8::Number::new(scope, fill);
- for index in 0..length {
- let _ = array_like.set_index(scope, index, value.into());
}
+ set_web_audio_number_slot(
+ scope,
+ args.this(),
+ ANALYSER_FFT_SIZE_SLOT,
+ parsed.value as f64,
+ );
}
-fn fill_numeric_array_like_with_profile(
- scope: &mut v8::PinScope<'_, '_>,
- value: v8::Local<'_, v8::Value>,
- profile: &[f64],
- fallback: f64,
+fn analyser_frequency_bin_count_getter_callback<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if let Some(size) = analyser_fft_size(scope, args.this()) {
+ rv.set_uint32(size / 2);
+ }
+}
+
+enum AnalyserData {
+ FloatFrequency,
+ FloatTimeDomain,
+ ByteFrequency,
+ ByteTimeDomain,
+}
+
+fn copy_analyser_data<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: &v8::FunctionCallbackArguments<'s>,
+ kind: AnalyserData,
) {
- let Ok(array_like) = v8::Local::<v8::Object>::try_from(value) else {
+ let Some(fft_size) = analyser_fft_size(scope, args.this()) else {
return;
};
- let Some(length_value) = array_like.get(scope, v8str(scope, "length").into()) else {
+ let array: Option<v8::Local<v8::TypedArray>> = match kind {
+ AnalyserData::FloatFrequency | AnalyserData::FloatTimeDomain => {
+ v8::Local::<v8::Float32Array>::try_from(args.get(0))
+ .ok()
+ .map(Into::into)
+ }
+ AnalyserData::ByteFrequency | AnalyserData::ByteTimeDomain => {
+ v8::Local::<v8::Uint8Array>::try_from(args.get(0))
+ .ok()
+ .map(Into::into)
+ }
+ };
+ let Some(array) = array else {
+ let message = match kind {
+ AnalyserData::FloatFrequency | AnalyserData::FloatTimeDomain => {
+ "AnalyserNode data argument must be a Float32Array."
+ }
+ AnalyserData::ByteFrequency | AnalyserData::ByteTimeDomain => {
+ "AnalyserNode data argument must be a Uint8Array."
+ }
+ };
+ throw_type_error(scope, message);
return;
};
- let Some(length) = length_value.uint32_value(scope) else {
+ if let Some(store) = array.get_backing_store()
+ && (store.is_shared() || store.is_resizable_by_user_javascript())
+ {
+ throw_type_error(
+ scope,
+ "AnalyserNode data requires a non-shared, fixed-length buffer.",
+ );
return;
+ }
+ let source_length = match kind {
+ AnalyserData::FloatFrequency | AnalyserData::ByteFrequency => fft_size / 2,
+ AnalyserData::FloatTimeDomain | AnalyserData::ByteTimeDomain => fft_size,
};
- let length = capped_numeric_array_like_length(length);
+ // Use the intrinsic view length, not a user-overridable JS property. The
+ // validated fftSize bounds the work, and excess destination entries stay intact.
+ let length = array.length().min(source_length as usize);
for index in 0..length {
- let fill = profile.get(index as usize).copied().unwrap_or(fallback);
+ // Preserve the existing compatibility profile. This is synthetic data,
+ // not an FFT of connected nodes; real audio graph processing is separate.
+ let fill = match kind {
+ AnalyserData::FloatFrequency => SYNTHETIC_ANALYSER_FREQUENCY_BINS
+ .get(index)
+ .or_else(|| SYNTHETIC_ANALYSER_FREQUENCY_BINS.last())
+ .copied()
+ .unwrap_or(-100.0),
+ AnalyserData::FloatTimeDomain | AnalyserData::ByteFrequency => 0.0,
+ AnalyserData::ByteTimeDomain => 128.0,
+ };
let value = v8::Number::new(scope, fill);
- let _ = array_like.set_index(scope, index, value.into());
+ if array.set_index(scope, index as u32, value.into()).is_none() {
+ return;
+ }
}
}
-fn capped_numeric_array_like_length(length: u32) -> u32 {
- length.min(MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH)
-}
-
fn oscillator_start_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
_args: v8::FunctionCallbackArguments<'s>,
@@ -1652,29 +1783,3 @@ fn float_to_usize(value: f64) -> Option<usize> {
}
usize::try_from(value as u64).ok()
}
-
-#[cfg(test)]
-mod tests {
- use super::{MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH, capped_numeric_array_like_length};
-
- #[test]
- fn numeric_array_like_fill_length_is_bounded() {
- assert_eq!(capped_numeric_array_like_length(0), 0);
- assert_eq!(
- capped_numeric_array_like_length(MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH - 1),
- MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH - 1
- );
- assert_eq!(
- capped_numeric_array_like_length(MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH),
- MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH
- );
- assert_eq!(
- capped_numeric_array_like_length(MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH + 1),
- MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH
- );
- assert_eq!(
- capped_numeric_array_like_length(u32::MAX),
- MAX_AUDIO_ANALYSER_ARRAY_FILL_LENGTH
- );
- }
-}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
index b11a60c0f..4426736c4 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
@@ -2524,14 +2524,7 @@ fn web_audio_declared_fixed_own_methods_keep_descriptors() {
ctx: ["addEventListener", "removeEventListener", "dispatchEvent"].map(name => describe(ctx, name)),
osc: ["connect", "disconnect", "start"].map(name => describe(osc, name)),
comp: ["connect", "disconnect"].map(name => describe(comp, name)),
- analyser: [
- "connect",
- "disconnect",
- "getFloatFrequencyData",
- "getFloatTimeDomainData",
- "getByteFrequencyData",
- "getByteTimeDomainData"
- ].map(name => describe(analyser, name)),
+ analyser: ["connect", "disconnect"].map(name => describe(analyser, name)),
param: describe(osc.frequency, "setValueAtTime")
});
})()
@@ -2541,7 +2534,7 @@ fn web_audio_declared_fixed_own_methods_keep_descriptors() {
assert_eq!(
result,
- r#"{"ctxKeys":"addEventListener,removeEventListener,dispatchEvent","oscKeys":"","compKeys":"","analyserKeys":"","paramKeys":"","ctx":["addEventListener:true:true:true:function:addEventListener:0","removeEventListener:true:true:true:function:removeEventListener:0","dispatchEvent:true:true:true:function:dispatchEvent:0"],"osc":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0","start:false:true:true:function:start:1"],"comp":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0"],"analyser":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0","getFloatFrequencyData:false:true:true:function:getFloatFrequencyData:1","getFloatTimeDomainData:false:true:true:function:getFloatTimeDomainData:1","getByteFrequencyData:false:true:true:function:getByteFrequencyData:1","getByteTimeDomainData:false:true:true:function:getByteTimeDomainData:1"],"param":"setValueAtTime:false:true:true:function:setValueAtTime:2"}"#
+ r#"{"ctxKeys":"addEventListener,removeEventListener,dispatchEvent","oscKeys":"","compKeys":"","analyserKeys":"","paramKeys":"","ctx":["addEventListener:true:true:true:function:addEventListener:0","removeEventListener:true:true:true:function:removeEventListener:0","dispatchEvent:true:true:true:function:dispatchEvent:0"],"osc":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0","start:false:true:true:function:start:1"],"comp":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0"],"analyser":["connect:false:true:true:function:connect:1","disconnect:false:true:true:function:disconnect:0"],"param":"setValueAtTime:false:true:true:function:setValueAtTime:2"}"#
);
}
@@ -2723,6 +2716,7 @@ fn web_audio_internal_maps_and_errors_ignore_public_primordial_overrides() {
globalThis.__webAudioPrimordialProbe = {
contextTag: Object.prototype.toString.call(context),
closedTag: Object.prototype.toString.call(closed),
+ closedState: context.state,
rejection: "pending"
};
rejected.catch(error => {
@@ -2744,7 +2738,7 @@ fn web_audio_internal_maps_and_errors_ignore_public_primordial_overrides() {
.expect("WebAudio primordial override probe should evaluate");
assert_eq!(
result,
- r#"{"contextTag":"[object AudioContext]","closedTag":"[object Promise]","rejection":"TypeError:true"}"#
+ r#"{"contextTag":"[object AudioContext]","closedTag":"[object Promise]","closedState":"closed","rejection":"TypeError:true"}"#
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
index 8a79216ab..406769769 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/mod.rs
@@ -30,4 +30,5 @@ mod structured_clone;
mod transferable_streams;
mod traversal;
mod trusted_types;
+mod web_audio;
mod webrtc;
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs
new file mode 100644
index 000000000..758438f7d
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs
@@ -0,0 +1,214 @@
+use super::*;
+
+#[test]
+fn web_audio_factories_are_shared_by_realtime_and_offline_contexts() {
+ let mut vm = new_storage_test_vm("https://audio-factories.test/");
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const realtime = new AudioContext();
+ const offline = new OfflineAudioContext(1, 64, 44100);
+ return JSON.stringify(["createOscillator", "createAnalyser", "createDynamicsCompressor"].map(name => {
+ const method = BaseAudioContext.prototype[name];
+ const liveNode = realtime[name]();
+ const offlineNode = offline[name]();
+ return [
+ method === realtime[name] && method === offline[name],
+ Object.hasOwn(BaseAudioContext.prototype, name),
+ Object.hasOwn(AudioContext.prototype, name),
+ Object.hasOwn(OfflineAudioContext.prototype, name),
+ method.length,
+ liveNode.constructor.name,
+ offlineNode.constructor.name,
+ liveNode !== realtime[name]()
+ ];
+ }));
+})()
+"#,
+ )
+ .expect("both audio context kinds should expose shared factories");
+ assert_eq!(
+ result,
+ r#"[[true,true,false,false,0,"OscillatorNode","OscillatorNode",true],[true,true,false,false,0,"AnalyserNode","AnalyserNode",true],[true,true,false,false,0,"DynamicsCompressorNode","DynamicsCompressorNode",true]]"#
+ );
+}
+
+#[test]
+fn web_audio_analyser_data_methods_are_shared_on_the_prototype() {
+ let mut vm = new_storage_test_vm("https://audio-analyser-methods.test/");
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const context = new OfflineAudioContext(1, 64, 44100);
+ const a = context.createAnalyser();
+ const b = context.createAnalyser();
+ return JSON.stringify([
+ "getFloatFrequencyData", "getFloatTimeDomainData",
+ "getByteFrequencyData", "getByteTimeDomainData"
+ ].map(name => {
+ const descriptor = Object.getOwnPropertyDescriptor(AnalyserNode.prototype, name);
+ return [
+ typeof AnalyserNode.prototype[name],
+ a[name] === b[name] && a[name] === AnalyserNode.prototype[name],
+ Object.hasOwn(a, name),
+ descriptor && descriptor.enumerable,
+ descriptor && descriptor.writable && descriptor.configurable,
+ descriptor && descriptor.value.length,
+ descriptor && descriptor.value.name === name
+ ];
+ }));
+})()
+"#,
+ )
+ .expect("analyser method descriptors should evaluate");
+ assert_eq!(
+ result,
+ r#"[["function",true,false,true,true,1,true],["function",true,false,true,true,1,true],["function",true,false,true,true,1,true],["function",true,false,true,true,1,true]]"#
+ );
+}
+
+#[test]
+fn web_audio_factories_reject_forged_contexts_and_keep_nodes_independent() {
+ let mut vm = new_storage_test_vm("https://audio-factory-receivers.test/");
+ let result = vm.eval(r#"
+(() => {
+ const errors = [];
+ for (const context of [new AudioContext(), new OfflineAudioContext(1, 64, 44100)]) {
+ for (const name of ['createOscillator', 'createAnalyser', 'createDynamicsCompressor']) {
+ for (const receiver of [{}, BaseAudioContext.prototype, Object.create(context), new Proxy(context, {})]) {
+ try { context[name].call(receiver); errors.push(name + ': accepted forged context'); }
+ catch (error) { if (error.name !== 'TypeError') errors.push(name + ': ' + error.name); }
+ }
+ }
+ const a = context.createOscillator(), b = context.createOscillator();
+ a.type = 'triangle';
+ if (a.frequency.setValueAtTime(880, 0) !== a.frequency) errors.push('AudioParam return value');
+ if (a.frequency.value !== 880 || b.frequency.value !== 440 || b.type !== 'sine') errors.push('shared oscillator state');
+ const analyser = context.createAnalyser();
+ if (a.connect(analyser) !== analyser || analyser.connect(context.destination) !== context.destination) errors.push('connect return value');
+ if (a.start(0) !== undefined || a.disconnect() !== undefined) errors.push('source return value');
+ }
+ if (AudioContext !== webkitAudioContext || typeof new webkitAudioContext().createOscillator !== 'function') errors.push('webkit alias');
+ if (typeof AudioContext.prototype.startRendering !== 'undefined') errors.push('offline-only method leaked');
+ return JSON.stringify(errors);
+})()
+"#).expect("audio factories should enforce receivers and keep per-node state");
+ assert_eq!(result, "[]");
+}
+
+#[test]
+fn web_audio_analyser_fft_size_validates_and_drives_readonly_bin_count() {
+ let mut vm = new_storage_test_vm("https://audio-fft-size.test/");
+ let result = vm.eval(r#"
+(() => {
+ const errors = [];
+ for (const context of [new AudioContext(), new OfflineAudioContext(1, 64, 44100)]) {
+ const analyser = context.createAnalyser(), other = context.createAnalyser();
+ for (let size = 32; size <= 32768; size *= 2) {
+ analyser.fftSize = size;
+ if (analyser.fftSize !== size || analyser.frequencyBinCount !== size / 2) errors.push('size ' + size);
+ }
+ for (const size of [-1, 0, 16, 31, 33, 1000, 65536, NaN, Infinity, -Infinity, undefined, null]) {
+ try { analyser.fftSize = size; errors.push('accepted ' + String(size)); }
+ catch (error) { if (error.name !== 'IndexSizeError') errors.push('wrong exception ' + error.name); }
+ if (analyser.fftSize !== 32768) errors.push('invalid assignment changed size');
+ }
+ for (const size of [Symbol(), 64n]) {
+ try { analyser.fftSize = size; errors.push('accepted non-number'); }
+ catch (error) { if (error.name !== 'TypeError') errors.push(error.name); }
+ }
+ analyser.fftSize = '64';
+ if (analyser.frequencyBinCount !== 32) errors.push('string conversion');
+ analyser.fftSize = 32.9;
+ if (analyser.fftSize !== 32) errors.push('unsigned long conversion');
+ if (Reflect.set(analyser, 'frequencyBinCount', 3) !== false || analyser.frequencyBinCount !== 16) errors.push('bin count is writable');
+ const sentinel = new Error('conversion');
+ try { analyser.fftSize = { valueOf() { throw sentinel; } }; errors.push('ignored conversion exception'); }
+ catch (error) { if (error !== sentinel) errors.push('replaced conversion exception'); }
+ if (other.fftSize !== 2048 || other.frequencyBinCount !== 1024) errors.push('shared analyser state');
+ }
+ return JSON.stringify(errors);
+})()
+"#).expect("analyser sizes should follow WebIDL conversion and validation");
+ assert_eq!(result, "[]");
+}
+
+#[test]
+fn web_audio_analyser_writes_only_available_samples_in_the_destination_view() {
+ let mut vm = new_storage_test_vm("https://audio-analyser-buffer.test/");
+ let result = vm.eval(r#"
+(() => {
+ const errors = [];
+ for (const context of [new AudioContext(), new OfflineAudioContext(1, 64, 44100)]) {
+ const analyser = context.createAnalyser();
+ for (const size of [32, 2048, 32768]) {
+ analyser.fftSize = size;
+ for (const [method, ArrayType, count, expected] of [
+ ['getFloatFrequencyData', Float32Array, size / 2, null],
+ ['getFloatTimeDomainData', Float32Array, size, 0],
+ ['getByteFrequencyData', Uint8Array, size / 2, 0],
+ ['getByteTimeDomainData', Uint8Array, size, 128]
+ ]) {
+ for (const length of [0, 1, count - 1, count, count + 3]) {
+ const storage = new ArrayType(length + 4).fill(57);
+ const view = storage.subarray(2, 2 + length);
+ Object.defineProperty(view, 'length', { get() { throw new Error('JS length must not be read'); } });
+ if (analyser[method](view) !== undefined) errors.push(method + ': return');
+ for (let i = 0; i < storage.length; i++) {
+ const written = i >= 2 && i < 2 + Math.min(count, length);
+ if (!written && storage[i] !== 57) errors.push(method + ': out-of-range write ' + i);
+ if (written && (expected === null ? !Number.isFinite(storage[i]) || storage[i] >= 0 : storage[i] !== expected)) errors.push(method + ': sample ' + i);
+ }
+ }
+ }
+ }
+ }
+ return JSON.stringify(errors);
+})()
+"#).expect("analyser reads should respect native view length, offset and sample count");
+ assert_eq!(result, "[]");
+}
+
+#[test]
+fn web_audio_analyser_checks_receiver_and_typed_array_without_duck_typing() {
+ let mut vm = new_storage_test_vm("https://audio-analyser-arguments.test/");
+ let result = vm.eval(r#"
+(() => {
+ const errors = [];
+ const analyser = new AudioContext().createAnalyser();
+ for (const [method, ArrayType, WrongArrayType] of [
+ ['getFloatFrequencyData', Float32Array, Uint8Array],
+ ['getFloatTimeDomainData', Float32Array, Uint8Array],
+ ['getByteFrequencyData', Uint8Array, Float32Array],
+ ['getByteTimeDomainData', Uint8Array, Float32Array]
+ ]) {
+ const expectTypeError = (run, label) => {
+ try { run(); errors.push(method + ': accepted ' + label); }
+ catch (error) { if (error.name !== 'TypeError') errors.push(method + ': ' + error.name); }
+ };
+ const fake = { get length() { throw new Error('must not read arbitrary length'); } };
+ for (const value of [undefined, null, 1, [], fake, new WrongArrayType(4), new Float64Array(4), new Uint8ClampedArray(4), new DataView(new ArrayBuffer(16)), new Proxy(new ArrayType(4), {})]) {
+ expectTypeError(() => analyser[method](value), 'wrong array');
+ }
+ expectTypeError(() => analyser[method](), 'missing argument');
+ expectTypeError(() => analyser[method](new ArrayType(new SharedArrayBuffer(16))), 'shared buffer');
+ expectTypeError(() => analyser[method](new ArrayType(new ArrayBuffer(16, { maxByteLength: 32 }))), 'resizable buffer');
+ const output = new ArrayType(4).fill(57);
+ for (const receiver of [{}, AnalyserNode.prototype, Object.create(analyser), new Proxy(analyser, {})]) {
+ expectTypeError(() => analyser[method].call(receiver, output), 'forged receiver');
+ }
+ if (!output.every(value => value === 57)) errors.push(method + ': invalid invocation wrote data');
+ }
+ const size = Object.getOwnPropertyDescriptor(AnalyserNode.prototype, 'fftSize');
+ const bins = Object.getOwnPropertyDescriptor(AnalyserNode.prototype, 'frequencyBinCount');
+ for (const run of [() => size.get.call({}), () => size.set.call({}, 32), () => bins.get.call({})]) {
+ try { run(); errors.push('accepted forged accessor receiver'); }
+ catch (error) { if (error.name !== 'TypeError') errors.push(error.name); }
+ }
+ return JSON.stringify(errors);
+})()
+"#).expect("analyser data methods should reject incompatible receivers and buffers");
+ assert_eq!(result, "[]");
+}
diff --git a/moli/tests/fetch_cli/eval.rs b/moli/tests/fetch_cli/eval.rs
index e6604928c..56495b7d4 100644
--- a/moli/tests/fetch_cli/eval.rs
+++ b/moli/tests/fetch_cli/eval.rs
@@ -65,6 +65,61 @@ fn run_eval_source(
run_moli(args)
}
+#[test]
+fn eval_can_use_realtime_audio_oscillator_and_analyser_shims() -> Result<()> {
+ let output = run_eval(
+ "data:text/html,<!doctype html><title>Web Audio</title>",
+ r#"(async () => {
+ const context = new AudioContext();
+ const oscillator = context.createOscillator();
+ const analyser = context.createAnalyser();
+ oscillator.type = 'triangle';
+ oscillator.frequency.setValueAtTime(880, context.currentTime);
+ analyser.fftSize = 32;
+ const connected = oscillator.connect(analyser).connect(context.destination);
+ oscillator.start(0);
+ const frequencies = new Float32Array(analyser.frequencyBinCount + 1).fill(42);
+ const readResult = analyser.getFloatFrequencyData(frequencies);
+ const waveform = new Uint8Array(analyser.fftSize);
+ analyser.getByteTimeDomainData(waveform);
+ oscillator.disconnect();
+ analyser.disconnect();
+ await context.close();
+ return {
+ oscillator: oscillator instanceof OscillatorNode,
+ frequency: oscillator.frequency.value,
+ analyser: analyser instanceof AnalyserNode,
+ connected: connected === context.destination,
+ readReturnedUndefined: readResult === undefined,
+ bins: analyser.frequencyBinCount,
+ filled: frequencies.slice(0, -1).every(value => Number.isFinite(value) && value < 0),
+ untouchedTail: frequencies[analyser.frequencyBinCount],
+ silentWaveform: waveform.every(value => value === 128),
+ state: context.state
+ };
+ })()"#,
+ &[],
+ )?;
+ assert!(output.status.success(), "{}", clean_output(&output.stderr));
+ let value: serde_json::Value = serde_json::from_slice(&output.stdout)?;
+ assert_eq!(
+ value,
+ serde_json::json!({
+ "oscillator": true,
+ "frequency": 880,
+ "analyser": true,
+ "connected": true,
+ "readReturnedUndefined": true,
+ "bins": 16,
+ "filled": true,
+ "untouchedTail": 42,
+ "silentWaveform": true,
+ "state": "closed"
+ })
+ );
+ Ok(())
+}
+
#[test]
fn eval_computed_sizes_only_use_existing_geometry_with_or_without_css() -> Result<()> {
let url = "data:text/html,<!doctype html><div id=target style=\"max-width:20px\"></div><canvas id=canvas style=\"max-width:40px\"></canvas>";