I ran into this pretty quickly when working with wasm. My current approach is something like this, where the rust code returns a buffer via get_result_buffer:
var $buffer_ptr = exports.get_result_buffer(BUFFER_SIZE);
var $buffer_view = new Int32Array(exports.memory.buffer, $buffer_ptr, BUFFER_SIZE);
function get_buffer_view() {
if ($buffer_view.byteLength === 0) {
$buffer_view = new Int32Array(exports.memory.buffer, $buffer_ptr, BUFFER_SIZE);
}
return $buffer_view;
}
Constantly checking the state of this in case the buffers have become detached feels like pointless boilerplate -- is there any discussion of providing js types that handle this automatically? Is there a better approach to what I'm doing above?
It is returning a pointer to what was the vector structure (no ABI so just raw memory outside rust.) Rather than pointer to first item (u8) that's allocated on the heap.
Not something I have looked at recently. I know the cargo-web builder injects a callback to handle the growth; not sure if it would work once multi-threading gets added.