Skip to content

Commit bbb1676

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

23 files changed

+168
-15
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: 27 additions & 3 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) {
@@ -577,6 +595,7 @@ internal class __NSSwiftData : NSData {
577595
_range = range
578596
}
579597

598+
#if !os(WASI)
580599
public required init?(coder aDecoder: NSCoder) {
581600
fatalError("This should have been encoded as NSData.")
582601
}
@@ -585,6 +604,7 @@ internal class __NSSwiftData : NSData {
585604
// This should encode this object just like NSData does, and .classForCoder should do the rest.
586605
super.encode(with: aCoder)
587606
}
607+
#endif
588608

589609
override var length: Int {
590610
return _range.upperBound - _range.lowerBound
@@ -654,7 +674,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
654674
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
655675
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum
656676
@usableFromInline var bytes: Buffer
657-
#elseif arch(i386) || arch(arm)
677+
#elseif arch(i386) || arch(arm) || arch(wasm32)
658678
@usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8,
659679
UInt8, UInt8) //len //enum
660680
@usableFromInline var bytes: Buffer
@@ -683,7 +703,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
683703
assert(count <= MemoryLayout<Buffer>.size)
684704
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
685705
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)
706+
#elseif arch(i386) || arch(arm) || arch(wasm32)
687707
bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0))
688708
#else
689709
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -866,7 +886,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
866886

867887
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
868888
@usableFromInline internal typealias HalfInt = Int32
869-
#elseif arch(i386) || arch(arm)
889+
#elseif arch(i386) || arch(arm) || arch(wasm32)
870890
@usableFromInline internal typealias HalfInt = Int16
871891
#else
872892
#error("This architecture isn't known. Add it to the 32-bit or 64-bit line.")
@@ -2012,6 +2032,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20122032
}
20132033
}
20142034

2035+
#if !os(WASI)
20152036
/// Initialize a `Data` with the contents of a `URL`.
20162037
///
20172038
/// - parameter url: The `URL` to read.
@@ -2024,6 +2045,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20242045
return Data(bytes: d.bytes, count: d.length)
20252046
}
20262047
}
2048+
#endif
20272049

20282050
/// Initialize a `Data` from a Base-64 encoded String using the given options.
20292051
///
@@ -2287,6 +2309,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
22872309
}
22882310
#endif
22892311

2312+
#if !os(WASI)
22902313
/// Write the contents of the `Data` to a location.
22912314
///
22922315
/// - parameter url: The location to write the data into.
@@ -2307,6 +2330,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23072330
#endif
23082331
}
23092332
}
2333+
#endif
23102334

23112335
// MARK: -
23122336

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
5757
self.init(array: elements)
5858
}
5959

60+
#if !os(WASI)
6061
public required convenience init?(coder aDecoder: NSCoder) {
6162
guard aDecoder.allowsKeyedCoding else {
6263
preconditionFailure("Unkeyed coding is unsupported.")
@@ -74,6 +75,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
7475
self.init(array: objects)
7576
}
7677
}
78+
#endif
7779

7880
public convenience init(object anObject: Any) {
7981
self.init(array: [anObject])
@@ -106,6 +108,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
106108
buffer.deallocate()
107109
}
108110

111+
#if !os(WASI)
109112
open func encode(with aCoder: NSCoder) {
110113
guard aCoder.allowsKeyedCoding else {
111114
preconditionFailure("Unkeyed coding is unsupported.")
@@ -124,6 +127,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
124127
public static var supportsSecureCoding: Bool {
125128
return true
126129
}
130+
#endif
127131

128132
open override func copy() -> Any {
129133
return copy(with: nil)
@@ -440,6 +444,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
440444
return objects
441445
}
442446

447+
#if !os(WASI)
443448
open func write(to url: URL) throws {
444449
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: .xml, options: 0)
445450
try pListData.write(to: url, options: .atomic)
@@ -463,6 +468,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
463468
return false
464469
}
465470
}
471+
#endif
466472

467473
open func objects(at indexes: IndexSet) -> [Any] {
468474
var objs = [Any]()
@@ -653,6 +659,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
653659
return lastEqual ? result + 1 : result
654660
}
655661

662+
#if !os(WASI)
656663
public convenience init(contentsOf url: URL, error: ()) throws {
657664
let plistDoc = try Data(contentsOf: url)
658665
guard let plistArray = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Array<Any>
@@ -679,6 +686,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
679686
return nil
680687
}
681688
}
689+
#endif
682690

683691
open func pathsMatchingExtensions(_ filterTypes: [String]) -> [String] {
684692
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ 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+
#if !os(WASI)
4040
/// Encodes an instance of a conforming class using a given archiver.
4141
///
4242
/// - Parameter aCoder: An archiver object.
@@ -46,6 +46,7 @@ public protocol NSCoding {
4646
///
4747
/// - Parameter aDecoder: An unarchiver object.
4848
init?(coder aDecoder: NSCoder)
49+
#endif
4950
}
5051

5152
/// Conforming to the `NSSecureCoding` protocol indicates that an object handles
@@ -76,8 +77,9 @@ public protocol NSCoding {
7677
/// In addition, the class must override its `NSSecureCoding` method to return
7778
/// `true`.
7879
public protocol NSSecureCoding : NSCoding {
79-
80+
#if !os(WASI)
8081
static var supportsSecureCoding: Bool { get }
82+
#endif
8183
}
8284

8385
/// The `NSCoder` abstract class declares the interface used by concrete

Sources/Foundation/NSConcreteValue.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ internal class NSConcreteValue : NSValue {
111111
return Data(bytes: boundBytes, count: self._size).description
112112
}
113113

114+
#if !os(WASI)
114115
convenience required init?(coder aDecoder: NSCoder) {
115116
guard aDecoder.allowsKeyedCoding else {
116117
preconditionFailure("Unkeyed coding is unsupported.")
@@ -133,6 +134,7 @@ internal class NSConcreteValue : NSValue {
133134
aCoder.encode(String(cString: self.objCType)._bridgeToObjectiveC())
134135
aCoder.encodeValue(ofObjCType: self.objCType, at: self.value)
135136
}
137+
#endif
136138

137139
private var _size : Int {
138140
return self._typeInfo.size

0 commit comments

Comments
 (0)