emscripten-core / emscripten-core/emscripten

EMSCRIPTEN_BINDINGS and classes

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

Description

I'm trying to do a pretty simple thing - create a button fully using emscripten. Bindings however don't seem to work in this situation.

Button.h:
```c++
class Button {
public:
Button(const std::string &id, std::function callback);

void onClick();

private:
std::string id;
emscripten::val element;
std::function onClickCallback;
};
```

Button.cpp:
```c++
Button::Button(const std::string &id, std::function callback) : id(id) {
onClickCallback = callback;
element = val::global("document").call("getElementById", id);

element.call(
"addEventListener",
std::string("click"),
val::module_property("onClick")
);
}

void Button::onClick() {
if (onClickCallback) onClickCallback();
}

EMSCRIPTEN_BINDINGS(button_bindings) {
class_("Button")
.constructor>()
.function("onClick", &Button::onClick);
}
```

I have a general understanding of why it might not work, but I want to clarify and perhaps find a solution.

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.