Skip to content

Commit fffa523

Browse files
authored
embind: Support TS generation for static class properties. (#20075)
1 parent 097290d commit fffa523

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/embind/embind_ts.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var LibraryEmbind = {
5656
this.name = name;
5757
this.methods = [];
5858
this.staticMethods = [];
59+
this.staticProperties = [];
5960
this.constructors = [
6061
new FunctionDefinition('default', this, [])
6162
];
@@ -72,6 +73,7 @@ var LibraryEmbind = {
7273
for (const property of this.properties) {
7374
out.push(' ');
7475
property.print(nameMap, out);
76+
out.push(';\n');
7577
}
7678
for (const method of this.methods) {
7779
out.push(' ');
@@ -91,6 +93,10 @@ var LibraryEmbind = {
9193
out.push('; ');
9294
method.printFunction(nameMap, out);
9395
}
96+
for (const prop of this.staticProperties) {
97+
out.push('; ');
98+
prop.print(nameMap, out);
99+
}
94100
out.push('};\n');
95101
}
96102
},
@@ -101,7 +107,7 @@ var LibraryEmbind = {
101107
}
102108

103109
print(nameMap, out) {
104-
out.push(`${this.name}: ${nameMap(this.type)};\n`);
110+
out.push(`${this.name}: ${nameMap(this.type)}`);
105111
}
106112
},
107113
$ConstantDefinition: class ConstantDefinition {
@@ -399,6 +405,27 @@ var LibraryEmbind = {
399405
return [];
400406
});
401407
},
408+
_embind_register_class_class_property__deps: [
409+
'$readLatin1String', '$whenDependentTypesAreResolved', '$ClassProperty'],
410+
_embind_register_class_class_property: (rawClassType,
411+
fieldName,
412+
rawFieldType,
413+
rawFieldPtr,
414+
getterSignature,
415+
getter,
416+
setterSignature,
417+
setter) => {
418+
fieldName = readLatin1String(fieldName);
419+
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
420+
classType = classType[0];
421+
whenDependentTypesAreResolved([], [rawFieldType], function(types) {
422+
const prop = new ClassProperty(types[0], fieldName);
423+
classType.staticProperties.push(prop);
424+
return [];
425+
});
426+
return [];
427+
});
428+
},
402429
_embind_register_enum__deps: ['$readLatin1String', '$EnumDefinition', '$moduleDefinitions'],
403430
_embind_register_enum: function(rawType, name, size, isSigned) {
404431
name = readLatin1String(name);

test/other/embind_tsgen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class Test {
1818

1919
static int static_function(int x) { return 1; }
2020

21+
static int static_property;
22+
2123
private:
2224
int x;
2325
};
@@ -87,6 +89,7 @@ EMSCRIPTEN_BINDINGS(Test) {
8789
.function("constFn", &Test::const_fn)
8890
.property("x", &Test::getX, &Test::setX)
8991
.class_function("staticFunction", &Test::static_function)
92+
.class_property("staticProperty", &Test::static_property)
9093
;
9194

9295
function("class_returning_fn", &class_returning_fn);
@@ -144,6 +147,8 @@ EMSCRIPTEN_BINDINGS(Test) {
144147
.function("fn2", &DerivedClass::fn2);
145148
}
146149

150+
int Test::static_property = 42;
151+
147152
int main() {
148153
// Main should not be run during TypeScript generation.
149154
abort();

test/other/embind_tsgen.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface DerivedClass extends BaseClass {
5757
export type ValArr = [ number, number, number ];
5858

5959
export interface MainModule {
60-
Test: {new(): Test; staticFunction(_0: number): number};
60+
Test: {new(): Test; staticFunction(_0: number): number; staticProperty: number};
6161
class_returning_fn(): Test;
6262
class_unique_ptr_returning_fn(): Test;
6363
a_class_instance: Test;

0 commit comments

Comments
 (0)