question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WASM loader does not expose importObject

See original GitHub issue

WebAssembly modules are able to import foreign Javascript functions and memory objects that are passed in the second parameter of WebAssembly.instantiate(...). Unfortunately the current WASM loader is hard-coded to call instantiate with no import object.

https://github.com/parcel-bundler/parcel/blob/master/src/builtins/loaders/wasm-loader.js https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate

The simplest solution would be to have the WASM loader instead run WebAssembly.compile(...) and return the compiled WebAssembly.Module, so the user can call WebAssembly.instantiate(...) with whatever parameters they need. I think that’s how Webpacks wasm loader works.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:21
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
linclarkcommented, Jan 25, 2018

Yep. I kinda like the way webpack is planning to approach this (I think): imports in WASM files get mapped to module imports the same way imports in JS do.

You’re correct, this is the right approach. WASM modules will eventually be ES modules, so imports/exports should work the same as they do in JS. I will likely be championing this over the next few months. Work will be tracked in https://github.com/WebAssembly/design/issues/1087

The problem is that the current Rust wasm backend always generates only a single import, and it’s always called “env”

The webpack folks also brought this up. If no one responds with a plan to address this, I’ll bring it up in our biweekly WASM toolchain meeting (which is next Tuesday) and see if I can figure out what the plan is.

6reactions
zakcutnercommented, Mar 31, 2019

It would be really nice to have support for importObject – a killer feature of WASM is the ability to call JS functions from WASM modules (as well as the other way round of course)!

I feel that you shouldn’t have to compile WASM modules already knowing the paths of JS files they use. It would be nice to simply have them as standalone binary modules to which you can pass in imports they need to run (in keeping with the JS WebAssembly.instantiateStreaming() API).

Besides, this would also open up support for wasm_bindgen as well as other existing WASM compilers. @hitecherik and I thought perhaps a syntax like this would be preferable (and compatible with the current import syntax)…

const module = await import("module.wasm")(importObject);
module.foo();
Read more comments on GitHub >

github_iconTop Results From Across the Web

failed to load wasm application - Stack Overflow
ImportObjects is just an object you create with functions etc you want to expose to the wasm module. If you don't expose any...
Read more >
Using the WebAssembly JavaScript API - MDN Web Docs
This article has taken you through the basics of using the WebAssembly JavaScript API to include a WebAssembly module in a JavaScript ...
Read more >
Load wasm module using manifest V3 - Google Groups
The problem is that after enabling wasm loading in webpack, I get the following error ... Above code can still work in some...
Read more >
Understanding the JS API - WebAssembly
For basic loading, there are three steps: ... Instance objects are like function closures, pairing code with environment and are not structured cloneable....
Read more >
The `wasm-bindgen` Guide - Rust and WebAssembly
It does not generate an ES module. With that in mind the main difference is how the wasm/JS code is loaded, and here's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found