Skip to content

fix(noise): WASM rng cfg gates check wasm64, so wasm32 browser builds still panic - #221

Merged
silvia-odwyer merged 1 commit into
silvia-odwyer:masterfrom
iWhatty:fix/noise-wasm32-cfg
Jun 16, 2026
Merged

fix(noise): WASM rng cfg gates check wasm64, so wasm32 browser builds still panic#221
silvia-odwyer merged 1 commit into
silvia-odwyer:masterfrom
iWhatty:fix/noise-wasm32-cfg

Conversation

@iWhatty

@iWhatty iWhatty commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

The bug

add_noise_rand and pink_noise panic with RuntimeError: unreachable in every browser WASM build, despite the [WASM SUPPORT IS AVAILABLE] docstrings.

The js_sys::Math::random workaround introduced in #162 is gated at every use site on:

#[cfg(all(target_arch = "wasm64", not(target_os = "wasi")))]

Browser builds target wasm32-unknown-unknown, which fails that condition - so they compile the rand::thread_rng() branch instead, and thread_rng has no entropy source on wasm32-unknown-unknown, panicking on first call. (The use js_sys::Math::random import at the top of the file is already gated on target_family = "wasm", which is why the import compiles but goes unused on wasm32.)

The fix

Switch the seven use-site gates from target_arch = "wasm64" to target_family = "wasm", matching the existing import gate. wasm32 + wasm64 both take the Math.random path; native keeps rand::thread_rng().

Verification

  • cargo check clean on wasm32-unknown-unknown and native (aarch64-apple-darwin).
  • Runtime, via wasm-pack build --target nodejs of master vs this branch:
master:      add_noise_rand -> RuntimeError: unreachable
this branch: add_noise_rand + pink_noise run and mutate pixels

Context: we hit this in production on Photon Lab (a browser playground built on photon-rs) and had to cut the noise functions from the UI; this one-line-class fix would let us (and anyone on @silvia-odwyer/photon) re-enable them once released.

Thanks for photon - it's a joy to build on.

…ath.random path

The js_sys::Math::random workaround from silvia-odwyer#162 is gated on
target_arch = "wasm64" at every use site, but browser builds are
wasm32-unknown-unknown - they fail the cfg and fall through to
rand::thread_rng(), which panics (RuntimeError: unreachable) at the
first call to add_noise_rand or pink_noise in the browser.

Switch the use-site gates to target_family = "wasm" (matching the
existing use-import gate at the top of the file), so wasm32 and wasm64
both take the Math.random path and native keeps rand::thread_rng().

Verified: cargo check clean on wasm32-unknown-unknown and native;
wasm-pack nodejs build of master panics on add_noise_rand, this branch
runs both add_noise_rand and pink_noise and mutates pixels.

@silvia-odwyer silvia-odwyer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iWhatty Thank you very much for your PR and this fix, much appreciated! Everything looks good to me, so going to merge this now ✅

I also checked out Photon Lab, I'm delighted that you decided to use Photon for this :) Going to add a link to this web app in the Readme also so others might see it and try it out 🚀

@silvia-odwyer
silvia-odwyer merged commit 65acb42 into silvia-odwyer:master Jun 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants