Skip to content

Commit 7a98383

Browse files
committed
Disable file/dispatch-related code in Foundation for WASI
1 parent 08f2025 commit 7a98383

23 files changed

+164
-47
lines changed

Sources/Foundation/CGFloat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@frozen
1111
public struct CGFloat {
12-
#if arch(i386) || arch(arm)
12+
#if arch(i386) || arch(arm) || arch(wasm32)
1313
/// The native type used to store the CGFloat, which is Float on
1414
/// 32-bit architectures and Double on 64-bit architectures.
1515
public typealias NativeType = Float
@@ -185,7 +185,7 @@ extension CGFloat : BinaryFloatingPoint {
185185

186186
@_transparent
187187
public init(bitPattern: UInt) {
188-
#if arch(i386) || arch(arm)
188+
#if arch(i386) || arch(arm) || arch(wasm32)
189189
native = NativeType(bitPattern: UInt32(bitPattern))
190190
#elseif arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
191191
native = NativeType(bitPattern: UInt64(bitPattern))

Sources/Foundation/CharacterSet.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
157157
_wrapped = _SwiftNSCharacterSet(immutableObject: NSCharacterSet(bitmapRepresentation: data))
158158
}
159159

160+
#if !os(WASI)
160161
/// Initialize with the contents of a file.
161162
///
162163
/// Returns `nil` if there was an error reading the file.
@@ -168,6 +169,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
168169
return nil
169170
}
170171
}
172+
#endif
171173

172174
public func hash(into hasher: inout Hasher) {
173175
hasher.combine(_mapUnmanaged { $0 })

Sources/Foundation/Data.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@
1212

1313
#if DEPLOYMENT_RUNTIME_SWIFT
1414

15+
#if canImport(Glibc)
16+
@usableFromInline let calloc = Glibc.calloc
17+
@usableFromInline let malloc = Glibc.malloc
18+
@usableFromInline let free = Glibc.free
19+
@usableFromInline let memset = Glibc.memset
20+
@usableFromInline let memcpy = Glibc.memcpy
21+
@usableFromInline let memcmp = Glibc.memcmp
22+
#elseif canImport(WASILibc)
23+
@usableFromInline let calloc = WASILibc.calloc
24+
@usableFromInline let malloc = WASILibc.malloc
25+
@usableFromInline let free = WASILibc.free
26+
@usableFromInline let memset = WASILibc.memset
27+
@usableFromInline let memcpy = WASILibc.memcpy
28+
@usableFromInline let memcmp = WASILibc.memcmp
29+
#endif
30+
1531
#if !canImport(Darwin)
1632
@inlinable // This is @inlinable as trivially computable.
1733
internal func malloc_good_size(_ size: Int) -> Int {
@@ -23,6 +39,8 @@ internal func malloc_good_size(_ size: Int) -> Int {
2339

2440
#if canImport(Glibc)
2541
import Glibc
42+
#elseif canImport(WASILibc)
43+
import WASILibc
2644
#endif
2745

2846
internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
@@ -562,30 +580,30 @@ internal class __NSSwiftData : NSData {
562580
override var classForCoder: AnyClass {
563581
return NSData.self
564582
}
565-
583+
566584
override init() {
567585
fatalError()
568586
}
569-
587+
570588
private init(_correctly: Void) {
571589
super.init()
572590
}
573-
591+
574592
convenience init(backing: __DataStorage, range: Range<Data.Index>) {
575593
self.init(_correctly: ())
576594
_backing = backing
577595
_range = range
578596
}
579-
597+
580598
public required init?(coder aDecoder: NSCoder) {
581599
fatalError("This should have been encoded as NSData.")
582600
}
583-
601+
584602
override func encode(with aCoder: NSCoder) {
585603
// This should encode this object just like NSData does, and .classForCoder should do the rest.
586604
super.encode(with: aCoder)
587605
}
588-
606+
589607
override var length: Int {
590608
return _range.upperBound - _range.lowerBound
591609
}
@@ -608,7 +626,7 @@ internal class __NSSwiftData : NSData {
608626
override func copy(with zone: NSZone? = nil) -> Any {
609627
return self
610628
}
611-
629+
612630
override func mutableCopy(with zone: NSZone? = nil) -> Any {
613631
return NSMutableData(bytes: bytes, length: length)
614632
}
@@ -654,7 +672,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
654672
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
655673
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum
656674
@usableFromInline var bytes: Buffer
657-
#elseif arch(i386) || arch(arm)
675+
#elseif arch(i386) || arch(arm) || arch(wasm32)
658676
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8,
659677
UInt8, UInt8) //len //enum
660678
@usableFromInline var bytes: Buffer
@@ -683,7 +701,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
683701
assert(count <= MemoryLayout<Buffer>.size)
684702
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
685703
bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0))
686-
#elseif arch(i386) || arch(arm)
704+
#elseif arch(i386) || arch(arm) || arch(wasm32)
687705
bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0))
688706
#else
689707
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -866,7 +884,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
866884

