WebAssembly.instantiateStreaming requires an optional argument

Viewed 626

I get a Uncaught (in promise) TypeError: second argument must be an object error when I run the following code:

async function loadWasm(url) {
  const fetchPromise = fetch(url)
  const { module, instance } = await WebAssembly.instantiateStreaming(fetchPromise)
  // [...]
}
loadWasm('http://localhost:3000/path/to/file.wasm')

Looking at the documentation, it says that the second argument of WebAssembly.instantiateStreaming (importObject) is optional, so I don't understand why the browser makes it mandatory nonetheless?

I've tested this in Firefox 78.7.0esr (64-bit) and Chrome 88.0.4324.96 (Official Build) (64-bit). In case it's of any importance, the code is transpiled with webpack 5 in a ruby on rails 6.1 application.

Update:

I opened an issue on mdn's github repository thinking the documentation might need an update. So far it seems the documentation is good so it might be an implementation issue. To be confirmed.

Update:

It's neither an implementation issue nor a documentation mistake. I posted my conclusion about this issue in the answer below.

1 Answers

https://github.com/WebAssembly/spec/issues/1284#issuecomment-772286062

It is required only if the module you're instantiating actually imports something.

It's marked as optional in the documentation because it's not always needed, not because we have the choice to provide it or not. It's the wasm package that dictates whether it is optional or required.

Related