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.
— SwiftWasm (@swiftwasm) May 11, 2019
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
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.
— Pedro José Pereira Vieito (@pvieito) May 12, 2019
— Johannes Weiss (@johannesweiss) May 10, 2019
Run Swift in browsers – SwiftWasm compiles your Swift code to WebAssembly.https://t.co/KnLN5HJuBS
— Frank Denis (@jedisct1) May 10, 2019
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.