cm

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2024 License: Apache-2.0 Imports: 2 Imported by: 343

Documentation

Overview

Package cm contains types and functions for interfacing with the WebAssembly Component Model.

The types in this package (such as List, Option, Result, and Variant) are designed to match the memory layout of Component Model types as specified in the Canonical ABI.

Index

Constants

View Source
const (
	// ResultOK represents the OK case of a result.
	ResultOK = false

	// ResultErr represents the error case of a result.
	ResultErr = true
)
View Source
const MaxTuple = 16

MaxTuple specifies the maximum number of fields in a Tuple* type, currently Tuple16. See https://github.com/WebAssembly/component-model/issues/373 for more information.

View Source
const ResourceNone = 0

ResourceNone is a sentinel value indicating a null or uninitialized resource. This is a reserved value specified in the Canonical ABI runtime state.

Variables

This section is empty.

Functions

func BoolToU32

func BoolToU32[B ~bool](v B) uint32

BoolToU32 converts a value whose underlying type is bool into a uint32. Used to lower a bool into a Core WebAssembly i32 as specified in the Canonical ABI.

func Case

func Case[T any, V AnyVariant[Tag, Shape, Align], Tag Discriminant, Shape, Align any](v *V, tag Tag) *T

Case returns a non-nil *T if the Variant case is equal to tag, otherwise it returns nil.

func Err

func Err[R AnyResult[Shape, OK, Err], Shape, OK, Err any](err Err) R

Err returns an error result with shape Shape and type OK and Err. Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.

func F32ToU32

func F32ToU32(v float32) uint32

F32ToU32 maps the bits of a float32 into a uint32. Used to lower a float32 into a Core WebAssembly i32 as specified in the Canonical ABI.

func F32ToU64 added in v0.2.1

func F32ToU64(v float32) uint64

F32ToU64 maps the bits of a float32 into a uint64. Used to lower a float32 into a Core WebAssembly i64 when required by the Canonical ABI.

func F64ToU64

func F64ToU64(v float64) uint64

F64ToU64 maps the bits of a float64 into a uint64. Used to lower a float64 into a Core WebAssembly i64 as specified in the Canonical ABI.

func LiftList

func LiftList[L AnyList[T], T any, Data unsafe.Pointer | uintptr | *T, Len AnyInteger](data Data, len Len) L

LiftList lifts Core WebAssembly types into a List.

func LiftString

func LiftString[T ~string, Data unsafe.Pointer | uintptr | *uint8, Len AnyInteger](data Data, len Len) T

LiftString lifts Core WebAssembly types into a [string].

func LowerList

func LowerList[L AnyList[T], T any](list L) (*T, uint32)

LowerList lowers a List into a pair of Core WebAssembly types.

func LowerString

func LowerString[S ~string](s S) (*byte, uint32)

LowerString lowers a string into a pair of Core WebAssembly types.

func New

func New[V AnyVariant[Tag, Shape, Align], Tag Discriminant, Shape, Align any, T any](tag Tag, data T) V

New returns a Variant with tag of type Disc, storage and GC shape of type Shape, aligned to type Align, with a value of type T.

func OK

func OK[R AnyResult[Shape, OK, Err], Shape, OK, Err any](ok OK) R

OK returns an OK result with shape Shape and type OK and Err. Pass Result[OK, OK, Err] or Result[Err, OK, Err] as the first type argument.

func PointerToU32

func PointerToU32[T any](v *T) uint32

PointerToU32 converts a pointer of type *T into a uint32. Used to lower a pointer into a Core WebAssembly i32 as specified in the Canonical ABI.

func PointerToU64

func PointerToU64[T any](v *T) uint64

PointerToU64 converts a pointer of type *T into a uint64. Used to lower a pointer into a Core WebAssembly i64 as specified in the Canonical ABI.

func Reinterpret

func Reinterpret[T, From any](from From) (to T)

Reinterpret reinterprets the bits of type From into type T. Will panic if the size of From is smaller than the size of To.

func U32ToBool

func U32ToBool(v uint32) bool

U32ToBool converts a uint32 into a bool. Used to lift a Core WebAssembly i32 into a bool as specified in the Canonical ABI.

func U32ToF32

func U32ToF32(v uint32) float32

U32ToF32 maps the bits of a uint32 into a float32. Used to lift a Core WebAssembly i32 into a float32 as specified in the Canonical ABI.

func U32ToPointer

func U32ToPointer[T any](v uint32) *T

