Slide 1

Slide 1 text

Yuta Saito 2024/09/17 Running Swift on WebAssembly Platforms Swift Server Side Meetup #03

Slide 2

Slide 2 text

About Me • Yuta Saito / @kateinoigakukun • Master's student from Tokyo, Japan • Committer of Swift and CRuby • Working on WebAssembly support

Slide 3

Slide 3 text

What is WebAssembly A binary program format compiled from various languages Design Philosophy • Portable • Virtualization-based Security • Deterministic • Embeddable • …

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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!

Slide 10

Slide 10 text

TODO: Show browser view

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

TODO: Show browser view

Slide 14

Slide 14 text

https://github.com/apple/swift-for-wasm-examples More minimalistic example? Multi-Platform Libraries With Swift for WebAssembly by Max Desiatov @ Wasm I/O 2024

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Yuta Saito 2024/09/17 Running Swift on WebAssembly Platforms Swift Server Side Meetup #03

Slide 17

Slide 17 text

Yuta Saito 2024/09/17 Running WebAssembly with Swift Swift Server Side Meetup #03

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

The State of Swift and WebAssembly Swift C / C++ Go Rust … Web browsers wasm-micro-runtime wasmtime … Language Runtime WasmKit

Slide 22

Slide 22 text

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))) ) ) """ ) )

Slide 23

Slide 23 text

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)])

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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