Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wasm32 with atomics #797

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ jobs:
- name: Fast RNG
run: wasm-pack test --node -- --features "js v4 fast-rng"

- name: +atomics
env:
RUSTFLAGS: '-C target-feature=+atomics'
run: cargo check --target wasm32-unknown-unknown --features "$VERSION_FEATURES $DEP_FEATURES js"

- name: rng-getrandom
env:
RUSTFLAGS: '--cfg getrandom_backend="wasm_js"'
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ v6 = ["atomic"]
v7 = ["rng"]
v8 = []

js = ["dep:wasm-bindgen"]
js = ["dep:wasm-bindgen", "dep:js-sys"]

rng = ["dep:getrandom"]
rng-getrandom = ["rng", "dep:getrandom", "uuid-rng-internal-lib", "uuid-rng-internal-lib/getrandom"]
Expand Down Expand Up @@ -173,6 +173,11 @@ version = "0.6"
version = "0.2"
optional = true

# Private
[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown", target_feature = "atomics"))'.dependencies.js-sys]
version = "0.3"
optional = true

[dev-dependencies.bincode]
version = "1.0"

Expand Down
5 changes: 4 additions & 1 deletion src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ mod imp {
*/

use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

#[cfg(target_feature = "atomics")]
use core::convert::TryInto;

// Maximum buffer size allowed in `Crypto.getRandomValuesSize` is 65536 bytes.
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
Expand Down Expand Up @@ -287,7 +290,7 @@ mod imp {
return false;
}

sub_buf.copy_to_uninit(chunk);
sub_buf.copy_to(chunk);
}

true
Expand Down