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

rand panic when use wasm-bindgen #616

Closed
uonr opened this issue Sep 25, 2018 · 8 comments
Closed

rand panic when use wasm-bindgen #616

uonr opened this issue Sep 25, 2018 · 8 comments

Comments

@uonr
Copy link

uonr commented Sep 25, 2018

wasm-0000006e-415:2 Uncaught (in promise) RuntimeError: unreachable
    at __rust_start_panic (wasm-function[415]:1)
    at rust_panic (wasm-function[414]:30)
    at std::panicking::rust_panic_with_hook::hd20860d15108b702 (wasm-function[411]:444)
    at std::panicking::continue_panic_fmt::h5ff9aa1d01d30565 (wasm-function[410]:122)
    at std::panicking::begin_panic_fmt::h401167bbcd0103e3 (wasm-function[408]:95)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::_$u7b$$u7b$closure$u7d$$u7d$::h8926014834b86cc9 (wasm-function[215]:204)
    at _$LT$core..result..Result$LT$T$C$$u20$E$GT$$GT$::unwrap_or_else::hd14d23538d2305a0 (wasm-function[87]:530)
    at rand::rngs::thread::THREAD_RNG_KEY::__init::h200f34f719285fc0 (wasm-function[214]:112)
    at _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hd848dfe862f0da01 (wasm-function[80]:52)
    at _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::ha36a539a75cd6604 (wasm-function[82]:283)

simple code:

extern crate wasm_bindgen;
extern crate rand;
use wasm_bindgen::prelude::*;
// use rand::rngs::OsRng;

#[wasm_bindgen]
extern {
    fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    // let _rng = OsRng::new();
    let value: i32 = rand::random();
    alert(&format!("Hello, {}!", value));
}

if use OsRng :

extern crate wasm_bindgen;
extern crate rand;
use wasm_bindgen::prelude::*;
use rand::rngs::OsRng;

#[wasm_bindgen]
extern {
    fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    let _rng = OsRng::new();
    // let value: i32 = rand::random();
    alert(&format!("Hello, {}!", name));
}

can't found

error[E0432]: unresolved import `rand::rngs::OsRng`
 --> src/lib.rs:4:5
  |
4 | use rand::rngs::OsRng;
  |     ^^^^^^^^^^^^^^^^^ no `OsRng` in `rngs`
@uonr
Copy link
Author

uonr commented Sep 25, 2018

I've created a repo for reproduce this bug:
https://github.com/quanbrew/rust-wasm-rand-bug-reproduce

@dhardy
Copy link
Member

dhardy commented Sep 25, 2018

From rngs/mod.rs:

#[cfg(all(feature="std",
          any(target_os = "linux", target_os = "android",
              target_os = "netbsd",
              target_os = "dragonfly",
              target_os = "haiku",
              target_os = "emscripten",
              target_os = "solaris",
              target_os = "cloudabi",
              target_os = "macos", target_os = "ios",
              target_os = "freebsd",
              target_os = "openbsd", target_os = "bitrig",
              target_os = "redox",
              target_os = "fuchsia",
              windows,
              all(target_arch = "wasm32", feature = "stdweb"),
              all(target_arch = "wasm32", feature = "wasm-bindgen"),
)))]
pub use self::os::OsRng;

You're using wasm32 with the wasm-bindgen feature?

@uonr
Copy link
Author

uonr commented Sep 25, 2018

the package `play` depends on `rand`, with features: `wasm-bindgen` but `rand` does not have these features.

Can't find "wasm-bindgen" feature in Cargo.toml https://github.com/rust-random/rand/blob/master/Cargo.toml

@upsuper
Copy link
Contributor

upsuper commented Sep 25, 2018

It seems that rand on crates.io doesn't include wasm-bindgen yet. You need to use the git version instead.

@dhardy
Copy link
Member

dhardy commented Sep 25, 2018

No, you'll have to wait for the 0.6 release for that #520

@coltfred
Copy link
Contributor

We have successfully used rand's git master from wasm using wasm-bindgen. I'm working on helping with 0.6 so we can get that out ASAP.

@gregtatum
Copy link

Still not working for me on 0.6.1 or from specifying git master.

panicked at 'could not initialize thread_rng: No entropy sources available (permanently unavailable)', /.../.cargo/git/checkouts/rand-cc8f5b7ec2d3b6d9/2b5eac5/src/rngs/thread.rs:82:17

@dhardy
Copy link
Member

dhardy commented Dec 21, 2018

@gregtatum since we're now on 0.6, I think it would be better to open a new issue; I think this one counts as "solved" now.

Could you include the error message from OsRng::new() in your report?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants