Skip to content

Commit 7cde98b

Browse files
authored
Add wasm_js feature flag (#574)
1 parent 6be4012 commit 7cde98b

File tree

8 files changed

+60
-48
lines changed

8 files changed

+60
-48
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ jobs:
142142
{ description: WasmV1, target: wasm32v1-none },
143143
]
144144
feature: [
145-
{ description: no_std, feature: "", build-std: "core,alloc", std: false },
146-
{ feature: --features std, build-std: "panic_abort,std", std: true },
145+
{ description: no_std, feature: "--features wasm_js", build-std: "core,alloc", std: false },
146+
{ feature: "--features wasm_js,std", build-std: "panic_abort,std", std: true },
147147
]
148148
atomic: [
149149
{ flags: "" },

.github/workflows/tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ jobs:
237237
- {
238238
description: Web,
239239
version: stable,
240-
flags: -Dwarnings --cfg getrandom_backend="wasm_js",
241-
args: --features=std,
240+
flags: '-Dwarnings --cfg getrandom_backend="wasm_js"',
241+
args: '--features=std,wasm_js',
242242
}
243243
- {
244244
description: Web with Atomics,
245245
version: nightly,
246246
components: rust-src,
247247
flags: '-Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory',
248-
args: '--features=std -Zbuild-std=panic_abort,std',
248+
args: '--features=std,wasm_js -Zbuild-std=panic_abort,std',
249249
}
250250
steps:
251251
- uses: actions/checkout@v4

.github/workflows/workspace.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ jobs:
4848
- name: Web WASM (wasm_js.rs)
4949
env:
5050
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js"
51-
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
51+
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features wasm_js
5252
- name: Web WASM with atomics (wasm_js.rs)
5353
env:
5454
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="wasm_js" -Ctarget-feature=+atomics,+bulk-memory
55-
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown
55+
run: cargo clippy -Zbuild-std --target wasm32-unknown-unknown --features wasm_js
5656
- name: Linux (linux_android.rs)
5757
env:
5858
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom"

Cargo.toml

+17-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ repository = "https://github.com/rust-random/getrandom"
1111
categories = ["os", "no-std"]
1212
exclude = [".*"]
1313

14+
[features]
15+
# Implement std::error::Error for getrandom::Error and
16+
# use std to retrieve OS error descriptions
17+
std = []
18+
# Unstable feature to support being a libstd dependency
19+
rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]
20+
21+
# Optional backend: wasm_js
22+
# This flag enables the backend but does not select it. To use the backend, use
23+
# this flag *and* set getrandom_backend=wasm_js (see README).
24+
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]
25+
1426
[dependencies]
1527
cfg-if = "1"
1628

@@ -59,20 +71,13 @@ wasi = { version = "0.13", default-features = false }
5971
windows-targets = "0.52"
6072

