Skip to content

Commit

Permalink
custom: Add Custom RNG for wasm-bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jan 9, 2020
1 parent 4fcfba2 commit e8d6f15
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -97,6 +99,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
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ appveyor = { repository = "rust-random/getrandom" }
[workspace]
members = [
"custom/stdweb",
"custom/wasm-bindgen",
]

[dependencies]
Expand Down
30 changes: 30 additions & 0 deletions custom/wasm-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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:/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"
10 changes: 6 additions & 4 deletions src/wasm32_bindgen.rs → custom/wasm-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
// 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;
use std::thread_local;

use wasm_bindgen::prelude::*;

use crate::Error;
use getrandom::{register_custom_getrandom, Error};

#[derive(Clone, Debug)]
enum RngSource {
Expand All @@ -29,7 +29,9 @@ thread_local!(
static RNG_SOURCE: RefCell<Option<RngSource>> = 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::<usize>(), 4);

RNG_SOURCE.with(|f| {
Expand Down
10 changes: 6 additions & 4 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
// 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;

#[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];
Expand All @@ -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];
Expand Down

0 comments on commit e8d6f15

Please sign in to comment.