djinni
djinni copied to clipboard
[Wasm] Emscripten typed_memory_view
We have code in DataView_wasm.hpp and djinni_wasm.cpp that creates a JS Uint8Array view of memory on the wasm heap:
static auto uint8ArrayClass = em::val::global("Uint8Array");
unsigned addr = reinterpret_cast<unsigned>(buffer.data());
unsigned size = static_cast<unsigned>(buffer.size());
em::val uint8ArrayObj = uint8ArrayClass.new_(getWasmMemoryBuffer(), addr, size);
return uint8ArrayObj;
The Uint8Array view is used regardless of the data type in C++ - whether it is std::vector<uint8_t>
or std::string
.
Emscripten offers a dedicated API for this - typed_memory_view
, should we be using it? Is it more reliable or performant?
return em::val view(em::typed_memory_view(buffer.size(), buffer.data()));
I think that does the same thing: https://github.com/emscripten-core/emscripten/blob/f6fa2ec09b650838bdb8cda689f28e1ce0f15788/src/embind/embind.js#L911
Regarding the "typed" part, we won't get element type unless we turn the dataview
type into a generic one.