Skip to content

Commit 6b81620

Browse files
authored
[tsgen] Support JSPI when emitting TypeScript definitions. (emscripten-core#23320)
- Adds a Promise<T> wrapper around the return type for async embind functions. - Disables JSPI while generating the definitions, since it's not needed. Fixes emscripten-core#23272
1 parent 9f83cfa commit 6b81620

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

src/embind/embind_gen.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ var LibraryEmbind = {
8383
if (this.isNonnullReturn && this.returnType instanceof PointerDefinition) {
8484
returnType = this.returnType.classType;
8585
}
86-
out.push(`): ${nameMap(returnType, true)}`);
86+
returnType = nameMap(returnType, true);
87+
if (this.isAsync) {
88+
returnType = `Promise<${returnType}>`;
89+
}
90+
out.push(`): ${returnType}`);
8791
}
8892

8993
printFunction(nameMap, out) {

test/other/embind_tsgen_jspi.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <emscripten/bind.h>
2+
3+
using namespace emscripten;
4+
5+
void sleep() {}
6+
7+
EMSCRIPTEN_BINDINGS(Test) {
8+
function("sleep", &sleep, async());
9+
}

test/other/embind_tsgen_jspi.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 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+
}
15+
interface WasmModule {
16+
}
17+
18+
interface EmbindModule {
19+
sleep(): Promise<void>;
20+
}
21+
22+
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/test_other.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,13 @@ def test_embind_tsgen_memory64(self):
35273527
self.get_emcc_args())
35283528
self.assertFileContents(test_file('other/embind_tsgen_memory64.d.ts'), read_file('embind_tsgen_memory64.d.ts'))
35293529

3530+
@requires_jspi
3531+
def test_embind_tsgen_jspi(self):
3532+
self.run_process([EMXX, test_file('other/embind_tsgen_jspi.cpp'),
3533+
'-lembind', '--emit-tsd', 'embind_tsgen_jspi.d.ts', '-sJSPI'] +
3534+
self.get_emcc_args())
3535+
self.assertFileContents(test_file('other/embind_tsgen_jspi.d.ts'), read_file('embind_tsgen_jspi.d.ts'))
3536+
35303537
@parameterized({
35313538
'': [0],
35323539
'wasm_exnref': [1]

tools/link.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,11 @@ def run_embind_gen(wasm_target, js_syms, extra_settings, linker_inputs):
20012001
setup_environment_settings()
20022002
# Use a separate Wasm file so the JS does not need to be modified after emscripten.emscript.
20032003
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
20042009
# Embind may be included multiple times, de-duplicate the list first.
20052010
settings.JS_LIBRARIES = dedup_list(settings.JS_LIBRARIES)
20062011
# Replace embind with the TypeScript generation version.

0 commit comments

Comments
 (0)