Last active
August 17, 2020 08:29
-
-
Save ragingwind/be144f32c550cc46e7028819cca9365b to your computer and use it in GitHub Desktop.
Allocate memory with typed array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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