Skip to content

Add printable core types and a typed Trap kind in WasmKitError - #391

Merged
kateinoigakukun merged 2 commits into
mainfrom
katei/error-kinds
Jul 29, 2026
Merged

Add printable core types and a typed Trap kind in WasmKitError#391
kateinoigakukun merged 2 commits into
mainfrom
katei/error-kinds

Conversation

@kateinoigakukun

@kateinoigakukun kateinoigakukun commented Jul 27, 2026

Copy link
Copy Markdown
Member

Part of the Embedded Swift support work (#357), split out for incremental review. Two commits:

Add CustomStringConvertible to core Wasm types — hand-written descriptions for ValueType, ReferenceType/HeapType, FunctionType, Value, Reference, V128 (WasmTypes) and Limits, TableType, GlobalType, Mutability (WasmParser). String interpolation of these types previously fell back to reflection-based stringification, which Embedded Swift lacks; the explicit descriptions produce wasm-flavored text instead, e.g. (i32, i32) -> (i32), i32(5), funcref. Array formatting goes through static descriptionList(_:) methods on the element types rather than extensions on Array itself.

WasmKitError: carry Trap as a typed error kind — groundwork for typed throws in the instantiation path. WasmKitError gains a trap kind so Trap-throwing sub-operations can participate in a throws(WasmKitError) chain, Trap/TrapReason are declared Sendable, and the few reflection-dependent error messages (value arrays, instance handles, backtrace addresses) are rewritten into explicit formatting so they also compile under Embedded Swift. Error message text changes slightly where reflection dumps were previously used; no test relied on the old text.

Comment thread Sources/WasmTypes/Description.swift Outdated
private func hexByte(_ byte: UInt8) -> String {
let digits = "0123456789abcdef"
let high = digits[digits.index(digits.startIndex, offsetBy: Int(byte >> 4))]
let low = digits[digits.index(digits.startIndex, offsetBy: Int(byte & 0xF))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe have digits as an array of ASCII bytes that you can directly index with O(1)?

extension Value {
/// Returns the descriptions of `values` joined with ", ".
public static func descriptionList(_ values: [Value]) -> String {
values.map { $0.description }.joined(separator: ", ")

@MaxDesiatov MaxDesiatov Jul 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunate that we don't have description(into: inout UTF8Span) or smth like that, printing values could potentially be a hot path after all (in debugging scenarios at least)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, UTF8Span is immutable? Maybe MutableSpan<UInt8> or whatever, and we can assume Value has a max description length that we can statically preallocate a buffer for, v128 being the longest possible value

Hand-written descriptions for value/reference/function/limit types so string
interpolation does not fall back to reflection, which Embedded Swift lacks.
Output is wasm-flavored text instead of the previous reflection dumps.
Groundwork for typed throws in the instantiation path: WasmKitError can wrap
a Trap so Trap-throwing sub-operations can participate in a typed
throws(WasmKitError) chain. Declares Sendable on Trap/TrapReason and rewrites
the few reflection-dependent error messages (arrays, instance handles,
backtrace addresses) into explicit formatting so they also compile under
Embedded Swift.
@kateinoigakukun
kateinoigakukun added this pull request to the merge queue Jul 29, 2026
Merged via the queue into main with commit 3d436eb Jul 29, 2026
43 of 47 checks passed
@kateinoigakukun
kateinoigakukun deleted the katei/error-kinds branch July 29, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants