I have a C++ class with a number of pure virtual methods. To allow that a JS class can implement those I need to bind the constructor(s) of that class. Unfortunately, embind won't allow that, because of the pure virtual methods.
As an attempt to solve this problem I created a helper C++ class, which implements all those pure virtual methods and returns empty values for them. This leaves me with this code:
class Test {
public:
Test(int i) {}
~Test() {}
virtual std::string get() const = 0;
}
class TestHelper extends Test {
public:
virtual std::string get() const override { return ""; }
}
class TestWrapper : public wrapper<TestHelper> {
public:
EMSCRIPTEN_WRAPPER(TestWrapper);
virtual ~TestWrapper() noexcept override {
}
}
EMSCRIPTEN_BINDINGS(main) {
class_<TestHelper, base<Test>>("Test")
.allow_subclass<TestWrapper>("TestWrapper");
}
This compiles fine and both Test
and TestWrapper
exist in the generated wasm module. However, neither of the two contain the methods extend
and implement
. Other classes, which don't use an intermediate class to implement the pure virtual methods contain those methods.
What is the correct way to generate the missing methods also in the case of the Test
class?
The extend
and implement
methods are only added, if the entire inheritance chain is bound. In the scenario above no binding for the Test
class exists. Once it is added, also the missing methods appear.