Skip to content

Commit 2a9d635

Browse files
committed
[WASM] Avoid Foundation.Thread and Dispatch
1 parent 58d9c83 commit 2a9d635

File tree

11 files changed

+50
-2
lines changed

11 files changed

+50
-2
lines changed

Sources/Nimble/Adapters/NMBExpectation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
#if canImport(Darwin) && !SWIFT_PACKAGE
24
import class Foundation.NSObject
35
import typealias Foundation.TimeInterval
@@ -150,3 +152,5 @@ public class NMBExpectation: NSObject {
150152
}
151153

152154
#endif
155+
156+
#endif // #if !os(WASI)

Sources/Nimble/Adapters/NimbleEnvironment.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
#if !os(WASI)
12
import Dispatch
2-
import class Foundation.NSObject
33
import class Foundation.Thread
4+
#endif
5+
import class Foundation.NSObject
46

57
/// "Global" state of Nimble is stored here. Only DSL functions should access / be aware of this
68
/// class' existence
79
internal class NimbleEnvironment: NSObject {
10+
#if os(WASI)
11+
static var activeInstance: NimbleEnvironment = NimbleEnvironment()
12+
#else
813
static var activeInstance: NimbleEnvironment {
914
get {
1015
let env = Thread.current.threadDictionary["NimbleEnvironment"]
@@ -20,6 +25,7 @@ internal class NimbleEnvironment: NSObject {
2025
Thread.current.threadDictionary["NimbleEnvironment"] = newValue
2126
}
2227
}
28+
#endif
2329

2430
// swiftlint:disable:next todo
2531
// TODO: eventually migrate the global to this environment value
@@ -29,15 +35,19 @@ internal class NimbleEnvironment: NSObject {
2935
}
3036

3137
var suppressTVOSAssertionWarning: Bool = false
38+
#if !os(WASI)
3239
var awaiter: Awaiter
40+
#endif
3341

3442
override init() {
43+
#if !os(WASI)
3544
let timeoutQueue = DispatchQueue.global(qos: .userInitiated)
3645
awaiter = Awaiter(
3746
waitLock: AssertionWaitLock(),
3847
asyncQueue: .main,
3948
timeoutQueue: timeoutQueue
4049
)
50+
#endif
4151

4252
super.init()
4353
}

Sources/Nimble/DSL+Wait.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Dispatch
24
import Foundation
35

@@ -117,3 +119,5 @@ internal func blockedRunLoopErrorMessageFor(_ fnName: String, leeway: DispatchTi
117119
public func waitUntil(timeout: DispatchTimeInterval = AsyncDefaults.timeout, file: FileString = #file, line: UInt = #line, action: @escaping (@escaping () -> Void) -> Void) {
118120
NMBWait.until(timeout: timeout, file: file, line: line, action: action)
119121
}
122+
123+
#endif // #if !os(WASI)

Sources/Nimble/Matchers/Async.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Foundation
24
import Dispatch
35

@@ -180,3 +182,5 @@ extension Expectation {
180182
return toNever(predicate, until: until, pollInterval: pollInterval, description: description)
181183
}
182184
}
185+
186+
#endif // #if !os(WASI)

Sources/Nimble/Matchers/PostNotification.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
#if canImport(Foundation)
24
import Foundation
35

@@ -97,4 +99,6 @@ public func postDistributedNotifications<Out>(
9799
}
98100
#endif
99101

100-
#endif
102+
#endif // #if canImport(Foundation)
103+
104+
#endif // #if !os(WASI)

Sources/Nimble/Utils/Await.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import CoreFoundation
24
import Dispatch
35
import Foundation
@@ -369,3 +371,5 @@ internal func pollBlock(
369371

370372
return result
371373
}
374+
375+
#endif // #if !os(WASI)

Sources/Nimble/Utils/DispatchTimeInterval.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Dispatch
24

35
#if canImport(CDispatch)
@@ -39,3 +41,5 @@ extension TimeInterval {
3941
}
4042
}
4143
#endif
44+
45+
#endif // #if !os(WASI)

Tests/NimbleTests/AsynchronousTest.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import Dispatch
24
import CoreFoundation
35
import Foundation
@@ -283,3 +285,5 @@ final class AsyncTest: XCTestCase {
283285
}
284286
}
285287
}
288+
289+
#endif // #if !os(WASI)

Tests/NimbleTests/Helpers/utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#if !os(WASI)
12
import Dispatch
3+
#endif
24
import Foundation
35
@testable import Nimble
46
import XCTest
@@ -64,12 +66,14 @@ func failsWithErrorMessageForNil(_ message: String, file: FileString = #file, li
6466
failsWithErrorMessage("\(message) (use beNil() to match nils)", file: file, line: line, preferOriginalSourceLocation: preferOriginalSourceLocation, closure: closure)
6567
}
6668

69+
#if !os(WASI)
6770
func deferToMainQueue(action: @escaping () -> Void) {
6871
DispatchQueue.main.async {
6972
Thread.sleep(forTimeInterval: 0.01)
7073
action()
7174
}
7275
}
76+
#endif
7377

7478
#if canImport(Darwin) && !SWIFT_PACKAGE
7579
public class NimbleHelper: NSObject {

Tests/NimbleTests/Matchers/PostNotificationTest.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#if !os(WASI)
2+
13
import XCTest
24
import Nimble
35
import Foundation
@@ -79,3 +81,5 @@ final class PostNotificationTest: XCTestCase {
7981
}
8082
#endif
8183
}
84+
85+
#endif // #if !os(WASI)

0 commit comments

Comments
 (0)