diff --git a/Package.swift b/Package.swift index 40e28c845..52aa87831 100644 --- a/Package.swift +++ b/Package.swift @@ -123,6 +123,8 @@ extension Array where Element == PackageDescription.SwiftSetting { .enableUpcomingFeature("InternalImportsByDefault"), .define("SWT_TARGET_OS_APPLE", .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])), + + .define("SWT_NO_FILE_IO", .when(platforms: [.wasi])), ] } diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index 71b1fd1e7..a2006b1e6 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -123,6 +123,8 @@ extension Array where Element == PackageDescription.SwiftSetting { .enableUpcomingFeature("InternalImportsByDefault"), .define("SWT_TARGET_OS_APPLE", .when(platforms: [.macOS, .iOS, .macCatalyst, .watchOS, .tvOS, .visionOS])), + + .define("SWT_NO_FILE_IO", .when(platforms: [.wasi])), ] } diff --git a/Sources/Testing/Events/Recorder/Event.Symbol.swift b/Sources/Testing/Events/Recorder/Event.Symbol.swift index 719c3e795..10f0fbf93 100644 --- a/Sources/Testing/Events/Recorder/Event.Symbol.swift +++ b/Sources/Testing/Events/Recorder/Event.Symbol.swift @@ -100,7 +100,7 @@ extension Event.Symbol { /// be used to represent it in text-based output. The value of this property /// is platform-dependent. public var unicodeCharacter: Character { -#if SWT_TARGET_OS_APPLE || os(Linux) +#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI) switch self { case .default: // Unicode: WHITE DIAMOND diff --git a/Sources/Testing/Events/TimeValue.swift b/Sources/Testing/Events/TimeValue.swift index f798a78ef..033aa9f86 100644 --- a/Sources/Testing/Events/TimeValue.swift +++ b/Sources/Testing/Events/TimeValue.swift @@ -75,6 +75,10 @@ extension TimeValue: Codable {} extension TimeValue: CustomStringConvertible { var description: String { +#if os(WASI) + // BUG: https://github.com/apple/swift/issues/72398 + return String(describing: Duration(self)) +#else let (secondsFromAttoseconds, attosecondsRemaining) = attoseconds.quotientAndRemainder(dividingBy: 1_000_000_000_000_000_000) let seconds = seconds + secondsFromAttoseconds var milliseconds = attosecondsRemaining / 1_000_000_000_000_000 @@ -88,6 +92,7 @@ extension TimeValue: CustomStringConvertible { } return String(cString: buffer.baseAddress!) } +#endif } } diff --git a/Sources/Testing/SourceAttribution/Backtrace.swift b/Sources/Testing/SourceAttribution/Backtrace.swift index 3d9977c76..df8834477 100644 --- a/Sources/Testing/SourceAttribution/Backtrace.swift +++ b/Sources/Testing/SourceAttribution/Backtrace.swift @@ -72,6 +72,10 @@ public struct Backtrace: Sendable { initializedCount = .init(backtrace(addresses.baseAddress!, .init(addresses.count))) #elseif os(Windows) initializedCount = Int(RtlCaptureStackBackTrace(0, ULONG(addresses.count), addresses.baseAddress!, nil)) +#elseif os(WASI) + // SEE: https://github.com/WebAssembly/WASI/issues/159 + // SEE: https://github.com/apple/swift/pull/31693 + initializedCount = 0 #else #warning("Platform-specific implementation missing: backtraces unavailable") initializedCount = 0 diff --git a/Sources/Testing/Support/Environment.swift b/Sources/Testing/Support/Environment.swift index fb01d95bc..d9fa050b3 100644 --- a/Sources/Testing/Support/Environment.swift +++ b/Sources/Testing/Support/Environment.swift @@ -36,7 +36,7 @@ enum Environment { static func variable(named name: String) -> String? { #if SWT_NO_ENVIRONMENT_VARIABLES simulatedEnvironment.rawValue[name] -#elseif SWT_TARGET_OS_APPLE || os(Linux) +#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(WASI) getenv(name).flatMap { String(validatingUTF8: $0) } #elseif os(Windows) name.withCString(encodedAs: UTF16.self) { name in diff --git a/Sources/Testing/Support/FileHandle.swift b/Sources/Testing/Support/FileHandle.swift index 247ada218..adab81981 100644 --- a/Sources/Testing/Support/FileHandle.swift +++ b/Sources/Testing/Support/FileHandle.swift @@ -140,7 +140,7 @@ struct FileHandle: ~Copyable, Sendable { let fd: CInt = -1 #endif - if fd >= 0 { + if Bool(fd >= 0) { return try body(fd) } return try body(nil) diff --git a/Sources/Testing/Support/Locked.swift b/Sources/Testing/Support/Locked.swift index 5c9fddde7..05bef558a 100644 --- a/Sources/Testing/Support/Locked.swift +++ b/Sources/Testing/Support/Locked.swift @@ -40,6 +40,8 @@ struct Locked: RawRepresentable, Sendable where T: Sendable { private typealias _Lock = pthread_mutex_t #elseif os(Windows) private typealias _Lock = SRWLOCK +#elseif os(WASI) + // No locks on WASI. #else #warning("Platform-specific implementation missing: locking unavailable") private typealias _Lock = Void @@ -53,6 +55,8 @@ struct Locked: RawRepresentable, Sendable where T: Sendable { _ = pthread_mutex_destroy(lock) #elseif os(Windows) // No deinitialization needed. +#elseif os(WASI) + // No locks on WASI. #else #warning("Platform-specific implementation missing: locking unavailable") #endif @@ -70,6 +74,8 @@ struct Locked: RawRepresentable, Sendable where T: Sendable { _ = pthread_mutex_init(lock, nil) #elseif os(Windows) InitializeSRWLock(lock) +#elseif os(WASI) + // No locks on WASI. #else #warning("Platform-specific implementation missing: locking unavailable") #endif @@ -105,6 +111,8 @@ struct Locked: RawRepresentable, Sendable where T: Sendable { defer { ReleaseSRWLockExclusive(lock) } +#elseif os(WASI) + // No locks on WASI. #else #warning("Platform-specific implementation missing: locking unavailable") #endif diff --git a/Sources/Testing/Support/Versions.swift b/Sources/Testing/Support/Versions.swift index 8c4251bc4..5fe79f02e 100644 --- a/Sources/Testing/Support/Versions.swift +++ b/Sources/Testing/Support/Versions.swift @@ -31,7 +31,7 @@ let operatingSystemVersion: String = { default: return "\(productVersion) (\(buildNumber))" } -#elseif !SWT_NO_UNAME && (SWT_TARGET_OS_APPLE || os(Linux)) +#elseif !SWT_NO_UNAME && (SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)) var name = utsname() if 0 == uname(&name) { let release = withUnsafeBytes(of: name.release) { release in diff --git a/Sources/TestingInternals/Discovery.cpp b/Sources/TestingInternals/Discovery.cpp index bba05ffdc..60d482993 100644 --- a/Sources/TestingInternals/Discovery.cpp +++ b/Sources/TestingInternals/Discovery.cpp @@ -260,7 +260,7 @@ static void enumerateTypeMetadataSections(const SectionEnumerator& body) { } } -#elif defined(__linux__) || defined(_WIN32) +#elif defined(__linux__) || defined(_WIN32) || defined(__wasi__) #pragma mark - Linux/Windows implementation /// Specifies the address range corresponding to a section. diff --git a/Tests/TestingTests/Support/EnvironmentTests.swift b/Tests/TestingTests/Support/EnvironmentTests.swift index b25d4528c..66e1c838a 100644 --- a/Tests/TestingTests/Support/EnvironmentTests.swift +++ b/Tests/TestingTests/Support/EnvironmentTests.swift @@ -79,7 +79,7 @@ extension Environment { environment[name] = value } return true -#elseif SWT_TARGET_OS_APPLE || os(Linux) +#elseif SWT_TARGET_OS_APPLE || os(Linux) || os(WASI) if let value { return 0 == setenv(name, value, 1) }