Skip to content

Commit e3f8a40

Browse files
Engine API: Minor documentation updates and add accessors for entities
1 parent b667d3b commit e3f8a40

File tree

10 files changed

+54
-15
lines changed

10 files changed

+54
-15
lines changed

Sources/WasmKit/Docs.docc/Docs.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,26 @@ See [examples](https://github.com/swiftwasm/WasmKit/tree/main/Examples) for exec
8989

9090
### Basic Concepts
9191

92+
- ``Engine``
93+
- ``Store``
9294
- ``Module``
9395
- ``Instance``
94-
- ``Runtime``
95-
- ``Store``
96+
- ``Function``
9697

9798
### Binary Parser
9899

99100
- ``parseWasm(bytes:features:)``
100101
- ``parseWasm(filePath:features:)``
101102

103+
### Other WebAssembly Entities
104+
105+
- ``Global``
106+
- ``Memory``
107+
- ``Table``
108+
102109
### Extending Runtime
103110

104-
- ``HostModule``
105-
- ``HostFunction``
111+
- ``Imports``
106112
- ``Caller``
107113
- ``GuestMemory``
108114
- ``UnsafeGuestPointer``

Sources/WasmKit/Engine.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import _CWasmKit.Platform
22

33
/// A WebAssembly execution engine.
4+
///
5+
/// An engine is responsible storing the configuration for the execution of
6+
/// WebAssembly code such as interpreting mode, enabled features, etc.
7+
/// Typically, you will need a single engine instance per application.
48
public final class Engine {
9+
/// The engine configuration.
510
public let configuration: EngineConfiguration
611
let interceptor: EngineInterceptor?
712
let funcTypeInterner: Interner<FunctionType>
@@ -22,6 +27,7 @@ public final class Engine {
2227
public func instantiate(module: Module) -> Instance { fatalError() }
2328
}
2429

30+
/// The configuration for the WebAssembly execution engine.
2531
public struct EngineConfiguration {
2632
/// The threading model, which determines how to dispatch instruction
2733
/// execution, to use for the virtual machine interpreter.

Sources/WasmKit/Execution/Instances.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public struct Exports: Sequence {
143143
}
144144

145145
/// A stateful instance of a WebAssembly module.
146-
/// Usually instantiated by ``Runtime/instantiate(module:)``.
146+
/// Usually instantiated by ``Module/instantiate(store:imports:)``.
147147
/// > Note:
148148
/// <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
149149
public struct Instance {
@@ -310,6 +310,30 @@ typealias InternalTable = EntityHandle<TableEntity>
310310
public struct Table: Equatable {
311311
let handle: InternalTable
312312
let allocator: StoreAllocator
313+
314+
init(handle: InternalTable, allocator: StoreAllocator) {
315+
self.handle = handle
316+
self.allocator = allocator
317+
}
318+
319+
/// Creates a new table instance with the given type.
320+
public init(store: Store, type: TableType) throws {
321+
self.init(
322+
handle: try store.allocator.allocate(tableType: type, resourceLimiter: store.resourceLimiter),
323+
allocator: store.allocator
324+
)
325+
}
326+
327+
/// The type of the table instance.
328+
public var type: TableType {
329+
handle.tableType
330+
}
331+
332+
/// Accesses the element at the given index.
333+
public subscript(index: Int) -> Reference {
334+
get { handle.elements[index] }
335+
nonmutating set { handle.withValue { $0.elements[index] = newValue } }
336+
}
313337
}
314338

315339
struct MemoryEntity /* : ~Copyable */ {
@@ -389,6 +413,11 @@ public struct Memory: Equatable {
389413
public var data: [UInt8] {
390414
handle.data
391415
}
416+
417+
/// The type of the memory instance.
418+
public var type: MemoryType {
419+
handle.limit
420+
}
392421
}
393422

394423
extension Memory: GuestMemory {

Sources/WasmKit/Execution/Runtime.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ public final class Runtime {
205205
/// return []
206206
/// }
207207
/// ```
208+
@available(*, deprecated, renamed: "Function", message: "`HostFunction` is now unified with `Function`")
208209
public struct HostFunction {
209-
// @available(*, deprecated, renamed: "Function.init(store:type:implementation:)")
210+
@available(*, deprecated, renamed: "Function.init(store:type:implementation:)", message: "Use `Engine`-based API instead")
210211
public init(type: FunctionType, implementation: @escaping (Caller, [Value]) throws -> [Value]) {
211212
self.type = type
212213
self.implementation = implementation
@@ -217,6 +218,7 @@ public struct HostFunction {
217218
}
218219

219220
/// A collection of globals and functions that are exported from a host module.
221+
@available(*, deprecated, message: "Use `Imports`")
220222
public struct HostModule {
221223
public init(
222224
globals: [String: Global] = [:],

Sources/WasmKit/Execution/Store.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class Store {
1616
/// The allocator allocating and retaining resources for this store.
1717
let allocator: StoreAllocator
1818
/// The engine associated with this store.
19-
let engine: Engine
19+
public let engine: Engine
2020

2121
/// Create a new store associated with the given engine.
2222
public init(engine: Engine) {

Sources/WasmKit/Execution/StoreAllocator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ extension StoreAllocator {
467467

468468
/// > Note:
469469
/// <https://webassembly.github.io/spec/core/exec/modules.html#alloc-table>
470-
private func allocate(tableType: TableType, resourceLimiter: any ResourceLimiter) throws -> InternalTable {
470+
func allocate(tableType: TableType, resourceLimiter: any ResourceLimiter) throws -> InternalTable {
471471
let pointer = try tables.allocate(initializing: TableEntity(tableType, resourceLimiter: resourceLimiter))
472472
return InternalTable(unsafe: pointer)
473473
}

Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extension WASIBridgeToHost {
1616
}
1717
}
1818

19+
@available(*, deprecated, renamed: "link(to:store:)", message: "Use `Engine`-based API instead")
1920
public var hostModules: [String: HostModule] {
2021
wasiHostModules.mapValues { (module: WASIHostModule) -> HostModule in
2122
HostModule(

Sources/WasmParser/Docs.docc/Docs.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ while let payload = try parser.parseNext() {
6060

6161
### Core Module Elements
6262

63-
- ``FunctionType``
6463
- ``Import``
6564
- ``ImportDescriptor``
6665
- ``Export``

Sources/WasmParser/InstructionVisitor.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,7 @@ public struct InstructionTracingVisitor<V: InstructionVisitor>: InstructionVisit
12431243
/// A visitor for WebAssembly instructions.
12441244
///
12451245
/// The visitor pattern is used while parsing WebAssembly expressions to allow for easy extensibility.
1246-
/// See the following parsing functions:
1247-
/// - ``parseExpression(bytes:features:hasDataCount:visitor:)``
1248-
/// - ``parseExpression(stream:features:hasDataCount:visitor:)``
1246+
/// See the expression parsing method ``Code/parseExpression(visitor:)``
12491247
public protocol InstructionVisitor {
12501248

12511249
/// The return type of visitor methods.

Utilities/Sources/WasmGen.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ enum WasmGen {
6161
/// A visitor for WebAssembly instructions.
6262
///
6363
/// The visitor pattern is used while parsing WebAssembly expressions to allow for easy extensibility.
64-
/// See the following parsing functions:
65-
/// - ``parseExpression(bytes:features:hasDataCount:visitor:)``
66-
/// - ``parseExpression(stream:features:hasDataCount:visitor:)``
64+
/// See the expression parsing method ``Code/parseExpression(visitor:)``
6765
public protocol InstructionVisitor {
6866
6967
/// The return type of visitor methods.

0 commit comments

Comments
 (0)