Skip to content

Commit d5f1953

Browse files
ydnardeadprogram
authored andcommitted
internal/{cm,wasi}: regenerate WASI 0.2 bindings with wasm-tools-go v0.3.0
1 parent a0d4ecb commit d5f1953

File tree

70 files changed

+3350
-684
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3350
-684
lines changed

src/internal/cm/abi.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package cm
22

33
import "unsafe"
44

5+
// AnyInteger is a type constraint for any integer type.
6+
type AnyInteger interface {
7+
~int | ~uint | ~uintptr | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64
8+
}
9+
510
// Reinterpret reinterprets the bits of type From into type T.
611
// Will panic if the size of From is smaller than the size of To.
712
func Reinterpret[T, From any](from From) (to T) {
@@ -19,19 +24,19 @@ func LowerString[S ~string](s S) (*byte, uint32) {
1924
}
2025

2126
// LiftString lifts Core WebAssembly types into a [string].
22-
func LiftString[T ~string, Data unsafe.Pointer | uintptr | *uint8, Len uint | uintptr | uint32 | uint64](data Data, len Len) T {
27+
func LiftString[T ~string, Data unsafe.Pointer | uintptr | *uint8, Len AnyInteger](data Data, len Len) T {
2328
return T(unsafe.String((*uint8)(unsafe.Pointer(data)), int(len)))
2429
}
2530

2631
// LowerList lowers a [List] into a pair of Core WebAssembly types.
27-
func LowerList[L ~struct{ list[T] }, T any](list L) (*T, uint32) {
32+
func LowerList[L AnyList[T], T any](list L) (*T, uint32) {
2833
l := (*List[T])(unsafe.Pointer(&list))
2934
return l.data, uint32(l.len)
3035
}
3136

3237
// LiftList lifts Core WebAssembly types into a [List].
33-
func LiftList[L List[T], T any, Data unsafe.Pointer | uintptr | *T, Len uint | uintptr | uint32 | uint64](data Data, len Len) L {
34-
return L(NewList((*T)(unsafe.Pointer(data)), uint(len)))
38+
func LiftList[L AnyList[T], T any, Data unsafe.Pointer | uintptr | *T, Len AnyInteger](data Data, len Len) L {
39+
return L(NewList((*T)(unsafe.Pointer(data)), len))
3540
}
3641

3742
// BoolToU32 converts a value whose underlying type is [bool] into a [uint32].
@@ -84,6 +89,25 @@ func F64ToU64(v float64) uint64 { return *(*uint64)(unsafe.Pointer(&v)) }
8489
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
8590
func U64ToF64(v uint64) float64 { return *(*float64)(unsafe.Pointer(&v)) }
8691

92+
// F32ToU64 maps the bits of a [float32] into a [uint64].
93+
// Used to lower a [float32] into a Core WebAssembly i64 when required by the [Canonical ABI].
94+
//
95+
// [float32]: https://pkg.go.dev/builtin#float32
96+
// [uint64]: https://pkg.go.dev/builtin#uint64
97+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
98+
func F32ToU64(v float32) uint64 { return uint64(*(*uint32)(unsafe.Pointer(&v))) }
99+
100+
// U64ToF32 maps the bits of a [uint64] into a [float32].
101+
// Used to lift a Core WebAssembly i64 into a [float32] when required by the [Canonical ABI].
102+
//
103+
// [uint64]: https://pkg.go.dev/builtin#uint64
104+
// [float32]: https://pkg.go.dev/builtin#float32
105+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
106+
func U64ToF32(v uint64) float32 {
107+
truncated := uint32(v)
108+
return *(*float32)(unsafe.Pointer(&truncated))
109+
}
110+
87111
// PointerToU32 converts a pointer of type *T into a [uint32].
88112
// Used to lower a pointer into a Core WebAssembly i32 as specified in the [Canonical ABI].
89113
//

src/internal/cm/hostlayout_go122.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !go1.23
2+
3+
package cm
4+
5+
// HostLayout marks a struct as using host memory layout.
6+
// See [structs.HostLayout] in Go 1.23 or later.
7+
type HostLayout struct {
8+
_ hostLayout // prevent accidental conversion with plain struct{}
9+
}
10+
11+
type hostLayout struct{}

src/internal/cm/hostlayout_go123.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build go1.23
2+
3+
package cm
4+
5+
import "structs"
6+
7+
// HostLayout marks a struct as using host memory layout.
8+
// See [structs.HostLayout] in Go 1.23 or later.
9+
type HostLayout = structs.HostLayout

src/internal/cm/list.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ import "unsafe"
44

55
// List represents a Component Model list.
66
// The binary representation of list<T> is similar to a Go slice minus the cap field.
7-
type List[T any] struct{ list[T] }
7+
type List[T any] struct {
8+
_ HostLayout
9+
list[T]
10+
}
11+
12+
// AnyList is a type constraint for generic functions that accept any [List] type.
13+
type AnyList[T any] interface {
14+
~struct {
15+
_ HostLayout
16+
list[T]
17+
}
18+
}
819

920
// NewList returns a List[T] from data and len.
10-
func NewList[T any](data *T, len uint) List[T] {
21+
func NewList[T any, Len AnyInteger](data *T, len Len) List[T] {
1122
return List[T]{
12-
list[T]{
23+
list: list[T]{
1324
data: data,
14-
len: len,
25+
len: uintptr(len),
1526
},
1627
}
1728
}
@@ -20,15 +31,16 @@ func NewList[T any](data *T, len uint) List[T] {
2031
// The underlying slice data is not copied, and the resulting List points at the
2132
// same array storage as the slice.
2233
func ToList[S ~[]T, T any](s S) List[T] {
23-
return NewList[T](unsafe.SliceData([]T(s)), uint(len(s)))
34+
return NewList[T](unsafe.SliceData([]T(s)), uintptr(len(s)))
2435
}
2536

2637
// list represents the internal representation of a Component Model list.
2738
// It is intended to be embedded in a [List], so embedding types maintain
2839
// the methods defined on this type.
2940
type list[T any] struct {
41+
_ HostLayout
3042
data *T
31-
len uint
43+
len uintptr
3244
}
3345

3446
// Slice returns a Go slice representing the List.
@@ -42,7 +54,7 @@ func (l list[T]) Data() *T {
4254
}
4355

4456
// Len returns the length of the list.
45-
// TODO: should this return an int instead of a uint?
46-
func (l list[T]) Len() uint {
57+
// TODO: should this return an int instead of a uintptr?
58+
func (l list[T]) Len() uintptr {
4759
return l.len
4860
}

src/internal/cm/option.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package cm
33
// Option represents a Component Model [option<T>] type.
44
//
55
// [option<T>]: https://component-model.bytecodealliance.org/design/wit.html#options
6-
type Option[T any] struct{ option[T] }
6+
type Option[T any] struct {
7+
_ HostLayout
8+
option[T]
9+
}
710

811
// None returns an [Option] representing the none case,
912
// equivalent to the zero value.
@@ -14,7 +17,7 @@ func None[T any]() Option[T] {
1417
// Some returns an [Option] representing the some case.
1518
func Some[T any](v T) Option[T] {
1619
return Option[T]{
17-
option[T]{
20+
option: option[T]{
1821
isSome: true,
1922
some: v,
2023
},
@@ -25,6 +28,7 @@ func Some[T any](v T) Option[T] {
2528
// The first byte is a bool representing none or some,
2629
// followed by storage for the associated type T.
2730
type option[T any] struct {
31+
_ HostLayout
2832
isSome bool
2933
some T
3034
}
@@ -42,3 +46,14 @@ func (o *option[T]) Some() *T {
4246
}
4347
return nil
4448
}
49+
50+
// Value returns T if o represents the some case,
51+
// or the zero value of T if o represents the none case.
52+
// This does not have a pointer receiver, so it can be chained.
53+
func (o option[T]) Value() T {
54+
if !o.isSome {
55+
var zero T
56+
return zero
57+
}
58+
return o.some
59+
}

src/internal/cm/result.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@ type BoolResult bool
1717
// Result represents a result sized to hold the Shape type.
1818
// The size of the Shape type must be greater than or equal to the size of OK and Err types.
1919
// For results with two zero-length types, use [BoolResult].
20-
type Result[Shape, OK, Err any] struct{ result[Shape, OK, Err] }
20+
type Result[Shape, OK, Err any] struct {
21+
_ HostLayout
22+
result[Shape, OK, Err]
23+
}
24+
25+
// AnyResult is a type constraint for generic functions that accept any [Result] type.
26+
type AnyResult[Shape, OK, Err any] interface {
27+
~struct {
28+
_ HostLayout
29+
result[Shape, OK, Err]
30+
}
31+
}
2132

2233
// result represents the internal representation of a Component Model result type.
2334
type result[Shape, OK, Err any] struct {
35+
_ HostLayout
2436
isErr bool
2537
_ [0]OK
2638
_ [0]Err
@@ -88,8 +100,8 @@ func (r *result[Shape, OK, Err]) validate() {
88100

89101
// OK returns an OK result with shape Shape and type OK and Err.
90102
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
91-
func OK[R ~struct{ result[Shape, OK, Err] }, Shape, OK, Err any](ok OK) R {
92-
var r struct{ result[Shape, OK, Err] }
103+
func OK[R AnyResult[Shape, OK, Err], Shape, OK, Err any](ok OK) R {
104+
var r Result[Shape, OK, Err]
93105
r.validate()
94106
r.isErr = ResultOK
95107
*((*OK)(unsafe.Pointer(&r.data))) = ok
@@ -98,8 +110,8 @@ func OK[R ~struct{ result[Shape, OK, Err] }, Shape, OK, Err any](ok OK) R {
98110

99111
// Err returns an error result with shape Shape and type OK and Err.
100112
// Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.
101-
func Err[R ~struct{ result[Shape, OK, Err] }, Shape, OK, Err any](err Err) R {
102-
var r struct{ result[Shape, OK, Err] }
113+
func Err[R AnyResult[Shape, OK, Err], Shape, OK, Err any](err Err) R {
114+
var r Result[Shape, OK, Err]
103115
r.validate()
104116
r.isErr = ResultErr
105117
*((*Err)(unsafe.Pointer(&r.data))) = err

src/internal/cm/tuple.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cm
44
//
55
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
66
type Tuple[T0, T1 any] struct {
7+
_ HostLayout
78
F0 T0
89
F1 T1
910
}
@@ -12,6 +13,7 @@ type Tuple[T0, T1 any] struct {
1213
//
1314
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
1415
type Tuple3[T0, T1, T2 any] struct {
16+
_ HostLayout
1517
F0 T0
1618
F1 T1
1719
F2 T2
@@ -21,6 +23,7 @@ type Tuple3[T0, T1, T2 any] struct {
2123
//
2224
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
2325
type Tuple4[T0, T1, T2, T3 any] struct {
26+
_ HostLayout
2427
F0 T0
2528
F1 T1
2629
F2 T2
@@ -31,6 +34,7 @@ type Tuple4[T0, T1, T2, T3 any] struct {
3134
//
3235
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
3336
type Tuple5[T0, T1, T2, T3, T4 any] struct {
37+
_ HostLayout
3438
F0 T0
3539
F1 T1
3640
F2 T2
@@ -42,6 +46,7 @@ type Tuple5[T0, T1, T2, T3, T4 any] struct {
4246
//
4347
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
4448
type Tuple6[T0, T1, T2, T3, T4, T5 any] struct {
49+
_ HostLayout
4550
F0 T0
4651
F1 T1
4752
F2 T2
@@ -54,6 +59,7 @@ type Tuple6[T0, T1, T2, T3, T4, T5 any] struct {
5459
//
5560
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
5661
type Tuple7[T0, T1, T2, T3, T4, T5, T6 any] struct {
62+
_ HostLayout
5763
F0 T0
5864
F1 T1
5965
F2 T2
@@ -67,6 +73,7 @@ type Tuple7[T0, T1, T2, T3, T4, T5, T6 any] struct {
6773
//
6874
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
6975
type Tuple8[T0, T1, T2, T3, T4, T5, T6, T7 any] struct {
76+
_ HostLayout
7077
F0 T0
7178
F1 T1
7279
F2 T2
@@ -81,6 +88,7 @@ type Tuple8[T0, T1, T2, T3, T4, T5, T6, T7 any] struct {
8188
//
8289
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
8390
type Tuple9[T0, T1, T2, T3, T4, T5, T6, T7, T8 any] struct {
91+
_ HostLayout
8492
F0 T0
8593
F1 T1
8694
F2 T2
@@ -96,6 +104,7 @@ type Tuple9[T0, T1, T2, T3, T4, T5, T6, T7, T8 any] struct {
96104
//
97105
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
98106
type Tuple10[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 any] struct {
107+
_ HostLayout
99108
F0 T0
100109
F1 T1
101110
F2 T2
@@ -112,6 +121,7 @@ type Tuple10[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 any] struct {
112121
//
113122
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
114123
type Tuple11[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any] struct {
124+
_ HostLayout
115125
F0 T0
116126
F1 T1
117127
F2 T2
@@ -129,6 +139,7 @@ type Tuple11[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any] struct {
129139
//
130140
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
131141
type Tuple12[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any] struct {
142+
_ HostLayout
132143
F0 T0
133144
F1 T1
134145
F2 T2
@@ -147,6 +158,7 @@ type Tuple12[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any] struct {
147158
//
148159
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
149160
type Tuple13[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any] struct {
161+
_ HostLayout
150162
F0 T0
151163
F1 T1
152164
F2 T2
@@ -166,6 +178,7 @@ type Tuple13[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any] struct {
166178
//
167179
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
168180
type Tuple14[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any] struct {
181+
_ HostLayout
169182
F0 T0
170183
F1 T1
171184
F2 T2
@@ -186,6 +199,7 @@ type Tuple14[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any] str
186199
//
187200
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
188201
type Tuple15[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any] struct {
202+
_ HostLayout
189203
F0 T0
190204
F1 T1
191205
F2 T2
@@ -207,6 +221,7 @@ type Tuple15[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any
207221
//
208222
// [Component Model tuple]: https://component-model.bytecodealliance.org/design/wit.html#tuples
209223
type Tuple16[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any] struct {
224+
_ HostLayout
210225
F0 T0
211226
F1 T1
212227
F2 T2

src/internal/cm/variant.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ type Discriminant interface {
1212
// Variant represents a loosely-typed Component Model variant.
1313
// Shape and Align must be non-zero sized types. To create a variant with no associated
1414
// types, use an enum.
15-
type Variant[Tag Discriminant, Shape, Align any] struct{ variant[Tag, Shape, Align] }
15+
type Variant[Tag Discriminant, Shape, Align any] struct {
16+
_ HostLayout
17+
variant[Tag, Shape, Align]
18+
}
19+
20+
// AnyVariant is a type constraint for generic functions that accept any [Variant] type.
21+
type AnyVariant[Tag Discriminant, Shape, Align any] interface {
22+
~struct {
23+
_ HostLayout
24+
variant[Tag, Shape, Align]
25+
}
26+
}
1627

1728
// NewVariant returns a [Variant] with tag of type Disc, storage and GC shape of type Shape,
1829
// aligned to type Align, with a value of type T.
@@ -26,7 +37,7 @@ func NewVariant[Tag Discriminant, Shape, Align any, T any](tag Tag, data T) Vari
2637

2738
// New returns a [Variant] with tag of type Disc, storage and GC shape of type Shape,
2839
// aligned to type Align, with a value of type T.
29-
func New[V ~struct{ variant[Tag, Shape, Align] }, Tag Discriminant, Shape, Align any, T any](tag Tag, data T) V {
40+
func New[V AnyVariant[Tag, Shape, Align], Tag Discriminant, Shape, Align any, T any](tag Tag, data T) V {
3041
validateVariant[Tag, Shape, Align, T]()
3142
var v variant[Tag, Shape, Align]
3243
v.tag = tag
@@ -35,7 +46,7 @@ func New[V ~struct{ variant[Tag, Shape, Align] }, Tag Discriminant, Shape, Align
3546
}
3647

3748
// Case returns a non-nil *T if the [Variant] case is equal to tag, otherwise it returns nil.
38-
func Case[T any, V ~struct{ variant[Tag, Shape, Align] }, Tag Discriminant, Shape, Align any](v *V, tag Tag) *T {
49+
func Case[T any, V AnyVariant[Tag, Shape, Align], Tag Discriminant, Shape, Align any](v *V, tag Tag) *T {
3950
validateVariant[Tag, Shape, Align, T]()
4051
v2 := (*variant[Tag, Shape, Align])(unsafe.Pointer(v))
4152
if v2.tag == tag {
@@ -47,6 +58,7 @@ func Case[T any, V ~struct{ variant[Tag, Shape, Align] }, Tag Discriminant, Shap
4758
// variant is the internal representation of a Component Model variant.
4859
// Shape and Align must be non-zero sized types.
4960
type variant[Tag Discriminant, Shape, Align any] struct {
61+
_ HostLayout
5062
tag Tag
5163
_ [0]Align
5264
data Shape // [unsafe.Sizeof(*(*Shape)(unsafe.Pointer(nil)))]byte

0 commit comments

Comments
 (0)