From 0b63037c19711829d1c5c558167d803867525d55 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 30 Apr 2025 16:14:41 +0800 Subject: [PATCH 1/3] Split out the letacy hook-based global task executor --- .../JavaScriptEventLoop+LegacyHooks.swift | 107 ++++++++++++++++++ .../JavaScriptEventLoop.swift | 101 +---------------- 2 files changed, 110 insertions(+), 98 deletions(-) create mode 100644 Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift new file mode 100644 index 00000000..d22b0a64 --- /dev/null +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift @@ -0,0 +1,107 @@ +import _CJavaScriptEventLoop +import _CJavaScriptKit + +@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) +extension JavaScriptEventLoop { + + static func installByLegacyHook() { +#if compiler(>=5.9) + typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( + swift_task_asyncMainDrainQueue_original, swift_task_asyncMainDrainQueue_override + ) -> Void + let swift_task_asyncMainDrainQueue_hook_impl: swift_task_asyncMainDrainQueue_hook_Fn = { _, _ in + swjs_unsafe_event_loop_yield() + } + swift_task_asyncMainDrainQueue_hook = unsafeBitCast( + swift_task_asyncMainDrainQueue_hook_impl, + to: UnsafeMutableRawPointer?.self + ) +#endif + + typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnownedJob, swift_task_enqueueGlobal_original) + -> Void + let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in + JavaScriptEventLoop.shared.unsafeEnqueue(job) + } + swift_task_enqueueGlobal_hook = unsafeBitCast( + swift_task_enqueueGlobal_hook_impl, + to: UnsafeMutableRawPointer?.self + ) + + typealias swift_task_enqueueGlobalWithDelay_hook_Fn = @convention(thin) ( + UInt64, UnownedJob, swift_task_enqueueGlobalWithDelay_original + ) -> Void + let swift_task_enqueueGlobalWithDelay_hook_impl: swift_task_enqueueGlobalWithDelay_hook_Fn = { + delay, + job, + original in + JavaScriptEventLoop.shared.enqueue(job, withDelay: delay) + } + swift_task_enqueueGlobalWithDelay_hook = unsafeBitCast( + swift_task_enqueueGlobalWithDelay_hook_impl, + to: UnsafeMutableRawPointer?.self + ) + +#if compiler(>=5.7) + typealias swift_task_enqueueGlobalWithDeadline_hook_Fn = @convention(thin) ( + Int64, Int64, Int64, Int64, Int32, UnownedJob, swift_task_enqueueGlobalWithDelay_original + ) -> Void + let swift_task_enqueueGlobalWithDeadline_hook_impl: swift_task_enqueueGlobalWithDeadline_hook_Fn = { + sec, + nsec, + tsec, + tnsec, + clock, + job, + original in + JavaScriptEventLoop.shared.enqueue(job, withDelay: sec, nsec, tsec, tnsec, clock) + } + swift_task_enqueueGlobalWithDeadline_hook = unsafeBitCast( + swift_task_enqueueGlobalWithDeadline_hook_impl, + to: UnsafeMutableRawPointer?.self + ) +#endif + + typealias swift_task_enqueueMainExecutor_hook_Fn = @convention(thin) ( + UnownedJob, swift_task_enqueueMainExecutor_original + ) -> Void + let swift_task_enqueueMainExecutor_hook_impl: swift_task_enqueueMainExecutor_hook_Fn = { job, original in + JavaScriptEventLoop.shared.unsafeEnqueue(job) + } + swift_task_enqueueMainExecutor_hook = unsafeBitCast( + swift_task_enqueueMainExecutor_hook_impl, + to: UnsafeMutableRawPointer?.self + ) + + } +} + + +#if compiler(>=5.7) +/// Taken from https://github.com/apple/swift/blob/d375c972f12128ec6055ed5f5337bfcae3ec67d8/stdlib/public/Concurrency/Clock.swift#L84-L88 +@_silgen_name("swift_get_time") +internal func swift_get_time( + _ seconds: UnsafeMutablePointer, + _ nanoseconds: UnsafeMutablePointer, + _ clock: CInt +) + +@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) +extension JavaScriptEventLoop { + fileprivate func enqueue( + _ job: UnownedJob, + withDelay seconds: Int64, + _ nanoseconds: Int64, + _ toleranceSec: Int64, + _ toleranceNSec: Int64, + _ clock: Int32 + ) { + var nowSec: Int64 = 0 + var nowNSec: Int64 = 0 + swift_get_time(&nowSec, &nowNSec, clock) + let delayNanosec = (seconds - nowSec) * 1_000_000_000 + (nanoseconds - nowNSec) + enqueue(job, withDelay: delayNanosec <= 0 ? 0 : UInt64(delayNanosec)) + } +} +#endif + diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index d7394a0d..399bcf76 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -119,77 +119,10 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { private static func installGlobalExecutorIsolated() { guard !didInstallGlobalExecutor else { return } didInstallGlobalExecutor = true - - #if compiler(>=5.9) - typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( - swift_task_asyncMainDrainQueue_original, swift_task_asyncMainDrainQueue_override - ) -> Void - let swift_task_asyncMainDrainQueue_hook_impl: swift_task_asyncMainDrainQueue_hook_Fn = { _, _ in - swjs_unsafe_event_loop_yield() - } - swift_task_asyncMainDrainQueue_hook = unsafeBitCast( - swift_task_asyncMainDrainQueue_hook_impl, - to: UnsafeMutableRawPointer?.self - ) - #endif - - typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnownedJob, swift_task_enqueueGlobal_original) - -> Void - let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in - JavaScriptEventLoop.shared.unsafeEnqueue(job) - } - swift_task_enqueueGlobal_hook = unsafeBitCast( - swift_task_enqueueGlobal_hook_impl, - to: UnsafeMutableRawPointer?.self - ) - - typealias swift_task_enqueueGlobalWithDelay_hook_Fn = @convention(thin) ( - UInt64, UnownedJob, swift_task_enqueueGlobalWithDelay_original - ) -> Void - let swift_task_enqueueGlobalWithDelay_hook_impl: swift_task_enqueueGlobalWithDelay_hook_Fn = { - delay, - job, - original in - JavaScriptEventLoop.shared.enqueue(job, withDelay: delay) - } - swift_task_enqueueGlobalWithDelay_hook = unsafeBitCast( - swift_task_enqueueGlobalWithDelay_hook_impl, - to: UnsafeMutableRawPointer?.self - ) - - #if compiler(>=5.7) - typealias swift_task_enqueueGlobalWithDeadline_hook_Fn = @convention(thin) ( - Int64, Int64, Int64, Int64, Int32, UnownedJob, swift_task_enqueueGlobalWithDelay_original - ) -> Void - let swift_task_enqueueGlobalWithDeadline_hook_impl: swift_task_enqueueGlobalWithDeadline_hook_Fn = { - sec, - nsec, - tsec, - tnsec, - clock, - job, - original in - JavaScriptEventLoop.shared.enqueue(job, withDelay: sec, nsec, tsec, tnsec, clock) - } - swift_task_enqueueGlobalWithDeadline_hook = unsafeBitCast( - swift_task_enqueueGlobalWithDeadline_hook_impl, - to: UnsafeMutableRawPointer?.self - ) - #endif - - typealias swift_task_enqueueMainExecutor_hook_Fn = @convention(thin) ( - UnownedJob, swift_task_enqueueMainExecutor_original - ) -> Void - let swift_task_enqueueMainExecutor_hook_impl: swift_task_enqueueMainExecutor_hook_Fn = { job, original in - JavaScriptEventLoop.shared.unsafeEnqueue(job) - } - swift_task_enqueueMainExecutor_hook = unsafeBitCast( - swift_task_enqueueMainExecutor_hook_impl, - to: UnsafeMutableRawPointer?.self - ) + installByLegacyHook() } - private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { + internal func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { let milliseconds = nanoseconds / 1_000_000 setTimeout( Double(milliseconds), @@ -203,7 +136,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { ) } - private func unsafeEnqueue(_ job: UnownedJob) { + internal func unsafeEnqueue(_ job: UnownedJob) { #if canImport(wasi_pthread) && compiler(>=6.1) && _runtime(_multithreaded) guard swjs_get_worker_thread_id_cached() == SWJS_MAIN_THREAD_ID else { // Notify the main thread to execute the job when a job is @@ -237,34 +170,6 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { } } -#if compiler(>=5.7) -/// Taken from https://github.com/apple/swift/blob/d375c972f12128ec6055ed5f5337bfcae3ec67d8/stdlib/public/Concurrency/Clock.swift#L84-L88 -@_silgen_name("swift_get_time") -internal func swift_get_time( - _ seconds: UnsafeMutablePointer, - _ nanoseconds: UnsafeMutablePointer, - _ clock: CInt -) - -@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) -extension JavaScriptEventLoop { - fileprivate func enqueue( - _ job: UnownedJob, - withDelay seconds: Int64, - _ nanoseconds: Int64, - _ toleranceSec: Int64, - _ toleranceNSec: Int64, - _ clock: Int32 - ) { - var nowSec: Int64 = 0 - var nowNSec: Int64 = 0 - swift_get_time(&nowSec, &nowNSec, clock) - let delayNanosec = (seconds - nowSec) * 1_000_000_000 + (nanoseconds - nowNSec) - enqueue(job, withDelay: delayNanosec <= 0 ? 0 : UInt64(delayNanosec)) - } -} -#endif - @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) extension JSPromise { /// Wait for the promise to complete, returning (or throwing) its result. From cf93244b8ce54cf5d5812f96dcc21f6b1eee16f2 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 30 Apr 2025 17:16:56 +0800 Subject: [PATCH 2/3] Use the new `ExecutorFactory` protocol to provide a default executor --- .../JavaScriptEventLoop+ExecutorFactory.swift | 91 +++++++++++++++++++ .../JavaScriptEventLoop+LegacyHooks.swift | 27 +++--- .../JavaScriptEventLoop.swift | 13 ++- .../WebWorkerTaskExecutorTests.swift | 4 +- 4 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift new file mode 100644 index 00000000..d008ea67 --- /dev/null +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift @@ -0,0 +1,91 @@ +// Implementation of custom executors for JavaScript event loop +// This file implements the ExecutorFactory protocol to provide custom main and global executors +// for Swift concurrency in JavaScript environment. +// See: https://github.com/swiftlang/swift/pull/80266 +// See: https://forums.swift.org/t/pitch-2-custom-main-and-global-executors/78437 + +import _CJavaScriptKit + +#if compiler(>=6.2) + +// MARK: - MainExecutor Implementation +// MainExecutor is used by the main actor to execute tasks on the main thread +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *) +extension JavaScriptEventLoop: MainExecutor { + public func run() throws { + // This method is called from `swift_task_asyncMainDrainQueueImpl`. + // https://github.com/swiftlang/swift/blob/swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a/stdlib/public/Concurrency/ExecutorImpl.swift#L28 + // Yield control to the JavaScript event loop to skip the `exit(0)` + // call by `swift_task_asyncMainDrainQueueImpl`. + swjs_unsafe_event_loop_yield() + } + public func stop() {} +} + +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) +extension JavaScriptEventLoop: TaskExecutor {} + +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *) +extension JavaScriptEventLoop: SchedulableExecutor { + public func enqueue( + _ job: consuming ExecutorJob, + after delay: C.Duration, + tolerance: C.Duration?, + clock: C + ) { + let milliseconds = Self.delayInMilliseconds(from: delay, clock: clock) + self.enqueue( + UnownedJob(job), + withDelay: milliseconds + ) + } + + private static func delayInMilliseconds(from duration: C.Duration, clock: C) -> Double { + let swiftDuration = clock.convert(from: duration)! + let (seconds, attoseconds) = swiftDuration.components + return Double(seconds) * 1_000 + (Double(attoseconds) / 1_000_000_000_000_000) + } +} + +// MARK: - ExecutorFactory Implementation +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *) +extension JavaScriptEventLoop: ExecutorFactory { + // Forward all operations to the current thread's JavaScriptEventLoop instance + final class CurrentThread: TaskExecutor, SchedulableExecutor, MainExecutor, SerialExecutor { + func checkIsolated() {} + + func enqueue(_ job: consuming ExecutorJob) { + JavaScriptEventLoop.shared.enqueue(job) + } + + func enqueue( + _ job: consuming ExecutorJob, + after delay: C.Duration, + tolerance: C.Duration?, + clock: C + ) { + JavaScriptEventLoop.shared.enqueue( + job, + after: delay, + tolerance: tolerance, + clock: clock + ) + } + func run() throws { + try JavaScriptEventLoop.shared.run() + } + func stop() { + JavaScriptEventLoop.shared.stop() + } + } + + public static var mainExecutor: any MainExecutor { + CurrentThread() + } + + public static var defaultExecutor: any TaskExecutor { + CurrentThread() + } +} + +#endif // compiler(>=6.2) diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift index d22b0a64..54d1c5dd 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift @@ -3,9 +3,9 @@ import _CJavaScriptKit @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) extension JavaScriptEventLoop { - + static func installByLegacyHook() { -#if compiler(>=5.9) + #if compiler(>=5.9) typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( swift_task_asyncMainDrainQueue_original, swift_task_asyncMainDrainQueue_override ) -> Void @@ -16,10 +16,10 @@ extension JavaScriptEventLoop { swift_task_asyncMainDrainQueue_hook_impl, to: UnsafeMutableRawPointer?.self ) -#endif + #endif typealias swift_task_enqueueGlobal_hook_Fn = @convention(thin) (UnownedJob, swift_task_enqueueGlobal_original) - -> Void + -> Void let swift_task_enqueueGlobal_hook_impl: swift_task_enqueueGlobal_hook_Fn = { job, original in JavaScriptEventLoop.shared.unsafeEnqueue(job) } @@ -32,17 +32,18 @@ extension JavaScriptEventLoop { UInt64, UnownedJob, swift_task_enqueueGlobalWithDelay_original ) -> Void let swift_task_enqueueGlobalWithDelay_hook_impl: swift_task_enqueueGlobalWithDelay_hook_Fn = { - delay, + nanoseconds, job, original in - JavaScriptEventLoop.shared.enqueue(job, withDelay: delay) + let milliseconds = Double(nanoseconds / 1_000_000) + JavaScriptEventLoop.shared.enqueue(job, withDelay: milliseconds) } swift_task_enqueueGlobalWithDelay_hook = unsafeBitCast( swift_task_enqueueGlobalWithDelay_hook_impl, to: UnsafeMutableRawPointer?.self ) - -#if compiler(>=5.7) + + #if compiler(>=5.7) typealias swift_task_enqueueGlobalWithDeadline_hook_Fn = @convention(thin) ( Int64, Int64, Int64, Int64, Int32, UnownedJob, swift_task_enqueueGlobalWithDelay_original ) -> Void @@ -60,8 +61,8 @@ extension JavaScriptEventLoop { swift_task_enqueueGlobalWithDeadline_hook_impl, to: UnsafeMutableRawPointer?.self ) -#endif - + #endif + typealias swift_task_enqueueMainExecutor_hook_Fn = @convention(thin) ( UnownedJob, swift_task_enqueueMainExecutor_original ) -> Void @@ -76,7 +77,6 @@ extension JavaScriptEventLoop { } } - #if compiler(>=5.7) /// Taken from https://github.com/apple/swift/blob/d375c972f12128ec6055ed5f5337bfcae3ec67d8/stdlib/public/Concurrency/Clock.swift#L84-L88 @_silgen_name("swift_get_time") @@ -99,9 +99,8 @@ extension JavaScriptEventLoop { var nowSec: Int64 = 0 var nowNSec: Int64 = 0 swift_get_time(&nowSec, &nowNSec, clock) - let delayNanosec = (seconds - nowSec) * 1_000_000_000 + (nanoseconds - nowNSec) - enqueue(job, withDelay: delayNanosec <= 0 ? 0 : UInt64(delayNanosec)) + let delayMilliseconds = (seconds - nowSec) * 1_000 + (nanoseconds - nowNSec) / 1_000_000 + enqueue(job, withDelay: delayMilliseconds <= 0 ? 0 : Double(delayMilliseconds)) } } #endif - diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index 399bcf76..1cb90f8d 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -119,13 +119,20 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { private static func installGlobalExecutorIsolated() { guard !didInstallGlobalExecutor else { return } didInstallGlobalExecutor = true + #if compiler(>=6.2) + if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *) { + // For Swift 6.2 and above, we can use the new `ExecutorFactory` API + _Concurrency._createExecutors(factory: JavaScriptEventLoop.self) + } + #else + // For Swift 6.1 and below, we need to install the global executor by hook API installByLegacyHook() + #endif } - internal func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { - let milliseconds = nanoseconds / 1_000_000 + internal func enqueue(_ job: UnownedJob, withDelay milliseconds: Double) { setTimeout( - Double(milliseconds), + milliseconds, { #if compiler(>=5.9) job.runSynchronously(on: self.asUnownedSerialExecutor()) diff --git a/Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift b/Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift index acc6fccf..f743d8ef 100644 --- a/Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift +++ b/Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift @@ -90,9 +90,7 @@ final class WebWorkerTaskExecutorTests: XCTestCase { } } let taskRunOnMainThread = await task.value - // FIXME: The block passed to `MainActor.run` should run on the main thread - // XCTAssertTrue(taskRunOnMainThread) - XCTAssertFalse(taskRunOnMainThread) + XCTAssertTrue(taskRunOnMainThread) // After the task is done, back to the main thread XCTAssertTrue(isMainThread()) From 3e1107fbc6a33c9d92e47506a7802cbc87b0c530 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 12:44:26 +0800 Subject: [PATCH 3/3] CI: Update nightly toolchain in CI workflow --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cd9c6849..cf022434 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,12 +21,12 @@ jobs: target: "wasm32-unknown-wasi" - os: ubuntu-22.04 toolchain: - download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a-ubuntu22.04.tar.gz + download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a-ubuntu22.04.tar.gz wasi-backend: Node target: "wasm32-unknown-wasi" - os: ubuntu-22.04 toolchain: - download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a-ubuntu22.04.tar.gz + download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a/swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a-ubuntu22.04.tar.gz wasi-backend: Node target: "wasm32-unknown-wasip1-threads"