diff --git a/.travis.yml b/.travis.yml index b211d420..70bed8b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -75,7 +75,8 @@ matrix: - 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 + - cd ../wasm-bindgen + - cargo test --target wasm32-unknown-unknown - GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser - CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --features=test-in-browser @@ -84,6 +85,7 @@ matrix: rust: nightly os: linux install: + - rustup target add wasm32-unknown-unknown - cargo --list | egrep "^\s*deadlinks$" -q || cargo install cargo-deadlinks - cargo deadlinks -V script: @@ -96,6 +98,7 @@ matrix: # remove cached documentation, otherwise files from previous PRs can get included - rm -rf target/doc - cargo doc --no-deps --features=std,custom + - cargo doc --no-deps --manifest-path=custom/wasm-bindgen/Cargo.toml --target=wasm32-unknown-unknown - cargo deadlinks --dir target/doc # also test minimum dependency versions are usable - cargo generate-lockfile -Z minimal-versions diff --git a/Cargo.toml b/Cargo.toml index 50619b57..b576b840 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ appveyor = { repository = "rust-random/getrandom" } [workspace] members = [ "custom/stdweb", + "custom/wasm-bindgen", ] [dependencies] diff --git a/custom/wasm-bindgen/Cargo.toml b/custom/wasm-bindgen/Cargo.toml new file mode 100644 index 00000000..93202fa6 --- /dev/null +++ b/custom/wasm-bindgen/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "wasm-bindgen-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 wasm-bindgen" +documentation = "https://docs.rs/wasm-bindgen-getrandom" +repository = "https://github.com/rust-random/getrandom" +categories = ["wasm"] + +[dependencies] +getrandom = { path = "../..", version = "0.2", features = ["custom"] } +wasm-bindgen = "0.2.29" + +[dev-dependencies] +wasm-bindgen-test = "0.2" + +# Test-only features allowing us to reuse most of the code in common.rs +[features] +default = ["test-bindgen"] +test-bindgen = [] +test-in-browser = ["test-bindgen"] + +[[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_bindgen.rs b/custom/wasm-bindgen/src/lib.rs similarity index 92% rename from src/wasm32_bindgen.rs rename to custom/wasm-bindgen/src/lib.rs index 42d3eda9..521ff198 100644 --- a/src/wasm32_bindgen.rs +++ b/custom/wasm-bindgen/src/lib.rs @@ -6,8 +6,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Implementation for WASM via wasm-bindgen -extern crate std; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +compile_error!("This crate is only for the `wasm32-unknown-unknown` target"); use core::cell::RefCell; use core::mem; @@ -15,7 +15,7 @@ use std::thread_local; use wasm_bindgen::prelude::*; -use crate::Error; +use getrandom::{register_custom_getrandom, Error}; #[derive(Clone, Debug)] enum RngSource { @@ -29,7 +29,9 @@ thread_local!( static RNG_SOURCE: RefCell> = RefCell::new(None); ); -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); RNG_SOURCE.with(|f| { diff --git a/tests/common.rs b/tests/common.rs index d629e02c..a19af2d5 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,8 +1,10 @@ // Explicitly use the Custom RNG crates to link them in. #[cfg(feature = "test-stdweb")] use stdweb_getrandom as _; +#[cfg(feature = "test-bindgen")] +use wasm_bindgen_getrandom as _; -#[cfg(feature = "wasm-bindgen")] +#[cfg(feature = "test-bindgen")] use wasm_bindgen_test::*; use getrandom::getrandom; @@ -10,14 +12,14 @@ use getrandom::getrandom; #[cfg(feature = "test-in-browser")] wasm_bindgen_test_configure!(run_in_browser); -#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)] +#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)] #[test] fn test_zero() { // Test that APIs are happy with zero-length requests getrandom(&mut [0u8; 0]).unwrap(); } -#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)] +#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)] #[test] fn test_diff() { let mut v1 = [0u8; 1000]; @@ -35,7 +37,7 @@ fn test_diff() { assert!(n_diff_bits >= v1.len() as u32); } -#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)] +#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)] #[test] fn test_huge() { let mut huge = [0u8; 100_000];