Skip to content

Commit cb158e3

Browse files
authored
[embind] Use type alias for string arguments definitions. (#21694)
Makes the TypeScript output easier to read. Fixes #21642
1 parent 3081a26 commit cb158e3

8 files changed

+39
-15
lines changed

src/embind/embind_gen.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ var LibraryEmbind = {
311311
$TsPrinter: class {
312312
constructor(definitions) {
313313
this.definitions = definitions;
314-
const jsString = 'ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string';
314+
const jsString = 'EmbindString'; // Type alias for multiple types.
315315
// The mapping is in the format of '<c++ name>' => ['toWireType', 'fromWireType']
316316
// or if the to/from wire types are the same use a single element.
317317
this.builtInToJsName = new Map([
@@ -334,6 +334,8 @@ var LibraryEmbind = {
334334
['std::u32string', ['string']],
335335
['emscripten::val', ['any']],
336336
]);
337+
// Signal that the type alias for EmbindString is needed.
338+
this.usedEmbindString = false;
337339
}
338340

339341
typeToJsName(type, isFromWireType = false) {
@@ -345,7 +347,11 @@ var LibraryEmbind = {
345347
throw new Error(`Missing primitive type to TS type for '${type.name}'`);
346348
}
347349
const [toWireType, fromWireType = toWireType] = this.builtInToJsName.get(type.name);
348-
return isFromWireType ? fromWireType : toWireType;
350+
const tsName = isFromWireType ? fromWireType : toWireType;
351+
if (tsName === 'EmbindString') {
352+
this.usedEmbindString = true;
353+
}
354+
return tsName;
349355
}
350356
if (type instanceof PointerDefinition) {
351357
return this.typeToJsName(type.classType);
@@ -372,7 +378,10 @@ var LibraryEmbind = {
372378
}
373379
def.printModuleEntry(this.typeToJsName.bind(this), out);
374380
}
375-
out.push('}');
381+
out.push('}\n');
382+
if (this.usedEmbindString) {
383+
out.unshift('type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;\n');
384+
}
376385
console.log(out.join(''));
377386
}
378387
},

test/other/embind_tsgen.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface WasmModule {
1515
_main(_0: number, _1: number): number;
1616
}
1717

18+
type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
1819
export interface Test {
1920
x: number;
2021
readonly y: number;
@@ -24,8 +25,8 @@ export interface Test {
2425
functionFive(x: number, y: number): number;
2526
constFn(): number;
2627
longFn(_0: number): number;
27-
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
28-
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
28+
functionThree(_0: EmbindString): number;
29+
functionSix(str: EmbindString): number;
2930
delete(): void;
3031
}
3132

@@ -118,7 +119,8 @@ interface EmbindModule {
118119
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
119120
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
120121
function_with_callback_param(_0: (message: string) => void): number;
121-
string_test(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): string;
122+
string_test(_0: EmbindString): string;
122123
wstring_test(_0: string): string;
123124
}
125+
124126
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/other/embind_tsgen_bigint.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ interface WasmModule {
1717
interface EmbindModule {
1818
bigintFn(_0: bigint): bigint;
1919
}
20+
2021
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/other/embind_tsgen_ignore_1.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface WasmModule {
3434
__emscripten_thread_exit(_0: number): void;
3535
}
3636

37+
type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
3738
export interface Test {
3839
x: number;
3940
readonly y: number;
@@ -43,8 +44,8 @@ export interface Test {
4344
functionFive(x: number, y: number): number;
4445
constFn(): number;
4546
longFn(_0: number): number;
46-
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
47-
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
47+
functionThree(_0: EmbindString): number;
48+
functionSix(str: EmbindString): number;
4849
delete(): void;
4950
}
5051

@@ -137,7 +138,8 @@ interface EmbindModule {
137138
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
138139
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
139140
function_with_callback_param(_0: (message: string) => void): number;
140-
string_test(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): string;
141+
string_test(_0: EmbindString): string;
141142
wstring_test(_0: string): string;
142143
}
144+
143145
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/other/embind_tsgen_ignore_2.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
interface WasmModule {
33
}
44

5+
type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
56
export interface Test {
67
x: number;
78
readonly y: number;
@@ -11,8 +12,8 @@ export interface Test {
1112
functionFive(x: number, y: number): number;
1213
constFn(): number;
1314
longFn(_0: number): number;
14-
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
15-
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
15+
functionThree(_0: EmbindString): number;
16+
functionSix(str: EmbindString): number;
1617
delete(): void;
1718
}
1819

@@ -105,8 +106,9 @@ interface EmbindModule {
105106
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
106107
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
107108
function_with_callback_param(_0: (message: string) => void): number;
108-
string_test(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): string;
109+
string_test(_0: EmbindString): string;
109110
wstring_test(_0: string): string;
110111
}
112+
111113
export type MainModule = WasmModule & EmbindModule;
112114
export default function MainModuleFactory (options?: unknown): Promise<MainModule>;

test/other/embind_tsgen_ignore_3.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface WasmModule {
1515
_main(_0: number, _1: number): number;
1616
}
1717

18+
type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
1819
export interface Test {
1920
x: number;
2021
readonly y: number;
@@ -24,8 +25,8 @@ export interface Test {
2425
functionFive(x: number, y: number): number;
2526
constFn(): number;
2627
longFn(_0: number): number;
27-
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
28-
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
28+
functionThree(_0: EmbindString): number;
29+
functionSix(str: EmbindString): number;
2930
delete(): void;
3031
}
3132

@@ -118,7 +119,8 @@ interface EmbindModule {
118119
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
119120
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
120121
function_with_callback_param(_0: (message: string) => void): number;
121-
string_test(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): string;
122+
string_test(_0: EmbindString): string;
122123
wstring_test(_0: string): string;
123124
}
125+
124126
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/other/embind_tsgen_memory64.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ interface WasmModule {
1717
interface EmbindModule {
1818
longFn(_0: bigint): bigint;
1919
}
20+
2021
export type MainModule = WasmModule & typeof RuntimeExports & EmbindModule;

test/test_other.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,6 +3204,11 @@ def test_embind_tsgen(self):
32043204
# expected.
32053205
self.do_runf('other/embind_tsgen.cpp', 'main ran',
32063206
emcc_args=['-lembind', '--emit-tsd', 'embind_tsgen.d.ts'])
3207+
3208+
# Test that the output compiles with a TS file that uses the defintions.
3209+
cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', '--noEmit']
3210+
shared.check_call(cmd)
3211+
32073212
actual = read_file('embind_tsgen.d.ts')
32083213
self.assertFileContents(test_file('other/embind_tsgen.d.ts'), actual)
32093214

0 commit comments

Comments
 (0)