WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
Use WebAssembly in JavaScript
compile c/c++/rust code
source code
1
2
3
intadd(inta,intb){returna+b;}
emcc
1
emcc src/add.c -s STANDALONE_WASM=1 -o add.wasm
fetch
use fetch to load the wasm file
1
2
3
4
5
6
fetch('add.wasm').then((response)=>response.arrayBuffer()).then((bytes)=>WebAssembly.instantiate(bytes,importObject)).then((results)=>{// Do something with the compiled results!
});
initiate
call WebAssembly.instantiate
new api: WebAssembly.compileStream (not supported in nodejs 12.x)