Skip to content

Commit d1fbb4b

Browse files
authored
[jspi] Fix TSD generation when using asyncify library functions. (#23419)
Instead of disabling JSPI during TSD generation just skip instrumenting the wasm exports and imports so JSPI isn't needed. Fixes #23418
1 parent 7e37224 commit d1fbb4b

File tree

6 files changed

+19
-21
lines changed

6 files changed

+19
-21
lines changed

src/library_async.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ addToLibrary({
3838
rewindArguments: {},
3939
#endif
4040
instrumentWasmImports(imports) {
41+
#if EMBIND_GEN_MODE
42+
// Instrumenting is not needed when generating code.
43+
return imports;
44+
#endif
4145
#if ASYNCIFY_DEBUG
4246
dbg('asyncify instrumenting imports');
4347
#endif
@@ -103,6 +107,10 @@ addToLibrary({
103107
},
104108
#endif
105109
instrumentWasmExports(exports) {
110+
#if EMBIND_GEN_MODE
111+
// Instrumenting is not needed when generating code.
112+
return exports;
113+
#endif
106114
#if ASYNCIFY_DEBUG
107115
dbg('asyncify instrumenting exports');
108116
#endif

src/settings_internal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ var EMBIND = false;
9292
// Whether a TypeScript definition file has been requested.
9393
var EMIT_TSD = false;
9494

95+
// This will be true during the generation of code in run_embind_gen. Helpful
96+
// for detecting if either TSD file or embind AOT JS generation is running.
97+
var EMBIND_GEN_MODE = false;
98+
9599
// Whether the main() function reads the argc/argv parameters.
96100
var MAIN_READS_PARAMS = true;
97101

test/other/embind_tsgen_jspi.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <emscripten/bind.h>
2+
#include <emscripten.h>
23

34
using namespace emscripten;
45

5-
void sleep() {}
6+
void sleep() {
7+
emscripten_sleep(0);
8+
}
69

710
EMSCRIPTEN_BINDINGS(Test) {
811
function("sleep", &sleep, async());

test/other/embind_tsgen_jspi.d.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
2-
declare namespace RuntimeExports {
3-
let HEAPF32: any;
4-
let HEAPF64: any;
5-
let HEAP_DATA_VIEW: any;
6-
let HEAP8: any;
7-
let HEAPU8: any;
8-
let HEAP16: any;
9-
let HEAPU16: any;
10-
let HEAP32: any;
11-
let HEAPU32: any;
12-
let HEAP64: any;
13-
let HEAPU64: any;
14-
}
152
interface WasmModule {
163
}
174

185
interface EmbindModule {
196
sleep(): Promise<void>;
207
}
218

22-
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
9+
export type MainModule = WasmModule & EmbindModule;

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ def test_embind_tsgen_memory64(self):
35303530
@requires_jspi
35313531
def test_embind_tsgen_jspi(self):
35323532
self.run_process([EMXX, test_file('other/embind_tsgen_jspi.cpp'),
3533-
'-lembind', '--emit-tsd', 'embind_tsgen_jspi.d.ts', '-sJSPI'] +
3533+
'-lembind', '--emit-tsd', 'embind_tsgen_jspi.d.ts', '-sJSPI', '-sSTRICT', '--no-entry'] +
35343534
self.get_emcc_args())
35353535
self.assertFileContents(test_file('other/embind_tsgen_jspi.d.ts'), read_file('embind_tsgen_jspi.d.ts'))
35363536

tools/link.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,7 @@ def run_embind_gen(wasm_target, js_syms, extra_settings, linker_inputs):
19641964
# Save settings so they can be restored after TS generation.
19651965
original_settings = settings.backup()
19661966
settings.attrs.update(extra_settings)
1967+
settings.EMBIND_GEN_MODE = True
19671968

19681969
if settings.MAIN_MODULE and linker_inputs:
19691970
# Copy libraries to the temp directory so they can be used when running
@@ -2001,11 +2002,6 @@ def run_embind_gen(wasm_target, js_syms, extra_settings, linker_inputs):
20012002
setup_environment_settings()
20022003
# Use a separate Wasm file so the JS does not need to be modified after emscripten.emscript.
20032004
settings.SINGLE_FILE = False
2004-
if settings.ASYNCIFY == 2:
2005-
# JSPI is not needed to generate the definitions.
2006-
# TODO: when the emsdk node version supports JSPI, it probably makes more sense
2007-
# to enable it in node than disabling the setting here.
2008-
settings.ASYNCIFY = 0
20092005
# Embind may be included multiple times, de-duplicate the list first.
20102006
settings.JS_LIBRARIES = dedup_list(settings.JS_LIBRARIES)
20112007
# Replace embind with the TypeScript generation version.

0 commit comments

Comments
 (0)