Skip to content

Commit

Permalink
[tsgen] Support JSPI when emitting TypeScript definitions.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
brendandahl committed Jan 7, 2025
1 parent 2396d3e commit a09ec2e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/embind/embind_gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/other/embind_tsgen_jspi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <emscripten/bind.h>

using namespace emscripten;

void sleep() {}

EMSCRIPTEN_BINDINGS(Test) {
function("sleep", &sleep, async());
}
22 changes: 22 additions & 0 deletions test/other/embind_tsgen_jspi.d.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
}

export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;
7 changes: 7 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a09ec2e

Please sign in to comment.