|
| 1 | +@preconcurrency import JavaScriptKit |
| 2 | +@preconcurrency import JavaScriptEventLoop |
| 3 | +import _Concurrency |
| 4 | + |
| 5 | +#if compiler(>=6.3) |
| 6 | +typealias DefaultExecutorFactory = JavaScriptEventLoop |
| 7 | +#endif |
| 8 | + |
| 9 | +nonisolated(unsafe) var testsPassed = 0 |
| 10 | +nonisolated(unsafe) var testsFailed = 0 |
| 11 | + |
| 12 | +@MainActor |
| 13 | +func check(_ condition: Bool, _ message: String) { |
| 14 | + let console = JSObject.global.console |
| 15 | + if condition { |
| 16 | + testsPassed += 1 |
| 17 | + _ = console.log("PASS: \(message)") |
| 18 | + } else { |
| 19 | + testsFailed += 1 |
| 20 | + _ = console.log("FAIL: \(message)") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +@main |
| 25 | +struct App { |
| 26 | + static func main() async throws(JSException) { |
| 27 | + JavaScriptEventLoop.installGlobalExecutor() |
| 28 | + |
| 29 | + // Test 1: Basic async/await with checked continuation |
| 30 | + let value: Int = await withCheckedContinuation { cont in |
| 31 | + cont.resume(returning: 42) |
| 32 | + } |
| 33 | + check(value == 42, "withCheckedContinuation returns correct value") |
| 34 | + |
| 35 | + // Test 2: Unsafe continuation |
| 36 | + let value2: Int = await withUnsafeContinuation { cont in |
| 37 | + cont.resume(returning: 7) |
| 38 | + } |
| 39 | + check(value2 == 7, "withUnsafeContinuation returns correct value") |
| 40 | + |
| 41 | + // Test 3: JSPromise creation and .value await |
| 42 | + let promise = JSPromise(resolver: { resolve in |
| 43 | + resolve(.success(JSValue.number(123))) |
| 44 | + }) |
| 45 | + let result: JSPromise.Result = await withUnsafeContinuation { continuation in |
| 46 | + promise.then( |
| 47 | + success: { |
| 48 | + continuation.resume(returning: .success($0)) |
| 49 | + return JSValue.undefined |
| 50 | + }, |
| 51 | + failure: { |
| 52 | + continuation.resume(returning: .failure($0)) |
| 53 | + return JSValue.undefined |
| 54 | + } |
| 55 | + ) |
| 56 | + } |
| 57 | + if case .success(let val) = result { |
| 58 | + check(val.number == 123, "JSPromise.value resolves correctly") |
| 59 | + } else { |
| 60 | + check(false, "JSPromise.value resolves correctly") |
| 61 | + } |
| 62 | + |
| 63 | + // Test 4: setTimeout-based delay via JSPromise |
| 64 | + let startTime = JSObject.global.Date.now().number! |
| 65 | + let delayValue: Int = await withUnsafeContinuation { cont in |
| 66 | + _ = JSObject.global.setTimeout!( |
| 67 | + JSOneshotClosure { _ in |
| 68 | + cont.resume(returning: 42) |
| 69 | + return .undefined |
| 70 | + }, |
| 71 | + 100 |
| 72 | + ) |
| 73 | + } |
| 74 | + let elapsed = JSObject.global.Date.now().number! - startTime |
| 75 | + check(delayValue == 42 && elapsed >= 90, "setTimeout delay works (\(elapsed)ms elapsed)") |
| 76 | + |
| 77 | + // Test 5: Multiple concurrent tasks (using withUnsafeContinuation to avoid nonisolated hop) |
| 78 | + var results: [Int] = [] |
| 79 | + let task1 = Task { return 1 } |
| 80 | + let task2 = Task { return 2 } |
| 81 | + let task3 = Task { return 3 } |
| 82 | + let r1: Int = await withUnsafeContinuation { cont in |
| 83 | + Task { cont.resume(returning: await task1.value) } |
| 84 | + } |
| 85 | + let r2: Int = await withUnsafeContinuation { cont in |
| 86 | + Task { cont.resume(returning: await task2.value) } |
| 87 | + } |
| 88 | + let r3: Int = await withUnsafeContinuation { cont in |
| 89 | + Task { cont.resume(returning: await task3.value) } |
| 90 | + } |
| 91 | + results.append(r1) |
| 92 | + results.append(r2) |
| 93 | + results.append(r3) |
| 94 | + results.sort() |
| 95 | + check(results == [1, 2, 3], "Concurrent tasks all complete") |
| 96 | + |
| 97 | + // Test 6: Promise chaining with .then |
| 98 | + let chained = JSPromise(resolver: { resolve in |
| 99 | + resolve(.success(JSValue.number(10))) |
| 100 | + }).then(success: { value in |
| 101 | + return JSValue.number(value.number! * 2) |
| 102 | + }).then(success: { value in |
| 103 | + return JSValue.number(value.number! + 5) |
| 104 | + }) |
| 105 | + let chainedResult: JSPromise.Result = await withUnsafeContinuation { continuation in |
| 106 | + chained.then( |
| 107 | + success: { |
| 108 | + continuation.resume(returning: .success($0)) |
| 109 | + return JSValue.undefined |
| 110 | + }, |
| 111 | + failure: { |
| 112 | + continuation.resume(returning: .failure($0)) |
| 113 | + return JSValue.undefined |
| 114 | + } |
| 115 | + ) |
| 116 | + } |
| 117 | + if case .success(let val) = chainedResult { |
| 118 | + check(val.number == 25, "Promise chaining works (10 * 2 + 5 = 25)") |
| 119 | + } else { |
| 120 | + check(false, "Promise chaining should succeed") |
| 121 | + } |
| 122 | + |
| 123 | + // Summary |
| 124 | + let console = JSObject.global.console |
| 125 | + let totalTests = testsPassed + testsFailed |
| 126 | + _ = console.log("TOTAL: \(totalTests) tests, \(testsPassed) passed, \(testsFailed) failed") |
| 127 | + if testsFailed > 0 { |
| 128 | + fatalError("\(testsFailed) test(s) failed") |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments