Skip to content

Instantly share code, notes, and snippets.

@jcttrll
Created July 3, 2018 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcttrll/959c84bba82e36ebdb6801955d66c7a8 to your computer and use it in GitHub Desktop.
Save jcttrll/959c84bba82e36ebdb6801955d66c7a8 to your computer and use it in GitHub Desktop.
Trivial example of running assembled WebAssembly text in Node
(module
(func $add (param $a i32) (param $b i32) (result i32)
get_local $a
get_local $b
i32.add
)
(export "add" (func $add))
)
var fs = require("fs")
var binary = fs.readFileSync("blah.wasm")
var module = new WebAssembly.Module(binary)
var instance = new WebAssembly.Instance(module)
console.log(instance.exports.add(23, 45))
@jcttrll
Copy link
Author

jcttrll commented Jul 3, 2018

To Run

Clone the WABT repository and build it:

git clone https://github.com/WebAssembly/wabt.git
cd wabt
git submodule update --init
mkdir build
cd build
cmake ..
make

Ensure Node.js 8 (or later) is available on your path:

node --version

Download the files in the Gist into the build directory and assemble blah.wast:

./wat2wasm blah.wast

Run the JS loader in Node:

node run-wasm.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment