Skip to content

Commit 34ae4c8

Browse files
Add entrypoint for WASI reactor initialization
1 parent cc7dac1 commit 34ae4c8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import WasmKit
44
public typealias WASIBridgeToHost = WASI.WASIBridgeToHost
55

66
extension WASIBridgeToHost {
7+
8+
/// Register the WASI implementation to the given `imports`.
9+
///
10+
/// - Parameters:
11+
/// - imports: The imports scope to register the WASI implementation.
12+
/// - store: The store to create the host functions.
713
public func link(to imports: inout Imports, store: Store) {
814
for (moduleName, module) in wasiHostModules {
915
for (name, function) in module.functions {
@@ -35,6 +41,13 @@ extension WASIBridgeToHost {
3541
}
3642
}
3743

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

63+
/// Start a WASI application as a `reactor` instance.
64+
///
65+
/// See <https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md>
66+
/// for more information about the WASI Preview 1 Application ABI.
67+
///
68+
/// - Parameter instance: The WASI application instance.
69+
public func initialize(_ instance: Instance) throws {
70+
if let initialize = instance.exports[function: "_initialize"] {
71+
// Call the optional `_initialize` function.
72+
_ = try initialize()
73+
}
74+
}
75+
5076
@available(*, deprecated, message: "Use `Engine`-based API instead")
5177
public func start(_ instance: Instance, runtime: Runtime) throws -> UInt32 {
5278
return try start(instance)

0 commit comments

Comments
 (0)