custom: Add custom RNG for stdweb
This commit is contained in:
committed by
Artyom Pavlov
parent
3d7a7b5f06
commit
7d558f1cc3
+3
-2
@@ -71,8 +71,9 @@ matrix:
|
||||
# wasi tests
|
||||
- cargo test --target wasm32-wasi
|
||||
# stdweb tests (Node, Chrome)
|
||||
- cargo web test --nodejs --target=wasm32-unknown-unknown --features=stdweb
|
||||
- cargo web test --target=wasm32-unknown-unknown --features=stdweb
|
||||
- cd custom/stdweb
|
||||
- cargo web test --nodejs --target=wasm32-unknown-unknown
|
||||
- cargo web test --target=wasm32-unknown-unknown
|
||||
# wasm-bindgen tests (Node, Firefox, Chrome)
|
||||
- cargo test --target wasm32-unknown-unknown --features=wasm-bindgen
|
||||
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
|
||||
|
||||
@@ -14,6 +14,11 @@ exclude = ["utils/*", ".*", "appveyor.yml"]
|
||||
travis-ci = { repository = "rust-random/getrandom" }
|
||||
appveyor = { repository = "rust-random/getrandom" }
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
"custom/stdweb",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "0.1.2"
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "stdweb-getrandom"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
authors = ["The Rand Project Developers"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Custom shim for using getrandom with stdweb"
|
||||
documentation = "https://docs.rs/stdweb-getrandom"
|
||||
repository = "https://github.com/rust-random/getrandom"
|
||||
categories = ["wasm"]
|
||||
|
||||
[dependencies]
|
||||
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
|
||||
stdweb = "0.4.18"
|
||||
|
||||
# Test-only features allowing us to reuse most of the code in common.rs
|
||||
[features]
|
||||
default = ["test-stdweb"]
|
||||
test-stdweb = []
|
||||
|
||||
[[test]]
|
||||
name = "common"
|
||||
path = "../../tests/common.rs"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
default-target = "wasm32-unknown-unknown"
|
||||
@@ -6,15 +6,16 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Implementation for WASM via stdweb
|
||||
extern crate std;
|
||||
#![recursion_limit = "128"]
|
||||
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
|
||||
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");
|
||||
|
||||
use core::mem;
|
||||
use std::sync::Once;
|
||||
|
||||
use stdweb::js;
|
||||
|
||||
use crate::Error;
|
||||
use getrandom::{register_custom_getrandom, Error};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum RngSource {
|
||||
@@ -22,7 +23,9 @@ enum RngSource {
|
||||
Node,
|
||||
}
|
||||
|
||||
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
|
||||
register_custom_getrandom!(getrandom_inner);
|
||||
|
||||
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
|
||||
assert_eq!(mem::size_of::<usize>(), 4);
|
||||
static ONCE: Once = Once::new();
|
||||
static mut RNG_SOURCE: Result<RngSource, Error> = Ok(RngSource::Node);
|
||||
@@ -1,3 +1,7 @@
|
||||
// Explicitly use the Custom RNG crates to link them in.
|
||||
#[cfg(feature = "test-stdweb")]
|
||||
use stdweb_getrandom as _;
|
||||
|
||||
#[cfg(feature = "wasm-bindgen")]
|
||||
use wasm_bindgen_test::*;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user