$ show swiftwasm/JavaScriptKit
JavaScriptKit is a production-grade Swift framework and Swift Package Manager plugin suite that enables bidirectional JavaScript interop for Swift code compiled to WebAssembly (WASM). The core runtime (JavaScriptKit) provides low-level types such as JSObject, JSValue, JSClosure, JSPromise, and JSBigInt, along with value conversion protocols (ConvertibleToJSValue, ConstructibleFromJSValue) and thread-local storage helpers. The BridgeJS plugin uses Swift macros (@JS, @JSFunction, @JSClass, etc.) to automatically generate Swift and JavaScript glue code, including WebAssembly export/import tables, stack-based marshalling, codec caches, and closure lifecycle management. The PackageToJS plugin packages the compiled WASM module into a deployable JavaScript package with platform-specific instantiation logic for browsers, Node.js, and WASI. The repository includes extensive snapshot-based code generation tests, runtime integration tests, benchmarks, and real-world examples (browser apps, Node.js servers, multithreading with web workers, OffscreenCanvas, actor-based concurrency). The architecture is a layered combination of a Swift library, Swift macros, a CLI tool (bridge-js), and SPM build plugins, all heavily dependent on WebAssembly-specific Swift features (@_expose(wasm), @_cdecl) and the Swift concurrency runtime. The project is actively maintained with a large test surface, but carries medium technical drift due to reliance on experimental Swift features and snapshot brittleness.
The LLM identified 4 files worth highlighting for reuse:
A generic ThreadLocal and LazyThreadLocal property wrapper for per-thread storage in Swift, using a dictionary keyed by pthread thread ID. This pattern is isolated (no internal imports beyond Foundation/Darwin/Glibc), demonstrates non-obvious handling of thread lifetime and lazy initialization, and can be extracted into any Swift project needing thread-local variables without global mutable state.
A generic JSUndefinedOr<Wrapped> enum that distinguishes between JavaScript 'undefined' and 'null', providing a type-safe alternative to Swift's Optional (which maps to null). The implementation is self-contained (only depends on JSValue and ConvertibleFromJSValue protocols) and can be reused in any JavaScript interop project to correctly model the JS undefined/null distinction.
A minimal, generic incremental build system implemented in ~300 lines. It defines a Task graph with mtime-based change detection, phony tasks, fingerprint support, and shell command execution. The module has zero internal dependencies (only Foundation) and can be extracted into any Swift project needing a simple, file-based build orchestrator without external build systems.
A TypeScript module managing Swift closures passed to JavaScript. It uses FinalizationRegistry for automatic cleanup and provides a typed interface for registering, invoking, and releasing closures. The implementation is isolated (no imports from other runtime modules) and demonstrates a robust pattern for bridging callbacks across language boundaries with proper lifecycle handling.