Skip to content

Commit bdf5b0d

Browse files
authored
[tsgen] Handle different get/set types for value_objects too. (#22439)
Mentioned in #22387
1 parent 14cb3a3 commit bdf5b0d

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

src/embind/embind_gen.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ var LibraryEmbind = {
243243
}
244244

245245
},
246+
$printProperty: (prop, nameMap, out) => {
247+
const setType = nameMap(prop.type, false);
248+
const getType = nameMap(prop.type, true);
249+
if (prop.readonly || setType === getType) {
250+
out.push(`${prop.readonly ? 'readonly ' : ''}${prop.name}: ${getType}`);
251+
return;
252+
}
253+
// The getter/setter types don't match, so generate each get/set definition.
254+
out.push(`get ${prop.name}(): ${getType}`);
255+
out.push(`set ${prop.name}(value: ${setType})`);
256+
},
257+
$ClassProperty__deps: ['$printProperty'],
246258
$ClassProperty: class {
247259
constructor(type, name, readonly) {
248260
this.type = type;
@@ -251,15 +263,7 @@ var LibraryEmbind = {
251263
}
252264

253265
print(nameMap, out) {
254-
const setType = nameMap(this.type, false);
255-
const getType = nameMap(this.type, true);
256-
if (this.readonly || setType === getType) {
257-
out.push(`${this.readonly ? 'readonly ' : ''}${this.name}: ${getType}`);
258-
return;
259-
}
260-
// The getter/setter types don't match, so generate each get/set definition.
261-
out.push(`get ${this.name}(): ${getType}`);
262-
out.push(`set ${this.name}(value: ${setType})`);
266+
printProperty(this, nameMap, out);
263267
}
264268
},
265269
$ConstantDefinition: class {
@@ -325,6 +329,7 @@ var LibraryEmbind = {
325329
out.push(' ];\n\n');
326330
}
327331
},
332+
$ValueObjectDefinition__deps: ['$printProperty'],
328333
$ValueObjectDefinition: class {
329334
constructor(typeId, name) {
330335
this.typeId = typeId;
@@ -336,12 +341,14 @@ var LibraryEmbind = {
336341
}
337342

338343
print(nameMap, out) {
339-
out.push(`export type ${this.name} = {\n`);
344+
out.push(`export type ${this.name} = {\n `);
340345
const outFields = [];
341-
for (const {name, type} of this.fields) {
342-
outFields.push(` ${name}: ${nameMap(type)}`);
346+
for (const field of this.fields) {
347+
const property = [];
348+
printProperty(field, nameMap, property);
349+
outFields.push(...property);
343350
}
344-
out.push(outFields.join(',\n'))
351+
out.push(outFields.join(',\n '))
345352
out.push('\n};\n\n');
346353
}
347354
},

test/other/embind_tsgen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ EMSCRIPTEN_DECLARE_VAL_TYPE(CallbackType);
6060
struct ValObj {
6161
Foo foo;
6262
Bar bar;
63+
std::string str;
6364
CallbackType callback;
6465
ValObj() : callback(val::undefined()) {}
6566
};
@@ -178,6 +179,7 @@ EMSCRIPTEN_BINDINGS(Test) {
178179
value_object<ValObj>("ValObj")
179180
.field("foo", &ValObj::foo)
180181
.field("bar", &ValObj::bar)
182+
.field("str", &ValObj::str)
181183
.field("callback", &ValObj::callback);
182184

183185
register_vector<int>("IntVec");

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;
@@ -103,6 +97,14 @@ export interface DerivedClass extends BaseClass {
10397

10498
export type ValArr = [ number, number, number ];
10599

100+
export type ValObj = {
101+
foo: Foo,
102+
bar: Bar,
103+
get str(): string,
104+
set str(value: EmbindString),
105+
callback: (message: string) => void
106+
};
107+
106108
interface EmbindModule {
107109
Test: {
108110
staticFunction(_0: number): number;

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;
@@ -112,6 +106,14 @@ export interface DerivedClass extends BaseClass {
112106

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

109+
export type ValObj = {
110+
foo: Foo,
111+
bar: Bar,
112+
get str(): string,
113+
set str(value: EmbindString),
114+
callback: (message: string) => void
115+
};
116+
115117
interface EmbindModule {
116118
Test: {
117119
staticFunction(_0: number): number;

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;
@@ -89,6 +83,14 @@ export interface DerivedClass extends BaseClass {
8983

9084
export type ValArr = [ number, number, number ];
9185

86+
export type ValObj = {
87+
foo: Foo,
88+
bar: Bar,
89+
get str(): string,
90+
set str(value: EmbindString),
91+
callback: (message: string) => void
92+
};
93+
9294
interface EmbindModule {
9395
Test: {
9496
staticFunction(_0: number): number;

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;
@@ -103,6 +97,14 @@ export interface DerivedClass extends BaseClass {
10397

10498
export type ValArr = [ number, number, number ];
10599

100+
export type ValObj = {
101+
foo: Foo,
102+
bar: Bar,
103+
get str(): string,
104+
set str(value: EmbindString),
105+
callback: (message: string) => void
106+
};
107+
106108
interface EmbindModule {
107109
Test: {
108110
staticFunction(_0: number): number;

0 commit comments

Comments
 (0)