Skip to content

Commit 2ccbca7

Browse files
authored
Enable wasm_js feature of getrandom in boa_engine crate (#4241)
1 parent 2a59eb3 commit 2ccbca7

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,24 @@ Check [debugging.md](./docs/debugging.md) for more info on debugging.
111111

112112
### Web Assembly
113113

114-
This interpreter can be exposed to JavaScript!
115-
You can build the example locally with:
114+
> [!IMPORTANT]
115+
>
116+
> This only applies to `wasm32-unknown-unknown` target,
117+
> `WASI` and `Emscripten` target variants are handled automatically.
116118
117-
```shell
118-
npm run build
119-
```
119+
- Enable the `js` feature flag.
120+
- Set `RUSTFLAGS='--cfg getrandom_backend="wasm_js"'`
120121

121-
In the console you can use `window.evaluate` to pass JavaScript in.
122-
To develop on the web assembly side you can run:
122+
The `rustflags` can also be set by adding a `.cargo/config.toml` file in the project root directory:
123123

124-
```shell
125-
npm run serve
124+
```toml
125+
[target.wasm32-unknown-unknown]
126+
rustflags = '--cfg getrandom_backend="wasm_js"'
126127
```
127128

128-
then go to `http://localhost:8080`.
129+
For more information see: [`getrandom` WebAssembly Support][getrandom-webassembly-support]
130+
131+
[getrandom-webassembly-support]: https://docs.rs/getrandom/latest/getrandom/index.html#webassembly-support
129132

130133
## Usage
131134

core/engine/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ temporal = ["dep:icu_calendar", "dep:temporal_rs", "dep:iana-time-zone"]
6565
experimental = ["temporal"]
6666

6767
# Enable binding to JS APIs for system related utilities.
68-
js = ["dep:web-time"]
68+
js = ["dep:web-time", "dep:getrandom"]
6969

7070
[dependencies]
7171
tag_ptr.workspace = true
@@ -158,6 +158,8 @@ iana-time-zone = { version = "0.1.63", optional = true }
158158

159159
[target.'cfg(all(target_family = "wasm", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
160160
web-time = { workspace = true, optional = true }
161+
# NOTE: This enables the wasm_js required for rand to work on wasm
162+
getrandom = { workspace = true, features = ["wasm_js"], optional = true }
161163

162164
[dev-dependencies]
163165
criterion.workspace = true

core/engine/src/sys/js/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub(crate) use getrandom as _;
12
pub(crate) use web_time as time;

ffi/wasm/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ rust-version.workspace = true
1414
[dependencies]
1515
boa_engine = { workspace = true, features = ["js"] }
1616
wasm-bindgen = { workspace = true, default-features = false }
17-
getrandom = { workspace = true, features = ["wasm_js"] }
1817
console_error_panic_hook.workspace = true
1918

2019
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies]
2120
wasm-bindgen-test.workspace = true
2221

2322
[features]
24-
default = ["boa_engine/annex-b", "boa_engine/intl_bundled", "boa_engine/experimental"]
23+
default = [
24+
"boa_engine/annex-b",
25+
"boa_engine/intl_bundled",
26+
"boa_engine/experimental",
27+
]
2528

2629
[lib]
2730
crate-type = ["cdylib", "lib"]

ffi/wasm/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![allow(unused_crate_dependencies)]
44

55
use boa_engine::{Context, Source};
6-
use getrandom as _;
76
use wasm_bindgen::prelude::*;
87

98
#[wasm_bindgen(start)]

0 commit comments

Comments
 (0)