Skip to content

Commit 34e010c

Browse files
authored
[tsgen] Emit getters/setters for properties when the types are differ… (#22415)
…ent. Emitting a better type was already done for functions that returned strings, but now works with properties as well. Fixes #22387
1 parent 0bd6747 commit 34e010c

File tree

6 files changed

+128
-30
lines changed

6 files changed

+128
-30
lines changed

src/embind/embind_gen.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ var LibraryEmbind = {
175175
}
176176
out.push(' {\n');
177177
for (const property of this.properties) {
178-
out.push(' ');
179-
property.print(nameMap, out);
180-
out.push(';\n');
178+
const props = [];
179+
property.print(nameMap, props);
180+
for (const formattedProp of props) {
181+
out.push(` ${formattedProp};\n`);
182+
}
181183
}
182184
for (const method of this.methods) {
183185
out.push(' ');
@@ -205,9 +207,15 @@ var LibraryEmbind = {
205207
for (const prop of this.staticProperties) {
206208
const entry = [];
207209
prop.print(nameMap, entry);
208-
entries.push(entry.join(''));
210+
entries.push(...entry);
211+
}
212+
if (entries.length) {
213+
out.push('\n');
214+
for (const entry of entries) {
215+
out.push(` ${entry};\n`);
216+
}
217+
out.push(' ');
209218
}
210-
out.push(entries.join('; '));
211219
out.push('};\n');
212220
}
213221

@@ -243,7 +251,15 @@ var LibraryEmbind = {
243251
}
244252

245253
print(nameMap, out) {
246-
out.push(`${this.readonly ? 'readonly ' : ''}${this.name}: ${nameMap(this.type)}`);
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})`);
247263
}
248264
},
249265
$ConstantDefinition: class {

test/other/embind_tsgen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ class Test {
2222

2323
int getY() const { return y; }
2424

25+
std::string string_property;
26+
2527
static int static_function(int x) { return 1; }
2628

2729
static int static_property;
30+
static std::string static_string_property;
2831

2932
private:
3033
int x;
@@ -135,9 +138,11 @@ EMSCRIPTEN_BINDINGS(Test) {
135138
.function("constFn", &Test::const_fn)
136139
.property("x", &Test::getX, &Test::setX)
137140
.property("y", &Test::getY)
141+
.property("stringProperty", &Test::string_property)
138142
.class_function("staticFunction", &Test::static_function)
139143
.class_function("staticFunctionWithParam(x)", &Test::static_function)
140144
.class_property("staticProperty", &Test::static_property)
145+
.class_property("staticStringProperty", &Test::static_string_property)
141146
;
142147

143148
function("class_returning_fn", &class_returning_fn);
@@ -221,6 +226,7 @@ EMSCRIPTEN_BINDINGS(Test) {
221226
}
222227

223228
int Test::static_property = 42;
229+
std::string Test::static_string_property = "";
224230

225231
int main() {
226232
// Main should not be run during TypeScript generation, but should run when

test/other/embind_tsgen.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
2020
export interface Test {
2121
x: number;
2222
readonly y: number;
23+
get stringProperty(): string;
24+
set stringProperty(value: EmbindString);
2325
functionOne(_0: number, _1: number): number;
2426
functionTwo(_0: number, _1: number): number;
2527
functionFour(_0: boolean): number;
@@ -102,7 +104,13 @@ export interface DerivedClass extends BaseClass {
102104
export type ValArr = [ number, number, number ];
103105

104106
interface EmbindModule {
105-
Test: {staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
107+
Test: {
108+
staticFunction(_0: number): number;
109+
staticFunctionWithParam(x: number): number;
110+
staticProperty: number;
111+
get staticStringProperty(): string;
112+
set staticStringProperty(value: EmbindString);
113+
};
106114
class_returning_fn(): Test;
107115
class_unique_ptr_returning_fn(): Test;
108116
Obj: {};
@@ -112,12 +120,23 @@ interface EmbindModule {
112120
Bar: {valueOne: BarValue<0>, valueTwo: BarValue<1>, valueThree: BarValue<2>};
113121
EmptyEnum: {};
114122
enum_returning_fn(): Bar;
115-
IntVec: {new(): IntVec};
116-
MapIntInt: {new(): MapIntInt};
123+
IntVec: {
124+
new(): IntVec;
125+
};
126+
MapIntInt: {
127+
new(): MapIntInt;
128+
};
117129
Foo: {};
118-
ClassWithConstructor: {new(_0: number, _1: ValArr): ClassWithConstructor};
119-
ClassWithTwoConstructors: {new(): ClassWithTwoConstructors; new(_0: number): ClassWithTwoConstructors};
120-
ClassWithSmartPtrConstructor: {new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor};
130+
ClassWithConstructor: {
131+
new(_0: number, _1: ValArr): ClassWithConstructor;
132+
};
133+
ClassWithTwoConstructors: {
134+
new(): ClassWithTwoConstructors;
135+
new(_0: number): ClassWithTwoConstructors;
136+
};
137+
ClassWithSmartPtrConstructor: {
138+
new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor;
139+
};
121140
BaseClass: {};
122141
DerivedClass: {};
123142
a_bool: boolean;

test/other/embind_tsgen_ignore_1.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
2929
export interface Test {
3030
x: number;
3131
readonly y: number;
32+
get stringProperty(): string;
33+
set stringProperty(value: EmbindString);
3234
functionOne(_0: number, _1: number): number;
3335
functionTwo(_0: number, _1: number): number;
3436
functionFour(_0: boolean): number;
@@ -111,7 +113,13 @@ export interface DerivedClass extends BaseClass {
111113
export type ValArr = [ number, number, number ];
112114

113115
interface EmbindModule {
114-
Test: {staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
116+
Test: {
117+
staticFunction(_0: number): number;
118+
staticFunctionWithParam(x: number): number;
119+
staticProperty: number;
120+
get staticStringProperty(): string;
121+
set staticStringProperty(value: EmbindString);
122+
};
115123
class_returning_fn(): Test;
116124
class_unique_ptr_returning_fn(): Test;
117125
Obj: {};
@@ -121,12 +129,23 @@ interface EmbindModule {
121129
Bar: {valueOne: BarValue<0>, valueTwo: BarValue<1>, valueThree: BarValue<2>};
122130
EmptyEnum: {};
123131
enum_returning_fn(): Bar;
124-
IntVec: {new(): IntVec};
125-
MapIntInt: {new(): MapIntInt};
132+
IntVec: {
133+
new(): IntVec;
134+
};
135+
MapIntInt: {
136+
new(): MapIntInt;
137+
};
126138
Foo: {};
127-
ClassWithConstructor: {new(_0: number, _1: ValArr): ClassWithConstructor};
128-
ClassWithTwoConstructors: {new(): ClassWithTwoConstructors; new(_0: number): ClassWithTwoConstructors};
129-
ClassWithSmartPtrConstructor: {new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor};
139+
ClassWithConstructor: {
140+
new(_0: number, _1: ValArr): ClassWithConstructor;
141+
};
142+
ClassWithTwoConstructors: {
143+
new(): ClassWithTwoConstructors;
144+
new(_0: number): ClassWithTwoConstructors;
145+
};
146+
ClassWithSmartPtrConstructor: {
147+
new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor;
148+
};
130149
BaseClass: {};
131150
DerivedClass: {};
132151
a_bool: boolean;

test/other/embind_tsgen_ignore_2.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
66
export interface Test {
77
x: number;
88
readonly y: number;
9+
get stringProperty(): string;
10+
set stringProperty(value: EmbindString);
911
functionOne(_0: number, _1: number): number;
1012
functionTwo(_0: number, _1: number): number;
1113
functionFour(_0: boolean): number;
@@ -88,7 +90,13 @@ export interface DerivedClass extends BaseClass {
8890
export type ValArr = [ number, number, number ];
8991

9092
interface EmbindModule {
91-
Test: {staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
93+
Test: {
94+
staticFunction(_0: number): number;
95+
staticFunctionWithParam(x: number): number;
96+
staticProperty: number;
97+
get staticStringProperty(): string;
98+
set staticStringProperty(value: EmbindString);
99+
};
92100
class_returning_fn(): Test;
93101
class_unique_ptr_returning_fn(): Test;
94102
Obj: {};
@@ -98,12 +106,23 @@ interface EmbindModule {
98106
Bar: {valueOne: BarValue<0>, valueTwo: BarValue<1>, valueThree: BarValue<2>};
99107
EmptyEnum: {};
100108
enum_returning_fn(): Bar;
101-
IntVec: {new(): IntVec};
102-
MapIntInt: {new(): MapIntInt};
109+
IntVec: {
110+
new(): IntVec;
111+
};
112+
MapIntInt: {
113+
new(): MapIntInt;
114+
};
103115
Foo: {};
104-
ClassWithConstructor: {new(_0: number, _1: ValArr): ClassWithConstructor};
105-
ClassWithTwoConstructors: {new(): ClassWithTwoConstructors; new(_0: number): ClassWithTwoConstructors};
106-
ClassWithSmartPtrConstructor: {new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor};
116+
ClassWithConstructor: {
117+
new(_0: number, _1: ValArr): ClassWithConstructor;
118+
};
119+
ClassWithTwoConstructors: {
120+
new(): ClassWithTwoConstructors;
121+
new(_0: number): ClassWithTwoConstructors;
122+
};
123+
ClassWithSmartPtrConstructor: {
124+
new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor;
125+
};
107126
BaseClass: {};
108127
DerivedClass: {};
109128
a_bool: boolean;

test/other/embind_tsgen_ignore_3.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type EmbindString = ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string;
2020
export interface Test {
2121
x: number;
2222
readonly y: number;
23+
get stringProperty(): string;
24+
set stringProperty(value: EmbindString);
2325
functionOne(_0: number, _1: number): number;
2426
functionTwo(_0: number, _1: number): number;
2527
functionFour(_0: boolean): number;
@@ -102,7 +104,13 @@ export interface DerivedClass extends BaseClass {
102104
export type ValArr = [ number, number, number ];
103105

104106
interface EmbindModule {
105-
Test: {staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
107+
Test: {
108+
staticFunction(_0: number): number;
109+
staticFunctionWithParam(x: number): number;
110+
staticProperty: number;
111+
get staticStringProperty(): string;
112+
set staticStringProperty(value: EmbindString);
113+
};
106114
class_returning_fn(): Test;
107115
class_unique_ptr_returning_fn(): Test;
108116
Obj: {};
@@ -112,12 +120,23 @@ interface EmbindModule {
112120
Bar: {valueOne: BarValue<0>, valueTwo: BarValue<1>, valueThree: BarValue<2>};
113121
EmptyEnum: {};
114122
enum_returning_fn(): Bar;
115-
IntVec: {new(): IntVec};
116-
MapIntInt: {new(): MapIntInt};
123+
IntVec: {
124+
new(): IntVec;
125+
};
126+
MapIntInt: {
127+
new(): MapIntInt;
128+
};
117129
Foo: {};
118-
ClassWithConstructor: {new(_0: number, _1: ValArr): ClassWithConstructor};
119-
ClassWithTwoConstructors: {new(): ClassWithTwoConstructors; new(_0: number): ClassWithTwoConstructors};
120-
ClassWithSmartPtrConstructor: {new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor};
130+
ClassWithConstructor: {
131+
new(_0: number, _1: ValArr): ClassWithConstructor;
132+
};
133+
ClassWithTwoConstructors: {
134+
new(): ClassWithTwoConstructors;
135+
new(_0: number): ClassWithTwoConstructors;
136+
};
137+
ClassWithSmartPtrConstructor: {
138+
new(_0: number, _1: ValArr): ClassWithSmartPtrConstructor;
139+
};
121140
BaseClass: {};
122141
DerivedClass: {};
123142
a_bool: boolean;

0 commit comments

Comments
 (0)