Skip to content

Commit

Permalink
embind: Support readonly properties (emscripten-core#20080)
Browse files Browse the repository at this point in the history
* embind: Support readonly properties

* embind: Add test and doc for readonly property

---------

Co-authored-by: Reza Roohian <roohian@magazino.eu>
  • Loading branch information
mrroohian and Reza Roohian authored Aug 21, 2023
1 parent 3737863 commit 3dbcacf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ For example:
.constructor<int, std::string>()
.function("incrementX", &MyClass::incrementX)
.property("x", &MyClass::getX, &MyClass::setX)
.property("x_readonly", &MyClass::getX)
.class_function("getStringFromInstance", &MyClass::getStringFromInstance)
;
}
Expand Down
10 changes: 6 additions & 4 deletions src/embind/embind_ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ var LibraryEmbind = {
}
},
$ClassProperty: class ClassProperty {
constructor(type, name) {
constructor(type, name, readonly) {
this.type = type;
this.name = name;
this.readonly = readonly;
}

print(nameMap, out) {
out.push(`${this.name}: ${nameMap(this.type)}`);
out.push(`${this.readonly ? 'readonly ' : ''}${this.name}: ${nameMap(this.type)}`);
}
},
$ConstantDefinition: class ConstantDefinition {
Expand Down Expand Up @@ -377,11 +378,12 @@ var LibraryEmbind = {
setter,
setterContext) {
fieldName = readLatin1String(fieldName);
assert(getterReturnType === setterArgumentType, 'Mismatched getter and setter types are not supported.');
const readonly = setter === 0;
assert(readonly || getterReturnType === setterArgumentType, 'Mismatched getter and setter types are not supported.');
whenDependentTypesAreResolved([], [classType], function(classType) {
classType = classType[0];
whenDependentTypesAreResolved([], [getterReturnType], function(types) {
const prop = new ClassProperty(types[0], fieldName);
const prop = new ClassProperty(types[0], fieldName, readonly);
classType.properties.push(prop);
return [];
});
Expand Down
4 changes: 4 additions & 0 deletions test/other/embind_tsgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class Test {
int getX() const { return x; }
void setX(int x_) { x = x_; }

int getY() const { return y; }

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

static int static_property;

private:
int x;
int y;
};

Test class_returning_fn() { return Test(); }
Expand Down Expand Up @@ -88,6 +91,7 @@ EMSCRIPTEN_BINDINGS(Test) {
.function("functionFour", &Test::function_four)
.function("constFn", &Test::const_fn)
.property("x", &Test::getX, &Test::setX)
.property("y", &Test::getY)
.class_function("staticFunction", &Test::static_function)
.class_property("staticProperty", &Test::static_property)
;
Expand Down
1 change: 1 addition & 0 deletions test/other/embind_tsgen.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Test {
x: number;
readonly y: number;
functionOne(_0: number, _1: number): number;
functionTwo(_0: number, _1: number): number;
functionFour(_0: boolean): number;
Expand Down

0 comments on commit 3dbcacf

Please sign in to comment.