From cbd99e5e8822018f028464927dac13beb4007bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Thu, 30 Nov 2017 20:52:56 +0100 Subject: [PATCH] Add wasm support --- src/jitter.rs | 16 ++++++++++++---- src/os.rs | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/jitter.rs b/src/jitter.rs index 0e5b1bd0981..aeb9fd5d800 100644 --- a/src/jitter.rs +++ b/src/jitter.rs @@ -171,11 +171,11 @@ impl JitterRng { ec } - + /// Configures how many rounds are used to generate each 64-bit value. /// This must be greater than zero, and has a big impact on performance /// and output quality. - /// + /// /// `new_with_timer` conservatively uses 64 rounds, but often less rounds /// can be used. The `test_timer()` function returns the minimum number of /// rounds required for full strength (platform dependent), so one may use @@ -437,6 +437,9 @@ impl JitterRng { // or check for `CONFIG_X86_TSC`, but it does not make much sense as the // following sanity checks verify that we have a high-resolution timer. + #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + return Err(TimerError::NoTimer); + let mut delta_sum = 0; let mut old_delta = 0; @@ -549,7 +552,7 @@ impl JitterRng { const FACTOR: u32 = 3; fn log2(x: u64) -> u32 { 64 - x.leading_zeros() } - + // pow(δ, FACTOR) must be representable; if you have overflow reduce FACTOR Ok(64 * 2 * FACTOR / (log2(delta_average.pow(FACTOR)) + 1)) } @@ -654,7 +657,7 @@ impl JitterRng { } } -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows")))] +#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows", all(target_arch = "wasm32", not(target_os = "emscripten")))))] fn get_nstime() -> u64 { use std::time::{SystemTime, UNIX_EPOCH}; @@ -693,6 +696,11 @@ fn get_nstime() -> u64 { t as u64 } +#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +fn get_nstime() -> u64 { + unreachable!() +} + // A function that is opaque to the optimizer to assist in avoiding dead-code // elimination. Taken from `bencher`. fn black_box(dummy: T) -> T { diff --git a/src/os.rs b/src/os.rs index a54e4eaa323..7ff61a738a5 100644 --- a/src/os.rs +++ b/src/os.rs @@ -544,6 +544,26 @@ mod imp { } } +#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +mod imp { + use std::io; + use Rng; + + #[derive(Debug)] + pub struct OsRng; + + impl OsRng { + pub fn new() -> io::Result { + Err(io::Error::new(io::ErrorKind::Other, "Not supported")) + } + } + + impl Rng for OsRng { + fn next_u32(&mut self) -> u32 { + panic!("Not supported") + } + } +} #[cfg(test)] mod test {