U32ToPointer converts a uint32 into a pointer of type *T. Used to lift a Core WebAssembly i32 into a pointer as specified in the Canonical ABI.

func U64ToF32 added in v0.2.1

func U64ToF32(v uint64) float32

U64ToF32 maps the bits of a uint64 into a float32. Used to lift a Core WebAssembly i64 into a float32 when required by the Canonical ABI.

func U64ToF64

func U64ToF64(v uint64) float64

U64ToF64 maps the bits of a uint64 into a float64. Used to lift a Core WebAssembly i64 into a float64 as specified in the Canonical ABI.

func U64ToPointer

func U64ToPointer[T any](v uint64) *T

U64ToPointer converts a uint64 into a pointer of type *T. Used to lift a Core WebAssembly i64 into a pointer as specified in the Canonical ABI.

Types

type AnyInteger added in v0.2.3

type AnyInteger interface {
	~int | ~uint | ~uintptr | ~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32 | ~int64 | ~uint64
}

AnyInteger is a type constraint for any integer type.

type AnyList added in v0.2.2

type AnyList[T any] interface {
	~struct {
		// contains filtered or unexported fields
	}
}

AnyList is a type constraint for generic functions that accept any List type.

type AnyResult added in v0.2.2

type AnyResult[Shape, OK, Err any] interface {
	~struct {
		// contains filtered or unexported fields
	}
}

AnyResult is a type constraint for generic functions that accept any Result type.

type AnyVariant added in v0.2.2

type AnyVariant[Tag Discriminant, Shape, Align any] interface {
	~struct {
		// contains filtered or unexported fields
	}
}

AnyVariant is a type constraint for generic functions that accept any Variant type.

type BoolResult

type BoolResult bool

BoolResult represents a result with no OK or error type. False represents the OK case and true represents the error case.

type Discriminant

type Discriminant interface {
	bool | uint8 | uint16 | uint32
}

Discriminant is the set of types that can represent the tag or discriminator of a variant. Use bool for 2-case variant types, result<T>, or option<T> types, uint8 where there are 256 or fewer cases, uint16 for up to 65,536 cases, or uint32 for anything greater.

type HostLayout added in v0.2.1

type HostLayout = structs.HostLayout

HostLayout marks a struct as using host memory layout. See structs.HostLayout in Go 1.23 or later.

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

List represents a Component Model list. The binary representation of list<T> is similar to a Go slice minus the cap field.

func NewList

func NewList[T any, Len AnyInteger](data *T, len Len) List[T]

NewList returns a List[T] from data and len.

func ToList

func ToList[S ~[]T, T any](s S) List[T]

ToList returns a List[T] equivalent to the Go slice s. The underlying slice data is not copied, and the resulting List points at the same array storage as the slice.

func (List) Data

func (l List) Data() *T

Data returns the data pointer for the list.

func (List) Len

func (l List) Len() uintptr

Len returns the length of the list. TODO: should this return an int instead of a uintptr?

func (List) Slice

func (l List) Slice() []T

Slice returns a Go slice representing the List.

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option represents a Component Model option<T> type.

func None

func None[T any]() Option[T]

None returns an Option representing the none case, equivalent to the zero value.

func Some

func Some[T any](v T) Option[T]

Some returns an Option representing the some case.

func (*Option) None

func (o *Option) None() bool

None returns true if o represents the none case.

func (*Option) Some

func (o *Option) Some() *T

Some returns a non-nil *T if o represents the some case, or nil if o represents the none case.

func (Option) Value added in v0.2.2

func (o Option) Value() T

Value returns T if o represents the some case, or the zero value of T if o represents the none case. This does not have a pointer receiver, so it can be chained.

type Rep

type Rep uint32

Rep represents a Component Model resource rep, the core representation type of a resource. It is represented in the Canonical ABI as an 32-bit integer.

type Resource

type Resource uint32

Resource represents an opaque Component Model resource handle. It is represented in the Canonical ABI as an 32-bit integer.

type Result

type Result[Shape, OK, Err any] struct {
	// contains filtered or unexported fields
}

Result represents a result sized to hold the Shape type. The size of the Shape type must be greater than or equal to the size of OK and Err types. For results with two zero-length types, use BoolResult.

func (*Result) Err

func (r *Result) Err() *Err

Err returns a non-nil *Err pointer if r represents the error case. If r represents the OK case, then it returns nil.

func (*Result) IsErr

func (r *Result) IsErr() bool

IsErr returns true if r represents the error case.

func (*Result) IsOK

