Skip to content

Commit c5b4506

Browse files
committed
Add tests for new ABI types memory layout
Types like Clock, Subscription, and Event that cover WebAssembly System Interface should have their size, alignment, and load/store functionality tested for conformance with the ABI specified in the standard https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md#-subscription_clock-record This change adds corresponding tests that cover this.
1 parent 498d0be commit c5b4506

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Tests/WASITests/WASITests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import XCTest
22

3+
import WasmKit
4+
import WasmTypes
35
@testable import WASI
46

57
final class WASITests: XCTestCase {
@@ -134,4 +136,37 @@ final class WASITests: XCTestCase {
134136
XCTAssertEqual(error, .ELOOP)
135137
}
136138
}
139+
140+
func testWASIAbi() throws {
141+
let engine = Engine()
142+
let store = Store(engine: engine)
143+
let memory = try Memory(store: store, type: .init(min: 1))
144+
145+
// Test union size and alignment end-to-end
146+
let start = UnsafeGuestRawPointer(memorySpace: memory, offset: 0)
147+
var pointer = start
148+
let read = WASIAbi.Subscription.Union.fdRead(.init(0))
149+
let write = WASIAbi.Subscription.Union.fdWrite(.init(0))
150+
let clock = WASIAbi.Subscription.Union.clock(.init(id: .REALTIME, timeout: 42, precision: 0, flags: []))
151+
let event = WASIAbi.Event(userData: 3, error: .EIO, eventType: .fdRead, fdReadWrite: .init(nBytes: 37, flags: [.hangup]))
152+
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 1, union: read))
153+
XCTAssertEqual(pointer.offset, 48)
154+
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 2, union: write))
155+
XCTAssertEqual(pointer.offset, 48 * 2)
156+
WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 3, union: clock))
157+
XCTAssertEqual(pointer.offset, 48 * 3)
158+
WASIAbi.Event.writeToGuest(at: &pointer, value: event)
159+
XCTAssertEqual(pointer.offset, 48 * 3 + 32)
160+
161+
// Test that reading back yields same result
162+
pointer = start
163+
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 1, union: read))
164+
XCTAssertEqual(pointer.offset, 48)
165+
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 2, union: write))
166+
XCTAssertEqual(pointer.offset, 48 * 2)
167+
XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 3, union: clock))
168+
XCTAssertEqual(pointer.offset, 48 * 3)
169+
XCTAssertEqual(WASIAbi.Event.readFromGuest(&pointer), event)
170+
XCTAssertEqual(pointer.offset, 48 * 3 + 32)
171+
}
137172
}

0 commit comments

Comments
 (0)