2

I know it is possible to pass arrays of integer to web assembly using something like this :

const bin = ...; // WebAssembly binary, I assume below that it imports a memory from module "imports", field "memory".
const module = new WebAssembly.Module(bin);
const memory = new WebAssembly.Memory({ initial: 2 }); // Size is in pages.
const instance = new WebAssembly.Instance(module, { imports: { memory: memory } });
const arrayBuffer = memory.buffer;
const buffer = new Uint8Array(arrayBuffer);

I have read a lot of documentation and some questions that looks like what I was looking for :

And yet none of those answered my question.

here is a small example of AssemblyScript that describe the kind of function I would like to use :

class Dummy {
  constructor()
}

export function getDummy(): Dummy {
   return new Dummy();
}

export function workWithDummy(dummies: Dummy[] = []) {
  // do something
}

and in the javaScript code :

const fs = require('fs');
const {resolve} = require('../utils/utils.js');
const env = {
    abort: (message, filename, line, column) => {
        throw new Error(`${message} in ${filename} at ${line}:${column}`);
    }
};

module.exports = fs.promises.readFile(resolve('parser.wasm')).then(buffer => {
    return WebAssembly.instantiate(buffer, {env: env}).then(wasmModule => {
       const module = wasmModule.instance.exports;
       module.workWithDummy([module.getDummy()]); //won't work
    });
});

I am running this code in nodeJs 18.1.0

To sum up my question is : how to make this line work ? module.workWithDummy([module.getDummy()]); //won't work

Galyfray
  • 118
  • 1
  • 10

0 Answers0