Skip to content

Add partial WASI/Wasm support. #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])),
]
}

Expand Down
2 changes: 2 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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])),
]
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Events/Recorder/Event.Symbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Sources/Testing/Events/TimeValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,6 +92,7 @@ extension TimeValue: CustomStringConvertible {
}
return String(cString: buffer.baseAddress!)
}
#endif
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/Testing/SourceAttribution/Backtrace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ struct FileHandle: ~Copyable, Sendable {
let fd: CInt = -1
#endif

if fd >= 0 {
if Bool(fd >= 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file doesn't build for WASI, however there's a dead-code warning here on platforms where fileno() isn't defined which is redundant in the face of our explicit warning, so this change silences it.

return try body(fd)
}
return try body(nil)
Expand Down
8 changes: 8 additions & 0 deletions Sources/Testing/Support/Locked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct Locked<T>: 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
Expand All @@ -53,6 +55,8 @@ struct Locked<T>: 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
Expand All @@ -70,6 +74,8 @@ struct Locked<T>: 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
Expand Down Expand Up @@ -105,6 +111,8 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
defer {
ReleaseSRWLockExclusive(lock)
}
#elseif os(WASI)
// No locks on WASI.
#else
#warning("Platform-specific implementation missing: locking unavailable")
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/Versions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/TestingInternals/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Tests/TestingTests/Support/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down