WebAssembly

Mariano Guerra (@warianoguerra)

Co-founder event-fabric.com

StuttgartJS, August 2016

Vocabulary

Assembly

The real one, often abbreviated asm:

Projects implemented completely in assembly

LLVM

llvm.org

LLVM Frontends

Source Code -> LLVM IR*

* IR: intermediate representation

LLVM Backends

LLVM IR -> CPU specific binary*

* ARM, x86, MIPS, PowerPC etc.

asm.js

Optimizable low-level subset of JavaScript*

* Not for humans

WebAssembly

Examples

What Wasm isn't

Wasm Status

Getting to MVP

Wasm Status

Wasm Status

Wasm Status

Browser

Wasm Support

View Source

Firefox Stable (44)

No

No

Firefox Nightly

Yes

Yes[1]

Chrome Stable (52)

Yes

No

Chrome Canary

Yes

No

Edge

No

No

(as of August 10th, 2016)

[1] Custom Format (js-like, not s-expression based)

Wasm Roadmap

Wasm MVP

Wasm Post-MVP

Wasm Future

Wasm Future

Wasm Future

Wasm Future

Wasm MVP

Wasm Binary Format

Wasm Binary Format

Wasm Text Format

Wasm Text Format

Wasm in a Nutshell

From Ben Titzer @ VMSS16's slides

Wasm Data Types

Wasm Functions

State

Data Operations

Structured Control Flow

Wasm Module

Wasm Module

Imports

Export

Start function

Global variables

Memory

Data

Tables

Elements

Functions

Code

Index Spaces

Tools

Emscripten

C/C++ -> llvm* -> asm.js

* LLVM Emscripten backend

Binaryen

Binaryen does

Yo dawg

Binaryen (C++) -> Emscripten -> asm.js wasm polyfill

Example Time!

Hello world

Let's translate hello.c to asm.js and Wasm.

Setting up the environment

Setting up the environment

What can I do with this?

Idea

A sometimes minimal FORTH compiler and tutorial for Linux but in WebAssembly!

Problem

No easy translation from asm to Wasm

wasm vs asm

Alternative

Translate the C version mentioned in the comments.

Problem II

The code was a little unreadable: http://ftp.funet.fi/pub/doc/IOCCC/1992/buzzard.2.orig.c

Solution

First deobfuscated it

Then

Result

Ricardo Forth

Result

Version

SLOC

Boilerplate

Total SLOC

C

229

0

229

JS (me)

241

0

241

Wasm (me)

425

0

425

Wasm[1] (Binaryen)

25626

5162

30788

asmjs[2] (Emscripten)

10322

4740

15062

[1] I close parenthesis the lisp way, binaryen doesn't

[2] "almost wasm"

Demo

Resources

Videos

More Resources

Thanks!