Skip to content

Commit 112a223

Browse files
committed
Disable direct threading for WASI, add CSystemExtras
1 parent d9b4c5d commit 112a223

File tree

16 files changed

+92
-55
lines changed

16 files changed

+92
-55
lines changed

Package.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,17 @@ let package = Package(
8989
.target(
9090
name: "SystemExtras",
9191
dependencies: [
92-
.product(name: "SystemPackage", package: "swift-system")
92+
.product(name: "SystemPackage", package: "swift-system"),
93+
.target(name: "CSystemExtras", condition: .when(platforms: [.wasi])),
9394
],
9495
exclude: ["CMakeLists.txt"],
9596
swiftSettings: [
9697
.define("SYSTEM_PACKAGE_DARWIN", .when(platforms: DarwinPlatforms))
9798
]
9899
),
99100

101+
.target(name: "CSystemExtras"),
102+
100103
.executableTarget(
101104
name: "WITTool",
102105
dependencies: [
@@ -121,7 +124,7 @@ let package = Package(
121124
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
122125
package.dependencies += [
123126
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
124-
.package(url: "https://github.com/apple/swift-system", from: "1.6.0"),
127+
.package(url: "https://github.com/apple/swift-system", from: "1.5.0"),
125128
]
126129
} else {
127130
package.dependencies += [

Sources/CLI/Commands/Run.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ struct Run: ParsableCommand {
5959
var directories: [String] = []
6060

6161
enum ThreadingModel: String, ExpressibleByArgument, CaseIterable {
62-
case direct
62+
#if !os(WASI)
63+
case direct
64+
#endif
6365
case token
6466

6567
func resolve() -> EngineConfiguration.ThreadingModel {
6668
switch self {
67-
case .direct: return .direct
69+
#if !os(WASI)
70+
case .direct: return .direct
71+
#endif
6872
case .token: return .token
6973
}
7074
}

Sources/CSystemExtras/CSystemExtras.c

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <time.h>
2+
3+
inline static clockid_t csystemextras_monotonic_clockid() {
4+
return CLOCK_MONOTONIC;
5+
}

Sources/SystemExtras/Clock.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Android
1313
import CSystem
1414
import ucrt
1515
#elseif os(WASI)
16+
import CSystemExtras
1617
import WASILibc
1718
#else
1819
#error("Unsupported Platform")
@@ -48,6 +49,11 @@ extension Clock {
4849
public static var monotonic: Clock { Clock(rawValue: _CLOCK_MONOTONIC) }
4950
#endif
5051

52+
#if os(WASI)
53+
@_alwaysEmitIntoClient
54+
public static var monotonic: Clock { Clock(rawValue: csystemextras_monotonic_clockid()) }
55+
#endif
56+
5157
#if os(OpenBSD) || os(FreeBSD)
5258
@_alwaysEmitIntoClient
5359
public static var uptime: Clock { Clock(rawValue: _CLOCK_UPTIME) }

Sources/WASI/Clock.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,10 @@ public protocol MonotonicClock {
9292
/// A monotonic clock that uses the system's monotonic clock.
9393
public struct SystemMonotonicClock: MonotonicClock {
9494
private var underlying: SystemExtras.Clock {
95-
#if os(Linux) || os(Android)
95+
#if os(Linux) || os(Android) || os(WASI)
9696
return .monotonic
9797
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
9898
return .rawUptime
99-
#elseif os(WASI)
100-
return .monotonic
10199
#elseif os(OpenBSD) || os(FreeBSD)
102100
return .uptime
103101
#else

Sources/WASI/Platform/PlatformTypes.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ extension WASIAbi.Fdflags {
4343
if platformOpenOptions.contains(.nonBlocking) {
4444
fdFlags.insert(.NONBLOCK)
4545
}
46-
#if !os(WASI)
47-
if platformOpenOptions.contains(.dataSync) {
48-
fdFlags.insert(.DSYNC)
49-
}
50-
if platformOpenOptions.contains(.fileSync) {
51-
fdFlags.insert(.SYNC)
52-
}
53-
#endif
46+
#if !os(WASI)
47+
if platformOpenOptions.contains(.dataSync) {
48+
fdFlags.insert(.DSYNC)
49+
}
50+
if platformOpenOptions.contains(.fileSync) {
51+
fdFlags.insert(.SYNC)
52+
}
53+
#endif
5454
#if os(Linux)
5555
if platformOpenOptions.contains(.readSync) {
5656
fdFlags.insert(.RSYNC)
@@ -69,14 +69,14 @@ extension WASIAbi.Fdflags {
6969
if self.contains(.NONBLOCK) {
7070
flags.insert(.nonBlocking)
7171
}
72-
#if !os(WASI)
73-
if self.contains(.DSYNC) {
74-
flags.insert(.dataSync)
75-
}
76-
if self.contains(.SYNC) {
77-
flags.insert(.fileSync)
78-
}
79-
#endif
72+
#if !os(WASI)
73+
if self.contains(.DSYNC) {
74+
flags.insert(.dataSync)
75+
}
76+
if self.contains(.SYNC) {
77+
flags.insert(.fileSync)
78+
}
79+
#endif
8080
#if os(Linux)
8181
if self.contains(.RSYNC) {
8282
flags.insert(.readSync)

Sources/WASI/Platform/SandboxPrimitives/Open.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,17 @@ struct PathResolution {
129129
throw openErrno
130130
}
131131

132-
try self.symlink(component: component)
132+
#if os(WASI)
133+
throw Errno.notSupported
134+
#else
135+
try self.symlink(component: component)
136+
#endif
133137
#endif
134138
}
135139
}
136140
}
137141

138-
#if !os(Windows)
142+
#if !os(Windows) && !os(WASI)
139143
mutating func symlink(component: FilePath.Component) throws {
140144
/// Thin wrapper around readlinkat(2)
141145
func _readlinkat(_ fd: CInt, _ path: UnsafePointer<CChar>) throws -> FilePath {

Sources/WIT/PackageResolver.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,3 @@ public struct LocalFileLoader: PackageFileLoader {
241241
return try fileManager.contentsOfDirectory(atPath: depsDir.path)
242242
}
243243
}
244-

Sources/WasmKit/Engine.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public struct EngineConfiguration {
3434
/// The threading model, which determines how to dispatch instruction
3535
/// execution, to use for the virtual machine interpreter.
3636
public enum ThreadingModel {
37-
/// Direct threaded code
38-
/// - Note: This is the default model for platforms that support
39-
/// `musttail` calls.
40-
case direct
37+
#if !os(WASI)
38+
/// Direct threaded code
39+
/// - Note: This is the default model for platforms that support
40+
/// `musttail` calls.
41+
case direct
42+
#endif
4143
/// Indirect threaded code
4244
/// - Note: This is a fallback model for platforms that do not support
4345
/// `musttail` calls.
@@ -48,7 +50,11 @@ public struct EngineConfiguration {
4850
}
4951

5052
static var defaultForCurrentPlatform: ThreadingModel {
51-
return useDirectThreadedCode ? .direct : .token
53+
#if os(WASI)
54+
return .token
55+
#else
56+
return useDirectThreadedCode ? .direct : .token
57+
#endif
5258
}
5359
}
5460

0 commit comments

Comments
 (0)