Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ matrix:
- cargo doc --no-deps --all-features
after_success:
- travis-cargo --only nightly doc-upload

- rust: nightly
install:
- rustup target add wasm32-unknown-unknown
- cargo install --force cargo-web
script:
- cargo web test --target wasm32-unknown-unknown --nodejs

# Trust cross-built/emulated targets. We must repeat all non-default values.
- rust: stable
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ cloudabi = "0.0.3"

[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-zircon = "0.3.2"

[target.wasm32-unknown-unknown.dependencies]
stdweb = "0.4"
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
#![cfg_attr(not(feature="std"), no_std)]
#![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
#![cfg_attr(feature = "i128_support", feature(i128_type, i128))]
#![cfg_attr(all(target_arch = "wasm32", not(target_os = "emscripten")), recursion_limit="128")]

#[cfg(feature="std")] extern crate std as core;
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
Expand All @@ -257,6 +258,10 @@
#[cfg(feature="serde-1")] extern crate serde;
#[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive;

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[macro_use]
extern crate stdweb;

#[cfg(feature = "log")] #[macro_use] extern crate log;
#[cfg(not(feature = "log"))] macro_rules! trace { ($($x:tt)*) => () }
#[cfg(not(feature = "log"))] macro_rules! debug { ($($x:tt)*) => () }
Expand Down Expand Up @@ -1553,6 +1558,7 @@ mod test {

#[test]
#[cfg(feature="std")]
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
fn test_thread_rng() {
let mut r = thread_rng();
r.gen::<i32>();
Expand Down
91 changes: 85 additions & 6 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,27 +584,98 @@ mod imp {

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
mod imp {
use std::io;
use std::mem;
use stdweb::unstable::TryInto;
use stdweb::web::error::Error as WebError;
use {Error, ErrorKind};

#[derive(Debug)]
pub struct OsRng;
enum OsRngInner {
Browser,
Node
}

#[derive(Debug)]
pub struct OsRng(OsRngInner);

impl OsRng {
pub fn new() -> Result<OsRng, Error> {
Err(Error::new(ErrorKind::Unavailable, "not supported on WASM"))
let result = js! {
try {
if (
typeof window === "object" &&
typeof window.crypto === "object" &&
typeof window.crypto.getRandomValues === "function"
) {
return { success: true, ty: 1 };
}

if (typeof require("crypto").randomBytes === "function") {
return { success: true, ty: 2 };
}

return { success: false, error: new Error("not supported") };
} catch(err) {
return { success: false, error: err };
}
};

if js!{ return @{ result.as_ref() }.success } == true {
let ty = js!{ return @{ result }.ty };

if ty == 1 { Ok(OsRng(OsRngInner::Browser)) }
else if ty == 2 { Ok(OsRng(OsRngInner::Node)) }
else { unreachable!() }
} else {
let err: WebError = js!{ return @{ result }.error }.try_into().unwrap();
Err(Error::with_cause(ErrorKind::Unavailable, "WASM Error", err))
}
}

pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
Err(Error::new(ErrorKind::Unavailable, "not supported on WASM"))
assert_eq!(mem::size_of::<usize>(), 4);

let len = v.len() as u32;
let ptr = v.as_mut_ptr() as i32;

let result = match self.0 {
OsRngInner::Browser => js! {
try {
let array = new Uint8Array(@{ len });
window.crypto.getRandomValues(array);
HEAPU8.set(array, @{ ptr });

return { success: true };
} catch(err) {
return { success: false, error: err };
}
},
OsRngInner::Node => js! {
try {
let bytes = require("crypto").randomBytes(@{ len });
HEAPU8.set(new Uint8Array(bytes), @{ ptr });

return { success: true };
} catch(err) {
return { success: false, error: err };
}
}
};

if js!{ return @{ result.as_ref() }.success } == true {
Ok(())
} else {
let err: WebError = js!{ return @{ result }.error }.try_into().unwrap();
Err(Error::with_cause(ErrorKind::Unexpected, "WASM Error", err))
}
}
}
}

#[cfg(test)]
mod test {
use std::sync::mpsc::channel;
use RngCore;
use OsRng;
use std::thread;

#[test]
fn test_os_rng() {
Expand All @@ -615,10 +686,18 @@ mod test {

let mut v = [0u8; 1000];
r.fill_bytes(&mut v);

let mut v2 = [0u8; 1000];
r.fill_bytes(&mut v2);

assert_ne!(v[..], v2[..]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

#[cfg(not(any(target_arch = "wasm32", target_arch = "asmjs")))]
#[test]
fn test_os_rng_tasks() {
use std::sync::mpsc::channel;
use std::thread;

let mut txs = vec!();
for _ in 0..20 {
Expand Down
12 changes: 6 additions & 6 deletions src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ mod test {
let mut r = ::test::rng(402);

// sample 0 items
assert_eq!(&sample_slice(&mut r, empty, 0)[..], []);
assert_eq!(&sample_slice(&mut r, &[42, 2, 42], 0)[..], []);
assert_eq!(&sample_slice(&mut r, empty, 0)[..], [0u8; 0]);
assert_eq!(&sample_slice(&mut r, &[42, 2, 42], 0)[..], [0u8; 0]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the extra verbosity useful?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, this may seem a rust type inference bug.

screenshot_20180227_224051

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try removing the first &; the function returns a Vec so it's not necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not work.

screenshot_20180227_231025


// sample 1 item
assert_eq!(&sample_slice(&mut r, &[42], 1)[..], [42]);
Expand All @@ -269,12 +269,12 @@ mod test {
let v = sample_slice(&mut r, &[42, 133], 2);
assert!(&v[..] == [42, 133] || v[..] == [133, 42]);

assert_eq!(&sample_indices_inplace(&mut r, 0, 0)[..], []);
assert_eq!(&sample_indices_inplace(&mut r, 1, 0)[..], []);
assert_eq!(&sample_indices_inplace(&mut r, 0, 0)[..], [0usize; 0]);
assert_eq!(&sample_indices_inplace(&mut r, 1, 0)[..], [0usize; 0]);
assert_eq!(&sample_indices_inplace(&mut r, 1, 1)[..], [0]);

assert_eq!(&sample_indices_cache(&mut r, 0, 0)[..], []);
assert_eq!(&sample_indices_cache(&mut r, 1, 0)[..], []);
assert_eq!(&sample_indices_cache(&mut r, 0, 0)[..], [0usize; 0]);
assert_eq!(&sample_indices_cache(&mut r, 1, 0)[..], [0usize; 0]);
assert_eq!(&sample_indices_cache(&mut r, 1, 1)[..], [0]);

// Make sure lucky 777's aren't lucky
Expand Down