Attempt at Swift WebAssembly

Β· 851 words Β· 4 minute read

Something was itching iside to try Swift WebAssembly for the Web.

Preface πŸ”—

At my current job, I brought WebAssembly via Emscripten SDK to one of our core products about 3 years ago. Before that I was toying with SDK for some time and following their progress.
During the project, I acquired extensive knowledge and experience on WebAssembly and Emscripten.
Even made an attempt at creating a similar interface to embind for native platforms - jsb. We might extend it and use in Complete Anatomy’s main product.

Attempt 1 πŸ”—

My first look is obviously at SwiftWasm.
There is documentation and it looks promissing at first glance. But most of it directing user to use an older versions of Swift. So, I ignore it and try to use latest swift 6.0.2.

Obviously many things does not work.

Don’t want to spend time writing about it, just don’t make same mistake assuming it might work.

Perhaps with exact versions of swift and system from 2-3 year old, some things might work. As we know Swift toolchain rapidly evolving and relaying on older version is not wise thing to do.

Finally, my advice, don’t spend time on trying to build TokamakUI. Main author and contributor now working at apple for few years. My guess is that the contract with Apple is prohibiting him from continuing work on the project, and/or the focus is mostly on underlying fundamentals, i.e., reducing WASM binary size and improving the ecosystem for WASM.

Attempt 2 πŸ”—

Last summer I heard about Swift on Embedded but did not pay enough attention to the topic. I remember trying it out, installing Swift 6.0 toolchain before its release and attempting to run swift-for-wasm-examples - which did not work for me at the time for some reason. I had no time to continue and put it away.

I decided to make another attempt and build at least a simple hello world with Swift Embedded.
Should be easy enough!
Found this thread on forums.swift.org which has detailed instructions on how to create “Hello world!”.
I followed and it did produce a wokring sample. This is what needs to be placed in the Hello World Web App in the first place for SwiftWasm documentation.

That was fast, but not everything that great as it seems.

Swift Embedded is still in the experimental phase, and developers are simply on their own without any frameworks, i.e. no Foundation or even support for strings. Wild west and full of opportunities to create. The Swift package manager is not ready for embedded packages too. This is something actively being worked on.

I thought to myself, this is okay. I still can make an attempt at compiling Swift files, make them into a library, and link to the Emscripten SDK. Perhaps later I can expose more functions to Swift from the SDK and create something.

This is how emsdk_swift sample project came to live.

img

Size of *.wasm file is tiny, about 20Kb, and majority of symbols from emscripten.

Attempt 3 πŸ”—

Going back to non Embedded Swift for WebAssembly.

While researching, I stumbled across this thread on Swift forums which is exactly pointing out the problems I have run into myself!
How far can i get with Swift WebAssembly in 30 minutes?

Very detailed overview of all problems and tried approaches. TBH, I’m impressed with how the author conveys the problems, very extensive experience and deep thinking behind the scenes.

Down in the thread, new tool Archer mentioned.
Based on the topic, I think the author might have also gotten frustrated with the experience of using the Carton tool.

Archer looks promising and uses Carton JavaScript shim for loading Wasm modules.
The “Hello” sample uses JavaScriptKit and the produced stripped-down WASM file is about 3.9Mb. Which is not bad!
Project is well structured and has everything needed in one place to get started. Only wish is to have a working Swift toolchain with SPM.

Conclusion πŸ”—

This is not a secret and should not be a shocker news. Swift for WebAssembly is still in the early stages but is about to get to the point where it is much more usable for Web without jumping over the hoops and a long list of compiler arguments.

When WASM became a thing for other areas of computing, it also created a vast amount of ways how to package it and interfaces. WASI is on the way to consolidate it. But it is still a long way before it can unleash the full potential of web browsers. So, I don’t think current-day WASM for web should be based on WASI. Perhaps WASI not going to provide tools for classic runtime, yet. Instead, it is currently on developers to solve it, and Swift is not an exception. Swift for Web needs a lightweight SDK which can offer runtime similar to the emscripten approach.

Tools like Tokamak, carton, webber, and similar is a too big jump to make Swift WebAssembly work in a Web browser without addressing conceptually underlying problems.

In general, I’m excited about Swift Embedded Evolution. This is the right approach, starting from small and incrementally improving.

img