Skip to content

Commit 918bb21

Browse files
Engine API: Migrate all Runtime usage in ./Tests
1 parent 90ad3d4 commit 918bb21

File tree

11 files changed

+76
-61
lines changed

11 files changed

+76
-61
lines changed

Sources/WITOverlayGenerator/HostGenerators/HostWorldGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct HostWorldGenerator: ASTVisitor {
2323
struct \(ConvertCase.pascalCase(world.name)) {
2424
let instance: WasmKit.Instance
2525
26-
static func link(_ hostModules: inout [String: HostModule]) {
26+
static func link(to imports: inout Imports, store: Store) {
2727
}
2828
""")
2929
// Enter world struct body

Sources/WasmKit/Engine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public final class Engine {
1111
/// - Parameters:
1212
/// - configuration: The engine configuration.
1313
/// - interceptor: An optional runtime interceptor to intercept execution of instructions.
14-
public init(configuration: EngineConfiguration, interceptor: EngineInterceptor? = nil) {
14+
public init(configuration: EngineConfiguration = EngineConfiguration(), interceptor: EngineInterceptor? = nil) {
1515
self.configuration = configuration
1616
self.interceptor = interceptor
1717
self.funcTypeInterner = Interner()

Sources/WasmKit/Execution/Instances.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ public struct Memory: Equatable {
367367
let handle: InternalMemory
368368
let allocator: StoreAllocator
369369

370+
init(handle: InternalMemory, allocator: StoreAllocator) {
371+
self.handle = handle
372+
self.allocator = allocator
373+
}
374+
375+
/// Creates a new memory instance with the given type.
376+
public init(store: Store, type: MemoryType) throws {
377+
self.init(
378+
handle: try store.allocator.allocate(memoryType: type, resourceLimiter: store.resourceLimiter),
379+
allocator: store.allocator
380+
)
381+
}
382+
370383
/// Returns a copy of the memory data.
371384
public var data: [UInt8] {
372385
handle.data

Sources/WasmKitWASI/WASIBridgeToHost+WasmKit.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ public typealias WASIBridgeToHost = WASI.WASIBridgeToHost
55

66
extension WASIBridgeToHost {
77
public func link(to imports: inout Imports, store: Store) {
8-
var imports = Imports()
98
for (moduleName, module) in wasiHostModules {
109
for (name, function) in module.functions {
1110
imports.define(

Tests/WASITests/IntegrationTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ final class IntegrationTests: XCTestCase {
116116
$0[$1] = suitePath.appendingPathComponent($1).path
117117
}
118118
)
119-
let runtime = Runtime(hostModules: wasi.hostModules)
119+
let engine = Engine()
120+
let store = Store(engine: engine)
121+
var imports = Imports()
122+
wasi.link(to: &imports, store: store)
120123
let module = try parseWasm(filePath: FilePath(path.path))
121-
let instance = try runtime.instantiate(module: module)
122-
let exitCode = try wasi.start(instance, runtime: runtime)
124+
let instance = try module.instantiate(store: store, imports: imports)
125+
let exitCode = try wasi.start(instance)
123126
XCTAssertEqual(exitCode, manifest.exitCode ?? 0, path.path)
124127
}
125128
}

Tests/WITOverlayGeneratorTests/Runtime/RuntimeSmokeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import XCTest
66
class RuntimeSmokeTests: XCTestCase {
77
func testCallExportByGuest() throws {
88
var harness = try RuntimeTestHarness(fixture: "Smoke")
9-
try harness.build(link: SmokeTestWorld.link(_:)) { (runtime, instance) in
9+
try harness.build(link: SmokeTestWorld.link) { (instance) in
1010
let component = SmokeTestWorld(instance: instance)
1111
_ = try component.hello()
1212
}

Tests/WITOverlayGeneratorTests/Runtime/RuntimeTestHarness.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,24 @@ struct RuntimeTestHarness {
123123

124124
/// Build up WebAssembly module from the fixture and instantiate WasmKit runtime with the module.
125125
mutating func build(
126-
link: (inout [String: HostModule]) -> Void,
127-
run: (Runtime, Instance) throws -> Void
126+
link: (inout Imports, Store) -> Void,
127+
run: (Instance) throws -> Void
128128
) throws {
129129
for compile in [compileForEmbedded, compileForWASI] {
130130
defer { cleanupTemporaryFiles() }
131131
let compiled = try compile(collectGuestInputFiles())
132132

133+
let engine = Engine()
134+
let store = Store(engine: engine)
135+
133136
let wasi = try WASIBridgeToHost(args: [compiled.path])
134-
var hostModules: [String: HostModule] = wasi.hostModules
135-
link(&hostModules)
137+
var imports = Imports()
138+
wasi.link(to: &imports, store: store)
139+
link(&imports, store)
136140

137141
let module = try parseWasm(filePath: .init(compiled.path))
138-
let runtime = Runtime(hostModules: hostModules)
139-
let instance = try runtime.instantiate(module: module)
140-
try run(runtime, instance)
142+
let instance = try module.instantiate(store: store, imports: imports)
143+
try run(instance)
141144
}
142145
}
143146

Tests/WITOverlayGeneratorTests/Runtime/RuntimeTypesTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import XCTest
77
class RuntimeTypesTests: XCTestCase {
88
func testNumber() throws {
99
var harness = try RuntimeTestHarness(fixture: "Number")
10-
try harness.build(link: NumberTestWorld.link(_:)) { (runtime, instance) in
10+
try harness.build(link: NumberTestWorld.link) { (instance) in
1111
let component = NumberTestWorld(instance: instance)
1212

1313
XCTAssertEqual(try component.roundtripBool(v: true), true)
@@ -74,7 +74,7 @@ class RuntimeTypesTests: XCTestCase {
7474

7575
func testChar() throws {
7676
var harness = try RuntimeTestHarness(fixture: "Char")
77-
try harness.build(link: CharTestWorld.link(_:)) { (runtime, instance) in
77+
try harness.build(link: CharTestWorld.link) { (instance) in
7878
let component = CharTestWorld(instance: instance)
7979

8080
for char in "abcd🍏👨‍👩‍👦‍👦".unicodeScalars {
@@ -85,7 +85,7 @@ class RuntimeTypesTests: XCTestCase {
8585

8686
func testOption() throws {
8787
var harness = try RuntimeTestHarness(fixture: "Option")
88-
try harness.build(link: OptionTestWorld.link(_:)) { (runtime, instance) in
88+
try harness.build(link: OptionTestWorld.link) { (instance) in
8989
let component = OptionTestWorld(instance: instance)
9090
let value1 = try component.returnNone()
9191
XCTAssertEqual(value1, nil)
@@ -111,7 +111,7 @@ class RuntimeTypesTests: XCTestCase {
111111

112112
func testRecord() throws {
113113
var harness = try RuntimeTestHarness(fixture: "Record")
114-
try harness.build(link: RecordTestWorld.link(_:)) { (runtime, instance) in
114+
try harness.build(link: RecordTestWorld.link) { (instance) in
115115
let component = RecordTestWorld(instance: instance)
116116
_ = try component.returnEmpty()
117117

@@ -129,7 +129,7 @@ class RuntimeTypesTests: XCTestCase {
129129

130130
func testString() throws {
131131
var harness = try RuntimeTestHarness(fixture: "String")
132-
try harness.build(link: StringTestWorld.link(_:)) { (runtime, instance) in
132+
try harness.build(link: StringTestWorld.link) { (instance) in
133133
let component = StringTestWorld(instance: instance)
134134
XCTAssertEqual(try component.returnEmpty(), "")
135135
XCTAssertEqual(try component.roundtrip(v: "ok"), "ok")
@@ -142,7 +142,7 @@ class RuntimeTypesTests: XCTestCase {
142142

143143
func testList() throws {
144144
var harness = try RuntimeTestHarness(fixture: "List")
145-
try harness.build(link: ListTestWorld.link(_:)) { (runtime, instance) in
145+
try harness.build(link: ListTestWorld.link) { (instance) in
146146
let component = ListTestWorld(instance: instance)
147147
XCTAssertEqual(try component.returnEmpty(), [])
148148
for value in [[], [1, 2, 3]] as [[UInt8]] {
@@ -157,7 +157,7 @@ class RuntimeTypesTests: XCTestCase {
157157

158158
func testVariant() throws {
159159
var harness = try RuntimeTestHarness(fixture: "Variant")
160-
try harness.build(link: VariantTestWorld.link(_:)) { (runtime, instance) in
160+
try harness.build(link: VariantTestWorld.link) { (instance) in
161161
let component = VariantTestWorld(instance: instance)
162162
XCTAssertEqual(try component.returnSingle(), .a(33_550_336))
163163

@@ -185,7 +185,7 @@ class RuntimeTypesTests: XCTestCase {
185185

186186
func testResult() throws {
187187
var harness = try RuntimeTestHarness(fixture: "Result")
188-
try harness.build(link: ResultTestWorld.link(_:)) { (runtime, instance) in
188+
try harness.build(link: ResultTestWorld.link) { (instance) in
189189
let component = ResultTestWorld(instance: instance)
190190

191191
let value4 = try component.roundtripResult(v: .success(()))
@@ -217,7 +217,7 @@ class RuntimeTypesTests: XCTestCase {
217217

218218
func testEnum() throws {
219219
var harness = try RuntimeTestHarness(fixture: "Enum")
220-
try harness.build(link: EnumTestWorld.link(_:)) { (runtime, instance) in
220+
try harness.build(link: EnumTestWorld.link) { (instance) in
221221
let component = EnumTestWorld(instance: instance)
222222

223223
let value1 = try component.roundtripSingle(v: .a)
@@ -236,7 +236,7 @@ class RuntimeTypesTests: XCTestCase {
236236

237237
func testFlags() throws {
238238
var harness = try RuntimeTestHarness(fixture: "Flags")
239-
try harness.build(link: FlagsTestWorld.link(_:)) { (runtime, instance) in
239+
try harness.build(link: FlagsTestWorld.link) { (instance) in
240240
let component = FlagsTestWorld(instance: instance)
241241

242242
XCTAssertEqual(try component.roundtripSingle(v: []), [])
@@ -260,7 +260,7 @@ class RuntimeTypesTests: XCTestCase {
260260

261261
func testTuple() throws {
262262
var harness = try RuntimeTestHarness(fixture: "Tuple")
263-
try harness.build(link: TupleTestWorld.link(_:)) { (runtime, instance) in
263+
try harness.build(link: TupleTestWorld.link) { (instance) in
264264
let component = TupleTestWorld(instance: instance)
265265
let value1 = try component.roundtrip(v: (true, 42))
266266
XCTAssertEqual(value1.0, true)
@@ -270,7 +270,7 @@ class RuntimeTypesTests: XCTestCase {
270270

271271
func testInterface() throws {
272272
var harness = try RuntimeTestHarness(fixture: "Interface")
273-
try harness.build(link: InterfaceTestWorld.link(_:)) { (runtime, instance) in
273+
try harness.build(link: InterfaceTestWorld.link) { (instance) in
274274
let component = InterfaceTestWorld(instance: instance)
275275
let value1 = try component.roundtripT1(v: 42)
276276
XCTAssertEqual(value1, 42)
@@ -284,7 +284,7 @@ class RuntimeTypesTests: XCTestCase {
284284
func testNaming() throws {
285285
// Ensure compilation succeed for both host and guest
286286
var harness = try RuntimeTestHarness(fixture: "Naming")
287-
try harness.build(link: NamingTestWorld.link(_:), run: { _, _ in })
287+
try harness.build(link: NamingTestWorld.link, run: { _ in })
288288
}
289289
}
290290

Tests/WasmKitTests/Execution/HostModuleTests.swift

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@ import XCTest
66

77
final class HostModuleTests: XCTestCase {
88
func testImportMemory() throws {
9-
let runtime = Runtime()
9+
let engine = Engine()
10+
let store = Store(engine: engine)
1011
let memoryType = MemoryType(min: 1, max: nil)
11-
let memory = try runtime.store.allocator.allocate(
12-
memoryType: memoryType, resourceLimiter: DefaultResourceLimiter())
13-
try runtime.register(
14-
HostModule(
15-
memories: [
16-
"memory": Memory(
17-
handle: memory,
18-
allocator: runtime.store
19-
.allocator
20-
)
21-
]
22-
),
23-
as: "env"
24-
)
12+
let memory = try WasmKit.Memory(store: store, type: memoryType)
13+
let imports: Imports = [
14+
"env": ["memory": memory]
15+
]
2516

2617
let module = try parseWasm(
2718
bytes: wat2wasm(
@@ -30,13 +21,14 @@ final class HostModuleTests: XCTestCase {
3021
(import "env" "memory" (memory 1))
3122
)
3223
"""))
33-
XCTAssertNoThrow(try runtime.instantiate(module: module))
24+
XCTAssertNoThrow(try module.instantiate(store: store, imports: imports))
3425
// Ensure the allocated address is valid
3526
_ = memory.data
3627
}
3728

