Created
May 29, 2019 18:44
-
-
Save alinz/b7e3cf6101dd2b195decaea12d11a2a4 to your computer and use it in GitHub Desktop.
WebAssembly on Node.js
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 { readFileSync } = require("fs"); | |
async function instantiate(source, config) { | |
const compiled = await WebAssembly.compile(source); | |
return new WebAssembly.Instance(compiled, config); | |
} | |
const config = { | |
env: { | |
__memory_base: 0, | |
__table_base: 0, | |
memory: new WebAssembly.Memory({ | |
initial: 256, | |
maximum: 256 | |
}), | |
table: new WebAssembly.Table({ | |
initial: 0, | |
element: "anyfunc" | |
}) | |
} | |
}; | |
async function main() { | |
const source = readFileSync("./test.wasm"); | |
const instance = await instantiate(source, config); | |
console.log(instance.exports._add(9, 9)); | |
console.log(instance.exports._add(9, 19)); | |
console.log(instance.exports._add(9, 29)); | |
console.log(instance.exports._add(9, 39)); | |
} | |
main(); |
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
build: | |
emcc test.c -O3 -s WASM=1 -s EXPORTED_FUNCTIONS='["_add"]' -s SIDE_MODULE=1 -o test.wasm | |
run: | |
node index.js |
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 a, int b) | |
{ | |
return a + b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment