Skip to content

Commit 56e2cd7

Browse files
committed
Add WASIp3 support
1 parent 2d4f37c commit 56e2cd7

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

.github/workflows/tests.yml

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,58 @@ jobs:
336336
# Firefox doesn't support module service workers and therefor can't import scripts
337337
run: wasm-pack test --headless --chrome -- ${{ matrix.rust.args }}
338338

339-
wasi:
340-
name: WASI
339+
wasi_p1:
340+
name: WASIp1
341341
runs-on: ubuntu-24.04
342342
steps:
343343
- uses: actions/checkout@v6
344344
- uses: dtolnay/rust-toolchain@master
345345
with:
346346
toolchain: 1.85
347-
targets: wasm32-wasip1,wasm32-wasip2
347+
targets: wasm32-wasip1
348348
- name: Install Wasmtime
349349
run: |
350-
VERSION=v24.0.0
350+
VERSION=v40.0.0
351351
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
352352
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
353353
wasmtime --version
354354
- uses: Swatinem/rust-cache@v2
355-
- name: WASI 0.1 Test
356-
run: cargo test --target wasm32-wasip1
357-
- name: WASI 0.2 Test
358-
run: cargo test --target wasm32-wasip2
355+
- run: cargo test --target wasm32-wasip1
356+
357+
wasi_p2:
358+
name: WASIp2
359+
runs-on: ubuntu-24.04
360+
steps:
361+
- uses: actions/checkout@v6
362+
- uses: dtolnay/rust-toolchain@master
363+
with:
364+
toolchain: 1.85
365+
targets: wasm32-wasip2
366+
- name: Install Wasmtime
367+
run: |
368+
VERSION=v40.0.0
369+
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
370+
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
371+
wasmtime --version
372+
- uses: Swatinem/rust-cache@v2
373+
- run: cargo test --target wasm32-wasip2
374+
375+
wasi_p3:
376+
name: WASIp3
377+
runs-on: ubuntu-24.04
378+
steps:
379+
- uses: actions/checkout@v6
380+
- uses: dtolnay/rust-toolchain@master
381+
with:
382+
# TODO: switch to stable after pre-built
383+
# `std` is published for wasm32-wasip3
384+
toolchain: nightly-2026-01-07
385+
components: rust-src
386+
- name: Install Wasmtime
387+
run: |
388+
VERSION=v40.0.0
389+
URL=https://github.com/bytecodealliance/wasmtime/releases/download/${VERSION}/wasmtime-${VERSION}-x86_64-linux.tar.xz
390+
wget -O - $URL | tar -xJ --strip-components=1 -C ~/.cargo/bin
391+
wasmtime --version
392+
- uses: Swatinem/rust-cache@v2
393+
- run: cargo test -Zbuild-std --target wasm32-wasip3

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- `RawOsError` type alias [#739]
1111
- `SysRng` behind new feature `sys_rng` [#751]
12+
- WASIp3 support [#779]
1213

1314
### Changed
1415
- Use Edition 2024 and MSRV 1.85 [#749]
1516

1617
[#739]: https://github.com/rust-random/getrandom/pull/739
1718
[#749]: https://github.com/rust-random/getrandom/pull/749
1819
[#751]: https://github.com/rust-random/getrandom/pull/751
20+
[#779]: https://github.com/rust-random/getrandom/pull/779
1921

2022
## [0.3.4] - 2025-10-14
2123

src/backends.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,9 @@ cfg_if! {
151151
if #[cfg(target_env = "p1")] {
152152
mod wasi_p1;
153153
pub use wasi_p1::*;
154-
} else if #[cfg(any(target_env = "p2", target_env = "p3"))] {
154+
} else {
155155
mod wasi_p2_3;
156156
pub use wasi_p2_3::*;
157-
} else {
158-
compile_error!(
159-
"Unknown version of WASI (only previews 1 and 2 are supported) \
160-
or Rust version older than 1.80 was used"
161-
);
162157
}
163158
}
164159
} else if #[cfg(target_os = "hermit")] {

src/backends/wasi_p2_3.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
//! Implementation for WASI Preview 2.
1+
//! Implementation for WASIp2 and WASIp3.
22
use crate::Error;
33
use core::{mem::MaybeUninit, ptr::copy_nonoverlapping};
4+
45
#[cfg(target_env = "p2")]
5-
use wasip2::random::random::get_random_u64;
6+
use wasip2 as wasi;
7+
8+
// Workaround to silence `unexpected_cfgs` warning
9+
// on Rust version between 1.85 and 1.91
10+
#[cfg(not(target_env = "p2"))]
611
#[cfg(target_env = "p3")]
7-
use wasip3::random::random::get_random_u64;
12+
use wasip3 as wasi;
13+
14+
#[cfg(not(target_env = "p2"))]
15+
#[cfg(not(target_env = "p3"))]
16+
compile_error!("Unknown version of WASI (only previews 1, 2 and 3 are supported)");
17+
18+
use wasi::random::random::get_random_u64;
819

920
#[inline]
1021
pub fn inner_u32() -> Result<u32, Error> {

0 commit comments

Comments
 (0)