Skip to content

Commit e7b4cbf

Browse files
Merge pull request #4915 from kateinoigakukun/pr-aad5de6289e0575216a3850e34deeea997cb248d
[wasm] Exclude some completely unavailable APIs from build
2 parents 83634d0 + ddb58bd commit e7b4cbf

10 files changed

+51
-1
lines changed

Sources/Foundation/DispatchData+DataProtocol.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
13+
#if canImport(Dispatch)
1414
import Dispatch
1515

1616
extension DispatchData : DataProtocol {
@@ -54,3 +54,4 @@ extension DispatchData : DataProtocol {
5454
return regions
5555
}
5656
}
57+
#endif

Sources/Foundation/NSNotification.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ open class NSNotification: NSObject, NSCopying, NSCoding {
8282
}
8383
}
8484

85+
#if canImport(Dispatch)
86+
8587
private class NSNotificationReceiver : NSObject {
8688
fileprivate var name: Notification.Name?
8789
fileprivate var block: ((Notification) -> Void)?
@@ -191,3 +193,5 @@ open class NotificationCenter: NSObject {
191193
}
192194

193195
}
196+
197+
#endif // canImport(Dispatch)

Sources/Foundation/NotificationQueue.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Dispatch)
1011
@_implementationOnly import CoreFoundation
1112

1213
extension NotificationQueue {
@@ -176,3 +177,5 @@ open class NotificationQueue: NSObject {
176177
}
177178

178179
}
180+
181+
#endif

Sources/Foundation/Operation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Dispatch)
1011
import Dispatch
1112

1213
internal let _NSOperationIsFinished = "isFinished"
@@ -1438,3 +1439,4 @@ extension OperationQueue {
14381439
}
14391440
}
14401441
}
1442+
#endif

Sources/Foundation/Port.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public protocol PortDelegate: AnyObject {
8383
func handle(_ message: PortMessage)
8484
}
8585

86+
#if os(WASI)
87+
88+
@available(*, unavailable, message: "SocketPort is not available on this platform.")
89+
open class SocketPort: Port {}
90+
91+
#else
92+
8693
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
8794
import Glibc
8895
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
@@ -1106,3 +1113,5 @@ fileprivate extension Data {
11061113
return self[...self.index(self.startIndex, offsetBy: range.upperBound)]
11071114
}
11081115
}
1116+
1117+
#endif

Sources/Foundation/Process.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Dispatch)
1011
@_implementationOnly import CoreFoundation
1112
#if os(Windows)
1213
import WinSDK
@@ -1174,3 +1175,5 @@ extension Process {
11741175

11751176
public static let didTerminateNotification = NSNotification.Name(rawValue: "NSTaskDidTerminateNotification")
11761177
}
1178+
1179+
#endif

Sources/Foundation/Progress.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Dispatch)
1011
import Dispatch
1112

1213
/**
@@ -545,3 +546,4 @@ fileprivate class _ProgressTSD : NSObject {
545546
childAttached = false
546547
}
547548
}
549+
#endif

Sources/Foundation/RunLoop.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ extension RunLoop.Mode {
5151
}
5252
}
5353

54+
#if !canImport(Dispatch)
55+
56+
open class RunLoop: NSObject {
57+
@available(*, unavailable, message: "RunLoop is not available on WASI")
58+
open class var current: RunLoop {
59+
fatalError("RunLoop is not available on WASI")
60+
}
61+
62+
@available(*, unavailable, message: "RunLoop is not available on WASI")
63+
open class var main: RunLoop {
64+
fatalError("RunLoop is not available on WASI")
65+
}
66+
67+
internal final var currentCFRunLoop: CFRunLoop { NSUnsupported() }
68+
}
69+
70+
#else
71+
5472
internal func _NSRunLoopNew(_ cf: CFRunLoop) -> Unmanaged<AnyObject> {
5573
let rl = Unmanaged<RunLoop>.passRetained(RunLoop(cfObject: cf))
5674
return unsafeBitCast(rl, to: Unmanaged<AnyObject>.self) // this retain is balanced on the other side of the CF fence
@@ -421,3 +439,5 @@ extension RunLoop._Source {
421439
unsafeBitCast(_cfSourceStorage, to: CFRunLoopSource.self)
422440
}
423441
}
442+
443+
#endif // canImport(Dispatch)

Sources/Foundation/Thread.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Dispatch)
1011
@_implementationOnly import CoreFoundation
1112
#if os(Windows)
1213
import WinSDK
@@ -448,3 +449,4 @@ extension NSNotification.Name {
448449
public static let NSDidBecomeSingleThreaded = NSNotification.Name(rawValue: "NSDidBecomeSingleThreadedNotification")
449450
public static let NSThreadWillExit = NSNotification.Name(rawValue: "NSThreadWillExitNotification")
450451
}
452+
#endif

Sources/Foundation/UserDefaults.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,9 @@ open class UserDefaults: NSObject {
406406

407407
_ = defaults.synchronize()
408408

409+
#if canImport(Dispatch)
409410
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
411+
#endif
410412
}
411413
}
412414

@@ -418,7 +420,9 @@ open class UserDefaults: NSObject {
418420

419421
_ = defaults.synchronize()
420422

423+
#if canImport(Dispatch)
421424
NotificationCenter.default.post(name: UserDefaults.didChangeNotification, object: self)
425+
#endif
422426
}
423427
}
424428

0 commit comments

Comments
 (0)