Skip to content

Commit d1510fd

Browse files
authored
Merge pull request #1863 from compnerd/collection
Foundation: sundry fixes for Windows port
2 parents 536f4b8 + 9355737 commit d1510fd

11 files changed

+77
-7
lines changed

Foundation/CGFloat.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,13 @@ public func scalbn(_ x: CGFloat, _ n: Int) -> CGFloat {
863863
return CGFloat(scalbn(x.native, n))
864864
}
865865

866+
#if !os(Windows)
866867
@_transparent
867868
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
868869
let (value, sign) = lgamma(x.native)
869870
return (CGFloat(value), sign)
870871
}
872+
#endif
871873

872874
@_transparent
873875
public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int) {

Foundation/FoundationErrors.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ internal func _NSErrorWithErrno(_ posixErrno : Int32, reading : Bool, path : Str
179179
case ENOENT: cocoaError = .fileNoSuchFile
180180
case EPERM, EACCES: cocoaError = .fileWriteNoPermission
181181
case ENAMETOOLONG: cocoaError = .fileWriteInvalidFileName
182+
#if os(Windows)
183+
case ENOSPC: cocoaError = .fileWriteOutOfSpace
184+
#else
182185
case EDQUOT, ENOSPC: cocoaError = .fileWriteOutOfSpace
186+
#endif
183187
case EROFS: cocoaError = .fileWriteVolumeReadOnly
184188
case EEXIST: cocoaError = .fileWriteFileExists
185189
default: cocoaError = .fileWriteUnknown

Foundation/NSDate.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public var NSTimeIntervalSince1970: Double {
1515
return 978307200.0
1616
}
1717

18+
#if os(Windows)
19+
extension TimeInterval {
20+
init(_ ftTime: FILETIME) {
21+
self = Double((ftTime.dwHighDateTime << 32) | ftTime.dwLowDateTime) - NSTimeIntervalSince1970;
22+
}
23+
}
24+
#endif
25+
1826
open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
1927
typealias CFType = CFDate
2028

@@ -52,6 +60,20 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
5260
return Date().timeIntervalSinceReferenceDate
5361
}
5462

63+
#if os(Windows)
64+
public convenience override init() {
65+
var stTime: SYSTEMTIME = SYSTEMTIME()
66+
var ftTime: FILETIME = FILETIME()
67+
68+
GetSystemTime(&stTime)
69+
SystemTimeToFileTime(&stTime, &ftTime)
70+
71+
let timestamp: UInt64 = (UInt64(ftTime.dwLowDateTime) << 0)
72+
+ (UInt64(ftTime.dwHighDateTime) << 32)
73+
- UInt64(NSTimeIntervalSince1970)
74+
self.init(timeIntervalSinceReferenceDate: TimeInterval(timestamp))
75+
}
76+
#else
5577
public convenience override init() {
5678
var tv = timeval()
5779
let _ = withUnsafeMutablePointer(to: &tv) { t in
@@ -61,6 +83,7 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
6183
timestamp += TimeInterval(tv.tv_usec) / 1000000.0
6284
self.init(timeIntervalSinceReferenceDate: timestamp)
6385
}
86+
#endif
6487

6588
public required init(timeIntervalSinceReferenceDate ti: TimeInterval) {
6689
_timeIntervalSinceReferenceDate = ti

Foundation/NSPathUtilities.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,19 @@ public func NSFullUserName() -> String {
607607

608608
internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) {
609609
let template = filePath + ".tmp.XXXXXX"
610+
#if os(Windows)
611+
let maxLength: Int = Int(MAX_PATH + 1)
612+
var buf: [UInt16] = Array<UInt16>(repeating: 0, count: maxLength)
613+
let length = GetTempPathW(DWORD(MAX_PATH), &buf)
614+
precondition(length <= MAX_PATH - 14, "temp path too long")
615+
if GetTempFileNameW(buf, unsafeBitCast("SCF".utf16, to: LPCWSTR?.self), 0,
616+
&buf) == FALSE {
617+
throw _NSErrorWithErrno(Int32(GetLastError()), reading: false,
618+
path: filePath)
619+
}
620+
let pathResult = FileManager.default.string(withFileSystemRepresentation: String(decoding: buf, as: UTF16.self), length: wcslen(buf))
621+
let fd = open(pathResult, _O_CREAT)
622+
#else
610623
let maxLength = Int(PATH_MAX) + 1
611624
var buf = [Int8](repeating: 0, count: maxLength)
612625
let _ = template._nsObject.getFileSystemRepresentation(&buf, maxLength: maxLength)
@@ -615,6 +628,7 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
615628
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
616629
}
617630
let pathResult = FileManager.default.string(withFileSystemRepresentation: buf, length: Int(strlen(buf)))
631+
#endif
618632
return (fd, pathResult)
619633
}
620634

Foundation/NSPlatform.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
fileprivate let _NSPageSize = Int(vm_page_size)
1212
#elseif os(Linux) || os(Android)
1313
fileprivate let _NSPageSize = Int(getpagesize())
14+
#elseif os(Windows)
15+
fileprivate var _NSPageSize: Int {
16+
var siInfo: SYSTEM_INFO = SYSTEM_INFO()
17+
GetSystemInfo(&siInfo)
18+
return Int(siInfo.dwPageSize)
19+
}
1420
#endif
1521

1622
public func NSPageSize() -> Int {

Foundation/NSSwiftRuntime.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import CoreFoundation
2626
import WinSDK
2727
#endif
2828

29-
#if os(Android) // shim required for bzero
29+
// shim required for bzero
30+
#if os(Android) || os(Windows)
3031
@_transparent func bzero(_ ptr: UnsafeMutableRawPointer, _ size: size_t) {
3132
memset(ptr, 0, size)
3233
}

Foundation/NSURL.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,13 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
556556

557557
// Memory leak. See https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Issues.md
558558
open var fileSystemRepresentation: UnsafePointer<Int8> {
559-
559+
560+
#if os(Windows)
561+
let bufSize = Int(MAX_PATH + 1)
562+
#else
560563
let bufSize = Int(PATH_MAX + 1)
561-
564+
#endif
565+
562566
let _fsrBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufSize)
563567
_fsrBuffer.initialize(repeating: 0, count: bufSize)
564568

Foundation/Stream.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ open class InputStream: Stream {
153153
}
154154

155155
open override var streamStatus: Status {
156+
#if os(Windows)
157+
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream).rawValue))!
158+
#else
156159
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream)))!
160+
#endif
157161
}
158162

