walloc icon indicating copy to clipboard operation
walloc copied to clipboard

Passing arrays

Open neurolabusc opened this issue 4 years ago • 0 comments

Hello, The included test.js demonstrates that malloc is able to allocate and release memory, but it does not make it explicit how arrays of data can be passed between C and JavaScript. I realize that I am unfamiliar with WASM and JavaScript. However, I think many developers would benefit by having a simple demo showing how to use walloc for computation.

I have provided a simple demo here. The JavaScript writes to an array, and the C code returns the sum as well as modifying the array. This seems to do what I want. However, while it works, I am not freeing the array. It would be great if you could provide a minimal demo that illustrates best practices for walloc.

Here is my demo running:

walloc > emcc -O2 -s ALLOW_MEMORY_GROWTH -s MAXIMUM_MEMORY=4GB -s WASM=1 -I. func.c walloc.c -o funcx.js; node test.js

Instance [WebAssembly.Instance] {}
input  Uint8ClampedArray(8) [
  0, 1, 0, 1,
  0, 1, 0, 1
] ...
Sum: 3554568
result  Uint8ClampedArray(8) [
  0, 1, 2, 3,
  4, 0, 1, 2
] ...
Done

neurolabusc avatar Dec 06 '21 15:12 neurolabusc