WebAssembly

Concepts of Programming Languages

Sebastian Macke

Rosenheim Technical University

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
- 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

2

What is WebAssembly?

3

JavaScript vs. WebAssembly?

4

Design

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

Design

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

Stackmachine in Detail

7

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()
8

Supported languages in 2022

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

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()
10

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>
11

Exercise

12

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)
    }
}
13

Applications

14

Applications

15

Outside the browser

16

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
}
17

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>
18

What is WASI?

Question:

You have an Hello world executable.
What "Symbols" do you have export and import to execute the executable?

19

WASI Demo

20

Exercise

21

Future - The Skill Tree

22

Future - MVP

23

Future

24

Future

25

Future

26

Future

27

Future

28

Future

29

Future

30

Links

31

Links

32

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.)