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.
You can now compile Swift to WebAssembly to run on the Web.
Try it right now in your browser at https://t.co/k745Z491lr!
Want to help? Join us on GitHub: https://t.co/kqP0HeONsW— 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
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.
This is really cool:@SwiftWasm – Compile your Swift code to WebAssembly and run it on your browser: https://t.co/TOzc3z5c1U pic.twitter.com/fpkCaoWZhf
— Pedro José Pereira Vieito (@pvieito) May 12, 2019
Wow, that is amazing. SwiftWasm! Seriously amazing work @maxdesiatov and co: https://t.co/BsNjcpHNzL
— 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.
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