Skip to content

Provide a way to reject Promise in instantiateWasm #23780

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions site/source/docs/api_reference/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ Other methods

.. js:function:: Module.instantiateWasm

When targeting WebAssembly, Module.instantiateWasm is an optional user-implemented callback function that the Emscripten runtime calls to perform the WebAssembly instantiation action. The callback function will be called with two parameters, ``imports`` and ``successCallback``. ``imports`` is a JS object which contains all the function imports that need to be passed to the WebAssembly Module when instantiating, and once instantiated, this callback function should call ``successCallback()`` with the generated WebAssembly Instance object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put Module.instantiateWasm in double backticks here?

Also, can you wrap these lines at 80 columns?


The instantiation can be performed either synchronously or asynchronously. The return value of this function should contain the ``exports`` object of the instantiated WebAssembly Module, or an empty dictionary object ``{}`` if the instantiation is performed asynchronously, or ``false`` if instantiation failed.
When targeting WebAssembly, Module.instantiateWasm is an optional user-implemented callback function that the Emscripten runtime calls to perform the WebAssembly instantiation action. The callback function will be called with three parameters, ``imports``, ``successCallback`` and ``errorCallback``. ``imports`` is a JS object which contains all the function imports that need to be passed to the WebAssembly Module when instantiating. Once instantiated, this callback function should call ``successCallback(instance, module)`` with the generated WebAssembly Instance and Module objects. The instantiation can be performed either synchronously or asynchronously. If an error occurs during instantiation, the callback function should call ``errorCallback(error)``.

Overriding the WebAssembly instantiation procedure via this function is useful when you have other custom asynchronous startup actions or downloads that can be performed in parallel to WebAssembly compilation. Implementing this callback allows performing all of these in parallel. See the file ``test/manual_wasm_instantiate.html`` and the test ``browser.test_manual_wasm_instantiate`` for an example of how this construct works in action.

Expand Down
8 changes: 4 additions & 4 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ function getWasmImports() {
var info = getWasmImports();

#if expectToReceiveOnModule('instantiateWasm')
// User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
// User shell pages can write their own Module.instantiateWasm = function(imports, successCallback, errorCallback) callback
// to manually instantiate the Wasm module themselves. This allows pages to
// run the instantiation parallel to any other async startup actions they are
// performing.
Expand All @@ -989,10 +989,10 @@ function getWasmImports() {
#if ASSERTIONS
try {
#endif
Module['instantiateWasm'](info, (mod, inst) => {
receiveInstance(mod, inst);
Module['instantiateWasm'](info, (inst, mod) => {
receiveInstance(inst, mod);
resolve(mod.exports);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this needs to be inst.exports

});
}, reject);
#if ASSERTIONS
} catch(e) {
err(`Module.instantiateWasm callback failed with error: ${e}`);
Expand Down
9 changes: 9 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15681,6 +15681,15 @@ def test_instantiate_wasm(self):
}''')
self.do_runf('test_manual_wasm_instantiate.c', emcc_args=['--pre-js=pre.js'])

@also_with_modularize
def test_instantiate_wasm_failure(self):
create_file('pre.js', '''
Module['instantiateWasm'] = (imports, successCallback, failureCallback) => {
failureCallback('wasm instantiation failed');
return {}; // Compiling asynchronously, no exports.
}''')
self.do_runf('test_manual_wasm_instantiate.c', 'wasm instantiation failed', emcc_args=['--pre-js=pre.js'], assert_returncode=NON_ZERO)

def test_late_module_api_assignment(self):
# When sync instantiation is used (or when async/await is used in MODULARIZE mode) certain
# Module properties cannot be assigned in `--post-js` code because its too late by the time
Expand Down