Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(webaudio): derive AudioNode graph argument validation

ldm03b982427c4a7
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 68972c500..5f6091e73 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
@@ -42,14 +42,12 @@ pub(super) fn set_destination<'s>(
set_private_value(scope, context, DESTINATION, node.into());
}
-pub(super) fn require_node_context<'s>(
+// Receivers are validated by the AudioNode declaration; destination arguments
+// are validated by the derived parsers below. This helper only reads graph state.
+pub(super) fn 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.");
@@ -109,23 +107,44 @@ fn remove_edge<'s>(
set_private_value(scope, node, slot, array.into());
}
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.connect")]
+struct ConnectArgs<'s> {
+ #[webidl(required, interface = web_api_interfaces::AudioNode)]
+ destination: v8::Local<'s, v8::Object>,
+ #[webidl(default = 0)]
+ output: u32,
+ #[webidl(default = 0)]
+ input: u32,
+}
+
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.disconnect")]
+struct DisconnectNodeArgs<'s> {
+ #[webidl(required, interface = web_api_interfaces::AudioNode)]
+ destination: v8::Local<'s, v8::Object>,
+ #[webidl(default = 0)]
+ output: u32,
+ #[webidl(default = 0)]
+ input: u32,
+}
+
+#[derive(webidl::WebIdlArgs)]
+#[webidl(prefix = "AudioNode.disconnect")]
+struct DisconnectOutputArgs {
+ #[webidl(required)]
+ output: u32,
+}
+
pub(super) fn connect<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: &v8::FunctionCallbackArguments<'s>,
) -> Option<v8::Local<'s, v8::Object>> {
+ let parsed = webidl::parse_args::<ConnectArgs>(scope, args)?;
let source = args.this();
- let context = require_node_context(scope, source)?;
- let Ok(destination) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
- throw_type_error(
- scope,
- "AudioNode.connect requires an AudioNode destination.",
- );
- 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)?;
+ let destination = parsed.destination;
+ let context = node_context(scope, source)?;
+ let target_context = node_context(scope, destination)?;
if context != target_context {
throw_dom_exception(
scope,
@@ -147,7 +166,7 @@ pub(super) fn connect<'s>(
return None;
}
// The remaining node implementations each expose a single output/input port.
- if output != 0 || input != 0 {
+ if parsed.output != 0 || parsed.input != 0 {
throw_dom_exception(
scope,
"IndexSizeError",
@@ -170,16 +189,51 @@ pub(super) fn disconnect<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: &v8::FunctionCallbackArguments<'s>,
) {
+ // Only the one-argument overload can select an output number. Platform
+ // interface identity distinguishes it from a destination; ordinary objects
+ // follow unsigned-long conversion, including their numeric coercion hooks.
+ let destination_overload = if args.length() == 1 {
+ match v8::Local::<v8::Object>::try_from(args.get(0)) {
+ Ok(object) => {
+ web_api_interfaces::AudioNode::is_instance(scope, object)
+ || web_api_interfaces::AudioParam::is_instance(scope, object)
+ }
+ Err(_) => false,
+ }
+ } else {
+ args.length() > 1
+ };
+ let (selected, output, input) = if destination_overload {
+ let Some(parsed) = webidl::parse_args::<DisconnectNodeArgs>(scope, args) else {
+ return;
+ };
+ (Some(parsed.destination), parsed.output, parsed.input)
+ } else if args.length() == 1 {
+ let Some(parsed) = webidl::parse_args::<DisconnectOutputArgs>(scope, args) else {
+ return;
+ };
+ (None, parsed.output, 0)
+ } else {
+ (None, 0, 0)
+ };
+ // Finish conversion before observing graph state, validating ports, or
+ // removing any edge. A later conversion exception must leave edges intact.
let source = args.this();
- let Some(context) = require_node_context(scope, source) else {
+ let Some(context) = node_context(scope, source) else {
return;
};
+ if output != 0 || input != 0 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Audio node port index is out of range.",
+ );
+ return;
+ }
let outputs = objects(scope, source, OUTPUTS);
- let selected = if args.length() == 0 {
- None
- } else if args.get(0).is_object() {
- let destination = v8::Local::<v8::Object>::try_from(args.get(0)).unwrap();
- if require_node_context(scope, destination).is_none() {
+ if let Some(destination) = selected {
+ if node_context(scope, destination).is_none() {
return;
}
if !outputs.contains(&destination) {
@@ -191,35 +245,6 @@ pub(super) fn disconnect<'s>(
);
return;
}
- Some(destination)
- } else {
- let Some(port) = args.get(0).uint32_value(scope) else {
- return;
- };
- if port != 0 {
- throw_dom_exception(
- scope,
- "IndexSizeError",
- 1,
- "Audio node port index is out of range.",
- );
- return;
- }
- None
- };
- for index in 1..args.length().min(3) {
- let Some(port) = args.get(index).uint32_value(scope) else {
- return;
- };
- if port != 0 {
- throw_dom_exception(
- scope,
- "IndexSizeError",
- 1,
- "Audio node port index is out of range.",
- );
- return;
- }
}
for destination in outputs {
if selected.is_none_or(|selected| selected == destination) {
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
index 4029462b0..81891a825 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/node.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/node.rs
@@ -85,7 +85,7 @@ fn context_getter<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
- if let Some(context) = graph::require_node_context(scope, args.this()) {
+ if let Some(context) = graph::node_context(scope, args.this()) {
rv.set(context.into());
}
}
@@ -108,7 +108,7 @@ fn offline_destination_channel_count<'s>(
if !web_api_interfaces::AudioDestinationNode::is_instance(scope, node) {
return None;
}
- let context = graph::require_node_context(scope, node)?;
+ let context = graph::node_context(scope, node)?;
web_audio_number_slot(scope, context, OFFLINE_AUDIO_CHANNEL_COUNT_SLOT)
}
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
index a3a64c206..21746833f 100644
--- a/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.js
+++ b/moli-renderer-v8/src/script_vm/tests/audio_node_interfaces.js
@@ -61,6 +61,59 @@
assert(error instanceof w.TypeError,'conversion exception in callee realm');
}finally{foreign.close();}
});
+ await check(label+'/connect/destination-conversion',()=>{
+ const source=nodes[1][1],target=nodes[2][1];
+ let conversions=0,traps=0;
+ const port={valueOf(){conversions++;return 0;}},trap=()=>{traps++;throw Error('destination trap');};
+ const revoked=Proxy.revocable(target,{});revoked.revoke();
+ for(const destination of [undefined,null,{},prototype,Object.create(target),new Proxy(target,{get:trap,getPrototypeOf:trap}),revoked.proxy]){
+ let error;try{prototype.connect.call(source,destination,port,port);}catch(e){error=e;}
+ assert(error instanceof w.TypeError,'derived destination check uses callee TypeError');
+ }
+ let error;try{prototype.connect.call(source);}catch(e){error=e;}
+ assert(error instanceof w.TypeError,'required destination arity');
+ assert(conversions===0&&traps===0,'destination identity precedes port conversion without Proxy hooks');
+ for(const ports of [[],[undefined,undefined],[null,null],[2**32,2**32+0.75]]){
+ assert(prototype.connect.call(source,target,...ports)===target,'derived defaults and unsigned-long conversion');
+ prototype.disconnect.call(source,target);
+ }
+ });
+ await check(label+'/disconnect/conversion-and-atomicity',()=>{
+ const source=nodes[1][1],target=nodes[2][1],unconnected=context.createAnalyser(),sentinel={};
+ const expectMissing=()=>{let error;try{prototype.disconnect.call(source,target);}catch(e){error=e;}assert(error?.name==='InvalidAccessError','connection removed exactly once');};
+ prototype.connect.call(source,target);
+ for(const destination of [target,unconnected]){
+ const conversions=[];let error;
+ try{prototype.disconnect.call(source,destination,{valueOf(){conversions.push('output');return 4;}},{valueOf(){conversions.push('input');throw sentinel;}});}catch(e){error=e;}
+ assert(error===sentinel&&conversions.join()==='output,input','all ports convert before ranges and graph state');
+ }
+ prototype.disconnect.call(source,target);expectMissing();
+ prototype.connect.call(source,target);
+ let conversions=0,error;
+ try{prototype.disconnect.call(source,0,{valueOf(){conversions++;return 0;}});}catch(e){error=e;}
+ assert(error instanceof w.TypeError&&conversions===0,'two-argument overload requires a native destination before ports');
+ prototype.disconnect.call(source,target);expectMissing();
+ for(const output of [0,undefined,null,new w.Number(0),{valueOf(){return 2**32;}}]){
+ prototype.connect.call(source,target);
+ assert(prototype.disconnect.call(source,output)===undefined,'one-argument output overload converts numbers and ordinary objects');
+ expectMissing();
+ }
+ prototype.connect.call(source,target);
+ try{prototype.disconnect.call(source,{valueOf(){throw sentinel;}});}catch(e){error=e;}
+ assert(error===sentinel,'numeric overload preserves coercion exception');
+ prototype.disconnect.call(source,target);expectMissing();
+ });
+ await check(label+'/disconnect/destination-conversion',()=>{
+ const source=nodes[1][1],target=nodes[2][1];
+ let conversions=0,traps=0;
+ const port={valueOf(){conversions++;return 0;}},trap=()=>{traps++;throw Error('destination trap');};
+ const revoked=Proxy.revocable(target,{});revoked.revoke();
+ for(const destination of [undefined,null,{},prototype,Object.create(target),new Proxy(target,{get:trap,getPrototypeOf:trap}),revoked.proxy]){
+ let error;try{prototype.disconnect.call(source,destination,port,port);}catch(e){error=e;}
+ assert(error instanceof w.TypeError,'derived disconnect destination uses callee TypeError');
+ }
+ assert(conversions===0&&traps===0,'disconnect destination validation precedes ports without author hooks');
+ });
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);