|
| 1 | +# WebAssembly Overview - Compilation Target for PizzaScript |
| 2 | + |
| 3 | +- Introduction |
| 4 | +- WebAssembly |
| 5 | + - Design |
| 6 | + - Key Features |
| 7 | + - Syntax |
| 8 | + - Examples |
| 9 | +- Demo |
| 10 | + - What about PizzaScript? |
| 11 | +- Summary |
| 12 | + - Feedback |
| 13 | + |
| 14 | +- Get to know `WebAssembly`, understand goals & definitions |
| 15 | +- See `WebAssembly` programmatic usage with Web |
| 16 | + |
| 17 | +# PizzaScript |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +- Learn `Go` language, and key libraries like `RxGo` |
| 22 | +- Understand how programming languages & interpreters work |
| 23 | +- Experiment with `WebAssembly` |
| 24 | + |
| 25 | +# WebAssembly |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +Bringing other languages to Web since 2014?! |
| 30 | + |
| 31 | +[Windows 95 with WebAssembly](https://archive.org/details/win95_in_dosbox) |
| 32 | + |
| 33 | +### WebAssembly (abbreviated Wasm) is a **binary instruction format** for **a stack-based virtual machine**. Wasm is designed as a **portable target** for **compilation of high-level languages** like `C/C++/Rust`, enabling deployment on the web for client and server applications. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## [Code First](https://mbebenita.github.io/WasmExplorer/) |
| 38 | + |
| 39 | +```c |
| 40 | +int add(int a, int b) { |
| 41 | + return a + b; |
| 42 | +} |
| 43 | +``` |
| 44 | +
|
| 45 | +```wat |
| 46 | +(module |
| 47 | + (func $add (param $lhs i32) (param $rhs i32) (result i32) |
| 48 | + get_local $lhs |
| 49 | + get_local $rhs |
| 50 | + i32.add) |
| 51 | + (export "add" (func $add)) |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +```javascript |
| 56 | +WebAssembly.instantiateStreaming(fetch(`program.wasm`)) |
| 57 | + .then(prog => { |
| 58 | + console.log(prog.instance.exports.add(1, 2)) |
| 59 | + }) |
| 60 | +``` |
| 61 | + |
| 62 | +[WasmExplorer Explorer](https://mbebenita.github.io/WasmExplorer/) - nice playground to compile C/C++ to `WebAssembly` |
| 63 | + |
| 64 | +## [History](https://www.youtube.com/watch?v=6r0NKEQqkz0) |
| 65 | + |
| 66 | +- `9 December 2011`, Google Native Client (`NaCl` and `PNaCl`) 😅 |
| 67 | +- `11 October 2013` - `18 August 2014`, Mozilla [`asm.js`](http://asmjs.org/) - `JavaScript` subset |
| 68 | +- `2015 - 2017`, WebAssembly Project Announce |
| 69 | + - `WebAssembly Working Group` |
| 70 | + - `Core Specification`, 2016 |
| 71 | + - Official logo, 2017 😂 |
| 72 | + - *Browser Preview* |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +- `2017 --> WebAssembly 1.0 MVP -->` [Proposals and WIP](https://github.com/WebAssembly/proposals) |
| 77 | + |
| 78 | +> The initial (MVP) WebAssembly API and binary format is complete to the extent that no further design work is possible without implementation experience and significant usage |
| 79 | +
|
| 80 | +## [Use-Cases](https://webassembly.org/docs/use-cases/) and usage examples |
| 81 | + |
| 82 | +- [Windows 95 with WebAssembly](https://archive.org/details/win95_in_dosbox) |
| 83 | +- `ZIP` for `WebAssembly` ?! |
| 84 | + - [Almost](https://github.com/YuJianrong/node-unrar.js) |
| 85 | + |
| 86 | +- [Doom 3](http://www.continuation-labs.com/projects/d3wasm/) |
| 87 | + - [Demo](http://wasm.continuation-labs.com/d3demo/) |
| 88 | + |
| 89 | +- Dynamic [`Polyfills` not only for `Web`](https://developer.mozilla.org/en-US/docs/WebAssembly/existing_C_to_wasm) |
| 90 | +- [Games](https://hackernoon.com/games-build-on-webassembly-3679b3962a19) |
| 91 | + |
| 92 | +### [Languages and Features Support](https://github.com/appcypher/awesome-wasm-langs) |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +# [Design Goals](https://webassembly.github.io/spec/core/intro/introduction.html#design-goals) |
| 99 | + |
| 100 | +- **Fast**: executes with near native code performance |
| 101 | +- **Safe**: code is validated and executes in a memory-safe, sandboxed environment |
| 102 | +- **Compact**: has a binary format that is fast to process |
| 103 | +- **Modular**: programs can be split up in smaller parts |
| 104 | +- **Efficient**: can be decoded, validated, and compiled in a fast single pass |
| 105 | +- **Platform-independent** |
| 106 | + |
| 107 | +# Definitions & [MVP](https://webassembly.org/docs/mvp/) |
| 108 | + |
| 109 | +- `Modules` and `JavaScript API` in secure environment |
| 110 | +- `Binary format (wasm)` - fast binary encoded format |
| 111 | +- `Text format` - text format for debugging |
| 112 | +- `wasm` engine design to be implemented by browsers and other environments |
| 113 | +- [WebAssembly High-Level Goals](https://webassembly.org/docs/high-level-goals/) |
| 114 | + - *execute in the same semantic universe as JavaScript* 🤔 |
| 115 | + |
| 116 | +- **No Support** |
| 117 | + - Garbage Collector (proposal...) |
| 118 | + - DOM Access |
| 119 | + - Old Browsers... |
| 120 | + |
| 121 | +And more! |
| 122 | +- [x] Threads |
| 123 | + |
| 124 | +## `Stack-based Virtual Machine`? |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +## Capabities |
| 131 | + |
| 132 | +- Data Types |
| 133 | + - void i32 i64 f32 f64 |
| 134 | +- Data Operations |
| 135 | + - i32: + - * / % << >> >>> etc |
| 136 | + - i64: + - * / % << >> >>> etc |
| 137 | + - f32: + - * / sqrt ceil floor |
| 138 | + - f64: + - * / sqrt ceil floor |
| 139 | +- Structured Control Flow |
| 140 | + - if loop block br switch |
| 141 | +- Functions |
| 142 | +- State: linear memory |
| 143 | + |
| 144 | +# JavaScript API |
| 145 | + |
| 146 | +- `WebAssembly` object |
| 147 | + - `Module`, `Instance`, `Memory`, `Table` |
| 148 | + - `validate()` |
| 149 | + - `compile()` |
| 150 | + - `instantiate()` |
| 151 | + |
| 152 | +```js |
| 153 | +var importObj = {js: { |
| 154 | + import1: () => console.log("hello,"), |
| 155 | + import2: () => console.log("world!"), |
| 156 | +}}; |
| 157 | + |
| 158 | +fetch('demo.wasm').then(response => |
| 159 | + response.arrayBuffer() |
| 160 | +).then(buffer => |
| 161 | + WebAssembly.instantiate(buffer, importObj) |
| 162 | +).then(({module, instance}) => |
| 163 | + instance.exports.f() |
| 164 | +); |
| 165 | +``` |
| 166 | + |
| 167 | +## [Modules](https://webassembly.org/docs/modules/) |
| 168 | + |
| 169 | +> The distributable, loadable, and executable unit of code in WebAssembly |
| 170 | +
|
| 171 | +- `imports`: `js, env, table, memory` |
| 172 | + |
| 173 | +```wat |
| 174 | +(module |
| 175 | + (import "js" "import1" (func $i1)) |
| 176 | + (import "js" "import2" (func $i2)) |
| 177 | + (func $main (call $i1)) |
| 178 | + (start $main) |
| 179 | + (func (export "f") (call $i2)) |
| 180 | +) |
| 181 | +``` |
| 182 | + |
| 183 | +- <a href="https://github.com/WebAssembly/proposals/issues/12">🛤 ECMAScript module integration</a> |
| 184 | + |
| 185 | +```html |
| 186 | +<script type="module" src="proposal.wasm"></script> |
| 187 | +``` |
| 188 | + |
| 189 | +## Tooling & Compilation |
| 190 | + |
| 191 | +- `Emscripten` |
| 192 | + |
| 193 | +``` |
| 194 | +C/C++/Rust -> AST -> Binary (.wasm) -> AST -> ...Module |
| 195 | +``` |
| 196 | + |
| 197 | +[](https://youtu.be/njt-Qzw0mVY?t=1135) |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +```bash |
| 202 | +# cmake |
| 203 | +PATH="/Applications/CMake.app/Contents/bin":"$PATH" |
| 204 | +cmake |
| 205 | +# wabt |
| 206 | +git clone --recursive https://github.com/WebAssembly/wabt |
| 207 | +cd wabt |
| 208 | +mkdir build |
| 209 | +cd build |
| 210 | +cmake .. |
| 211 | +cmake --build . |
| 212 | +PATH=$PATH:$(pwd) |
| 213 | +wasm-decompile --help |
| 214 | +# emsdk |
| 215 | +git clone https://github.com/emscripten-core/emsdk.git |
| 216 | +cd emsdk |
| 217 | +git pull |
| 218 | +./emsdk install latest |
| 219 | +./emsdk activate latest |
| 220 | +source ./emsdk_env.sh |
| 221 | +``` |
| 222 | + |
| 223 | + |
| 224 | +### [WebAssembly System Interface (WASI)](https://github.com/bytecodealliance/wasmtime/blob/master/docs/WASI-intro.md) |
| 225 | + |
| 226 | +[](https://github.com/bytecodealliance/wasmtime/blob/master/docs/wasi-software-architecture.png) |
| 227 | + |
| 228 | +# Performance |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +# [Demo](otus/webassembly/index.html) |
| 234 | + |
| 235 | +- [Simple Add Function](otus/webassembly/add.wat) |
| 236 | +- [Call Imported API](otus/webassembly/import.wat) |
| 237 | +- [Store API](otus/webassembly/store.wat) |
| 238 | + |
| 239 | +## [Fibonacci in c, js, and performance](/Users/rd25xo/Developer/experiments/otus/webassembly/fibonacci.sh) |
| 240 | + |
| 241 | +## [Go Hello World](https://github.com/golang/go/wiki/WebAssembly#getting-started) |
| 242 | + |
| 243 | +```bash |
| 244 | +mkdir docs |
| 245 | +GOOS=js GOARCH=wasm go build -o docs/main.wasm wasm.go |
| 246 | +cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" docs |
| 247 | +npx http-serve -p 8081 docs |
| 248 | +``` |
| 249 | + |
| 250 | +## What About PizzaScript |
| 251 | + |
| 252 | +- [Output wat](https://webassembly.studio/) |
| 253 | + |
| 254 | +```wat |
| 255 | +(module |
| 256 | + (import "console" "log" (func $log (param i32))) |
| 257 | + (func $add (result i32) |
| 258 | + i32.const 13 |
| 259 | + i32.const 13 |
| 260 | + i32.add |
| 261 | + i32.const 13 |
| 262 | + i32.const 13 |
| 263 | + i32.add |
| 264 | + i32.add) |
| 265 | + (export "add" (func $add)) |
| 266 | + (func (export "logIt") |
| 267 | + call $add |
| 268 | + call $log) |
| 269 | +) |
| 270 | +``` |
| 271 | + |
| 272 | +- Make a simple module |
| 273 | +- Wasm output by `wabt` |
| 274 | + |
| 275 | +## Summary |
| 276 | + |
| 277 | +- `WebAssembly` is a highly effective inter programming language protocol, which can be executed in browser and other environments in a secure context |
| 278 | + |
| 279 | +## Links |
| 280 | + |
| 281 | +- [Build Your Own WebAssembly Compiler - Colin Eberhardt, QCon San Francisco 2019](https://www.youtube.com/watch?v=OsGnMm59wb4) |
| 282 | +- [Compiling for the Web with WebAssembly (Google I/O '17)](https://www.youtube.com/watch?v=6v4E6oksar0) |
| 283 | +- [WebAssembly](https://webassembly.org/) |
| 284 | +- [WebAssembly: Disrupting JavaScript - Dan Callahan](https://www.youtube.com/watch?v=7mBf3Gig9io) |
| 285 | +- [Why we Need WebAssembly - An Interview with Brendan Eich - Eric Elliott](https://medium.com/javascript-scene/why-we-need-webassembly-an-interview-with-brendan-eich-7fb2a60b0723) |
| 286 | +- [WebAssembly Explorer](https://mbebenita.github.io/WasmExplorer/) |
| 287 | +- [WebAssembly for Web Developers (Google I/O ’19)](https://www.youtube.com/watch?v=njt-Qzw0mVY) |
| 288 | +- [WebAssembly Threads - HTTP 203](https://www.youtube.com/watch?v=x9RP-M6q2Mg) |
0 commit comments