SwiftyWasmer
A Swift API for the Wasmer WebAssembly Runtime
Install / Use
/learn @helje5/SwiftyWasmerREADME
SwiftyWasmer
A Swift API for the Wasmer WebAssembly Runtime.
SwiftyWasmer packages the
Wasmer C API
for the Swift programming language,
i.e. it provides CWasmer system library with a proper module map
and a Wasmer module with a nice Swift style API for Wasmer.
Note: This is for embedding/running WebAssembly (Wasm) modules from within a Swift host program. It is not about compiling Swift to WebAssembly (there is the SwiftWasm effort for this).
What does it look like? Like this:
import Wasmer
// Just load a file into memory
let wasmFile = URL(fileURLWithPath: "sum.wasm")
let wasmData = try Data(contentsOf: wasmFile)
// Compile the Data into a module, and instantiate that
let module = try WebAssembly.Module (wasmData)
let instance = try WebAssembly.Instance(module)
// Run a function exported by the Module
let results = try instance.exports.sum(.i32(7), .i32(8))
Wasmer Installation for Swift
This works on either Intel or M1 Macs.
Installing Wasmer itself is easy:
curl https://get.wasmer.io -sSfL | sh
Or install a tarball: Wasmer 1.0.0 wherever you like.
To work, the
Swift Package Manager
requires a
pkg-config
file in a system location.
Fortunately wasmer config can generate one for you:
wasmer config --pkg-config > /usr/local/lib/pkgconfig/wasmer.pc
Unfortunately the generated file is a little b0rked in 1.0.0. Open up the file in your favorite editor:
emacs /usr/local/lib/pkgconfig/wasmer.pc
mate /usr/local/lib/pkgconfig/wasmer.pc
open /usr/local/lib/pkgconfig/wasmer.pc
And adjust two little things:
- remove the
/wasmerfrom theCflagsline, it should then read:Cflags: -I/Users/helge/.wasmer/include - add
-lffito theLibsline, it should then read:Libs: -L/Users/helge/.wasmer/lib -lwasmer -lffi
(the latter is only required when linking static libs, but doesn't hurt either way).
To link statically (recommended), move libwasmer.dylib out of the way:
mv ~/.wasmer/lib/libwasmer.dylib ~/.wasmer/lib/libwasmer.dylib-away
(Dynamic linking should also work, but you'd need to ensure that the dylib is in the dynamic library lookup path.)
Tests
In a separate package, because they carry some larger binaries: SwiftyWasmerTests.
Links
- Wasmer
- WebAssembly
- SwiftWasm (compiling Swift to Wasm)
Who
SwiftyWasmer is brought to you by the Always Right Institute and ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.
Related Skills
node-connect
352.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
