Merge pull request #20 from nipzu/master
Added support for wasm32-unknown-wasi
This commit is contained in:
commit
3142dee2f3
@ -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
|
||||
@ -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
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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"
|
||||
@ -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 = []
|
||||
|
@ -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)
|
||||
|
15
src/lib.rs
15
src/lib.rs
@ -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
|
||||
@ -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
|
||||
//!
|
||||
@ -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",
|
||||
@ -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;
|
||||
@ -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
|
||||
@ -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",
|
||||
)),
|
||||
@ -225,6 +237,7 @@ mod_use!(
|
||||
target_arch = "wasm32",
|
||||
any(
|
||||
target_os = "emscripten",
|
||||
target_env = "wasi",
|
||||
feature = "wasm-bindgen",
|
||||
feature = "stdweb",
|
||||
),
|
||||
|
27
src/wasi.rs
Normal file
27
src/wasi.rs
Normal file
@ -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 }
|
Loading…
x
Reference in New Issue
Block a user