SwiftWasm Factorial - Swift-wasm Typing CST Test
Loading…
SwiftWasm Factorial — Swift-wasm Code
Calculates factorial recursively and prints result.
# swift/demo/factorial.swift
import JavaScriptKit
let console = JSObject.global.console
func factorial(_ n: Int) -> Int {
if n <= 1 { return 1 }
return n * factorial(n - 1)
}
console.log!("Factorial(5): \(factorial(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.