1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Platform-specific extensions for [`web-thread`](crate) on the Web platform
//! to spawn and use audio worklets. See
//! [`BaseAudioContextExt::audio_worklet_node()`] for a usage example.

use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::future::Future;
use std::panic::RefUnwindSafe;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{any, fmt, io};

use js_sys::{Array, Iterator, Object};
#[cfg(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
))]
use web_sys::{AudioWorkletGlobalScope, BaseAudioContext};
use web_sys::{AudioWorkletNode, AudioWorkletNodeOptions, AudioWorkletProcessor};

#[cfg(any(feature = "message", docsrs))]
use super::message::MessageSend;
#[cfg(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
))]
use crate::thread::audio_worklet;
use crate::Thread;

#[cfg(not(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
)))]
mod audio_worklet {
	pub(super) struct AudioWorkletHandle;
	pub(super) struct RegisterThreadFuture;
}
#[cfg(not(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
)))]
mod web_sys {
	pub(super) struct AudioWorkletNodeOptions;
	pub(super) struct AudioWorkletProcessor;
	pub(super) struct AudioWorkletNode;
}
#[cfg(not(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
)))]
mod js_sys {
	pub(super) struct Array;
	pub(super) struct Iterator;
	pub(super) struct Object;
}

/// Extension for [`BaseAudioContext`].
#[cfg_attr(
	not(all(
		target_family = "wasm",
		target_os = "unknown",
		feature = "audio-worklet"
	)),
	doc = "",
	doc = "[`BaseAudioContext`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.BaseAudioContext.html"
)]
pub trait BaseAudioContextExt {
	/// Registers a thread at this [`BaseAudioContext`].
	///
	/// # Notes
	///
	/// Unfortunately there is currently no way to determine when the thread has
	/// fully shutdown. So this will leak memory unless
	/// [`AudioWorkletHandle::release()`] is called.
	///
	/// # Errors
	///
	/// - If a thread was already registered at this [`BaseAudioContext`].
	/// - If the [`BaseAudioContext`] is [`closed`].
	/// - If the main thread does not support spawning threads, see
	///   [`has_spawn_support()`](super::has_spawn_support).
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// use web_sys::AudioContext;
	/// use web_thread::web::audio_worklet::BaseAudioContextExt;
	///
	/// let context = AudioContext::new().unwrap();
	/// context.clone().register_thread(
	/// 	None,
	/// 	|| {
	/// 		// Do work.
	/// 	},
	/// ).await.unwrap();
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	///
	/// [`closed`]: https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/state#closed
	#[cfg_attr(
		not(all(
			target_family = "wasm",
			target_os = "unknown",
			feature = "audio-worklet"
		)),
		doc = "[`BaseAudioContext`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.BaseAudioContext.html"
	)]
	fn register_thread<F>(self, stack_size: Option<usize>, f: F) -> RegisterThreadFuture
	where
		F: 'static + FnOnce() + Send;

	/// Registers a thread at this [`BaseAudioContext`].
	///
	/// # Notes
	///
	/// Unfortunately there is currently no way to determine when the thread has
	/// fully shutdown. So this will leak memory unless
	/// [`AudioWorkletHandle::release()`] is called.
	///
	/// # Errors
	///
	/// - If a thread was already registered at this [`BaseAudioContext`].
	/// - If the [`BaseAudioContext`] is [`closed`].
	/// - If the main thread does not support spawning threads, see
	///   [`has_spawn_support()`](super::has_spawn_support).
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// use js_sys::ArrayBuffer;
	/// use web_sys::AudioContext;
	/// use web_thread::web::audio_worklet::BaseAudioContextExt;
	/// use web_thread::web::message::TransferableWrapper;
	///
	/// let context = AudioContext::new().unwrap();
	/// let buffer = TransferableWrapper(ArrayBuffer::new(1024));
	/// context
	/// 	.clone()
	/// 	.register_thread_with_message(
	/// 		None,
	/// 		|message| {
	/// 			let buffer: ArrayBuffer = message.0;
	/// 			// Do work.
	/// #           let _ = buffer;
	/// 		},
	/// 		buffer,
	/// 	)
	/// 	.await
	/// 	.unwrap();
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	///
	/// [`closed`]: https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/state#closed
	#[cfg_attr(
		not(all(
			target_family = "wasm",
			target_os = "unknown",
			feature = "audio-worklet"
		)),
		doc = "[`BaseAudioContext`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.BaseAudioContext.html"
	)]
	#[cfg(any(feature = "message", docsrs))]
	fn register_thread_with_message<F, M>(
		self,
		stack_size: Option<usize>,
		f: F,
		message: M,
	) -> RegisterThreadFuture
	where
		F: 'static + FnOnce(M) + Send,
		M: 'static + MessageSend;

	/// Instantiates a [`AudioWorkletProcessor`]. No `data` will be delivered if
	/// `name` corresponds to a different type registered with
	/// [`AudioWorkletGlobalScopeExt::register_processor_ext()`]. If `name`
	/// corresponds to a [`AudioWorkletProcessor`] not registered through
	/// [`AudioWorkletGlobalScopeExt::register_processor_ext()`], it will leak
	/// `data`.
	///
	/// # Errors
	///
	/// - If [`Self::register_thread()`] was not called on this context yet.
	/// - If [`new AudioWorkletNode`] throws an exception.
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// # use wasm_bindgen::JsCast;
	/// use web_sys::{AudioContext, AudioWorkletGlobalScope, AudioWorkletNodeOptions, AudioWorkletProcessor};
	/// use web_thread::web::{self, YieldTime};
	/// use web_thread::web::audio_worklet::{AudioWorkletGlobalScopeExt, BaseAudioContextExt, ExtendAudioWorkletProcessor};
	///
	/// /// Example [`AudioWorkletProcessor`].
	/// struct TestProcessor;
	///
	/// impl ExtendAudioWorkletProcessor for TestProcessor {
	/// 	type Data = String;
	///
	/// 	fn new(
	/// 		_: AudioWorkletProcessor,
	/// 		data: Option<Self::Data>,
	/// 		_: AudioWorkletNodeOptions,
	/// 	) -> Self {
	/// 		assert_eq!(data.as_deref(), Some("test"));
	/// 		Self
	/// 	}
	/// }
	///
	/// let context = AudioContext::new().unwrap();
	/// let (sender, receiver) = async_channel::bounded(1);
	/// context.clone().register_thread(
	/// 	None,
	/// 	move || {
	/// 		let global: AudioWorkletGlobalScope = js_sys::global().unchecked_into();
	/// 		global
	/// 			.register_processor_ext::<TestProcessor>("test")
	/// 			.unwrap();
	/// 		sender.try_send(()).unwrap();
	/// 	},
	/// ).await.unwrap();
	///
	/// // Wait until processor is registered.
	/// receiver.recv().await.unwrap();
	/// web::yield_now_async(YieldTime::UserBlocking).await;
	///
	/// let node = context.audio_worklet_node::<TestProcessor>("test", String::from("test"), None).unwrap();
	/// # let _ = node;
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	///
	/// [`new AudioWorkletNode`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletNode/AudioWorkletNode
	/// [`AudioWorkletProcessor`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor
	fn audio_worklet_node<P>(
		&self,
		name: &str,
		data: P::Data,
		options: Option<&AudioWorkletNodeOptions>,
	) -> Result<AudioWorkletNode, AudioWorkletNodeError<P>>
	where
		P: 'static + ExtendAudioWorkletProcessor;
}

