return 'array' to js function - emscripten binding

1,581 views
Skip to first unread message

naky...@gmail.com

unread,
Jun 4, 2018, 10:46:21 PM6/4/18
to emscripten-discuss

Hello All,

I'd like to use 'array' as return value (c++ side) and bind this function with EMSCRIPTEN_BINDINGS.

In main.cpp file, i'd like to use function like below,

uint32_t* get_frame_buffer() {
  printf("return frame buffer!\n");
  //return test_buffer;

And i'd like to use this function  at js side, i'd like to get buffer value at js side.

I tried like below but it's not working.
  //emscripten::function("get_frame_buffer", &get_frame_buffer, allow_raw_pointer);

Could you somebody help me?

Thanks in advance:)

Brion Vibber

unread,
Jun 5, 2018, 5:12:26 AM6/5/18
to emscripten Mailing List
On Mon, Jun 4, 2018 at 7:46 PM <naky...@gmail.com> wrote:
I'd like to use 'array' as return value (c++ side) and bind this function with EMSCRIPTEN_BINDINGS.

In main.cpp file, i'd like to use function like below,

uint32_t* get_frame_buffer() {
  printf("return frame buffer!\n");
  //return test_buffer;

And i'd like to use this function  at js side, i'd like to get buffer value at js side.

I tried like below but it's not working.
  //emscripten::function("get_frame_buffer", &get_frame_buffer, allow_raw_pointer);

Embind's raw pointer handling is aimed at passing objects by reference, and doesn't really apply to primitive types and arrays like this. But good news! There's two ways to do what you want without much trouble. :)

One is to explicitly return an integer type such as size_t from your bound function; cast the pointer to a size_t and let it pass through to JS as a number.

Another way, which may produce more comfortable JS bindings, is to have the binding return an emscripten::val and instead of just returning an offset, return a Uint32Array view into the heap:

Good luck!

-- brion
Reply all
Reply to author
Forward
0 new messages