Hello Web Assembly (WASM) !

Thach MAI
Slide location: http://thachmai.info/2016/11/05/hello-webassembly

Javascript is THE language of the web

Try noscript to get a glimpse of the world sans JS
The pace of JS evolution is staggering and unlikely to stop
ES6 is a modern scripting language

Javascript isn't perfect

Javascript source is text based, so it gets big fast
Processing data in Javascript is slow:
Javascript is currently the compiler target of the web, a role ill-suited for a scripting language

What is Web Assembly?

An cross-vendor initiative to create a "machine language" for the web
Announced mid 2015. Currently on browser preview milestone on Chrome, Firefox and Edge. Activated with a config option, `chrome://flags` for Chrome, `about:config` for Firefox
Web Assembly format is a binary, low-level byte code format
A demo is worth 1000 slideshows: http://webassembly.org/demo/

Goals of Web Assembly

WebAssembly doesn't set out to replace Javascript! In fact, currently it can only be executed from Javascript
WASM aims to bring more capabilities to the web: faster web pages and the possibility of a web platform
Currently memory is managed manually. Eventually, WASM wants to support dynamic memory allocation
Currently WASM can only interact with the world through JS API. Eventually, WASM wants to implement native Web API

My first WASM program

(module
  (func $addTwo (param i32 i32) (result i32)
    (i32.add
      (get_local 0)
      (get_local 1)))
  (export "addTwo" $addTwo))
        

Running WASM with JS

fetch('test.wasm')
    .then(response => response.arrayBuffer())
    .then(bytes => WebAssembly.compile(bytes))
    .then(compiled => new WebAssembly.Instance(compiled, null))
    .then(instance => {
        console.log(instance.exports.addTwo(41, 1));
    });
        

WASM Tooling

Head over to http://webassembly.org/ for the latest info
I use wabt to write the program. Note that currently you need to switch to branch `binary_0xb` to run on Chrome
Emscripten can be used to compile C/C++ into WASM