Skip to content

Commit a3433a8

Browse files
committed
[tsgen] Expand TS generation test to compile and run a TS file.
In the new TS file we can add tests for things where just checking if the definition file compiles is not enough. This will help catch issues like emscripten-core#22569.
1 parent bf910d6 commit a3433a8

File tree

8 files changed

+91
-29
lines changed

8 files changed

+91
-29
lines changed

test/other/embind_tsgen.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,19 @@ struct ValArr {
5858
EMSCRIPTEN_DECLARE_VAL_TYPE(CallbackType);
5959

6060
struct ValObj {
61-
Foo foo;
6261
Bar bar;
62+
std::string string;
6363
CallbackType callback;
6464
ValObj() : callback(val::undefined()) {}
6565
};
6666

67+
ValObj getValObj() {
68+
ValObj o;
69+
return o;
70+
}
71+
72+
void setValObj(ValObj v) {}
73+
6774
class ClassWithConstructor {
6875
public:
6976
ClassWithConstructor(int, const ValArr&) {}
@@ -190,9 +197,11 @@ EMSCRIPTEN_BINDINGS(Test) {
190197
.element(emscripten::index<3>());
191198

192199
value_object<ValObj>("ValObj")
193-
.field("foo", &ValObj::foo)
200+
.field("string", &ValObj::string)
194201
.field("bar", &ValObj::bar)
195202
.field("callback", &ValObj::callback);
203+
function("getValObj", &getValObj);
204+
function("setValObj", &setValObj);
196205

197206
register_vector<int>("IntVec");
198207

test/other/embind_tsgen.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ export interface ClassWithSmartPtrConstructor {
8585
delete(): void;
8686
}
8787

88-
export type ValObj = {
89-
foo: Foo,
90-
bar: Bar,
91-
callback: (message: string) => void
92-
};
93-
9488
export interface BaseClass {
9589
fn(_0: number): number;
9690
delete(): void;
@@ -113,6 +107,12 @@ export interface InterfaceWrapper extends Interface {
113107

114108
export type ValArr = [ number, number, number ];
115109

110+
export type ValObj = {
111+
string: EmbindString,
112+
bar: Bar,
113+
callback: (message: string) => void
114+
};
115+
116116
interface EmbindModule {
117117
Test: {
118118
staticFunction(_0: number): number;
@@ -163,6 +163,8 @@ interface EmbindModule {
163163
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
164164
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
165165
function_with_callback_param(_0: (message: string) => void): number;
166+
getValObj(): ValObj;
167+
setValObj(_0: ValObj): void;
166168
string_test(_0: EmbindString): string;
167169
wstring_test(_0: string): string;
168170
}

test/other/embind_tsgen.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
import moduleLoader from './embind_tsgen.mjs';
3+
4+
const module = await moduleLoader();
5+
6+
// Test a few variations of passing value_objects with strings.
7+
module.setValObj({
8+
bar: module.Bar.valueOne,
9+
string: "ABCD",
10+
callback: () => {}
11+
});
12+
13+
module.setValObj({
14+
bar: module.Bar.valueOne,
15+
string: new Int8Array([65, 66, 67, 68]),
16+
callback: () => {}
17+
});
18+
19+
const valObj = module.getValObj();
20+
// TODO: remove the cast below when better definitions are generated for value
21+
// objects.
22+
const valString : string = valObj.string as string;
23+
24+
// Ensure nonnull pointers do no need a cast or nullptr check to use.
25+
const obj = module.getNonnullPointer();
26+
obj.delete();
27+
28+
console.log('ts ran');

test/other/embind_tsgen_ignore_1.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ export interface ClassWithSmartPtrConstructor {
9494
delete(): void;
9595
}
9696

97-
export type ValObj = {
98-
foo: Foo,
99-
bar: Bar,
100-
callback: (message: string) => void
101-
};
102-
10397
export interface BaseClass {
10498
fn(_0: number): number;
10599
delete(): void;
@@ -122,6 +116,12 @@ export interface InterfaceWrapper extends Interface {
122116

123117
export type ValArr = [ number, number, number ];
124118

119+
export type ValObj = {
120+
string: EmbindString,
121+
bar: Bar,
122+
callback: (message: string) => void
123+
};
124+
125125
interface EmbindModule {
126126
Test: {
127127
staticFunction(_0: number): number;
@@ -172,6 +172,8 @@ interface EmbindModule {
172172
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
173173
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
174174
function_with_callback_param(_0: (message: string) => void): number;
175+
getValObj(): ValObj;
176+
setValObj(_0: ValObj): void;
175177
string_test(_0: EmbindString): string;
176178
wstring_test(_0: string): string;
177179
}

test/other/embind_tsgen_ignore_2.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ export interface ClassWithSmartPtrConstructor {
7171
delete(): void;
7272
}
7373

74-
export type ValObj = {
75-
foo: Foo,
76-
bar: Bar,
77-
callback: (message: string) => void
78-
};
79-
8074
export interface BaseClass {
8175
fn(_0: number): number;
8276
delete(): void;
@@ -99,6 +93,12 @@ export interface InterfaceWrapper extends Interface {
9993

10094
export type ValArr = [ number, number, number ];
10195

96+
export type ValObj = {
97+
string: EmbindString,
98+
bar: Bar,
99+
callback: (message: string) => void
100+
};
101+
102102
interface EmbindModule {
103103
Test: {
104104
staticFunction(_0: number): number;
@@ -149,6 +149,8 @@ interface EmbindModule {
149149
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
150150
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
151151
function_with_callback_param(_0: (message: string) => void): number;
152+
getValObj(): ValObj;
153+
setValObj(_0: ValObj): void;
152154
string_test(_0: EmbindString): string;
153155
wstring_test(_0: string): string;
154156
}

test/other/embind_tsgen_ignore_3.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ export interface ClassWithSmartPtrConstructor {
8585
delete(): void;
8686
}
8787

88-
export type ValObj = {
89-
foo: Foo,
90-
bar: Bar,
91-
callback: (message: string) => void
92-
};
93-
9488
export interface BaseClass {
9589
fn(_0: number): number;
9690
delete(): void;
@@ -113,6 +107,12 @@ export interface InterfaceWrapper extends Interface {
113107

114108
export type ValArr = [ number, number, number ];
115109

110+
export type ValObj = {
111+
string: EmbindString,
112+
bar: Bar,
113+
callback: (message: string) => void
114+
};
115+
116116
interface EmbindModule {
117117
Test: {
118118
staticFunction(_0: number): number;
@@ -163,6 +163,8 @@ interface EmbindModule {
163163
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
164164
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
165165
function_with_callback_param(_0: (message: string) => void): number;
166+
getValObj(): ValObj;
167+
setValObj(_0: ValObj): void;
166168
string_test(_0: EmbindString): string;
167169
wstring_test(_0: string): string;
168170
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "index",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "main.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": ""
13+
}

test/test_other.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,12 +3365,16 @@ def test_jspi_add_function(self):
33653365
def test_embind_tsgen(self, opts):
33663366
# Check that TypeScript generation works and that the program is runs as
33673367
# expected.
3368-
self.do_runf('other/embind_tsgen.cpp', 'main ran',
3369-
emcc_args=['-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts)
3368+
self.emcc(test_file('other/embind_tsgen.cpp'),
3369+
['-o', 'embind_tsgen.mjs', '-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts)
33703370

33713371
# Test that the output compiles with a TS file that uses the defintions.
3372-
cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', '--noEmit']
3372+
shutil.copyfile(test_file('other/embind_tsgen.ts'), 'main.ts')
3373+
# A package file with type=module is needed to allow top level await in TS.
3374+
shutil.copyfile(test_file('other/embind_tsgen_package.json'), 'package.json')
3375+
cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', 'main.ts', '--module', 'NodeNext', '--moduleResolution', 'nodenext']
33733376
shared.check_call(cmd)
3377+
self.assertContained('main ran\nts ran', self.run_js('main.js'))
33743378

33753379
actual = read_file('embind_tsgen.d.ts')
33763380
self.assertFileContents(test_file('other/embind_tsgen.d.ts'), actual)

0 commit comments

Comments
 (0)