Skip to content

Commit 6a7952e

Browse files
committed
Add exit API
Add `exit` as suggested in the roadmap. There are a few outstanding tasks with this API - [ ] Discuss API naming on the Swift forum. - [ ] Availablility annotation (I would like some suggestion from a reviewer as opposed to guessing it myself.) - [ ] Improve the documantations if deemed necessary. [roadmap]: apple#16
1 parent 1d5e35a commit 6a7952e

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

Sources/System/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ See https://swift.org/LICENSE.txt for license information
99

1010
add_library(SystemPackage
1111
Errno.swift
12+
Exit.swift
1213
FileDescriptor.swift
1314
FileHelpers.swift
1415
FileOperations.swift

Sources/System/Exit.swift

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
11+
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
12+
import Darwin
13+
#elseif os(Linux) || os(FreeBSD) || os(Android)
14+
import Glibc
15+
#elseif os(Windows)
16+
import ucrt
17+
#else
18+
#error("Unsupported Platform")
19+
#endif
20+
21+
public struct ExitStatus: RawRepresentable, Hashable, Codable {
22+
/// The raw exit status code for C standard library.
23+
@_alwaysEmitIntoClient
24+
public let rawValue: CInt
25+
26+
/// Create an exit status.
27+
///
28+
/// - Parameter rawValue: The raw exit status code for C standard library.
29+
@_alwaysEmitIntoClient
30+
public init(rawValue: CInt) { self.rawValue = rawValue }
31+
32+
/// Success exit status.
33+
@_alwaysEmitIntoClient
34+
public static var success: Self { .init(rawValue: EXIT_SUCCESS) }
35+
36+
/// Failure exit status.
37+
@_alwaysEmitIntoClient
38+
public static var failure: Self { .init(rawValue: EXIT_FAILURE) }
39+
}
40+
41+
42+
/// Terminate the current process.
43+
///
44+
/// - Parameter status: The exit status for the finished process.
45+
public func exit(with status: ExitStatus) -> Never {
46+
exit(status.rawValue)
47+
}

Tests/SystemTests/ExitTest.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
import XCTest
11+
12+
#if SYSTEM_PACKAGE
13+
import SystemPackage
14+
#else
15+
import System
16+
#endif
17+
18+
final class ExitTest: XCTestCase {
19+
func testConstants() {
20+
XCTAssert(EXIT_SUCCESS == ExitStatus.success.rawValue)
21+
XCTAssert(EXIT_FAILURE == ExitStatus.failure.rawValue)
22+
}
23+
}

Tests/SystemTests/XCTestManifests.swift

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ extension ErrnoTest {
1111
]
1212
}
1313

14+
extension ExitTest {
15+
// DO NOT MODIFY: This is autogenerated, use:
16+
// `swift test --generate-linuxmain`
17+
// to regenerate.
18+
static let __allTests__ExitTest = [
19+
("testConstants", testConstants),
20+
]
21+
}
22+
1423
extension FileDescriptorTest {
1524
// DO NOT MODIFY: This is autogenerated, use:
1625
// `swift test --generate-linuxmain`
@@ -27,6 +36,7 @@ extension FileOperationsTest {
2736
// to regenerate.
2837
static let __allTests__FileOperationsTest = [
2938
("testAdHocOpen", testAdHocOpen),
39+
("testAdHocPipe", testAdHocPipe),
3040
("testGithubIssues", testGithubIssues),
3141
("testHelpers", testHelpers),
3242
("testSyscalls", testSyscalls),
@@ -116,6 +126,7 @@ extension SystemStringTest {
116126
public func __allTests() -> [XCTestCaseEntry] {
117127
return [
118128
testCase(ErrnoTest.__allTests__ErrnoTest),
129+
testCase(ExitTest.__allTests__ExitTest),
119130
testCase(FileDescriptorTest.__allTests__FileDescriptorTest),
120131
testCase(FileOperationsTest.__allTests__FileOperationsTest),
121132
testCase(FilePathComponentsTest.__allTests__FilePathComponentsTest),

0 commit comments

Comments
 (0)