@@ -119,80 +119,20 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
119
119
private static func installGlobalExecutorIsolated( ) {
120
120
guard !didInstallGlobalExecutor else { return }
121
121
didInstallGlobalExecutor = true
122
-
123
- #if compiler(>=5.9)
124
- typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention ( thin) (
125
- swift_task_asyncMainDrainQueue_original , swift_task_asyncMainDrainQueue_override
126
- ) -> Void
127
- let swift_task_asyncMainDrainQueue_hook_impl : swift_task_asyncMainDrainQueue_hook_Fn = { _, _ in
128
- swjs_unsafe_event_loop_yield ( )
129
- }
130
- swift_task_asyncMainDrainQueue_hook = unsafeBitCast (
131
- swift_task_asyncMainDrainQueue_hook_impl,
132
- to: UnsafeMutableRawPointer ? . self
133
- )
134
- #endif
135
-
136
- typealias swift_task_enqueueGlobal_hook_Fn = @convention ( thin) ( UnownedJob , swift_task_enqueueGlobal_original )
137
- -> Void
138
- let swift_task_enqueueGlobal_hook_impl : swift_task_enqueueGlobal_hook_Fn = { job, original in
139
- JavaScriptEventLoop . shared. unsafeEnqueue ( job)
140
- }
141
- swift_task_enqueueGlobal_hook = unsafeBitCast (
142
- swift_task_enqueueGlobal_hook_impl,
143
- to: UnsafeMutableRawPointer ? . self
144
- )
145
-
146
- typealias swift_task_enqueueGlobalWithDelay_hook_Fn = @convention ( thin) (
147
- UInt64 , UnownedJob , swift_task_enqueueGlobalWithDelay_original
148
- ) -> Void
149
- let swift_task_enqueueGlobalWithDelay_hook_impl : swift_task_enqueueGlobalWithDelay_hook_Fn = {
150
- delay,
151
- job,
152
- original in
153
- JavaScriptEventLoop . shared. enqueue ( job, withDelay: delay)
154
- }
155
- swift_task_enqueueGlobalWithDelay_hook = unsafeBitCast (
156
- swift_task_enqueueGlobalWithDelay_hook_impl,
157
- to: UnsafeMutableRawPointer ? . self
158
- )
159
-
160
- #if compiler(>=5.7)
161
- typealias swift_task_enqueueGlobalWithDeadline_hook_Fn = @convention ( thin) (
162
- Int64 , Int64 , Int64 , Int64 , Int32 , UnownedJob , swift_task_enqueueGlobalWithDelay_original
163
- ) -> Void
164
- let swift_task_enqueueGlobalWithDeadline_hook_impl : swift_task_enqueueGlobalWithDeadline_hook_Fn = {
165
- sec,
166
- nsec,
167
- tsec,
168
- tnsec,
169
- clock,
170
- job,
171
- original in
172
- JavaScriptEventLoop . shared. enqueue ( job, withDelay: sec, nsec, tsec, tnsec, clock)
122
+ #if compiler(>=6.2)
123
+ if #available( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , visionOS 9999 , * ) {
124
+ // For Swift 6.2 and above, we can use the new `ExecutorFactory` API
125
+ _Concurrency. _createExecutors ( factory: JavaScriptEventLoop . self)
173
126
}
174
- swift_task_enqueueGlobalWithDeadline_hook = unsafeBitCast (
175
- swift_task_enqueueGlobalWithDeadline_hook_impl,
176
- to: UnsafeMutableRawPointer ? . self
177
- )
127
+ #else
128
+ // For Swift 6.1 and below, we need to install the global executor by hook API
129
+ installByLegacyHook ( )
178
130
#endif
179
-
180
- typealias swift_task_enqueueMainExecutor_hook_Fn = @convention ( thin) (
181
- UnownedJob , swift_task_enqueueMainExecutor_original
182
- ) -> Void
183
- let swift_task_enqueueMainExecutor_hook_impl : swift_task_enqueueMainExecutor_hook_Fn = { job, original in
184
- JavaScriptEventLoop . shared. unsafeEnqueue ( job)
185
- }
186
- swift_task_enqueueMainExecutor_hook = unsafeBitCast (
187
- swift_task_enqueueMainExecutor_hook_impl,
188
- to: UnsafeMutableRawPointer ? . self
189
- )
190
131
}
191
132
192
- private func enqueue( _ job: UnownedJob , withDelay nanoseconds: UInt64 ) {
193
- let milliseconds = nanoseconds / 1_000_000
133
+ internal func enqueue( _ job: UnownedJob , withDelay milliseconds: Double ) {
194
134
setTimeout (
195
- Double ( milliseconds) ,
135
+ milliseconds,
196
136
{
197
137
#if compiler(>=5.9)
198
138
job. runSynchronously ( on: self . asUnownedSerialExecutor ( ) )
@@ -203,7 +143,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
203
143
)
204
144
}
205
145
206
- private func unsafeEnqueue( _ job: UnownedJob ) {
146
+ internal func unsafeEnqueue( _ job: UnownedJob ) {
207
147
#if canImport(wasi_pthread) && compiler(>=6.1) && _runtime(_multithreaded)
208
148
guard swjs_get_worker_thread_id_cached ( ) == SWJS_MAIN_THREAD_ID else {
209
149
// Notify the main thread to execute the job when a job is
@@ -237,34 +177,6 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
237
177
}
238
178
}
239
179
240
- #if compiler(>=5.7)
241
- /// Taken from https://github.com/apple/swift/blob/d375c972f12128ec6055ed5f5337bfcae3ec67d8/stdlib/public/Concurrency/Clock.swift#L84-L88
242
- @_silgen_name ( " swift_get_time " )
243
- internal func swift_get_time(
244
- _ seconds: UnsafeMutablePointer < Int64 > ,
245
- _ nanoseconds: UnsafeMutablePointer < Int64 > ,
246
- _ clock: CInt
247
- )
248
-
249
- @available( macOS 14 . 0 , iOS 17.0 , watchOS 10.0 , tvOS 17.0 , * )
250
- extension JavaScriptEventLoop {
251
- fileprivate func enqueue(
252
- _ job: UnownedJob ,
253
- withDelay seconds: Int64 ,
254
- _ nanoseconds: Int64 ,
255
- _ toleranceSec: Int64 ,
256
- _ toleranceNSec: Int64 ,
257
- _ clock: Int32
258
- ) {
259
- var nowSec : Int64 = 0
260
- var nowNSec : Int64 = 0
261
- swift_get_time ( & nowSec, & nowNSec, clock)
262
- let delayNanosec = ( seconds - nowSec) * 1_000_000_000 + ( nanoseconds - nowNSec)
263
- enqueue ( job, withDelay: delayNanosec <= 0 ? 0 : UInt64 ( delayNanosec) )
264
- }
265
- }
266
- #endif
267
-
268
180
@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
269
181
extension JSPromise {
270
182
/// Wait for the promise to complete, returning (or throwing) its result.
0 commit comments