Extending a C++ class with embed

100 views
Skip to first unread message

Jim Lloyd

unread,
Mar 7, 2021, 3:45:16 PM3/7/21
to emscripten-discuss
I'm climbing the learning curve with embind and  having difficulty using the documentation for Deriving from C++ classes in Javascript to produce working code. The test embind_test.cpp looks like it should be sufficient to fill in the gap in my understanding, but isn't quite doing it.

My main sticking point right now is that it seems that I am missing some critical bit of information for how to update Module to include the prototype for the interface I am trying to extend.

The abstract base class is declared with this:

struct JsPredictInterface
{
explicit JsPredictInterface(const std::string& modelUri);

virtual ~JsPredictInterface();
virtual tz::Tenzor predict(const tz::Tenzor& input) = 0;

std::string mModelUri;
};

(The method predict and in particular its use of tz::Tenzor must still be implemented, and is shown only to illustrate my intent. I have confirmed that if I comment out all code related to the predict interface that the problem below still occurs.)

The bindings for it are declared with this:

struct JsPredictWrapper : public emscripten::wrapper<JsPredictInterface>
{
EMSCRIPTEN_WRAPPER(JsPredictWrapper);

tz::Tenzor predict(const tz::Tenzor& input)
{
return call<tz::Tenzor>("predict", input);
}
};

EMSCRIPTEN_BINDINGS(JsPredictInterface) {
emscripten::class_<JsPredictInterface>("JsPredictInterface")
.function("predict", &JsPredictInterface::predict, emscripten::pure_virtual())
.allow_subclass<JsPredictWrapper>("JsPredictWrapper", emscripten::constructor<std::string>())
;
}

I attempt to extend the interface with this code:

EM_JS(void, createImpl, (emscripten::val context, std::string modelPath), {
const assert = require('assert');
assert.ok(Module);
assert.ok(Module.JsPredictInterface);
const PredictorImpl = Module.JsPredictInterface.extend("JsPredictInterface", {
...
});

const impl = new PredictorImpl(modelPath);

context = {...context, impl};
});

When the function createImpl is called it  fails with the assertion assert.ok(Module.JsPredictInterface).

What am I likely missing to ensure that Module includes the JsPredictInterface object? Do I have to write the code to update Module? So far my reading of the documentation has led me to believe that this would be taking care of by the EMSCRIPTEN_BINDINGS implementation.

Thanks.


Reply all
Reply to author
Forward
0 new messages