Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wasm-bindgen rand bindings #600

Merged
merged 1 commit into from Sep 7, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lib.rs
Expand Up @@ -229,7 +229,6 @@
#![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
#![cfg_attr(all(feature="simd_support", feature="nightly"), feature(stdsimd))]
#![cfg_attr(feature = "stdweb", recursion_limit="128")]
#![cfg_attr(feature = "wasm-bindgen", feature(use_extern_macros))]

#[cfg(feature = "std")] extern crate core;
#[cfg(all(feature = "alloc", not(feature="std")))] #[macro_use] extern crate alloc;
Expand Down Expand Up @@ -898,10 +897,14 @@ pub mod __wbg_shims {
pub use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
pub type This;
pub static this: This;
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)]
Expand Down
18 changes: 10 additions & 8 deletions src/rngs/os.rs
Expand Up @@ -1121,15 +1121,17 @@ mod imp {
impl OsRngImpl for OsRng {
fn new() -> Result<OsRng, Error> {
// First up we need to detect if we're running in node.js or a
// browser. Both environments have `this` defined as an object for
// our own scope, and we use that to look up other values.
// browser. To do this we get ahold of the `this` object (in a bit
// of a roundabout fashion).
//
// Here we test if `this.self` is defined. If so we're in a browser
// (either main window or web worker) and if not we're in node. If
// it turns out we're in node.js then we require the `crypto`
// package and use that. The API we're using was added in Node 6.x
// so it should be safe to assume tha it works.
if this.self_().is_undefined() {
// Once we have `this` we look at its `self` property, which is
// only defined on the web (either a main window or web worker).
let this = Function::new("return this").call(&JsValue::undefined());
assert!(this != JsValue::undefined());
let this = This::from(this);
let is_browser = this.self_() != JsValue::undefined();

if !is_browser {
return Ok(OsRng::Node(node_require("crypto")))
}

Expand Down