Skip to content

Instantly share code, notes, and snippets.

@yusukebe
Last active August 12, 2022 22:32
Show Gist options
  • Save yusukebe/9a502ab6ee67ef99c17699b4f53e9064 to your computer and use it in GitHub Desktop.
Save yusukebe/9a502ab6ee67ef99c17699b4f53e9064 to your computer and use it in GitHub Desktop.
Run `.wasm` from Node.js script
int add(int x, int y){
return x + y * 2;
}
const { promisify } = require('util')
const fs = require('fs')
const path = require('path')
const readFile = promisify(fs.readFile)
readFile(path.join(__dirname, './add.wasm'))
.then(WebAssembly.compile)
.then((wasm) => new WebAssembly.Instance(wasm))
.then((instance) => {
console.log(instance.exports.add(1, 2))
})
{
"name": "simple",
"version": "0.0.1",
"scripts": {
"build": "emcc ./add.c -o ./add.wasm --no-entry -s EXPORTED_FUNCTIONS=\"['_add']\"",
"start": "node ./index.js"
},
"main": "index.js",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment