Pase una matriz de JavaScript como argumento a una función WebAssembly

Me gustaría probar WebAssembly para hacer algunos cálculos de matriz complejos.

Así que he escrito una función simple de C ++ agregando dosint matrices que contienen 3 elementos cada una:

// hello.cpp
extern "C" {

void array_add(int * summed, int* a, int* b) {
  for (int i=0; i < 3; i++) {
    summed[i] = a[i] + b[i];
  }
}

}

Y compiló esto con:

emcc hello.cpp -s WASM=1 -s "MODULARIZE=1" -s "EXPORT_NAME='HELLO'" -s "BINARYEN_METHOD='native-wasm'" -s "EXPORTED_FUNCTIONS=['_array_add']" -o build/hello.js

Lo que genera entre otros, unjs y unwasm archivo. Los cargo con la siguiente página html:

<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="build/hello.js"></script>
    <script type="text/javascript">
      function reqListener () {
        // Loading wasm module
        var arrayBuffer = oReq.response
        HELLO['wasmBinary'] = arrayBuffer
        hello = HELLO({ wasmBinary: HELLO.wasmBinary })

        // Calling function
        var result = new Int32Array(3)
        var a = new Int32Array([1, 2, 3])
        var b = new Int32Array([4, 5, 2])
        hello._array_add(result, a, b)
        console.log('result', result)
      }

      var oReq = new XMLHttpRequest();
      oReq.responseType = "arraybuffer";
      oReq.addEventListener("load", reqListener);
      oReq.open("GET", "build/hello.wasm");
      oReq.send();
    </script>
  </head>
  <body>

  </body>
</html>

Pero de alguna manera, elresult matriz es siempre[0, 0, 0].

He intentado una variedad de cosas, incluida la llamada a la función conccall() (verdocumentos emscripten ) y parece que no puedo pasar una matriz como argumento de mi función compilada wasm.

Por ejemplo, con la siguiente función de C ++:

extern "C" {

int first(int * arr) {
  return arr[0];
}

}

El resultado cuando se llama en JavaScript es un entero aleatorio, en lugar del valor esperado de la matriz que pasé como argumento.

¿Qué me estoy perdiendo?

nótese bien : No sé casi nada sobre C ++, así que disculpen si esta es una pregunta para principiantes relacionada con mi ignorancia de C ++ ...

Respuestas a la pregunta(2)

Su respuesta a la pregunta