fix(noise): WASM rng cfg gates check wasm64, so wasm32 browser builds still panic - #221
Merged
Merged
Conversation
…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
approved these changes
Jun 16, 2026
silvia-odwyer
left a comment
Owner
There was a problem hiding this comment.
@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 🚀
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.
The bug
add_noise_randandpink_noisepanic withRuntimeError: unreachablein every browser WASM build, despite the [WASM SUPPORT IS AVAILABLE] docstrings.The
js_sys::Math::randomworkaround 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 therand::thread_rng()branch instead, andthread_rnghas no entropy source on wasm32-unknown-unknown, panicking on first call. (Theuse js_sys::Math::randomimport at the top of the file is already gated ontarget_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"totarget_family = "wasm", matching the existing import gate. wasm32 + wasm64 both take theMath.randompath; native keepsrand::thread_rng().Verification
cargo checkclean onwasm32-unknown-unknownand native (aarch64-apple-darwin).wasm-pack build --target nodejsof master vs this branch: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.