WebAssembly

Concepts of Programming Languages

Sebastian Macke

Rosenheim Technical University

What is WebAssembly?

What is it?

Why it exists?

2

WebAssembly is a new type of (virtual) machine and architecture.

3

What is WebAssembly?

- 1995: JavaScript is developed within 10 days by Brendan Eich

- ... Java Applets, Flash, ActiveX, Silverlight
- ... JavaScript improvements, ...

- 2015: First presentation of WebAssembly from the W3C WebAssembly Working Group
- 2016: Google, Microsoft and Mozilla show their first implementations
- 2017: WebAssembly is offically supported by all Web Browsers
- 2019: A WebAssembly system interface is specified for portable applications outside the browser
- 2022: Docker suggests Wasm as an alternative to containers
- 2025: Webassembly 3.0 (64-Bit, Garbage Collector, ...)
- Now:  WebAssembly is constantly enhanced (https://webassembly.org/roadmap/)

After the introduction of JavaScript in the 90s, it is the second language supported directly by web browsers

4

What is WebAssembly?

5

JavaScript vs. WebAssembly?

6

Design

- small size and easy to interpret
- Efficient decoding and compilation
- One-Pass Verification
- Representation of today's CPU architecture
7

Design

- Sandboxed
- 64kB segment. Enables hardware-supported boundary check
- Size adaptable at runtime
- Memory management for high-level languages
- Export/import of functions, global variables and so-called tables
- Open and possibility for debugging
8

Stackmachine in Detail

9

Example of a stack machine

Evaluate

i64.const 3   ;; push 3 onto stack
i64.const 9   ;; push 9 onto stack
i64.add       ;; pop 3 and 9 from stack, add them, push result onto stack
i64.const 4   ;; push 4 onto stack
i64.const 6   ;; push 6 onto stack
i64.add       ;; pop 4 and 6 from stack, add them, push result onto stack
i64.mul       ;; pop 12 and 15 from stack, multiply them, push result onto stack
10

Demo in WebAssembly Text Format

add.wat

(module
  (type (;0;) (func (param i32 i32) (result i32)))
  (func (;0;) (type 0) (param i32 i32) (result i32)
    local.get 0
    local.get 1
    i32.add)
  (export "add" (func 0)))

add.js

const fs = require('fs');

async function Run() {
    let buffer = await fs.readFileSync('./add.wasm');
    let result = await WebAssembly.instantiate(buffer);
    let add = result.instance.exports.add
    console.log(add(5, 2))
}

Run()
11

Supported languages in 2025

In Production:
πŸ” C              πŸ” C++              πŸ” Go           πŸ” Rust
Stable for production:
πŸ₯ .Net           πŸ₯ AssemblyScript   πŸ₯ Brainfuck    πŸ₯ C#
πŸ₯ Clean          πŸ₯ COBOL            πŸ₯ Dart         πŸ₯ F#
πŸ₯ Forth          πŸ₯ Grain            πŸ₯ Lobster      πŸ₯ Lua
πŸ₯ Never          πŸ₯ Rego             πŸ₯ TypeScript   πŸ₯ WebAssembly
πŸ₯ Zig
Unstable:
🐣 Ada            🐣 D                🐣 Eel          🐣 Elixir
🐣 Java           🐣 JavaScript       🐣 Lisp         πŸ₯ Kotlin/Wasm
🐣 Lys            🐣 Pascal           🐣 Perl         🐣 PHP
🐣 Poetry         🐣 Python           🐣 Prolog       🐣 R
🐣 Ruby           🐣 Scheme           🐣 Scopes       🐣 Swift
🐣 Tcl
Work in Progress:
πŸ₯š Faust          πŸ₯š Co               πŸ₯š Forest       πŸ₯š Haskell
πŸ₯š Julia          πŸ₯š Kou              πŸ₯š Nim          πŸ₯š Ocaml
πŸ₯š Plorth
12

Demo in C

add.c

int add(int a, int b) {
    return a + b;
}

add.js

const fs = require('fs');

async function Run() {
    let buffer = await fs.readFileSync('./add.wasm');
    let result = await WebAssembly.instantiate(buffer);
    let add = result.instance.exports.add
    console.log(add(5, 2))
}

Run()
13

Demo in Go

package main

func main() {
    println("Hello World")
}

index.html

<html>
<body>
    <script src="wasm_exec.js"></script>
    <script>
        const go = new Go();

        async function Run() {
            let response = await fetch('lib.wasm')
            let buffer = await response.arrayBuffer();
            let result = await WebAssembly.instantiate(buffer, go.importObject);
            go.run(result.instance)
        }

        Run()
    </script>
</body>
</html>
14

Exercise

15

WebAssembly and Go Routines

func ListPrimes() {
    for n := 100000000001; ; n++ {
        if big.NewInt(int64(n)).ProbablyPrime(100000) {
            fmt.Printf("%d\n", n)
        }
        runtime.Gosched()
    }
}

func main() {
    go ListPrimes()
    for {
        fmt.Printf("Hello, WebAssembly!\n")
        time.Sleep(1 * time.Second)
    }
}
16

Applications

17

Applications

18

Outside the browser

19

Used by

20

Calls from and to Go

//go:build js
// +build js

package main

import (
    "syscall/js"
)

func add(this js.Value, input []js.Value) any {
    result := input[0].Float() + input[1].Float()
    return js.ValueOf(result)
}

func main() {

    document := js.Global().Get("document")
    p := document.Call("createElement", "p")
    p.Set("innerHTML", "Hello WASM from Go!")
    document.Get("body").Call("appendChild", p)

    // register function
    js.Global().Set("add", js.FuncOf(add))

    // prevent exit
    c := make(chan struct{}, 0)
    <-c
}
21

Calls from and to Go

index.html

        const go = new Go();

        async function Run() {
            let response = await fetch('lib.wasm')
            let buffer = await response.arrayBuffer();
            let result = await WebAssembly.instantiate(buffer, go.importObject);
            go.run(result.instance)
            document.body.innerHTML += add(41, 1);
        }

        Run()
    </script>
22

What is WASI?

Question:

1. What are the issues with virtualization such as Docker?
2. You have an Hello world executable and a binary executable, that can only be access through
exported and imported functions. What functions do you have export and import to execute the executable?

23

WASI Demo

24

Exercise

25

Future

https://github.com/WebAssembly/proposals

https://github.com/WebAssembly/WASI/blob/main/docs/Proposals.md

26

Links

27

Links

28

Thank you

Sebastian Macke

Rosenheim Technical University

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)