Skip to content

Commit

Permalink
wasm: Move JS-based Custom RNGs back into the main crate
Browse files Browse the repository at this point in the history
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 <joerichey@google.com>
  • Loading branch information
josephlr committed May 29, 2020
1 parent 6aba12c commit eacffd5
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 82 deletions.
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ exclude = ["utils/*", ".*", "appveyor.yml"]
travis-ci = { repository = "rust-random/getrandom" }
appveyor = { repository = "rust-random/getrandom" }

[workspace]
members = [
"custom/stdweb",
"custom/wasm-bindgen",
]

[dependencies]
cfg-if = "0.1.2"

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

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", cargo_web))'.dependencies]
stdweb = { version = "0.4.18", default-features = false, optional = true }
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies]
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies]
wasm-bindgen-test = "0.3.12"

[features]
std = []
# Feature to enable fallback RDRAND-based implementation
rdrand = []
# Feature to enable JavaScript bindings on wasm32-unknown-unknown
js = ["stdweb", "wasm-bindgen"]
# Feature to enable custom RNG implementations
custom = []
# Unstable feature to support being a libstd dependency
Expand Down
17 changes: 0 additions & 17 deletions custom/stdweb/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions custom/stdweb/tests/test.rs

This file was deleted.

20 changes: 0 additions & 20 deletions custom/wasm-bindgen/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions custom/wasm-bindgen/tests/node.rs

This file was deleted.

9 changes: 0 additions & 9 deletions custom/wasm-bindgen/tests/web.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ cfg_if! {
} else if #[cfg(all(feature = "rdrand",
any(target_arch = "x86_64", target_arch = "x86")))] {
#[path = "rdrand.rs"] mod imp;
} else if #[cfg(all(feature = "js",
target_arch = "wasm32", target_os = "unknown"))] {
#[cfg_attr(cargo_web, path = "stdweb.rs")]
#[cfg_attr(not(cargo_web), path = "wasm-bindgen.rs")]
mod imp;
} else if #[cfg(feature = "custom")] {
use custom as imp;
} else {
Expand Down
12 changes: 3 additions & 9 deletions custom/stdweb/src/lib.rs → src/stdweb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::Error;

#![recursion_limit = "128"]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");

extern crate std;
use std::thread_local;

use stdweb::js;

use getrandom::{register_custom_getrandom, Error};

#[derive(Clone, Copy, PartialEq)]
enum RngSource {
Browser,
Expand All @@ -26,9 +22,7 @@ thread_local!(
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
);

register_custom_getrandom!(getrandom_inner);

fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
RNG_SOURCE.with(|&source| getrandom_fill(source?, dest))
}

Expand Down
10 changes: 3 additions & 7 deletions custom/wasm-bindgen/src/lib.rs → src/wasm-bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::Error;

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
compile_error!("This crate is only for the `wasm32-unknown-unknown` target");

extern crate std;
use std::thread_local;

use wasm_bindgen::prelude::*;

use getrandom::{register_custom_getrandom, Error};

enum RngSource {
Node(NodeCrypto),
Expand All @@ -26,9 +24,7 @@ thread_local!(
static RNG_SOURCE: Result<RngSource, Error> = getrandom_init();
);

register_custom_getrandom!(getrandom_inner);

fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
RNG_SOURCE.with(|result| {
let source = result.as_ref().map_err(|&e| e)?;

Expand Down

0 comments on commit eacffd5

Please sign in to comment.