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

Error calling wasm function with complex object as argument #10182

Closed
7 tasks done
grahamdaley opened this issue Sep 21, 2022 · 5 comments
Closed
7 tasks done

Error calling wasm function with complex object as argument #10182

grahamdaley opened this issue Sep 21, 2022 · 5 comments

Comments

@grahamdaley
Copy link

Describe the bug

I came up against an issue when developing a Vite 3 based project that calls a wasm function that passes a complex object (an array of arrays of numeric values) from the JS world into the wasm.

Vite seems to have a problem binding to the JS code generated by wasm-build, which results in the following error message being shown in the browser console:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="./hello_wasm_bg.js" error: module is not an object or function

I have created an example project to illustrate this.

Please let me know if this is a bug, or if the error is my own.

Many thanks!

Reproduction

https://github.com/grahamdaley/vite-wasm-test

System Info

System:
    OS: macOS 12.6
    CPU: (8) arm64 Apple M1 Pro
    Memory: 175.95 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Safari: 16.0
  npmPackages:
    @vitejs/plugin-vue: ^3.1.0 => 3.1.0 
    vite: ^3.1.0 => 3.1.3

Used Package Manager

yarn

Logs

No response

Validations

@sapphi-red
Copy link
Member

wasm-pack build ./src/hello_wasm --target bundler generates a code that uses ESM Integration Proposal for Wasm. Because this proposal is not yet stable, it's not supported by Vite. (This feature request is tracked at #4551.)

Using vite-plugin-wasm or using --target web will work.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Sep 21, 2022
@grahamdaley
Copy link
Author

I tried using --target web but it resulted in a slightly different error message:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="wbg" error: module is not an object or function

@grahamdaley
Copy link
Author

I would have used vite-plugin-wasm, but it seems it's not suitable for my particular use case, which needs the wasm calls to be made from within a web worker.

@sapphi-red
Copy link
Member

For both --target bundler and --target web, you'll need to import the generated JS file instead of wasm file.
This is because wasm-pack generates a glue code and the generated wasm file requires that.
https://github.com/grahamdaley/vite-wasm-test/blob/aab063da86737bf8a4db85cf39002a3ac25e255b/src/components/HelloWorld.vue#L3

@grahamdaley
Copy link
Author

That works perfectly! Thank you so much for your help, @sapphi-red!

@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants