Skip to content

An efficient way to pass TypedArrays with Embind #5519

@zandaqo

Description

@zandaqo

I'm trying to find a way to efficiently convert TypedArrays into std::vector (and vice versa) using Embind. I'm currently using vecFromJSArray but that's inefficient when compared to passing a pointer and directly accessing Emscripten heap as described here. However, playing with the heap is a bit cumbersome on the JS side, so I wonder if there is a better way to convert typed arrays. Maybe we can get a pointer to the begging of the TypedArray's buffer and use the length to find the rest and build a vector, something like this:

using namespace emscripten;

std::vector<double> vecFromJSTypedArray(const val& a) {
  return std::vector<double>(a["buffer"].as<?>(), a["buffer"].as<?>() + a["length"].as<unsigned>());
}

Not sure how to do it though, I'd appreciate any pointers (no pun intended) here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions