Compare commits

..
Author SHA1 Message Date
Max Desiatov 94090e1c5c WIP: fix errors when building for Embedded Swift
rdar://159277249
2025-08-27 14:56:23 +01:00
18 changed files with 155 additions and 143 deletions

No files matched your search

+4 -4
View File
@@ -25,7 +25,7 @@ struct Available {
self.name = "System"
self.version = version
self.osAvailability = osAvailability
self.sourceAvailability = "macOS 14.0, iOS 8.0, watchOS 2.0, tvOS 9.0, visionOS 1.0"
self.sourceAvailability = "macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, visionOS 1.0"
}
var swiftSetting: SwiftSetting {
@@ -81,7 +81,7 @@ let swiftSettings = swiftSettingsAvailability + swiftSettingsCI + [
"SYSTEM_PACKAGE_DARWIN",
.when(platforms: [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .visionOS])),
.define("SYSTEM_PACKAGE"),
// .define("ENABLE_MOCKING", .when(configuration: .debug)),
.define("ENABLE_MOCKING", .when(configuration: .debug)),
.enableExperimentalFeature("Lifetimes"),
]
@@ -97,8 +97,8 @@ let platforms: [SupportedPlatform] = [
.tvOS("26"),
.visionOS("26"),
]
#else
let platforms: [SupportedPlatform] = [.macOS(.v14)]
#else
let platforms: [SupportedPlatform]? = nil
#endif
#if os(Linux)
+12 -4
View File
@@ -1390,10 +1390,6 @@ public struct Errno: RawRepresentable, Error, Hashable {
public static var EOPNOTSUPP: Errno { notSupportedOnSocket }
}
#if !hasFeature(Embedded)
extension FileDescriptor: Codable {}
#endif
// Constants defined in header but not man page
@available(System 0.0.1, *)
extension Errno {
@@ -1566,6 +1562,8 @@ extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
public var debugDescription: String { self.description }
}
#if !$Embedded
@available(System 0.0.1, *)
extension Errno {
@_alwaysEmitIntoClient
@@ -1575,3 +1573,13 @@ extension Errno {
}
}
@available(System 0.0.1, *)
extension Errno: Codable {}
public typealias SystemError = any Error
#else
public typealias SystemError = Errno
#endif // !$Embedded
+13 -9
View File
@@ -25,10 +25,6 @@ public struct FileDescriptor: RawRepresentable, Hashable {
public init(rawValue: CInt) { self.rawValue = rawValue }
}
#if !hasFeature(Embedded)
extension FileDescriptor: Codable {}
#endif
// Standard file descriptors.
@available(System 0.0.1, *)
extension FileDescriptor {
@@ -406,13 +402,21 @@ extension FileDescriptor {
}
}
#if !hasFeature(Embedded)
extension FileDescriptor.AccessMode: Codable { }
#if !$Embedded
extension FileDescriptor.OpenOptions: Codable { }
@available(System 0.0.1, *)
extension FileDescriptor: Codable {}
extension FileDescriptor.SeekOrigin: Codable { }
#endif
@available(System 0.0.1, *)
extension FileDescriptor.AccessMode: Codable {}
@available(System 0.0.1, *)
extension FileDescriptor.OpenOptions: Codable {}
@available(System 0.0.1, *)
extension FileDescriptor.SeekOrigin: Codable {}
#endif // !$Embedded
@available(System 0.0.1, *)
extension FileDescriptor.AccessMode
+28 -28
View File
@@ -30,24 +30,24 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
#if !os(Windows)
return try path.withCString {
return try path.withCString { (cString) throws(SystemError) in
try FileDescriptor.open(
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
cString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
}
#else
return try path.withPlatformString {
#else
return try path.withPlatformString { (platformString) throws(SystemError) in
try FileDescriptor.open(
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
platformString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
}
#endif
}
#if !os(Windows)
// On Darwin, `CInterop.PlatformChar` is less available than
#if !os(Windows)
// On Darwin, `CInterop.PlatformChar` is less available than
// `FileDescriptor.open`, so we need to use `CChar` instead.
/// Opens or creates a file for reading or writing.
///
/// - Parameters:
@@ -69,7 +69,7 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try FileDescriptor._open(
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
).get()
@@ -116,7 +116,7 @@ extension FileDescriptor {
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
permissions: FilePermissions? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try FileDescriptor._open(
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
).get()
@@ -149,7 +149,7 @@ extension FileDescriptor {
///
/// The corresponding C function is `close`.
@_alwaysEmitIntoClient
public func close() throws { try _close().get() }
public func close() throws(SystemError) { try _close().get() }
@usableFromInline
internal func _close() -> Result<(), Errno> {
@@ -169,7 +169,7 @@ extension FileDescriptor {
@discardableResult
public func seek(
offset: Int64, from whence: FileDescriptor.SeekOrigin
) throws -> Int64 {
) throws(SystemError) -> Int64 {
try _seek(offset: offset, from: whence).get()
}
@@ -187,7 +187,7 @@ extension FileDescriptor {
@available(*, unavailable, renamed: "seek")
public func lseek(
offset: Int64, from whence: FileDescriptor.SeekOrigin
) throws -> Int64 {
) throws(SystemError) -> Int64 {
try seek(offset: offset, from: whence)
}
@@ -214,7 +214,7 @@ extension FileDescriptor {
public func read(
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@@ -222,7 +222,7 @@ extension FileDescriptor {
internal func _read(
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool
) throws -> Result<Int, Errno> {
) throws(SystemError) -> Result<Int, Errno> {
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
system_read(self.rawValue, buffer.baseAddress, buffer.count)
}
@@ -251,7 +251,7 @@ extension FileDescriptor {
fromAbsoluteOffset offset: Int64,
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _read(
fromAbsoluteOffset: offset,
into: buffer,
@@ -276,7 +276,7 @@ extension FileDescriptor {
fromAbsoluteOffset offset: Int64,
into buffer: UnsafeMutableRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try read(
fromAbsoluteOffset: offset,
into: buffer,
@@ -303,7 +303,7 @@ extension FileDescriptor {
public func write(
_ buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _write(buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@@ -337,7 +337,7 @@ extension FileDescriptor {
toAbsoluteOffset offset: Int64,
_ buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try _write(toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@@ -359,7 +359,7 @@ extension FileDescriptor {
toAbsoluteOffset offset: Int64,
into buffer: UnsafeRawBufferPointer,
retryOnInterrupt: Bool = true
) throws -> Int {
) throws(SystemError) -> Int {
try write(
toAbsoluteOffset: offset,
buffer,
@@ -402,7 +402,7 @@ extension FileDescriptor {
public func duplicate(
as target: FileDescriptor? = nil,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(SystemError) -> FileDescriptor {
try _duplicate(as: target, retryOnInterrupt: retryOnInterrupt).get()
}
@@ -411,7 +411,7 @@ extension FileDescriptor {
internal func _duplicate(
as target: FileDescriptor?,
retryOnInterrupt: Bool
) throws -> Result<FileDescriptor, Errno> {
) throws(SystemError) -> Result<FileDescriptor, Errno> {
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
if let target = target {
return system_dup2(self.rawValue, target.rawValue)
@@ -422,13 +422,13 @@ extension FileDescriptor {
@_alwaysEmitIntoClient
@available(*, unavailable, renamed: "duplicate")
public func dup() throws -> FileDescriptor {
public func dup() throws(SystemError) -> FileDescriptor {
fatalError("Not implemented")
}
@_alwaysEmitIntoClient
@available(*, unavailable, renamed: "duplicate")
public func dup2() throws -> FileDescriptor {
public func dup2() throws(SystemError) -> FileDescriptor {
fatalError("Not implemented")
}
}
@@ -444,7 +444,7 @@ extension FileDescriptor {
/// The corresponding C function is `pipe`.
@_alwaysEmitIntoClient
@available(System 1.1.0, *)
public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
public static func pipe() throws(SystemError) -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
try _pipe().get()
}
@@ -490,7 +490,7 @@ extension FileDescriptor {
public func resize(
to newSize: Int64,
retryOnInterrupt: Bool = true
) throws {
) throws(SystemError) {
try _resize(
to: newSize,
retryOnInterrupt: retryOnInterrupt
@@ -537,7 +537,7 @@ extension FilePermissions {
/// afterwards, because of the way reading the creation mask works.
internal static func withCreationMask<R>(
_ permissions: FilePermissions,
body: () throws -> R
body: () throws(SystemError) -> R
) rethrows -> R {
let oldMask = _umask(permissions.rawValue)
defer {
+4 -2
View File
@@ -68,7 +68,8 @@ extension FilePath {
@available(System 0.0.1, *)
extension FilePath: Hashable {}
#if !hasFeature(Embedded)
#if !$Embedded
@available(System 0.0.1, *)
extension FilePath: Codable {
// Encoder is synthesized; it probably should have been explicit and used
@@ -89,4 +90,5 @@ extension FilePath: Codable {
}
}
}
#endif
#endif // !$Embedded
@@ -128,15 +128,11 @@ extension SystemString {
}
}
#if hasFeature(Embedded)
protocol _StrSliceCodable {}
#else
protocol _StrSliceCodable: Codable {}
#endif
#if !$Embedded
// Unifying protocol for common functionality between roots, components,
// and views onto SystemString and FilePath.
internal protocol _StrSlice: _PlatformStringable, Hashable, _StrSliceCodable {
internal protocol _StrSlice: _PlatformStringable, Hashable, Codable {
var _storage: SystemString { get }
var _range: Range<SystemString.Index> { get }
@@ -145,6 +141,19 @@ internal protocol _StrSlice: _PlatformStringable, Hashable, _StrSliceCodable {
func _invariantCheck()
}
#else
internal protocol _StrSlice: _PlatformStringable, Hashable {
var _storage: SystemString { get }
var _range: Range<SystemString.Index> { get }
init?(_ str: SystemString)
func _invariantCheck()
}
#endif // !$Embedded
extension _StrSlice {
internal var _slice: Slice<SystemString> {
Slice(base: _storage, bounds: _range)
+5 -5
View File
@@ -64,7 +64,7 @@ extension FilePath {
}
#if !os(Windows)
// Note: This function should have been opaque, but it shipped as
// Note: This function should have been opaque, but it shipped as
// `@_alwaysEmitIntoClient` in macOS 12/iOS 15, and now it is stuck
// this way forever. (Or until the language provides a way for us
// to declare separate availability for a function's exported symbol
@@ -84,7 +84,7 @@ extension FilePath {
/// Don't try to store the pointer for later use.
@_alwaysEmitIntoClient
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) rethrows -> Result {
return try withCString(body)
}
@@ -102,7 +102,7 @@ extension FilePath {
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) rethrows -> Result {
return try _withPlatformString(body)
}
@@ -595,8 +595,8 @@ extension FilePath {
/// For backwards compatibility only. This function is equivalent to
/// the preferred `withPlatformString`.
public func withCString<Result>(
_ body: (UnsafePointer<CChar>) throws -> Result
) rethrows -> Result {
_ body: (UnsafePointer<CChar>) throws(SystemError) -> Result
) throws(SystemError) -> Result {
return try _withPlatformString(body)
}
}
+6 -6
View File
@@ -20,8 +20,8 @@
/// deletes the directory and all of its contents before returning.
internal func withTemporaryFilePath<R>(
basename: FilePath.Component,
_ body: (FilePath) throws -> R
) throws -> R {
_ body: (FilePath) throws(SystemError) -> R
) throws(SystemError) -> R {
let temporaryDir = try createUniqueTemporaryDirectory(basename: basename)
defer {
try? _recursiveRemove(at: temporaryDir)
@@ -44,9 +44,9 @@ fileprivate let base64 = Array<UInt8>(
///
/// This function will throw if there is an error, except if the error
/// is that the directory exists, in which case it returns `false`.
fileprivate func makeLockedDownDirectory(at path: FilePath) throws -> Bool {
return try path.withPlatformString {
if system_mkdir($0, 0o700) == 0 {
fileprivate func makeLockedDownDirectory(at path: FilePath) throws(SystemError) -> Bool {
return try path.withPlatformString { (platformString) throws(SystemError) in
if system_mkdir(platformString, 0o700) == 0 {
return true
}
let err = system_errno
@@ -83,7 +83,7 @@ fileprivate func createRandomString(length: Int) -> String {
/// string of characters.
fileprivate func createUniqueTemporaryDirectory(
basename: FilePath.Component
) throws -> FilePath {
) throws(SystemError) -> FilePath {
var tempDir = try _getTemporaryDirectory()
tempDir.append(basename)
@@ -10,7 +10,7 @@
#if !os(Windows)
/// Get the path to the system temporary directory.
internal func _getTemporaryDirectory() throws -> FilePath {
internal func _getTemporaryDirectory() throws(SystemError) -> FilePath {
guard let tmp = system_getenv("TMPDIR") else {
return "/tmp"
}
@@ -26,22 +26,22 @@ internal func _getTemporaryDirectory() throws -> FilePath {
/// Removes a directory completely, including all of its contents.
internal func _recursiveRemove(
at path: FilePath
) throws {
) throws(SystemError) {
let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory)
defer {
try? dirfd.close()
}
let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0)
try withUnsafeBytes(of: dot) {
try withUnsafeBytes(of: dot) { (bytes) throws(SystemError) in
try recursiveRemove(
in: dirfd.rawValue,
name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress!
name: bytes.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress!
)
}
try path.withPlatformString {
if system_rmdir($0) != 0 {
try path.withPlatformString { (platformString) throws(SystemError) in
if system_rmdir(platformString) != 0 {
throw Errno.current
}
}
@@ -81,8 +81,8 @@ fileprivate func impl_opendirat(
fileprivate func forEachFile(
in dirfd: CInt,
subdir: UnsafePointer<CInterop.PlatformChar>,
_ body: (system_dirent) throws -> ()
) throws {
_ body: (system_dirent) throws(SystemError) -> ()
) throws(SystemError) {
guard let dir = impl_opendirat(dirfd, subdir) else {
throw Errno.current
}
@@ -113,7 +113,7 @@ fileprivate func forEachFile(
fileprivate func recursiveRemove(
in dirfd: CInt,
name: UnsafePointer<CInterop.PlatformChar>
) throws {
) throws(SystemError) {
// First, deal with subdirectories
try forEachFile(in: dirfd, subdir: name) { dirent in
if dirent.d_type == SYSTEM_DT_DIR {
+5 -4
View File
@@ -132,10 +132,6 @@ public struct FilePermissions: OptionSet, Sendable, Hashable {
public static var saveText: FilePermissions { .init(rawValue: 0o1000) }
}
#if !hasFeature(Embedded)
extension FilePermissions: Codable { }
#endif
@available(System 0.0.1, *)
extension FilePermissions
: CustomStringConvertible, CustomDebugStringConvertible
@@ -176,3 +172,8 @@ extension FilePermissions
/// A textual representation of the file permissions, suitable for debugging.
public var debugDescription: String { self.description }
}
#if !$Embedded
@available(System 0.0.1, *)
extension FilePermissions: Codable {}
#endif
+1 -5
View File
@@ -13,7 +13,7 @@ public extension IORing {
}
public extension IORing.Completion {
struct Flags: OptionSet, Hashable {
struct Flags: OptionSet, Hashable, Codable {
public let rawValue: UInt32
@inlinable public init(rawValue: UInt32) {
@@ -32,10 +32,6 @@ public extension IORing.Completion {
}
}
#if !hasFeature(Embedded)
extension IORing.Completion.Flags: Codable {}
#endif
public extension IORing.Completion {
@inlinable var context: UInt64 {
get {
+3 -13
View File
@@ -45,7 +45,7 @@ extension RawIORequest {
case unlinkAt = 36
}
public struct Flags: OptionSet, Hashable {
public struct Flags: OptionSet, Hashable, Codable {
public let rawValue: UInt8
@inlinable public init(rawValue: UInt8) {
@@ -135,7 +135,7 @@ extension RawIORequest {
// splice_flags
}
public struct ReadWriteFlags: OptionSet, Hashable {
public struct ReadWriteFlags: OptionSet, Hashable, Codable {
public var rawValue: UInt32
@inlinable public init(rawValue: UInt32) {
self.rawValue = rawValue
@@ -156,7 +156,7 @@ extension RawIORequest {
@inlinable public static var append: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 4) }
}
public struct TimeOutFlags: OptionSet, Hashable {
public struct TimeOutFlags: OptionSet, Hashable, Codable {
public var rawValue: UInt32
@inlinable public init(rawValue: UInt32) {
@@ -168,16 +168,6 @@ extension RawIORequest {
}
}
#if !hasFeature(Embedded)
extension IORing.Completion.Flags: Codable {}
extension RawIORequest.Flags: Codable {}
extension RawIORequest.ReadWriteFlags: Codable {}
extension RawIORequest.TimeOutFlags: Codable {}
#endif
extension RawIORequest {
@inlinable
static func nop() -> RawIORequest {
+3 -10
View File
@@ -37,17 +37,10 @@ internal typealias _COffT = off_t
// MARK: syscalls and variables
#if SYSTEM_PACKAGE_DARWIN
#if hasFeature(Embedded)
internal var system_errno: CInt {
get { _DarwinFoundation1.errno }
set { _DarwinFoundation1.errno = newValue }
}
#else
internal var system_errno: CInt {
get { Darwin.errno }
set { Darwin.errno = newValue }
}
#endif
#elseif os(Windows)
internal var system_errno: CInt {
get {
@@ -123,9 +116,9 @@ internal func system_memset(
// Interop between String and platfrom string
extension String {
internal func _withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
internal func _withPlatformString<Success, Failure: Error>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(Failure) -> Success
) throws(Failure) -> Success {
// Need to #if because CChar may be signed
#if os(Windows)
return try withCString(encodedAs: CInterop.PlatformUnicodeEncoding.self, body)
+9 -9
View File
@@ -17,7 +17,7 @@
// enabled in testing builds of System currently, to minimize runtime overhead of release builds.
//
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
internal struct Trace {
internal struct Entry {
@@ -115,12 +115,12 @@ extension MockingDriver {
}
}
#endif // ENABLE_MOCKING
#endif // ENABLE_MOCKING && !$Embedded
@inline(__always)
internal var mockingEnabled: Bool {
// Fast constant-foldable check for release builds
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
return contextualMockingEnabled
#else
return false
@@ -129,15 +129,15 @@ internal var mockingEnabled: Bool {
@inline(__always)
internal var forceWindowsPaths: Bool? {
#if !ENABLE_MOCKING
return nil
#else
#if ENABLE_MOCKING && !$Embedded
return MockingDriver.forceWindowsPaths
#else
return nil
#endif
}
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
// Strip the mock_system prefix and the arg list suffix
private func originalSyscallName(_ function: String) -> String {
// `function` must be of format `system_<name>(<parameters>)`
@@ -193,12 +193,12 @@ internal func _mockOffT(
) -> _COffT {
_COffT(mockImpl(name: name, path: path, args))
}
#endif // ENABLE_MOCKING
#endif // ENABLE_MOCKING && !$Embedded
// Force paths to be treated as Windows syntactically if `enabled` is
// true, and as POSIX syntactically if not.
internal func _withWindowsPaths(enabled: Bool, _ body: () -> ()) {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
MockingDriver.withMockingEnabled { driver in
driver.forceWindowsSyntaxForPaths = enabled
body()
+16 -16
View File
@@ -31,7 +31,7 @@ import Android
internal func system_open(
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled {
return _mock(path: path, oflag)
}
@@ -43,7 +43,7 @@ internal func system_open(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32, _ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled {
return _mock(path: path, oflag, mode)
}
@@ -53,7 +53,7 @@ internal func system_open(
// close
internal func system_close(_ fd: Int32) -> Int32 {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fd) }
#endif
return close(fd)
@@ -63,7 +63,7 @@ internal func system_close(_ fd: Int32) -> Int32 {
internal func system_read(
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int
) -> Int {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
#endif
return read(fd, buf, nbyte)
@@ -73,7 +73,7 @@ internal func system_read(
internal func system_pread(
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int, _ offset: off_t
) -> Int {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
#endif
#if os(Android)
@@ -91,7 +91,7 @@ internal func system_pread(
internal func system_lseek(
_ fd: Int32, _ off: off_t, _ whence: Int32
) -> off_t {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mockOffT(fd, off, whence) }
#endif
return lseek(fd, off, whence)
@@ -101,7 +101,7 @@ internal func system_lseek(
internal func system_write(
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int
) -> Int {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
#endif
return write(fd, buf, nbyte)
@@ -111,7 +111,7 @@ internal func system_write(
internal func system_pwrite(
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int, _ offset: off_t
) -> Int {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
#endif
#if os(Android)
@@ -127,14 +127,14 @@ internal func system_pwrite(
#if !os(WASI)
internal func system_dup(_ fd: Int32) -> Int32 {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fd) }
#endif
return dup(fd)
}
internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fd, fd2) }
#endif
return dup2(fd, fd2)
@@ -143,7 +143,7 @@ internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
#if !os(WASI)
internal func system_pipe(_ fds: UnsafeMutablePointer<Int32>) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fds) }
#endif
return pipe(fds)
@@ -151,7 +151,7 @@ internal func system_pipe(_ fds: UnsafeMutablePointer<Int32>) -> CInt {
#endif
internal func system_ftruncate(_ fd: Int32, _ length: off_t) -> Int32 {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fd, length) }
#endif
return ftruncate(fd, length)
@@ -161,7 +161,7 @@ internal func system_mkdir(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ mode: CInterop.Mode
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(path: path, mode) }
#endif
return mkdir(path, mode)
@@ -170,7 +170,7 @@ internal func system_mkdir(
internal func system_rmdir(
_ path: UnsafePointer<CInterop.PlatformChar>
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(path: path) }
#endif
return rmdir(path)
@@ -208,7 +208,7 @@ internal func system_unlinkat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ flag: CInt
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled { return _mock(fd, path, flag) }
#endif
return unlinkat(fd, path, flag)
@@ -247,7 +247,7 @@ internal func system_openat(
_ path: UnsafePointer<CInterop.PlatformChar>,
_ oflag: Int32
) -> CInt {
#if ENABLE_MOCKING
#if ENABLE_MOCKING && !$Embedded
if mockingEnabled {
return _mock(fd, path, oflag)
}
+4 -4
View File
@@ -158,8 +158,8 @@ extension String {
/// only during the execution of this method.
/// Don't try to store the pointer for later use.
public func withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) throws(SystemError) -> Result {
try _withPlatformString(body)
}
@@ -189,8 +189,8 @@ extension CInterop.PlatformUnicodeEncoding.CodeUnit {
internal protocol _PlatformStringable {
func _withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
) throws(SystemError) -> Result
init?(_platformString: UnsafePointer<CInterop.PlatformChar>)
}
+13 -8
View File
@@ -8,7 +8,8 @@
*/
// A platform-native character representation, currently used for file paths
internal struct SystemChar: RawRepresentable, Sendable, Comparable, Hashable {
internal struct SystemChar:
RawRepresentable, Sendable, Comparable, Hashable {
internal typealias RawValue = CInterop.PlatformChar
internal var rawValue: RawValue
@@ -22,9 +23,11 @@ internal struct SystemChar: RawRepresentable, Sendable, Comparable, Hashable {
}
}
#if !hasFeature(Embedded)
#if !$Embedded
extension SystemChar: Codable {}
#endif
#endif // !$Embedded
extension SystemChar {
internal init(ascii: Unicode.Scalar) {
@@ -107,7 +110,7 @@ extension SystemString {
}
return true
}
fileprivate func _invariantCheck() {
#if DEBUG
precondition(_invariantsSatisfied())
@@ -140,7 +143,6 @@ extension SystemString: RandomAccessCollection, MutableCollection {
}
}
}
extension SystemString: RangeReplaceableCollection {
internal mutating func replaceSubrange<C: Collection>(
_ subrange: Range<Index>, with newElements: C
@@ -179,11 +181,12 @@ extension SystemString: RangeReplaceableCollection {
extension SystemString: Hashable {}
#if !hasFeature(Embedded)
#if !$Embedded
extension SystemString: Codable {
// Encoder is synthesized; it probably should have been explicit and used
// a single-value container, but making that change now is somewhat risky.
// Decoder is written explicitly to ensure that we validate invariants on
// untrusted input.
public init(from decoder: any Decoder) throws {
@@ -201,9 +204,11 @@ extension SystemString: Codable {
}
}
}
#endif
#endif // !$Embedded
extension SystemString {
internal func withNullTerminatedSystemChars<T>(
_ f: (UnsafeBufferPointer<SystemChar>) throws -> T
) rethrows -> T {
+5 -1
View File
@@ -43,6 +43,8 @@ internal func nothingOrErrno<I: FixedWidthInteger>(
valueOrErrno(retryOnInterrupt: retryOnInterrupt, f).map { _ in () }
}
#if !$Embedded
// Run a precondition for debug client builds
internal func _debugPrecondition(
_ condition: @autoclosure () -> Bool,
@@ -52,10 +54,12 @@ internal func _debugPrecondition(
// Only check in debug mode.
if _slowPath(_isDebugAssertConfiguration()) {
precondition(
condition(), "\(message)", file: file, line: line)
condition(), String(describing: message), file: file, line: line)
}
}
#endif // !$Embedded
extension OpaquePointer {
internal var _isNULL: Bool {
OpaquePointer(bitPattern: Int(bitPattern: self)) == nil