6173
# wasm_js
62-
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
63-
wasm-bindgen = { version = "0.2.98", default-features = false }
64-
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
65-
js-sys = { version = "0.3.77", default-features = false }
66-
[target.'cfg(all(getrandom_backend = "wasm_js", target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
74+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
75+
wasm-bindgen = { version = "0.2.98", default-features = false, optional = true }
76+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
77+
js-sys = { version = "0.3.77", default-features = false, optional = true }
78+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
6779
wasm-bindgen-test = "0.3"
6880

69-
[features]
70-
# Implement std::error::Error for getrandom::Error and
71-
# use std to retrieve OS error descriptions
72-
std = []
73-
# Unstable feature to support being a libstd dependency
74-
rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"]
75-
7681
[lints.rust.unexpected_cfgs]
7782
level = "warn"
7883
check-cfg = [

README.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ of randomness based on their specific needs:
8282
| `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
8383
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
8484
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
85-
| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]
85+
| `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Requires feature `wasm_js` ([see below](#webassembly-support)).
8686
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
8787

8888
Opt-in backends can be enabled using the `getrandom_backend` configuration flag.
@@ -112,18 +112,19 @@ the `wasm32-unknown-unknown` target (i.e. the target used by `wasm-pack`)
112112
is not automatically supported since, from the target name alone, we cannot deduce
113113
which JavaScript interface should be used (or if JavaScript is available at all).
114114

115-
Instead, *if the `wasm_js` backend is enabled*, this crate will assume
116-
that you are building for an environment containing JavaScript, and will
117-
call the appropriate Web Crypto methods [described above](#opt-in-backends) using
118-
the [`wasm-bindgen`] toolchain. Both web browser (main window and Web Workers)
119-
and Node.js (v19 or later) environments are supported.
115+
To enable `getrandom`'s functionality on `wasm32-unknown-unknown` using the Web
116+
Crypto methods [described above](#opt-in-backends) via [`wasm-bindgen`], do
117+
*both* of the following:
120118

121-
To enable the `wasm_js` backend, you can add the following lines to your
122-
project's `.cargo/config.toml` file:
123-
```toml
124-
[target.wasm32-unknown-unknown]
125-
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
126-
```
119+
- Use the `wasm_js` feature flag, i.e.
120+
`getrandom = { version = "0.3", features = ["wasm_js"] }`.
121+
On its own, this only makes the backend available. (As a side effect this
122+
will make your `Cargo.lock` significantly larger if you are not already
123+
using [`wasm-bindgen`], but otherwise enabling this feature is harmless.)
124+
- Set `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'` ([see above](#opt-in-backends)).
125+
126+
This backend supports both web browsers (main window and Web Workers)
127+
and Node.js (v19 or later) environments.
127128

128129
### Custom backend
129130

src/backends.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ cfg_if! {
1919
} else if #[cfg(getrandom_backend = "rndr")] {
2020
mod rndr;
2121
pub use rndr::*;
22-
} else if #[cfg(getrandom_backend = "wasm_js")] {
23-
mod wasm_js;
24-
pub use wasm_js::*;
22+
} else if #[cfg(all(getrandom_backend = "wasm_js"))] {
23+
cfg_if! {
24+
if #[cfg(feature = "wasm_js")] {
25+
mod wasm_js;
26+
pub use wasm_js::*;
27+
} else {
28+
compile_error!(
29+
"The \"wasm_js\" backend requires the `wasm_js` feature \
30+
for `getrandom`. For more information see: \
31+
https://docs.rs/getrandom/#webassembly-support"
32+
);
33+
}
34+
}
2535
} else if #[cfg(target_os = "espidf")] {
2636
mod esp_idf;
2737
pub use esp_idf::*;
@@ -145,14 +155,14 @@ cfg_if! {
145155
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
146156
mod rdrand;
147157
pub use rdrand::*;
148-
} else if #[cfg(all(
149-
target_arch = "wasm32",
150-
any(target_os = "unknown", target_os = "none")
151-
))] {
152-
compile_error!("the wasm32-unknown-unknown targets are not supported \
153-
by default, you may need to enable the \"wasm_js\" \
154-
configuration flag. For more information see: \
155-
https://docs.rs/getrandom/#webassembly-support");
158+
} else if #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))] {
159+
compile_error!(
160+
"The wasm32-unknown-unknown targets are not supported by default; \
161+
you may need to enable the \"wasm_js\" configuration flag. Note \
162+
that enabling the `wasm_js` feature flag alone is insufficient. \
163+
For more information see: \
164+
https://docs.rs/getrandom/#webassembly-support"
165+
);
156166
} else {
157167
compile_error!("target is not supported. You may need to define \
158168
a custom backend see: \

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Error {
116116
Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure",
117117
#[cfg(all(windows, target_vendor = "win7"))]
118118
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
119-
#[cfg(getrandom_backend = "wasm_js")]
119+
#[cfg(all(feature = "wasm_js", getrandom_backend = "wasm_js"))]
120120
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
121121
#[cfg(target_os = "vxworks")]
122122
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",

tests/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use core::mem::MaybeUninit;
22
use getrandom::{fill, fill_uninit};
33

4-
#[cfg(all(
5-
getrandom_backend = "wasm_js",
6-
target_arch = "wasm32",
7-
target_os = "unknown"
8-
))]
4+
#[cfg(all(feature = "wasm_js", target_arch = "wasm32", target_os = "unknown"))]
95
use wasm_bindgen_test::wasm_bindgen_test as test;
106

117
#[test]

0 commit comments

Comments
 (0)