Skip to content

Commit 90ad3d4

Browse files
Engine API: Migrate WITOverlayGenerator
1 parent f931f53 commit 90ad3d4

File tree

7 files changed

+86
-81
lines changed

7 files changed

+86
-81
lines changed

Sources/WITOverlayGenerator/HostGenerators/HostExportFunction.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,28 @@ struct HostExportFunction {
252252
)
253253
var signature = try signatureTranslation.signature(function: function, name: ConvertCase.camelCase(kebab: name.apiSwiftName))
254254
let witParameters = signature.parameters.map(\.label)
255-
signature.parameters.insert(("runtime", "Runtime"), at: 0)
256255
signature.hasThrows = true
257256
printer.write(line: signature.description + " {")
258257
try printer.indent {
259258
let optionsVar = builder.variable("options")
260259
printer.write(line: "let \(optionsVar) = CanonicalOptions._derive(from: instance, exportName: \"\(name.abiName)\")")
261-
printer.write(line: "let \(context.contextVar) = CanonicalCallContext(options: \(optionsVar), instance: instance, runtime: runtime)")
260+
printer.write(line: "let \(context.contextVar) = CanonicalCallContext(options: \(optionsVar), instance: instance)")
262261
// Suppress unused variable warning for "context"
263262
printer.write(line: "_ = \(context.contextVar)")
264263

265264
let arguments = try printLowerArguments(
266265
parameterNames: witParameters, coreSignature: coreSignature,
267266
typeResolver: typeResolver, printer: printer
268267
)
269-
var call = "try runtime.invoke(instance, function: \"\(name.abiName)\""
268+
let functionVar = builder.variable("function")
269+
printer.write(multiline: """
270+
guard let \(functionVar) = instance.exports[function: \"\(name.abiName)\"] else {
271+
throw CanonicalABIError(description: "Function \\"\(name.abiName)\\" not found in the instance")
272+
}
273+
""")
274+
var call = "try \(functionVar)("
270275
if !arguments.isEmpty {
271-
call += ", with: [\(arguments.map(\.description).joined(separator: ", "))]"
276+
call += "[\(arguments.map(\.description).joined(separator: ", "))]"
272277
}
273278
call += ")"
274279
if coreSignature.isIndirectResult {

Sources/WasmKit/Component/CanonicalCall.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
@_exported import WasmTypes
22

3-
struct CanonicalABIError: Error, CustomStringConvertible {
4-
let description: String
3+
/// Error type for canonical ABI operations.
4+
public struct CanonicalABIError: Error, CustomStringConvertible {
5+
public let description: String
6+
7+
@_documentation(visibility: internal)
8+
public init(description: String) {
9+
self.description = description
10+
}
511
}
612

713
/// Call context for `(canon lift)` or `(canon lower)` operations.
@@ -14,17 +20,14 @@ public struct CanonicalCallContext {
1420
public let options: CanonicalOptions
1521
/// The module instance that defines the lift/lower operation.
1622
public let instance: Instance
17-
/// The executing `Runtime` instance
18-
public let runtime: Runtime
1923
/// A reference to the guest memory.
2024
public var guestMemory: Memory {
2125
options.memory
2226
}
2327

24-
public init(options: CanonicalOptions, instance: Instance, runtime: Runtime) {
28+
public init(options: CanonicalOptions, instance: Instance) {
2529
self.options = options
2630
self.instance = instance
27-
self.runtime = runtime
2831
}
2932

3033
/// Call `cabi_realloc` export with the given arguments.
@@ -56,9 +59,9 @@ extension CanonicalCallContext {
5659
return instance
5760
}
5861

59-
@available(*, deprecated, renamed: "init(options:instance:runtime:)")
62+
@available(*, deprecated, renamed: "init(options:instance:)")
6063
public init(options: CanonicalOptions, moduleInstance: Instance, runtime: Runtime) {
61-
self.init(options: options, instance: moduleInstance, runtime: runtime)
64+
self.init(options: options, instance: moduleInstance)
6265
}
6366
}
6467

Sources/WasmKit/Component/CanonicalOptions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public struct CanonicalOptions {
3535
/// FIXME: This deriviation is wrong because the options should be determined by `(canon lift)` or `(canon lower)`
3636
/// in an encoded component at componetizing-time. (e.g. wit-component tool is one of the componetizers)
3737
/// Remove this temporary method after we will accept binary form of component file.
38-
public static func _derive(from moduleInstance: Instance, exportName: String) -> CanonicalOptions {
39-
guard case let .memory(memory) = moduleInstance.exports["memory"] else {
38+
public static func _derive(from instance: Instance, exportName: String) -> CanonicalOptions {
39+
guard case let .memory(memory) = instance.exports["memory"] else {
4040
fatalError("Missing required \"memory\" export")
4141
}
4242
return CanonicalOptions(
4343
memory: memory, stringEncoding: .utf8,
44-
realloc: moduleInstance.exportedFunction(name: "cabi_realloc"),
45-
postReturn: moduleInstance.exportedFunction(name: "cabi_post_\(exportName)"))
44+
realloc: instance.exportedFunction(name: "cabi_realloc"),
45+
postReturn: instance.exportedFunction(name: "cabi_post_\(exportName)"))
4646
}
4747
}

Sources/WasmKit/Execution/Runtime.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import WasmParser
22

33
/// A container to manage execution state of one or more module instances.
4+
@available(*, deprecated, message: "Use `Engine` instead")
45
public final class Runtime {
56
public let store: Store
67
let engine: Engine
@@ -24,7 +25,6 @@ public final class Runtime {
2425
/// - Parameter hostModules: Host module names mapped to their corresponding ``HostModule`` definitions.
2526
/// - Parameter interceptor: An optional runtime interceptor to intercept execution of instructions.
2627
/// - Parameter configuration: An optional runtime configuration to customize the runtime behavior.
27-
@available(*, deprecated)
2828
public init(
2929
hostModules: [String: HostModule] = [:],
3030
interceptor: EngineInterceptor? = nil,
@@ -44,9 +44,7 @@ public final class Runtime {
4444
func internType(_ type: FunctionType) -> InternedFuncType {
4545
return funcTypeInterner.intern(type)
4646
}
47-
}
4847

49-
extension Runtime {
5048
public func instantiate(module: Module) throws -> Instance {
5149
return try module.instantiate(
5250
store: store,
@@ -137,9 +135,7 @@ extension Runtime {
137135

138136
return result
139137
}
140-
}
141138

142-
extension Runtime {
143139
@available(*, unavailable, message: "Runtime doesn't manage execution state anymore. Use Execution.step instead")
144140
public func step() throws {
145141
fatalError()

Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extension WASIBridgeToHost {
4747
return 0
4848
}
4949

50+
@available(*, deprecated, message: "Use `Engine`-based API instead")
5051
public func start(_ instance: Instance, runtime: Runtime) throws -> UInt32 {
5152
return try start(instance)
5253
}

Tests/WITOverlayGeneratorTests/Runtime/RuntimeSmokeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class RuntimeSmokeTests: XCTestCase {
88
var harness = try RuntimeTestHarness(fixture: "Smoke")
99
try harness.build(link: SmokeTestWorld.link(_:)) { (runtime, instance) in
1010
let component = SmokeTestWorld(instance: instance)
11-
_ = try component.hello(runtime: runtime)
11+
_ = try component.hello()
1212
}
1313
}
1414
}

0 commit comments

Comments
 (0)