|
| 1 | +# Swift (Wasm) |
| 2 | + |
| 3 | +[Swift](https://www.swift.org/) is a general-purpose programming language developed by Apple, designed for safety, performance and expressiveness. |
| 4 | + |
| 5 | +In LiveCodes, Swift is compiled to WebAssembly by the Swift compiler itself, running on WebAssembly: `swift-frontend` compiles the source and LLVM's `wasm-ld` links the result, entirely in the browser. No backend is involved. |
| 6 | + |
| 7 | +:::info Note |
| 8 | + |
| 9 | +The native `swiftc` is a driver that spawns the compiler frontend and the linker as separate processes, and WebAssembly cannot spawn processes. Here the two run as separate WebAssembly modules in the same [result page](../features/result.mdx), driven by JavaScript. The toolchain is about 70 MB and is downloaded on the first run, which can take some time. |
| 10 | + |
| 11 | +::: |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Demo: |
| 16 | + |
| 17 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 18 | + |
| 19 | +export const swiftConfig = { |
| 20 | + activeEditor: 'script', |
| 21 | + script: { |
| 22 | + language: 'swift-wasm', |
| 23 | + content: `struct Greeting { |
| 24 | + let name: String |
| 25 | + var message: String { "Hello, \\(name)!" } |
| 26 | +}\n |
| 27 | +for name in ["World", "LiveCodes", "Swift"] { |
| 28 | + print(Greeting(name: name).message) |
| 29 | +}`, |
| 30 | + }, |
| 31 | + mode: 'simple', |
| 32 | + editor: 'auto', |
| 33 | + tools: { |
| 34 | + status: 'full', |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +<LiveCodes config={swiftConfig}></LiveCodes> |
| 39 | + |
| 40 | +### Standard I/O |
| 41 | + |
| 42 | +Swift code runs in the context of the [result page](../features/result.mdx). Standard output (e.g. `print`) is captured and displayed in the [console](../features/console.mdx). Standard input is passed by setting the `livecodes.swift.input` property in JavaScript and is read in the Swift code using `readLine()`. |
| 43 | + |
| 44 | +### Communication with JavaScript |
| 45 | + |
| 46 | +The Swift code runs in the context of the result page. A few helper properties and methods are available in the browser global `livecodes.swift` object: |
| 47 | + |
| 48 | +- `livecodes.swift.input`: The standard input passed to the Swift program. It can be set before the initial run, or passed to `run` for subsequent runs. |
| 49 | +- `livecodes.swift.loaded`: A promise that resolves when the Swift environment (compiler, linker and standard library) is fully loaded (and rejects if it fails to load). Other helpers should be used after this promise resolves. |
| 50 | +- `livecodes.swift.ready`: A boolean that is `true` once the environment is loaded and a run has completed. |
| 51 | +- `livecodes.swift.output`: The standard output of the last run. |
| 52 | +- `livecodes.swift.error`: The compiler diagnostics or runtime error of the last run, if any. |
| 53 | +- `livecodes.swift.exitCode`: The exit code of the last run. |
| 54 | +- `livecodes.swift.run(input)`: Runs the code in the editor, optionally passing standard input, and returns a promise that resolves to `{ output, error, exitCode }`. |
| 55 | + |
| 56 | +```js |
| 57 | +await livecodes.swift.loaded; |
| 58 | +const { output, error, exitCode } = await livecodes.swift.run(livecodes.swift.input); |
| 59 | +console.log(output); |
| 60 | +``` |
| 61 | + |
| 62 | +## Language Info |
| 63 | + |
| 64 | +### Name |
| 65 | + |
| 66 | +`swift-wasm` |
| 67 | + |
| 68 | +### Aliases / Extensions |
| 69 | + |
| 70 | +`swift`, `swift-wasm`, `wasm.swift` |
| 71 | + |
| 72 | +### Editor |
| 73 | + |
| 74 | +`script` |
| 75 | + |
| 76 | +## Compiler |
| 77 | + |
| 78 | +The [Swift compiler](https://github.com/swiftlang/swift) frontend (`swift-frontend`) and LLVM's `wasm-ld` linker, both compiled to WebAssembly and running in the browser. The build is provided by [`swift-toolchain-wasm`](https://github.com/tothambrus11/swift-toolchain-wasm). |
| 79 | + |
| 80 | +### Version |
| 81 | + |
| 82 | +Swift 6.3.3 |
| 83 | + |
| 84 | +## Code Formatting |
| 85 | + |
| 86 | +Not supported. |
| 87 | + |
| 88 | +## Limitations |
| 89 | + |
| 90 | +The compiler runs on WebAssembly, which affects what it can do: |
| 91 | + |
| 92 | +- Programs are single-file. |
| 93 | +- **Macros and compiler plugins are not supported.** Swift runs them as separate processes, and WebAssembly cannot spawn processes. |
| 94 | +- **The Swift-implemented optimizer passes are disabled** because C++ interop is not available for this target. This affects optimization quality, not correctness. |
| 95 | +- **A 4 GiB address space limit applies.** Single-file programs fit comfortably; very large modules may not. |
| 96 | +- The toolchain (about 70 MB) is downloaded on the first run. |
| 97 | + |
| 98 | +## Example Usage |
| 99 | + |
| 100 | +This example passes standard input and runs the code again on each click, using the browser global `livecodes.swift` (also check the code in the HTML editor): |
| 101 | + |
| 102 | +<LiveCodes template="swift-wasm" height="80vh"></LiveCodes> |
| 103 | + |
| 104 | +## Live Reload |
| 105 | + |
| 106 | +By default, new code changes are sent to the result page for re-evaluation without a full page reload, avoiding the need to reinitialize the Swift WebAssembly environment. This behavior can be disabled by adding the code comment `// __livecodes_reload__` to the Swift code, which forces a full page reload. |
| 107 | + |
| 108 | +This comment can be added in the [`hiddenContent` property of the editor](../configuration/configuration-object.mdx#markup) for embedded playgrounds. |
| 109 | + |
| 110 | +## Starter Template |
| 111 | + |
| 112 | +https://livecodes.io/?template=swift-wasm |
| 113 | + |
| 114 | +## Links |
| 115 | + |
| 116 | +- [Swift](https://www.swift.org/) |
| 117 | +- [The Swift Programming Language](https://docs.swift.org/swift-book/) |
| 118 | +- [Swift compiler](https://github.com/swiftlang/swift) |
| 119 | +- [swift-toolchain-wasm](https://github.com/tothambrus11/swift-toolchain-wasm) |
0 commit comments