Skip to content

Commit 628cda2

Browse files
committed
custom: Add custom RNG for stdweb
1 parent 3f8379f commit 628cda2

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
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

Lines changed: 5 additions & 0 deletions
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
log = { version = "0.4", optional = true }
1924
cfg-if = "0.1.2"

custom/stdweb/Cargo.toml

Lines changed: 26 additions & 0 deletions
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 renamed to custom/stdweb/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
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;
@@ -16,15 +17,17 @@ use stdweb::js;
1617
use stdweb::unstable::TryInto;
1718
use stdweb::web::error::Error as WebError;
1819

19-
use crate::Error;
20+
use getrandom::{register_custom_getrandom, Error};
2021

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

27-
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
28+
register_custom_getrandom!(getrandom_inner);
29+
30+
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2831
assert_eq!(mem::size_of::<usize>(), 4);
2932
static ONCE: Once = Once::new();
3033
static mut RNG_SOURCE: Result<RngSource, Error> = Ok(RngSource::Node);
@@ -68,8 +71,6 @@ fn getrandom_init() -> Result<RngSource, Error> {
6871
unreachable!()
6972
}
7073
} else {
71-
let _err: WebError = js! { return @{ result }.error }.try_into().unwrap();
72-
error!("getrandom unavailable: {}", _err);
7374
Err(Error::STDWEB_NO_RNG)
7475
}
7576
}
@@ -104,8 +105,6 @@ fn getrandom_fill(source: RngSource, dest: &mut [u8]) -> Result<(), Error> {
104105
};
105106

106107
if js! { return @{ result.as_ref() }.success } != true {
107-
let _err: WebError = js! { return @{ result }.error }.try_into().unwrap();
108-
error!("getrandom failed: {}", _err);
109108
return Err(Error::STDWEB_RNG_FAILED);
110109
}
111110
}

tests/common.rs

Lines changed: 4 additions & 0 deletions
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)