#[cfg(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
))]
impl<T> BaseAudioContextExt for T
where
	BaseAudioContext: From<T>,
	T: AsRef<BaseAudioContext>,
{
	fn register_thread<F>(
		self,
		stack_size: Option<usize>,
		#[allow(clippy::min_ident_chars)] f: F,
	) -> RegisterThreadFuture
	where
		F: 'static + FnOnce() + Send,
	{
		RegisterThreadFuture(audio_worklet::register_thread(self.into(), stack_size, f))
	}

	#[cfg(any(feature = "message", docsrs))]
	fn register_thread_with_message<F, M>(
		self,
		stack_size: Option<usize>,
		#[allow(clippy::min_ident_chars)] f: F,
		message: M,
	) -> RegisterThreadFuture
	where
		F: 'static + FnOnce(M) + Send,
		M: 'static + MessageSend,
	{
		RegisterThreadFuture(audio_worklet::register_thread_with_message(
			self.into(),
			stack_size,
			f,
			message,
		))
	}

	fn audio_worklet_node<P>(
		&self,
		name: &str,
		data: P::Data,
		options: Option<&AudioWorkletNodeOptions>,
	) -> Result<AudioWorkletNode, AudioWorkletNodeError<P>>
	where
		P: 'static + ExtendAudioWorkletProcessor,
	{
		audio_worklet::audio_worklet_node(self.as_ref(), name, data, options)
	}
}

