SwiftWasm Add Two Numbers - Swift-wasm Typing CST Test
Loading…
SwiftWasm Add Two Numbers — Swift-wasm Code
Adds two integers and prints the sum to console.
# swift/demo/add.swift
import JavaScriptKit
let console = JSObject.global.console
func add(a: Int, b: Int) {
console.log!("Sum: \(a + b)")
}
add(a: 3, b: 5)Swift-wasm Language Guide
SwiftWasm is a toolchain and ecosystem that compiles Swift code into WebAssembly, allowing developers to run Swift applications directly in the browser, WASI runtimes, serverless platforms, and edge environments. It brings Swift’s safety, performance, and modern language features to WebAssembly-based execution environments.
Primary Use Cases
- ▸Running Swift code in the browser
- ▸Building rich client-side apps using Swift + DOM APIs
- ▸WASI-based CLI tools written in Swift
- ▸Edge computing microservices in Swift
- ▸Full-stack Swift (backend + Wasm frontend)
Notable Features
- ▸Swift -> WebAssembly compilation support
- ▸JavaScript ↔ Swift bridging APIs
- ▸WASI target support for system-level interactions
- ▸Event loops and async/await support
- ▸Swift Package Manager integration
Origin & Creator
SwiftWasm originated as an open-source community project led by Max Desiatov and contributors around 2019, expanding the Swift compiler to support WebAssembly targets.
Industrial Note
SwiftWasm is gaining traction for Swift-based web UIs, full-stack Swift apps, serverless Wasm microservices, and Apple ecosystem developers who want to run Swift outside Apple hardware.
Quick Explain
- ▸SwiftWasm compiles Swift source code into WebAssembly using a specialized fork of the Swift compiler.
- ▸It enables running Swift apps inside browsers and headless WASI runtimes.
- ▸Supports JavaScript interop for DOM access and Web APIs.
- ▸Allows building serverless WebAssembly microservices using Swift.
- ▸Makes Swift viable beyond Apple platforms by targeting Wasm.
Core Features
- ▸Swift compiler backend targeting Wasm32
- ▸Interop with JavaScript using the JavaScriptKit library
- ▸Standard library subset adapted for WebAssembly
- ▸WASI compatibility for command-line apps
- ▸SwiftPM support for dependency management
Learning Path
- ▸Learn Swift basics
- ▸Understand WebAssembly concepts
- ▸Install SwiftWasm toolchain
- ▸Build and run a SwiftWasm browser app
- ▸Deploy to web or WASI runtime
Practical Examples
- ▸Counter app using Swift + DOM APIs
- ▸SwiftWasm game loop with Canvas API
- ▸Swift-WASI CLI tool
- ▸Edge computing function deployed via Wasmer
- ▸Swift-based code editors/tools running in browser
Comparisons
- ▸SwiftWasm vs Rust/Wasm: easier syntax, smaller ecosystem
- ▸SwiftWasm vs AssemblyScript: more powerful language
- ▸SwiftWasm vs TinyGo: bigger binaries, richer language
- ▸SwiftWasm vs C/C++ Emscripten: safer memory model
- ▸SwiftWasm vs JavaScript: faster compute, static types
Strengths
- ▸Modern, expressive Swift language for web/edge apps
- ▸Strong type safety and memory safety
- ▸Great interoperability with JavaScript in browser
- ▸Fast and efficient Wasm binaries via LLVM
- ▸Unified full-stack Swift experience
Limitations
- ▸Not all Swift stdlib APIs are available
- ▸Apple frameworks (UIKit, Foundation Networking, SwiftUI) are not supported in Wasm
- ▸Binary sizes can be larger than TinyGo/AssemblyScript
- ▸Limited multithreading due to Wasm constraints
- ▸Smaller ecosystem compared to JS/Rust for Wasm
When NOT to Use
- ▸Apps needing UIKit/AppKit
- ▸Swift code that depends heavily on Apple frameworks
- ▸Large binary-sensitive applications
- ▸Performance-critical kernels where Rust/C++ excel
- ▸Projects needing multithreading
Cheat Sheet
- ▸carton dev -> run local wasm dev server
- ▸swift build --triple wasm32-unknown-wasi
- ▸import JavaScriptKit for DOM interop
- ▸Use JSObject.global for Web APIs
- ▸WASI target: wasm32-unknown-wasi
FAQ
- ▸Does SwiftWasm support UIKit?
- ▸No, Apple frameworks are not supported in WebAssembly.
- ▸Can SwiftWasm run in all browsers?
- ▸Yes, all modern browsers that support Wasm.
- ▸Does SwiftWasm support networking?
- ▸Yes via fetch() + JS interop.
- ▸Is multithreading available?
- ▸Not fully supported yet.
- ▸Can SwiftWasm be used for games?
- ▸Yes, with Canvas/WebGL APIs.
30-Day Skill Plan
- ▸Week 1: Swift + WebAssembly basics
- ▸Week 2: JavaScriptKit interop
- ▸Week 3: WASI-based Swift apps
- ▸Week 4: Optimization and profiling
- ▸Week 5: Full-scale SwiftWasm SPA or edge microservice
Final Summary
- ▸SwiftWasm brings Swift into the WebAssembly ecosystem.
- ▸Ideal for Swift developers building browser or edge apps.
- ▸Supports WASI, JavaScript interop, and async Swift.
- ▸Safe, portable, and capable for many modern workloads.
- ▸A strategic choice for full-stack Swift teams.
Project Structure
- ▸Sources/ - application code
- ▸Package.swift - dependencies
- ▸index.html - browser loader for wasm
- ▸main.wasm - compiled Swift output
- ▸carton configuration for development server
Monetization
- ▸Swift-based wasm SaaS widgets
- ▸WebAssembly plugins
- ▸Edge-optimized paid APIs
- ▸Client-side premium tools
- ▸Wasm-based computation engines
Productivity Tips
- ▸Use carton dev for hot reload
- ▸Prefer async/await over callbacks
- ▸Keep interop minimal
- ▸Reuse JS wrappers
- ▸Optimize builds with -Osize
Basic Concepts
- ▸Wasm module -> compiled Swift program
- ▸JavaScriptKit -> interop for browser APIs
- ▸WASI -> Swift CLI runtime for Wasm environments
- ▸SwiftPM -> dependency management
- ▸async/await -> event-driven browser logic