Skip to content
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

Add partial WASI/Wasm support. #301

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
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]
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])),
kateinoigakukun marked this conversation as resolved.
Show resolved Hide resolved
]
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Events/Recorder/Event.Symbol.swift
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
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
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
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
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: 4 additions & 4 deletions Sources/Testing/Support/Locked.swift
Expand Up @@ -36,7 +36,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
/// To keep the implementation of this type as simple as possible,
/// `pthread_mutex_t` is used on Apple platforms instead of `os_unfair_lock`
/// or `OSAllocatedUnfairLock`.
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
grynspan marked this conversation as resolved.
Show resolved Hide resolved
private typealias _Lock = pthread_mutex_t
#elseif os(Windows)
private typealias _Lock = SRWLOCK
Expand All @@ -49,7 +49,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
private final class _Storage: ManagedBuffer<T, _Lock> {
deinit {
withUnsafeMutablePointerToElements { lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
_ = pthread_mutex_destroy(lock)
#elseif os(Windows)
// No deinitialization needed.
Expand All @@ -66,7 +66,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
init(rawValue: T) {
let storage = _Storage.create(minimumCapacity: 1, makingHeaderWith: { _ in rawValue })
storage.withUnsafeMutablePointerToElements { lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
_ = pthread_mutex_init(lock, nil)
#elseif os(Windows)
InitializeSRWLock(lock)
Expand Down Expand Up @@ -95,7 +95,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
/// concurrency tools.
nonmutating func withLock<R>(_ body: (inout T) throws -> R) rethrows -> R {
try _storage.rawValue.withUnsafeMutablePointers { rawValue, lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
_ = pthread_mutex_lock(lock)
defer {
_ = pthread_mutex_unlock(lock)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Support/Versions.swift
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
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
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