Skip to content

Commit c9e256a

Browse files
authored
Merge pull request #195 from bytecodealliance/ydnar/wasm-types
cm: change list<T> len field to uintptr
2 parents 8e25846 + 055fc22 commit c9e256a

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88

99
- `wit-bindgen-go generate` now accepts a remote registry reference to pull down a WIT package and generate the Go code. Example: `wit-bindgen-go generate ghcr.io/webassembly/wasi/http:0.2.0`.
1010

11-
### Fixed
11+
### Changed
1212

13+
- `cm.List` now stores list length as a `uintptr`, permitted by the [Go wasm types proposal](https://github.com/golang/go/issues/66984). It was previously a `uint`, which was removed from the list of permitted types. There should be no change in the memory layout in TinyGo `GOARCH=wasm` or Go `GOARCH=wasm32` using 32-bit pointers.
14+
- The helper functions `cm.NewList`, `cm.LiftList`, and `cm.LiftString` now accept any integer type for `len`, defined as `cm.AnyInteger`.
1315
- `cm.Option[T].Value()` method now value receiver (not pointer receiver), so it can be chained.
1416

1517
## [v0.2.2] — 2024-10-03

cm/abi.go

Lines changed: 8 additions & 3 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,7 +24,7 @@ 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

@@ -30,8 +35,8 @@ func LowerList[L AnyList[T], T any](list L) (*T, uint32) {
3035
}
3136

3237
// LiftList lifts Core WebAssembly types into a [List].
33-
func LiftList[L AnyList[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].

cm/list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ type AnyList[T any] interface {
1818
}
1919

2020
// NewList returns a List[T] from data and len.
21-
func NewList[T any](data *T, len uint) List[T] {
21+
func NewList[T any, Len AnyInteger](data *T, len Len) List[T] {
2222
return List[T]{
2323
list: list[T]{
2424
data: data,
25-
len: len,
25+
len: uintptr(len),
2626
},
2727
}
2828
}
@@ -31,7 +31,7 @@ func NewList[T any](data *T, len uint) List[T] {
3131
// The underlying slice data is not copied, and the resulting List points at the
3232
// same array storage as the slice.
3333
func ToList[S ~[]T, T any](s S) List[T] {
34-
return NewList[T](unsafe.SliceData([]T(s)), uint(len(s)))
34+
return NewList[T](unsafe.SliceData([]T(s)), uintptr(len(s)))
3535
}
3636

3737
// list represents the internal representation of a Component Model list.
@@ -40,7 +40,7 @@ func ToList[S ~[]T, T any](s S) List[T] {
4040
type list[T any] struct {
4141
_ HostLayout
4242
data *T
43-
len uint
43+
len uintptr
4444
}
4545

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

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

0 commit comments

Comments
 (0)