|
| 1 | +import JavaScriptEventLoop |
| 2 | +import JavaScriptKit |
| 3 | +#if canImport(WASILibc) |
| 4 | +import WASILibc |
| 5 | +#elseif canImport(Darwin) |
| 6 | +import Darwin |
| 7 | +#endif |
| 8 | + |
| 9 | +#if compiler(>=5.5) |
| 10 | + |
| 11 | +func entrypoint() async throws { |
| 12 | + struct E: Error, Equatable { |
| 13 | + let value: Int |
| 14 | + } |
| 15 | + |
| 16 | + try await asyncTest("Task.init value") { |
| 17 | + let handle = Task { 1 } |
| 18 | + try expectEqual(await handle.value, 1) |
| 19 | + } |
| 20 | + |
| 21 | + try await asyncTest("Task.init throws") { |
| 22 | + let handle = Task { |
| 23 | + throw E(value: 2) |
| 24 | + } |
| 25 | + let error = try await expectAsyncThrow(await handle.value) |
| 26 | + let e = try expectCast(error, to: E.self) |
| 27 | + try expectEqual(e, E(value: 2)) |
| 28 | + } |
| 29 | + |
| 30 | + try await asyncTest("await resolved Promise") { |
| 31 | + let p = JSPromise(resolver: { resolve in |
| 32 | + resolve(.success(1)) |
| 33 | + }) |
| 34 | + try await expectEqual(p.value, 1) |
| 35 | + } |
| 36 | + |
| 37 | + try await asyncTest("await rejected Promise") { |
| 38 | + let p = JSPromise(resolver: { resolve in |
| 39 | + resolve(.failure(.number(3))) |
| 40 | + }) |
| 41 | + let error = try await expectAsyncThrow(await p.value) |
| 42 | + let jsValue = try expectCast(error, to: JSValue.self) |
| 43 | + try expectEqual(jsValue, 3) |
| 44 | + } |
| 45 | + |
| 46 | + try await asyncTest("Continuation") { |
| 47 | + let value = await withUnsafeContinuation { cont in |
| 48 | + cont.resume(returning: 1) |
| 49 | + } |
| 50 | + try expectEqual(value, 1) |
| 51 | + |
| 52 | + let error = try await expectAsyncThrow( |
| 53 | + try await withUnsafeThrowingContinuation { (cont: UnsafeContinuation<Never, Error>) in |
| 54 | + cont.resume(throwing: E(value: 2)) |
| 55 | + } |
| 56 | + ) |
| 57 | + let e = try expectCast(error, to: E.self) |
| 58 | + try expectEqual(e.value, 2) |
| 59 | + } |
| 60 | + |
| 61 | + try await asyncTest("Task.sleep(_:)") { |
| 62 | + let start = time(nil) |
| 63 | + await Task.sleep(2_000_000_000) |
| 64 | + let diff = difftime(time(nil), start); |
| 65 | + try expectEqual(diff >= 2, true) |
| 66 | + } |
| 67 | + |
| 68 | + try await asyncTest("Job reordering based on priority") { |
| 69 | + class Context: @unchecked Sendable { |
| 70 | + var completed: [String] = [] |
| 71 | + } |
| 72 | + let context = Context() |
| 73 | + |
| 74 | + // When no priority, they should be ordered by the enqueued order |
| 75 | + let t1 = Task(priority: nil) { |
| 76 | + context.completed.append("t1") |
| 77 | + } |
| 78 | + let t2 = Task(priority: nil) { |
| 79 | + context.completed.append("t2") |
| 80 | + } |
| 81 | + |
| 82 | + _ = await (t1.value, t2.value) |
| 83 | + try expectEqual(context.completed, ["t1", "t2"]) |
| 84 | + |
| 85 | + context.completed = [] |
| 86 | + // When high priority is enqueued after a low one, they should be re-ordered |
| 87 | + let t3 = Task(priority: .low) { |
| 88 | + context.completed.append("t3") |
| 89 | + } |
| 90 | + let t4 = Task(priority: .high) { |
| 91 | + context.completed.append("t4") |
| 92 | + } |
| 93 | + let t5 = Task(priority: .low) { |
| 94 | + context.completed.append("t5") |
| 95 | + } |
| 96 | + |
| 97 | + _ = await (t3.value, t4.value, t5.value) |
| 98 | + try expectEqual(context.completed, ["t4", "t3", "t5"]) |
| 99 | + } |
| 100 | + // FIXME(katei): Somehow it doesn't work due to a mysterious unreachable inst |
| 101 | + // at the end of thunk. |
| 102 | + // This issue is not only on JS host environment, but also on standalone coop executor. |
| 103 | + // try await asyncTest("Task.sleep(nanoseconds:)") { |
| 104 | + // try await Task.sleep(nanoseconds: 1_000_000_000) |
| 105 | + // } |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +// Note: Please define `USE_SWIFT_TOOLS_VERSION_NEWER_THAN_5_5` if the swift-tools-version is newer |
| 110 | +// than 5.5 to avoid the linking issue. |
| 111 | +#if USE_SWIFT_TOOLS_VERSION_NEWER_THAN_5_5 |
| 112 | +// Workaround: The latest SwiftPM rename main entry point name of executable target |
| 113 | +// to avoid conflicting "main" with test target since `swift-tools-version >= 5.5`. |
| 114 | +// The main symbol is renamed to "{{module_name}}_main" and it's renamed again to be |
| 115 | +// "main" when linking the executable target. The former renaming is done by Swift compiler, |
| 116 | +// and the latter is done by linker, so SwiftPM passes some special linker flags for each platform. |
| 117 | +// But SwiftPM assumes that wasm-ld supports it by returning an empty array instead of nil even though |
| 118 | +// wasm-ld doesn't support it yet. |
| 119 | +// ref: https://github.com/apple/swift-package-manager/blob/1be68e811d0d814ba7abbb8effee45f1e8e6ec0d/Sources/Build/BuildPlan.swift#L117-L126 |
| 120 | +// So define an explicit "main" by @_cdecl |
| 121 | +@_cdecl("main") |
| 122 | +func main(argc: Int32, argv: Int32) -> Int32 { |
| 123 | + JavaScriptEventLoop.installGlobalExecutor() |
| 124 | + Task { |
| 125 | + do { |
| 126 | + try await entrypoint() |
| 127 | + } catch { |
| 128 | + print(error) |
| 129 | + } |
| 130 | + } |
| 131 | + return 0 |
| 132 | +} |
| 133 | +#else |
| 134 | +JavaScriptEventLoop.installGlobalExecutor() |
| 135 | +Task { |
| 136 | + do { |
| 137 | + try await entrypoint() |
| 138 | + } catch { |
| 139 | + print(error) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#endif |
| 144 | + |
| 145 | + |
| 146 | +#endif |
0 commit comments