Enable wasm-bindgen feature with target wasm32-unknown-emscripten - #1349
Open
DouglasDwyer wants to merge 1 commit into
Open
Enable wasm-bindgen feature with target wasm32-unknown-emscripten#1349DouglasDwyer wants to merge 1 commit into
DouglasDwyer wants to merge 1 commit into
Conversation
The WebAudio host and its `wasm-bindgen`/`js-sys`/`web-sys` dependencies were gated on `all(target_arch = "wasm32", target_os = "unknown", feature = "wasm-bindgen")`. Nothing in the host needs `target_os = "unknown"` -- only `wasm32` and the browser Web APIs `feature = "wasm-bindgen"` pulls in -- so on `wasm32-unknown-emscripten` cpal silently fell back to the `null` host. Drop the `target_os = "unknown"` clause from the WebAudio gates (host/mod.rs, platform/mod.rs, lib.rs, sample_format.rs, Cargo.toml). `wasm32-unknown-unknown` still matches unchanged. The AudioWorklet host stays `wasm32-unknown-unknown`-only: its `worklet.js` bootstrap re-instantiates the module through wasm-bindgen's ESM `initSync`, which only exists in the `bundler`/`web` output that `target_os = "unknown"` implies. Its module gate, its `platform_impl` entry, and `frames_to_duration` (its sole wasm consumer) keep the constraint. Switch the WebAudio host's three JS event closures to `Closure::wrap_aborting`. `Closure::wrap` requires the closure to be `UnwindSafe` under `panic = "unwind"`, which is the default only on `wasm32-unknown-emscripten`; `wrap_aborting` selects the non-catching invoke shim on every target, identical to what `wrap` already did on the `panic = "abort"` wasm targets. Raises the minimum `wasm-bindgen` to 0.2.110, where `wrap_aborting` was added. Add a `wasm-emscripten` job to the platform matrix and a `WASM-emscripten` clippy entry, mirroring the existing `wasm-bindgen` coverage. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
roderickvd
requested changes
Sep 1, 2026
roderickvd
left a comment
Member
There was a problem hiding this comment.
That's great! Not so long ago we removed the old Emscripten host that had become defunct. This seems like a light-weight manner to get Emscripten support back.
Beyond the changes requested in the review points, please also consider updating README.md with Emscripten support.
| }) | ||
| as Box<dyn FnMut(AudioProcessingEvent)>); | ||
| as Box<dyn FnMut(AudioProcessingEvent)>; | ||
| let on_audio_process = Closure::wrap_aborting(on_audio_process_fn); |
Member
There was a problem hiding this comment.
wrap_abort instead of wrap would abort the whole WASM instance for regular WebAudio users, where currently they just get a JS exception.
|
|
||
| [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] | ||
| wasm-bindgen = { version = "0.2", optional = true } | ||
| [target.'cfg(target_arch = "wasm32")'.dependencies] |
Member
There was a problem hiding this comment.
This is not necessary for wasm32-wasip1/wasip2.
| - `InputCallbackInfo`/`OutputCallbackInfo` merged into `CallbackInfo`. | ||
| - `InputStreamTimestamp`/`OutputStreamTimestamp` merged into `StreamTimestamp`; `capture`/`playback` renamed `device`. | ||
| - Renamed the `wasm-beep` and `audioworklet-beep` examples to `webaudio` and `audioworklet`. | ||
| - **WebAudio**: the host and its `wasm-bindgen`/`js-sys`/`web-sys` dependencies are now gated on `target_arch = "wasm32"` rather than `all(target_arch = "wasm32", target_os = "unknown")`, so the WebAudio host works under `wasm32-unknown-emscripten` (previously it fell back to the `null` host). The AudioWorklet host stays `wasm32-unknown-unknown`-only. Raises the minimum `wasm-bindgen` to 0.2.110. |
Member
There was a problem hiding this comment.
Please make this consistent with the other lines: short and observable behavior, not implementation details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
wasm-bindgentool is finally getting support for integration with Emscripten. The feature is still quite new, but this means that most of Rust's web ecosystem can now work with the Emscripten target. I'd like to usecpalin a WASM/Emscripten project, but right now thewasm32-unknown-emscriptentarget is hard-coded to use the null backend. This PR eliminates the feature gate to make thewasm-bindgenfeature work withwasm32-unknown-emscriptentoo.This PR exposes the
webaudiobackend but not theaudioworkletbackend. That backend relies on re-instantiating the WASM module, but the way Emscripten modules get instantiated is different, so it wouldn't work without more changes.Changes
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "wasm-bindgen"))]with#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]for thewebaudiobackendClosure::wrap_abortinginstead ofClosure::wrapso that the code properly compiles on WASM targets withpanic=unwindwasm-bindgendependency to0.2.110in order to useClosure::wrap_abortingTesting
In my own project, I have gotten
cpalaudio working with a Rust/Emscripten WASM module in Chrome. This PR also adds CI checks to ensure that compilation is successful.Related issues
#92 #413 #810