Busy Developer's Guide to WebAssembly

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Before we begin

Quick note:

Objectives

What are we going to do today?

WebAssembly: Overview

Assembly language... for the Web?!?

WebAssembly: Overview

WebAssembly is...

WebAssembly: Overview

But... why?

WebAssembly: Overview

Goals of WASM:

WebAssembly: Overview

Use cases

WebAssembly: A History

How did we get here?

WebAssembly: A History

In the beginning (1997), browsers ran Javascript

WebAssembly: A History

In the beginning (1997), browsers ran Javascript

... and we all hated it

WebAssembly: A History

In the beginning (1997), browsers ran Javascript

... and we all hated it

WebAssembly: A History

Around the same time (1997), browsers also ran Java applets

WebAssembly: A History

Emphasis shifted entirely to the server

WebAssembly: A History

2006: jQuery begins to change minds

WebAssembly: A History

2011: Erik Meijer suggests "Javascript is the assembly language of the browser"

WebAssembly: A History

2013: What if we had a JS library that mimicked a CPU?

WebAssembly: A History

2015: WASM Community Group started

WebAssembly: A History

2018: WASM 1.0 ships

Getting Started

Hello, WebAssembly

Getting Started

WebAssembly is generally a compiler target format

Getting Started

Pick a source language to use

Getting Started

Pick a runtime engine

Getting Started

WASM-level tools

Getting Started

Testing: Add some numbers!

Getting Started

adder.wat: Export an add() function

(module
    (func $add (param $a i32) (param $b i32) (result i64)
        (i64.add
            (i64.extend_i32_s (local.get $a))
            (i64.extend_i32_s (local.get $b))
        )
    )
    (export "add" (func $add))
)

Getting Started

main.wat: Call the exported add() function

(module
    (import "adder" "add" (func $add (param i32 i32) (result i64)))
    (func $start
        ;; Call the imported "add" function with 2 and 4
        i32.const 2
        i32.const 4
        call $add
        drop ;; Discard the result
    )
    (start $start)
    (export "_start" (func $start))
)

Getting Started

Building

$ wat2wasm app.wat -o app.wasm

Running

$ wasm-interp app.wasm

WebAssembly Languages

Languages that can emit a WASM artifact/target

WebAssembly Languages

Groups

WebAssembly Languages

Mainstream (and wannabes)

WebAssembly Languages

Adapters

WebAssembly Languages

Academic

Web Assembly FFI

Calling into and out of the WASM environment

Web Assembly FFI

Highly dependent on engine used

Web Assembly FFI

Given: adder.wasm

(module
    (func $add (param $a i32) (param $b i32) (result i64)
        (i64.add
            (i64.extend_i32_s (local.get $a))
            (i64.extend_i32_s (local.get $b))
        )
    )
    (export "add" (func $add))
)

Web Assembly FFI

Browser, NodeJS

Web Assembly FFI

WebAssembly global object

Web Assembly FFI

Various WebAssembly.-prefixed constructors

Web Assembly FFI

Run in an HTML page: runwat.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Run WebAssembly - Check the Console</title>
</head>
<body>
    <script>
        async function loadWasm() {
            try {
                const response = await WebAssembly.instantiateStreaming(
                    fetch('main.wasm')
                );
                const { _start, add } = response.instance.exports;
                const result = add(5, 10);
                console.log("Result from Wasm:", result);
            } catch (error) {
                console.error("Error loading WebAssembly:", error);
            }
        }
        loadWasm();
    </script>
</body>
</html>

Make sure to load via HTTP: python3 -m http.server 9000

Web Assembly FFI

Run at the CLI: runwat.js

import fs from 'node:fs/promises';

// Use readFile to read contents of the "add.wasm" file
const wasmBuffer = await fs.readFile('main.wasm');

// Use the WebAssembly.instantiate method to instantiate the WebAssembly module
const wasmModule = await WebAssembly.instantiate(wasmBuffer);

// Exported function lives under instance.exports object
const { _start, add } = wasmModule.instance.exports;
_start();
const sum = add(55, 66);
console.log(sum, (typeof sum));
$ node runwat.js

Web Assembly FFI

WASI Specification

Semantics

WebAssembly interpreter/VM concepts

Semantics

WebAssembly interpeters

Semantics

WebAssembly execution

Semantics

Phase 1: Decoding

Semantics

Phase 2: Validation

Semantics

Phase 3: Execution

Glossary

A collection of terms

Glossary

Module:

Glossary

Functions:

Glossary

Table

Glossary

Linear memory

Glossary

Traps

Glossary

Embedder

WASM Bytecode

Instruction set for the WebAssembly virtual ISA

WASM Bytecode

Categories:

WASM Bytecode

Module/structure definition

WASM Bytecode

An empty module

(module)

WASM Bytecode

Module/structure definition

WASM Bytecode

Module/structure definition

WASM Bytecode

That add function again

(module
    (func $add (param $a i32) (param $b i32) (result i64)
        (i64.add
            (i64.extend_i32_s (local.get $a))
            (i64.extend_i32_s (local.get $b))
        )
    )
    (export "add" (func $add))
)

WASM Bytecode

Types

WASM Bytecode

Numeric "bases" to operations

WASM Bytecode

Numeric unary operations

WASM Bytecode

Numeric binary operations

WASM Bytecode

Variable instructions

WASM Bytecode

Memory instructions

WASM Bytecode

Control instructions

WASM Bytecode

Control instructions

WASM Tooling

An evolution in progress

WASM Tooling

WebAssembly maintains many tools

WASM Tooling

... including an IDE!

WASM Tooling

WABT tools

WASM Tooling

WABT tools

WASM Tooling

WABT tools

WASM Tooling

Binaryen

WASM Tooling

Binaryen tools

WASM Tooling

Binaryen tools

WASM Tooling

Binaryen tools

WASM Tooling

.NET

WASM Tooling

3rd-party Tooling

Summary

Wrapping up

Summary

WebAssembly...

WebAssembly Resources

Where to go to go get more

WebAssembly Resources

Web

WebAssembly Resources

Web

WebAssembly Resources

Languages

WebAssembly Resources

Runtimes

Credentials

Who is this guy?