/// Error returned by [`BaseAudioContextExt::audio_worklet_node()`].
pub struct AudioWorkletNodeError<P>
where
	P: ExtendAudioWorkletProcessor,
{
	/// The passed [`ExtendAudioWorkletProcessor::Data`].
	pub data: P::Data,
	/// The error thrown by [`new AudioWorkletNode`].
	///
	/// [`new AudioWorkletNode`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletNode/AudioWorkletNode
	pub error: io::Error,
}

impl<P> Debug for AudioWorkletNodeError<P>
where
	P: ExtendAudioWorkletProcessor,
{
	fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
		formatter
			.debug_struct("AudioWorkletNodeError")
			.field("data", &any::type_name::<P::Data>())
			.field("error", &self.error)
			.finish()
	}
}

impl<P> Display for AudioWorkletNodeError<P>
where
	P: ExtendAudioWorkletProcessor,
{
	fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
		Display::fmt(&self.error, formatter)
	}
}

impl<P> Error for AudioWorkletNodeError<P>
where
	P: ExtendAudioWorkletProcessor,
{
	fn source(&self) -> Option<&(dyn Error + 'static)> {
		Some(&self.error)
	}
}

/// Waits for the associated thread to register. See
/// [`BaseAudioContextExt::register_thread()`].
#[derive(Debug)]
pub struct RegisterThreadFuture(audio_worklet::RegisterThreadFuture);

impl Future for RegisterThreadFuture {
	type Output = io::Result<AudioWorkletHandle>;

	fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
		Pin::new(&mut self.0).poll(cx).map_ok(AudioWorkletHandle)
	}
}

impl RefUnwindSafe for RegisterThreadFuture {}

/// Handle to the audio worklet. See [`BaseAudioContextExt::register_thread()`].
#[derive(Debug)]
pub struct AudioWorkletHandle(audio_worklet::AudioWorkletHandle);

impl AudioWorkletHandle {
	/// Extracts a handle to the underlying thread.
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// use web_sys::{AudioContext, console};
	/// use web_thread::web::audio_worklet::BaseAudioContextExt;
	///
	/// let context = AudioContext::new().unwrap();
	/// let handle = context.clone().register_thread(
	/// 	None,
	/// 	|| {
	/// 		// Do work.
	/// 	},
	/// ).await.unwrap();
	///
	/// console::log_1(&format!("thread id: {:?}", handle.thread().id()).into());
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	#[must_use]
	pub const fn thread(&self) -> &Thread {
		self.0.thread()
	}

