Skip to content

Commit

Permalink
[tsgen] Fix modularize definition for sync compilation. (#21766)
Browse files Browse the repository at this point in the history
Fixes #21760
  • Loading branch information
brendandahl authored Apr 16, 2024
1 parent ebc7938 commit 9f19ec9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions test/other/test_emit_tsd_sync.d.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions test/other/test_tsd_sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import moduleFactory from './test_emit_tsd_sync.js'

const module = moduleFactory();
module._fooVoid();
module._fooInt(7, 8);
11 changes: 11 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
5 changes: 4 additions & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainModule>;\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


Expand Down

0 comments on commit 9f19ec9

Please sign in to comment.