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

The state of Swift for WebAssembly in 2022

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Yuta Saito
September 12, 2022

The state of Swift for WebAssembly in 2022

Yuta Saito

September 12, 2022

Transcript

43 slides
  1. About Me • @kateinoigakukun • Working at GoodNotes • SwiftWasm

    ͷϝϯςφ • ৽ถ Swift / CRuby / LLVM ίϛολ • Love Swift ! 2
  2. WebAssembly is a game changer A binary instruction format for

    a stack-based machine Designed to be • Portable • Size- and Load-time efficient • Secure by Sandbox 7
  3. 11

  4. 12

  5. ϓϩμΫγϣϯͰͷSwiftWasmͷϢʔεέʔε • GoodNotes1 • Note-taking app • Web Viewer for

    sharing • Luminal2 • Python notebook on browser 2 https://getluminal.com/ 1 https://www.goodnotes.com/ 13
  6. WASI compatible things Platforms • Node.js / Deno / Wasmtime

    • Fastly Compute@Edge • Cloudflare Workers • VSCode Extensions • Fluent Bit • Etc… Languages • C / C++ • Rust • Swift • Ruby • Etc… 20
  7. 23

  8. Web Ҏ֎ͷSwiftWasmͷϢʔεέʔε • Fastly Compute@Edge3 • Serverless platform based on

    WebAssembly • Enarx4 • WebAssembly + Confidential Computing on TEE (Trusted Execution Environment) 4 https://enarx.dev/docs/webassembly/swift 3 https://www.fastly.com/jp/blog/introducing-serverless-swift-building-on-compute-edge-with-andrew-barba 24
  9. 26

  10. Relative Pointer • Wasmͷ࢓༷Ͱ͸දݱͰ͖ͳ͔ͬͨ template<typename BasePtrTy, typename Offset> uintptr_t applyRelativeOffset(BasePtrTy

    *basePtr, Offset offset) { #ifdef __wasm__ // WebAssembly: hack: disable relative pointers return (uintptr_t)(intptr_t)offset; #endif auto base = reinterpret_cast<uintptr_t>(basePtr); auto extendOffset = (uintptr_t)(intptr_t)offset; return base + extendOffset; } 30
  11. Upstreaming patches • 44 patches for apple/swift • 16 patches

    for apple/swift-corelibs-foundation • 14 patches for apple/swift-package-manager • 4 patches for LLVM They are made by only 2 people! 36
  12. Swift Concurrency on Wasm JavaScriptKit provides a global executor implementation

    based on JS event-loop import JavaScriptEventLoop import JavaScriptKit JavaScriptEventLoop.installGlobalExecutor() func printZen() async throws { let fetch = JSObject.global.fetch.function! let response = try await JSPromise(fetch("https://api.github.com/zen").object!)!.value let text = try await JSPromise(response.text().object!)!.value print(text) } 37
  13. Tokamak Fiber Renderer • React 16Ҏ߱ͱಉ౳ͷΞϧΰϦζϜ • ਂ͘ωετͨ͠ViewͰ΋ελοΫΦʔόʔϑϩʔ͕ى͖ͳ͍ • ͜Ε·ͰͷStack

    Reconciler͸30 viewsͷωετͰ ελοΫΦʔόʔϑϩʔ͍ͯͨ͠ • Fiber ReconcilerͰ͸700·ͰOK https://github.com/TokamakUI/Tokamak/pull/471 38
  14. μWASI (swiftwasm/uwasi) • ܰྔWASI࣮૷ for ϒϥ΢β & Node.js • ैདྷͷ3rd-partyͷ࣮૷͸େ͖͗ͨ͢…

    (65.6 KB) • େ൒ͷػೳ͸࢖ͬͯͳ͔ͬͨ • SwiftWasmͰ৽͍͠ϞϊΛఏڙ: • Minimal: 3 KB • All features enabled: 6 KB https://github.com/swiftwasm/uwasi 39

More Decks by Yuta Saito

Other Decks in Technology