Return vector of struct from C++ to Javascript

1,753 views
Skip to first unread message

Sagar B

unread,
Sep 21, 2018, 7:21:48 AM9/21/18
to emscripten-discuss
Hi,

I am new to Webassembly. I want to return a vector of struct from C++ to javascript.

C++ side:
#include <emscripten/bind.h>
#include <string>
#include <vector>

using namespace emscripten;

struct employee {
   std::string name;
   int id;
   int salary;
};

std::vector<employee> returnVectorData () {
   std::vector<
employee
> v;
   v.push_back({"A", 1, 100000});
   v.push_back({"B", 2, 100000});
    v.push_back({"C", 3, 100000});
   return v;
}

EMSCRIPTEN_BINDINGS(module) {
   function("returnVectorData", &returnVectorData);
   register_vector<employee>("vector<employee>");
}


and want to do something like this on Javascript side:

var retVector = Module.returnVectorData();
var vectorSize = retVector.size();

for (var i = 0; i < vectorSize; i++) {
 console.log("value: ", retVector.get(i)); // access values from C++ and do something
}

I tried with int and string vectors, it runs perfectly but not working for vector of struct.
Please guide on how to do this.
Please forgive me if this seems to be very simple question.

Woof

unread,
Sep 23, 2018, 7:52:52 AM9/23/18
to emscripten-discuss
I am new to Webassembly. I want to return a vector of struct from C++ to javascript.

We build similar using emscripten::val before sending them to JS:

val entry = val::object();
entry.set(string("name"), string("A"));
entry.set(string("id"), 1);
entry.set(string("salary"), 100000);

Then getting the result of that from JS I see:

Object { name: "A", id: 1, salary: 100000 }

Hope that helps!

Brion Vibber

unread,
Sep 23, 2018, 12:21:40 PM9/23/18
to emscripten Mailing List
It looks like you've defined a binding for the vector type but not for the actual employee struct; this will prevent embind from being able to expose the contained objects.

Most likely you want to define a "value object" binding for the employee struct -- see https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html#value-types

-- brion

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sagar B

unread,
Sep 24, 2018, 2:30:06 AM9/24/18
to emscripten-discuss
Thanks for the help!

I was wondering that "if my return vector/array of objects contain more than 1000 elements, does embind would affect the performance?"
I have a requirement where such huge number of return objects is a common scenario.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Message has been deleted

Sagar B

unread,
Sep 24, 2018, 6:38:30 AM9/24/18
to emscripten-discuss
This might feel like a stupid question.
Now I want to send an array of complex object to C++ side.
Consider the object as:
struct two {
    float values[]; // length can be dynamic
};

struct one {
    struct two[]; // length can be dynamic
    int someValue;
};

void acceptData(one[] arr){ // again here the length is not fixed
    // process the data.
}

This acceptData(one[] arr)  function will be called from JS side.
I searched on how to send array of struct using Embind, but couldn't find any possible solution.
How to achieve this with embind?

On Sunday, September 23, 2018 at 9:51:40 PM UTC+5:30, Brion Vibber wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages