Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allocate memory with typed array
function TypedArrayToHeap(arr) {
const { name, BYTES_PER_ELEMENT } = arr.constructor;
const prefix = name.charAt(0).replace(/I|B/, '');
const heap = Module[`HEAP${prefix}${BYTES_PER_ELEMENT << 3}`];
if (!heap) {
throw new Error(`Unknow typed array ${heap}`);
}
const ptr = Module._malloc(arr.length * BYTES_PER_ELEMENT);
heap.set(arr, ptr / BYTES_PER_ELEMENT);
return ptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment