Skip to content

Commit 79aaf6e

Browse files
Bring Function examples from old HostFunction
1 parent fa138d8 commit 79aaf6e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/WasmKit/Execution/Function.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@ import struct WasmTypes.FunctionType
66
///
77
/// > Note:
88
/// <https://webassembly.github.io/spec/core/exec/runtime.html#function-instances>
9+
///
10+
/// ## Examples
11+
///
12+
/// This example section shows how to interact with WebAssembly process with ``Function``.
13+
///
14+
/// ### Print Int32 given by WebAssembly process
15+
///
16+
/// ```swift
17+
/// Function(store: store, parameters: [.i32]) { _, args in
18+
/// print(args[0])
19+
/// return []
20+
/// }
21+
/// ```
22+
///
23+
/// ### Print a UTF-8 string passed by a WebAssembly module instance
24+
///
25+
/// ```swift
26+
/// Function(store: store, parameters: [.i32, .i32]) { caller, args in
27+
/// let (stringPtr, stringLength) = (Int(args[0].i32), Int(args[1].i32))
28+
/// guard let memory = caller.instance?.exports[memory: "memory"] else {
29+
/// fatalError("Missing \"memory\" export")
30+
/// }
31+
/// let bytesRange = stringPtr..<(stringPtr + stringLength)
32+
/// print(String(decoding: memory.data[bytesRange], as: UTF8.self))
33+
/// return []
34+
/// }
35+
/// ```
936
public struct Function: Equatable {
1037
internal let handle: InternalFunction
1138
let store: Store

Sources/WasmKit/Module.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ typealias LabelIndex = UInt32
281281

282282
// MARK: - Module Entities
283283

284-
/// TODO: Rename to `GuestFunctionEntity`
285-
///
286284
/// An executable function representation in a module
287285
/// > Note:
288286
/// <https://webassembly.github.io/spec/core/syntax/modules.html#functions>

0 commit comments

Comments
 (0)