867885
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
868886
@usableFromInline internal typealias HalfInt = Int32
869-
#elseif arch(i386) || arch(arm)
887+
#elseif arch(i386) || arch(arm) || arch(wasm32)
870888
@usableFromInline internal typealias HalfInt = Int16
871889
#else
872890
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -2012,6 +2030,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20122030
}
20132031
}
20142032

2033+
#if !os(WASI)
20152034
/// Initialize a `Data` with the contents of a `URL`.
20162035
///
20172036
/// - parameter url: The `URL` to read.
@@ -2024,6 +2043,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20242043
return Data(bytes: d.bytes, count: d.length)
20252044
}
20262045
}
2046+
#endif
20272047

20282048
/// Initialize a `Data` from a Base-64 encoded String using the given options.
20292049
///
@@ -2287,6 +2307,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
22872307
}
22882308
#endif
22892309

2310+
#if !os(WASI)
22902311
/// Write the contents of the `Data` to a location.
22912312
///
22922313
/// - parameter url: The location to write the data into.
@@ -2307,6 +2328,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23072328
#endif
23082329
}
23092330
}
2331+
#endif
23102332

23112333
// MARK: -
23122334

Sources/Foundation/JSONSerialization.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ open class JSONSerialization : NSObject {
267267

268268
}
269269

270+
#if !os(WASI)
270271
/* Write JSON data into a stream. The stream should be opened and configured. The return value is the number of bytes written to the stream, or 0 on error. All other behavior of this method is the same as the dataWithJSONObject:options:error: method.
271272
*/
272273
open class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {
@@ -298,6 +299,7 @@ open class JSONSerialization : NSObject {
298299
} while stream.hasBytesAvailable
299300
return try jsonObject(with: data, options: opt)
300301
}
302+
#endif
301303
}
302304

303305
//MARK: - Encoding Detection

Sources/Foundation/NSArray.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
5656
public required convenience init(arrayLiteral elements: Any...) {
5757
self.init(array: elements)
5858
}
59-
59+
6060
public required convenience init?(coder aDecoder: NSCoder) {
6161
guard aDecoder.allowsKeyedCoding else {
6262
preconditionFailure("Unkeyed coding is unsupported.")
@@ -120,15 +120,15 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
120120
}
121121
}
122122
}
123-
123+
124124
public static var supportsSecureCoding: Bool {
125125
return true
126126
}
127-
127+
128128
open override func copy() -> Any {
129129
return copy(with: nil)
130130
}
131-
131+
132132
open func copy(with zone: NSZone? = nil) -> Any {
133133
if type(of: self) === NSArray.self {
134134
// return self for immutable type
@@ -140,11 +140,11 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
140140
}
141141
return NSArray(array: self.allObjects)
142142
}
143-
143+
144144
open override func mutableCopy() -> Any {
145145
return mutableCopy(with: nil)
146146
}
147-
147+
148148
open func mutableCopy(with zone: NSZone? = nil) -> Any {
149149
if type(of: self) === NSArray.self || type(of: self) === NSMutableArray.self {
150150
// always create and return an NSMutableArray
@@ -440,6 +440,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
440440
return objects
441441
}
442442

443+
#if !os(WASI)
443444
open func write(to url: URL) throws {
444445
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
445446
try pListData.write(to: url, options: .atomic)
@@ -463,6 +464,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
463464
return false
464465
}
465466
}
467+
#endif
466468

