Fix #682: move __wbg_shims module into rand_os

This commit is contained in:
Diggory Hardy 2019-01-08 10:37:04 +00:00
parent f5e749c6a3
commit 3145af1a0b
3 changed files with 56 additions and 56 deletions

View File

@ -148,7 +148,7 @@ matrix:
#- cargo web test --target wasm32-unknown-emscripten
#- cargo web test --nodejs --target wasm32-unknown-emscripten
#- cargo build --target wasm32-unknown-unknown # without any features
- cargo build --target wasm32-unknown-unknown --features=rand_os/wasm-bindgen
- cargo build --target wasm32-unknown-unknown --features=wasm-bindgen
- cd rand_os && cargo web test --target wasm32-unknown-unknown --features=stdweb
- rust: nightly

View File

@ -358,3 +358,58 @@ compile_error!("enable either wasm_bindgen or stdweb feature");
target_arch = "wasm32",
)))]
compile_error!("OS RNG support is not available for this platform");
// Due to rustwasm/wasm-bindgen#201 this can't be defined in the inner os
// modules, so hack around it for now and place it at the root.
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub mod __wbg_shims {
// `extern { type Foo; }` isn't supported on 1.22 syntactically, so use a
// macro to work around that.
macro_rules! rust_122_compat {
($($t:tt)*) => ($($t)*)
}
rust_122_compat! {
extern crate wasm_bindgen;
pub use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
pub type Function;
#[wasm_bindgen(constructor)]
pub fn new(s: &str) -> Function;
#[wasm_bindgen(method)]
pub fn call(this: &Function, self_: &JsValue) -> JsValue;
pub type This;
#[wasm_bindgen(method, getter, structural, js_name = self)]
pub fn self_(me: &This) -> JsValue;
#[wasm_bindgen(method, getter, structural)]
pub fn crypto(me: &This) -> JsValue;
#[derive(Clone, Debug)]
pub type BrowserCrypto;
// TODO: these `structural` annotations here ideally wouldn't be here to
// avoid a JS shim, but for now with feature detection they're
// unavoidable.
#[wasm_bindgen(method, js_name = getRandomValues, structural, getter)]
pub fn get_random_values_fn(me: &BrowserCrypto) -> JsValue;
#[wasm_bindgen(method, js_name = getRandomValues, structural)]
pub fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]);
#[wasm_bindgen(js_name = require)]
pub fn node_require(s: &str) -> NodeCrypto;
#[derive(Clone, Debug)]
pub type NodeCrypto;
#[wasm_bindgen(method, js_name = randomFillSync, structural)]
pub fn random_fill_sync(me: &NodeCrypto, buf: &mut [u8]);
}
}
}

View File

@ -712,61 +712,6 @@ pub fn random<T>() -> T where Standard: Distribution<T> {
thread_rng().gen()
}
// Due to rustwasm/wasm-bindgen#201 this can't be defined in the inner os
// modules, so hack around it for now and place it at the root.
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub mod __wbg_shims {
// `extern { type Foo; }` isn't supported on 1.22 syntactically, so use a
// macro to work around that.
macro_rules! rust_122_compat {
($($t:tt)*) => ($($t)*)
}
rust_122_compat! {
extern crate wasm_bindgen;
pub use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
pub type Function;
#[wasm_bindgen(constructor)]
pub fn new(s: &str) -> Function;
#[wasm_bindgen(method)]
pub fn call(this: &Function, self_: &JsValue) -> JsValue;
pub type This;
#[wasm_bindgen(method, getter, structural, js_name = self)]
pub fn self_(me: &This) -> JsValue;
#[wasm_bindgen(method, getter, structural)]
pub fn crypto(me: &This) -> JsValue;
#[derive(Clone, Debug)]
pub type BrowserCrypto;
// TODO: these `structural` annotations here ideally wouldn't be here to
// avoid a JS shim, but for now with feature detection they're
// unavoidable.
#[wasm_bindgen(method, js_name = getRandomValues, structural, getter)]
pub fn get_random_values_fn(me: &BrowserCrypto) -> JsValue;
#[wasm_bindgen(method, js_name = getRandomValues, structural)]
pub fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]);
#[wasm_bindgen(js_name = require)]
pub fn node_require(s: &str) -> NodeCrypto;
#[derive(Clone, Debug)]
pub type NodeCrypto;
#[wasm_bindgen(method, js_name = randomFillSync, structural)]
pub fn random_fill_sync(me: &NodeCrypto, buf: &mut [u8]);
}
}
}
#[cfg(test)]
mod test {
use rngs::mock::StepRng;