emscripten-core / emscripten-core/emscripten

Nested class fields not written from JavaScript

Open
#14,496 0 comments 0 reactions 0 assignees View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hi,

I have noticed that Embind does not update fields in nested classes, like you would expect in plain C/C++. Consider this example:
In C++:
```lang-cpp
class Data {
public:
int number = 5;
};

class Container {
public:
Data data;
};

int main() {
Container container;
std::cout << container.data.number << std::endl; // container.data.number is 5
container.data.number = 15; // setting number to 15
std::cout << container.data.number << std::endl; // container.data.number is 15
}

EMSCRIPTEN_BINDINGS(test) {
class_("Data")
.constructor()
.property("number", &Data::number);

class_("Container")
.constructor()
.property("data", &Container::data);
}
```
Output: 5 and 15

In JavaScript:
```lang-js
Module().then(Module => {
const container = new Module.Container();
console.log(container.data.number); // container.data.number is 5
container.data.number = 15; // setting number to 15
console.log(container.data.number); // container.data number is still 5
});
```
Output: 5 and 5, which is completely different from the result in C++.

Is this the intended behavior of Embind and if so, how would you change member variables of `Data` class in this case?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.