Skip to content

Commit 9f19ec9

Browse files
authored
[tsgen] Fix modularize definition for sync compilation. (#21766)
Fixes #21760
1 parent ebc7938 commit 9f19ec9

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

test/other/test_emit_tsd_sync.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TypeScript bindings for emscripten-generated code. Automatically generated at compile time.
2+
declare namespace RuntimeExports {
3+
let HEAPF64: any;
4+
let HEAP_DATA_VIEW: any;
5+
let HEAP8: any;
6+
let HEAPU8: any;
7+
let HEAP16: any;
8+
let HEAPU16: any;
9+
let HEAP32: any;
10+
let HEAPU32: any;
11+
let HEAP64: any;
12+
let HEAPU64: any;
13+
}
14+
interface WasmModule {
15+
_fooVoid(): void;
16+
_fooInt(_0: number, _1: number): number;
17+
_main(_0: number, _1: number): number;
18+
}
19+
20+
export type MainModule = WasmModule & typeof RuntimeExports;
21+
export default function MainModuleFactory (options?: unknown): MainModule;

test/other/test_tsd_sync.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import moduleFactory from './test_emit_tsd_sync.js'
2+
3+
const module = moduleFactory();
4+
module._fooVoid();
5+
module._fooInt(7, 8);

test/test_other.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,6 +3307,17 @@ def test_emit_tsd(self):
33073307
cmd = shared.get_npm_cmd('tsc') + [test_file('other/test_tsd.ts'), '--noEmit']
33083308
shared.check_call(cmd)
33093309

3310+
def test_emit_tsd_sync_compilation(self):
3311+
self.run_process([EMCC, test_file('other/test_emit_tsd.c'),
3312+
'--emit-tsd', 'test_emit_tsd_sync.d.ts',
3313+
'-sMODULARIZE', '-sWASM_ASYNC_COMPILATION=0',
3314+
'-o', 'test_emit_tsd_sync.js'] +
3315+
self.get_emcc_args())
3316+
self.assertFileContents(test_file('other/test_emit_tsd_sync.d.ts'), read_file('test_emit_tsd_sync.d.ts'))
3317+
# Test that the output compiles with a TS file that uses the defintions.
3318+
cmd = shared.get_npm_cmd('tsc') + [test_file('other/test_tsd_sync.ts'), '--noEmit']
3319+
shared.check_call(cmd)
3320+
33103321
def test_emit_tsd_wasm_only(self):
33113322
err = self.expect_fail([EMCC, test_file('other/test_emit_tsd.c'),
33123323
'--emit-tsd', 'test_emit_tsd_wasm_only.d.ts', '-o', 'out.wasm'])

tools/emscripten.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,10 @@ def create_tsd(metadata, embind_tsd):
664664
export_interfaces += ' & EmbindModule'
665665
out += f'export type MainModule = {export_interfaces};\n'
666666
if settings.MODULARIZE:
667-
out += 'export default function MainModuleFactory (options?: unknown): Promise<MainModule>;\n'
667+
return_type = 'MainModule'
668+
if settings.WASM_ASYNC_COMPILATION:
669+
return_type = f'Promise<{return_type}>'
670+
out += f'export default function MainModuleFactory (options?: unknown): {return_type};\n'
668671
return out
669672

670673

0 commit comments

Comments
 (0)