user12658460
user12658460

Reputation:

Running C++ Wasm In Node.js

I read this post about running webassembly in node.js. When i followed the instructions step by step, it worked. I wanted to replicate this in c++ instead of c, and when i started building the wasm with

em++ -O2 test.cpp -s WASM=1 -s SIDE_MODULE=1 -o test.wasm

and ran the node, it didnt run.

my node looks like this:

function main(cpp) {
  console.log(cpp.add(10, 29)); //even when i try cpp._add(10, 29) it doesn't work
}

WebAssembly.instantiate(new Uint8Array(fs.readFileSync('./test.wasm')), {
  env: {
    memoryBase: 0,
    tableBase: 0,
    memory: new WebAssembly.Memory({
      initial: 256
    }),
    table: new WebAssembly.Table({
      initial: 0,
      element: 'anyfunc'
    })
  }
})
.then(result => {
  main(result.instance.exports)
})
.catch(e => console.log(e));

My C++ code looks exactly like what the other post showed.

Upvotes: 1

Views: 1022

Answers (1)

user12658460
user12658460

Reputation:

Turns out that this was a simple mistake. In C the function name was what I declared in the file, while in c++ it turned into a string of characters (in my case: _Z3addii). When i did the same thing except replaced cpp.add with cpp._Z3addii it worked.

Upvotes: 2

Related Questions