I would like to create a std::vector (specifically, a std::vector<string>) in C++, and then pass it (or rather, a pointer to it) to Javascript, in order to be able to access its data/functions from directly-written Javascript.
So far I can get the integer pointer:
vector<string> myVector;
myVector.push_back("First item");
myVector.push_back("Second item");
EM_ASM_ARGS({
// Prints out an integer value of the pointer,
// but I would like to access the object members/data
// of myVector
console.log($0);
}, &myVector);
I have found information at http://kripken.github.io/emscripten-site/docs/api_reference/bind.h.html#register_vector__cCP and http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#built-in-type-conversions about the register_vector function
#include <emscripten/bind.h>
EMSCRIPTEN_BINDINGS(Wrappers) {
register_vector<std::string>("VectorString");
};
which can be used to create a new vector from the Javascript world:
var myVector = new Module.VectorString();
but I can't figure out how to use this to access vector that already exists in the C++ world.
If I try to use Module.Runtime.dynCall('v', $0, []); on the pointer, as in https://stackoverflow.com/a/29319440/1319998 when passing function pointers, then I get an error:
Invalid function pointer '380' called with signature 'v'
I have tried a few different combinations of signatures, and they all seem to fail (Which I'm not really surprised about: it's not really a function pointer I'm calling, but a pointer to an instance of an object)