days
-7
-1
hours
-2
-2
minutes
-3
-5
seconds
-1
-7
search
Input/output

Compile your Swift code into WebAssembly with SwiftWasm

Sarah Schlothauer
swiftwasm
© Shutterstock / ASakoulis

What happens when you bring Swift and WebAssembly together? Meet SwiftWasm: a work in progress that aims to compile your Swift code to WebAssembly. The aim of this project is so that developers may create Swift apps that run in the browser. There are plenty of issues that need fixing and hurdles to overcome; could you help with its development?

Even though WebAssembly is fairly new in the programming world (first announced in 2015), it’s quickly created a large amount of buzz and related projects. Add another toolchain to the board. Now, with the release of SwiftWasm, you can compile your Swift code to WebAssembly.

This project is still new and has room to grow and mature.

Compiling code

This project compiles Swift code into WebAssembly, so that developers can create Swift apps that run in the browser.

Why WebAssembly and makes it different from other platforms? From a GitHub pull request for Swift:

WebAssembly is a new platform, with unique attributes that pose issues to Swift’s runtime.

  • Functions have strict argument checking
    • Swift often calls functions with extra arguments
    • couldn’t find a way to fix this
    • so eg Optional.Map crashes because tries to call non-throwing closure with an extra error pointer
  • Limited relocation support in linker: can’t take difference between two symbols
    • Metadata relies on this. Solution: switch to absolute pointers
    • Not the first port that required this
    • We would like to find a way to merge absolute metadata support
    • or find a different workaround
    • eg only emitting Bitcode for LTO, avoiding intermediate .wasm files

You can test it out for yourself with the live demo on https://swiftwasm.org/.Compile Swift in the cloud and run in your browser.

It is downloadable for Windows systems. If using Linux, you will require a Windows Subsystem.

SEE ALSO: JAX DevOps 2019 takeaways: Security pitfalls and how to influence DevOps culture

Future project goals

Since this project is so new, SwiftWasm is not fully realized yet. As of writing this, many of Swift’s features do not function with WebAssembly. Want to help with development and speed up the process? Refer to this pull request on GitHub for more information about what needs work.

SEE ALSO: Lucet is the native WebAssembly compiler that meets your security needs

According to the pull request so far “Hello World” works, as well as “a large subset of the stdlib already works on WebAssembly”.

As for long-term project goals, here’s what the pull request states when looking forward to the future:

  • Get Swift’s other libraries (such as Foundation) working with WebAssembly (if possible). This includes several language-specific obstacles that are being discussed on how to overcome.
  • Support link-time optimization
  • Interop with JavaScript from Swift

For the current release version, so far these are the known issues:

  • Only the Swift stdlib is compiled: other libraries such as Foundation or SwiftPM are not included.
  • Some functions (such as Optional.Map) do not work due to calling convention differences between throwing and non-throwing closures

 Find SwiftWasm on GitHub.

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of