Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
Adi Shavit Avatar asked Feb 24 '17 09:02

Adi Shavit


1 Answers

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.

like image 105
Adi Shavit Avatar answered Nov 09 '22 05:11

Adi Shavit