	/// This releases memory allocated for the corresponding audio worklet
	/// thread.
	///
	/// # Safety
	///
	/// The corresponding thread must not currently or in the future access this
	/// Wasm module.
	///
	/// # Errors
	///
	/// If called from its corresponding audio worklet thread.
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// use wasm_bindgen_futures::JsFuture;
	/// use web_sys::AudioContext;
	/// use web_thread::web::audio_worklet::BaseAudioContextExt;
	///
	/// let context = AudioContext::new().unwrap();
	/// let (sender, receiver) = async_channel::bounded(1);
	/// let handle = context.clone().register_thread(
	/// 	None,
	/// 	move || {
	/// 		// Do work.
	/// 		sender.try_send(()).unwrap();
	/// 	},
	/// ).await.unwrap();
	///
	/// // Wait until audio worklet is finished.
	/// receiver.recv().await.unwrap();
	/// JsFuture::from(context.close().unwrap()).await.unwrap();
	/// // SAFETY: We are sure we are done with the audio worklet and didn't register any
	/// // events or promises that could call into the Wasm module later.
	/// unsafe { handle.release() }.unwrap();
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	pub unsafe fn release(self) -> Result<(), ReleaseError> {
		// SAFETY: See `ThreadMemory::release()`. Other safety guarantees have to be
		// uphold by the caller.
		unsafe { self.0.release() }
			.map_err(Self)
			.map_err(ReleaseError)
	}
}

/// Returned on error in [`AudioWorkletHandle::release()`].
#[derive(Debug)]
pub struct ReleaseError(pub AudioWorkletHandle);

impl Display for ReleaseError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
		formatter.write_str(
			"called `AudioWorkletHandle::release()` from its corresponding audio worklet thread",
		)
	}
}

impl Error for ReleaseError {}

/// Extension for [`AudioWorkletGlobalScope`].
#[cfg_attr(
	not(all(
		target_family = "wasm",
		target_os = "unknown",
		feature = "audio-worklet"
	)),
	doc = "",
	doc = "[`AudioWorkletGlobalScope`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.AudioWorkletGlobalScope.html"
)]
pub trait AudioWorkletGlobalScopeExt {
	/// Creates a class that extends [`AudioWorkletProcessor`] and calls
	/// [`AudioWorkletGlobalScope.registerProcessor()`]. This is a workaround
	/// for [`wasm-bindgen`] currently unable to extend classes, see
	/// [this `wasm-bindgen` issue](https://github.com/rustwasm/wasm-bindgen/issues/210).
	///
	/// # Notes
	///
	/// [`AudioWorkletGlobalScope.registerProcessor()`] does not sync with it's
	/// corresponding [`AudioWorkletNode`] immediately and requires at least one
	/// yield to the event loop cycle in the [`AudioWorkletNode`]s thread for
	/// [`AudioWorkletNode::new()`] to successfully find the requested
	/// [`AudioWorkletProcessor`] by its name. See [`yield_now_async()`].
	///
	/// # Errors
	///
	/// - If the `name` is empty.
	/// - If a processor with this `name` is already registered.
	/// - If this thread was not spawned by [`web-thread`](crate).
	///
	/// # Example
	///
	/// ```
	/// # #[cfg(all(target_feature = "atomics", not(unsupported_spawn)))]
	/// # wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
	/// # #[cfg_attr(all(target_feature = "atomics", not(unsupported_spawn)), wasm_bindgen_test::wasm_bindgen_test)]
	/// # async fn test() {
	/// # use wasm_bindgen::JsCast;
	/// use web_sys::{AudioContext, AudioWorkletGlobalScope, AudioWorkletNode};
	/// # use web_sys::{AudioWorkletNodeOptions, AudioWorkletProcessor};
	/// use web_thread::web::{self, YieldTime};
	/// use web_thread::web::audio_worklet::{AudioWorkletGlobalScopeExt, BaseAudioContextExt};
	/// # use web_thread::web::audio_worklet::ExtendAudioWorkletProcessor;
	///
	/// # struct TestProcessor;
	/// # impl ExtendAudioWorkletProcessor for TestProcessor {
	/// # 	type Data = ();
	/// # 	fn new(
	/// # 		_: AudioWorkletProcessor,
	/// # 		_: Option<Self::Data>,
	/// # 		_: AudioWorkletNodeOptions,
	/// # 	) -> Self {
	/// # 		Self
	/// # 	}
	/// # }
	/// #
	/// let context = AudioContext::new().unwrap();
	/// let (sender, receiver) = async_channel::bounded(1);
	/// context.clone().register_thread(
	/// 	None,
	/// 	move || {
	/// 		let global: AudioWorkletGlobalScope = js_sys::global().unchecked_into();
	/// 		global
	/// 			.register_processor_ext::<TestProcessor>("test")
	/// 			.unwrap();
	/// 		sender.try_send(()).unwrap();
	/// 	},
	/// ).await.unwrap();
	///
	/// // Wait until processor is registered.
	/// receiver.recv().await.unwrap();
	/// web::yield_now_async(YieldTime::UserBlocking).await;
	///
	/// let node = AudioWorkletNode::new(&context, "test").unwrap();
	/// # let _ = node;
	/// # }
	/// # #[cfg(not(all(target_feature = "atomics", not(unsupported_spawn))))]
	/// # let _ = test();
	/// ```
	///
	/// [`AudioWorkletGlobalScope.registerProcessor()`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletGlobalScope/registerProcessor
	/// [`AudioWorkletProcessor`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor
	/// [`yield_now_async()`]: super::yield_now_async
	#[cfg_attr(
		all(
			target_family = "wasm",
			target_os = "unknown",
			feature = "audio-worklet"
		),
		doc = "[`AudioWorkletNode`]: web_sys::AudioWorkletNode",
		doc = "[`AudioWorkletNode::new()`]: web_sys::AudioWorkletNode::new"
	)]
	#[cfg_attr(
		not(all(
			target_family = "wasm",
			target_os = "unknown",
			feature = "audio-worklet"
		)),
		doc = "[`AudioWorkletNode`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.AudioWorkletNode.html",
		doc = "[`AudioWorkletNode::new()`]: https://docs.rs/web-sys/0.3.68/web_sys/struct.AudioWorkletNode.html#method.new"
	)]
	#[cfg_attr(
		all(target_family = "wasm", target_os = "unknown"),
		doc = "[`wasm-bindgen`]: wasm_bindgen"
	)]
	#[cfg_attr(
		not(all(target_family = "wasm", target_os = "unknown")),
		doc = "[`wasm-bindgen`]: https://docs.rs/wasm-bindgen/0.2.91"
	)]
	fn register_processor_ext<P>(&self, name: &str) -> Result<(), io::Error>
	where
		P: 'static + ExtendAudioWorkletProcessor;
}

