rustwebassemblywasm-bindgenaudio-worklet

Extend AudioWorkletProcessor in wasm-bindgen?


The docs on how to use existing javascript web systems within a rust -> wasm flow using wasm-bindgen are pretty straight-forward. In short:

  1. List the needed features as dependencies
// cargo.toml

[dependencies.web-sys]
version = "0.3.4"
features = [
  'AudioContext',
  'OscillatorNode'
]
  1. Access through web_sys, and treat them as normal from there.
// lib.rs

#[wasm_bindgen]
impl FmOsc {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Result<FmOsc, JsValue> {
        let ctx = web_sys::AudioContext::new()?;
        let primary = ctx.create_oscillator()?;
        let fm_osc = ctx.create_oscillator()?;

Link to full example


While this approach works for most web systems, it is not so simple for ones which require you to extend a class. In this case I'm trying to use the AudioWorkletProccessor from wasm-bindgen

In standard usage, you are supposed to extend the AudioWorkletProcessor class, then register it. MDN Example


How can I approach this problem? First I need to extend AudioWorkletProcessor in Rust (which doesn't do normal classes and inheritance), overriding its process() function. Then I need to make sure registerProcessor() is injected into my rust space.


EDIT: WASM docs on extends


Solution

  • You will have to do that on the JavaScript side. wasm-bindgen does not support extending imported classes and re-exporting them to JavaScript; however, they do want to support it eventually and there is an issue to track it, and a related RFC.