Skip to content

Commit

Permalink
Merge pull request #600 from alexcrichton/fix-wasm-bindgen
Browse files Browse the repository at this point in the history
Update wasm-bindgen rand bindings
  • Loading branch information
dhardy committed Sep 7, 2018
2 parents 4163daa + 44cdb90 commit cb7f288
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
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

0 comments on commit cb7f288

Please sign in to comment.