File: ABIEntryPoint.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (138 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if canImport(Foundation) && !SWT_NO_ABI_ENTRY_POINT
#if SWT_BUILDING_WITH_CMAKE
@_implementationOnly import _TestingInternals
#else
private import _TestingInternals
#endif

extension ABIv0 {
  /// The type of the entry point to the testing library used by tools that want
  /// to remain version-agnostic regarding the testing library.
  ///
  /// - Parameters:
  ///   - configurationJSON: A buffer to memory representing the test
  ///     configuration and options. If `nil`, a new instance is synthesized
  ///     from the command-line arguments to the current process.
  ///   - recordHandler: A JSON record handler to which is passed a buffer to
  ///     memory representing each record as described in `ABI/JSON.md`.
  ///
  /// - Returns: Whether or not the test run finished successfully.
  ///
  /// - Throws: Any error that occurred prior to running tests. Errors that are
  ///   thrown while tests are running are handled by the testing library.
  public typealias EntryPoint = @convention(thin) @Sendable (
    _ configurationJSON: UnsafeRawBufferPointer?,
    _ recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void
  ) async throws -> Bool

  /// The entry point to the testing library used by tools that want to remain
  /// version-agnostic regarding the testing library.
  ///
  /// The value of this property is a Swift function that can be used by tools
  /// that do not link directly to the testing library and wish to invoke tests
  /// in a binary that has been loaded into the current process. The value of
  /// this property is accessible from C and C++ as a function with name
  /// `"swt_abiv0_getEntryPoint"` and can be dynamically looked up at runtime
  /// using `dlsym()` or a platform equivalent.
  ///
  /// The value of this property can be thought of as equivalent to a call to
  /// `swift test --event-stream-output-path` except that, instead of streaming
  /// JSON records to a named pipe or file, it streams them to an in-process
  /// callback.
  public static var entryPoint: EntryPoint {
    return { configurationJSON, recordHandler in
      try await Testing.entryPoint(
        configurationJSON: configurationJSON,
        recordHandler: recordHandler
      ) == EXIT_SUCCESS
    }
  }
}

/// An exported C function that is the equivalent of
/// ``ABIv0/entryPoint-swift.type.property``.
///
/// - Returns: The value of ``ABIv0/entryPoint-swift.type.property`` cast to an
///   untyped pointer.
@_cdecl("swt_abiv0_getEntryPoint")
@usableFromInline func abiv0_getEntryPoint() -> UnsafeRawPointer {
  unsafeBitCast(ABIv0.entryPoint, to: UnsafeRawPointer.self)
}

#if !SWT_NO_SNAPSHOT_TYPES
// MARK: - Xcode 16 Beta 1 compatibility

/// An older signature for ``ABIv0/EntryPoint-swift.typealias`` used by Xcode 16
/// Beta 1.
///
/// This type will be removed in a future update.
@available(*, deprecated, message: "Use ABIv0.EntryPoint instead.")
typealias ABIEntryPoint_v0 = @Sendable (
  _ argumentsJSON: UnsafeRawBufferPointer?,
  _ recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void
) async throws -> CInt

/// An older signature for ``ABIv0/entryPoint-swift.type.property`` used by
/// Xcode 16 Beta 1.
///
/// This function will be removed in a future update.
@available(*, deprecated, message: "Use ABIv0.entryPoint (swt_abiv0_getEntryPoint()) instead.")
@_cdecl("swt_copyABIEntryPoint_v0")
@usableFromInline func copyABIEntryPoint_v0() -> UnsafeMutableRawPointer {
  let result = UnsafeMutablePointer<ABIEntryPoint_v0>.allocate(capacity: 1)
  result.initialize { configurationJSON, recordHandler in
    try await entryPoint(
      configurationJSON: configurationJSON,
      eventStreamVersionIfNil: -1,
      recordHandler: recordHandler
    )
  }
  return .init(result)
}
#endif

// MARK: -

/// A common implementation for ``ABIv0/entryPoint-swift.type.property`` and
/// ``copyABIEntryPoint_v0()`` that provides Xcode 16 Beta 1 compatibility.
///
/// This function will be removed (with its logic incorporated into
/// ``ABIv0/entryPoint-swift.type.property``) in a future update.
private func entryPoint(
  configurationJSON: UnsafeRawBufferPointer?,
  eventStreamVersionIfNil: Int? = nil,
  recordHandler: @escaping @Sendable (_ recordJSON: UnsafeRawBufferPointer) -> Void
) async throws -> CInt {
  var args = try configurationJSON.map { configurationJSON in
    try JSON.decode(__CommandLineArguments_v0.self, from: configurationJSON)
  }

  // If the caller needs a nil event stream version to default to a specific
  // JSON schema, apply it here as if they'd specified it in the configuration
  // JSON blob.
  if let eventStreamVersionIfNil, args?.eventStreamVersion == nil {
    args?.eventStreamVersion = eventStreamVersionIfNil
  }

  let eventHandler = try eventHandlerForStreamingEvents(version: args?.eventStreamVersion, forwardingTo: recordHandler)
  let exitCode = await entryPoint(passing: args, eventHandler: eventHandler)

  // To maintain compatibility with Xcode 16 Beta 1, suppress custom exit codes.
  // (This is also needed by ABIv0.entryPoint to correctly treat the no-tests as
  // a successful run.)
  if exitCode == EXIT_NO_TESTS_FOUND {
    return EXIT_SUCCESS
  }
  return exitCode
}
#endif