File: WasmTypes.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (125 lines) | stat: -rw-r--r-- 4,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/// Type of a WebAssembly function.
///
/// > Note:
/// <https://webassembly.github.io/spec/core/syntax/types.html#function-types>
public struct FunctionType: Equatable, Hashable {
    public init(parameters: [ValueType], results: [ValueType] = []) {
        self.parameters = parameters
        self.results = results
    }

    /// The types of the function parameters.
    public let parameters: [ValueType]
    /// The types of the function results.
    public let results: [ValueType]
}

/// Reference types
public enum ReferenceType: UInt8, Equatable, Hashable {
    /// A nullable reference type to a function.
    case funcRef
    /// A nullable external reference type.
    case externRef
}

public enum ValueType: Equatable, Hashable {
    /// 32-bit signed or unsigned integer.
    case i32
    /// 64-bit signed or unsigned integer.
    case i64
    /// 32-bit IEEE 754 floating-point number.
    case f32
    /// 64-bit IEEE 754 floating-point number.
    case f64
    /// 128-bit vector of packed integer or floating-point data.
    case v128
    /// Reference value type.
    case ref(ReferenceType)
}

/// Runtime representation of a WebAssembly function reference.
public typealias FunctionAddress = Int
/// Runtime representation of an external entity reference.
public typealias ExternAddress = Int

@available(*, unavailable, message: "Address-based APIs has been removed; use `Table` instead")
public typealias TableAddress = Int
@available(*, unavailable, message: "Address-based APIs has been removed; use `Memory` instead")
public typealias MemoryAddress = Int
@available(*, unavailable, message: "Address-based APIs has been removed; use `Global` instead")
public typealias GlobalAddress = Int
@available(*, unavailable, message: "Address-based APIs has been removed")
public typealias ElementAddress = Int
@available(*, unavailable, message: "Address-based APIs has been removed")
public typealias DataAddress = Int

public enum Reference: Hashable {
    /// A reference to a function.
    case function(FunctionAddress?)
    /// A reference to an external entity.
    case extern(ExternAddress?)
}

/// Runtime representation of a value.
public enum Value: Hashable {
    /// Value of a 32-bit signed or unsigned integer.
    case i32(UInt32)
    /// Value of a 64-bit signed or unsigned integer.
    case i64(UInt64)
    /// Value of a 32-bit IEEE 754 floating-point number.
    case f32(UInt32)
    /// Value of a 64-bit IEEE 754 floating-point number.
    case f64(UInt64)
    /// Reference value.
    case ref(Reference)
}

extension Value {
    /// Create a new value from a signed 32-bit integer.
    public init(signed value: Int32) {
        self = .i32(UInt32(bitPattern: value))
    }

    /// Create a new value from a signed 64-bit integer.
    public init(signed value: Int64) {
        self = .i64(UInt64(bitPattern: value))
    }

    /// Create a new value from a 32-bit floating-point number.
    public static func fromFloat32(_ value: Float32) -> Value {
        return .f32(value.bitPattern)
    }

    /// Create a new value from a 64-bit floating-point number.
    public static func fromFloat64(_ value: Float64) -> Value {
        return .f64(value.bitPattern)
    }

    /// Returns the value as a 32-bit signed integer.
    /// - Precondition: The value is of type `i32`.
    public var i32: UInt32 {
        guard case let .i32(result) = self else { fatalError() }
        return result
    }

    /// Returns the value as a 64-bit signed integer.
    /// - Precondition: The value is of type `i64`.
    public var i64: UInt64 {
        guard case let .i64(result) = self else { fatalError() }
        return result
    }

    /// Returns the value as a 32-bit floating-point number.
    /// - Precondition: The value is of type `f32`.
    public var f32: UInt32 {
        guard case let .f32(result) = self else { fatalError() }
        return result
    }

    /// Returns the value as a 64-bit floating-point number.
    /// - Precondition: The value is of type `f64`.
    public var f64: UInt64 {
        guard case let .f64(result) = self else { fatalError() }
        return result
    }
}