3829
func testReentrancy() throws {
39-
let runtime = Runtime()
30+
let engine = Engine()
31+
let store = Store(engine: engine)
4032
let voidSignature = WasmTypes.FunctionType(parameters: [], results: [])
4133
let module = try parseWasm(
4234
bytes: wat2wasm(
@@ -58,9 +50,9 @@ final class HostModuleTests: XCTestCase {
5850

5951
var isExecutingFoo = false
6052
var isQuxCalled = false
61-
let hostModule = HostModule(
62-
functions: [
63-
"bar": HostFunction(type: voidSignature) { caller, _ in
53+
let imports: Imports = [
54+
"env": [
55+
"bar": Function(store: store, type: voidSignature) { caller, _ in
6456
// Ensure "invoke" executes instructions under the current call
6557
XCTAssertFalse(isExecutingFoo, "bar should not be called recursively")
6658
isExecutingFoo = true
@@ -69,17 +61,17 @@ final class HostModuleTests: XCTestCase {
6961
_ = try foo()
7062
return []
7163
},
72-
"qux": HostFunction(type: voidSignature) { _, _ in
64+
"qux": Function(store: store, type: voidSignature) { caller, _ in
7365
XCTAssertTrue(isExecutingFoo)
7466
isQuxCalled = true
7567
return []
7668
},
7769
]
78-
)
79-
try runtime.register(hostModule, as: "env")
80-
let instance = try runtime.instantiate(module: module)
70+
]
71+
let instance = try module.instantiate(store: store, imports: imports)
8172
// Check foo(wasm) -> bar(host) -> baz(wasm) -> qux(host)
82-
_ = try runtime.invoke(instance, function: "foo")
73+
let foo = try XCTUnwrap(instance.exports[function: "foo"])
74+
try foo()
8375
XCTAssertTrue(isQuxCalled)
8476
}
8577
}

Tests/WasmKitTests/Execution/Runtime/StoreAllocatorTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ final class StoreAllocatorTests: XCTestCase {
3434
(memory (;0;) 0)
3535
(export "a" (memory 0)))
3636
"""))
37-
let runtime = Runtime()
38-
_ = try runtime.instantiate(module: module)
39-
weakAllocator = runtime.store.allocator
37+
let engine = Engine()
38+
let store = Store(engine: engine)
39+
_ = try module.instantiate(store: store)
40+
weakAllocator = store.allocator
4041
}
4142
XCTAssertNil(weakAllocator)
4243
}

0 commit comments

Comments
 (0)