You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix wasm-bindgen-test compatibility with doctests in Node.js
This commit addresses crashes when running wasm-bindgen-test in doctests,
which execute in Node.js environments with specific limitations.
Problem:
When running doctests with wasm-bindgen-test, async code using spawn_local()
would crash with "not a shared typed array" error. This occurred because:
1. Doctests run in Node.js, not browsers
2. Node.js may not have SharedArrayBuffer enabled by default
3. Even with atomics compiled in, the runtime memory might not be shared
Solution:
1. SharedArrayBuffer detection: Before using Atomics.waitAsync(), we now
check if the memory buffer is actually a SharedArrayBuffer. If not,
we gracefully fall back to the polyfill implementation.
2. Worker availability handling: In Node.js doctests, Worker may not be
available or may behave differently. The polyfill now:
- Checks for Worker constructor availability before attempting to use it
- Falls back to setTimeout-based polling when Worker is unavailable
- Implements exponential backoff (1ms → 2ms → 4ms... up to 100ms)
- Polls up to 10 times before timing out
Design decisions:
- The SharedArrayBuffer check uses inline JS to properly detect the type,
as this can't be reliably done from Rust alone
- The polling fallback provides basic async behavior without Worker deps,
making doctests functional even in restricted environments
- The implementation prioritizes compatibility over performance in fallback
scenarios, as doctests are primarily for documentation/testing
This enables wasm-bindgen-test to work reliably in doctest environments
while maintaining full functionality in production browser contexts.
0 commit comments