|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2025 Apple Inc. and the Swift 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 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +#if !SWT_NO_INTEROP |
| 12 | +#if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK && !hasFeature(Embedded) |
| 13 | +private import _TestingInternals |
| 14 | +#else |
| 15 | +private import Synchronization |
| 16 | +#endif |
| 17 | + |
| 18 | +#if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK && !hasFeature(Embedded) |
| 19 | +/// The installed event handler. |
| 20 | +private nonisolated(unsafe) let _fallbackEventHandler = { |
| 21 | + let result = ManagedBuffer<FallbackEventHandler?, os_unfair_lock>.create( |
| 22 | + minimumCapacity: 1, |
| 23 | + makingHeaderWith: { _ in nil } |
| 24 | + ) |
| 25 | + result.withUnsafeMutablePointerToHeader { $0.initialize(to: nil) } |
| 26 | + return result |
| 27 | +}() |
| 28 | +#else |
| 29 | +/// `Atomic`-compatible storage for ``FallbackEventHandler``. |
| 30 | +private final class _FallbackEventHandlerStorage: Sendable, RawRepresentable { |
| 31 | + let rawValue: FallbackEventHandler |
| 32 | + |
| 33 | + init(rawValue: FallbackEventHandler) { |
| 34 | + self.rawValue = rawValue |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// The installed event handler. |
| 39 | +private let _fallbackEventHandler = Atomic<Unmanaged<_FallbackEventHandlerStorage>?>(nil) |
| 40 | +#endif |
| 41 | + |
| 42 | +/// A type describing a fallback event handler that testing API can invoke as an |
| 43 | +/// alternate method of reporting test events to the current test runner. |
| 44 | +/// |
| 45 | +/// For example, an `XCTAssert` failure in the body of a Swift Testing test |
| 46 | +/// cannot record issues directly with the Swift Testing runner. Instead, the |
| 47 | +/// framework packages the assertion failure as a JSON `Event` and invokes this |
| 48 | +/// handler to report the failure. |
| 49 | +/// |
| 50 | +/// - Parameters: |
| 51 | +/// - recordJSONSchemaVersionNumber: The JSON schema version used to encode |
| 52 | +/// the event record. |
| 53 | +/// - recordJSONBaseAddress: A pointer to the first byte of the encoded event. |
| 54 | +/// - recordJSONByteCount: The size of the encoded event in bytes. |
| 55 | +/// - reserved: Reserved for future use. |
| 56 | +@usableFromInline |
| 57 | +package typealias FallbackEventHandler = @Sendable @convention(c) ( |
| 58 | + _ recordJSONSchemaVersionNumber: UnsafePointer<CChar>, |
| 59 | + _ recordJSONBaseAddress: UnsafeRawPointer, |
| 60 | + _ recordJSONByteCount: Int, |
| 61 | + _ reserved: UnsafeRawPointer? |
| 62 | +) -> Void |
| 63 | + |
| 64 | +/// Get the current fallback event handler. |
| 65 | +/// |
| 66 | +/// - Returns: The currently-set handler function, if any. |
| 67 | +@_cdecl("_swift_testing_getFallbackEventHandler") |
| 68 | +@usableFromInline |
| 69 | +package func _swift_testing_getFallbackEventHandler() -> FallbackEventHandler? { |
| 70 | +#if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK && !hasFeature(Embedded) |
| 71 | + return _fallbackEventHandler.withUnsafeMutablePointers { fallbackEventHandler, lock in |
| 72 | + os_unfair_lock_lock(lock) |
| 73 | + defer { |
| 74 | + os_unfair_lock_unlock(lock) |
| 75 | + } |
| 76 | + return fallbackEventHandler.pointee |
| 77 | + } |
| 78 | +#else |
| 79 | + // If we had a setter, this load would present a race condition because |
| 80 | + // another thread could store a new value in between the load and the call to |
| 81 | + // `takeUnretainedValue()`, resulting in a use-after-free on this thread. We |
| 82 | + // would need a full lock in order to avoid that problem. However, because we |
| 83 | + // instead have a one-time installation function, we can be sure that the |
| 84 | + // loaded value (if non-nil) will never be replaced with another value. |
| 85 | + return _fallbackEventHandler.load(ordering: .sequentiallyConsistent).map { fallbackEventHandler in |
| 86 | + fallbackEventHandler.takeUnretainedValue().rawValue |
| 87 | + } |
| 88 | +#endif |
| 89 | +} |
| 90 | + |
| 91 | +/// Set the current fallback event handler if one has not already been set. |
| 92 | +/// |
| 93 | +/// - Parameters: |
| 94 | +/// - handler: The handler function to set. |
| 95 | +/// |
| 96 | +/// - Returns: Whether or not `handler` was installed. |
| 97 | +/// |
| 98 | +/// The fallback event handler can only be installed once per process, typically |
| 99 | +/// by the first testing library to run. If this function has already been |
| 100 | +/// called and the handler set, it does not replace the previous handler. |
| 101 | +@_cdecl("_swift_testing_installFallbackEventHandler") |
| 102 | +@usableFromInline |
| 103 | +package func _swift_testing_installFallbackEventHandler(_ handler: FallbackEventHandler) -> CBool { |
| 104 | + var result = false |
| 105 | + |
| 106 | +#if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK && !hasFeature(Embedded) |
| 107 | + result = _fallbackEventHandler.withUnsafeMutablePointers { fallbackEventHandler, lock in |
| 108 | + os_unfair_lock_lock(lock) |
| 109 | + defer { |
| 110 | + os_unfair_lock_unlock(lock) |
| 111 | + } |
| 112 | + guard fallbackEventHandler.pointee == nil else { |
| 113 | + return false |
| 114 | + } |
| 115 | + fallbackEventHandler.pointee = handler |
| 116 | + return true |
| 117 | + } |
| 118 | +#else |
| 119 | + let handler = Unmanaged.passRetained(_FallbackEventHandlerStorage(rawValue: handler)) |
| 120 | + defer { |
| 121 | + if !result { |
| 122 | + handler.release() |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + result = _fallbackEventHandler.compareExchange(expected: nil, desired: handler, ordering: .sequentiallyConsistent).exchanged |
| 127 | +#endif |
| 128 | + |
| 129 | + return result |
| 130 | +} |
| 131 | +#endif |
0 commit comments