emscripten-core / emscripten-core/emscripten

Provide `readyPromiseReject` to `instantiateWasm`

Open
#23,038 7 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When providing an `instantiateWasm` method to the Module, only a `successCallback` is passed to the method (see [docs here](https://github.com/emscripten-core/emscripten/blob/06cebfca51f908d3bc6897113330e45a77010db2/site/source/docs/api_reference/module.rst?plain=1#L169))

The problem is that no `failureCallback` is provided.
In the case of a sync instantiation of the WASM, I can return `false` if the instantiation fails.
But in the case of an async instantiation, if the instantiation fails, the error isn't properly handled and the page freezes.

I'd like `instantiateWasm` to take 3 args instead of 2: `imports`, `successCallback` and `failureCallback`.

This means changing this line:

https://github.com/emscripten-core/emscripten/blob/06cebfca51f908d3bc6897113330e45a77010db2/src/preamble.js#L1067

to

```js
return Module['instantiateWasm'](info, receiveInstance, readyPromiseReject);
```

### Current workaround

For others having the same problem, I'm currently using a dirty hack through `pre-js.js` by doing:

```js
// pre-js.js

if (Module['readyPromiseRejectWrapper']) {
Module['readyPromiseRejectWrapper']['value'] = readyPromiseReject;
}
```

And when I instantiate my module:

```js
const readyPromiseRejectWrapper = { value: (e) => {} };
// Pass the wrapper to Module
const module = Module(
readyPromiseRejectWrapper,
// You can use the wrapper in 'instantiateWasm' when instantiation fails
instantiateWasm: (imports, successCallback) => {
(async () => {
try {
const instance = await foo(); // instantiate the wasm
successCallback(instance, module);
} catch (e) {
readyPromiseRejectWrapper.value(e);
}
})();
return {};
}
);
```
**Version of emscripten/emsdk:**
emcc 3.1.61

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.