WebAssembly
Concepts of Programming Languages
Sebastian Macke
Rosenheim Technical University
Sebastian Macke
Rosenheim Technical University
- 1995: JavaScript is developed within 10 days by Brendan Eich - ... Java Applets, Flash, ActiveX, Silverlight - ... JavaScript improvements, ... - 2015: First presentation of WebAssembly from the W3C WebAssembly Working Group - 2016: Google, Microsoft and Mozilla show their first implementations - 2017: WebAssembly is offically supported by all Web Browsers - 2019: A WebAssembly system interface is specified for portable applications outside the browser - 2022: Docker suggests Wasm as an alternative to containers - Now: WebAssembly is constantly enhanced (https://webassembly.org/roadmap/)
After the introduction of JavaScript in the 90s, it is the second language supported directly by web browsers
- small size and easy to interpret
- Efficient decoding and compilation
- One-Pass Verification
- Representation of today's CPU architecture
- Sandboxed - 64kB segment. Enables hardware-supported boundary check - Size adaptable at runtime
- Export/import of functions, global variables and so-called tables
- Open and possibility for debugging
src/webassembly/wat
folderadd.wat
(module (type (;0;) (func (param i32 i32) (result i32))) (func (;0;) (type 0) (param i32 i32) (result i32) local.get 0 local.get 1 i32.add) (export "add" (func 0)))
add.js
const fs = require('fs'); async function Run() { let buffer = await fs.readFileSync('./add.wasm'); let result = await WebAssembly.instantiate(buffer); let add = result.instance.exports.add console.log(add(5, 2)) } Run()
github.com/appcypher/awesome-wasm-langs
In Production: π C π C++ π Go π Rust Stable for production: π₯ .Net π₯ AssemblyScript π₯ Brainfuck π₯ C# π₯ Clean π₯ COBOL π₯ F# π₯ Forth π₯ Lobster π₯ Lua π₯ Never π₯ Rego π₯ TypeScript π₯ WebAssembly π₯ Zig Unstable: π£ Ada π£ D π£ Eel π£ Elixir π£ Java π£ JavaScript π£ Kotlin/Wasm π£ Lisp π£ Lys π£ Pascal π£ Perl π£ PHP π£ Poetry π£ Python π£ Prolog π£ R π£ Ruby π£ Scheme π£ Scopes π£ Swift π£ Tcl Work in Progress: π₯ Dart π₯ Faust π₯ Co π₯ Forest π₯ Haskell π₯ Julia π₯ Kou π₯ Nim π₯ Ocaml π₯ Plorth
src/webassembly/C
folderadd.c
int add(int a, int b) { return a + b; }
add.js
const fs = require('fs'); async function Run() { let buffer = await fs.readFileSync('./add.wasm'); let result = await WebAssembly.instantiate(buffer); let add = result.instance.exports.add console.log(add(5, 2)) } Run()
package main func main() { println("Hello World") }
index.html
<html> <body> <script src="wasm_exec.js"></script> <script> const go = new Go(); async function Run() { let response = await fetch('lib.wasm') let buffer = await response.arrayBuffer(); let result = await WebAssembly.instantiate(buffer, go.importObject); go.run(result.instance) } Run() </script> </body> </html>
github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise11.md
func ListPrimes() { for n := 100000000001; ; n++ { if big.NewInt(int64(n)).ProbablyPrime(100000) { fmt.Printf("%d\n", n) } runtime.Gosched() } } func main() { go ListPrimes() for { fmt.Printf("Hello, WebAssembly!\n") time.Sleep(1 * time.Second) } }
webassembly.org/docs/use-cases/
//go:build js // +build js package main import ( "syscall/js" ) func add(this js.Value, input []js.Value) any { result := input[0].Float() + input[1].Float() return js.ValueOf(result) } func main() { document := js.Global().Get("document") p := document.Call("createElement", "p") p.Set("innerHTML", "Hello WASM from Go!") document.Get("body").Call("appendChild", p) // register function js.Global().Set("add", js.FuncOf(add)) // prevent exit c := make(chan struct{}, 0) <-c }
index.html
const go = new Go(); async function Run() { let response = await fetch('lib.wasm') let buffer = await response.arrayBuffer(); let result = await WebAssembly.instantiate(buffer, go.importObject); go.run(result.instance) document.body.innerHTML += add(41, 1); } Run() </script>
Question:
You have an Hello world executable.
What "Symbols" do you have export and import to execute the executable?
github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise11.md
github.com/mbasso/awesome-wasm
webassemblytoday.substack.com/
github.com/WebAssembly/binaryen
31