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

Running Swift on WebAssembly Platforms

Yuta Saito
September 17, 2024

Running Swift on WebAssembly Platforms

Swift Server Side Meetup #03
https://www.youtube.com/live/kc-a8_DFXgE

Yuta Saito

September 17, 2024
Tweet

More Decks by Yuta Saito

Other Decks in Programming

Transcript

  1. About Me • Yuta Saito / @kateinoigakukun • Master's student

    from Tokyo, Japan • Committer of Swift and CRuby • Working on WebAssembly support
  2. What is WebAssembly A binary program format compiled from various

    languages Design Philosophy • Portable • Virtualization-based Security • Deterministic • Embeddable • …
  3. Both languages and platforms benefit from Wasm • For language

    developers: • If it compiles to Wasm, it can run in various environments • For platform developers: • With a Wasm runtime, it can support multiple languages →Good for program distribution formats
  4. Wasm outside of Web browser? • Lightweight and easy to

    maintain • No kernel dependent isolation • Isolation within same process • Small spec, small runtime • Restricted security model
  5. Carton a build work fl ow tool for
 Web app

    development in Swift • Community-driven e ff ort to make Swift WebAssembly-friendly • Provides a collection of products related to Wasm and Swift SwiftWasm Project Swift SDK for WebAssembly JavaScriptKit a JS interop library for Swift All compiler and standard library changes have been upstreamed! swiftwasm.org
  6. Is it used in serious production? Hybrid way: Swift on

    Wasm + React.js Goodnotes Web fl owkey Web We were able to use 100%1 And more… ^1: https://forums.swift.org/t/web-workers-in-swift-wasm-via-da/60540 Yes! https://web.dev/case-studies/goodnotes
  7. Why building Web app by Swift? • Just for fun?

    • Yes, of course :) • Share client logics across platforms • e.g. Goodnotes reuse of more than 100 thousand lines of code
  8. How to start building Web app with Swift? $ #

    Create a new SwiftPM package $ swift package init --type executable --name Hello $ # Add carton build tool as a dependency $ swift package add-dependency https://github.com/swiftwasm/ carton --from 1.0.0 $ # Start dev server $ swift run carton dev $ # That’s it!
  9. How to interact with DOM? $ # Add JavaScript interop

    library $ swift package add-dependency https://github.com/swiftwasm/ JavaScriptKit --from 0.20.0 $ swift package add-target-dependency --package JavaScriptKit JavaScriptKit Hello
  10. How to interact with DOM? $ # Edit Sources/main.swift import

    JavaScriptKit let document = JSObject.global.document var p = document.createElement("p") p.innerText = "Hello Swift!” _ = document.body.appendChild(p)
  11. Next Steps • SwiftWasm book • https://book.swiftwasm.org • Examples of

    Swift for WebAssembly by Steven Van Impe • https://github.com/pwsacademy/swiftwasm-examples • Cross-Platform Swift: WebAssembly by Point-Free • https://www.pointfree.co/episodes/ep291-cross-platform-swift-webassembly • swift-react by @omochimetaru • https://github.com/omochi/swift-react Learn more about Swift on WebAssembly
  12. Carton a build work fl ow tool for
 Web app

    development in Swift • Community-driven e ff ort to make Swift WebAssembly-friendly • Provides collection of products related to Wasm and Swift SwiftWasm Project Swift SDK for WebAssembly JavaScriptKit a JS interop library for Swift
  13. Carton a build work fl ow tool for
 Web app

    development in Swift • Community-driven e ff ort to make Swift WebAssembly-friendly • Provides collection of products related to Wasm and Swift SwiftWasm Project Swift SDK for WebAssembly JavaScriptKit a JS interop library for Swift WasmKit a WebAssembly runtime
 for Swift NEW
  14. WasmKit • Minimal dependencies • The core runtime engine depends

    only on swift-system. • No Foundation dependency • Compact and embeddable • Debug build completes in 5 seconds • Batteries included • WASI support, WAT parser, etc. A reasonably fast Wasm interpreter written in Swift github.com/swiftwasm/WasmKit
  15. The State of Swift and WebAssembly Swift C / C++

    Go Rust … Web browsers wasm-micro-runtime wasmtime … Language Runtime WasmKit
  16. How to embed Wasm runtime to your App import WasmKit

    import WAT // Convert a WAT file to a Wasm binary, then parse it. let module = try parseWasm( bytes: try wat2wasm( """ (module (import "printer" "print_i32" (func $print_i32 (param i32))) (func (export "print_add") (param $x i32) (param $y i32) (call $print_i32 (i32.add (local.get $x) (local.get $y))) ) ) """ ) )
  17. How to embed Wasm runtime to your App // Define

    a host function that prints an i32 value. let hostPrint = HostFunction(type: FunctionType(parameters: [.i32])) { _, args in // This function is called from "print_add" in the WebAssembly module. print(args[0]) return [] } // Create a runtime importing the host function. let runtime = Runtime(hostModules: [ "printer": HostModule(functions: ["print_i32": hostPrint]) ]) let instance = try runtime.instantiate(module: module) // Invoke the exported function "print_add" _ = try runtime.invoke(instance, function: "print_add", with: [.i32(42), .i32(3)])
  18. Showcase: swift-subprocess-distributedactors WasmKit FrenchGreeter.wasm EnglishGreeter.wasm Distributed Plugin Actor System Host

    Call Greeter/greet Implements Greeter Distributed Actor protocol Process Execute plugin Greeters in isolated way but within a single process https://github.com/martiall/swift-subprocess-distributedactors Distributed Plugin Actor System Plugin Plugin
  19. Future: Speedup Swift Macros build by Wasm • Pre-compile Swift

    Macros into .wasm • Using Swift SDK for WebAssembly • Distribute .wasm Macros • Evaluate .wasm Macros without building themselves • Powered by WasmKit
  20. Conclusion • WebAssembly support in Swift evolves year and year

    • Now powers some serious production applications • WasmKit o ff ers an easy way to execute WebAssembly in your application