Skip to content

Commit 28eaa7e

Browse files
committed
getrandom-js: Merge wasm Custom RNG crates
Signed-off-by: Joe Richey <[email protected]>
1 parent c5145ab commit 28eaa7e

File tree

7 files changed

+40
-30
lines changed

7 files changed

+40
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ appveyor = { repository = "rust-random/getrandom" }
1616

1717
[workspace]
1818
members = [
19-
"custom/stdweb",
20-
"custom/wasm-bindgen",
19+
"custom/js",
2120
]
2221

2322
[dependencies]

custom/js/Cargo.toml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
[package]
2-
name = "wasm-bindgen-getrandom"
2+
name = "getrandom-js"
33
version = "0.1.0"
44
edition = "2018"
55
authors = ["The Rand Project Developers"]
66
license = "MIT OR Apache-2.0"
7-
description = "Custom shim for using getrandom with wasm-bindgen"
8-
documentation = "https://docs.rs/wasm-bindgen-getrandom"
7+
description = "Custom shim for using getrandom with JavaScript"
8+
documentation = "https://docs.rs/getrandom-js"
99
repository = "https://github.com/rust-random/getrandom"
1010
categories = ["wasm"]
1111

1212
[dependencies]
1313
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
14-
wasm-bindgen = "0.2.46"
14+
cfg-if = "0.1.2"
1515

16-
[dev-dependencies]
17-
wasm-bindgen-test = "0.2.46"
16+
[target.'cfg(cargo_web)'.dependencies]
17+
stdweb = "0.4.18"
18+
19+
[target.'cfg(not(cargo_web))'.dependencies]
20+
wasm-bindgen = "0.2.62"
21+
22+
[target.'cfg(not(cargo_web))'.dev-dependencies]
23+
wasm-bindgen-test = "0.3.12"
1824

1925
[package.metadata.docs.rs]
2026
default-target = "wasm32-unknown-unknown"

custom/js/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Necessary to work around complex macros in stdweb
2+
#![cfg_attr(cargo_web, recursion_limit = "128")]
3+
4+
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
5+
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");
6+
7+
#[cfg_attr(cargo_web, path = "stdweb.rs")]
8+
#[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")]
9+
mod imp;
10+
11+
getrandom::register_custom_getrandom!(imp::getrandom_inner);

custom/js/src/stdweb.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
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");
12-
138
use std::thread_local;
149

1510
use stdweb::js;
1611

17-
use getrandom::{register_custom_getrandom, Error};
12+
use getrandom::Error;
1813

1914
#[derive(Clone, Copy, PartialEq)]
2015
enum RngSource {
@@ -26,9 +21,7 @@ thread_local!(
2621
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
2722
);
2823

29-
register_custom_getrandom!(getrandom_inner);
30-
31-
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
24+
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3225
RNG_SOURCE.with(|&source| getrandom_fill(source?, dest))
3326
}
3427

custom/js/src/wasm-bindgen.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
9-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
10-
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");
11-
128
use std::thread_local;
139

1410
use wasm_bindgen::prelude::*;
1511

16-
use getrandom::{register_custom_getrandom, Error};
12+
use getrandom::Error;
1713

1814
enum RngSource {
1915
Node(NodeCrypto),
@@ -26,9 +22,7 @@ thread_local!(
2622
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
2723
);
2824

29-
register_custom_getrandom!(getrandom_inner);
30-
31-
fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
25+
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3226
RNG_SOURCE.with(|result| {
3327
let source = result.as_ref().map_err(|&e| e)?;
3428

custom/js/tests/node.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Explicitly use the Custom RNG crate to link it in.
2-
use wasm_bindgen_getrandom as _;
3-
42
use getrandom::getrandom;
3+
use getrandom_js as _;
4+
5+
// We don't use wasm_bindgen when building with cargo web
6+
#[cfg(cargo_web)]
7+
use test;
8+
#[cfg(not(cargo_web))]
59
use wasm_bindgen_test::wasm_bindgen_test as test;
10+
611
#[path = "../../../src/test_common.rs"]
712
mod test_common;

custom/js/tests/web.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Don't use the wasm_bindgen browser framework with cargo web
2+
#![cfg(not(cargo_web))]
13
// Explicitly use the Custom RNG crate to link it in.
2-
use wasm_bindgen_getrandom as _;
4+
use getrandom::getrandom;
5+
use getrandom_js as _;
36

47
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
5-
6-
use getrandom::getrandom;
78
use wasm_bindgen_test::wasm_bindgen_test as test;
9+
810
#[path = "../../../src/test_common.rs"]
911
mod test_common;

0 commit comments

Comments
 (0)