Last active
August 12, 2022 22:32
-
-
Save yusukebe/9a502ab6ee67ef99c17699b4f53e9064 to your computer and use it in GitHub Desktop.
Run `.wasm` from Node.js script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int add(int x, int y){ | |
return x + y * 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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