Snapchat / djinni

A tool for generating cross-language type declarations and interface bindings. Djinni's new home is in the Snapchat org.
Apache License 2.0
173 stars 50 forks source link

[Wasm] Emscripten typed_memory_view #103

Open bkotsopoulossc opened 2 years ago

bkotsopoulossc commented 2 years ago

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()));
li-feng-sc commented 1 year ago

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.