Skip to content

Commit 37f18a2

Browse files
committed
custom: Add Custom RNG for wasm-bindgen
1 parent eaf3077 commit 37f18a2

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ matrix:
7575
- cargo web test --nodejs --target=wasm32-unknown-unknown
7676
- cargo web test --target=wasm32-unknown-unknown
7777
# wasm-bindgen tests (Node, Firefox, Chrome)
78-
- cargo test --target wasm32-unknown-unknown --features=wasm-bindgen
78+
- cd ../wasm-bindgen
79+
- cargo test --target wasm32-unknown-unknown
7980
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
8081
- CHROMEDRIVER=$HOME/chromedriver cargo test --target wasm32-unknown-unknown --features=test-in-browser
8182

@@ -97,6 +98,7 @@ matrix:
9798
# remove cached documentation, otherwise files from previous PRs can get included
9899
- rm -rf target/doc
99100
- cargo doc --no-deps --features=std,custom
101+
- cargo doc --no-deps --manifest-path=custom/wasm-bindgen/Cargo.toml --target=wasm32-unknown-unknown
100102
- cargo deadlinks --dir target/doc
101103
# also test minimum dependency versions are usable
102104
- cargo generate-lockfile -Z minimal-versions

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ appveyor = { repository = "rust-random/getrandom" }
1717
[workspace]
1818
members = [
1919
"custom/stdweb",
20+
"custom/wasm-bindgen",
2021
]
2122

2223
[dependencies]

custom/wasm-bindgen/Cargo.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "wasm-bindgen-getrandom"
3+
version = "0.1.0"
4+
edition = "2018"
5+
authors = ["The Rand Project Developers"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Custom shim for using getrandom with wasm-bindgen"
8+
documentation = "https://docs.rs/wasm-bindgen-getrandom"
9+
repository = "https://github.com/rust-random/getrandom"
10+
categories = ["wasm"]
11+
12+
[dependencies]
13+
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
14+
wasm-bindgen = "0.2.29"
15+
16+
[dev-dependencies]
17+
wasm-bindgen-test = "0.2"
18+
19+
# Test-only features allowing us to reuse most of the code in common.rs
20+
[features]
21+
default = ["test-bindgen"]
22+
test-bindgen = []
23+
test-in-browser = ["test-bindgen"]
24+
25+
[[test]]
26+
name = "common"
27+
path = "../../tests/common.rs"
28+
29+
[package.metadata.docs.rs]
30+
default-target = "wasm32-unknown-unknown"

src/wasm32_bindgen.rs custom/wasm-bindgen/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Implementation for WASM via wasm-bindgen
10-
extern crate std;
9+
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
10+
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");
1111

1212
use core::cell::RefCell;
1313
use core::mem;
1414
use std::thread_local;
1515

1616
use wasm_bindgen::prelude::*;
1717

18-
use crate::Error;
18+
use getrandom::{register_custom_getrandom, Error};
1919

2020
#[derive(Clone, Debug)]
2121
enum RngSource {
@@ -29,7 +29,9 @@ thread_local!(
2929
static RNG_SOURCE: RefCell<Option<RngSource>> = RefCell::new(None);
3030
);
3131

32-
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
32+
register_custom_getrandom!(getrandom_inner);
33+
34+
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3335
assert_eq!(mem::size_of::<usize>(), 4);
3436

3537
RNG_SOURCE.with(|f| {

tests/common.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
// Explicitly use the Custom RNG crates to link them in.
22
#[cfg(feature = "test-stdweb")]
33
use stdweb_getrandom as _;
4+
#[cfg(feature = "test-bindgen")]
5+
use wasm_bindgen_getrandom as _;
46

5-
#[cfg(feature = "wasm-bindgen")]
7+
#[cfg(feature = "test-bindgen")]
68
use wasm_bindgen_test::*;
79

810
use getrandom::getrandom;
911

1012
#[cfg(feature = "test-in-browser")]
1113
wasm_bindgen_test_configure!(run_in_browser);
1214

13-
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)]
15+
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
1416
#[test]
1517
fn test_zero() {
1618
// Test that APIs are happy with zero-length requests
1719
getrandom(&mut [0u8; 0]).unwrap();
1820
}
1921

20-
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)]
22+
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
2123
#[test]
2224
fn test_diff() {
2325
let mut v1 = [0u8; 1000];
@@ -35,7 +37,7 @@ fn test_diff() {
3537
assert!(n_diff_bits >= v1.len() as u32);
3638
}
3739

38-
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen_test)]
40+
#[cfg_attr(feature = "test-bindgen", wasm_bindgen_test)]
3941
#[test]
4042
fn test_huge() {
4143
let mut huge = [0u8; 100_000];

0 commit comments

Comments
 (0)