diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt index d9ebc2ebb..836d849ae 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(FoundationEssentials SortComparator.swift UUID_Wrappers.swift UUID.swift + WASILibc+Extensions.swift WinSDK+Extensions.swift) add_subdirectory(AttributedString) diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift index 302d3c62d..78b86c266 100644 --- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift @@ -129,7 +129,7 @@ final class _ProcessInfo: Sendable { #else var ts: timespec = timespec() clock_gettime(CLOCK_MONOTONIC_RAW, &ts) - let time: UInt64 = UInt64(ts.tv_sec * 1000000000 + ts.tv_nsec) + let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec) #endif let timeString = String(time, radix: 16, uppercase: true) let padding = String(repeating: "0", count: 16 - timeString.count) diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift new file mode 100644 index 000000000..1c05f9958 --- /dev/null +++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +#if os(WASI) + +import WASILibc +internal import _FoundationCShims + +// MARK: - Clock + +internal var CLOCK_REALTIME: clockid_t { + return _platform_shims_clock_realtime() +} + +internal var CLOCK_MONOTONIC: clockid_t { + return _platform_shims_clock_monotonic() +} + +internal var CLOCK_MONOTONIC_RAW: clockid_t { + // WASI does not have a raw monotonic clock, so we use the monotonic clock instead. + return CLOCK_MONOTONIC +} + +#endif // os(WASI) diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h index 911fc9e7a..9c7e95925 100644 --- a/Sources/_FoundationCShims/include/platform_shims.h +++ b/Sources/_FoundationCShims/include/platform_shims.h @@ -68,4 +68,17 @@ typedef enum { INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); #endif +#if TARGET_OS_WASI +// Define clock id getter shims so that we can use them in Swift +// even if clock id macros can't be imported through ClangImporter. + +#include +static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) { + return CLOCK_MONOTONIC; +} +static inline _Nonnull clockid_t _platform_shims_clock_realtime(void) { + return CLOCK_REALTIME; +} +#endif + #endif /* CSHIMS_PLATFORM_SHIMS */