-
-
Notifications
You must be signed in to change notification settings - Fork 68
Bump 6.3 Swift toolchain snapshots in test.yml
#702
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
Open
MaxDesiatov
wants to merge
15
commits into
main
Choose a base branch
from
maxd/bump-toolchains
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
83d886f
Bump Swift toolchain snapshots in `test.yml`
MaxDesiatov 7ce78d2
Fix concurrency `@_spi`
MaxDesiatov 9eb38b6
Merge branch 'main' into maxd/bump-toolchains
MaxDesiatov 9714319
Downgrade `main` snapshot to old version
MaxDesiatov 9230c86
Fix 6.3 build error
MaxDesiatov 4c72b42
EmbeddedApp/main.swift: Fix capitalization in print statement
MaxDesiatov 8a7ae30
Add `Examples/EmbeddedConcurrency`
MaxDesiatov ef9ab92
EmbeddedConcurrency: don't use `-c release` for SwiftSyntax
MaxDesiatov 451000d
Fix warnings with untyped throws, fix npm install error
MaxDesiatov be0d11d
Fix formatting
MaxDesiatov 89d4b5c
Bump `build-examples` snapshots to 2026-03-14
MaxDesiatov fc27b84
Use 2026-03-09 for `main` development snapshots
MaxDesiatov 79772bd
Add Swift version check before building examples
MaxDesiatov 5d9aa13
Exercise `await` on `JSPromise/value`
MaxDesiatov 48e8916
Address PR feedback
MaxDesiatov 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
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,23 @@ | ||
| // swift-tools-version:6.0 | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "EmbeddedConcurrency", | ||
| dependencies: [ | ||
| .package(name: "JavaScriptKit", path: "../../") | ||
| ], | ||
| targets: [ | ||
| .executableTarget( | ||
| name: "EmbeddedConcurrencyApp", | ||
| dependencies: [ | ||
| "JavaScriptKit", | ||
| .product(name: "JavaScriptEventLoop", package: "JavaScriptKit"), | ||
| ], | ||
| swiftSettings: [ | ||
| .enableExperimentalFeature("Extern"), | ||
| .swiftLanguageMode(.v5), | ||
| ] | ||
| ) | ||
| ] | ||
| ) |
162 changes: 162 additions & 0 deletions
162
Examples/EmbeddedConcurrency/Sources/EmbeddedConcurrencyApp/App.swift
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,162 @@ | ||
| @preconcurrency import JavaScriptKit | ||
| @preconcurrency import JavaScriptEventLoop | ||
| import _Concurrency | ||
|
|
||
| #if compiler(>=6.3) | ||
| typealias DefaultExecutorFactory = JavaScriptEventLoop | ||
| #endif | ||
|
|
||
| @MainActor var testsPassed = 0 | ||
| @MainActor var testsFailed = 0 | ||
|
|
||
| @MainActor | ||
| func check(_ condition: Bool, _ message: String) { | ||
| let console = JSObject.global.console | ||
| if condition { | ||
| testsPassed += 1 | ||
| _ = console.log("PASS: \(message)") | ||
| } else { | ||
| testsFailed += 1 | ||
| _ = console.log("FAIL: \(message)") | ||
| } | ||
| } | ||
|
|
||
| @main | ||
| struct App { | ||
| @MainActor | ||
| static func main() async throws(JSException) { | ||
| JavaScriptEventLoop.installGlobalExecutor() | ||
|
|
||
| // Test 1: Basic async/await with checked continuation | ||
| let value: Int = await withCheckedContinuation { cont in | ||
| cont.resume(returning: 42) | ||
| } | ||
| check(value == 42, "withCheckedContinuation returns correct value") | ||
|
|
||
| // Test 2: Unsafe continuation | ||
| let value2: Int = await withUnsafeContinuation { cont in | ||
| cont.resume(returning: 7) | ||
| } | ||
| check(value2 == 7, "withUnsafeContinuation returns correct value") | ||
|
|
||
| // Test 3: JSPromise creation and .value await | ||
| let promise = JSPromise(resolver: { resolve in | ||
| resolve(.success(JSValue.number(123))) | ||
| }) | ||
| let result: JSPromise.Result = await withUnsafeContinuation { continuation in | ||
| promise.then( | ||
| success: { | ||
| continuation.resume(returning: .success($0)) | ||
| return JSValue.undefined | ||
| }, | ||
| failure: { | ||
| continuation.resume(returning: .failure($0)) | ||
| return JSValue.undefined | ||
| } | ||
| ) | ||
| } | ||
| if case .success(let val) = result { | ||
| check(val.number == 123, "JSPromise.value resolves correctly") | ||
| } else { | ||
| check(false, "JSPromise.value resolves correctly") | ||
| } | ||
|
|
||
| // Test 4: setTimeout-based delay via JSPromise | ||
| let startTime = JSObject.global.Date.now().number! | ||
| let delayValue: Int = await withUnsafeContinuation { cont in | ||
| _ = JSObject.global.setTimeout!( | ||
| JSOneshotClosure { _ in | ||
| cont.resume(returning: 42) | ||
| return .undefined | ||
| }, | ||
| 100 | ||
| ) | ||
| } | ||
| let elapsed = JSObject.global.Date.now().number! - startTime | ||
| check(delayValue == 42 && elapsed >= 90, "setTimeout delay works (\(elapsed)ms elapsed)") | ||
|
|
||
| // Test 5: Multiple concurrent tasks (using withUnsafeContinuation to avoid nonisolated hop) | ||
| var results: [Int] = [] | ||
| let task1 = Task { return 1 } | ||
| let task2 = Task { return 2 } | ||
| let task3 = Task { return 3 } | ||
| let r1: Int = await withUnsafeContinuation { cont in | ||
| Task { cont.resume(returning: await task1.value) } | ||
| } | ||
| let r2: Int = await withUnsafeContinuation { cont in | ||
| Task { cont.resume(returning: await task2.value) } | ||
| } | ||
| let r3: Int = await withUnsafeContinuation { cont in | ||
| Task { cont.resume(returning: await task3.value) } | ||
| } | ||
| results.append(r1) | ||
| results.append(r2) | ||
| results.append(r3) | ||
| results.sort() | ||
| check(results == [1, 2, 3], "Concurrent tasks all complete") | ||
|
|
||
| // Test 6: Promise chaining with .then | ||
| let chained = JSPromise(resolver: { resolve in | ||
| resolve(.success(JSValue.number(10))) | ||
| }).then(success: { value in | ||
| return JSValue.number(value.number! * 2) | ||
| }).then(success: { value in | ||
| return JSValue.number(value.number! + 5) | ||
| }) | ||
| let chainedResult: JSPromise.Result = await withUnsafeContinuation { continuation in | ||
| chained.then( | ||
| success: { | ||
| continuation.resume(returning: .success($0)) | ||
| return JSValue.undefined | ||
| }, | ||
| failure: { | ||
| continuation.resume(returning: .failure($0)) | ||
| return JSValue.undefined | ||
| } | ||
| ) | ||
| } | ||
| if case .success(let val) = chainedResult { | ||
| check(val.number == 25, "Promise chaining works (10 * 2 + 5 = 25)") | ||
| } else { | ||
| check(false, "Promise chaining should succeed") | ||
| } | ||
|
|
||
| // Test 7: JSPromise.value await (with async resolution) | ||
| let promise2 = JSPromise(resolver: { resolve in | ||
| _ = JSObject.global.setTimeout!( | ||
| JSOneshotClosure { _ in | ||
| resolve(.success(JSValue.number(456))) | ||
| return .undefined | ||
| }, | ||
| 1 | ||
| ) | ||
| }) | ||
| let awaitedValue = try await promise2.value | ||
| check(awaitedValue.number == 456, "JSPromise.value await returns correct value") | ||
|
|
||
| // Test 8: JSPromise.result await (with async resolution) | ||
| let promise3 = JSPromise(resolver: { resolve in | ||
| _ = JSObject.global.setTimeout!( | ||
| JSOneshotClosure { _ in | ||
| resolve(.success(JSValue.number(789))) | ||
| return .undefined | ||
| }, | ||
| 1 | ||
| ) | ||
| }) | ||
| let awaitedResult = await promise3.result | ||
| if case .success(let val) = awaitedResult { | ||
| check(val.number == 789, "JSPromise.result await resolves correctly") | ||
| } else { | ||
| check(false, "JSPromise.result await should succeed") | ||
| } | ||
|
|
||
| // Summary | ||
| let console = JSObject.global.console | ||
| let totalTests = testsPassed + testsFailed | ||
| _ = console.log("TOTAL: \(totalTests) tests, \(testsPassed) passed, \(testsFailed) failed") | ||
| if testsFailed > 0 { | ||
| fatalError("\(testsFailed) test(s) failed") | ||
| } | ||
| } | ||
| } |
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,8 @@ | ||
| #!/bin/bash | ||
| set -euxo pipefail | ||
| package_dir="$(cd "$(dirname "$0")" && pwd)" | ||
| swift package --package-path "$package_dir" \ | ||
| --swift-sdk "${SWIFT_SDK_ID_wasm32_unknown_wasip1:-${SWIFT_SDK_ID:-wasm32-unknown-wasip1}}-embedded" \ | ||
| js --default-platform node | ||
| npm -C "$package_dir/.build/plugins/PackageToJS/outputs/Package" install | ||
| node "$package_dir/run.mjs" |
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,47 @@ | ||
| import { instantiate } from | ||
| "./.build/plugins/PackageToJS/outputs/Package/instantiate.js" | ||
| import { defaultNodeSetup } from | ||
| "./.build/plugins/PackageToJS/outputs/Package/platforms/node.js" | ||
|
|
||
| const EXPECTED_TESTS = 8; | ||
| const TIMEOUT_MS = 30_000; | ||
|
|
||
| // Intercept console.log to capture test output | ||
| const originalLog = console.log; | ||
| let totalLine = null; | ||
| let resolveTotal = null; | ||
| const totalPromise = new Promise((resolve) => { resolveTotal = resolve; }); | ||
| console.log = (...args) => { | ||
| const line = args.join(" "); | ||
| originalLog.call(console, ...args); | ||
| if (line.startsWith("TOTAL:")) { | ||
| totalLine = line; | ||
| resolveTotal(); | ||
| } | ||
| }; | ||
|
|
||
| const options = await defaultNodeSetup(); | ||
| await instantiate(options); | ||
|
|
||
| // Wait for the async main to complete (tests run via microtasks/setTimeout) | ||
| const timeout = new Promise((_, reject) => | ||
| setTimeout(() => reject(new Error("Timed out waiting for test results")), TIMEOUT_MS) | ||
| ); | ||
| try { | ||
| await Promise.race([totalPromise, timeout]); | ||
| } catch (e) { | ||
| originalLog.call(console, `FAIL: ${e.message}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (!totalLine) { | ||
| originalLog.call(console, `FAIL: No test summary found — main() likely exited early`); | ||
| process.exit(1); | ||
| } | ||
| const match = totalLine.match(/TOTAL: (\d+) tests/); | ||
| const ran = match ? parseInt(match[1], 10) : 0; | ||
| if (ran !== EXPECTED_TESTS) { | ||
| originalLog.call(console, | ||
| `FAIL: Expected ${EXPECTED_TESTS} tests but only ${ran} ran`); | ||
| process.exit(1); | ||
| } |
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.
Do we have a better directory for test fixtures like this one?
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.
I haven't found a better place for it, since neither XCTest nor Swift Testing support Embedded Swift, so we can't integrate this sample code with existing tests.
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.
We can add a case to the PackageToJS plugin test