@@ -13,13 +13,13 @@ internal import _TestingInternals
1313
1414/// A type representing a testing library such as Swift Testing or XCTest.
1515@_spi ( Experimental)
16- public struct Library : Sendable {
17- /// The underlying instance of ``SWTLibrary ``.
16+ @ frozen public struct Library : Sendable {
17+ /// The underlying instance of ``Library/Record ``.
1818 ///
1919 /// - Important: The in-memory layout of ``Library`` must _exactly_ match the
20- /// layout of this type . As such, it must not contain any other stored
21- /// properties.
22- nonisolated ( unsafe) var rawValue: SWTLibrary
20+ /// layout of ``Library/Record`` . As such, ``Library`` must not contain any
21+ /// other stored properties.
22+ nonisolated ( unsafe) var rawValue: Record
2323
2424 /// The human-readable name of this library.
2525 ///
@@ -149,6 +149,42 @@ public struct Library: Sendable {
149149}
150150
151151#if !SWT_NO_RUNTIME_LIBRARY_DISCOVERY
152+ // MARK: - C structure
153+
154+ extension Library {
155+ @usableFromInline typealias EntryPoint = @convention ( c) (
156+ _ configurationJSON: UnsafeRawPointer ,
157+ _ configurationJSONByteCount: Int ,
158+ _ reserved: UInt ,
159+ _ context: UnsafeRawPointer ? ,
160+ _ recordJSONHandler: EntryPointRecordJSONHandler ,
161+ _ completionHandler: EntryPointCompletionHandler
162+ ) -> Void
163+
164+ @usableFromInline typealias EntryPointRecordJSONHandler = @convention ( c) (
165+ _ recordJSON: UnsafeRawPointer ,
166+ _ recordJSONByteCount: Int ,
167+ _ reserved: UInt ,
168+ _ context: UnsafeRawPointer ?
169+ ) -> Void
170+
171+ @usableFromInline typealias EntryPointCompletionHandler = @convention ( c) (
172+ _ resultJSON: UnsafeRawPointer ,
173+ _ resultJSONByteCount: Int ,
174+ _ reserved: UInt ,
175+ _ context: UnsafeRawPointer ?
176+ ) -> Void
177+
178+ /// A type that provides the C-compatible in-memory layout of the ``Library``
179+ /// Swift type.
180+ @usableFromInline typealias Record = (
181+ name: UnsafePointer < CChar > ,
182+ canonicalHint: UnsafePointer < CChar > ,
183+ entryPoint: EntryPoint ,
184+ reserved: ( UInt , UInt , UInt , UInt , UInt )
185+ )
186+ }
187+
152188// MARK: - Discovery
153189
154190/// A helper protocol that prevents the conformance of ``Library`` to
@@ -167,11 +203,11 @@ extension Library: _DiscoverableAsTestContent {
167203@_spi ( Experimental)
168204extension Library {
169205 /// Perform a one-time check that the in-memory layout of ``Library`` matches
170- /// that of ``SWTLibrary ``.
206+ /// that of ``Library/Record ``.
171207 private static let _validateMemoryLayout : Void = {
172- assert ( MemoryLayout< Library> . size == MemoryLayout< SWTLibrary > . size, " Library.size ( \( MemoryLayout< Library> . size) ) != SWTLibrary. size ( \( MemoryLayout< SWTLibrary > . size) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
173- assert ( MemoryLayout< Library> . stride == MemoryLayout< SWTLibrary > . stride, " Library.stride ( \( MemoryLayout< Library> . stride) ) != SWTLibrary. stride ( \( MemoryLayout< SWTLibrary > . stride) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
174- assert ( MemoryLayout< Library> . alignment == MemoryLayout< SWTLibrary > . alignment, " Library.alignment ( \( MemoryLayout< Library> . alignment) ) != SWTLibrary. alignment ( \( MemoryLayout< SWTLibrary > . alignment) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
208+ assert ( MemoryLayout< Library> . size == MemoryLayout< Library . Record > . size, " Library.size ( \( MemoryLayout< Library> . size) ) != Library.Record. size ( \( MemoryLayout< Library . Record > . size) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
209+ assert ( MemoryLayout< Library> . stride == MemoryLayout< Library . Record > . stride, " Library.stride ( \( MemoryLayout< Library> . stride) ) != Library.Record. stride ( \( MemoryLayout< Library . Record > . stride) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
210+ assert ( MemoryLayout< Library> . alignment == MemoryLayout< Library . Record > . alignment, " Library.alignment ( \( MemoryLayout< Library> . alignment) ) != Library.Record. alignment ( \( MemoryLayout< Library . Record > . alignment) ). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new " )
175211 } ( )
176212
177213 /// Create an instance of this type representing the testing library with the
@@ -210,7 +246,7 @@ extension Library {
210246extension Library {
211247 /// The ABI entry point function for the testing library, thunked so that it
212248 /// is compatible with the ``Library`` ABI.
213- private static let _libraryRecordEntryPoint : SWTLibraryEntryPoint = { configurationJSON, configurationJSONByteCount, _, context, recordJSONHandler, completionHandler in
249+ private static let _libraryRecordEntryPoint : Library . EntryPoint = { configurationJSON, configurationJSONByteCount, _, context, recordJSONHandler, completionHandler in
214250#if !SWT_NO_RUNTIME_LIBRARY_DISCOVERY
215251 // Capture appropriate state from the arguments to forward into the canonical
216252 // entry point function.
@@ -238,14 +274,15 @@ extension Library {
238274 args. eventStreamOutputPath = nil
239275
240276 // Create an async context and run tests within it.
241- let run = { [ args] in
277+ let run = { @ Sendable [ args] in
242278 let context = UnsafeRawPointer ( bitPattern: contextBitPattern) !
243279 let exitCode = await Testing . entryPoint ( passing: args, eventHandler: eventHandler)
244280 var resultJSON = " \( exitCode) "
245281 resultJSON. withUTF8 { resultJSON in
246282 completionHandler ( resultJSON. baseAddress!, resultJSON. count, 0 , context)
247283 }
248284 }
285+
249286#if !SWT_NO_UNSTRUCTURED_TASKS
250287 Task . detached { await run ( ) }
251288#else
@@ -262,7 +299,7 @@ extension Library {
262299 /// An instance of this type representing Swift Testing itself.
263300 public static let swiftTesting : Self = {
264301 Self (
265- rawValue: . init (
302+ rawValue: (
266303 name: StaticString ( " Swift Testing " ) . constUTF8CString,
267304 canonicalHint: StaticString ( " swift-testing " ) . constUTF8CString,
268305 entryPoint: _libraryRecordEntryPoint,
@@ -289,6 +326,9 @@ private func _libraryRecordAccessor(_ outValue: UnsafeMutableRawPointer, _ type:
289326 // Check if the name of the testing library the caller wants is equivalent to
290327 // "Swift Testing", ignoring case and punctuation. (If the caller did not
291328 // specify a library name, the caller wants records for all libraries.)
329+ //
330+ // Other libraries may opt to compare their hint values differently or accept
331+ // multiple different strings/patterns.
292332 let hint = hint. map { $0. load ( as: UnsafePointer< CChar> . self ) }
293333 if let hint {
294334 guard let hint = String ( validatingCString: hint) ,
@@ -298,10 +338,7 @@ private func _libraryRecordAccessor(_ outValue: UnsafeMutableRawPointer, _ type:
298338 }
299339
300340 // Initialize the provided memory to the (ABI-stable) library structure.
301- _ = outValue. initializeMemory (
302- as: SWTLibrary . self,
303- to: Library . swiftTesting. rawValue
304- )
341+ _ = outValue. initializeMemory ( as: Library . self, to: . swiftTesting)
305342
306343 return true
307344}
0 commit comments