diff --git a/Cargo.toml b/Cargo.toml index 2d8baa62..eee54add 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,9 +21,6 @@ core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" [target.'cfg(unix)'.dependencies] libc = { version = "0.2.154", default-features = false } -[target.'cfg(all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"))'.dependencies] -wasi = { version = "0.13", default-features = false } - [target.'cfg(all(windows, not(target_vendor = "win7")))'.dependencies] windows-targets = "0.52" diff --git a/src/wasi.rs b/src/wasi.rs index db2a800e..8c8804c6 100644 --- a/src/wasi.rs +++ b/src/wasi.rs @@ -11,6 +11,9 @@ compile_error!( or Rust version older than 1.80 was used" ); +#[cfg(not(target_arch = "wasm32"))] +compile_error!("Currently only 32-bit WASI targets are supported"); + #[cfg(target_env = "p1")] pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { // This linking is vendored from the wasi crate: @@ -34,7 +37,19 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { #[cfg(target_env = "p2")] pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { use core::ptr::copy_nonoverlapping; - use wasi::random::random::get_random_u64; + + // Based on the code from the wasi v0.13 crate: + // https://docs.rs/wasi/0.13.2+wasi-0.2.1/src/wasi/bindings.rs.html#10845-10860 + fn get_random_u64() -> u64 { + unsafe { + #[link(wasm_import_module = "wasi:random/random@0.2.1")] + extern "C" { + #[link_name = "get-random-u64"] + fn wit_import() -> i64; + } + wit_import() as u64 + } + } let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::>() };