Compile your Swift code into WebAssembly with SwiftWasm

Share

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.

The post Compile your Swift code into WebAssembly with SwiftWasm appeared first on JAXenter.

Source : JAXenter