From 9f19ec9eef86bd6301ce02fde3aa4c3b5a213cc6 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 16 Apr 2024 10:28:07 -0700 Subject: [PATCH] [tsgen] Fix modularize definition for sync compilation. (#21766) Fixes #21760 --- test/other/test_emit_tsd_sync.d.ts | 21 +++++++++++++++++++++ test/other/test_tsd_sync.ts | 5 +++++ test/test_other.py | 11 +++++++++++ tools/emscripten.py | 5 ++++- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/other/test_emit_tsd_sync.d.ts create mode 100644 test/other/test_tsd_sync.ts diff --git a/test/other/test_emit_tsd_sync.d.ts b/test/other/test_emit_tsd_sync.d.ts new file mode 100644 index 0000000000000..37d370ec26a46 --- /dev/null +++ b/test/other/test_emit_tsd_sync.d.ts @@ -0,0 +1,21 @@ +// TypeScript bindings for emscripten-generated code. Automatically generated at compile time. +declare namespace RuntimeExports { + 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 { + _fooVoid(): void; + _fooInt(_0: number, _1: number): number; + _main(_0: number, _1: number): number; +} + +export type MainModule = WasmModule & typeof RuntimeExports; +export default function MainModuleFactory (options?: unknown): MainModule; diff --git a/test/other/test_tsd_sync.ts b/test/other/test_tsd_sync.ts new file mode 100644 index 0000000000000..49ef1381046d9 --- /dev/null +++ b/test/other/test_tsd_sync.ts @@ -0,0 +1,5 @@ +import moduleFactory from './test_emit_tsd_sync.js' + +const module = moduleFactory(); +module._fooVoid(); +module._fooInt(7, 8); diff --git a/test/test_other.py b/test/test_other.py index cd8da1fdbf628..941ede3b1016a 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3307,6 +3307,17 @@ def test_emit_tsd(self): cmd = shared.get_npm_cmd('tsc') + [test_file('other/test_tsd.ts'), '--noEmit'] shared.check_call(cmd) + def test_emit_tsd_sync_compilation(self): + self.run_process([EMCC, test_file('other/test_emit_tsd.c'), + '--emit-tsd', 'test_emit_tsd_sync.d.ts', + '-sMODULARIZE', '-sWASM_ASYNC_COMPILATION=0', + '-o', 'test_emit_tsd_sync.js'] + + self.get_emcc_args()) + self.assertFileContents(test_file('other/test_emit_tsd_sync.d.ts'), read_file('test_emit_tsd_sync.d.ts')) + # Test that the output compiles with a TS file that uses the defintions. + cmd = shared.get_npm_cmd('tsc') + [test_file('other/test_tsd_sync.ts'), '--noEmit'] + shared.check_call(cmd) + def test_emit_tsd_wasm_only(self): err = self.expect_fail([EMCC, test_file('other/test_emit_tsd.c'), '--emit-tsd', 'test_emit_tsd_wasm_only.d.ts', '-o', 'out.wasm']) diff --git a/tools/emscripten.py b/tools/emscripten.py index 3217c1d6d7eca..08356b8f0cd4b 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -664,7 +664,10 @@ def create_tsd(metadata, embind_tsd): export_interfaces += ' & EmbindModule' out += f'export type MainModule = {export_interfaces};\n' if settings.MODULARIZE: - out += 'export default function MainModuleFactory (options?: unknown): Promise;\n' + return_type = 'MainModule' + if settings.WASM_ASYNC_COMPILATION: + return_type = f'Promise<{return_type}>' + out += f'export default function MainModuleFactory (options?: unknown): {return_type};\n' return out