Skip to content

Commit 25bec47

Browse files
authored
embind: Generate TS definitions from static class methods. (#19895)
1 parent 71c72a2 commit 25bec47

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/embind/embind_ts.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ var LibraryEmbind = {
4242
printFunction(nameMap, out) {
4343
out.push(`${this.name}`);
4444
this.printSignature(nameMap, out);
45-
out.push(';\n');
4645
}
4746

4847
printModuleEntry(nameMap, out) {
4948
out.push(' ');
5049
this.printFunction(nameMap, out);
50+
out.push(';\n');
5151
}
5252
},
5353
$ClassDefinition: class ClassDefinition {
5454
constructor(typeId, name, base = null) {
5555
this.typeId = typeId;
5656
this.name = name;
5757
this.methods = [];
58+
this.staticMethods = [];
5859
this.constructors = [
5960
new FunctionDefinition('default', this, [])
6061
];
@@ -75,6 +76,7 @@ var LibraryEmbind = {
7576
for (const method of this.methods) {
7677
out.push(' ');
7778
method.printFunction(nameMap, out);
79+
out.push(';\n');
7880
}
7981
out.push(' delete(): void;\n');
8082
out.push('}\n\n');
@@ -85,6 +87,10 @@ var LibraryEmbind = {
8587
// TODO Handle constructor overloading
8688
const constructor = this.constructors[this.constructors.length > 1 ? 1 : 0];
8789
constructor.printSignature(nameMap, out);
90+
for (const method of this.staticMethods) {
91+
out.push('; ');
92+
method.printFunction(nameMap, out);
93+
}
8894
out.push('};\n');
8995
}
9096
},
@@ -376,6 +382,23 @@ var LibraryEmbind = {
376382
return [];
377383
});
378384
},
385+
_embind_register_class_class_function__deps: ['$createFunctionDefinition'],
386+
_embind_register_class_class_function: function(rawClassType,
387+
methodName,
388+
argCount,
389+
rawArgTypesAddr,
390+
invokerSignature,
391+
rawInvoker,
392+
fn,
393+
isAsync) {
394+
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
395+
classType = classType[0];
396+
createFunctionDefinition(methodName, argCount, rawArgTypesAddr, false, (funcDef) => {
397+
classType.staticMethods.push(funcDef);
398+
});
399+
return [];
400+
});
401+
},
379402
_embind_register_enum__deps: ['$readLatin1String', '$EnumDefinition', '$moduleDefinitions'],
380403
_embind_register_enum: function(rawType, name, size, isSigned) {
381404
name = readLatin1String(name);

test/other/embind_tsgen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Test {
1616
int getX() const { return x; }
1717
void setX(int x_) { x = x_; }
1818

19+
static int static_function(int x) { return 1; }
20+
1921
private:
2022
int x;
2123
};
@@ -84,6 +86,7 @@ EMSCRIPTEN_BINDINGS(Test) {
8486
.function("functionFour", &Test::function_four)
8587
.function("constFn", &Test::const_fn)
8688
.property("x", &Test::getX, &Test::setX)
89+
.class_function("staticFunction", &Test::static_function)
8790
;
8891

8992
function("class_returning_fn", &class_returning_fn);

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};
60+
Test: {new(): Test; staticFunction(_0: number): number};
6161
class_returning_fn(): Test;
6262
class_unique_ptr_returning_fn(): Test;
6363
a_class_instance: Test;

0 commit comments

Comments
 (0)