WASM and Rust

By Casey Allred


Who am i?

  • Wrote ~20k LOC targeting WASM in Rust
  • UVU MCS project ~12k LOC currently at https://programwith.us
  • Work for LiveView Technology on the Web Development team
  • Have a wife, three beautiful little girls, and love playing Minecraft


  • What is WASM?
  • WAT
  • WASM Bindgen
  • Rust frontend frameworks

What is WASM?

“WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.”


- WebAssembly Working Group (https://www.w3.org/wasm/)


WAT - WebAssembly Text format

  • S-Expressions
  • Types
  • Functions
  • Local/Global
  • Imports/Exports
  • Memory
  • Table

WAT - S-Expressions


“Lisp like” expressions where everything is surrounded in ()


Most simple WASM module is:

which is a module that has nothing and does nothing.

WAT - Types

WebAssembly has the following value types:

  • i32: 32-bit integer
  • i64: 64-bit integer
  • f32: 32-bit floating point
  • f64: 64-bit floating point


The value types i32 and i64 are not inherently signed or unsigned. The interpretation of these types is determined by individual operators.

WAT - Types

  • Each parameter and local variable has exactly one value type.
  • Function signatures consist of a sequence of zero or more parameter types and a sequence of zero or more return types
    • In the MVP, a function can have at most one return type.

WAT - Functions

  • The signature declares what the function takes (parameters) and returns (return values).
  • The locals are like vars in JavaScript, but with explicit types declared.
  • The body is just a linear list of low-level instructions.
( func <signature> <locals> <body> )

WAT - Functions

(func $add (param $lhs i32) (param $rhs i32) (result i32)
    get_local $lhs
    get_local $rhs
(func $add (param i32) (param i32) (result i32) 
    get_local 0
    get_local 1

WAT - Local/Global

  (global $glob (mut i32) (i32.const 100))
  (func $add (param $lhs i32) (param $rhs i32) (result i32) (local $loc i32) 
    i32.const 10
    set_local $loc
    get_local $lhs
    get_local $rhs
    get_local $loc
    get_global $glob
  (export "add" (func $add))

WAT - Imports/Exports

  (global $glob (import "js" "global")  i32)
  (import "console" "log" (func $log (param i32)))
  (func (export "add") (param $lhs i32) (param $rhs i32) (result i32)
    i32.const 100
    call $log
    get_local $lhs
    get_local $rhs
var importObject = {
  console: {
    log: function(arg) {

WebAssembly.instantiateStreaming(fetch('logger.wasm'), importObject)
  .then(obj => {

WAT - Memory

  • Memory is just a large array of bytes that can grow over time.
  • WebAssembly contains instructions for reading and writing from linear memory.
    • i32.load
    • i32.store
    • ...

WAT - Memory

  (import "console" "log" (func $log (param i32 i32)))
  (import "js" "mem" (memory 1))
  (data (i32.const 0) "Hi")
  (func (export "writeHi")
    i32.const 0  ;; pass offset 0 to log
    i32.const 2  ;; pass length 2 to log
    call $log))
function consoleLogString(offset, length) {
  var bytes = new Uint8Array(memory.buffer, offset, length);
  var string = new TextDecoder('utf8').decode(bytes);

var memory = new WebAssembly.Memory({initial:1});

var importObject = { console: { log: consoleLogString }, js: { mem: memory } };

WebAssembly.instantiateStreaming(fetch('logger2.wasm'), importObject)
  .then(obj => {

WAT - Table

  (table 2 funcref)
  (func $f1 (result i32)
    i32.const 42)
  (func $f2 (result i32)
    i32.const 13)
  (elem (i32.const 0) $f1 $f2)
  (type $return_i32 (func (result i32)))
  (func (export "callByIndex") (param $i i32) (result i32)
    local.get $i
    call_indirect (type $return_i32))
  .then(obj => {
    // returns 42
    // returns 13
    // returns an error, because there is no index position 2 in the table


Let's get rust to do all that for us!

Demo - webassembly.studio

Useful Resources

  • Programming WebAssembly with Rust
    • https://pragprog.com/book/khrust/programming-webassembly-with-rust
  • crates.io
    • web-sys
    • js-sys
    • wasm-bindgen

Rust Frameworks

WASM and Rust

By sbditto85

WASM and Rust

  • 508