diff --git a/src/embind/embind_gen.js b/src/embind/embind_gen.js index 130e128e600e0..effee2691eb05 100644 --- a/src/embind/embind_gen.js +++ b/src/embind/embind_gen.js @@ -83,7 +83,11 @@ var LibraryEmbind = { if (this.isNonnullReturn && this.returnType instanceof PointerDefinition) { returnType = this.returnType.classType; } - out.push(`): ${nameMap(returnType, true)}`); + returnType = nameMap(returnType, true); + if (this.isAsync) { + returnType = `Promise<${returnType}>`; + } + out.push(`): ${returnType}`); } printFunction(nameMap, out) { diff --git a/test/other/embind_tsgen_jspi.cpp b/test/other/embind_tsgen_jspi.cpp new file mode 100644 index 0000000000000..10bbe5cc59fc9 --- /dev/null +++ b/test/other/embind_tsgen_jspi.cpp @@ -0,0 +1,9 @@ +#include + +using namespace emscripten; + +void sleep() {} + +EMSCRIPTEN_BINDINGS(Test) { + function("sleep", &sleep, async()); +} diff --git a/test/other/embind_tsgen_jspi.d.ts b/test/other/embind_tsgen_jspi.d.ts new file mode 100644 index 0000000000000..9be260c7fff78 --- /dev/null +++ b/test/other/embind_tsgen_jspi.d.ts @@ -0,0 +1,22 @@ +// TypeScript bindings for emscripten-generated code. Automatically generated at compile time. +declare namespace RuntimeExports { + let HEAPF32: any; + let HEAPF64: any; + let HEAP_DATA_VIEW: any; + let HEAP8: any; + let HEAPU8: any; + let HEAP16: any; + let HEAPU16: any; + let HEAP32: any; + let HEAPU32: any; + let HEAP64: any; + let HEAPU64: any; +} +interface WasmModule { +} + +interface EmbindModule { + sleep(): Promise; +} + +export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule; diff --git a/test/test_other.py b/test/test_other.py index 5212e19acc812..a0f3b0456fac5 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3527,6 +3527,13 @@ def test_embind_tsgen_memory64(self): self.get_emcc_args()) self.assertFileContents(test_file('other/embind_tsgen_memory64.d.ts'), read_file('embind_tsgen_memory64.d.ts')) + @requires_jspi + def test_embind_tsgen_jspi(self): + self.run_process([EMXX, test_file('other/embind_tsgen_jspi.cpp'), + '-lembind', '--emit-tsd', 'embind_tsgen_jspi.d.ts', '-sJSPI'] + + self.get_emcc_args()) + self.assertFileContents(test_file('other/embind_tsgen_jspi.d.ts'), read_file('embind_tsgen_jspi.d.ts')) + @parameterized({ '': [0], 'wasm_exnref': [1] diff --git a/tools/link.py b/tools/link.py index 016ebeb51cefa..9da89caca72f0 100644 --- a/tools/link.py +++ b/tools/link.py @@ -2001,6 +2001,11 @@ def run_embind_gen(wasm_target, js_syms, extra_settings, linker_inputs): setup_environment_settings() # Use a separate Wasm file so the JS does not need to be modified after emscripten.emscript. settings.SINGLE_FILE = False + if settings.JSPI: + # JSPI is not needed to generated the definitions. + # TODO: when the emsdk node version supports JSPI, it probably makes more sense + # to enable it in node than disabling the setting here. + settings.ASYNCIFY = 0 # Embind may be included multiple times, de-duplicate the list first. settings.JS_LIBRARIES = dedup_list(settings.JS_LIBRARIES) # Replace embind with the TypeScript generation version.