Les mains dans le cambouis avec WebAssembly

A presentation at DevFest Nantes in October 2019 in Nantes, France by Horacio Gonzalez

Slide 1

Slide 1

WebAssembly Codelab Stéphanie Moallic @steffy_29 Horacio Gonzalez @LostInBrittany

Slide 2

Slide 2

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

Slide 3

Slide 3

Stéphanie Moallic @steffy_29 Steffy29 “La dame du téléphone” - Quentin Adam Duchess Développeuse front Telecom chez OVHcloud Organisatrice d’évènements pour les développeurs et les enfants. Passionnée d’informatique mais pas que… Prédilection pour le développement front ainsi que les gadgets et autres jouets.

Slide 4

Slide 4

How is the codelab structured? What are we coding today?

Slide 5

Slide 5

A GitHub repository https://github.com/LostInBrittany/wasm-codelab

Slide 6

Slide 6

Nothing to install Using WebAssembly Explorer and WebAssembly Studio

Slide 7

Slide 7

Only additional tool: a web server Because of the browser security model

Slide 8

Slide 8

Procedure: follow the steps Step by step

Slide 9

Slide 9

But before coding, let’s speak What’s this WebAssembly thing?

Slide 10

Slide 10

Did we say WebAssembly? WASM for the friends…

Slide 11

Slide 11

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

Slide 12

Slide 12

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

Slide 13

Slide 13

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

Slide 14

Slide 14

With several key advantages

Slide 15

Slide 15

But above all… WebAssembly is not meant to replace JavaScript

Slide 16

Slide 16

Who is using WebAssembly today? And many more others…

Slide 17

Slide 17

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

Slide 18

Slide 18

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

Slide 19

Slide 19

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

Slide 20

Slide 20

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

Slide 21

Slide 21

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

Slide 22

Slide 22

WebAssembly project Joint effort

Slide 23

Slide 23

Hello W(ASM)orld My first WebAssembly program

Slide 24

Slide 24

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

Slide 25

Slide 25

We compile it with emscripten

Slide 26

Slide 26

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

Slide 27

Slide 27

We also get a .js file… Wrapping the WASM

Slide 28

Slide 28

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

Slide 29

Slide 29

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 30

Slide 30

I don’t want to install a compiler now… Let’s use WASM Explorer https://mbebenita.github.io/WasmExplorer/

Slide 31

Slide 31

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

Slide 32

Slide 32

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

Slide 33

Slide 33

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 34

Slide 34

Instantiating the WASM

Slide 35

Slide 35

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

Slide 36

Slide 36

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

Slide 37

Slide 37

You sold us a codelab! Stop speaking and let us code

Slide 38

Slide 38

You can do steps 01 and 02 now Let’s code, mates!

Slide 39

Slide 39

WASM outside the browser Not only for web developers

Slide 40

Slide 40

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

Slide 41

Slide 41

Includes WAPM The WebAssembly Package Manager

Slide 42

Slide 42

Some use cases What can I do with it?

Slide 43

Slide 43

Tapping into other languages ecosystems Don’t rewrite libs anymore

Slide 44

Slide 44

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

Slide 45

Slide 45

Communicating between JS and WASM Shared memory, functions…

Slide 46

Slide 46

Native WASM types are limited WASM currently has four available types: ● ● ● ● i32: 32-bit integer i64: 64-bit integer f32: 32-bit float f64: 64-bit float Types from languages compiled to WASM are mapped to these types

Slide 47

Slide 47

How can we share data? Using the same data in WASM and JS? Shared linear memory between them!

Slide 48

Slide 48

You can do steps 03 and 04 now Let’s code, mates!

Slide 49

Slide 49

AssemblyScript Writing WASM without learning a new language

Slide 50

Slide 50

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

Slide 51

Slide 51

Ahead of Time compiled TypeScript More predictable performance

Slide 52

Slide 52

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

Slide 53

Slide 53

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

Slide 54

Slide 54

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

Slide 55

Slide 55

You can do step 05 now Let’s code, mates!

Slide 56

Slide 56

Future To the infinity and beyond!

Slide 57

Slide 57

WebAssembly Threads Threads on Web Workers with shared linear memory

Slide 58

Slide 58

SIMD

Slide 59

Slide 59

Garbage collector And exception handling