func (r *Result) IsOK() bool

IsOK returns true if r represents the OK case.

func (*Result) OK

func (r *Result) OK() *OK

OK returns a non-nil *OK pointer if r represents the OK case. If r represents an error, then it returns nil.

func (Result) Result added in v0.3.1

func (r Result) Result() (ok OK, err Err, isErr bool)

Result returns (OK, zero value of Err, false) if r represents the OK case, or (zero value of OK, Err, true) if r represents the error case. This does not have a pointer receiver, so it can be chained.

type Tuple

type Tuple[T0, T1 any] struct {
	F0 T0
	F1 T1
	// contains filtered or unexported fields
}

Tuple represents a Component Model tuple with 2 fields.

type Tuple10

type Tuple10[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	F5 T5
	F6 T6
	F7 T7
	F8 T8
	F9 T9
	// contains filtered or unexported fields
}

Tuple10 represents a Component Model tuple with 10 fields.

type Tuple11

type Tuple11[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	// contains filtered or unexported fields
}

Tuple11 represents a Component Model tuple with 11 fields.

type Tuple12

type Tuple12[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	F11 T11
	// contains filtered or unexported fields
}

Tuple12 represents a Component Model tuple with 12 fields.

type Tuple13

type Tuple13[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	F11 T11
	F12 T12
	// contains filtered or unexported fields
}

Tuple13 represents a Component Model tuple with 13 fields.

type Tuple14

type Tuple14[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	F11 T11
	F12 T12
	F13 T13
	// contains filtered or unexported fields
}

Tuple14 represents a Component Model tuple with 14 fields.

type Tuple15

type Tuple15[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	F11 T11
	F12 T12
	F13 T13
	F14 T14
	// contains filtered or unexported fields
}

Tuple15 represents a Component Model tuple with 15 fields.

type Tuple16

type Tuple16[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any] struct {
	F0  T0
	F1  T1
	F2  T2
	F3  T3
	F4  T4
	F5  T5
	F6  T6
	F7  T7
	F8  T8
	F9  T9
	F10 T10
	F11 T11
	F12 T12
	F13 T13
	F14 T14
	F15 T15
	// contains filtered or unexported fields
}

Tuple16 represents a Component Model tuple with 16 fields.

type Tuple3

type Tuple3[T0, T1, T2 any] struct {
	F0 T0
	F1 T1
	F2 T2
	// contains filtered or unexported fields
}

Tuple3 represents a Component Model tuple with 3 fields.

type Tuple4

type Tuple4[T0, T1, T2, T3 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	// contains filtered or unexported fields
}

Tuple4 represents a Component Model tuple with 4 fields.

type Tuple5

type Tuple5[T0, T1, T2, T3, T4 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	// contains filtered or unexported fields
}

Tuple5 represents a Component Model tuple with 5 fields.

type Tuple6

type Tuple6[T0, T1, T2, T3, T4, T5 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	F5 T5
	// contains filtered or unexported fields
}

Tuple6 represents a Component Model tuple with 6 fields.

type Tuple7

type Tuple7[T0, T1, T2, T3, T4, T5, T6 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	F5 T5
	F6 T6
	// contains filtered or unexported fields
}

Tuple7 represents a Component Model tuple with 7 fields.

type Tuple8

type Tuple8[T0, T1, T2, T3, T4, T5, T6, T7 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	F5 T5
	F6 T6
	F7 T7
	// contains filtered or unexported fields
}

Tuple8 represents a Component Model tuple with 8 fields.

type Tuple9

type Tuple9[T0, T1, T2, T3, T4, T5, T6, T7, T8 any] struct {
	F0 T0
	F1 T1
	F2 T2
	F3 T3
	F4 T4
	F5 T5
	F6 T6
	F7 T7
	F8 T8
	// contains filtered or unexported fields
}

Tuple9 represents a Component Model tuple with 9 fields.

type Variant

type Variant[Tag Discriminant, Shape, Align any] struct {
	// contains filtered or unexported fields
}

Variant represents a loosely-typed Component Model variant. Shape and Align must be non-zero sized types. To create a variant with no associated types, use an enum.

func NewVariant

func NewVariant[Tag Discriminant, Shape, Align any, T any](tag Tag, data T) Variant[Tag, Shape, Align]

NewVariant returns a Variant with tag of type Disc, storage and GC shape of type Shape, aligned to type Align, with a value of type T.

func (*Variant) Tag

func (v *Variant) Tag() Tag

Tag returns the tag (discriminant) of variant v.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL