In-depth hands-on experience with WebAssembly
Boyan Mihaylov @boyanio boyan.io
with WebAssembly Boyan Mihaylov @boyanio boyan.io - - PowerPoint PPT Presentation
In-depth hands-on experience with WebAssembly Boyan Mihaylov @boyanio boyan.io https://github.com/boyanio/wasm-class/ @boyanio WebAssembly ( WASM ) is compiler target for programs on the Web @boyanio function add(a, b) { return a + b; }
Boyan Mihaylov @boyanio boyan.io
@boyanio
@boyanio
function add(a, b) { return a + b; }
@boyanio
1995 2008 2017 Browser wars: the introduction of JIT ? Node.js
@boyanio
It supports 32 and 64-bit integers (i32, i64) and floating points (f32, f64)
@boyanio
@boyanio
0061 736d 0100 0000 0187 8080 8000 0160 027f 7f01 7f03 8280 8080 0001 0004 8480 8080 0001 7000 0005 8380 8080 0001 0001 0681 8080 8000 0007 9080 8080 0002 066d 656d 6f72 7902 0003 6164 6400 000a 8d80 8080 0001 8780 8080 0000 2001 2000 6a0b
@boyanio
(module (table 0 anyfunc) (memory $0 1) (export “memory” (memory $0)) (export “add” (func $add)) (func $add (; 0 ;) (param $0 i32) (param $1 i32) (result i32) (i32.add (get_local $1) (get_local $0) ) ) )
@boyanio
Parse Compile +
Re-
Execute GC Fetch Decode Compile +
Execute Fetch Execute Fetch / Decode / Compile + optimize (streaming API)
@boyanio
@boyanio
@boyanio
.cpp Source code Windows MAC Linux x86 x64 x86 x64 x86 x64 ARM v8 ARM v7
@boyanio
.cpp Source code .wasm WebAssembly module
@boyanio
Gaming industry Unreal, Unity Multimedia AV1, FLIF, BPG Libraries OpenCV, Box2D, LibSass Platform simulation / emulation DOSBox, MAME, QEMU
@boyanio
.cpp .wasm .html WASM module HTML page Browser compile load with JavaScript execute Source
@boyanio
https://wasdk.github.io/WasmFiddle/
@boyanio
Open Source LLVM to JavaScript compiler https://kripken.github.io/emscripten-site/
@boyanio
@boyanio
Source program Intermediate Representation (IR) Assembly or machine code Frontend Backend Static analysis Semantic analysis Code generation Code optimization
@boyanio
C / C++ LLVM IR JavaScript clang Fastcomp (LLVM Backend)
@boyanio
@boyanio
function () { “use asm”; function add(a, b) { a = a | 0; b = b | 0; return a + b | 0; } return { add: add }; }
@boyanio http://kripken.github.io/mloc_emscripten_talk/#/28
@boyanio
@boyanio
C / C++ LLVM IR asm.js clang Fastcomp WASM asm2wasm
@boyanio
C / C++ LLVM IR WASM clang LLVM WASM Backend
@boyanio https://github.com/WebAssembly/design/blob/master/Modules.md https://github.com/WebAssembly/design/blob/master/Modules.md
@boyanio
const imports = { “name”: { “first”: “Anna”, “last”: “Nanna” }, “print”: function (what) { console.log(what); } }; const exports = module.exports; exports.printName(); exports.reverseName();
@boyanio
1 2 3 4 5
00010011
6
00100100
7
10011100
8
11100011
9
00101111
10
00010000
11
01001110
12 13 14 15 16 17 18 19 1 2 3 4 5 6 const imports = { “env”: { “memory”: new WebAssembly.Memory({ initial: 10, maximum: 100}), … } };
@boyanio
1 2 3 4 5
00010011
6
00100100
7
10011100
8
11100011
9
00101111
10
00010000
11
01001110
12 13 14 15 16 17 18 19 1 2 3 4 5 6 What WebAssembly sees What JavaScript sees
@boyanio
fetch(‘app.wasm’) .then(result => result.arrayBuffer()) .then(buffer => WebAssembly.instantiate(buffer, imports)) .then(({ module, instance }) => { instance.exports.main(); }); // Traditional approach WebAssembly.instantiateStreaming(fetch(‘app.wasm’), imports) .then(({ module, instance }) => { instance.exports.main(); }); // Using the streaming API
@boyanio
@boyanio
@boyanio
VM JavaScript WebAssembly GC Compiler … APIs Rendering DOM + CSSOM WebGL WebSockets WebAudio IndexedDB Service Workers … Resource <script> HTML, CSS, SVG, … Audio Storage Network Graphics … Sandbox, Same-origin policy, …
@boyanio http://caniuse.com/#search=WebAssembly
Fallback to asm.js
@boyanio
@boyanio
today everything goes through JavaScript
@boyanio
for better interop with JavaScript & WebAPIs
@boyanio
low-level buildings blocks for shared memory between threads, atomics and futexes
@boyanio
@boyanio
https://boyan.io/wasm-wheel/