1 of 33

WebAssembly n’est pas qu’une affaire de frontend

Benjamin Coenen

@bnj25

2 of 33

2

Benjamin Coenen

bnj25

bnjjj

3 of 33

https://www.apollographql.com/blog/announcement/backend/announcing-apollo-router-v1-0/

4 of 33

Summary

  • Historique
  • WebAssembly
  • Frontend side
  • Ecosystème et cas d’usages backend
  • Outils et libs
  • Exemples

5 of 33

Historique

  • Première version en 2015
  • Créé et maintenu par le W3C
  • Plus performant que JavaScript
  • Support d’autre langage que JavaScript dans le browser
  • Mozilla, Google, Fastly

6 of 33

WebAssembly

7 of 33

WebAssembly: langage à pile

Exemple opération: 5 + 2

8 of 33

WebAssembly: langage à pile

9 of 33

WebAssembly: langage à pile

10 of 33

WebAssembly: langage à pile

11 of 33

WebAssembly: langage à pile

12 of 33

WebAssembly: langage à pile

13 of 33

WebAssembly: 5 types

14 of 33

WebAssembly: formats

  • .wasm →Binary format
  • .wat →WebAssembly Text

15 of 33

WebAssembly

16 of 33

Frontend side

17 of 33

Frontend side: communication entre JavaScript et Wasm

18 of 33

Frontend side

19 of 33

Backend side: cas d’usage

  • Function As A Service (FAAS)
  • Smart contract
  • Création de module WebAssembly pour l’exécuter depuis d’autres langages
  • Création de plugins pour étendre des outils
  • Représentation intermédiaire pour compilateur

20 of 33

Backend side: projets existants

21 of 33

Backend side: projets existants

HIPPO

Deislab

22 of 33

Backend side: projets existants

23 of 33

Tools and libraries: runtimes (liste non-exhaustive)

  • Frontend
  • Backend

Wasmtime

24 of 33

Environnement sandboxé

X

X

25 of 33

Environnement sandboxé

V

V

26 of 33

Environnement sandboxé: exemple

27 of 33

Bindings

Cas d’usage: utilisation dans un autre langage

(func $concat (type $t10) (param $p0 i32) (param $p1 i32) (param $p2 i32) (param $p3 i32) (param $p4 i32) (param $p5 i32) (param $p6 i32)

#[no_mangle]

pub extern "C" fn concat(mut first: String, second: String) -> String {

first.push_str(&second);

first

}

ptr, len0 = _encode_utf8(first, realloc, memory, caller)

ptr1, len2 = _encode_utf8(second, realloc, memory, caller)

ret = self._concat(caller, ptr, len0, ptr1, len2)

assert(isinstance(ret, int))

load = _load(ctypes.c_int32, memory, caller, ret, 0)

load3 = _load(ctypes.c_int32, memory, caller, ret, 8)

ptr4 = load

len5 = load3

list = _decode_utf8(memory, caller, ptr4, len5)

28 of 33

Bindings générés via wit-bindgen

wit-bindgen, exemple de fichier .wit

Fichier bindings.py généré

+

#[no_mangle]

pub extern "C" fn concat(mut first: String, second: String) -> String {

first.push_str(&second);

first

}

concat: function(first: string, second: string) -> string

input = bindings.Input(store, linker, my_module)

print(input.concat(store, "hello", "world"))

29 of 33

Exécution au niveau de python

Runtime WASM

Lecture dans la mémoire du runtime via pointeur

30 of 33

Fichier wit auto généré grâce à witgen

Fichier .wit généré avec witgen

#[no_mangle]

#[witgen]

pub extern "C" fn concat(mut first: String, second: String) -> String {

first.push_str(&second);

first

}

concat: function(first: string, second: string) -> string

Fichier bindings.py généré

+

input = bindings.Input(store, linker, my_module)

print(input.concat(store, "hello", "world"))

31 of 33

Exemple wit-bindgen et witgen

32 of 33

Toolings

  • wasm-opt → optimise le wasm généré
  • wasm2wat → transforme le format .wasm en .wat pour débug
  • emscripten → compile du wasm vers du js compliant asm.js

33 of 33

Merci

bnj25

bnjjj