A Swift API for the Wasmer WebAssembly Runtime


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:

  1. remove the /wasmer from the Cflags line, it should then read: Cflags: -I/Users/helge/.wasmer/include
  2. add -lffi to the Libs line, 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.)


In a separate package, because they carry some larger binaries: SwiftyWasmerTests.



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.


  • Swift Tools 5.3.0
View More Packages from this Author


  • None
Last updated: Fri Jan 27 2023 22:47:12 GMT-0500 (GMT-05:00)