Repository navigation
add Swift language support #1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b39b38e
feat(Compilers): add Swift language support
hatemhosny fc9f6b6
i18n: add non-English translations for Swift (Wasm)
pullfrog[bot] 9299f9c
fix(swift-wasm): reject loaded on boot failure and recover runtime on…
pullfrog[bot] 8e5405e
i18n: add swiftWasm.link key and translations for the Swift tooltip l…
pullfrog[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Swift (Wasm) | ||
|
|
||
| [Swift](https://www.swift.org/) is a general-purpose programming language developed by Apple, designed for safety, performance and expressiveness. | ||
|
|
||
| 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. | ||
|
|
||
| :::info Note | ||
|
|
||
| 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. | ||
|
|
||
| ::: | ||
|
|
||
| ## Usage | ||
|
|
||
| Demo: | ||
|
|
||
| import LiveCodes from '../../src/components/LiveCodes.tsx'; | ||
|
|
||
| export const swiftConfig = { | ||
| activeEditor: 'script', | ||
| script: { | ||
| language: 'swift-wasm', | ||
| content: `struct Greeting { | ||
| let name: String | ||
| var message: String { "Hello, \\(name)!" } | ||
| }\n | ||
| for name in ["World", "LiveCodes", "Swift"] { | ||
| print(Greeting(name: name).message) | ||
| }`, | ||
| }, | ||
| mode: 'simple', | ||
| editor: 'auto', | ||
| tools: { | ||
| status: 'full', | ||
| }, | ||
| }; | ||
|
|
||
| <LiveCodes config={swiftConfig}></LiveCodes> | ||
|
|
||
| ### Standard I/O | ||
|
|
||
| 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()`. | ||
|
|
||
| ### Communication with JavaScript | ||
|
|
||
| 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: | ||
|
|
||
| - `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. | ||
| - `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. | ||
| - `livecodes.swift.ready`: A boolean that is `true` once the environment is loaded and a run has completed. | ||
| - `livecodes.swift.output`: The standard output of the last run. | ||
| - `livecodes.swift.error`: The compiler diagnostics or runtime error of the last run, if any. | ||
| - `livecodes.swift.exitCode`: The exit code of the last run. | ||
| - `livecodes.swift.run(input)`: Runs the code in the editor, optionally passing standard input, and returns a promise that resolves to `{ output, error, exitCode }`. | ||
|
|
||
| ```js | ||
| await livecodes.swift.loaded; | ||
| const { output, error, exitCode } = await livecodes.swift.run(livecodes.swift.input); | ||
| console.log(output); | ||
| ``` | ||
|
|
||
| ## Language Info | ||
|
|
||
| ### Name | ||
|
|
||
| `swift-wasm` | ||
|
|
||
| ### Aliases / Extensions | ||
|
|
||
| `swift`, `swift-wasm`, `wasm.swift` | ||
|
|
||
| ### Editor | ||
|
|
||
| `script` | ||
|
|
||
| ## Compiler | ||
|
|
||
| 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). | ||
|
|
||
| ### Version | ||
|
|
||
| Swift 6.3.3 | ||
|
|
||
| ## Code Formatting | ||
|
|
||
| Not supported. | ||
|
|
||
| ## Limitations | ||
|
|
||
| The compiler runs on WebAssembly, which affects what it can do: | ||
|
|
||
| - Programs are single-file. | ||
| - **Macros and compiler plugins are not supported.** Swift runs them as separate processes, and WebAssembly cannot spawn processes. | ||
| - **The Swift-implemented optimizer passes are disabled** because C++ interop is not available for this target. This affects optimization quality, not correctness. | ||
| - **A 4 GiB address space limit applies.** Single-file programs fit comfortably; very large modules may not. | ||
| - The toolchain (about 70 MB) is downloaded on the first run. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| 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): | ||
|
|
||
| <LiveCodes template="swift-wasm" height="80vh"></LiveCodes> | ||
|
|
||
| ## Live Reload | ||
|
|
||
| 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. | ||
|
|
||
| This comment can be added in the [`hiddenContent` property of the editor](../configuration/configuration-object.mdx#markup) for embedded playgrounds. | ||
|
|
||
| ## Starter Template | ||
|
|
||
| https://livecodes.io/?template=swift-wasm | ||
|
|
||
| ## Links | ||
|
|
||
| - [Swift](https://www.swift.org/) | ||
| - [The Swift Programming Language](https://docs.swift.org/swift-book/) | ||
| - [Swift compiler](https://github.com/swiftlang/swift) | ||
| - [swift-toolchain-wasm](https://github.com/tothambrus11/swift-toolchain-wasm) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: live-codes/livecodes
Length of output: 12040
🏁 Script executed:
Repository: live-codes/livecodes
Length of output: 2349
Set the whole-test timeout above the first-download budget.
playwright.config.tssets the test timeout to 60 seconds.test.slow()increases it to 180 seconds, but the Swift test allows 280 seconds for the first download. Set the timeout explicitly above 280 seconds.Suggested fix
test.slow(); + test.setTimeout(300_000);📝 Committable suggestion
🤖 Prompt for AI Agents