This is a simple example of compiling and running the WebAssembly textual format (wast) in Node.js. This does not cover compiling from a higher level language to WebAssembly (for that you probably want Emscripten). But if all you want to do is play around directly with the wast textual format, this should get you going quickly.
-
Install node 7.x (instructions). I've tested this with node 7.7.1
-
You need a wast to wasm compiler. There are two fairly straightfoward options: wabt (wabbit) and the official WebAssembly interpreter.
-
Option #1: wabt
-
You'll need a standard gcc toolchain to build wabt
-
Download and build a branch of wabt (binary_0xc) supporting the same version of WebAssembly as node 7 (version 12):
-
-
git clone --recursive https://github.com/WebAssembly/wabt/
cd wabt
git checkout binary_0xc
make gcc-release-
Option #2: spec interpreter
-
You'll need an Ocaml compiler version 4.02 or later (ocaml-nox on Ubuntu)
-
Download and build a branch of the spec interpreter (binary-0xc) supporting the same version of WebAssembly as node 7 (version 12):
-
git clone https://github.com/WebAssembly/spec
cd spec/interpreter
git checkout binary-0xc
make opt Compile a wast to wasm (binary module):
- Option #1: using wast2wasm from wabt:
wabt/out/gcc/Release/wast2wasm addTwo.wast -o addTwo.wasm- Option #2: using spec interpreter:
spec/interpreter/wasm.opt addTwo.wast -o addTwo.wasmRun an exported function in the wasm binary module using the
runasm.js script:
./runwasm.js addTwo.wasm addTwo 2 3