Wasm Reference
  • Welcome
  • Nodejs WAT Compiler
  • Types
    • Functions
    • Globals
    • Linear Memory
    • Tables
    • Numbers
    • Text
  • Structures
    • IF
    • LOOP
  • Algorithms
    • Hello World
    • Estimate Pi
  • Maths Helper Algorithms
    • Pseudorandom Number Generator
    • Factorial
    • Dist
    • Map
    • Fract
    • RemF or ModF
    • Round
    • Clamp
    • Mix
    • Pow
    • Approx Sin
    • Approx Cos
    • Approx Tan
    • ToRad and ToDeg
Powered by GitBook
On this page
  • Setup
  • Code (index.js)

Was this helpful?

Nodejs WAT Compiler

PreviousWelcomeNextFunctions

Last updated 5 years ago

Was this helpful?

This code will take a wat file (main.wat), compile it to wasm, then instantiate the wasm. This code requires the package .

Setup

$> npm init
$> npm install wabt

Code (index.js)

var { readFileSync } = require("fs");
var wabt = require("wabt")();

var inputWat = "main.wat";
var wasmModule = wabt.parseWat(inputWat, readFileSync(inputWat, "utf8"));
var importObject = { };

WebAssembly.instantiate(wasmModule.toBinary({}).buffer, importObject).then(function(res) {
    //run functions here
  });
WABT