nbind is a replacement for embind because otherwise it seemed very difficult or impossible to:
- Support also targeting Node.js and Electron addons
- Design C++ APIs without constraints (using pointers and references etc. as normal)
- Pass objects by value so they belong to an appropriate class in both C++ and JavaScript
Since everything had to be redesigned anyway for the above reasons, it was also possible to:
- Autogenerate TypeScript definitions
- Avoid having to register vectors and smart pointers to use them
- Avoid generating JavaScript dispatcher functions when argument and return values are numeric
- Support multiple inheritance
- Support passing 64-bit integers (converting to JavaScript double or using any JavaScript bignum library)
- Write internal code in ES6 style TypeScript instead of ES5
- Write all internal tests using plain JavaScript and set up Travis CI
- Use more compact syntax:
embind:
EMSCRIPTEN_BINDINGS(my_class_example) {
class_<MyClass>("MyClass")
.constructor<int, std::string>()
.function("incrementX", &MyClass::incrementX)
.property("x", &MyClass::getX, &MyClass::setX)
.class_function("getStringFromInstance", &MyClass::getStringFromInstance)
;
}
nbind:
NBIND_CLASS(MyClass) {
construct<int, std::string>();
method(incrementX);
getset(getX, setX);
method(getStringFromInstance);
}
Other future improvements are also possible but not sure about when there's time:
- Method overloading based on argument types (multiple inheritance makes this a bit more complicated)
- Support compiling to native code on mobile (React Native, Weex...)
- Precompile a single native Node.js addon for all versions Node.js and Electron on the same platform
Again the latter two are out of scope or impossible with embind.