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

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

OVHcloud: A Global Leader 250k Private cloud VMs running 1 Dedicated IaaS Europe 30 Datacenters Own 20Tbps Hosting capacity : 1.3M Physical Servers 360k Servers already deployed GDG Lille Netwok with 35 PoPs

1.3M Customers in 138 Countries

OVHcloud: Our solutions Cloud GDG Lille Web Hosting Mobile Hosting Telecom VPS Containers ▪ Dedicated Server Domain names VoIP Public Cloud Compute ▪ Data Storage Email SMS/Fax Private Cloud ▪ Network and Database CDN Virtual desktop Serveur dédié Security Object Storage Web hosting Cloud HubiC Over theBox ▪ Licences Cloud Desktop Securities MS Office Hybrid Cloud Messaging MS solutions

Did I say WebAssembly? WASM for the friends… GDG Lille

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

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

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

With several key advantages GDG Lille

But above all… WebAssembly is not meant to replace JavaScript GDG Lille

Who is using WebAssembly today? And many more others… GDG Lille

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

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

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

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

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

WebAssembly project Joint effort GDG Lille

Hello W(ASM)orld My first WebAssembly program GDG Lille

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

We compile it with emscripten GDG Lille

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

We also get a .js file… Wrapping the WASM GDG Lille

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

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 ○ GDG Lille Async JavaScript retrieving and instantiating the .wasm binary

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

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

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

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 GDG Lille

Instantiating the WASM GDG Lille

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

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

Communicating between JS and WASM Shared memory, functions… GDG Lille

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 GDG Lille

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

Some use cases What can I do with it? GDG Lille

Tapping into other languages ecosystems Don’t rewrite libs anymore GDG Lille

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

AssemblyScript Writing WASM without learning a new language GDG Lille

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

Ahead of Time compiled TypeScript More predictable performance GDG Lille

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

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

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

WASM outside the browser Not only for web developers GDG Lille

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

Includes WAPM The WebAssembly Package Manager GDG Lille

But I need some POSIX WebAssembly System Interface GDG Lille

Huge potential GDG Lille

Future To the infinity and beyond! GDG Lille

WebAssembly Threads Threads on Web Workers with shared linear memory GDG Lille

SIMD GDG Lille

Garbage collector And exception handling GDG Lille