Skip to content

armgit / moli

Public Imported from GitHub
0

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

feat(webaudio): unify native AudioNode interfaces

ldm0622fbed53b42
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
index 34e190407..fcef1b21c 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
@@ -1335,6 +1335,10 @@ const CONSTRUCTOR_SPECS_AFTER_STREAMS: &[ConstructorSpec] = &[
interface: web_api_interfaces::OfflineAudioContext::DESCRIPTOR,
kind: ConstructorKind::OfflineAudioContext,
},
+ ConstructorSpec {
+ interface: web_api_interfaces::AudioNode::DESCRIPTOR,
+ kind: ConstructorKind::Illegal,
+ },
ConstructorSpec {
interface: web_api_interfaces::AudioDestinationNode::DESCRIPTOR,
kind: ConstructorKind::AudioDestinationNode,
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 0175beedc..89f4ec3e8 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
@@ -15,6 +15,7 @@ use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
mod audio_param;
mod biquad;
mod graph;
+mod node;
use audio_param::{audio_param, detune_param};
@@ -73,8 +74,6 @@ struct AudioWorkletObjectDeclaration<'scope> {
#[derive(WebApiObject)]
#[webapi(interface = web_api_interfaces::AudioWorkletNode)]
struct AudioWorkletNodeObjectDeclaration<'scope> {
- #[webapi(data_property)]
- context: v8::Local<'scope, v8::Object>,
#[webapi(data_property)]
port: v8::Local<'scope, v8::Object>,
}
@@ -170,10 +169,6 @@ struct OscillatorNodeObjectDeclaration<'scope> {
frequency: v8::Local<'scope, v8::Object>,
#[webapi(data_property, readonly)]
detune: v8::Local<'scope, v8::Object>,
- #[webapi(method, length = 1, callback = audio_node_connect_callback)]
- connect: (),
- #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
- disconnect: (),
#[webapi(method, length = 1, callback = oscillator_start_callback)]
start: (),
}
@@ -193,10 +188,6 @@ struct DynamicsCompressorNodeObjectDeclaration<'scope> {
release: v8::Local<'scope, v8::Object>,
#[webapi(slot = DYNAMICS_COMPRESSOR_REDUCTION_SLOT)]
reduction: f64,
- #[webapi(method, length = 1, callback = audio_node_connect_callback)]
- connect: (),
- #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
- disconnect: (),
}
#[derive(WebApiFunctionTemplate)]
@@ -217,10 +208,6 @@ struct AnalyserNodeObjectDeclaration {
max_decibels: f64,
#[webapi(data_property = "smoothingTimeConstant")]
smoothing_time_constant: f64,
- #[webapi(method, length = 1, callback = audio_node_connect_callback)]
- connect: (),
- #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
- disconnect: (),
}
#[derive(WebApiFunctionTemplate)]
@@ -391,12 +378,7 @@ struct AudioContextTemplateDeclaration {
constructor_length = 2,
enumerable
)]
-struct AudioWorkletNodeTemplateDeclaration {
- #[webapi(method, length = 1, callback = audio_node_connect_callback)]
- connect: (),
- #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
- disconnect: (),
-}
+struct AudioWorkletNodeTemplateDeclaration {}
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::BaseAudioContext, enumerable)]
@@ -454,16 +436,7 @@ pub(in crate::context_bootstrap) fn install_web_audio_template_bindings<'s>(
template: v8::Local<'s, v8::FunctionTemplate>,
interface_name: &str,
) {
- if matches!(
- interface_name,
- "OscillatorNode"
- | "DynamicsCompressorNode"
- | "AnalyserNode"
- | "BiquadFilterNode"
- | "AudioDestinationNode"
- ) {
- graph::install(scope, template);
- }
+ node::install(scope, template, interface_name);
match interface_name {
"Worklet" => WorkletPrototypeDeclaration::initialize_prototype_template(
scope,
@@ -911,7 +884,7 @@ fn audio_worklet_node_constructor_callback<'s>(
};
let node = args.this();
- AudioWorkletNodeObjectDeclaration::new(context, port1)
+ AudioWorkletNodeObjectDeclaration::new(port1)
.initialize(scope, node)
.expect("AudioWorkletNode declaration should initialize object");
graph::initialize_node(scope, node, context);
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs
index 5e47d4a69..6dc73210a 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs
@@ -18,10 +18,6 @@ struct BiquadFilterNodeObjectDeclaration<'scope> {
q: v8::Local<'scope, v8::Object>,
#[webapi(data_property, readonly)]
gain: v8::Local<'scope, v8::Object>,
- #[webapi(method, length = 1, callback = audio_node_connect_callback)]
- connect: (),
- #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
- disconnect: (),
}
#[derive(WebApiFunctionTemplate)]
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs
index 9093e469b..68972c500 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs
@@ -14,39 +14,13 @@ const OUTPUTS: &str = "__moliAudioNodeOutputs";
const START_TIME: &str = "__moliAudioSourceStartTime";
const RENDERED_INPUT: &str = "__moliAudioNodeRenderedInput";
-#[derive(WebApiFunctionTemplate)]
-#[webapi(name = "AudioNode", enumerable)]
-struct AudioNodePrototypeDeclaration {
- #[webapi(accessor_property, getter = context_getter)]
- context: (),
-}
-
-pub(super) fn install<'s>(
- scope: &mut v8::PinScope<'s, '_, ()>,
- template: v8::Local<'s, v8::FunctionTemplate>,
-) {
- AudioNodePrototypeDeclaration::initialize_prototype_template(
- scope,
- template.prototype_template(scope),
- );
-}
-
-fn context_getter<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
-) {
- if let Some(context) = require_node_context(scope, args.this()) {
- rv.set(context.into());
- }
-}
-
pub(super) fn initialize_node<'s>(
scope: &mut v8::PinScope<'s, '_>,
node: v8::Local<'s, v8::Object>,
context: v8::Local<'s, v8::Object>,
) {
set_private_value(scope, node, CONTEXT, context.into());
+ node::initialize(scope, node);
for slot in [INPUTS, OUTPUTS] {
let array = v8::Array::new(scope, 0);
set_private_value(scope, node, slot, array.into());
@@ -68,10 +42,14 @@ pub(super) fn set_destination<'s>(
set_private_value(scope, context, DESTINATION, node.into());
}
-fn require_node_context<'s>(
+pub(super) fn require_node_context<'s>(
scope: &mut v8::PinScope<'s, '_>,
node: v8::Local<'s, v8::Object>,
) -> Option<v8::Local<'s, v8::Object>> {
+ if !web_api_interfaces::AudioNode::is_instance(scope, node) {
+ throw_type_error(scope, "Illegal invocation: expected an AudioNode.");
+ return None;
+ }
let context = web_audio_object_slot(scope, node, CONTEXT);
if context.is_none() {
throw_type_error(scope, "Illegal invocation: expected an AudioNode.");
@@ -110,7 +88,10 @@ fn add_edge<'s>(
set_private_value(scope, node, slot, array.into());
array
});
- let _ = array.set_index(scope, array.length(), other.into());
+ let index = v8::Integer::new_from_unsigned(scope, array.length())
+ .to_string(scope)
+ .expect("native array index should stringify");
+ let _ = array.create_data_property(scope, index.into(), other.into());
}
fn remove_edge<'s>(
@@ -142,6 +123,9 @@ pub(super) fn connect<'s>(
return None;
};
let target_context = require_node_context(scope, destination)?;
+ // Convert all Web IDL arguments before checking graph state or port ranges.
+ let output = args.get(1).uint32_value(scope)?;
+ let input = args.get(2).uint32_value(scope)?;
if context != target_context {
throw_dom_exception(
scope,
@@ -163,16 +147,14 @@ pub(super) fn connect<'s>(
return None;
}
// The remaining node implementations each expose a single output/input port.
- for index in 1..args.length().min(3) {
- if args.get(index).uint32_value(scope)? != 0 {
- throw_dom_exception(
- scope,
- "IndexSizeError",
- 1,
- "Audio node port index is out of range.",
- );
- return None;
- }
+ if output != 0 || input != 0 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Audio node port index is out of range.",
+ );
+ return None;
}
add_edge(scope, source, OUTPUTS, destination);
add_edge(scope, destination, INPUTS, source);
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/node.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/node.rs
new file mode 100644
index 000000000..4029462b0
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/node.rs
@@ -0,0 +1,297 @@
+//! Shared AudioNode bindings and native channel metadata for the partial backend.
+
+use super::*;
+
+const INPUT_COUNT: &str = "__moliAudioNodeInputCount";
+const OUTPUT_COUNT: &str = "__moliAudioNodeOutputCount";
+const CHANNEL_COUNT: &str = "__moliAudioNodeChannelCount";
+const CHANNEL_MODE: &str = "__moliAudioNodeChannelMode";
+const INTERPRETATION: &str = "__moliAudioNodeChannelInterpretation";
+const LISTENERS: &str = "__moliAudioNodeListeners";
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::AudioNode, enumerable, receiver)]
+struct AudioNodePrototypeDeclaration {
+ #[webapi(method, length = 1, callback = audio_node_connect_callback)]
+ connect: (),
+ #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
+ disconnect: (),
+ #[webapi(accessor_property, getter = context_getter)]
+ context: (),
+ #[webapi(accessor_property, getter = metadata_getter, data = v8str(scope, INPUT_COUNT))]
+ number_of_inputs: (),
+ #[webapi(accessor_property, getter = metadata_getter, data = v8str(scope, OUTPUT_COUNT))]
+ number_of_outputs: (),
+ #[webapi(accessor_property, getter = channel_count_getter, setter = channel_count_setter)]
+ channel_count: (),
+ #[webapi(accessor_property, getter = metadata_getter, setter = channel_mode_setter, data = v8str(scope, CHANNEL_MODE))]
+ channel_count_mode: (),
+ #[webapi(accessor_property, getter = metadata_getter, setter = interpretation_setter, data = v8str(scope, INTERPRETATION))]
+ channel_interpretation: (),
+}
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::AudioDestinationNode, enumerable, receiver)]
+struct DestinationPrototypeDeclaration {
+ #[webapi(accessor_property, getter = max_channel_count_getter)]
+ max_channel_count: (),
+}
+
+pub(super) fn install<'s>(
+ scope: &mut v8::PinScope<'s, '_, ()>,
+ template: v8::Local<'s, v8::FunctionTemplate>,
+ name: &str,
+) {
+ let prototype = template.prototype_template(scope);
+ match name {
+ "AudioNode" => {
+ AudioNodePrototypeDeclaration::initialize_prototype_template(scope, prototype)
+ }
+ "AudioDestinationNode" => {
+ DestinationPrototypeDeclaration::initialize_prototype_template(scope, prototype)
+ }
+ _ => {}
+ }
+}
+
+pub(super) fn initialize<'s>(scope: &mut v8::PinScope<'s, '_>, node: v8::Local<'s, v8::Object>) {
+ let source = web_api_interfaces::OscillatorNode::is_instance(scope, node);
+ let destination = web_api_interfaces::AudioDestinationNode::is_instance(scope, node);
+ let compressor = web_api_interfaces::DynamicsCompressorNode::is_instance(scope, node);
+ set_web_audio_number_slot(scope, node, INPUT_COUNT, if source { 0.0 } else { 1.0 });
+ // The existing terminal destination backend has no output port. Capturing
+ // its output, as proposed by Web Audio 1.1, needs a separate backend change.
+ set_web_audio_number_slot(
+ scope,
+ node,
+ OUTPUT_COUNT,
+ if destination { 0.0 } else { 1.0 },
+ );
+ set_web_audio_number_slot(scope, node, CHANNEL_COUNT, 2.0);
+ let mode = if destination {
+ "explicit"
+ } else if compressor {
+ "clamped-max"
+ } else {
+ "max"
+ };
+ set_private_value(scope, node, CHANNEL_MODE, v8str(scope, mode).into());
+ set_private_value(scope, node, INTERPRETATION, v8str(scope, "speakers").into());
+ super::super::media_queries::mark_simple_event_target_slot(scope, node, LISTENERS);
+}
+
+fn context_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if let Some(context) = graph::require_node_context(scope, args.this()) {
+ rv.set(context.into());
+ }
+}
+
+fn metadata_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ let slot = args.data().to_rust_string_lossy(scope);
+ if let Some(value) = get_private_value(scope, args.this(), &slot) {
+ rv.set(value);
+ }
+}
+
+fn offline_destination_channel_count<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+) -> Option<f64> {
+ if !web_api_interfaces::AudioDestinationNode::is_instance(scope, node) {
+ return None;
+ }
+ let context = graph::require_node_context(scope, node)?;
+ web_audio_number_slot(scope, context, OFFLINE_AUDIO_CHANNEL_COUNT_SLOT)
+}
+
+fn channel_count_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if let Some(count) = offline_destination_channel_count(scope, args.this())
+ .or_else(|| web_audio_number_slot(scope, args.this(), CHANNEL_COUNT))
+ {
+ rv.set(v8::Number::new(scope, count).into());
+ }
+}
+
+fn max_channel_count_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ // The partial realtime backend currently advertises two channels.
+ let count = offline_destination_channel_count(scope, args.this()).unwrap_or(2.0);
+ rv.set(v8::Number::new(scope, count).into());
+}
+
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.channelCount")]
+struct ChannelCountArgs {
+ #[webidl(required)]
+ value: u32,
+}
+
+fn channel_count_setter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ let Some(parsed) = webidl::parse_args::<ChannelCountArgs>(scope, &args) else {
+ return;
+ };
+ let node = args.this();
+ let value = parsed.value;
+ if let Some(count) = offline_destination_channel_count(scope, node) {
+ if f64::from(value) != count {
+ throw_dom_exception(
+ scope,
+ "NotSupportedError",
+ 9,
+ "Offline destination channel count cannot be changed.",
+ );
+ }
+ return;
+ }
+ if value == 0 {
+ throw_dom_exception(
+ scope,
+ "NotSupportedError",
+ 9,
+ "Audio nodes require at least one channel.",
+ );
+ return;
+ }
+ if web_api_interfaces::AudioDestinationNode::is_instance(scope, node) && value > 2 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Destination channel count exceeds the backend limit.",
+ );
+ return;
+ }
+ if value > 32
+ || (web_api_interfaces::DynamicsCompressorNode::is_instance(scope, node) && value > 2)
+ {
+ throw_dom_exception(
+ scope,
+ "NotSupportedError",
+ 9,
+ "Audio node channel count exceeds its limit.",
+ );
+ return;
+ }
+ set_web_audio_number_slot(scope, node, CHANNEL_COUNT, f64::from(value));
+}
+
+#[derive(Clone, Copy, webidl::WebIdlEnum)]
+#[webidl(name = "ChannelCountMode")]
+enum ChannelCountMode {
+ #[webidl(token = "max")]
+ Max,
+ #[webidl(token = "clamped-max")]
+ ClampedMax,
+ #[webidl(token = "explicit")]
+ Explicit,
+}
+
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.channelCountMode")]
+struct ChannelModeArgs {
+ #[webidl(required)]
+ value: String,
+}
+
+fn channel_mode_setter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ let Some(parsed) = webidl::parse_args::<ChannelModeArgs>(scope, &args) else {
+ return;
+ };
+ // Enum attribute setters convert to DOMString and ignore unknown tokens;
+ // enum operation arguments instead throw on an unknown token.
+ let Some(value) = <ChannelCountMode as webidl::WebIdlEnum>::parse_token(&parsed.value) else {
+ return;
+ };
+ let node = args.this();
+ if offline_destination_channel_count(scope, node).is_some()
+ && !matches!(value, ChannelCountMode::Explicit)
+ {
+ throw_dom_exception(
+ scope,
+ "InvalidStateError",
+ 11,
+ "Offline destination channel count mode cannot be changed.",
+ );
+ return;
+ }
+ if web_api_interfaces::DynamicsCompressorNode::is_instance(scope, node)
+ && matches!(value, ChannelCountMode::Max)
+ {
+ throw_dom_exception(
+ scope,
+ "NotSupportedError",
+ 9,
+ "Dynamics compressor does not support max channel count mode.",
+ );
+ return;
+ }
+ let value = match value {
+ ChannelCountMode::Max => "max",
+ ChannelCountMode::ClampedMax => "clamped-max",
+ ChannelCountMode::Explicit => "explicit",
+ };
+ set_private_value(scope, node, CHANNEL_MODE, v8str(scope, value).into());
+}
+
+#[derive(Clone, Copy, webidl::WebIdlEnum)]
+#[webidl(name = "ChannelInterpretation")]
+enum ChannelInterpretation {
+ #[webidl(token = "speakers")]
+ Speakers,
+ #[webidl(token = "discrete")]
+ Discrete,
+}
+
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.channelInterpretation")]
+struct InterpretationArgs {
+ #[webidl(required)]
+ value: String,
+}
+
+fn interpretation_setter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ let Some(parsed) = webidl::parse_args::<InterpretationArgs>(scope, &args) else {
+ return;
+ };
+ let Some(value) = <ChannelInterpretation as webidl::WebIdlEnum>::parse_token(&parsed.value)
+ else {
+ return;
+ };
+ let value = match value {
+ ChannelInterpretation::Speakers => "speakers",
+ ChannelInterpretation::Discrete => "discrete",
+ };
+ set_private_value(
+ scope,
+ args.this(),
+ INTERPRETATION,
+ v8str(scope, value).into(),
+ );
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.js b/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.js
new file mode 100644
index 000000000..a3a64c206
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.js
@@ -0,0 +1,76 @@
+(async()=>{
+ const rows=[],assert=(ok,message)=>{if(!ok)throw Error(message);},check=async(name,fn)=>{try{await fn();rows.push({name,pass:true});}catch(error){rows.push({name,pass:false,message:String(error)});}};
+ const realms=[['main',window],['child',document.getElementById('child').contentWindow]];
+ for(const [label,w] of realms){
+ const context=new w.AudioContext(),prototype=w.AudioNode?.prototype;
+ try {
+ await check(label+'/AudioNode',()=>{
+ const C=w.AudioNode,d=Object.getOwnPropertyDescriptor(w,'AudioNode');
+ assert(typeof C==='function'&&C.name==='AudioNode'&&C.length===0,'constructor');
+ assert(d.writable&&d.configurable&&!d.enumerable,'global descriptor');
+ assert(Object.getPrototypeOf(C)===w.EventTarget&&Object.getPrototypeOf(C.prototype)===w.EventTarget.prototype,'native EventTarget inheritance');
+ assert(C.prototype.constructor===C,'prototype constructor');
+ for(const call of [()=>C(),()=>new C()]){let error;try{call();}catch(e){error=e;}assert(error instanceof w.TypeError,'illegal constructor in callee realm');}
+ });
+ const nodes=[['destination',context.destination,w.AudioDestinationNode,1,0,2,'explicit'],['oscillator',context.createOscillator(),w.OscillatorNode,0,1,2,'max'],['analyser',context.createAnalyser(),w.AnalyserNode,1,1,2,'max'],['compressor',context.createDynamicsCompressor(),w.DynamicsCompressorNode,1,1,2,'clamped-max'],['biquad',context.createBiquadFilter(),w.BiquadFilterNode,1,1,2,'max']];
+ for(const [name,node,C,inputs,outputs,count,mode] of nodes){
+ await check(label+'/'+name+'/inheritance',()=>{
+ assert(node instanceof C&&node instanceof w.AudioNode&&node instanceof w.EventTarget,'native node identity');
+ assert((name==='oscillator'?prototype.isPrototypeOf(C.prototype):Object.getPrototypeOf(C.prototype)===prototype),'native interface hierarchy');
+ for(const key of ['context','connect','disconnect'])assert(!Object.hasOwn(node,key)&&!Object.hasOwn(C.prototype,key),'common property owner');
+ assert(node.context===context&&node.numberOfInputs===inputs&&node.numberOfOutputs===outputs&&node.channelCount===count&&node.channelCountMode===mode&&node.channelInterpretation==='speakers','channel defaults');
+ });
+ await check(label+'/'+name+'/EventTarget',()=>{
+ let calls=0;const listener=function(event){assert(this===node&&event.target===node,'event identity');calls++;};
+ w.EventTarget.prototype.addEventListener.call(node,'test',listener);
+ w.EventTarget.prototype.dispatchEvent.call(node,new w.Event('test'));
+ w.EventTarget.prototype.removeEventListener.call(node,'test',listener);
+ node.dispatchEvent(new w.Event('test'));assert(calls===1,'shared native listener storage');
+ });
+ }
+ const real=nodes[2][1],entries=[['context','get'],['numberOfInputs','get'],['numberOfOutputs','get'],['channelCount','get'],['channelCount','set'],['channelCountMode','get'],['channelCountMode','set'],['channelInterpretation','get'],['channelInterpretation','set'],['connect','value'],['disconnect','value']];
+ for(const [name,kind] of entries){await check(label+'/'+name+'/'+kind,()=>{
+ const d=Object.getOwnPropertyDescriptor(prototype,name),fn=d[kind];
+ assert(typeof fn==='function'&&d.enumerable&&d.configurable,'shared descriptor');
+ assert(fn.name===(kind==='value'?'':kind+' ')+name&&fn.length===(kind==='set'||name==='connect'?1:0),'member name and length');
+ let conversions=0,traps=0;const ignored={valueOf(){conversions++;throw Error('conversion');},toString(){conversions++;throw Error('conversion');}},trap=()=>{traps++;throw Error('author trap');},revoked=Proxy.revocable(real,{});revoked.revoke();
+ for(const receiver of [null,{},prototype,Object.create(real),new Proxy(real,{get:trap,getPrototypeOf:trap}),revoked.proxy]){
+ let error;try{fn.call(receiver,ignored,ignored,ignored);}catch(e){error=e;}assert(error instanceof w.TypeError,'callee realm rejects unbranded receiver');
+ }
+ assert(conversions===0&&traps===0,'brand precedes conversions and author hooks');
+ });}
+ await check(label+'/channels',()=>{
+ const node=nodes[2][1],compressor=nodes[3][1];
+ node.channelCount={valueOf(){return 3.75;}};node.channelCountMode='explicit';node.channelInterpretation='discrete';
+ assert(node.channelCount===3&&node.channelCountMode==='explicit'&&node.channelInterpretation==='discrete','native channel writes');
+ for(const [key,value,type] of [['channelCount',0,'NotSupportedError'],['channelCount',33,'NotSupportedError']]){
+ let error;try{node[key]=value;}catch(e){error=e;}assert(error?.name===type,'invalid '+key);}
+ for(const key of ['channelCountMode','channelInterpretation']){
+ const before=node[key];node[key]='invalid';assert(node[key]===before,'ignore invalid enum attribute');
+ const sentinel={};let error;try{node[key]={toString(){throw sentinel;}};}catch(e){error=e;}assert(error===sentinel,'preserve enum string conversion exception');
+ }
+ for(const [key,value] of [['channelCount',3],['channelCountMode','max']]){let error;try{compressor[key]=value;}catch(e){error=e;}assert(error?.name==='NotSupportedError','compressor limit');}
+ });
+ await check(label+'/cross-realm',()=>{
+ const foreign=new window.AudioContext();try {
+ const source=foreign.createOscillator(),target=foreign.createAnalyser();
+ assert(Object.getOwnPropertyDescriptor(prototype,'context').get.call(source)===foreign,'genuine foreign node');
+ assert(prototype.connect.call(source,target)===target,'borrowed native method');prototype.disconnect.call(source,target);
+ Object.getOwnPropertyDescriptor(prototype,'channelCount').set.call(target,3);assert(target.channelCount===3,'borrowed native setter');
+ let error;try{Object.getOwnPropertyDescriptor(prototype,'channelCountMode').set.call(target,Symbol());}catch(e){error=e;}
+ assert(error instanceof w.TypeError,'conversion exception in callee realm');
+ }finally{foreign.close();}
+ });
+ await check(label+'/graph',()=>{
+ const source=nodes[1][1],target=nodes[2][1];
+ assert(prototype.connect.call(source,target)===target,'connect returns target');prototype.disconnect.call(source,target);
+ let error;try{prototype.disconnect.call(source,target);}catch(e){error=e;}assert(error?.name==='InvalidAccessError','missing edge');
+ const sentinel={};let conversions=[];try{prototype.connect.call(source,target,{valueOf(){conversions.push('output');return 4;}},{valueOf(){conversions.push('input');throw sentinel;}});}catch(e){error=e;}
+ assert(error===sentinel&&conversions.join()==='output,input','convert all ports before range validation');
+ const other=new w.AudioContext();try{try{prototype.connect.call(source,other.destination,0,{valueOf(){throw sentinel;}});}catch(e){error=e;}assert(error===sentinel,'conversion precedes context check');}finally{other.close();}
+ Object.setPrototypeOf(source,null);assert(Object.getOwnPropertyDescriptor(prototype,'context').get.call(source)===context,'native identity independent of prototype');
+ });
+ }finally{await context.close();}
+ }
+ globalThis.__nodeReplacementResults={rows,passed:rows.filter(r=>r.pass).length,total:rows.length,failures:rows.filter(r=>!r.pass)};return rows.every(r=>r.pass);
+})()
diff --git a/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.rs b/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.rs
new file mode 100644
index 000000000..06bf49de3
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.rs
@@ -0,0 +1,31 @@
+use super::*;
+
+#[test]
+fn audio_nodes_share_native_inheritance_channels_and_receiver_validation() {
+ let mut vm = new_storage_page_task_executor_test_vm("https://audio-node-interfaces.test/");
+ vm.eval("document.body.innerHTML = '<iframe id=child></iframe>'")
+ .unwrap();
+ vm.eval(&format!("({}).then(value => globalThis.__audioNodeDone = value, error => globalThis.__audioNodeDone = String(error));", include_str!("audio_node_interfaces.js"))).unwrap();
+ assert_eq!(
+ vm.eval_after_selected_page_tasks("JSON.stringify(__nodeReplacementResults.failures)")
+ .unwrap(),
+ "[]"
+ );
+ assert_eq!(vm.eval("__audioNodeDone").unwrap(), "true");
+}
+
+#[test]
+fn offline_destination_channels_come_from_native_context_state() {
+ let mut vm = new_storage_page_task_executor_test_vm("https://audio-node-channels.test/");
+ assert_eq!(vm.eval(r#"(() => {
+ const context = new OfflineAudioContext(3,128,48000), node = context.destination;
+ context.channelCount = 9;
+ Object.defineProperty(context,'destination',{value:{}});
+ if (node.context !== context || node.channelCount !== 3 || node.maxChannelCount !== 3) throw Error('native context channels');
+ let error;try {node.channelCount = 2;} catch(e){error=e;}
+ if (error?.name !== 'NotSupportedError' || node.channelCount !== 3) throw Error('offline count cannot change');
+ try {node.channelCountMode = 'max';} catch(e){error=e;}
+ if (error?.name !== 'InvalidStateError' || node.channelCountMode !== 'explicit') throw Error('offline mode cannot change');
+ return true;
+ })()"#).unwrap(), "true");
+}
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 04a35a608..e62648ef0 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
@@ -2486,7 +2486,7 @@ fn web_audio_declared_objects_keep_brands_and_hidden_properties() {
}
#[test]
-fn web_audio_declared_fixed_own_methods_keep_descriptors() {
+fn web_audio_methods_preserve_own_and_shared_prototype_descriptors() {
let mut vm = new_storage_test_vm("https://audio-declared-own-methods.test/");
let result = vm
@@ -2520,9 +2520,11 @@ fn web_audio_declared_fixed_own_methods_keep_descriptors() {
analyserKeys: Object.keys(analyser).join(","),
paramKeys: Object.keys(osc.frequency).join(","),
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"].map(name => describe(analyser, name)),
+ graph: ["connect", "disconnect"].map(name => describe(AudioNode.prototype, name)),
+ shared: [ctx.destination, osc, comp, analyser].every(node =>
+ ["connect", "disconnect"].every(name =>
+ !Object.hasOwn(node, name) && node[name] === AudioNode.prototype[name])),
+ oscStart: describe(osc, "start"),
param: describe(osc.frequency, "setValueAtTime")
});
})()
@@ -2532,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"],"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"],"graph":["connect:true:true:true:function:connect:1","disconnect:true:true:true:function:disconnect:0"],"shared":true,"oscStart":"start:false:true:true:function:start:1","param":"setValueAtTime:false:true:true:function:setValueAtTime:2"}"#
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index d2cf0ea49..3c9a79968 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -2130,6 +2130,7 @@ fn decode_png_dimensions_from_data_url(data_url: &str) -> (u32, u32) {
(info.width, info.height)
}
+mod audio_node_interfaces;
mod blob_response_headers;
mod browser_api;
mod cache_interfaces;
diff --git a/moli-renderer-v8/src/web_api_interfaces.rs b/moli-renderer-v8/src/web_api_interfaces.rs
index 5d10ff52d..7d157729e 100644
--- a/moli-renderer-v8/src/web_api_interfaces.rs
+++ b/moli-renderer-v8/src/web_api_interfaces.rs
@@ -23,7 +23,7 @@ interfaces! {
AbortController;
AbortSignal: EventTarget;
AbstractRange;
- AnalyserNode;
+ AnalyserNode: AudioNode;
Animation;
AnimationEffect;
AnimationEvent: Event;
@@ -33,15 +33,16 @@ interfaces! {
Audio;
AudioBuffer;
AudioContext: BaseAudioContext;
- AudioDestinationNode;
+ AudioDestinationNode: AudioNode;
+ AudioNode: EventTarget;
AudioParam;
Worklet;
AudioWorklet: Worklet;
- AudioWorkletNode;
+ AudioWorkletNode: AudioNode;
BaseAudioContext: EventTarget;
BatteryManager;
BeforeUnloadEvent: Event;
- BiquadFilterNode;
+ BiquadFilterNode: AudioNode;
Blob;
BlobEvent: Event;
BroadcastChannel: EventTarget;
@@ -138,7 +139,7 @@ interfaces! {
DocumentTimeline: AnimationTimeline;
DocumentType: Node;
DragEvent: MouseEvent;
- DynamicsCompressorNode;
+ DynamicsCompressorNode: AudioNode;
Element: Node;
ElementInternals;
EncodedVideoChunk;
@@ -340,7 +341,7 @@ interfaces! {
OffscreenCanvas;
OffscreenCanvasRenderingContext2D;
Option;
- OscillatorNode;
+ OscillatorNode: AudioNode;
PageTransitionEvent: Event;
Path2D;
PaymentResponse: EventTarget;
diff --git a/moli-renderer-v8/src/worker/thread/tests/postmessage.rs b/moli-renderer-v8/src/worker/thread/tests/postmessage.rs
index 7e24a45a5..3d8ca9cf4 100644
--- a/moli-renderer-v8/src/worker/thread/tests/postmessage.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/postmessage.rs
@@ -3111,3 +3111,17 @@ async fn worker_rectangle_clones_use_native_prototypes_and_message_channel_deliv
);
assert_eq!(recv_post_json(&mut handle).await, r#""passed""#);
}
+
+#[tokio::test]
+async fn worker_does_not_expose_window_audio_node_interface() {
+ ensure_v8();
+ let mut handle = spawn_worker(
+ "postMessage({AudioNode:'AudioNode' in self});close();".to_owned(),
+ "https://audio-node-worker.test/worker.js".into(),
+ );
+ let message = timeout(TIMEOUT, handle.recv())
+ .await
+ .expect("timed out")
+ .expect("channel closed");
+ assert_eq!(expect_post_json(message), r#"{"AudioNode":false}"#);
+}