Skip to content

Instantly share code, notes, and snippets.

@adishavit
Created February 26, 2017 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adishavit/7cd50b3c202c9e8b4ad9518b208727d8 to your computer and use it in GitHub Desktop.
Save adishavit/7cd50b3c202c9e8b4ad9518b208727d8 to your computer and use it in GitHub Desktop.
Copy JS Int32Array to Emscripten heap for passing as C array
function _arrayToHeap(typedArray){
var numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;
var ptr = Module._malloc(numBytes);
var ptr_i32 = ptr >> 2; // divide byte offset by 4 to account for 4-byte int pointer
var heapBytes = Module.HEAP32.subarray(ptr_i32, ptr_i32 + typedArray.length);
heapBytes.set(typedArray);
return heapBytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment