How to bridge a JavaScript (ragged) array and an std::vector<std::vector<T>> object?

好久不见. 提交于 2019-12-07 15:17:18

问题


In JavaScript I have a list of "lines", each of which consists of indefinite number of "points", each of which has a form of [x, y]. So it is a 3D ragged array. Now I need to pass it to my C++ code with the help from emscripten (embind). Here's declaration of the C++ function:

Eigen::MatrixXd f(const std::vector<std::vector<std::vector<double>>>& lines);

And I would like to get a list of lists ([[m11, m12],[m22, m22],...]) in JavaScript after calling f. How to write the binding code in this case (the stuff inside EMSCRIPTEN_BINDINGS, for example)?

UPDATE: I can now pass the JavaScript array to C++. The binding part is something like

typedef std::vector<double> Point;
typedef std::vector<Point> Line;
EMSCRIPTEN_BINDINGS(module) {
  register_vector<Line>("LineArray");
  register_vector<Point>("Line");
  register_vector<double>("Point");
  emscripten::function("f_wrapper", &f_wrapper);
}

where f_wrapper calls f but returns vector<vector<double>> instead of MatrixXd. Now the problem is that I can only get an empty JavaScript object after calling f_wrapper. The JavaScript is

var Module = require('./bind.js'); // the output of em++
var cppAllLines = new Module.LineArray();
// some initialization
var result = Module.f_wrapper(cppAllLines); // an empty "Line" object

Any ideas?


回答1:


When passing an embound vector from a C++ function, such as

std::vector<std::vector<double>> f_wrapper(...);

to Javascript

var result = Module.f_wrapper(...);

The result object is not a Javascript array that implements length property or array-indexed access, so it can appear "empty" if using these to access its data.

But it does expose get and size functions to access the vector:

console.log('Back from C++', result.size(), result.get(0).get(1));

(The double get is because we're returning a vector of vectors)

For completeness, looking into the prototype of the object returned, it seems to expose the following functions.

  • get
  • push_back
  • resize
  • set
  • size

Slightly inconsistently it exposes get and set functions rather than an equivalent of the C++ at function. I suspect that it's not possible for an exactly equivalent function since at returns a reference which allows it to be used as a setter, which isn't possible in Javascript.



来源:https://stackoverflow.com/questions/35494467/how-to-bridge-a-javascript-ragged-array-and-an-stdvectorstdvectort-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!