Skip to content

Commit b5bcb4f

Browse files
committed
wasm: Move JS-based Custom RNGs back into the main crate
They will be gated behind the "js" feature, as we can now do detect, at compile-time, which implementation (wasm-bindgen vs stdweb) we should use. The "js" implementation takes precedence over the "custom" implementation. This prevents issues that arise from the buggy way Cargo handles features across multiple targets. Signed-off-by: Joe Richey <[email protected]>
1 parent 6aba12c commit b5bcb4f

File tree

9 files changed

+20
-82
lines changed

9 files changed

+20
-82
lines changed

Cargo.toml

+9-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ 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-
"custom/wasm-bindgen",
21-
]
22-
2317
[dependencies]
2418
cfg-if = "0.1.2"
2519

@@ -33,10 +27,19 @@ libc = { version = "0.2.64", default-features = false }
3327
[target.'cfg(target_os = "wasi")'.dependencies]
3428
wasi = "0.9"
3529

30+
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", cargo_web))'.dependencies]
31+
stdweb = { version = "0.4.18", default-features = false, optional = true }
32+
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies]
33+
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
34+
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies]
35+
wasm-bindgen-test = "0.3.12"
36+
3637
[features]
3738
std = []
3839
# Feature to enable fallback RDRAND-based implementation
3940
rdrand = []
41+
# Feature to enable JavaScript bindings on wasm32-unknown-unknown
42+
js = ["stdweb", "wasm-bindgen"]
4043
# Feature to enable custom RNG implementations
4144
custom = []
4245
# Unstable feature to support being a libstd dependency

custom/stdweb/Cargo.toml

-17
This file was deleted.

custom/stdweb/tests/test.rs

-7
This file was deleted.

custom/wasm-bindgen/Cargo.toml

-20
This file was deleted.

custom/wasm-bindgen/tests/node.rs

-7
This file was deleted.

custom/wasm-bindgen/tests/web.rs

-9
This file was deleted.

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ cfg_if! {
186186
} else if #[cfg(all(feature = "rdrand",
187187
any(target_arch = "x86_64", target_arch = "x86")))] {
188188
#[path = "rdrand.rs"] mod imp;
189+
} else if #[cfg(all(feature = "js",
190+
target_arch = "wasm32", target_os = "unknown"))] {
191+
#[cfg_attr(cargo_web, path = "stdweb.rs")]
192+
#[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")]
193+
mod imp;
189194
} else if #[cfg(feature = "custom")] {
190195
use custom as imp;
191196
} else {

custom/stdweb/src/lib.rs renamed to src/stdweb.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
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+
use crate::Error;
89

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-
10+
extern crate std;
1311
use std::thread_local;
1412

1513
use stdweb::js;
1614

17-
use getrandom::{register_custom_getrandom, Error};
18-
1915
#[derive(Clone, Copy, PartialEq)]
2016
enum RngSource {
2117
Browser,
@@ -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(|&source| getrandom_fill(source?, dest))
3327
}
3428

custom/wasm-bindgen/src/lib.rs renamed to src/wasm-bindgen.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
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+
use crate::Error;
89

9-
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
10-
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");
11-
10+
extern crate std;
1211
use std::thread_local;
1312

1413
use wasm_bindgen::prelude::*;
1514

16-
use getrandom::{register_custom_getrandom, Error};
1715

1816
enum RngSource {
1917
Node(NodeCrypto),
@@ -26,9 +24,7 @@ thread_local!(
2624
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
2725
);
2826

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

0 commit comments

Comments
 (0)