Skip to content

Commit 3dbcacf

Browse files
mrroohianReza Roohian
andauthored
embind: Support readonly properties (#20080)
* embind: Support readonly properties * embind: Add test and doc for readonly property --------- Co-authored-by: Reza Roohian <roohian@magazino.eu>
1 parent 3737863 commit 3dbcacf

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

site/source/docs/porting/connecting_cpp_and_javascript/embind.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ For example:
146146
.constructor<int, std::string>()
147147
.function("incrementX", &MyClass::incrementX)
148148
.property("x", &MyClass::getX, &MyClass::setX)
149+
.property("x_readonly", &MyClass::getX)
149150
.class_function("getStringFromInstance", &MyClass::getStringFromInstance)
150151
;
151152
}

src/embind/embind_ts.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ var LibraryEmbind = {
101101
}
102102
},
103103
$ClassProperty: class ClassProperty {
104-
constructor(type, name) {
104+
constructor(type, name, readonly) {
105105
this.type = type;
106106
this.name = name;
107+
this.readonly = readonly;
107108
}
108109

109110
print(nameMap, out) {
110-
out.push(`${this.name}: ${nameMap(this.type)}`);
111+
out.push(`${this.readonly ? 'readonly ' : ''}${this.name}: ${nameMap(this.type)}`);
111112
}
112113
},
113114
$ConstantDefinition: class ConstantDefinition {
@@ -377,11 +378,12 @@ var LibraryEmbind = {
377378
setter,
378379
setterContext) {
379380
fieldName = readLatin1String(fieldName);
380-
assert(getterReturnType === setterArgumentType, 'Mismatched getter and setter types are not supported.');
381+
const readonly = setter === 0;
382+
assert(readonly || getterReturnType === setterArgumentType, 'Mismatched getter and setter types are not supported.');
381383
whenDependentTypesAreResolved([], [classType], function(classType) {
382384
classType = classType[0];
383385
whenDependentTypesAreResolved([], [getterReturnType], function(types) {
384-
const prop = new ClassProperty(types[0], fieldName);
386+
const prop = new ClassProperty(types[0], fieldName, readonly);
385387
classType.properties.push(prop);
386388
return [];
387389
});

test/other/embind_tsgen.cpp

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

19+
int getY() const { return y; }
20+
1921
static int static_function(int x) { return 1; }
2022

2123
static int static_property;
2224

2325
private:
2426
int x;
27+
int y;
2528
};
2629

2730
Test class_returning_fn() { return Test(); }
@@ -88,6 +91,7 @@ EMSCRIPTEN_BINDINGS(Test) {
8891
.function("functionFour", &Test::function_four)
8992
.function("constFn", &Test::const_fn)
9093
.property("x", &Test::getX, &Test::setX)
94+
.property("y", &Test::getY)
9195
.class_function("staticFunction", &Test::static_function)
9296
.class_property("staticProperty", &Test::static_property)
9397
;

test/other/embind_tsgen.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface Test {
22
x: number;
3+
readonly y: number;
34
functionOne(_0: number, _1: number): number;
45
functionTwo(_0: number, _1: number): number;
56
functionFour(_0: boolean): number;

0 commit comments

Comments
 (0)