I've been experimenting with Emscripten to potentially bring a C++ API to web assembly.
I've successfully been able to export individual functions and classes from my API to the JS module, but I'm not so sure how to Embind full namespaces of methods as their own JS objects.
Let's say I have NamespaceA and NamespaceB (rough example below), how can I "embind" the methods in these namespaces to their own JS objects inside the main binding?
// Example namespace A
#include "myheaderfile_a.h"
uint8_t NamespaceA::functionA(uint8_t a, uint8_t b) {
return a+b;
}
uint8_t NamespaceA::functionB(uint8_t a, uint8_t b) {
return a+b*2;
}
uint8_t NamespaceA::functionC(uint8_t a, uint8_t b) {
return b - a;
}
// Example namespace B
#include "myheaderfile_b.h"
uint8_t NamespaceB::functionA(uint8_t a, uint8_t b) {
return a+b;
}
uint8_t NamespaceB::functionB(uint8_t a, uint8_t b) {
return a+b*2;
}
uint8_t NamespaceB::functionC(uint8_t a, uint8_t b) {
return b - a;
}
I'm looking for a usage structure as follows in Javascript:
const MyModule = require("binding.js");
MyModule.addOnPostRun(() => {
console.log(MyModule);
// NAMESPACE A
MyModule.NamespaceA.functionA(6, 4);
MyModule.NamespaceA.functionB(6, 4);
MyModule.NamespaceA.functionC(6, 4);
// NAMESPACE B
MyModule.NamespaceB.functionA(6, 4);
MyModule.NamespaceB.functionB(6, 4);
MyModule.NamespaceB.functionC(6, 4);
});
How would I achieve this?
I tried exporting a value_object but it seems I'm not able to use a namespace as a type. Even then, I don't think it's right to try export a function as a field:
EMSCRIPTEN_BINDINGS()
{
value_object<NamespaceA>("NamespaceA")
.field("functionA", &NamespaceA::functionA)
.field("functionB", &NamespaceA::functionB)
.field("functionC", &NamespaceA::functionC)
};