WebAssembly for Developers (web… or not)

A presentation at BordeauxJS in September 2019 in Bordeaux, France by Horacio Gonzalez

Slide 1

Slide 1

WebAssembly for Developers (web… or not) Horacio Gonzalez @LostInBrittany

Slide 2

Slide 2

Horacio Gonzalez @LostInBrittany Spaniard lost in Brittany, developer, dreamer and all-around geek

Slide 3

Slide 3

Did I say WebAssembly? WASM for the friends…

Slide 4

Slide 4

WebAssembly, what’s that? Let’s try to answer those (and other) questions…

Slide 5

Slide 5

A low-level binary format for the web Not a programming language A compilation target

Slide 6

Slide 6

That runs on a stack-based virtual machine A portable binary format that runs on all modern browsers… but also on NodeJS!

Slide 7

Slide 7

With several key advantages

Slide 8

Slide 8

But above all… WebAssembly is not meant to replace JavaScript

Slide 9

Slide 9

Who is using WebAssembly today? And many more others…

Slide 10

Slide 10

A bit of history Remembering the past to better understand the present

Slide 11

Slide 11

Executing other languages in the browser A long story, with many failures…

Slide 12

Slide 12

2012 - From C to JS: enter emscripten Passing by LLVM pivot

Slide 13

Slide 13

Wait, dude! What’s LLVM? A set of compiler and toolchain technologies

Slide 14

Slide 14

2013 - Generated JS is slow… Let’s use only a strict subset of JS: asm.js Only features adapted to AOT optimization

Slide 15

Slide 15

WebAssembly project Joint effort

Slide 16

Slide 16

Hello W(ASM)orld My first WebAssembly program

Slide 17

Slide 17

Do you remember your 101 C course? A simple HelloWorld in C

Slide 18

Slide 18

We compile it with emscripten

Slide 19

Slide 19

We get a .wasm file… Binary file, in the binary WASM format

Slide 20

Slide 20

We also get a .js file… Wrapping the WASM

Slide 21

Slide 21

And a .html file To quickly execute in the browser our WASM

Slide 22

Slide 22

And in a more Real WorldTM case? A simple process: ● Write or use existing code ○ In C, C++, Rust, Go, AssemblyScript… ● Compile ○ Get a binary .wasm file ● Include ○ The .wasm file into a project ● Instantiate ○ Async JavaScript compiling and instantiating the .wasm binary

Slide 23

Slide 23

I think I need a real example now Let’s use WASM Explorer https://mbebenita.github.io/WasmExplorer/

Slide 24

Slide 24

Let’s begin with the a simple function WAT: WebAssembly Text Format Human readable version of the .wasm binary

Slide 25

Slide 25

Download the binary .wasm file Now we need to call it from JS…

Slide 26

Slide 26

Instantiating the WASM 1. Get the .wasm binary file into an array buffer 2. Compile the bytes into a WebAssembly module 3. Instantiate the WebAssembly module

Slide 27

Slide 27

Instantiating the WASM

Slide 28

Slide 28

Loading the squarer function We instantiate the WASM by loading the wrapping JS

Slide 29

Slide 29

Using it! Directly from the browser console (it’s a simple demo…)

Slide 30

Slide 30

WASM outside the browser Not only for web developers

Slide 31

Slide 31

Run any code on any client… almost Languages compiling to WASM

Slide 32

Slide 32

Includes WAPM The WebAssembly Package Manager

Slide 33

Slide 33

Some Use Cases What can I do with this?

Slide 34

Slide 34

Tapping into other languages ecosystems Don’t rewrite libs anymore

Slide 35

Slide 35

Replacing problematic JS bits Predictable performance Same peak performance, but less variation

Slide 36

Slide 36

AssemblyScript Writing WASM without learning a new language

Slide 37

Slide 37

TypeScript subset compiled to WASM Why would I want to compile TypeScript to WASM?

Slide 38

Slide 38

Ahead of Time compiled TypeScript More predictable performance

Slide 39

Slide 39

Avoiding the dynamicness of JavaScrip More specific integer and floating point types

Slide 40

Slide 40

Objects cannot flow in and out of WASM yet Using a loader to write/read them to/from memory

Slide 41

Slide 41

No direct access to DOM Glue code using exports/imports to/from JavaScript

Slide 42

Slide 42

Future To the infinity and beyond!

Slide 43

Slide 43

WebAssembly Threads Threads on Web Workers with shared linear memory

Slide 44

Slide 44

SIMD

Slide 45

Slide 45

Garbage collector And exception handling