Skip to content

Commit

Permalink
Merge pull request #75 from swiftwasm/katei/documentation
Browse files Browse the repository at this point in the history
Documentation and reduce number of public APIs
  • Loading branch information
kateinoigakukun authored Apr 6, 2024
2 parents e0dd2b4 + cd4b320 commit e331658
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 45 deletions.
2 changes: 0 additions & 2 deletions Sources/Spectest/TestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,6 @@ extension Swift.Error {
return "data count and data section have inconsistent lengths"
case .expectedRefType:
return "malformed reference type"
case .unexpectedContent:
return "unexpected content after last section"
case .sectionSizeMismatch:
return "section size mismatch"
case .illegalOpcode:
Expand Down
3 changes: 3 additions & 0 deletions Sources/WasmKit/Component/ComponentTypes.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/// Error type raised from Component Model operations
public struct ComponentError<Content>: Error {
/// The content of the error
public let content: Content

/// Initialize a new error with the given content
public init(_ content: Content) {
self.content = content
}
Expand Down
5 changes: 0 additions & 5 deletions Sources/WasmKit/Docs.docc/Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,3 @@ _ = try runtime.invoke(instance, function: "_start")
- ``CanonicalOptions``
- ``CanonicalCallContext``
- ``ComponentError``

### Hook Runtime

- ``RuntimeInterceptor``
- ``GuestTimeProfiler``
41 changes: 41 additions & 0 deletions Sources/WasmKit/Execution/Runtime/GuestMemory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public struct GuestMemory {
private let store: Store
private let address: MemoryAddress

/// Creates a new memory instance from the given store and address
public init(store: Store, address: MemoryAddress) {
self.store = store
self.address = address
Expand Down Expand Up @@ -47,24 +48,29 @@ extension GuestPrimitivePointee {
}
}

/// Auto implementation of ``GuestPointee`` for ``RawRepresentable`` types
extension GuestPrimitivePointee where Self: RawRepresentable, Self.RawValue: GuestPointee {
/// Reads a value of RawValue type and constructs a value of Self type
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> Self {
Self(rawValue: .readFromGuest(pointer))!
}

/// Writes the raw value of the given value to the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: Self) {
Self.RawValue.writeToGuest(at: pointer, value: value.rawValue)
}
}

extension UInt8: GuestPrimitivePointee {
/// Reads a value of `UInt8` type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UInt8 {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt8.self)
return pointer.pointee
}
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UInt8) {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt8.self)
Expand All @@ -74,6 +80,7 @@ extension UInt8: GuestPrimitivePointee {
}

extension UInt16: GuestPrimitivePointee {
/// Reads a value of `UInt16` type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UInt16 {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt16.self)
Expand All @@ -86,6 +93,7 @@ extension UInt16: GuestPrimitivePointee {
}
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UInt16) {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt16.self)
Expand All @@ -101,6 +109,7 @@ extension UInt16: GuestPrimitivePointee {
}

extension UInt32: GuestPrimitivePointee {
/// Reads a value of `UInt32` type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UInt32 {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt32.self)
Expand All @@ -113,6 +122,7 @@ extension UInt32: GuestPrimitivePointee {
}
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UInt32) {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt32.self)
Expand All @@ -128,6 +138,7 @@ extension UInt32: GuestPrimitivePointee {
}

extension UInt64: GuestPrimitivePointee {
/// Reads a value of `UInt64` type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UInt64 {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt64.self)
Expand All @@ -140,6 +151,7 @@ extension UInt64: GuestPrimitivePointee {
}
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UInt64) {
pointer.withHostPointer { hostPointer in
let pointer = hostPointer.assumingMemoryBound(to: UInt64.self)
Expand All @@ -162,6 +174,7 @@ public struct UnsafeGuestRawPointer {
/// An offset from the base address of the guest memory region
public let offset: UInt32

/// Creates a new pointer from the given memory space and offset
public init(memorySpace: GuestMemory, offset: UInt32) {
self.memorySpace = memorySpace
self.offset = offset
Expand Down Expand Up @@ -193,29 +206,39 @@ public struct UnsafeGuestRawPointer {
}

extension UnsafeGuestRawPointer: GuestPointee {
/// Returns the size of this type in bytes in guest memory
public static var sizeInGuest: UInt32 {
return UInt32(MemoryLayout<UInt32>.size)
}

/// Returns the required alignment of this type, in bytes
public static var alignInGuest: UInt32 {
return UInt32(MemoryLayout<UInt32>.alignment)
}

/// Reads a value of self type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UnsafeGuestRawPointer {
UnsafeGuestRawPointer(memorySpace: pointer.memorySpace, offset: UInt32.readFromGuest(pointer))
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UnsafeGuestRawPointer) {
UInt32.writeToGuest(at: pointer, value: value.offset)
}
}

extension UnsafeGuestRawPointer {
/// Returns a boolean value indicating whether the first pointer references a guest
/// memory location earlier than the second pointer assuming they point the same guest
/// memory space.
public static func < (lhs: UnsafeGuestRawPointer, rhs: UnsafeGuestRawPointer) -> Bool {
// Assuming they point the same guest memory space
lhs.offset < rhs.offset
}

/// Returns a boolean value indicating whether the first pointer references a guest
/// memory location later than the second pointer assuming they point the same guest
/// memory space.
public static func > (lhs: UnsafeGuestRawPointer, rhs: UnsafeGuestRawPointer) -> Bool {
// Assuming they point the same guest memory space
lhs.offset > rhs.offset
Expand All @@ -224,12 +247,15 @@ extension UnsafeGuestRawPointer {

/// A pointee-bound pointer representation of guest memory space
public struct UnsafeGuestPointer<Pointee: GuestPointee> {
/// A raw pointer representation of guest memory space
public let raw: UnsafeGuestRawPointer

/// Creates a new pointer from the given raw pointer
public init(_ raw: UnsafeGuestRawPointer) {
self.raw = raw
}

/// Creates a new pointer from the given memory space and offset
public init(memorySpace: GuestMemory, offset: UInt32) {
self.raw = UnsafeGuestRawPointer(memorySpace: memorySpace, offset: offset)
}
Expand All @@ -249,47 +275,62 @@ public struct UnsafeGuestPointer<Pointee: GuestPointee> {
}

extension UnsafeGuestPointer: GuestPointee {
/// Returns the size of this type in bytes in guest memory
public static var sizeInGuest: UInt32 {
UnsafeGuestRawPointer.sizeInGuest
}

/// Returns the required alignment of this type, in bytes
public static var alignInGuest: UInt32 {
UnsafeGuestRawPointer.alignInGuest
}

/// Reads a value of self type from the given pointer of guest memory
public static func readFromGuest(_ pointer: UnsafeGuestRawPointer) -> UnsafeGuestPointer<Pointee> {
UnsafeGuestPointer(UnsafeGuestRawPointer.readFromGuest(pointer))
}

/// Writes the given value at the given pointer of guest memory
public static func writeToGuest(at pointer: UnsafeGuestRawPointer, value: UnsafeGuestPointer<Pointee>) {
UnsafeGuestRawPointer.writeToGuest(at: pointer, value: value.raw)
}
}

extension UnsafeGuestPointer {
/// Returns a new pointer offset from this pointer by the specified number of instances.
public static func + (lhs: UnsafeGuestPointer, rhs: UInt32) -> UnsafeGuestPointer {
let advanced = lhs.raw.advanced(by: Pointee.sizeInGuest * rhs)
return UnsafeGuestPointer(advanced)
}

/// Returns a new pointer offset from this pointer by the specified number of instances.
public static func += (lhs: inout Self, rhs: UInt32) {
lhs = lhs + rhs
}

/// Returns a boolean value indicating whether the first pointer references a guest
/// memory location earlier than the second pointer assuming they point the same guest
/// memory space.
public static func < (lhs: UnsafeGuestPointer, rhs: UnsafeGuestPointer) -> Bool {
lhs.raw < rhs.raw
}

/// Returns a boolean value indicating whether the first pointer references a guest
/// memory location later than the second pointer assuming they point the same guest
/// memory space.
public static func > (lhs: UnsafeGuestPointer, rhs: UnsafeGuestPointer) -> Bool {
lhs.raw > rhs.raw
}
}

/// A pointee-bound interface to a buffer of elements stored contiguously in guest memory
public struct UnsafeGuestBufferPointer<Pointee: GuestPointee> {
/// A pointer to the first element of the buffer
public let baseAddress: UnsafeGuestPointer<Pointee>
/// The number of elements in the buffer
public let count: UInt32

/// Creates a new buffer from the given base address and count
public init(baseAddress: UnsafeGuestPointer<Pointee>, count: UInt32) {
self.baseAddress = baseAddress
self.count = count
Expand Down
2 changes: 2 additions & 0 deletions Sources/WasmKit/Execution/Runtime/Profiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import SystemExtras
import SystemPackage

/// A simple time-profiler for guest process to emit `chrome://tracing` format
/// This profiler works only when WasmKit is built with debug configuration (`swift build -c debug`)
@_documentation(visibility: internal)
public class GuestTimeProfiler: RuntimeInterceptor {
struct Event: Codable {
enum Phase: String, Codable {
Expand Down
1 change: 1 addition & 0 deletions Sources/WasmKit/Execution/Runtime/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class Runtime {
}
}

@_documentation(visibility: internal)
public protocol RuntimeInterceptor {
func onEnterFunction(_ address: FunctionAddress, store: Store)
func onExitFunction(_ address: FunctionAddress, store: Store)
Expand Down
11 changes: 5 additions & 6 deletions Sources/WasmKit/Execution/Runtime/Stack.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#stack>

public struct Stack {
public enum Element: Equatable {
struct Stack {
enum Element: Equatable {
case value(Value)
case label(Label)
case frame(Frame)
Expand Down Expand Up @@ -296,7 +295,7 @@ struct BaseStackAddress {

/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#frames>
public struct Frame {
struct Frame {
let arity: Int
let module: ModuleAddress
let baseStackAddress: BaseStackAddress
Expand All @@ -323,7 +322,7 @@ public struct Frame {
}

extension Frame: Equatable {
public static func == (_ lhs: Frame, _ rhs: Frame) -> Bool {
static func == (_ lhs: Frame, _ rhs: Frame) -> Bool {
lhs.module == rhs.module && lhs.arity == rhs.arity
}
}
Expand All @@ -341,7 +340,7 @@ extension Stack {
}

extension Frame: CustomDebugStringConvertible {
public var debugDescription: String {
var debugDescription: String {
"[A=\(arity), BA=\(baseStackAddress), F=\(address?.description ?? "nil")]"
}
}
Expand Down
23 changes: 15 additions & 8 deletions Sources/WasmKit/Execution/Types/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,40 @@ public enum Trap: Error {
case unreachable

// Stack
/// Stack overflow
case stackOverflow
case stackTypeMismatch(expected: Any.Type, actual: Stack.Element?)
/// The stack value type does not match the expected type
case stackValueTypesMismatch(expected: ValueType, actual: ValueType)
case stackElementNotFound(Any.Type, index: Int)
case localIndexOutOfRange(index: UInt32)
/// Too deep call stack
case callStackExhausted

// Store
case globalAddressOutOfRange(index: GlobalAddress)
case globalImmutable(index: GlobalAddress)
/// Out of bounds table access
case outOfBoundsTableAccess(index: ElementIndex)
/// Reading a dropped reference
case readingDroppedReference(index: ElementIndex)

// Invocation
/// Exported function not found
case exportedFunctionNotFound(ModuleInstance, name: String)
/// The table element is not initialized
case tableUninitialized(ElementIndex)
/// Undefined element in the table
case undefinedElement
/// Table size overflow
case tableSizeOverflow
/// Indirect call type mismatch
case callIndirectFunctionTypeMismatch(actual: FunctionType, expected: FunctionType)
/// Out of bounds memory access
case outOfBoundsMemoryAccess
/// Invalid function index
case invalidFunctionIndex(FunctionIndex)
case poppedLabelMismatch
case labelMismatch
/// Integer divided by zero
case integerDividedByZero
/// Integer overflowed during arithmetic operation
case integerOverflowed
/// Invalid conversion to integer
case invalidConversionToInteger
case tooManyBlockParameters([ValueType])

/// Human-readable text representation of the trap that `.wast` text format expects in assertions
public var assertionText: String {
Expand Down
7 changes: 4 additions & 3 deletions Sources/WasmKit/Execution/Types/Instances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public struct MemoryInstance {
}
}

/// Instance of a global
/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
public struct GlobalInstance {
Expand All @@ -160,7 +161,7 @@ public struct GlobalInstance {

/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#element-instances>
public struct ElementInstance {
struct ElementInstance {
public let type: ReferenceType
public var references: [Reference]

Expand All @@ -171,9 +172,9 @@ public struct ElementInstance {

/// > Note:
/// <https://webassembly.github.io/spec/core/exec/runtime.html#syntax-datainst>
public struct DataInstance {
struct DataInstance {
/// Bytes stored in this data instance.
public let data: [UInt8]
let data: [UInt8]
}

/// > Note:
Expand Down
Loading

0 comments on commit e331658

Please sign in to comment.