A 32-bit ISA with a set of Bytecode designed for a Stack-Based Virtual Machine. We can migrating low level software written in Rust, C, COBOL in a web browser at near native performance. It consists of web assembly Bytecode compiled to WASM binary.

Creating WASM Binaries

  1. Use Web Assembly Text
  2. Use Emscripten to convert C/C++ program to WASM Module
  3. Use AssemblyScript to convert TypeScript into WASM Module

WASM In the Browser

  • WASM must be instantiated by a javascript handler, as it cannot talk to the DOM directly.

Boilerplates

JS Handler

// providing functions for WASM to callback
let imports = {
	math : {
		callback : x => console.log("result is", x);
	}
}
let module = await WebAssembly.instantiateStreaming(fetch('main.wasm'), imports)
// using exports of WASM
let x = module.instance.exports.add(5,10)

WAT

(module
	(import "math" "callback" (func $callback))	
	(export "add" (func $add))
	(export "subtract" (func $subtract))
	(func $add (param $a i32) (param $b i32) (result i32)
		local.get $a
		local.get $b
		i32.add
		local.set $sum ;; set result to sum
		local.get $sum ;; put sum onto stack
		call $callback ;; call callback with $sum
		local.get $sum ;; put sum onto stack again
	)
	(func $subtract (param $a i32) (param $b i32) (result i32)
		local.get $a
		local.get $b
		i32.sub
	)
)

Compiling WAT

wat2wasm myfile.wat

Concepts

Instructions

Guides