2 min read

The attempts of porting Swift to WebAssembly has been going on for very long, and finally, a team of developers has come up with SwiftWasm, which was released last week. With this tool, you will now be able to run your Swift code on the web by compiling it to WebAseembly.

The SwiftWasm tool is built on top of the WASI SDK, which is a WASI-enabled C/C++ toolchain. This makes the WebAssembly executables generated by SwiftWasm work on both browsers and standalone WebAssembly runtimes such as Wasmtime, Fastly’s Lucet, or any other WASI-compatible WebAssembly runtime.

How you can work with SwiftWasm?

While macOS does not need any dependencies to be installed, some dependencies need to be installed on Ubuntu and Windows:

On Ubuntu install ‘libatomic1’:

sudo apt-get install libatomic1

On Windows:

First Install the Windows Subsystem for Linux, and then install the libatomic1 library.

The next step is to compile SwiftWasm by running the following commands:

./swiftwasm example/hello.swift hello.wasm

To run the resulting ‘hello.wasm’ file, go to the SwiftWasm polyfill and upload the file. You will see the output in the textbox. This polyfill supports Firefox 66, Chrome 74, and Safari 12.1.

The news of having a tool for running Swift on the web has got many developers excited.

The project is still work-in-progress and thus has some limitations. Currently, only the Swift ‘stdlib’ is compiled and other libraries such as Foundation or SwiftPM are not included. Few functions such as ‘Optional.Map’ does not work because of the calling convention differences between throwing and non-throwing closures.

If you want to contribute to this project, check out its pull request on Swift’s GitHub repository to know more about its current status. You can try SwiftWasm on its official website.

Read Next

Swift is improving the UI of its generics model with the “reverse generics” system

Swift 5 for Xcode 10.2 is here!

Implementing Dependency Injection in Swift [Tutorial]