/
types-example.wit
194 lines (163 loc) · 4.96 KB
/
types-example.wit
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package types-example-namespace:types-example-pkg
interface types-interface {
/// "package of named fields"
record r {
a: u32,
b: string,
c: list<tuple<string, option<t4>>>,
d: option<option<tuple<list<s64>, option<option<u64>>>>>,
e: errno,
i: input,
p: permissions,
f: many-flags,
}
/// values of this type will be one of the specified cases
variant human {
baby,
/// type payload
child(u32), // optional type payload
adult,
}
/// similar to `variant`, but no type payloads
enum errno {
too-big,
too-small,
too-fast,
too-slow,
}
/// similar to `variant`, but doesn't require naming cases and all variants
/// have a type payload -- note that this is not a C union, it still has a
/// discriminant
union input {
u64,
string,
}
/// a bitflags type
flags permissions {
read,
write,
exec,
}
flags many-flags {
f1, f2, f3, f4, f5, f6, f7, f8, f9, f10,
f11, f12, f13, f14, f15, f16, f17, f18, f19, f20,
f21, f22, f23, f24, f25, f26, f27, f28, f29, f30,
f31, f32, f33,
}
// type aliases are allowed to primitive types and additionally here are some
// examples of other types
type t1 = u32
type t2 = tuple<u32, u64>
type t3 = string
type t4 = option<u32>
/// no "ok" type
type t5 = result<_, errno> // no "ok" type
type t6 = result<string> // no "err" type
type t7 = result<char, errno> // both types specified
type t8 = result // no "ok" or "err" type
type t9 = list<string>
type t10 = t9
}
interface round-trip-numbers {
record round-trip-numbers-data {
un8: u8,
un16: u16,
un32: u32,
un64: u64,
si8: s8,
si16: s16,
si32: s32,
si64: s64,
f32: float32,
f64: float64,
}
record round-trip-numbers-list-data {
un8: list<u8>,
un16: list<u16>,
un32: list<u32>,
un64: list<u64>,
si8: list<s8>,
si16: list<s16>,
si32: list<s32>,
si64: list<s64>,
si64-list: list<list<s64>>,
un64-list: list<list<u64>>,
un8-list: list<list<u8>>,
f32: list<float32>,
f64: list<float64>,
}
round-trip-numbers: func(data: round-trip-numbers-data) -> round-trip-numbers-data
round-trip-numbers-list: func(data: round-trip-numbers-list-data) -> round-trip-numbers-list-data
}
/// Comment for import interface
interface api-imports {
use types-interface.{t7, r, permissions, input, errno}
/// Same name as the type in `types-interface`, but this is a different type
variant human {
baby,
child(u64),
adult(tuple<string, option<option<string>>, tuple<s64>>),
}
api-a1-b2: func(arg: list<human>) -> (h1: t7, val2: human)
/// Function with a record, enum, flags and union types
record-func: func(r: r, e: errno, p: permissions, i: input) -> (r: r, e: errno, p: permissions, i: input)
}
interface api {
use types-interface.{r, permissions, input, errno as errno-renamed}
/// Comment for export function
f12: func() -> (val-one: tuple<s32>, val2: string)
/// Comment for t5 in api
type t5 = result<_, option<errno>>
record errno {
a-u1: u64,
/// A list of signed 64-bit integers
list-s1: list<s64>,
str: option<string>,
c: option<char>,
}
class: func(break: option<option<t5>>) -> tuple<>
continue: func(abstract: option<result<_, errno>>, extends: tuple<>) -> (%implements: option<tuple<>>)
/// Function with a record, enum, flags and union types
record-func: func(r: r, e: errno-renamed, p: permissions, i: input) -> (r: r, e: errno-renamed, p: permissions, i: input)
resource r1 {
/// constructor for r1
constructor(name: string)
/// Comment for f2
length: func() -> u32
name: func() -> string
static-default: static func() -> string
/// Comment for static f1
static-f1: static func(a: r1) -> string
merge: static func(lhs: borrow<r1>, rhs: borrow<r1>) -> r1
}
static-f1: func(a: r1) -> string
merge: func(lhs: borrow<r1>, rhs: borrow<r1>) -> r1
}
use api-imports as imports
use round-trip-numbers as round-trip-numbers-host
world types-example {
use types-interface.{t2 as t2-renamed, t10, permissions}
import imports
import print: func(message: string, level: log-level)
/// Comment for import inline
import inline: interface {
/// Comment for import inline function
inline-imp: func(args: list<option<char>>) -> result<_, char>
}
import round-trip-numbers-host
export round-trip-numbers
export api
enum log-level {
/// lowest level
debug,
info,
warn,
error,
}
record empty {}
export f-f1: func(typedef: t10) -> t10
export f1: func(f: float32, f-list: list<tuple<char, float64>>) -> (val-p1: s64, val2: string)
/// t2 has been renamed with `use types-interface.{t2 as t2-renamed}`
export re-named: func(perm: option<permissions>, e: option<empty>) -> t2-renamed
export re-named2: func(tup: tuple<list<u16>>, e: empty) -> tuple<option<u8>, s8>
}