Skip to content

Commit 96c731b

Browse files
committed
custom: Add custom RNG for stdweb
1 parent 4057b91 commit 96c731b

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ matrix:
7171
# wasi tests
7272
- cargo test --target wasm32-wasi
7373
# stdweb tests (Node, Chrome)
74-
- cargo web test --nodejs --target=wasm32-unknown-unknown --features=stdweb
75-
- cargo web test --target=wasm32-unknown-unknown --features=stdweb
74+
- cd custom/stdweb
75+
- cargo web test --nodejs --target=wasm32-unknown-unknown
76+
- cargo web test --target=wasm32-unknown-unknown
7677
# wasm-bindgen tests (Node, Firefox, Chrome)
7778
- cargo test --target wasm32-unknown-unknown --features=wasm-bindgen
7879
- GECKODRIVER=$HOME/geckodriver cargo test --target wasm32-unknown-unknown --features=test-in-browser

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ exclude = ["utils/*", ".*", "appveyor.yml"]
1414
travis-ci = { repository = "rust-random/getrandom" }
1515
appveyor = { repository = "rust-random/getrandom" }
1616

17+
[workspace]
18+
members = [
19+
"custom/stdweb",
20+
]
21+
1722
[dependencies]
1823
cfg-if = "0.1.2"
1924

custom/stdweb/Cargo.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "stdweb-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 stdweb"
8+
documentation = "https://docs.rs/stdweb-getrandom"
9+
repository = "https://github.com/rust-random/getrandom"
10+
categories = ["wasm"]
11+
12+
[dependencies]
13+
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
14+
stdweb = "0.4.18"
15+
16+
# Test-only features allowing us to reuse most of the code in common.rs
17+
[features]
18+
default = ["test-stdweb"]
19+
test-stdweb = []
20+
21+
[[test]]
22+
name = "common"
23+
path = "../../tests/common.rs"
24+
25+
[package.metadata.docs.rs]
26+
default-target = "wasm32-unknown-unknown"

src/wasm32_stdweb.rs custom/stdweb/src/lib.rs

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

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

1213
use core::mem;
1314
use std::sync::Once;
1415

1516
use stdweb::js;
1617

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

1920
#[derive(Clone, Copy, Debug)]
2021
enum RngSource {
2122
Browser,
2223
Node,
2324
}
2425

25-
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
26+
register_custom_getrandom!(getrandom_inner);
27+
28+
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2629
assert_eq!(mem::size_of::<usize>(), 4);
2730
static ONCE: Once = Once::new();
2831
static mut RNG_SOURCE: Result<RngSource, Error> = Ok(RngSource::Node);

tests/common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Explicitly use the Custom RNG crates to link them in.
2+
#[cfg(feature = "test-stdweb")]
3+
use stdweb_getrandom as _;
4+
15
#[cfg(feature = "wasm-bindgen")]
26
use wasm_bindgen_test::*;
37

0 commit comments

Comments
 (0)