Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wasm 101

Wasm 101

WebAssembly с нуля :)

Polina Gurtovaya

March 19, 2020
Tweet

More Decks by Polina Gurtovaya

Other Decks in Programming

Transcript

  1. 2

  2. 3

  3. 4

  4. 10 (func (export "add") (param i32) (param i32) (result i32)

    local.get 0 local.get 1 i32.add) add(1, 2) Module Memory Function Instruction Table
  5. 12 (module (func $i (import "imports" "logger") (param i32)) (memory

    (import "imports" "importedMemory") 1) (func (export "exportedFunc") i32.const 42 call $i) (func (export "add") (param i32 i32) (result i32) local.get 0 local.get 1 i32.add) (data (i32.const 0) "Fifty"))
  6. Wasm и JavaScript — лучшие друзья 14 (module (func $i

    (import "imports" "logger") (param i32)) (memory (import "imports" "importedMemory") 1) (func (export "exportedFunc") i32.const 42 call $i) (func (export "add") (param i32 i32) (result i32) local.get 0 local.get 1 i32.add) (data (i32.const 0) "tver.io")) const importedMemory = new WebAssembly.Memory({ initial: 1, maximum: 10 }); WebAssembly.instantiateStreaming(fetch("simple.wasm"), { imports: { logger: function(arg) { console.log("first function call", arg); }, importedMemory } }).then(obj => { obj.instance.exports.exportedFunc(); const three = obj.instance.exports.add(1, 2); console.log("1 + 2 =", three); var memoryArray = new Uint32Array(importedMemory.buffer, 0, 10); console.log( "reading from memory", new TextDecoder("utf8").decode(memoryArray) ); });
  7. Мы поняли, что: Wasm = IR Wasm легко парсить Wasm

    = легкий формат Wasm = песочница 15
  8. 25 emcc -O3 \ -s ENVIRONMENT="web" \ -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' \

    -I libwebp libwebp/src/{dec,dsp,demux,enc,mux,utils}/*.c \ -o encoder.js encoder.c Вот так можно скомпилировать libwebp в wasm