Skip to content

Commit ed90316

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

19 files changed

+127
-8
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: 25 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) {
@@ -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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/NSData.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
//
99

1010
@_implementationOnly import CoreFoundation
11+
#if !os(WASI)
1112
import Dispatch
13+
#endif
1214

1315
extension NSData {
1416
public struct ReadingOptions : OptionSet {
@@ -149,6 +151,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
149151
_init(bytes: bytes, length: length, copy: false, deallocator: deallocator)
150152
}
151153

154+
#if !os(WASI)
152155
/// Initializes a data object with the contents of the file at a given path.
153156
public init(contentsOfFile path: String, options readOptionsMask: ReadingOptions = []) throws {
154157
super.init()
@@ -171,6 +174,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
171174
return nil
172175
}
173176
}
177+
#endif
174178

175179
/// Initializes a data object with the contents of another data object.
176180
public init(data: Data) {
@@ -180,6 +184,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
180184
}
181185
}
182186

187+
#if !os(WASI)
183188
/// Initializes a data object with the data from the location specified by a given URL.
184189
public init(contentsOf url: URL, options readOptionsMask: ReadingOptions = []) throws {
185190
super.init()
@@ -212,6 +217,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
212217
return try _NSNonfileURLContentLoader.current.contentsOf(url: url)
213218
}
214219
}
220+
#endif
215221

216222
/// Initializes a data object with the given Base64 encoded string.
217223
public init?(base64Encoded base64String: String, options: Base64DecodingOptions = []) {
@@ -291,6 +297,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
291297
return isEqual(to: data._swiftObject)
292298
}
293299

300+
#if !os(WASI)
294301
if let data = value as? DispatchData {
295302
if data.count != length {
296303
return false
@@ -300,6 +307,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
300307
return memcmp(bytes1, bytes2, length) == 0
301308
}
302309
}
310+
#endif
303311

304312
return false
305313
}
@@ -425,6 +433,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
425433
}
426434
}
427435

436+
#if !os(WASI)
428437
internal static func readBytesFromFileWithExtendedAttributes(_ path: String, options: ReadingOptions) throws -> NSDataReadResult {
429438
guard let handle = FileHandle(path: path, flags: O_RDONLY, createMode: 0) else {
430439
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
@@ -528,6 +537,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
528537
}
529538
try write(toFile: url.path, options: writeOptionsMask)
530539
}
540+
#endif
531541

532542
// MARK: - Bytes
533543
/// Copies a number of bytes from the start of the data object into a given buffer.
@@ -994,6 +1004,7 @@ open class NSMutableData : NSData {
9941004
super.init(data: data)
9951005
}
9961006

1007+
#if !os(WASI)
9971008
public override init?(contentsOfFile path: String) {
9981009
super.init(contentsOfFile: path)
9991010
}
@@ -1009,6 +1020,7 @@ open class NSMutableData : NSData {
10091020
public override init(contentsOf url: URL, options: NSData.ReadingOptions = []) throws {
10101021
try super.init(contentsOf: url, options: options)
10111022
}
1023+
#endif
10121024

10131025
public override init?(base64Encoded base64Data: Data, options: NSData.Base64DecodingOptions = []) {
10141026
super.init(base64Encoded: base64Data, options: options)

Sources/Foundation/NSDictionary.swift

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

1010

1111
@_implementationOnly import CoreFoundation
12+
13+
#if !os(WASI)
1214
import Dispatch
15+
#endif
1316

1417
open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding, ExpressibleByDictionaryLiteral {
1518
private let _cfinfo = _CFInfo(typeID: CFDictionaryGetTypeID())
@@ -48,6 +51,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
4851
return NSGeneratorEnumerator(_storage.keys.map { __SwiftValue.fetch(nonOptional: $0) }.makeIterator())
4952
}
5053

54+
#if !os(WASI)
5155
@available(*, deprecated)
5256
public convenience init?(contentsOfFile path: String) {
5357
self.init(contentsOf: URL(fileURLWithPath: path))
@@ -64,6 +68,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
6468
return nil
6569
}
6670
}
71+
#endif
6772

6873
public override convenience init() {
6974
self.init(objects: [], forKeys: [], count: 0)
@@ -494,6 +499,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
494499
return objects
495500
}
496501

502+
#if !os(WASI)
497503
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
498504
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
499505
}
@@ -508,6 +514,7 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
508514
return false
509515
}
510516
}
517+
#endif
511518

512519
open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
513520
enumerateKeysAndObjects(options: [], using: block)
@@ -538,13 +545,19 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
538545
}
539546
}
540547

548+
#if !os(WASI)
541549
if opts.contains(.concurrent) {
542550
DispatchQueue.concurrentPerform(iterations: count, execute: iteration)
543551
} else {
544552
for idx in 0..<count {
545553
iteration(idx)
546554
}
547555
}
556+
#else
557+
for idx in 0..<count {
558+
iteration(idx)
559+
}
560+
#endif
548561
}
549562
}
550563

Sources/Foundation/NSIndexSet.swift

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

10+
#if !os(WASI)
1011
import Dispatch
12+
#endif
1113

1214
/* Class for managing set of indexes. The set of valid indexes are 0 .. NSNotFound - 1; trying to use indexes outside this range is an error. NSIndexSet uses NSNotFound as a return value in cases where the queried index doesn't exist in the set; for instance, when you ask firstIndex and there are no indexes; or when you ask for indexGreaterThanIndex: on the last index, and so on.
1315

@@ -504,13 +506,19 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
504506
}
505507
}
506508
}
509+
#if !os(WASI)
507510
if opts.contains(.concurrent) {
508511
DispatchQueue.concurrentPerform(iterations: Int(rangeSequence.count), execute: iteration)
509512
} else {
510513
for idx in 0..<Int(rangeSequence.count) {
511514
iteration(idx)
512515
}
513516
}
517+
#else
518+
for idx in 0..<Int(rangeSequence.count) {
519+
iteration(idx)
520+
}
521+
#endif
514522
}
515523

516524
return result

0 commit comments

Comments
 (0)