1

I got this code from stackoverflow link:

How to use WebAssembly from node.js?

Create a test.c file:

int add(int a, int b) {
  return a + b;
}  

Build the standalone .wasm file

emcc test.c -O2 -s WASM=1 -s SIDE_MODULE=1 -o test.wasm  

Use the .wasm file in Node.js app:

const util = require('util');
const fs = require('fs');
var source = fs.readFileSync('./test.wasm');
const env = {
    memoryBase: 0,
    tableBase: 0,
    memory: new WebAssembly.Memory({
      initial: 256
    }),
    table: new WebAssembly.Table({
      initial: 0,
      element: 'anyfunc'
    })
  }

var typedArray = new Uint8Array(source);

WebAssembly.instantiate(typedArray, {
  env: env
}).then(result => {
  console.log(util.inspect(result, true, 0));
  console.log(result.instance.exports._add(9, 9));
}).catch(e => {
  // error caught
  console.log(e);
});  

Receiving error when starting Node.js server with

node Node.js  

LinkError: WebAssembly Instantiation: Import #3 module="env" function="abort" error: function import requires a callable

PATurmel
  • 77
  • 1
  • 5

1 Answers1

2

Loading an emscripten-generated SIDE_MODULE using your own custom code is going to be tricky, because the module you built with emscripten expects to be hosts by emscripten-generated JavaScript. If order to make it work you will need to provide a compatible environment. If might be easier to drop the SIDE_MODULE and have emscripten output the js module for you with -o <something>.js which will generate both wasm file and a js file that loads it.

If you do want to continue to hand-write your loading code it looks like the first thing you need to do is private and implementation of the abort function in the env you pass to instantiate. There will most likely be other functions you need too, and this interface could change as emscripten evolves.

sbc100
  • 2,619
  • 14
  • 11