159163
open override var streamError: Error? {
@@ -205,7 +209,11 @@ open class OutputStream : Stream {
205209
}
206210

207211
open override var streamStatus: Status {
212+
#if os(Windows)
213+
return Stream.Status(rawValue: UInt(CFWriteStreamGetStatus(_stream).rawValue))!
214+
#else
208215
return Stream.Status(rawValue: UInt(CFWriteStreamGetStatus(_stream)))!
216+
#endif
209217
}
210218

211219
open class func toMemory() -> Self {

Foundation/URLSession/Configuration.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ internal extension URLSession._Configuration {
101101
// Configure NSURLRequests
102102
internal extension URLSession._Configuration {
103103
func configure(request: URLRequest) -> URLRequest {
104-
var request = request
105104
return setCookies(on: request)
106105
}
107106

Foundation/URLSession/libcurl/EasyHandle.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,13 @@ internal extension _EasyHandle {
374374
/// errno number from last connect failure
375375
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLINFO_OS_ERRNO.html
376376
var connectFailureErrno: Int {
377+
#if os(Windows) && (arch(arm64) || arch(x86_64))
378+
var errno = Int32()
379+
#else
377380
var errno = Int()
381+
#endif
378382
try! CFURLSession_easy_getinfo_long(rawHandle, CFURLSessionInfoOS_ERRNO, &errno).asError()
379-
return errno
383+
return numericCast(errno)
380384
}
381385
}
382386

Foundation/URLSession/libcurl/MultiHandle.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,14 @@ fileprivate extension URLSession._MultiHandle {
8383
}.asError()
8484
// Timeout:
8585
try! CFURLSession_multi_setopt_ptr(rawHandle, CFURLSessionMultiOptionTIMERDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError()
86-
try! CFURLSession_multi_setopt_tf(rawHandle, CFURLSessionMultiOptionTIMERFUNCTION) { (_, timeout: Int, userdata: UnsafeMutableRawPointer?) -> Int32 in
86+
#if os(Windows) && (arch(arm64) || arch(x86_64))
87+
typealias CFURLSessionMultiOption = Int32
88+
#else
89+
typealias CFURLSessionMultiOption = Int
90+
#endif
91+
try! CFURLSession_multi_setopt_tf(rawHandle, CFURLSessionMultiOptionTIMERFUNCTION) { (_, timeout: CFURLSessionMultiOption, userdata: UnsafeMutableRawPointer?) -> Int32 in
8792
guard let handle = URLSession._MultiHandle.from(callbackUserData: userdata) else { fatalError() }
88-
handle.updateTimeoutTimer(to: timeout)
93+
handle.updateTimeoutTimer(to: numericCast(timeout))
8994
return 0
9095
}.asError()
9196
}

0 commit comments

Comments
 (0)