Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Miscellaneous Foundation fixes #4917

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/Foundation/Host.swift
Expand Up @@ -91,6 +91,8 @@ open class Host: NSObject {
return "localhost"
}
return String(cString: hostname)
#elseif os(WASI) // WASI does not have uname
return "localhost"
#else
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
Expand Down Expand Up @@ -170,6 +172,9 @@ open class Host: NSObject {
}
_names = [info]
_resolved = true
#elseif os(WASI) // WASI does not have getifaddrs
_names = [info]
_resolved = true
#else
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) != 0 {
Expand Down Expand Up @@ -267,6 +272,11 @@ open class Host: NSObject {

_resolved = true
}
#elseif os(WASI) // WASI does not have getaddrinfo
if let info = _info {
_names = [info]
_resolved = true
}
#else
if let info = _info {
var flags: Int32 = 0
Expand Down
2 changes: 0 additions & 2 deletions Sources/Foundation/NSCharacterSet.swift
Expand Up @@ -184,7 +184,6 @@ open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
_CFCharacterSetInitWithBitmapRepresentation(_cfMutableObject, data._cfObject)
}

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

open func characterIsMember(_ aCharacter: unichar) -> Bool {
return longCharacterIsMember(UInt32(aCharacter))
Expand Down
10 changes: 0 additions & 10 deletions Sources/Foundation/NSKeyedArchiver.swift
Expand Up @@ -150,10 +150,6 @@ open class NSKeyedArchiver : NSCoder {
/// - Returns: `true` if the operation was successful, otherwise `false`.
@available(swift, deprecated: 9999, renamed: "archivedData(withRootObject:requiringSecureCoding:)")
open class func archiveRootObject(_ rootObject: Any, toFile path: String) -> Bool {
#if os(WASI)
assertionFailure("\(#function) does not support file access on WASI")
return false
#else
var fd : Int32 = -1
var auxFilePath : String
var finishedEncoding : Bool = false
Expand Down Expand Up @@ -187,7 +183,6 @@ open class NSKeyedArchiver : NSCoder {
finishedEncoding = keyedArchiver._flags.contains(.finishedEncoding)

return finishedEncoding
#endif
}

public convenience init(requiringSecureCoding: Bool) {
Expand Down Expand Up @@ -228,13 +223,8 @@ open class NSKeyedArchiver : NSCoder {
success = true
}
} else {
#if !os(WASI)
let stream = unsafeBitCast(self._stream, to: CFWriteStream.self)
success = CFPropertyListWrite(plist, stream, kCFPropertyListXMLFormat_v1_0, 0, nil) > 0
#else
assertionFailure("\(#function) only supports data streams on WASI")
return false
#endif
}

return success
Expand Down
6 changes: 0 additions & 6 deletions Sources/Foundation/NSPathUtilities.swift
Expand Up @@ -75,11 +75,9 @@ public func NSTemporaryDirectory() -> String {
}
}
#endif
#if !os(WASI)
if let tmpdir = ProcessInfo.processInfo.environment["TMPDIR"] {
return normalizedPath(with: tmpdir)
}
#endif
#if os(Android)
// Bionic uses /data/local/tmp/ as temporary directory. TMPDIR is rarely
// defined.
Expand Down Expand Up @@ -433,7 +431,6 @@ extension NSString {
return paths.map(appendingPathComponent)
}

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

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

#if !os(WASI)
public var fileSystemRepresentation: UnsafePointer<Int8> {
return FileManager.default.fileSystemRepresentation(withPath: self._swiftObject)
}
#endif

public func getFileSystemRepresentation(_ cname: UnsafeMutablePointer<Int8>, maxLength max: Int) -> Bool {
#if os(Windows)
Expand Down
4 changes: 0 additions & 4 deletions Sources/Foundation/NSString.swift
Expand Up @@ -25,11 +25,7 @@ func NSLocalizedString(_ key: String,
bundle: Bundle = Bundle.main,
value: String = "",
comment: String) -> String {
#if os(WASI)
return key
#else
return bundle.localizedString(forKey: key, value: value, table: tableName)
#endif
}

internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/ScannerAPI.swift
Expand Up @@ -43,7 +43,7 @@ extension Scanner {
if let value = scanInt64(representation: representation) {
return Int(value)
}
#elseif arch(i386) || arch(arm)
#elseif arch(i386) || arch(arm) || arch(wasm32)
if let value = scanInt32(representation: representation) {
return Int(value)
}
Expand Down