Skip to content

Foundation: sundry fixes for Windows port #1863

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

Merged
merged 1 commit into from
Jan 31, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Foundation/CGFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -863,11 +863,13 @@ public func scalbn(_ x: CGFloat, _ n: Int) -> CGFloat {
return CGFloat(scalbn(x.native, n))
}

#if !os(Windows)
@_transparent
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
let (value, sign) = lgamma(x.native)
return (CGFloat(value), sign)
}
#endif

@_transparent
public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int) {
Expand Down
4 changes: 4 additions & 0 deletions Foundation/FoundationErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ internal func _NSErrorWithErrno(_ posixErrno : Int32, reading : Bool, path : Str
case ENOENT: cocoaError = .fileNoSuchFile
case EPERM, EACCES: cocoaError = .fileWriteNoPermission
case ENAMETOOLONG: cocoaError = .fileWriteInvalidFileName
#if os(Windows)
case ENOSPC: cocoaError = .fileWriteOutOfSpace
#else
case EDQUOT, ENOSPC: cocoaError = .fileWriteOutOfSpace
#endif
case EROFS: cocoaError = .fileWriteVolumeReadOnly
case EEXIST: cocoaError = .fileWriteFileExists
default: cocoaError = .fileWriteUnknown
Expand Down
23 changes: 23 additions & 0 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public var NSTimeIntervalSince1970: Double {
return 978307200.0
}

#if os(Windows)
extension TimeInterval {
init(_ ftTime: FILETIME) {
self = Double((ftTime.dwHighDateTime << 32) | ftTime.dwLowDateTime) - NSTimeIntervalSince1970;
}
}
#endif

open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
typealias CFType = CFDate

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

#if os(Windows)
public convenience override init() {
var stTime: SYSTEMTIME = SYSTEMTIME()
var ftTime: FILETIME = FILETIME()

GetSystemTime(&stTime)
SystemTimeToFileTime(&stTime, &ftTime)

let timestamp: UInt64 = (UInt64(ftTime.dwLowDateTime) << 0)
+ (UInt64(ftTime.dwHighDateTime) << 32)
- UInt64(NSTimeIntervalSince1970)
self.init(timeIntervalSinceReferenceDate: TimeInterval(timestamp))
}
#else
public convenience override init() {
var tv = timeval()
let _ = withUnsafeMutablePointer(to: &tv) { t in
Expand All @@ -61,6 +83,7 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
timestamp += TimeInterval(tv.tv_usec) / 1000000.0
self.init(timeIntervalSinceReferenceDate: timestamp)
}
#endif

public required init(timeIntervalSinceReferenceDate ti: TimeInterval) {
_timeIntervalSinceReferenceDate = ti
Expand Down
14 changes: 14 additions & 0 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,19 @@ public func NSFullUserName() -> String {

internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) {
let template = filePath + ".tmp.XXXXXX"
#if os(Windows)
let maxLength: Int = Int(MAX_PATH + 1)
var buf: [UInt16] = Array<UInt16>(repeating: 0, count: maxLength)
let length = GetTempPathW(DWORD(MAX_PATH), &buf)
precondition(length <= MAX_PATH - 14, "temp path too long")
if GetTempFileNameW(buf, unsafeBitCast("SCF".utf16, to: LPCWSTR?.self), 0,
&buf) == FALSE {
throw _NSErrorWithErrno(Int32(GetLastError()), reading: false,
path: filePath)
}
let pathResult = FileManager.default.string(withFileSystemRepresentation: String(decoding: buf, as: UTF16.self), length: wcslen(buf))
let fd = open(pathResult, _O_CREAT)
#else
let maxLength = Int(PATH_MAX) + 1
var buf = [Int8](repeating: 0, count: maxLength)
let _ = template._nsObject.getFileSystemRepresentation(&buf, maxLength: maxLength)
Expand All @@ -615,6 +628,7 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
throw _NSErrorWithErrno(errno, reading: false, path: filePath)
}
let pathResult = FileManager.default.string(withFileSystemRepresentation: buf, length: Int(strlen(buf)))
#endif
return (fd, pathResult)
}

Expand Down
6 changes: 6 additions & 0 deletions Foundation/NSPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
fileprivate let _NSPageSize = Int(vm_page_size)
#elseif os(Linux) || os(Android)
fileprivate let _NSPageSize = Int(getpagesize())
#elseif os(Windows)
fileprivate var _NSPageSize: Int {
var siInfo: SYSTEM_INFO = SYSTEM_INFO()
GetSystemInfo(&siInfo)
return Int(siInfo.dwPageSize)
}
#endif

public func NSPageSize() -> Int {
Expand Down
3 changes: 2 additions & 1 deletion Foundation/NSSwiftRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import CoreFoundation
import WinSDK
#endif

#if os(Android) // shim required for bzero
// shim required for bzero
#if os(Android) || os(Windows)
@_transparent func bzero(_ ptr: UnsafeMutableRawPointer, _ size: size_t) {
memset(ptr, 0, size)
}
Expand Down
8 changes: 6 additions & 2 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,13 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {

// Memory leak. See https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Issues.md
open var fileSystemRepresentation: UnsafePointer<Int8> {


#if os(Windows)
let bufSize = Int(MAX_PATH + 1)
#else
let bufSize = Int(PATH_MAX + 1)

#endif

let _fsrBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufSize)
_fsrBuffer.initialize(repeating: 0, count: bufSize)

Expand Down
8 changes: 8 additions & 0 deletions Foundation/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ open class InputStream: Stream {
}

open override var streamStatus: Status {
#if os(Windows)
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream).rawValue))!
#else
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream)))!
#endif
}

