Passing a JS ArrayBuffer or TypedArray to Emscripten w/o Copying

I have a very large ArrayBuffer (or TypedArray) in JavaScript that I want to pass to an emscriptened function. I’d like to pass the raw bytes without incurring a copy.

If my C/C++ function takes an std::string as in:

void processBuffer(std::string const& buffer)

I can get the data, but IIUC, the conversion to std::string will incur a copy of the buffer.

Is there a way to pass the raw buffer without a copy?
My access is strictly read-only.

I tried:

void processBuffer(const char* str, size_t size);

with setting allow_raw_pointers() in the EMSCRIPTEN_BINDINGS, but this does not seem to work.
What am I missing?

Answering myself.
As it currently stands, there is no way to allow the emscriptened C/C++ code to access JS allocated memory buffers.

That being said, buffers allocated via Module._malloc() can be passed “by pointer” when using the C API.

Embinding will add additional copying into the C++ types.

For more info see this thread on the emscripten mailing list.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Is store.dispatch in Redux synchronous or asynchronous

Similar Posts