From 34ae4c81d1dc8cd2d2a6835ead709fa432cefbc9 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 10 Jan 2025 23:07:21 +0900 Subject: [PATCH] Add entrypoint for WASI reactor initialization --- .../WASIBridgeToHost+WasmKit.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift b/Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift index 67de2d90..f5470276 100644 --- a/Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift +++ b/Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift @@ -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 { @@ -35,6 +41,13 @@ extension WASIBridgeToHost { } } + /// Start a WASI application as a `command` instance. + /// + /// See + /// 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 { @@ -47,6 +60,19 @@ extension WASIBridgeToHost { return 0 } + /// Start a WASI application as a `reactor` instance. + /// + /// See + /// 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)