diff --git a/.travis.yml b/.travis.yml index 716757b..b211d42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 2f5df22..50619b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/custom/stdweb/Cargo.toml b/custom/stdweb/Cargo.toml new file mode 100644 index 0000000..2b94124 --- /dev/null +++ b/custom/stdweb/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/wasm32_stdweb.rs b/custom/stdweb/src/lib.rs similarity index 90% rename from src/wasm32_stdweb.rs rename to custom/stdweb/src/lib.rs index 4e28d78..adfb339 100644 --- a/src/wasm32_stdweb.rs +++ b/custom/stdweb/src/lib.rs @@ -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::(), 4); static ONCE: Once = Once::new(); static mut RNG_SOURCE: Result = Ok(RngSource::Node); diff --git a/tests/common.rs b/tests/common.rs index afefa03..d629e02 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -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::*;