Browser Support Test — Swift source
Audit your real browser against 30 modern web capabilities - WebGPU, WASM, WebAuthn, Service Workers and more. Get a weighted readiness score, copy-paste detection snippets, and a shareable link.
This is the Swift implementation — the same logic the interactive tool runs, in a shareable, citable form.
// browser-support-test — Swift polyglot showcase port.
//
// Runtime capability probe, weighted score, and support tier.
//
// This is the Swift sibling of src/tools/BrowserSupportTest.tsx (+ the
// browser-support/* modules) that powers the live tool.
//
// ILLUSTRATIVE PORT: the live tool detects *browser* features at runtime
// (navigator/window probes) — a browser-only concern that cannot run in a
// native Swift process. This port shows the ANALOGOUS pattern for a Swift
// build: it probes compile-time `#if` conditions (os(), arch(),
// canImport()) — the mechanism Swift uses to gate platform features, and
// Swift's direct equivalent of Rust's cfg!(). The scoring algorithm and
// tier thresholds below are byte-for-byte the same as the TypeScript
// lib's; only the probe targets differ.
//
// Language: Swift (5.9, standard library only).
// Ported from src/tools/BrowserSupportTest.tsx (+ browser-support/* modules).
// Source: CosmoDev polyglot showcase port.
// License: display source — part of CosmoDev's polyglot tool pages.
//
// (The trailing top-level `main()` call keeps the file runnable with
// `swift swift.swift` — in a package executable it belongs in main.swift.)
import Foundation
let SUPPORTED = "supported"
let UNSUPPORTED = "unsupported"
let LIMITED = "limited"
let UNKNOWN = "unknown"
/// One capability the audit can check; `probe` returns true — or throws —
/// from THIS build. A throwing probe degrades to `unknown`, mirroring the
/// try/rescue arms of the sibling ports.
struct Check {
let id: String
let title: String
let category: String
let weight: Double
let probe: () throws -> Bool
}
/// Outcome of one probe.
struct FeatureResult {
let id: String
let status: String
}
/// contribution × weight. Same values as src/tools/browser-support/score.ts.
func contribution(_ status: String) -> Double {
switch status {
case "supported": return 1.0
case "limited": return 0.5
case "needs-https": return 0.3
default: return 0.0 // unsupported, unknown
}
}
/// Weighted readiness 0–100; matches computeScore() exactly. Results whose
/// id isn't in the catalog are ignored.
func computeScore(_ results: [FeatureResult], _ catalog: [Check]) -> Int {
var earned = 0.0
var possible = 0.0
for r in results {
// Look up this result's weight in the catalog (skip unknown ids).
for c in catalog where c.id == r.id {
earned += contribution(r.status) * c.weight
possible += c.weight
break
}
}
if possible == 0.0 { return 0 }
return Int((earned / possible * 100).rounded())
}
/// tier_for mirrors tierFor() in the island — same thresholds.
func tierFor(_ score: Int) -> (label: String, tone: String) {
if score >= 90 { return ("Fully modern", "text-success") }
if score >= 75 { return ("Very capable", "text-success") }
if score >= 55 { return ("Capable, minor gaps", "text-accent") }
if score >= 35 { return ("Showing its age", "text-fg-muted") }
return ("Legacy browser", "text-danger")
}
// Probes. `#if` conditions are resolved at compile time from the target
// triple — Swift's equivalent of Rust's cfg!() / the browser's
// `'feature' in window`.
func probeUnix() -> Bool {
#if os(macOS) || os(Linux) || os(FreeBSD) || os(OpenBSD)
return true
#else
return false
#endif
}
func probeWasm() -> Bool {
#if arch(wasm32)
return true
#else
return false
#endif
}
func probeNeon() -> Bool {
#if arch(arm64)
return true // NEON is part of the arm64 baseline
#else
return false
#endif
}
func probeSse2() -> Bool {
#if arch(x86_64)
return true // SSE2 is part of the x86-64 baseline
#else
return false
#endif
}
func probeFoundation() -> Bool {
#if canImport(Foundation)
return true
#else
return false
#endif
}
/// Representative catalog of #if-gated capabilities.
func checks() -> [Check] {
[
Check(id: "ptr-64", title: "64-bit pointers", category: "Platform", weight: 1.0,
probe: { MemoryLayout<Int>.size == 8 }),
Check(id: "unix", title: "Unix target", category: "Platform", weight: 2.0, probe: probeUnix),
Check(id: "wasm", title: "WASM target", category: "Platform", weight: 2.0, probe: probeWasm),
Check(id: "neon", title: "NEON", category: "Cpu", weight: 2.0, probe: probeNeon),
Check(id: "sse2", title: "SSE2", category: "Cpu", weight: 2.0, probe: probeSse2),
Check(id: "foundation", title: "Foundation", category: "Runtime", weight: 3.0, probe: probeFoundation),
]
}
/// Run probes; a throw degrades to 'unknown' so one failure can't abort
/// the audit (`try?` is Swift's catch_unwind/rescue).
func runChecks(_ catalog: [Check]) -> [FeatureResult] {
catalog.map { c in
let status: String
if let ok = try? c.probe() {
status = ok ? SUPPORTED : UNSUPPORTED
} else {
status = UNKNOWN
}
return FeatureResult(id: c.id, status: status)
}
}
func platformName() -> String {
#if os(macOS)
return "macOS"
#elseif os(Linux)
return "Linux"
#elseif os(Windows)
return "Windows"
#else
return "unknown"
#endif
}
func main() {
let catalog = checks()
let results = runChecks(catalog)
let score = computeScore(results, catalog)
let tier = tierFor(score)
let supported = results.filter { $0.status == SUPPORTED }.count
print("browser-support-test (Swift 5.9) on \(platformName())")
print("\(supported)/\(results.count) capabilities · readiness \(score) · \(tier.label)")
// Silence unused warning when LIMITED/UNKNOWN are intentionally absent
// from probes.
_ = LIMITED
_ = UNKNOWN
}
main()
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →