Skip to content

Commit

Permalink
Add JSPI support to MINIMAL_RUNTIME build mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
juj committed Sep 4, 2024
1 parent 7d78ad8 commit b5a1bdf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
59 changes: 39 additions & 20 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,50 @@
{{{ exportRuntime() }}}

#if HAS_MAIN // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
function run() {
#if MEMORYPROFILER
emscriptenMemoryProfiler.onPreloadComplete();
#endif

<<< ATMAINS >>>

#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which
// pthread_create()s a new thread that will call the user's real main() for
// the application.
var ret = __emscripten_proxy_main();
#else
var ret = _main();

#if EXIT_RUNTIME
function exitRuntime(ret) {
callRuntimeCallbacks(__ATEXIT__);
<<< ATEXITS >>>
#if PTHREADS
PThread.terminateAllThreads();
#endif

#endif

#if EXIT_RUNTIME

#if ASSERTIONS
runtimeExited = true;
#endif

_proc_exit(ret);

#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
}
#endif

function run() {
#if MEMORYPROFILER
emscriptenMemoryProfiler.onPreloadComplete();
#endif
#endif // PROXY_TO_PTHREAD

<<< ATMAINS >>>

#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which
// pthread_create()s a new thread that will call the user's real main() for
// the application.
__emscripten_proxy_main();
#elif ASYNCIFY == 2 && EXIT_RUNTIME
// In JSPI-enabled build mode, the main() function will return a Promise,
// which resolves to the process exit code.
_main().then(exitRuntime);
#elif EXIT_RUNTIME
// In regular exitRuntime mode, exit with the given return code from main().
exitRuntime(_main());
#else
// Run a persistent (never-exiting) application starting at main().
_main();
#endif

#if STACK_OVERFLOW_CHECK
checkStackCookie();
Expand Down Expand Up @@ -101,6 +112,10 @@ function loadModule() {
assignWasmImports();
#endif

#if ASYNCIFY
Asyncify.instrumentWasmImports(wasmImports);
#endif

var imports = {
#if MINIFY_WASM_IMPORTED_MODULES
'a': wasmImports,
Expand Down Expand Up @@ -138,7 +153,7 @@ if (!Module['wasm']) throw 'Must load WebAssembly Module in to variable Module.w
WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
#endif

#if !LibraryManager.has('library_exports.js')
#if !LibraryManager.has('library_exports.js') && ASYNCIFY != 1
// If not using the emscripten_get_exported_function() API, keep the
// `wasmExports` variable in local scope to this instantiate function to save
// code size. (otherwise access it without to export it to outer scope)
Expand Down Expand Up @@ -169,6 +184,10 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
wasmExports = output.instance.exports;
#endif

#if ASYNCIFY
wasmExports = Asyncify.instrumentWasmExports(wasmExports);
#endif

#if MEMORY64 || CAN_ADDRESS_2GB
wasmExports = applySignatureConversions(wasmExports);
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/preamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ function assert(condition, text) {
}
#endif
#if ASYNCIFY == 1 // ASYNCIFY-mode requires checking ABORT variable to avoid operating if code has aborted during an unwind
var ABORT = 0;
#endif
/** @param {string|number=} what */
function abort(what) {
#if ASYNCIFY == 1
ABORT = 1;
#endif
throw {{{ ASSERTIONS ? 'new Error(what)' : 'what' }}};
}
Expand Down
2 changes: 2 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3153,8 +3153,10 @@ def test_cocos2d_hello(self):

@parameterized({
'asyncify': (['-sASYNCIFY=1'],),
'asyncify_minimal_runtime': (['-sMINIMAL_RUNTIME', '-sASYNCIFY=1'],),
'jspi': (['-sASYNCIFY=2', '-Wno-experimental'],),
'jspi_wasm_bigint': (['-sASYNCIFY=2', '-sWASM_BIGINT', '-Wno-experimental'],),
'jspi_wasm_bigint_minimal_runtime': (['-sMINIMAL_RUNTIME', '-sASYNCIFY=2', '-sWASM_BIGINT', '-Wno-experimental'],),
})
def test_async(self, args):
if is_jspi(args) and not is_chrome():
Expand Down

0 comments on commit b5a1bdf

Please sign in to comment.