Skip to content

Commit 3142dee

Browse files
authored
Merge pull request #20 from nipzu/master
Added support for wasm32-unknown-wasi
2 parents c2f13c8 + 0f411bb commit 3142dee

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ matrix:
6060
- cargo test
6161

6262
- rust: nightly
63-
env: DESCRIPTION="WASM via emscripten, stdweb and wasm-bindgen"
63+
env: DESCRIPTION="WASM via emscripten, stdweb, wasm-bindgen and WASI"
6464
install:
6565
- rustup target add wasm32-unknown-unknown
6666
- rustup target add wasm32-unknown-emscripten
67+
- rustup target add wasm32-unknown-wasi
6768
- nvm install 9
6869
- ./utils/ci/install_cargo_web.sh
6970
- cargo web prepare-emscripten
@@ -80,6 +81,7 @@ matrix:
8081
#- cargo web test --target wasm32-unknown-emscripten
8182
#- cargo web test --nodejs --target wasm32-unknown-emscripten
8283
#- cargo build --target wasm32-unknown-unknown # without any features
84+
- cargo build --target wasm32-unknown-wasi
8385
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
8486
- cargo web test --target wasm32-unknown-unknown --features=stdweb
8587
- cargo build --manifest-path tests/wasm_bindgen/Cargo.toml --target wasm32-unknown-unknown

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [0.1.2] - 2019-04-06
9+
- Add support for `wasm32-unknown-wasi` target.
10+
811
## [0.1.1] - 2019-04-05
912
- Enable std functionality for CloudABI by default.
1013

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "getrandom"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2018"
55
authors = ["The Rand Project Developers"]
66
license = "MIT OR Apache-2.0"
@@ -36,5 +36,8 @@ fuchsia-cprng = "0.1"
3636
wasm-bindgen = { version = "0.2.29", optional = true }
3737
stdweb = { version = "0.4.9", optional = true }
3838

39+
[target.wasm32-unknown-wasi.dependencies]
40+
libc = "0.2.51"
41+
3942
[features]
4043
std = []

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This library is `no_std` compatible, but uses `std` on most platforms.
4545
The `log` library is supported as an optional dependency. If enabled, error
4646
reporting will be improved on some platforms.
4747

48-
For WebAssembly (`wasm32`), Enscripten targets are supported directly; otherwise
48+
For WebAssembly (`wasm32`), WASI and Emscripten targets are supported directly; otherwise
4949
one of the following features must be enabled:
5050

5151
- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)

src/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
//! | SGX | RDRAND
2828
//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and ams.js][14])
2929
//! | Node.js | [`crypto.randomBytes`][15] (see [Support for WebAssembly and ams.js][16])
30+
//! | WASI | [`__wasi_random_get`][17]
3031
//!
3132
//! Getrandom doesn't have a blanket implementation for all Unix-like operating
3233
//! systems that reads from `/dev/urandom`. This ensures all supported operating
@@ -44,6 +45,10 @@
4445
//! features are activated for this crate. Note that if both features are
4546
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
4647
//! `getrandom` will always fail.
48+
//!
49+
//! The WASI target `wasm32-unknown-wasi` uses the `__wasi_random_get`
50+
//! function defined by the WASI standard.
51+
//!
4752
//!
4853
//! ## Early boot
4954
//!
@@ -108,6 +113,7 @@
108113
//! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
109114
//! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
110115
//! [16]: #support-for-webassembly-and-amsjs
116+
//! [17]: https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-api.md#__wasi_random_get
111117
112118
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
113119
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
@@ -135,7 +141,10 @@ extern crate std;
135141
target_os = "dragonfly",
136142
target_os = "haiku",
137143
target_os = "linux",
138-
target_arch = "wasm32",
144+
all(
145+
target_arch = "wasm32",
146+
not(target_env = "wasi")
147+
),
139148
))]
140149
mod utils;
141150
mod error;
@@ -181,11 +190,13 @@ mod_use!(cfg(target_os = "redox"), use_file);
181190
mod_use!(cfg(target_os = "solaris"), solaris_illumos);
182191
mod_use!(cfg(windows), windows);
183192
mod_use!(cfg(target_env = "sgx"), sgx);
193+
mod_use!(cfg(target_env = "wasi"), wasi);
184194

185195
mod_use!(
186196
cfg(all(
187197
target_arch = "wasm32",
188198
not(target_os = "emscripten"),
199+
not(target_env = "wasi"),
189200
feature = "wasm-bindgen"
190201
)),
191202
wasm32_bindgen
@@ -195,6 +206,7 @@ mod_use!(
195206
cfg(all(
196207
target_arch = "wasm32",
197208
not(target_os = "emscripten"),
209+
not(target_env = "wasi"),
198210
not(feature = "wasm-bindgen"),
199211
feature = "stdweb",
200212
)),
@@ -225,6 +237,7 @@ mod_use!(
225237
target_arch = "wasm32",
226238
any(
227239
target_os = "emscripten",
240+
target_env = "wasi",
228241
feature = "wasm-bindgen",
229242
feature = "stdweb",
230243
),

src/wasi.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for WASI
10+
use crate::Error;
11+
use core::num::NonZeroU32;
12+
use std::io;
13+
14+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
15+
let ret = unsafe {
16+
libc::__wasi_random_get(dest.as_mut_ptr() as *mut libc::c_void, dest.len())
17+
};
18+
if ret == libc::__WASI_ESUCCESS {
19+
Ok(())
20+
} else {
21+
error!("WASI: __wasi_random_get failed with return value {}", ret);
22+
Err(io::Error::last_os_error().into())
23+
}
24+
}
25+
26+
#[inline(always)]
27+
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> { None }

0 commit comments

Comments
 (0)