Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ let package = Package(
.target(
name: "SystemExtras",
dependencies: [
.product(name: "SystemPackage", package: "swift-system")
.product(name: "SystemPackage", package: "swift-system"),
.target(name: "CSystemExtras", condition: .when(platforms: [.wasi])),
],
exclude: ["CMakeLists.txt"],
swiftSettings: [
.define("SYSTEM_PACKAGE_DARWIN", .when(platforms: DarwinPlatforms))
]
),

.target(name: "CSystemExtras"),

.executableTarget(
name: "WITTool",
dependencies: [
Expand All @@ -121,7 +124,7 @@ let package = Package(
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
.package(url: "https://github.com/apple/swift-system", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-system", from: "1.5.0"),
]
} else {
package.dependencies += [
Expand Down
8 changes: 6 additions & 2 deletions Sources/CLI/Commands/Run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ struct Run: ParsableCommand {
var directories: [String] = []

enum ThreadingModel: String, ExpressibleByArgument, CaseIterable {
case direct
#if !os(WASI)
case direct
#endif
case token

func resolve() -> EngineConfiguration.ThreadingModel {
switch self {
case .direct: return .direct
#if !os(WASI)
case .direct: return .direct
#endif
case .token: return .token
}
}
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions Sources/CSystemExtras/include/clock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <time.h>

inline static clockid_t csystemextras_monotonic_clockid() {
return CLOCK_MONOTONIC;
}
6 changes: 6 additions & 0 deletions Sources/SystemExtras/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Android
import CSystem
import ucrt
#elseif os(WASI)
import CSystemExtras
import WASILibc
#else
#error("Unsupported Platform")
Expand Down Expand Up @@ -48,6 +49,11 @@ extension Clock {
public static var monotonic: Clock { Clock(rawValue: _CLOCK_MONOTONIC) }
#endif

#if os(WASI)
@_alwaysEmitIntoClient
public static var monotonic: Clock { Clock(rawValue: csystemextras_monotonic_clockid()) }
#endif

#if os(OpenBSD) || os(FreeBSD)
@_alwaysEmitIntoClient
public static var uptime: Clock { Clock(rawValue: _CLOCK_UPTIME) }
Expand Down
4 changes: 1 addition & 3 deletions Sources/WASI/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ public protocol MonotonicClock {
/// A monotonic clock that uses the system's monotonic clock.
public struct SystemMonotonicClock: MonotonicClock {
private var underlying: SystemExtras.Clock {
#if os(Linux) || os(Android)
#if os(Linux) || os(Android) || os(WASI)
return .monotonic
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
return .rawUptime
#elseif os(WASI)
return .monotonic
#elseif os(OpenBSD) || os(FreeBSD)
return .uptime
#else
Expand Down
32 changes: 16 additions & 16 deletions Sources/WASI/Platform/PlatformTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ extension WASIAbi.Fdflags {
if platformOpenOptions.contains(.nonBlocking) {
fdFlags.insert(.NONBLOCK)
}
#if !os(WASI)
if platformOpenOptions.contains(.dataSync) {
fdFlags.insert(.DSYNC)
}
if platformOpenOptions.contains(.fileSync) {
fdFlags.insert(.SYNC)
}
#endif
#if !os(WASI)
if platformOpenOptions.contains(.dataSync) {
fdFlags.insert(.DSYNC)
}
if platformOpenOptions.contains(.fileSync) {
fdFlags.insert(.SYNC)
}
#endif
#if os(Linux)
if platformOpenOptions.contains(.readSync) {
fdFlags.insert(.RSYNC)
Expand All @@ -69,14 +69,14 @@ extension WASIAbi.Fdflags {
if self.contains(.NONBLOCK) {
flags.insert(.nonBlocking)
}
#if !os(WASI)
if self.contains(.DSYNC) {
flags.insert(.dataSync)
}
if self.contains(.SYNC) {
flags.insert(.fileSync)
}
#endif
#if !os(WASI)
if self.contains(.DSYNC) {
flags.insert(.dataSync)
}
if self.contains(.SYNC) {
flags.insert(.fileSync)
}
#endif
#if os(Linux)
if self.contains(.RSYNC) {
flags.insert(.readSync)
Expand Down
8 changes: 6 additions & 2 deletions Sources/WASI/Platform/SandboxPrimitives/Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ struct PathResolution {
throw openErrno
}

try self.symlink(component: component)
#if os(WASI)
throw Errno.notSupported
#else
try self.symlink(component: component)
#endif
#endif
}
}
}

#if !os(Windows)
#if !os(Windows) && !os(WASI)
mutating func symlink(component: FilePath.Component) throws {
/// Thin wrapper around readlinkat(2)
func _readlinkat(_ fd: CInt, _ path: UnsafePointer<CChar>) throws -> FilePath {
Expand Down
1 change: 0 additions & 1 deletion Sources/WIT/PackageResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,3 @@ public struct LocalFileLoader: PackageFileLoader {
return try fileManager.contentsOfDirectory(atPath: depsDir.path)
}
}

16 changes: 11 additions & 5 deletions Sources/WasmKit/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public struct EngineConfiguration {
/// The threading model, which determines how to dispatch instruction
/// execution, to use for the virtual machine interpreter.
public enum ThreadingModel {
/// Direct threaded code
/// - Note: This is the default model for platforms that support
/// `musttail` calls.
case direct
#if !os(WASI)
/// Direct threaded code
/// - Note: This is the default model for platforms that support
/// `musttail` calls.
case direct
#endif
/// Indirect threaded code
/// - Note: This is a fallback model for platforms that do not support
/// `musttail` calls.
Expand All @@ -48,7 +50,11 @@ public struct EngineConfiguration {
}

static var defaultForCurrentPlatform: ThreadingModel {
return useDirectThreadedCode ? .direct : .token
#if os(WASI)
return .token
#else
return useDirectThreadedCode ? .direct : .token
#endif
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/WasmKit/Execution/DispatchInstruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,7 @@ extension Execution {
}
}

#if !os(WASI)
extension Instruction {
/// The tail-calling execution handler for the instruction.
var handler: UInt {
Expand All @@ -1941,3 +1942,4 @@ extension Instruction {
}
}
}
#endif
46 changes: 25 additions & 21 deletions Sources/WasmKit/Execution/Execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ extension Execution {
)
do {
switch self.store.value.engine.configuration.threadingModel {
case .direct:
try runDirectThreaded(sp: sp, pc: pc, md: md, ms: ms)
#if !os(WASI)
case .direct:
try runDirectThreaded(sp: sp, pc: pc, md: md, ms: ms)
#endif
case .token:
try runTokenThreaded(sp: &sp, pc: &pc, md: &md, ms: &ms)
}
Expand All @@ -395,27 +397,29 @@ extension Execution {
}
}

/// Starts the main execution loop using the direct threading model.
@inline(never)
mutating func runDirectThreaded(
sp: Sp, pc: Pc, md: Md, ms: Ms
) throws {
var pc = pc
let handler = pc.read(wasmkit_tc_exec.self)
wasmkit_tc_start(handler, sp, pc, md, ms, &self)
if let (rawError, trappingSp) = self.trap {
let error = unsafeBitCast(rawError, to: Error.self)
// Manually release the error object because the trap is caught in C and
// held as a raw pointer.
wasmkit_swift_errorRelease(rawError)

guard let trap = error as? Trap else {
throw error
#if !os(WASI)
/// Starts the main execution loop using the direct threading model.
@inline(never)
mutating func runDirectThreaded(
sp: Sp, pc: Pc, md: Md, ms: Ms
) throws {
var pc = pc
let handler = pc.read(wasmkit_tc_exec.self)
wasmkit_tc_start(handler, sp, pc, md, ms, &self)
if let (rawError, trappingSp) = self.trap {
let error = unsafeBitCast(rawError, to: Error.self)
// Manually release the error object because the trap is caught in C and
// held as a raw pointer.
wasmkit_swift_errorRelease(rawError)

guard let trap = error as? Trap else {
throw error
}
// Attach backtrace if the thrown error is a trap
throw trap.withBacktrace(Self.captureBacktrace(sp: trappingSp, store: store.value))
}
// Attach backtrace if the thrown error is a trap
throw trap.withBacktrace(Self.captureBacktrace(sp: trappingSp, store: store.value))
}
}
#endif

#if EngineStats
/// A helper structure for collecting instruction statistics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ typealias OpcodeID = UInt64
extension Instruction {
func headSlot(threadingModel: EngineConfiguration.ThreadingModel) -> CodeSlot {
switch threadingModel {
case .direct:
return CodeSlot(handler)
#if !os(WASI)
case .direct:
return CodeSlot(handler)
#endif
case .token:
return opcodeID
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/_CWasmKit/include/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// rather than a language standard feature after LLVM 19. We check
// `__has_attribute(swiftasynccc)` too for compatibility with older versions.
// See https://github.com/llvm/llvm-project/pull/85347
#if __has_feature(swiftasynccc) || __has_extension(swiftasynccc)
#if !defined(__wasi__) && (__has_feature(swiftasynccc) || __has_extension(swiftasynccc))
# define WASMKIT_HAS_SWIFTASYNCCC 1
#else
# define WASMKIT_HAS_SWIFTASYNCCC 0
Expand Down
2 changes: 2 additions & 0 deletions Sources/_CWasmKit/include/_CWasmKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef void *_Nullable Pc;
typedef void *_Nullable Md;
typedef size_t Ms;

#if WASMKIT_USE_DIRECT_THREADED_CODE
/// The function type for executing a single instruction and transitioning to
/// the next instruction by tail calling. `swiftasync` calling convention is
/// used to keep `state` in the context register and to force tail calling.
Expand All @@ -40,6 +41,7 @@ static inline void wasmkit_tc_start(
) {
exec(sp, pc, md, ms, state);
}
#endif

static inline void wasmkit_fwrite_stderr(const char *_Nonnull str, size_t len) {
fwrite(str, 1, len, stderr);
Expand Down
2 changes: 2 additions & 0 deletions Utilities/Sources/VMGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ enum VMGen {
+ generateDirectThreadedCode(instructions: instructions, inlineImpls: inlineImpls)
+ """

#if !os(WASI)
extension Instruction {
/// The tail-calling execution handler for the instruction.
var handler: UInt {
Expand All @@ -471,6 +472,7 @@ enum VMGen {
}
}
}
#endif

"""
),
Expand Down
Loading