Skip to content

Commit de93e26

Browse files
[wasm] Miscellaneous Foundation fixes
* Removed too conservative guards * Port `Host` class * Add `arch(wasm32)` to 32-bit arch conditions
1 parent 514e2bd commit de93e26

File tree

6 files changed

+11
-23
lines changed

6 files changed

+11
-23
lines changed

Sources/Foundation/Host.swift

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ open class Host: NSObject {
9191
return "localhost"
9292
}
9393
return String(cString: hostname)
94+
#elseif os(WASI) // WASI does not have uname
95+
return "localhost"
9496
#else
9597
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
9698
defer {
@@ -170,6 +172,9 @@ open class Host: NSObject {
170172
}
171173
_names = [info]
172174
_resolved = true
175+
#elseif os(WASI) // WASI does not have getifaddrs
176+
_names = [info]
177+
_resolved = true
173178
#else
174179
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
175180
if getifaddrs(&ifaddr) != 0 {
@@ -267,6 +272,11 @@ open class Host: NSObject {
267272

268273
_resolved = true
269274
}
275+
#elseif os(WASI) // WASI does not have getaddrinfo
276+
if let info = _info {
277+
_names = [info]
278+
_resolved = true
279+
}
270280
#else
271281
if let info = _info {
272282
var flags: Int32 = 0

Sources/Foundation/NSCharacterSet.swift

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

187-
#if !os(WASI)
188187
public convenience init?(contentsOfFile fName: String) {
189188
do {
190189
let data = try Data(contentsOf: URL(fileURLWithPath: fName))
@@ -330,7 +329,6 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
330329
aCoder.encode(true, forKey: .characterSetIsInvertedKey)
331330
}
332331
}
333-
#endif
334332

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

Sources/Foundation/NSKeyedArchiver.swift

-10
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ open class NSKeyedArchiver : NSCoder {
150150
/// - Returns: `true` if the operation was successful, otherwise `false`.
151151
@available(swift, deprecated: 9999, renamed: "archivedData(withRootObject:requiringSecureCoding:)")
152152
open class func archiveRootObject(_ rootObject: Any, toFile path: String) -> Bool {
153-
#if os(WASI)
154-
assertionFailure("\(#function) does not support file access on WASI")
155-
return false
156-
#else
157153
var fd : Int32 = -1
158154
var auxFilePath : String
159155
var finishedEncoding : Bool = false
@@ -187,7 +183,6 @@ open class NSKeyedArchiver : NSCoder {
187183
finishedEncoding = keyedArchiver._flags.contains(.finishedEncoding)
188184

189185
return finishedEncoding
190-
#endif
191186
}
192187

193188
public convenience init(requiringSecureCoding: Bool) {
@@ -228,13 +223,8 @@ open class NSKeyedArchiver : NSCoder {
228223
success = true
229224
}
230225
} else {
231-
#if !os(WASI)
232226
let stream = unsafeBitCast(self._stream, to: CFWriteStream.self)
233227
success = CFPropertyListWrite(plist, stream, kCFPropertyListXMLFormat_v1_0, 0, nil) > 0
234-
#else
235-
assertionFailure("\(#function) only supports data streams on WASI")
236-
return false
237-
#endif
238228
}
239229

240230
return success

Sources/Foundation/NSPathUtilities.swift

-6
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ public func NSTemporaryDirectory() -> String {
7575
}
7676
}
7777
#endif
78-
#if !os(WASI)
7978
if let tmpdir = ProcessInfo.processInfo.environment["TMPDIR"] {
8079
return normalizedPath(with: tmpdir)
8180
}
82-
#endif
8381
#if os(Android)
8482
// Bionic uses /data/local/tmp/ as temporary directory. TMPDIR is rarely
8583
// defined.
@@ -433,7 +431,6 @@ extension NSString {
433431
return paths.map(appendingPathComponent)
434432
}
435433

436-
#if !os(WASI)
437434
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
438435
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
439436
public func completePath(into outputName: inout String?, caseSensitive flag: Bool, matchesInto outputArray: inout [String], filterTypes: [String]?) -> Int {
@@ -536,7 +533,6 @@ extension NSString {
536533
return { $0.lowercased().hasPrefix(prefix) }
537534
}
538535
}
539-
#endif
540536

541537
internal func _longestCommonPrefix(_ strings: [String], caseSensitive: Bool) -> String? {
542538
guard !strings.isEmpty else {
@@ -584,11 +580,9 @@ extension NSString {
584580
return path + "/"
585581
}
586582

587-
#if !os(WASI)
588583
public var fileSystemRepresentation: UnsafePointer<Int8> {
589584
return FileManager.default.fileSystemRepresentation(withPath: self._swiftObject)
590585
}
591-
#endif
592586

593587
public func getFileSystemRepresentation(_ cname: UnsafeMutablePointer<Int8>, maxLength max: Int) -> Bool {
594588
#if os(Windows)

Sources/Foundation/NSString.swift

-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ func NSLocalizedString(_ key: String,
2525
bundle: Bundle = Bundle.main,
2626
value: String = "",
2727
comment: String) -> String {
28-
#if os(WASI)
29-
return key
30-
#else
3128
return bundle.localizedString(forKey: key, value: value, table: tableName)
32-
#endif
3329
}
3430

3531
internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue

Sources/Foundation/ScannerAPI.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension Scanner {
4343
if let value = scanInt64(representation: representation) {
4444
return Int(value)
4545
}
46-
#elseif arch(i386) || arch(arm)
46+
#elseif arch(i386) || arch(arm) || arch(wasm32)
4747
if let value = scanInt32(representation: representation) {
4848
return Int(value)
4949
}

0 commit comments

Comments
 (0)