diff --git a/site/source/docs/api_reference/module.rst b/site/source/docs/api_reference/module.rst index c6f2bf0868f50..c621eb356cfb1 100644 --- a/site/source/docs/api_reference/module.rst +++ b/site/source/docs/api_reference/module.rst @@ -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. - - 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. diff --git a/src/preamble.js b/src/preamble.js index 4d516dab9d9a1..8f5822323a4aa 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -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. @@ -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); - }); + }, reject); #if ASSERTIONS } catch(e) { err(`Module.instantiateWasm callback failed with error: ${e}`); diff --git a/test/test_other.py b/test/test_other.py index 4aaa0b964e5fd..77a38213a0a3e 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -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