open override var streamError: Error? {
Expand Down Expand Up @@ -205,7 +209,11 @@ open class OutputStream : Stream {
}

open override var streamStatus: Status {
#if os(Windows)
return Stream.Status(rawValue: UInt(CFWriteStreamGetStatus(_stream).rawValue))!
#else
return Stream.Status(rawValue: UInt(CFWriteStreamGetStatus(_stream)))!
#endif
}

open class func toMemory() -> Self {
Expand Down
1 change: 0 additions & 1 deletion Foundation/URLSession/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ internal extension URLSession._Configuration {
// Configure NSURLRequests
internal extension URLSession._Configuration {
func configure(request: URLRequest) -> URLRequest {
var request = request
return setCookies(on: request)
}

Expand Down
6 changes: 5 additions & 1 deletion Foundation/URLSession/libcurl/EasyHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,13 @@ internal extension _EasyHandle {
/// errno number from last connect failure
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLINFO_OS_ERRNO.html
var connectFailureErrno: Int {
#if os(Windows) && (arch(arm64) || arch(x86_64))
var errno = Int32()
#else
var errno = Int()
#endif
try! CFURLSession_easy_getinfo_long(rawHandle, CFURLSessionInfoOS_ERRNO, &errno).asError()
return errno
return numericCast(errno)
}
}

Expand Down
9 changes: 7 additions & 2 deletions Foundation/URLSession/libcurl/MultiHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ fileprivate extension URLSession._MultiHandle {
}.asError()
// Timeout:
try! CFURLSession_multi_setopt_ptr(rawHandle, CFURLSessionMultiOptionTIMERDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError()
try! CFURLSession_multi_setopt_tf(rawHandle, CFURLSessionMultiOptionTIMERFUNCTION) { (_, timeout: Int, userdata: UnsafeMutableRawPointer?) -> Int32 in
#if os(Windows) && (arch(arm64) || arch(x86_64))
typealias CFURLSessionMultiOption = Int32
#else
typealias CFURLSessionMultiOption = Int
#endif
try! CFURLSession_multi_setopt_tf(rawHandle, CFURLSessionMultiOptionTIMERFUNCTION) { (_, timeout: CFURLSessionMultiOption, userdata: UnsafeMutableRawPointer?) -> Int32 in
guard let handle = URLSession._MultiHandle.from(callbackUserData: userdata) else { fatalError() }
handle.updateTimeoutTimer(to: timeout)
handle.updateTimeoutTimer(to: numericCast(timeout))
return 0
}.asError()
}
Expand Down