Skip to content

Commit e5db73d

Browse files
committed
[JSPI] Do not automatically wrap async library functions.
In a number of places (e.g. `emscripten_async_wget_data`) library functions use an `async` JS functions, but they do not actually want to use JSPI wrappers. Change it so functions must explicitly use `foo__async: true` to enable JSPI wrappers. Fixes #20289
1 parent 3d5f9af commit e5db73d

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ See docs/process.md for more on how version tagging works.
3232
for chrome, or
3333
emrun --browser=firefox --browser-args=-headless [..]
3434
for firefox. (#24537)
35+
- When JSPI is enabled `async` library functions are no longer automatically
36+
wrapped with `WebAssembly.Suspending` functions. To automatically wrap library
37+
functions for use with JSPI they must now explicitly set
38+
`myLibraryFunction__async: true`.
3539

3640
4.0.10 - 06/07/25
3741
-----------------

src/jsifier.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ function(${args}) {
534534
if (ASYNCIFY) {
535535
const original = LibraryManager.library[symbol];
536536
if (typeof original == 'function') {
537-
isAsyncFunction =
538-
LibraryManager.library[symbol + '__async'] ||
539-
original.constructor.name == 'AsyncFunction';
537+
isAsyncFunction = LibraryManager.library[symbol + '__async'];
540538
}
541539
if (isAsyncFunction) {
542540
asyncFuncs.push(symbol);

src/lib/libasync.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ addToLibrary({
559559
#endif
560560

561561
_load_secondary_module__sig: 'v',
562+
_load_secondary_module__async: true,
562563
_load_secondary_module: async function() {
563564
// Mark the module as loading for the wasm module (so it doesn't try to load it again).
564565
wasmExports['load_secondary_module_status'].value = 1;

test/test_other.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,34 @@ def test_jspi_add_function(self):
34843484
'-Wno-experimental']
34853485
self.do_runf('other/test_jspi_add_function.c', 'done')
34863486

3487+
@requires_jspi
3488+
def test_jspi_async_function(self):
3489+
# Make sure async library functions are not automatically JSPI'd.
3490+
create_file('lib.js', r'''
3491+
addToLibrary({
3492+
foo: async function(f) { await Promise.resolve(); },
3493+
});
3494+
''')
3495+
create_file('main.c', r'''
3496+
#include <emscripten.h>
3497+
extern void foo();
3498+
EMSCRIPTEN_KEEPALIVE void test() {
3499+
foo();
3500+
}
3501+
''')
3502+
create_file('post.js', r'''
3503+
Module.onRuntimeInitialized = () => {
3504+
_test()
3505+
console.log('done');
3506+
};
3507+
''')
3508+
self.emcc_args += [
3509+
'-sJSPI',
3510+
'--js-library=lib.js',
3511+
'-Wno-experimental',
3512+
'--post-js=post.js']
3513+
self.do_runf('main.c', 'done')
3514+
34873515
@requires_dev_dependency('typescript')
34883516
@parameterized({
34893517
'commonjs': [['-sMODULARIZE'], ['--module', 'commonjs', '--moduleResolution', 'node']],

0 commit comments

Comments
 (0)