467469
open func objects(at indexes: IndexSet) -> [Any] {
468470
var objs = [Any]()
@@ -653,6 +655,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
653655
return lastEqual ? result + 1 : result
654656
}
655657

658+
#if !os(WASI)
656659
public convenience init(contentsOf url: URL, error: ()) throws {
657660
let plistDoc = try Data(contentsOf: url)
658661
guard let plistArray = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Array<Any>
@@ -679,6 +682,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
679682
return nil
680683
}
681684
}
685+
#endif
682686

683687
open func pathsMatchingExtensions(_ filterTypes: [String]) -> [String] {
684688
guard !filterTypes.isEmpty else {

Sources/Foundation/NSCalendar.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
13781378
}
13791379
}
13801380

1381+
#if !os(WASI)
13811382
// This notification is posted through [NSNotificationCenter defaultCenter]
13821383
// when the system day changes. Register with "nil" as the object of this
13831384
// notification. If the computer/device is asleep when the day changed,
@@ -1391,6 +1392,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
13911392
extension NSNotification.Name {
13921393
public static let NSCalendarDayChanged = NSNotification.Name(rawValue: "NSCalendarDayChangedNotification")
13931394
}
1395+
#endif
13941396

13951397

13961398
extension NSCalendar: _SwiftBridgeable {

Sources/Foundation/NSCharacterSet.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
184184
_CFCharacterSetInitWithBitmapRepresentation(_cfMutableObject, data._cfObject)
185185
}
186186

187+
#if !os(WASI)
187188
public convenience init?(contentsOfFile fName: String) {
188189
do {
189190
let data = try Data(contentsOf: URL(fileURLWithPath: fName))
@@ -329,6 +330,7 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
329330
aCoder.encode(true, forKey: .characterSetIsInvertedKey)
330331
}
331332
}
333+
#endif
332334

333335
open func characterIsMember(_ aCharacter: unichar) -> Bool {
334336
return longCharacterIsMember(UInt32(aCharacter))

Sources/Foundation/NSCoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ extension NSCoder {
3636
/// Any object class that should be codable must adopt the NSCoding protocol and
3737
/// implement its methods.
3838
public protocol NSCoding {
39-
39+
4040
/// Encodes an instance of a conforming class using a given archiver.
4141
///
4242
/// - Parameter aCoder: An archiver object.
4343
func encode(with aCoder: NSCoder)
44-
44+
4545
/// Initializes an object from data in a given unarchiver.
4646
///
4747
/// - Parameter aDecoder: An unarchiver object.
@@ -76,7 +76,7 @@ public protocol NSCoding {
7676
/// In addition, the class must override its `NSSecureCoding` method to return
7777
/// `true`.
7878
public protocol NSSecureCoding : NSCoding {
79-
79+
8080
static var supportsSecureCoding: Bool { get }
8181
}
8282

Sources/Foundation/NSConcreteValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ internal class NSConcreteValue : NSValue {
110110
let boundBytes = self.value.bindMemory(to: UInt8.self, capacity: self._size)
111111
return Data(bytes: boundBytes, count: self._size).description
112112
}
113-
113+
114114
convenience required init?(coder aDecoder: NSCoder) {
115115
guard aDecoder.allowsKeyedCoding else {
116116
preconditionFailure("Unkeyed coding is unsupported.")

0 commit comments

Comments
 (0)