#[cfg(all(
	target_family = "wasm",
	target_os = "unknown",
	feature = "audio-worklet"
))]
impl AudioWorkletGlobalScopeExt for AudioWorkletGlobalScope {
	fn register_processor_ext<P>(&self, name: &str) -> Result<(), io::Error>
	where
		P: 'static + ExtendAudioWorkletProcessor,
	{
		audio_worklet::register_processor::<P>(name)
	}
}

/// Extends type with [`AudioWorkletProcessor`].
///
/// [`AudioWorkletProcessor`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor
pub trait ExtendAudioWorkletProcessor {
	/// Data passed into [`Self::new()`] when using
	/// [`BaseAudioContextExt::audio_worklet_node()`].
	type Data: 'static + Send;

	/// Equivalent to [`AudioWorkletProcessor()`].
	///
	/// [`AudioWorkletProcessor()`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/AudioWorkletProcessor
	fn new(
		this: AudioWorkletProcessor,
		data: Option<Self::Data>,
		options: AudioWorkletNodeOptions,
	) -> Self;

	/// Equivalent to [`AudioWorkletProcessor.process()`].
	///
	/// [`AudioWorkletProcessor.process()`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/process
	#[allow(unused_variables)]
	fn process(&mut self, inputs: Array, outputs: Array, parameters: Object) -> bool {
		false
	}

	/// Equivalent to [`AudioWorkletProcessor.parameterDescriptors`].
	///
	/// [`AudioWorkletProcessor.parameterDescriptors`]: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/parameterDescriptors
	#[allow(clippy::must_use_candidate)]
	fn parameter_descriptors() -> Iterator {
		Array::new().values()
	}
}