Skip to content

Commit

Permalink
Merge pull request #20 from nipzu/master
Browse files Browse the repository at this point in the history
Added support for wasm32-unknown-wasi
  • Loading branch information
dhardy authored Apr 6, 2019
2 parents c2f13c8 + 0f411bb commit 3142dee
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ matrix:
- cargo test

- rust: nightly
env: DESCRIPTION="WASM via emscripten, stdweb and wasm-bindgen"
env: DESCRIPTION="WASM via emscripten, stdweb, wasm-bindgen and WASI"
install:
- rustup target add wasm32-unknown-unknown
- rustup target add wasm32-unknown-emscripten
- rustup target add wasm32-unknown-wasi
- nvm install 9
- ./utils/ci/install_cargo_web.sh
- cargo web prepare-emscripten
Expand All @@ -80,6 +81,7 @@ matrix:
#- cargo web test --target wasm32-unknown-emscripten
#- cargo web test --nodejs --target wasm32-unknown-emscripten
#- cargo build --target wasm32-unknown-unknown # without any features
- cargo build --target wasm32-unknown-wasi
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
- cargo web test --target wasm32-unknown-unknown --features=stdweb
- cargo build --manifest-path tests/wasm_bindgen/Cargo.toml --target wasm32-unknown-unknown
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.1.2] - 2019-04-06
- Add support for `wasm32-unknown-wasi` target.

## [0.1.1] - 2019-04-05
- Enable std functionality for CloudABI by default.

Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "getrandom"
version = "0.1.1"
version = "0.1.2"
edition = "2018"
authors = ["The Rand Project Developers"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -36,5 +36,8 @@ fuchsia-cprng = "0.1"
wasm-bindgen = { version = "0.2.29", optional = true }
stdweb = { version = "0.4.9", optional = true }

[target.wasm32-unknown-wasi.dependencies]
libc = "0.2.51"

[features]
std = []
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This library is `no_std` compatible, but uses `std` on most platforms.
The `log` library is supported as an optional dependency. If enabled, error
reporting will be improved on some platforms.

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

- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)
Expand Down
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//! | SGX | RDRAND
//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and ams.js][14])
//! | Node.js | [`crypto.randomBytes`][15] (see [Support for WebAssembly and ams.js][16])
//! | WASI | [`__wasi_random_get`][17]
//!
//! Getrandom doesn't have a blanket implementation for all Unix-like operating
//! systems that reads from `/dev/urandom`. This ensures all supported operating
Expand All @@ -44,6 +45,10 @@
//! features are activated for this crate. Note that if both features are
//! enabled `wasm-bindgen` will be used. If neither feature is enabled,
//! `getrandom` will always fail.
//!
//! The WASI target `wasm32-unknown-wasi` uses the `__wasi_random_get`
//! function defined by the WASI standard.
//!
//!
//! ## Early boot
//!
Expand Down Expand Up @@ -108,6 +113,7 @@
//! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
//! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
//! [16]: #support-for-webassembly-and-amsjs
//! [17]: https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-api.md#__wasi_random_get
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
Expand Down Expand Up @@ -135,7 +141,10 @@ extern crate std;
target_os = "dragonfly",
target_os = "haiku",
target_os = "linux",
target_arch = "wasm32",
all(
target_arch = "wasm32",
not(target_env = "wasi")
),
))]
mod utils;
mod error;
Expand Down Expand Up @@ -181,11 +190,13 @@ mod_use!(cfg(target_os = "redox"), use_file);
mod_use!(cfg(target_os = "solaris"), solaris_illumos);
mod_use!(cfg(windows), windows);
mod_use!(cfg(target_env = "sgx"), sgx);
mod_use!(cfg(target_env = "wasi"), wasi);

mod_use!(
cfg(all(
target_arch = "wasm32",
not(target_os = "emscripten"),
not(target_env = "wasi"),
feature = "wasm-bindgen"
)),
wasm32_bindgen
Expand All @@ -195,6 +206,7 @@ mod_use!(
cfg(all(
target_arch = "wasm32",
not(target_os = "emscripten"),
not(target_env = "wasi"),
not(feature = "wasm-bindgen"),
feature = "stdweb",
)),
Expand Down Expand Up @@ -225,6 +237,7 @@ mod_use!(
target_arch = "wasm32",
any(
target_os = "emscripten",
target_env = "wasi",
feature = "wasm-bindgen",
feature = "stdweb",
),
Expand Down
27 changes: 27 additions & 0 deletions src/wasi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Implementation for WASI
use crate::Error;
use core::num::NonZeroU32;
use std::io;

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let ret = unsafe {
libc::__wasi_random_get(dest.as_mut_ptr() as *mut libc::c_void, dest.len())
};
if ret == libc::__WASI_ESUCCESS {
Ok(())
} else {
error!("WASI: __wasi_random_get failed with return value {}", ret);
Err(io::Error::last_os_error().into())
}
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> { None }

0 comments on commit 3142dee

Please sign in to comment.