02 Webassembly

Webassembly logo

Webassembly = wasm

Browser Support?

wasm browser support

Node.js

Support since v8.0

What is wasm?

  • Bytecode for the browser
  • All major browser vendors agreed on format
  • Browsers implement VM to run wasm in

What languages can be compiled to wasm?

But what does that actually mean?

  • Compile anything to the web
  • Create npm packages out of any third party library
  • Pick the right tool for the task (e.g. math)

Compilation example

C

                
                    int factorial(int n) {
                      if (n == 0)
                        return 1;
                      else
                        return n * factorial(n - 1);
                    }
                
                

Intermediate format

                
                    get_local 0
                    i64.eqz
                    if (result i64)
                        i64.const 1
                    else
                        get_local 0
                        get_local 0
                        i64.const 1
                        i64.sub
                        call 0
                        i64.mul
                    end
                
                

wasm binary format

                
                    20 00
                    50
                    04 7E
                    42 01
                    05
                    20 00
                    20 00
                    42 01
                    7D
                    10 00
                    7E
                    0B
                
                

Text representation (s-expr)

                
                    (module
                      (import "math" "exp" (func $exp (param f64) (result f64)))
                      (func (export "doubleExp") (param $0 f64) (result f64)
                        (f64.mul
                          (call $exp
                            (get_local $0)
                          )
                          (f64.const 2)
                        )
                      )
                    )
                
                

Limitations

Memory

  • Only flat linear memory support
  • No Garbage Collection except custom written
  • Work in progress

DOM

  • No DOM access from within wasm
  • DOM API proposed for future version

GPU

  • No GPU access
  • Not planned right now
  • So no blockchain mining in the browser :)
  • And no heavy Machine Learning...

Types

wasm interface types

How do we exchange other types?

  • Other types we have to pass the reference and read it directly from memory
  • We'll see that later

What is coming?

Garbage Collection

  • Currently not supported
  • Languages with GC compile it themselves
  • Bad for binary size and efficiency

Host bindings

  • Currently we have a very limited interface
  • Add sharing of JS/DOM objects
  • Create, call, manipulate and pass objects

Threads

  • Shared access to memory and atomic operations
  • No mechanism for creating threads
  • Supplied by the host which will be Web Workers

Exception Handling

  • No support so far
  • Fairly challenging proposal (#2)
  • Currently proposing a generic concept of events

Reference Types

  • Currently minimal set of types
  • Serialize more complex types to memory
  • New "anyref" type that allows references to JS objects

And so much more...

Resources

Questions?