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
wasm_bindgen_test can't build crate which uses OsRng #1189
Comments
Thanks for the bug report and test case! Interesting, looks like we aren't generating the JS glue for an import function:
|
This is a bug in the This diff fixes it: diff --git a/rand_os/src/wasm32_bindgen.rs b/rand_os/src/wasm32_bindgen.rs
index 8e7c979a22..15f24dcee5 100644
--- a/rand_os/src/wasm32_bindgen.rs
+++ b/rand_os/src/wasm32_bindgen.rs
@@ -5,53 +5,19 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Implementation for WASM via wasm-bindgen
use rand_core::{Error, ErrorKind};
use super::OsRngImpl;
-
+use super::__wbg_shims::*;
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]);
-}
#[derive(Clone, Debug)]
pub enum OsRng {
Node(NodeCrypto),
Browser(BrowserCrypto),
}
impl OsRngImpl for OsRng { I'll submit this upstream. |
Use the `__wbg_shims` imports instead of creating new, local imports in the `wasm32_bindgen` module. Fixes rustwasm/wasm-bindgen#1189
No worries, let me know if y'all run into any more issues getting the tests up and running |
I made a simple crate to experiment with wasm_bindgen_test. It looks like if the crate uses
OsRng
, runningwasm-pack test --node
fails with the error below. I tried using wasm-bindgen to make a module, and requiring the module in a js program, and theOsRng
rng works as expected.Here's a simple example which demonstrates the problem: https://github.com/stevebob/wasm-bindgen-test-rand-os-error
The text was updated successfully, but these errors were encountered: