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

Exporting struct causes TypeError: WebAssembly.instantiate() when module imported #1877

Closed
AlexEales opened this issue Nov 24, 2019 · 5 comments
Labels

Comments

@AlexEales
Copy link

Describe the Bug

It appears whenever I try to import from a WASm bundle which contains an exported struct a TypeError: WebAssembly.instantiate() is thrown. I simplified down the struct I was trying to export to just be:

#[wasm_bindgen]
pub struct SimpleStruct {
    value: i32,
}

#[wasm_bindgen]
impl {
    pub fn new(value: i32) -> SimpleStruct {
        SimpleStruct { value }
    } 
}

And I'm still getting the error? Could this be something to do with the WASM module not being generated correctly? It seemed pretty normal when I checked and compared it to the expected output of exporting a struct on this page.

I'm using Parcel bundler to create a simple website using WASM, I'm aware of the lack of compatibility between the two out of the box so I am simply recompiling my Rust package when I need to and then pointing the TS files at the generates JS file in the pkg directory.

Steps to Reproduce

  1. Copy above code + some arbitrary function exported to WASM
  2. Compile with wasm-pack build.
  3. Import arbitrary function inJS.
  4. Serve local JS and observe error.

If applicable, add a link to a test case (as a zip file or link to a repository we can clone).

Expected Behavior

Importing from the bundle should not error if it contains a struct being exported.

Actual Behavior

TypeError: WebAssembly.instantiate() is thrown regardless of structs' contents.

Additional Context

Full error message: Uncaught (in promise) TypeError: WebAssembly.instantiate(): Imports argument must be present and must be an object

@AlexEales AlexEales added the bug label Nov 24, 2019
@AlexEales
Copy link
Author

My Cargo.toml:

[package]
name = "simple"
version = "0.1.0"
authors = ["Alex Eales"]
edition = "2018"

[dependencies]
rand = "0.7"

[dependencies.wasm-bindgen]
version = "0.2.55"

[lib]
crate-type = ["cdylib"]

@Pauan
Copy link
Contributor

Pauan commented Nov 25, 2019

I'm not 100% sure, but this sounds like a bug with Parcel (which doesn't have great wasm support at the moment).

Do you still get the error if you use Webpack instead?

@AlexEales
Copy link
Author

Thanks for the quick response @Pauan I do not get the error with webpack, I had a feeling it would be a Parcel issue. Do you have any idea what might be causing this so I could relay this to the Parcel team and try and get a fix for this going? :)

@AlexEales
Copy link
Author

Have found a Parcel plugin which seems to solve the issue, will leave the link here incase anyone else goes down this rabbit hole!

https://github.com/rustwasm/rust-parcel-template

@Pauan
Copy link
Contributor

Pauan commented Nov 25, 2019

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

No branches or pull requests

2 participants