I have a function that takes a std::function as a parameter:
class Foo {
virtual void bar(std::function<void()> &&func) = 0;
};
I want to create a JS wrapper that implements Foo.
struct FooWrapper : public wrapper<Foo> {
EMSCRIPTEN_WRAPPER(FooWrapper);
void bar(std::function<void()> &&func) override {
call<void>("bar", std::move(func));
}
};
EMSCRIPTEN_BINDINGS(Foo) {
class_<Foo>("Foo")
.smart_ptr<Foo>("Foo")
.allow_subclass<FooWrapper, std::shared_ptr<FooWrapper>>("FooWraper", "FooWraperSharedPtr");
}
However, when I try to call bar from JavaScript (TypeScript), I get a BindingError.
const foo = // Create Foo object
foo.bar(() => {
// Do something
})
BindingError: parameter 1 has unknown type NSt3__28functionIFvvEEE
Does anyone know how to bind a std::function so that I can pass a JavaScript lambda function to it?
This actually is possible.
EMSCRIPTEN_BINDINGS(FUNCTION_VOID) {
class_<std::function<void()>>("Function_Void")
.constructor()
.function("opCall", &std::function<void()>::operator());
}
The JavaScript can then execute a std::function by simply calling opCall().