问题描述
关于如何使用wasm-bindgen在rust -> wasm
流中使用现有javascript网络系统的文档非常简单。简而言之:
- 列出所需的功能作为依赖项
// cargo.toml
...omitted...
[dependencies.web-sys]
version = "0.3.4"
features = [
'AudioContext','OscillatorNode'
]
- 通过web_sys进行访问,然后从那里将它们视为正常对象。
// lib.rs
...omitted...
#[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()?;
...omitted...
链接到完整示例:https://rustwasm.github.io/docs/wasm-bindgen/examples/web-audio.html
尽管这种方法适用于大多数Web系统,但对于那些需要扩展类的系统而言,并不是那么简单。在这种情况下,我尝试使用AudioWorkletProccessor from wasm-bindgen
在标准用法中,应该扩展AudioWorkletProcessor
类,然后注册它。 Mdn示例:https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_AudioWorklet#Basic_code_framework
我该如何解决这个问题?首先,我需要在Rust中扩展AudioWorkletProcessor
(不执行常规类和继承),以覆盖其process()
函数。然后,我需要确保将registerProcessor()
注入了我的锈蚀空间。
编辑:https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/on-js-imports/extends.html