Advertisement
quantumech

Untitled

Apr 23rd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Module.onRuntimeInitialized = function()
  2. {
  3.     // Create new typed array initialized to [1,2,3,40000,5]
  4.     // You can also use Float32Array, or any other TypeArray
  5.     // Keep in mind that you need to use the correctly sized TypeArray
  6.     // To align with the size of the C datatype of what you are setting
  7.     let data = new Int32Array([1,2,3,4,5])
  8.  
  9.     // Allocate block of memory for array and get address of the block
  10.     let ptr = Module._malloc(data.BYTES_PER_ELEMENT * data.length)
  11.  
  12.     // Set the block of memory to the int array
  13.     // You NEED to use 'new Uint8Array(data.buffer)' to wrap 'data'
  14.     // because you are setting data in HEAPU8 which requires a view
  15.     // to an unsigned byte-sized int array
  16.     //
  17.     // So if you used HEAP8 instead of HEAPU8, you would use
  18.     // 'new Int8Array' instead
  19.     Module.HEAPU8.set(new Uint8Array(data.buffer), ptr);
  20.  
  21.     // Call sum_arr() with pointer to array
  22.     // Should print '1 2 3 4 5' and return the sum of all the numbers in 'data'
  23.     Module.cwrap('sum_arr', 'number', ['number', 'number'])(ptr, 5)
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement