/
abi.go
328 lines (294 loc) · 10.2 KB
/
abi.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package wit
import (
"slices"
"strconv"
)
// Align aligns ptr with alignment align.
func Align(ptr, align uintptr) uintptr {
return (ptr + align - 1) &^ (align - 1)
}
// Discriminant returns the smallest WIT integer type that can represent 0...n.
// Used by the [Canonical ABI] for [Variant] types.
//
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#alignment
func Discriminant(n int) Type {
switch {
case n <= 1<<8:
return U8{}
case n <= 1<<16:
return U16{}
}
return U32{}
}
// ABI is the interface implemented by any type that can report its
// [Canonical ABI] [size], [alignment], and [flat] representation.
//
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
// [size]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#size
// [alignment]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#alignment
// [flat]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening
type ABI interface {
Size() uintptr
Align() uintptr
Flat() []Type
}
// Despecialize [despecializes] k if k implements [Despecializer].
// Otherwise, it returns k unmodified.
// See the [canonical ABI documentation] for more information.
//
// [despecializes]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#despecialization
// [canonical ABI documentation]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#despecialization
func Despecialize(k TypeDefKind) TypeDefKind {
if d, ok := k.(interface{ Despecialize() TypeDefKind }); ok {
return d.Despecialize()
}
return k
}
// HasPointer returns whether or not t contains a [Type] with a pointer, e.g. [String] or [List].
func HasPointer(t TypeDefKind) bool {
t = Despecialize(t)
if p, ok := t.(interface{ hasPointer() bool }); ok {
return p.hasPointer()
}
return false
}
// HasResource returns whether or not t contains a resource type, typically an [Own] or [Borrow] handle.
func HasResource(t TypeDefKind) bool {
t = Despecialize(t)
if p, ok := t.(interface{ hasResource() bool }); ok {
return p.hasResource()
}
return false
}
// HasBorrow returns whether or not t contains a [Borrow] type.
func HasBorrow(t TypeDefKind) bool {
t = Despecialize(t)
if p, ok := t.(interface{ hasBorrow() bool }); ok {
return p.hasBorrow()
}
return false
}
// Direction represents the direction a type or function is represented within a component,
// whether it is an importer (consumer), or an exporter (producer). When applied to functions,
// this represents the [Canonical ABI] [lift] and [lower] operations, for lowering into or lifting out of linear memory.
//
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md#canonical-abi
// [lift]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lift
// [lower]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lower
type Direction int
// String implements the Stringer interface.
func (dir Direction) String() string {
switch dir {
case Imported:
return "imported"
case Exported:
return "exported"
default:
return strconv.Itoa(int(dir))
}
}
const (
// Exported represents types and functions imported into a component from the host or another component.
// This corresponds to the the Canonical ABI [lower] operation, lowering Component Model types into linear memory.
// Used for calling functions imported using //go:wasmimport.
//
// [lower]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lower
Imported Direction = 0
// Exported represents types and functions exported from a component to the host or another component.
// This corresponds to the Canonical ABI [lift] operation, lifting Component Model types out of linear memory.
// Used for exporting functions using //go:wasmexport.
//
// [lift]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lift
Exported Direction = 1
)
// ResourceNew returns the implied [resource-new] function for t.
// If t is not a [Resource], this returns nil.
//
// [resource-new]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-resourcenew
func (t *TypeDef) ResourceNew() *Function {
if _, ok := t.Kind.(*Resource); !ok {
return nil
}
return &Function{
Name: "[resource-new]" + t.TypeName(),
Kind: &Static{Type: t},
Params: []Param{{Name: "rep", Type: &TypeDef{Kind: &Borrow{Type: t}}}},
Results: []Param{{Type: t}},
Docs: Docs{Contents: "Creates a new resource handle."},
}
}
// ResourceRep returns the implied [resource-rep] method for t.
// If t is not a [Resource], this returns nil.
//
// [resource-rep]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-resourcerep
func (t *TypeDef) ResourceRep() *Function {
if _, ok := t.Kind.(*Resource); !ok {
return nil
}
return &Function{
Name: "[resource-rep]" + t.TypeName(),
Kind: &Method{Type: t},
Params: []Param{{Name: "self", Type: t}},
Results: []Param{{Type: &TypeDef{Kind: &Borrow{Type: t}}}},
Docs: Docs{Contents: "Returns the underlying resource representation."},
}
}
// ResourceDrop returns the implied [resource-drop] method for t.
// If t is not a [Resource], this returns nil.
//
// [resource-drop]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-resourcedrop
func (t *TypeDef) ResourceDrop() *Function {
if _, ok := t.Kind.(*Resource); !ok {
return nil
}
return &Function{
Name: "[resource-drop]" + t.TypeName(),
Kind: &Method{Type: t},
Params: []Param{{Name: "self", Type: t}},
Docs: Docs{Contents: "Drops a resource handle."},
}
}
// Destructor returns the implied destructor ([dtor]) method for t.
// If t is not a [Resource], this returns nil.
//
// [dtor]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-resourcedrop
func (t *TypeDef) Destructor() *Function {
if _, ok := t.Kind.(*Resource); !ok {
return nil
}
return &Function{
Name: "[dtor]" + t.TypeName(),
Kind: &Method{Type: t},
Params: []Param{{Name: "self", Type: &TypeDef{Kind: &Borrow{Type: t}}}},
Docs: Docs{Contents: "Resource destructor."},
}
}
// PostReturn returns a [post-return] function for f, which is part of the
// Component Model machinery that allows the caller of f to call back into the component to clean up results.
// Returns nil if f has no results, therefore does not require cleanup.
//
// [post-return]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lift
func (f *Function) PostReturn() *Function {
if !f.ReturnsPointer() {
return nil
}
var kind FunctionKind
switch k := f.Kind.(type) {
case *Method:
kind = &Static{Type: k.Type}
case *Constructor:
kind = &Static{Type: k.Type}
case *Static:
kind = &Static{Type: k.Type}
case *Freestanding:
kind = &Freestanding{}
}
params := slices.Clone(f.Results)
if params[0].Name == "" {
params[0].Name = "result"
}
return &Function{
Name: "cabi_post_" + f.Name,
Kind: kind,
Params: params,
Docs: Docs{Contents: "Post-return cleanup function."},
}
}
// ReturnsBorrow reports whether [Function] f returns a [Borrow] handle,
// which is not permitted by the Component Model specification.
func (f *Function) ReturnsBorrow() bool {
for _, r := range f.Results {
if HasBorrow(r.Type) {
return true
}
}
return false
}
// ReturnsPointer reports whether [Function] f returns a value containing a pointer,
// which would require a post-return cleanup function if exported.
func (f *Function) ReturnsPointer() bool {
for _, r := range f.Results {
if HasPointer(r.Type) {
return true
}
}
return false
}
const (
// MaxFlatParams is the maximum number of [flattened parameters] a function can have
// as defined in the Component Model Canonical ABI.
//
// [flattened parameters]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening
MaxFlatParams = 16
// MaxFlatResults is the maximum number of [flattened results] a function can have
// as defined in the Component Model Canonical ABI.
//
// [flattened results]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening
MaxFlatResults = 1
)
// CoreFunction returns a [Core WebAssembly function] of [Function] f.
// Its params and results may be [flattened] according to the Canonical ABI specification.
// The flattening rules vary based on whether the returned function is imported or exported,
// e.g. using go:wasmimport or go:wasmexport.
//
// [Core WebAssembly function]: https://webassembly.github.io/spec/core/syntax/modules.html#syntax-func
// [flattened]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#flattening
func (f *Function) CoreFunction(op Direction) *Function {
if len(f.Params) == 0 && len(f.Results) == 0 {
return f
}
// Clone the function
cf := *f
// Max 16 params
if len(flatParams(f.Params)) > MaxFlatParams {
cf.Params = []Param{compoundParam("param", "params", f.Params)}
}
// Max 1 result
if len(flatParams(f.Results)) > MaxFlatResults {
p := compoundParam("result", "results", f.Results)
if op == Exported {
cf.Results = []Param{p}
} else {
cf.Params = append(cf.Params, p)
cf.Results = nil
}
}
return &cf
}
func flatParams(params []Param) []Type {
flat := make([]Type, 0, len(params))
for _, p := range params {
flat = append(flat, p.Type.Flat()...)
}
return flat
}
// compoundParam returns a single param that represents
// the combined param(s), using a [Pointer].
func compoundParam(singular, plural string, params []Param) Param {
if len(params) == 0 {
panic("BUG: len(params) == 0")
}
name := params[0].Name
var t Type
if len(params) == 1 {
if name == "" {
name = singular
}
t = params[0].Type
} else {
name = plural
r := &Record{}
t = &TypeDef{Kind: r}
for _, p := range params {
r.Fields = append(r.Fields,
Field{
Name: p.Name,
Type: p.Type,
})
}
}
return Param{
Name: name,
Type: &TypeDef{Kind: &Pointer{Type: t}},
}
}