Skip to content

Commit

Permalink
Add exit API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dduan committed Apr 20, 2022
1 parent 1d5e35a commit 6a7952e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See https://swift.org/LICENSE.txt for license information

add_library(SystemPackage
Errno.swift
Exit.swift
FileDescriptor.swift
FileHelpers.swift
FileOperations.swift
Expand Down
47 changes: 47 additions & 0 deletions Sources/System/Exit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/


#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import Glibc
#elseif os(Windows)
import ucrt
#else
#error("Unsupported Platform")
#endif

public struct ExitStatus: RawRepresentable, Hashable, Codable {
/// The raw exit status code for C standard library.
@_alwaysEmitIntoClient
public let rawValue: CInt

/// Create an exit status.
///
/// - Parameter rawValue: The raw exit status code for C standard library.
@_alwaysEmitIntoClient
public init(rawValue: CInt) { self.rawValue = rawValue }

/// Success exit status.
@_alwaysEmitIntoClient
public static var success: Self { .init(rawValue: EXIT_SUCCESS) }

/// Failure exit status.
@_alwaysEmitIntoClient
public static var failure: Self { .init(rawValue: EXIT_FAILURE) }
}


/// Terminate the current process.
///
/// - Parameter status: The exit status for the finished process.
public func exit(with status: ExitStatus) -> Never {
exit(status.rawValue)
}
23 changes: 23 additions & 0 deletions Tests/SystemTests/ExitTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/

import XCTest

#if SYSTEM_PACKAGE
import SystemPackage
#else
import System
#endif

final class ExitTest: XCTestCase {
func testConstants() {
XCTAssert(EXIT_SUCCESS == ExitStatus.success.rawValue)
XCTAssert(EXIT_FAILURE == ExitStatus.failure.rawValue)
}
}
11 changes: 11 additions & 0 deletions Tests/SystemTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ extension ErrnoTest {
]
}

extension ExitTest {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ExitTest = [
("testConstants", testConstants),
]
}

extension FileDescriptorTest {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand All @@ -27,6 +36,7 @@ extension FileOperationsTest {
// to regenerate.
static let __allTests__FileOperationsTest = [
("testAdHocOpen", testAdHocOpen),
("testAdHocPipe", testAdHocPipe),
("testGithubIssues", testGithubIssues),
("testHelpers", testHelpers),
("testSyscalls", testSyscalls),
Expand Down Expand Up @@ -116,6 +126,7 @@ extension SystemStringTest {
public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(ErrnoTest.__allTests__ErrnoTest),
testCase(ExitTest.__allTests__ExitTest),
testCase(FileDescriptorTest.__allTests__FileDescriptorTest),
testCase(FileOperationsTest.__allTests__FileOperationsTest),
testCase(FilePathComponentsTest.__allTests__FilePathComponentsTest),
Expand Down

0 comments on commit 6a7952e

Please sign in to comment.