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

wasm_bindgen_test can't build crate which uses OsRng #1189

Closed
gridbugs opened this issue Jan 16, 2019 · 4 comments
Closed

wasm_bindgen_test can't build crate which uses OsRng #1189

gridbugs opened this issue Jan 16, 2019 · 4 comments
Labels
bug codegen Issues related to emitting code at the end of the wasm-bindgen pipeline wasm-bindgen-test The wasm-bindgen-test crate

Comments

@gridbugs
Copy link
Contributor

I made a simple crate to experiment with wasm_bindgen_test. It looks like if the crate uses OsRng, running wasm-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 the OsRng rng works as expected.

Here's a simple example which demonstrates the problem: https://github.com/stevebob/wasm-bindgen-test-rand-os-error

  [1/5] Checking `rustc` version...
  [2/5] Adding WASM target...
  info: component 'rust-std' for target 'wasm32-unknown-unknown' is up to date
  [3/5] Compiling tests to WASM...
     Compiling foo v0.1.0 (/home/steve/src/wasm-bindgen-test-rand-os-error)
      Finished dev [unoptimized + debuginfo] target(s) in 0.24s
  wasm-bindgen 0.2.31
  [5/5] Running tests in node...
      Finished dev [unoptimized + debuginfo] target(s) in 0.03s
       Running target/wasm32-unknown-unknown/debug/deps/foo-5b4a5ece76605715.wasm
  LinkError: WebAssembly Instantiation: Import #25 module="./wasm-bindgen-test" function="__wbg_new_bc890df3e5e6a91d" error: function im
      at Object.<anonymous> (/home/steve/src/wasm-bindgen-test-rand-os-error/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test_bg.
      at Module._compile (internal/modules/cjs/loader.js:721:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
      at Module.load (internal/modules/cjs/loader.js:620:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
      at Function.Module._load (internal/modules/cjs/loader.js:552:3)
      at Module.require (internal/modules/cjs/loader.js:657:17)
      at require (internal/modules/cjs/helpers.js:22:18)
      at Object.<anonymous> (/home/steve/src/wasm-bindgen-test-rand-os-error/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js:
      at Module._compile (internal/modules/cjs/loader.js:721:30)
  error: test failed, to rerun pass '--lib'
| Executing bindgen...
Error: Running Wasm tests with wasm-bindgen-test failed
Caused by: failed to execute `cargo test`: exited with exit code: 1
@fitzgen
Copy link
Member

fitzgen commented Jan 17, 2019

Thanks for the bug report and test case!

Interesting, looks like we aren't generating the JS glue for an import function:

$ CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=~/.cargo/bin/wasm-bindgen-test-runner cargo test --target wasm32-unknown-unknown
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running target/wasm32-unknown-unknown/debug/deps/foo-adfbb63bba495ab8.wasm
LinkError: WebAssembly Instantiation: Import #24 module="./wasm-bindgen-test" function="__wbg_new_bc890df3e5e6a91d" error: function import requires a callable
    at Object.<anonymous> (/home/fitzgen/scratch/wasm-bindgen-test-rand-os-error/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test_bg.js:8:22)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/fitzgen/scratch/wasm-bindgen-test-rand-os-error/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js:592:8)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
error: test failed, to rerun pass '--lib'

~/scratch/wasm-bindgen-test-rand-os-error
$ grep -rn __wbg_new_bc890df3e5e6a91d target/wasm32-unknown-unknown/
Binary file target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test_bg.wasm matches
Binary file target/wasm32-unknown-unknown/debug/foo-adfbb63bba495ab8.wasm matches
Binary file target/wasm32-unknown-unknown/debug/incremental/rand_os-1z1h9s9wn30q3/s-f8lwsl4j60-u75e00-179vlqig5d7ou/query-cache.bin matches
Binary file target/wasm32-unknown-unknown/debug/incremental/rand_os-1z1h9s9wn30q3/s-f8lwsl4j60-u75e00-179vlqig5d7ou/3x8gezbu8qqsd7k8.o matches
Binary file target/wasm32-unknown-unknown/debug/incremental/rand_os-1z1h9s9wn30q3/s-f8lwsl4j60-u75e00-179vlqig5d7ou/218z11sdbbbehka.o matches
Binary file target/wasm32-unknown-unknown/debug/incremental/foo-2n9pht1batj6o/s-f8lwsn92br-1ss3tvv-3b9jse8c0tsti/query-cache.bin matches
Binary file target/wasm32-unknown-unknown/debug/incremental/foo-1e9m7tqhb06d/s-f8lwqhlnzo-klnrt2-3ci2ukc0wll5z/query-cache.bin matches
Binary file target/wasm32-unknown-unknown/debug/deps/librand_os-d51fe60f993f7c8d.rlib matches
Binary file target/wasm32-unknown-unknown/debug/deps/foo-adfbb63bba495ab8.wasm matches
Binary file target/wasm32-unknown-unknown/debug/deps/librand_os-88108a596a2a47c5.rlib matches
Binary file target/wasm32-unknown-unknown/debug/deps/foo-254d859930642056.wasm matches
Binary file target/wasm32-unknown-unknown/debug/foo-254d859930642056.wasm matches

@fitzgen fitzgen added bug wasm-bindgen-test The wasm-bindgen-test crate codegen Issues related to emitting code at the end of the wasm-bindgen pipeline labels Jan 17, 2019
@fitzgen
Copy link
Member

fitzgen commented Jan 17, 2019

This is a bug in the rand_os crate, where it was incorrectly avoiding the can't-define-wasm-bindgen-imports constraint/bug. It was trying to avoid it with the __wbg_shims module, but then it wasn't using those imports and instead was making new (invalid because of private module) imports in the wasm32_bindgen module.

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.

@fitzgen fitzgen closed this as completed Jan 17, 2019
fitzgen added a commit to fitzgen/rand that referenced this issue Jan 17, 2019
Use the `__wbg_shims` imports instead of creating new, local imports in the
`wasm32_bindgen` module.

Fixes rustwasm/wasm-bindgen#1189
@dhardy
Copy link

dhardy commented Jan 17, 2019

Thanks for fixing this @fitzgen; I guess things got messed up when creating the rand_os crate; hopefully @stevebob's tests will stop that happening again.

@fitzgen
Copy link
Member

fitzgen commented Jan 17, 2019

No worries, let me know if y'all run into any more issues getting the tests up and running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug codegen Issues related to emitting code at the end of the wasm-bindgen pipeline wasm-bindgen-test The wasm-bindgen-test crate
Projects
None yet
Development

No branches or pull requests

3 participants