|
| 1 | +--- |
| 2 | +date: 2021-12-11 14:34 |
| 3 | +description: An update on what happened in the SwiftWasm ecosystem during December 2020. |
| 4 | +--- |
| 5 | +# What's new in SwiftWasm #5 |
| 6 | + |
| 7 | +Happy New Year everyone! Here's a digest of what happened with [SwiftWasm](https://swiftwasm.org) in |
| 8 | +December of 2020, published with a slight delay (it's 2021 already after all 😅). |
| 9 | + |
| 10 | +## SwiftWasm community |
| 11 | + |
| 12 | +One thing we forgot to mention in our November update is that the SwiftWasm community now has |
| 13 | +[its own Discord server](https://discord.gg/ashJW8T8yp). In case you prefer Slack to Discord, we |
| 14 | +recommend you to join the `#webassembly` channel in [the SwiftPM Slack |
| 15 | +workspace](https://swift-package-manager.herokuapp.com/). |
| 16 | + |
| 17 | +In December we saw a lot of projects built with SwiftWasm shared by the community. Here a few most |
| 18 | +noteworthy: |
| 19 | + |
| 20 | +* [An online Multi-Player Meme Party Game written in Swift](https://github.com/nerdsupremacist/memes), |
| 21 | +using [Vapor](https://vapor.codes/) for backend and SwiftWasm with |
| 22 | +[Tokamak](https://tokamak.dev) for frontend, built by [Mathias Quintero](https://github.com/nerdsupremacist). |
| 23 | +* [Tic-tac-toe game](https://garrepi.dev/tic-tac-toe/) [built with SwiftWasm](https://github.com/johngarrett/tic-tac-toe) by [John Garrett](https://github.com/johngarrett). |
| 24 | +* [WebAssembly example with React and SwiftWasm](https://expressflow.com/blog/posts/webassembly-example-with-react-and-swiftwasm) by |
| 25 | +[Martin Vasko](https://github.com/martinvasko). |
| 26 | + |
| 27 | +## Documentation |
| 28 | + |
| 29 | +### SwiftWasm book |
| 30 | + |
| 31 | +We're working on tracking down all the possible edge cases when porting code from other |
| 32 | +platforms in [the SwiftWasm book](https://book.swiftwasm.org/). Previously we were asked how to port |
| 33 | +code that depends on the `Darwin` module on Apple platforms or `Glibc` on Linux. We recommend |
| 34 | +using the `WASILibc` module, which obviously somewhat differs from libc on other platforms. We've |
| 35 | +added [a corresponding note clarifying this](https://book.swiftwasm.org/getting-started/libc.html) |
| 36 | +to the book. |
| 37 | + |
| 38 | +## Toolchain |
| 39 | + |
| 40 | +As the Swift team already announced [the release process for the 5.4 |
| 41 | +version](https://forums.swift.org/t/swift-5-4-release-process/41936), we started preparing our |
| 42 | +corresponding SwiftWasm 5.4 release. The [`swiftwasm-release/5.4`](https://github.com/swiftwasm/swift/tree/swiftwasm-release/5.4) |
| 43 | +branch in our fork now [tracks the upstream `release/5.4` |
| 44 | +branch](https://github.com/swiftwasm/swift/pull/2380), as we plan to tag our own 5.4.0 later |
| 45 | +this year. |
| 46 | + |
| 47 | +Additionally, we made sure that the fork of our toolchain |
| 48 | +[can be compiled on Apple Silicon Macs](https://github.com/swiftwasm/swift/pull/2405). While GitHub |
| 49 | +Actions doesn't provide CI agents for the M1 architecture, the SwiftWasm toolchain can be built |
| 50 | +locally for Apple Silicon, and we hope to provide a prebuilt distribution archive for it in some |
| 51 | +future release. |
| 52 | + |
| 53 | +### Experimental async/await support |
| 54 | + |
| 55 | +[@kateinoigakukun](https://github.com/kateinoigakukun) enabled [experimental concurrency support |
| 56 | +in SwiftWasm](https://github.com/swiftwasm/swift/pull/2408) and fixed several issues that |
| 57 | +previously prevented us from enabling `async`/`await` in development snapshots. Currently, starting with |
| 58 | +[swift-wasm-DEVELOPMENT-SNAPSHOT-2021-01-02-a](https://github.com/swiftwasm/swift/releases/tag/swift-wasm-DEVELOPMENT-SNAPSHOT-2021-01-02-a), |
| 59 | +the toolchain only supports a single-threaded task executor. This executor is suitable for usage in |
| 60 | +standalone WASI hosts such as Wasmer or Wasmtime. Unfortunately, it blocks the JavaScript event loop |
| 61 | +until all jobs are completed, and is unable to run any jobs created after the first event loop |
| 62 | +cycle. While this makes it unsuitable for JavaScript environments, we were able to work around that |
| 63 | +in JavaScriptKit as discussed in the next section. |
| 64 | + |
| 65 | +## Libraries |
| 66 | + |
| 67 | +### JavaScriptKit |
| 68 | + |
| 69 | +[@kateinoigakukun](https://github.com/kateinoigakukun) started implementing [a Swift concurrency |
| 70 | +task executor](https://github.com/swiftwasm/JavaScriptKit/pull/112) integrated with the JavaScript |
| 71 | +event loop. There are still several issues, but it's working well as a proof of concept. This |
| 72 | +experimental API allows us to utilize `async`/`await` in SwiftWasm apps for browsers and Node.js |
| 73 | +like this: |
| 74 | + |
| 75 | +```swift |
| 76 | +import JavaScriptEventLoop |
| 77 | +import JavaScriptKit |
| 78 | + |
| 79 | +JavaScriptEventLoop.install() |
| 80 | +let fetch = JSObject.global.fetch.function!.async |
| 81 | + |
| 82 | +func printZen() async { |
| 83 | + let result = try! await fetch("https://api.github.com/zen").object! |
| 84 | + let text = try! await result.asyncing.text!() |
| 85 | + print(text) |
| 86 | +} |
| 87 | + |
| 88 | +JavaScriptEventLoop.runAsync { |
| 89 | + await printZen() |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### WebAssembly Micro Runtime |
| 94 | + |
| 95 | +[@kateinoigakukun](https://github.com/kateinoigakukun) published [a Swift WebAssembly runtime |
| 96 | +package](https://github.com/swiftwasm/wamr-swift) powered by [the WebAssembly Micro Runtime |
| 97 | +project](https://github.com/bytecodealliance/wasm-micro-runtime) (WAMR). This allows us to remove Wasmer |
| 98 | +dependency from `carton` and embed the WebAssembly runtime in the `carton` binary for use in |
| 99 | +commands such as `carton test`. |
| 100 | + |
| 101 | +We were also able to test this package on Apple Silicon and submit [a PR |
| 102 | +upstream](https://github.com/bytecodealliance/wasm-micro-runtime/pull/480) to make it work. |
| 103 | + |
| 104 | +### DOMKit |
| 105 | + |
| 106 | +Our long-term goal is to make [DOMKit](https://github.com/swiftwasm/DOMKit) the recommended library |
| 107 | +for type-safe interactions with Web APIs in SwiftWasm. While it's still at an early stage, we've |
| 108 | +updated it to [JavaScriptKit 0.9 and added support for `globalThis`](https://github.com/swiftwasm/DOMKit/pull/3). |
| 109 | +[In a separate PR](https://github.com/swiftwasm/DOMKit/pull/4) we've cleaned up unused code and |
| 110 | +fixed an event handlers crash. |
| 111 | + |
| 112 | +### Tokamak |
| 113 | + |
| 114 | +With enough changes to warrant a new release, we've published [Tokamak |
| 115 | +0.6.0](https://github.com/TokamakUI/Tokamak/releases/tag/0.6.0), which introduced support for |
| 116 | +the `Image` view loading images bundled as SwiftPM resources, implemented by |
| 117 | +[@j-f1](https://github.com/j-f1). It also adds the `PreferenceKey` protocol and related modifiers. |
| 118 | +developed by [@carson-katri](https://github.com/carson-katri). |
| 119 | + |
| 120 | +Since then we've also added [the `PreviewProvider` protocol](https://github.com/TokamakUI/Tokamak/pull/328), |
| 121 | +[an implementation of `TextEditor`](https://github.com/TokamakUI/Tokamak/pull/329), and updated |
| 122 | +our [example code in `README.md` for script injection](https://github.com/TokamakUI/Tokamak/pull/332). |
| 123 | +Additionally, a contribution from [David Hunt](https://github.com/foscomputerservices) [added |
| 124 | +missing typealiases to TokamakDOM](https://github.com/TokamakUI/Tokamak/pull/331) that should improve |
| 125 | +compatibility with SwiftUI, while [Jed Fox](https://github.com/j-f1) removed [redundant `path` element |
| 126 | +from SVG output](https://github.com/TokamakUI/Tokamak/pull/341). |
| 127 | + |
| 128 | +## Developer tools |
| 129 | + |
| 130 | +### `carton` |
| 131 | + |
| 132 | +We'd like to welcome [@thecb4](https://github.com/thecb4), who is the latest addition to the `carton` |
| 133 | +maintainers team! Thanks to his work, [project targets were restructured for better |
| 134 | +testability](https://github.com/swiftwasm/carton/pull/191), and |
| 135 | +now [`test`](https://github.com/swiftwasm/carton/pull/198), [`dev`, and `bundle`](https://github.com/swiftwasm/carton/pull/196) |
| 136 | +commands are covered with end-to-end tests. |
| 137 | + |
| 138 | +There's [ongoing work](https://github.com/swiftwasm/carton/pull/195) to integrate [the WAMR |
| 139 | +package](https://github.com/swiftwasm/wamr-swift) mentioned above with the `carton test` command. |
| 140 | +Also, `carton` now [correctly handles system target dependencies |
| 141 | +in `Package.swift`](https://github.com/swiftwasm/carton/pull/189). |
| 142 | + |
| 143 | +On top of that, stack traces from |
| 144 | +Chrome and Safari are [now supported in `carton dev` with proper symbol |
| 145 | +demangling](https://github.com/swiftwasm/carton/pull/186), thanks to the work by [@j-f1](https://github.com/j-f1). |
| 146 | +Additionally, [@yonihemi](https://github.com/yonihemi) submitted [a PR which integrates `carton` |
| 147 | +with libSwiftPM](https://github.com/swiftwasm/carton/pull/194) allowing us to reuse its model types. |
| 148 | + |
| 149 | +Most of these changes were included in [0.9.0](https://github.com/swiftwasm/carton/releases/tag/0.9.0) |
| 150 | +and [0.9.1](https://github.com/swiftwasm/carton/releases/tag/0.9.1) releases published in December. |
| 151 | + |
| 152 | +### `webidl2swift` |
| 153 | + |
| 154 | +[`webidl2swift`](https://github.com/Apodini/webidl2swift) is the foundation on which |
| 155 | +our [DOMKit](https://github.com/swiftwasm/DOMKit/) framework is built. Web API across all browsers is specified in |
| 156 | +[the Web IDL format](https://en.wikipedia.org/wiki/Web_IDL), which can then be parsed to generate |
| 157 | +type-safe bindings in Swift. The parser doesn't support all IDL files out there yet, but we've |
| 158 | +[updated dependencies and updated the code |
| 159 | +generator](https://github.com/Apodini/webidl2swift/pull/10) to certain JavaScriptKit types that |
| 160 | +previously were deprecated. |
| 161 | + |
| 162 | +### WasmTransformer |
| 163 | + |
| 164 | +There were a few changes to [the WasmTransformer package](https://github.com/swiftwasm/WasmTransformer), |
| 165 | +which we use in `carton` and intend to use in a few other dev tools. Specifically, we've [generalized |
| 166 | +section info parsing](https://github.com/swiftwasm/WasmTransformer/pull/12) across different transformers, |
| 167 | +[implemented a basic section size profiler](https://github.com/swiftwasm/WasmTransformer/pull/14), |
| 168 | +and [made the `TypeSection` type public](https://github.com/swiftwasm/WasmTransformer/pull/15) to |
| 169 | +make it easier to analyze sections of WebAssembly binaries. |
| 170 | + |
| 171 | +### Gravity |
| 172 | + |
| 173 | +We're excited to announce our new developer tool enabled by WasmTransformer. |
| 174 | +[Gravity](https://github.com/swiftwasm/gravity) is a binary code size profiler for WebAssembly built |
| 175 | +with WasmTransformer, [SwiftUI](https://developer.apple.com/xcode/swiftui/), and |
| 176 | +[TCA](https://github.com/pointfreeco/swift-composable-architecture/). It's an application for macOS |
| 177 | +that allows you to open a WebAssembly binary and view the size of different sections. Contents of |
| 178 | +some of the sections is also parsed for further analysis. |
| 179 | + |
| 180 | +## Contributions |
| 181 | + |
| 182 | +A lot of our progress with SwiftWasm wouldn't be possible without payments from our GitHub Sponsors. |
| 183 | +Their contribution is deeply appreciated and allows us to spend more time on SwiftWasm projects. You can |
| 184 | +see the list of sponsors and make your contribution on [our SwiftWasm organization sponsorship |
| 185 | +page](https://github.com/sponsors/swiftwasm), or personal sponsorship pages of [Carson |
| 186 | +Katri](https://github.com/sponsors/carson-katri), [Yuta |
| 187 | +Saito](https://github.com/sponsors/kateinoigakukun) and [Max |
| 188 | +Desiatov](https://github.com/sponsors/MaxDesiatov). |
| 189 | + |
| 190 | +Thanks for reading! 👋 |
0 commit comments