Skip to content

Add entrypoint for WASI reactor initialization #173

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 1 commit into from
Jan 10, 2025
Merged
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
26 changes: 26 additions & 0 deletions Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import WasmKit
public typealias WASIBridgeToHost = WASI.WASIBridgeToHost

extension WASIBridgeToHost {

/// Register the WASI implementation to the given `imports`.
///
/// - Parameters:
/// - imports: The imports scope to register the WASI implementation.
/// - store: The store to create the host functions.
public func link(to imports: inout Imports, store: Store) {
for (moduleName, module) in wasiHostModules {
for (name, function) in module.functions {
Expand Down Expand Up @@ -35,6 +41,13 @@ extension WASIBridgeToHost {
}
}

/// Start a WASI application as a `command` instance.
///
/// See <https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md>
/// for more information about the WASI Preview 1 Application ABI.
///
/// - Parameter instance: The WASI application instance.
/// - Returns: The exit code returned by the WASI application.
public func start(_ instance: Instance) throws -> UInt32 {
do {
guard let start = instance.exports[function: "_start"] else {
Expand All @@ -47,6 +60,19 @@ extension WASIBridgeToHost {
return 0
}

/// Start a WASI application as a `reactor` instance.
///
/// See <https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md>
/// for more information about the WASI Preview 1 Application ABI.
///
/// - Parameter instance: The WASI application instance.
public func initialize(_ instance: Instance) throws {
if let initialize = instance.exports[function: "_initialize"] {
// Call the optional `_initialize` function.
_ = try initialize()
}
}

@available(*, deprecated, message: "Use `Engine`-based API instead")
public func start(_ instance: Instance, runtime: Runtime) throws -> UInt32 {
return try start(instance)
Expand Down
Loading