wit

package
v0.0.0-...-9d94586 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Overview

Package wit contains a Go representation of the WIT (WebAssembly Interface Type) specification as defined in the WebAssembly Component Model.

Structure

A fully-decoded Resolve struct is a cyclical data structure containing World, Interface, and TypeDef values that reference each other. Tools to decode polymorphic, cyclical JSON are implemented in the codec package. Types defined in this package largely map 1:1 to their equivalent types in the wit-parser crate (source), accommodating differences between the Go and Rust type systems.

Types that are represented as Rust enums are implemented via sealed Go interfaces, implemented by other types in this package. An example is WorldItem, which is the set of types that a World may import or export, currently Interface, TypeDef, and Function.

JSON

DecodeJSON decodes fully-resolved WIT descriptions in JSON format generated by wasm-tools into a Resolve:

f, err := os.Open("wasi-clocks.wit.json")
if err != nil {
	return err
}
defer f.Close()

res, err := wit.DecodeJSON(f)
if err != nil {
	return err
}

// Do something with res

Index

Constants

View Source
const (
	// Lift represents the Canonical ABI [lift] direction, lifting Component Model types out of linear memory.
	// Used for exporting functions to the WebAssembly host (or another component) using //go:wasmexport.
	//
	// [lift]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lift
	Lift Op = 0

	// Lower represents the Canonical ABI [lower] operation, lowering Component Model types into linear memory.
	// Used for calling functions imported from the WebAssembly host (or another component) using //go:wasmimport.
	//
	// [lower]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#canon-lower
	Lower Op = 1

	// 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
)
View Source
const (
	DocPrefix  = "///"
	LineLength = 80
)

Variables

This section is empty.

Functions

func Align

func Align(ptr, align uintptr) uintptr

Align aligns ptr with alignment align.

func HasBorrow

func HasBorrow(t TypeDefKind) bool

HasBorrow returns whether or not t contains a Borrow type.

func HasPointer

func HasPointer(t TypeDefKind) bool

HasPointer returns whether or not t contains a Type with a pointer, e.g. String or List.

func KindOf

func KindOf[K TypeDefKind](t Type) (kind K)

KindOf probes Type t to determine if it is a TypeDef with TypeDefKind K. It returns the underlying Kind if present.

Types

type ABI

type ABI interface {
	Size() uintptr
	Align() uintptr
	Flat() []Type
}

ABI is the interface implemented by any type that can report its Canonical ABI size, alignment, and flat representation.

type Bool

type Bool struct {
	// contains filtered or unexported fields
}

Bool represents the WIT primitive type bool, a boolean value either true or false. It is equivalent to the Go type bool. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (Bool) Align

func (Bool) Align() uintptr

Align returns the byte alignment for values of this type.

func (Bool) Flat

func (Bool) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (Bool) HasPointer

func (Bool) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (Bool) Size

func (Bool) Size() uintptr

Size returns the byte size for values of this type.

func (Bool) TypeName

func (Bool) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (Bool) WIT

func (p Bool) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (Bool) WITKind

func (Bool) WITKind() string

WITKind returns the WIT kind.

type Borrow

type Borrow struct {
	Type *TypeDef
	// contains filtered or unexported fields
}

Borrow represents a WIT borrowed handle. It implements the Handle, Node, ABI, and TypeDefKind interfaces.

func (Borrow) Align

func (Borrow) Align() uintptr

Align returns the ABI byte alignment for this Handle.

func (Borrow) Flat

func (Borrow) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (*Borrow) HasBorrow

func (b *Borrow) HasBorrow() bool

HasBorrow returns whether t contains a Borrow. This always returns true.

func (Borrow) Size

func (Borrow) Size() uintptr

Size returns the ABI byte size for this Handle.

func (*Borrow) WIT

func (b *Borrow) WIT(ctx Node, name string) string

WIT returns the WIT text format for Borrow h.

func (*Borrow) WITKind

func (*Borrow) WITKind() string

WITKind returns the WIT kind.

type Case

type Case struct {
	Name string
	Type Type // optional associated Type (can be nil)
	Docs Docs
}

Case represents a single case in a Variant. It implements the Node interface.

func (*Case) DecodeField

func (c *Case) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Case) WIT

func (c *Case) WIT(ctx Node, _ string) string

WIT returns the WIT text format for Variant Case c.

func (*Case) WITKind

func (*Case) WITKind() string

WITKind returns the WIT kind.

type Char

type Char struct {
	// contains filtered or unexported fields
}

Char represents the WIT primitive type char, a single Unicode character, specifically a Unicode scalar value. It is equivalent to the Go type rune. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (Char) Align

func (Char) Align() uintptr

Align returns the byte alignment for values of this type.

func (Char) Flat

func (Char) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (Char) HasPointer

func (Char) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (Char) Size

func (Char) Size() uintptr

Size returns the byte size for values of this type.

func (Char) TypeName

func (Char) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (Char) WIT

func (p Char) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (Char) WITKind

func (Char) WITKind() string

WITKind returns the WIT kind.

type Constructor

type Constructor struct {
	Type Type
	// contains filtered or unexported fields
}

Constructor represents a function that is a constructor for its associated Type.

type Docs

type Docs struct {
	Contents string // may be empty
}

Docs represent WIT documentation text extracted from comments.

func (*Docs) DecodeField

func (d *Docs) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Docs) WIT

func (d *Docs) WIT(_ Node, _ string) string

WIT returns the WIT text format for Docs d.

func (*Docs) WITKind

func (*Docs) WITKind() string

WITKind returns the WIT kind.

type Enum

type Enum struct {
	Cases []EnumCase
	// contains filtered or unexported fields
}

Enum represents a WIT enum type, which is a Variant without associated data. The equivalent in Go is a set of const identifiers declared with iota. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Enum) Align

func (e *Enum) Align() uintptr

Align returns the ABI byte alignment for Enum e. It is first despecialized into a Variant with no associated types, then aligned.

func (*Enum) DecodeField

func (e *Enum) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Enum) Despecialize

func (e *Enum) Despecialize() TypeDefKind

Despecialize despecializes Enum e into a Variant with no associated types. See the canonical ABI documentation for more information.

func (*Enum) Flat

func (v *Enum) Flat() []Type

Flat returns the flattened ABI representation of Enum e.

func (*Enum) Size

func (e *Enum) Size() uintptr

Size returns the ABI byte size for Enum e, the smallest integer type that can represent 0...len(e.Cases). It is first despecialized into a Variant with no associated types, then sized.

func (Enum) TypeName

func (Enum) TypeName() string

func (*Enum) WIT

func (e *Enum) WIT(ctx Node, name string) string

WIT returns the WIT text format for Enum e.

func (*Enum) WITKind

func (*Enum) WITKind() string

WITKind returns the WIT kind.

type EnumCase

type EnumCase struct {
	Name string
	Docs Docs
}

EnumCase represents a single case in an Enum. It implements the Node interface.

func (*EnumCase) DecodeField

func (c *EnumCase) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*EnumCase) WIT

func (c *EnumCase) WIT(ctx Node, _ string) string

WIT returns the WIT text format for EnumCase c.

func (*EnumCase) WITKind

func (*EnumCase) WITKind() string

WITKind returns the WIT kind.

type F32

type F32 struct {
	// contains filtered or unexported fields
}

F32 represents the WIT primitive type f32, a 32-bit floating point value. It is equivalent to the Go type float32. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (F32) Align

func (F32) Align() uintptr

Align returns the byte alignment for values of this type.

func (F32) Flat

func (F32) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (F32) HasPointer

func (F32) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (F32) Size

func (F32) Size() uintptr

Size returns the byte size for values of this type.

func (F32) TypeName

func (F32) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (F32) WIT

func (p F32) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (F32) WITKind

func (F32) WITKind() string

WITKind returns the WIT kind.

type F64

type F64 struct {
	// contains filtered or unexported fields
}

F64 represents the WIT primitive type f64, a 64-bit floating point value. It is equivalent to the Go type float64. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (F64) Align

func (F64) Align() uintptr

Align returns the byte alignment for values of this type.

func (F64) Flat

func (F64) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (F64) HasPointer

func (F64) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (F64) Size

func (F64) Size() uintptr

Size returns the byte size for values of this type.

func (F64) TypeName

func (F64) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (F64) WIT

func (p F64) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (F64) WITKind

func (F64) WITKind() string

WITKind returns the WIT kind.

type Field

type Field struct {
	Name string
	Type Type
	Docs Docs
}

Field represents a field in a Record.

func (*Field) DecodeField

func (f *Field) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Field) WIT

func (f *Field) WIT(ctx Node, name string) string

WIT returns the WIT text format for Field f.

func (*Field) WITKind

func (*Field) WITKind() string

WITKind returns the WIT kind.

type Flag

type Flag struct {
	Name string
	Docs Docs
}

Flag represents a single flag value in a Flags type. It implements the Node interface.

func (*Flag) DecodeField

func (f *Flag) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Flag) WIT

func (f *Flag) WIT(ctx Node, _ string) string

WIT returns the WIT text format for Flag f.

func (*Flag) WITKind

func (*Flag) WITKind() string

WITKind returns the WIT kind.

type Flags

type Flags struct {
	Flags []Flag
	// contains filtered or unexported fields
}

Flags represents a WIT flags type, stored as a bitfield. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Flags) Align

func (f *Flags) Align() uintptr

Align returns the ABI byte alignment of Flags f.

func (*Flags) DecodeField

func (f *Flags) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Flags) Flat

func (f *Flags) Flat() []Type

Flat returns the flattened ABI representation of Flags f.

func (*Flags) Size

func (f *Flags) Size() uintptr

Size returns the ABI byte size of Flags f.

func (Flags) TypeName

func (Flags) TypeName() string

func (*Flags) WIT

func (f *Flags) WIT(ctx Node, name string) string

WIT returns the WIT text format for Flags f.

func (*Flags) WITKind

func (*Flags) WITKind() string

WITKind returns the WIT kind.

type Freestanding

type Freestanding struct {
	// contains filtered or unexported fields
}

Freestanding represents a free-standing function that is not a method, static, or a constructor.

type Function

type Function struct {
	Name    string
	Kind    FunctionKind
	Params  []Param // arguments to the function
	Results []Param // a function can have a single anonymous result, or > 1 named results
	Docs    Docs
	// contains filtered or unexported fields
}

Function represents a WIT function. Functions can be freestanding, methods, constructors or static. It implements the Node and WorldItem interfaces.

func (*Function) BaseName

func (f *Function) BaseName() string

BaseName returns the base name of Function f. For static functions, this returns the function name unchanged. For constructors, this removes the [constructor] and type prefix. For static functions, this removes the [static] and type prefix. For methods, this removes the [method] and type prefix. For special functions like [resource-drop], it will return a well-known value.

func (*Function) CoreFunction

func (f *Function) CoreFunction(op Op) *Function

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.

func (*Function) DecodeField

func (f *Function) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Function) IsAdmin

func (f *Function) IsAdmin() bool

IsAdmin returns true if Function f is an administrative function in the Canonical ABI.

func (*Function) IsConstructor

func (f *Function) IsConstructor() bool

IsConstructor returns true if Function f is a constructor. To qualify, it must have a *Static Kind with a non-nil type.

func (*Function) IsFreestanding

func (f *Function) IsFreestanding() bool

IsFreestanding returns true if Function f is a freestanding function, and not a constructor, method, or static function.

func (*Function) IsMethod

func (f *Function) IsMethod() bool

IsMethod returns true if Function f is a method. To qualify, it must have a *Method Kind with a non-nil Type which matches borrow<t> of its first param.

func (*Function) IsStatic

func (f *Function) IsStatic() bool

IsStatic returns true if Function f is a static function. To qualify, it must have a *Static Kind with a non-nil type.

func (*Function) ReturnsBorrow

func (f *Function) ReturnsBorrow() bool

ReturnsBorrow reports whether Function f returns a Borrow handle, which is not permitted by the Component Model specification.

func (*Function) Type

func (f *Function) Type() Type

Type returns the associated (self) Type for Function f, if f is a constructor, method, or static function. If f is a freestanding function, this returns nil.

func (*Function) WIT

func (f *Function) WIT(ctx Node, name string) string

WIT returns the WIT text format for Function f.

func (*Function) WITKind

func (f *Function) WITKind() string

WITKind returns the WIT kind for Function f.

type FunctionKind

type FunctionKind interface {
	// contains filtered or unexported methods
}

FunctionKind represents the kind of a WIT function, which can be one of Freestanding, Method, Static, or Constructor.

type Future

type Future struct {
	Type Type // optional associated Type (can be nil)
	// contains filtered or unexported fields
}

Future represents a WIT future type, expected to be part of WASI Preview 3. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Future) Align

func (*Future) Align() uintptr

Align returns the ABI byte alignment a Future. TODO: what is the ABI alignment of a future?

func (*Future) Flat

func (*Future) Flat() []Type

Flat returns the flattened ABI representation of Future. TODO: what is the ABI representation of a stream?

func (*Future) HasBorrow

func (f *Future) HasBorrow() bool

HasBorrow returns whether Future f contains a Borrow.

func (*Future) HasPointer

func (f *Future) HasPointer() bool

HasPointer returns whether the ABI representation of Future f contains a pointer.

func (*Future) Size

func (*Future) Size() uintptr

Size returns the ABI byte size for a Future. TODO: what is the ABI size of a future?

func (Future) TypeName

func (Future) TypeName() string

func (*Future) WIT

func (f *Future) WIT(_ Node, name string) string

WIT returns the WIT text format for Future f.

func (*Future) WITKind

func (*Future) WITKind() string

WITKind returns the WIT kind.

type Handle

type Handle interface {
	TypeDefKind
	// contains filtered or unexported methods
}

Handle represents a WIT handle type. It conforms to the Node, ABI, and TypeDefKind interfaces. Handles represent the passing of unique ownership of a resource between two components. When the owner of an owned handle drops that handle, the resource is destroyed. In contrast, a borrowed handle represents a temporary loan of a handle from the caller to the callee for the duration of the call.

type Ident

type Ident struct {
	// Namespace specifies the package namespace, such as "wasi" in "wasi:foo/bar".
	Namespace string

	// Package specifies the name of the package.
	Package string

	// Extension optionally specifies a world or interface name.
	Extension string

	// Version optionally specifies version information.
	Version *semver.Version
}

Ident represents a Component Model identifier for a Package, World, or Interface, such as wasi:clocks@0.2.0 or wasi:clocks/wall-clock@0.2.0.

A Ident contains a namespace and package name, along with an optional extension and SemVer version.

func ParseIdent

func ParseIdent(s string) (Ident, error)

ParseIdent parses a WIT identifier string into an Ident, returning any errors encountered. The resulting Ident may not be valid.

func (*Ident) DecodeString

func (pn *Ident) DecodeString(s string) error

DecodeString implements the codec.StringDecoder interface to decode a string value into an Ident.

func (*Ident) String

func (id *Ident) String() string

String implements fmt.Stringer, returning the canonical string representation of an Ident.

func (*Ident) UnversionedString

func (id *Ident) UnversionedString() string

UnversionedString returns a string representation of an Ident without version information.

func (*Ident) Validate

func (id *Ident) Validate() error

Validate validates id, returning any errors.

type Interface

type Interface struct {
	Name      *string
	TypeDefs  ordered.Map[string, *TypeDef]
	Functions ordered.Map[string, *Function]

	// The [Package] that this Interface belongs to. It must be non-nil when fully resolved.
	Package *Package
	Docs    Docs
	// contains filtered or unexported fields
}

An Interface represents a collection of types and functions, which are imported into or exported from a WebAssembly component. It implements the Node, TypeOwner, and WorldItem interfaces.

func (*Interface) AllFunctions

func (i *Interface) AllFunctions() iterate.Seq[*Function]

AllFunctions returns a sequence that yields each Function in an Interface. The sequence stops if yield returns false.

func (*Interface) WIT

func (i *Interface) WIT(ctx Node, name string) string

WIT returns the WIT text format for Interface i.

func (*Interface) WITKind

func (*Interface) WITKind() string

WITKind returns the WIT kind.

type List

type List struct {
	Type Type
	// contains filtered or unexported fields
}

List represents a WIT list type, which is an ordered vector of an arbitrary type. It implements the Node, ABI, and TypeDefKind interfaces.

func (*List) Align

func (*List) Align() uintptr

Align returns the ABI byte alignment a List.

func (*List) Flat

func (*List) Flat() []Type

Flat returns the flattened ABI representation of List.

func (*List) HasBorrow

func (l *List) HasBorrow() bool

HasBorrow returns whether List l contains a Borrow.

func (*List) HasPointer

func (*List) HasPointer() bool

HasPointer returns whether the ABI representation of a List contains a pointer. This always returns true.

func (*List) Size

func (*List) Size() uintptr

Size returns the ABI byte size for a List.

func (List) TypeName

func (List) TypeName() string

func (*List) WIT

func (l *List) WIT(_ Node, name string) string

WIT returns the WIT text format for List l.

func (*List) WITKind

func (*List) WITKind() string

WITKind returns the WIT kind.

type Method

type Method struct {
	Type Type
	// contains filtered or unexported fields
}

Method represents a function that is a method on its associated Type. The first argument to the function is self, an instance of Type.

type Node

type Node interface {
	// WITKind returns the human-readable WIT kind this Node represents, e.g. "type" or "function".
	WITKind() string

	// WIT returns the WIT text format for a Node in a given context, which may be nil.
	WIT(ctx Node, name string) string
}

Node is the common interface implemented by the WIT (WebAssembly Interface Type) types in this package.

type Op

type Op uint8

Op represents the Canonical ABI lift and lower operations, for lowering into or lifting out of linear memory.

type Option

type Option struct {
	Type Type
	// contains filtered or unexported fields
}

Option represents a WIT option type, a special case of Variant. An Option can contain a value of a single type, either build-in or user defined, or no value. The equivalent in Go for an option<string> could be represented as *string. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Option) Align

func (o *Option) Align() uintptr

Align returns the ABI byte alignment for Option o. It is first despecialized into a Variant with two cases, "none" and "some(T)", then aligned.

func (*Option) Despecialize

func (o *Option) Despecialize() TypeDefKind

Despecialize despecializes Option o into a Variant with two cases, "none" and "some". See the canonical ABI documentation for more information.

func (*Option) Flat

func (o *Option) Flat() []Type

Flat returns the flattened ABI representation of Option o.

func (*Option) Size

func (o *Option) Size() uintptr

Size returns the ABI byte size for Option o. It is first despecialized into a Variant with two cases, "none" and "some(T)", then sized.

func (Option) TypeName

func (Option) TypeName() string

func (*Option) WIT

func (o *Option) WIT(_ Node, name string) string

WIT returns the WIT text format for Option o.

func (*Option) WITKind

func (*Option) WITKind() string

WITKind returns the WIT kind.

type Own

type Own struct {
	Type *TypeDef
	// contains filtered or unexported fields
}

Own represents an WIT owned handle. It implements the Handle, Node, ABI, and TypeDefKind interfaces.

func (Own) Align

func (Own) Align() uintptr

Align returns the ABI byte alignment for this Handle.

func (Own) Flat

func (Own) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (Own) Size

func (Own) Size() uintptr

Size returns the ABI byte size for this Handle.

func (*Own) WIT

func (o *Own) WIT(ctx Node, name string) string

WIT returns the WIT text format for Own h.

func (*Own) WITKind

func (*Own) WITKind() string

WITKind returns the WIT kind.

type Package

type Package struct {
	Name       Ident
	Interfaces ordered.Map[string, *Interface]
	Worlds     ordered.Map[string, *World]
	Docs       Docs
}

Package represents a WIT package within a Resolve. It implements the Node interface.

A Package is a collection of Interface and World values. Additionally, a Package contains a unique identifier that affects generated components and uniquely identifies this particular package.

func (*Package) WIT

func (p *Package) WIT(ctx Node, _ string) string

WIT returns the WIT text format of Package p.

func (*Package) WITKind

func (*Package) WITKind() string

WITKind returns the WIT kind.

type Param

type Param struct {
	Name string
	Type Type
}

Param represents a parameter to or the result of a Function. A Param can be unnamed.

func (*Param) DecodeField

func (p *Param) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Param) WIT

func (p *Param) WIT(_ Node, _ string) string

WIT returns the WIT text format of Param p.

func (*Param) WITKind

func (*Param) WITKind() string

WITKind returns the WIT kind.

type Pointer

type Pointer struct {
	Type Type
	// contains filtered or unexported fields
}

Pointer represents a pointer to a WIT type. It is only used for ABI representation, e.g. pointers to function parameters or return values.

func (*Pointer) Align

func (*Pointer) Align() uintptr

Align returns the ABI byte alignment for Pointer.

func (*Pointer) Flat

func (*Pointer) Flat() []Type

Flat returns the flattened ABI representation of Pointer.

func (*Pointer) HasPointer

func (*Pointer) HasPointer() bool

HasPointer returns whether the ABI representation of Pointer contains a pointer. This always returns true.

func (*Pointer) Size

func (*Pointer) Size() uintptr

Size returns the ABI byte size for Pointer.

func (Pointer) TypeName

func (Pointer) TypeName() string

func (*Pointer) WIT

func (p *Pointer) WIT(_ Node, name string) string

WIT returns the WIT text format for Pointer p. Note: there is no canonical WIT representation of a pointer. This method exists solely to implement the Node interface.

func (*Pointer) WITKind

func (*Pointer) WITKind() string

WITKind returns the WIT kind.

type Primitive

type Primitive interface {
	Type
	// contains filtered or unexported methods
}

Primitive is the interface implemented by WIT primitive types. It also conforms to the Node, ABI, Type, and TypeDefKind interfaces.

type Record

type Record struct {
	Fields []Field
	// contains filtered or unexported fields
}

Record represents a WIT record type, akin to a struct. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Record) Align

func (r *Record) Align() uintptr

Align returns the ABI byte alignment for Record r.

func (*Record) DecodeField

func (r *Record) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Record) Flat

func (r *Record) Flat() []Type

Flat returns the flattened ABI representation of Record r.

func (*Record) HasPointer

func (r *Record) HasPointer() bool

HasPointer returns whether the ABI representation of Record r contains a pointer.

func (*Record) Size

func (r *Record) Size() uintptr

Size returns the ABI byte size for Record r.

func (Record) TypeName

func (Record) TypeName() string

func (*Record) WIT

func (r *Record) WIT(ctx Node, name string) string

WIT returns the WIT text format for Record r.

func (*Record) WITKind

func (*Record) WITKind() string

WITKind returns the WIT kind.

type Resolve

type Resolve struct {
	Worlds     []*World
	Interfaces []*Interface
	TypeDefs   []*TypeDef
	Packages   []*Package
}

Resolve represents a fully resolved set of WIT (WebAssembly Interface Type) packages and worlds. It implements the Node interface.

This structure contains a graph of WIT packages and their contents merged together into slices organized by type. Items are sorted topologically and everything is fully resolved.

Each World, Interface, TypeDef, or Package in a Resolve must be non-nil.

func DecodeJSON

func DecodeJSON(r io.Reader) (*Resolve, error)

DecodeJSON decodes JSON from r into a Resolve struct. It returns any error that may occur during decoding.

func LoadJSON

func LoadJSON(path string) (*Resolve, error)

LoadJSON loads a WIT JSON file from path. If path is "" or "-", it reads from os.Stdin.

func LoadWIT

func LoadWIT(path string) (*Resolve, error)

LoadWIT loads WIT data from path by processing it through wasm-tools. This will fail if wasm-tools is not in $PATH. If path is "" or "-", it reads from os.Stdin.

func (*Resolve) AllFunctions

func (r *Resolve) AllFunctions() iterate.Seq[*Function]

AllFunctions returns a sequence that yields each Function in a Resolve. The sequence stops if yield returns false.

func (*Resolve) DecodeField

func (c *Resolve) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Resolve) ResolveCodec

func (res *Resolve) ResolveCodec(v any) codec.Codec

ResolveCodec implements the codec.Resolver interface translating types to decoding/encoding-aware versions.

func (*Resolve) WIT

func (r *Resolve) WIT(_ Node, _ string) string

WIT returns the WIT text format for Resolve r. Note that the return value could represent multiple files, so may not be precisely valid WIT text.

func (*Resolve) WITKind

func (*Resolve) WITKind() string

WITKind returns the WIT kind.

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource represents a WIT resource type. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Resource) Align

func (*Resource) Align() uintptr

Align returns the ABI byte alignment for Resource.

func (*Resource) Flat

func (*Resource) Flat() []Type

Flat returns the flattened ABI representation of Resource.

func (*Resource) Size

func (*Resource) Size() uintptr

Size returns the ABI byte size for Resource.

func (Resource) TypeName

func (Resource) TypeName() string

func (*Resource) WIT

func (r *Resource) WIT(ctx Node, name string) string

WIT returns the WIT text format for Resource r.

func (*Resource) WITKind

func (*Resource) WITKind() string

WITKind returns the WIT kind.

type Result

type Result struct {
	OK  Type // optional associated Type (can be nil)
	Err Type // optional associated Type (can be nil)
	// contains filtered or unexported fields
}

Result represents a WIT result type, which is the result of a function call, returning an optional value and/or an optional error. It is roughly equivalent to the Go pattern of returning (T, error). It implements the Node, ABI, and TypeDefKind interfaces.

func (*Result) Align

func (r *Result) Align() uintptr

Align returns the ABI byte alignment for Result r. It is first despecialized into a Variant with two cases "ok" and "error", then aligned.

func (*Result) DecodeField

func (r *Result) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Result) Despecialize

func (r *Result) Despecialize() TypeDefKind

Despecialize despecializes Result o into a Variant with two cases, "ok" and "error". See the canonical ABI documentation for more information.

func (*Result) Flat

func (r *Result) Flat() []Type

Flat returns the flattened ABI representation of Result r.

func (*Result) Size

func (r *Result) Size() uintptr

Size returns the ABI byte size for Result r. It is first despecialized into a Variant with two cases "ok" and "error", then sized.

func (Result) TypeName

func (Result) TypeName() string

func (*Result) WIT

func (r *Result) WIT(_ Node, name string) string

WIT returns the WIT text format for Result r.

func (*Result) WITKind

func (*Result) WITKind() string

WITKind returns the WIT kind.

type S16

type S16 struct {
	// contains filtered or unexported fields
}

S16 represents the WIT primitive type s16, a signed 16-bit integer. It is equivalent to the Go type int16. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (S16) Align

func (S16) Align() uintptr

Align returns the byte alignment for values of this type.

func (S16) Flat

func (S16) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (S16) HasPointer

func (S16) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (S16) Size

func (S16) Size() uintptr

Size returns the byte size for values of this type.

func (S16) TypeName

func (S16) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (S16) WIT

func (p S16) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (S16) WITKind

func (S16) WITKind() string

WITKind returns the WIT kind.

type S32

type S32 struct {
	// contains filtered or unexported fields
}

S32 represents the WIT primitive type s32, a signed 32-bit integer. It is equivalent to the Go type int32. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (S32) Align

func (S32) Align() uintptr

Align returns the byte alignment for values of this type.

func (S32) Flat

func (S32) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (S32) HasPointer

func (S32) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (S32) Size

func (S32) Size() uintptr

Size returns the byte size for values of this type.

func (S32) TypeName

func (S32) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (S32) WIT

func (p S32) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (S32) WITKind

func (S32) WITKind() string

WITKind returns the WIT kind.

type S64

type S64 struct {
	// contains filtered or unexported fields
}

S64 represents the WIT primitive type s64, a signed 64-bit integer. It is equivalent to the Go type int64. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (S64) Align

func (S64) Align() uintptr

Align returns the byte alignment for values of this type.

func (S64) Flat

func (S64) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (S64) HasPointer

func (S64) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (S64) Size

func (S64) Size() uintptr

Size returns the byte size for values of this type.

func (S64) TypeName

func (S64) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (S64) WIT

func (p S64) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (S64) WITKind

func (S64) WITKind() string

WITKind returns the WIT kind.

type S8

type S8 struct {
	// contains filtered or unexported fields
}

S8 represents the WIT primitive type s8, a signed 8-bit integer. It is equivalent to the Go type int8. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (S8) Align

func (S8) Align() uintptr

Align returns the byte alignment for values of this type.

func (S8) Flat

func (S8) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (S8) HasPointer

func (S8) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (S8) Size

func (S8) Size() uintptr

Size returns the byte size for values of this type.

func (S8) TypeName

func (S8) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (S8) WIT

func (p S8) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (S8) WITKind

func (S8) WITKind() string

WITKind returns the WIT kind.

type Static

type Static struct {
	Type Type
	// contains filtered or unexported fields
}

Static represents a function that is a static method of its associated Type.

type Stream

type Stream struct {
	Element Type // optional associated Type (can be nil)
	End     Type // optional associated Type (can be nil)
	// contains filtered or unexported fields
}

Stream represents a WIT stream type, expected to be part of WASI Preview 3. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Stream) Align

func (*Stream) Align() uintptr

Align returns the ABI byte alignment a Stream. TODO: what is the ABI alignment of a stream?

func (*Stream) DecodeField

func (s *Stream) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Stream) Flat

func (*Stream) Flat() []Type

Flat returns the flattened ABI representation of Stream. TODO: what is the ABI representation of a stream?

func (*Stream) HasBorrow

func (s *Stream) HasBorrow() bool

HasBorrow returns whether Stream s contains a Borrow.

func (*Stream) HasPointer

func (s *Stream) HasPointer() bool

HasPointer returns whether the ABI representation of Stream s contains a pointer.

func (*Stream) Size

func (*Stream) Size() uintptr

Size returns the ABI byte size for a Stream. TODO: what is the ABI size of a stream?

func (Stream) TypeName

func (Stream) TypeName() string

func (*Stream) WIT

func (s *Stream) WIT(_ Node, name string) string

WIT returns the WIT text format for Stream s.

func (*Stream) WITKind

func (*Stream) WITKind() string

WITKind returns the WIT kind.

type String

type String struct {
	// contains filtered or unexported fields
}

String represents the WIT primitive type string, a finite string of Unicode characters. It is equivalent to the Go type string. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (String) Align

func (String) Align() uintptr

Align returns the byte alignment for values of this type.

func (String) Flat

func (String) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (String) HasPointer

func (String) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (String) Size

func (String) Size() uintptr

Size returns the byte size for values of this type.

func (String) TypeName

func (String) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (String) WIT

func (p String) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (String) WITKind

func (String) WITKind() string

WITKind returns the WIT kind.

type Tuple

type Tuple struct {
	Types []Type
	// contains filtered or unexported fields
}

Tuple represents a WIT tuple type. A tuple type is an ordered fixed length sequence of values of specified types. It is similar to a Record, except that the fields are identified by their order instead of by names. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Tuple) Align

func (t *Tuple) Align() uintptr

Align returns the ABI byte alignment for Tuple t. It is first despecialized into a Record with 0-based integer field names, then aligned.

func (*Tuple) DecodeField

func (t *Tuple) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Tuple) Despecialize

func (t *Tuple) Despecialize() TypeDefKind

Despecialize despecializes Tuple e into a Record with 0-based integer field names. See the canonical ABI documentation for more information.

func (*Tuple) Flat

func (t *Tuple) Flat() []Type

Flat returns the flattened ABI representation of Tuple t.

func (*Tuple) Size

func (t *Tuple) Size() uintptr

Size returns the ABI byte size for Tuple t. It is first despecialized into a Record with 0-based integer field names, then sized.

func (*Tuple) Type

func (t *Tuple) Type() Type

Type returns a non-nil Type if all types in t are the same. Returns nil if t contains more than one type.

func (Tuple) TypeName

func (Tuple) TypeName() string

func (*Tuple) WIT

func (t *Tuple) WIT(ctx Node, name string) string

WIT returns the WIT text format for Tuple t.

func (*Tuple) WITKind

func (*Tuple) WITKind() string

WITKind returns the WIT kind.

type Type

type Type interface {
	TypeDefKind
	// contains filtered or unexported methods
}

Type is the interface implemented by any type definition. This can be a primitive type or a user-defined type in a TypeDef. It also conforms to the Node, ABI, and TypeDefKind interfaces.

func Discriminant

func Discriminant(n int) Type

Discriminant returns the smallest WIT integer type that can represent 0...n. Used by the Canonical ABI for Variant types.

func ParseType

func ParseType(s string) (Type, error)

ParseType parses a WIT primitive type string into the associated Type implementation from this package. It returns an error if the type string is not recognized.

type TypeDef

type TypeDef struct {
	Name  *string
	Kind  TypeDefKind
	Owner TypeOwner
	Docs  Docs
	// contains filtered or unexported fields
}

TypeDef represents a WIT type definition. A TypeDef may be named or anonymous, and optionally belong to a World or Interface. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (*TypeDef) Align

func (t *TypeDef) Align() uintptr

Align returns the byte alignment for values of type t.

func (*TypeDef) Constructor

func (t *TypeDef) Constructor() *Function

Constructor returns the constructor for TypeDef t, or nil if none. Currently t must be a Resource to have a constructor.

func (*TypeDef) Flat

func (t *TypeDef) Flat() []Type

Flat returns the flattened ABI representation of t.

func (*TypeDef) HasBorrow

func (t *TypeDef) HasBorrow() bool

HasBorrow returns whether TypeDef t contains a Borrow.

func (*TypeDef) HasPointer

func (t *TypeDef) HasPointer() bool

HasPointer returns whether the ABI representation of TypeDef t contains a pointer.

func (*TypeDef) Methods

func (t *TypeDef) Methods() []*Function

Methods returns all methods for TypeDef t. Currently t must be a Resource to have methods.

func (*TypeDef) Package

func (t *TypeDef) Package() *Package

Package returns the Package that t is associated with, if any.

func (*TypeDef) ResourceDrop

func (t *TypeDef) ResourceDrop() *Function

ResourceDrop returns the implied resource-drop method for t. If t is not a Resource, this returns nil.

func (*TypeDef) Root

func (t *TypeDef) Root() *TypeDef

Root returns the root TypeDef of type alias t. If t is not a type alias, Root returns t.

func (*TypeDef) Size

func (t *TypeDef) Size() uintptr

Size returns the byte size for values of type t.

func (*TypeDef) StaticFunctions

func (t *TypeDef) StaticFunctions() []*Function

StaticFunctions returns all static functions for TypeDef t. Currently t must be a Resource to have static functions.

func (*TypeDef) TypeName

func (t *TypeDef) TypeName() string

TypeName returns the WIT type name for t. Returns an empty string if t is anonymous.

func (*TypeDef) WIT

func (t *TypeDef) WIT(ctx Node, name string) string

WIT returns the WIT text format for TypeDef t.

func (*TypeDef) WITKind

func (t *TypeDef) WITKind() string

WITKind returns the [WIT] kind.

type TypeDefKind

type TypeDefKind interface {
	Node
	ABI
	TypeName() string
	// contains filtered or unexported methods
}

TypeDefKind represents the underlying type in a TypeDef, which can be one of Record, Resource, Handle, Flags, Tuple, Variant, Enum, Option, Result, List, Future, Stream, or Type. It implements the Node and ABI interfaces.

func Despecialize

func Despecialize(k TypeDefKind) TypeDefKind

Despecialize despecializes k if k implements [Despecializer]. Otherwise, it returns k unmodified. See the canonical ABI documentation for more information.

type TypeOwner

type TypeOwner interface {
	Node
	AllFunctions() iterate.Seq[*Function]
	// contains filtered or unexported methods
}

TypeOwner is the interface implemented by any type that can own a TypeDef, currently World and Interface.

type U16

type U16 struct {
	// contains filtered or unexported fields
}

U16 represents the WIT primitive type u16, an unsigned 16-bit integer. It is equivalent to the Go type uint16. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (U16) Align

func (U16) Align() uintptr

Align returns the byte alignment for values of this type.

func (U16) Flat

func (U16) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (U16) HasPointer

func (U16) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (U16) Size

func (U16) Size() uintptr

Size returns the byte size for values of this type.

func (U16) TypeName

func (U16) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (U16) WIT

func (p U16) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (U16) WITKind

func (U16) WITKind() string

WITKind returns the WIT kind.

type U32

type U32 struct {
	// contains filtered or unexported fields
}

U32 represents the WIT primitive type u32, an unsigned 32-bit integer. It is equivalent to the Go type uint32. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (U32) Align

func (U32) Align() uintptr

Align returns the byte alignment for values of this type.

func (U32) Flat

func (U32) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (U32) HasPointer

func (U32) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (U32) Size

func (U32) Size() uintptr

Size returns the byte size for values of this type.

func (U32) TypeName

func (U32) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (U32) WIT

func (p U32) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (U32) WITKind

func (U32) WITKind() string

WITKind returns the WIT kind.

type U64

type U64 struct {
	// contains filtered or unexported fields
}

U64 represents the WIT primitive type u64, an unsigned 64-bit integer. It is equivalent to the Go type uint64. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (U64) Align

func (U64) Align() uintptr

Align returns the byte alignment for values of this type.

func (U64) Flat

func (U64) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (U64) HasPointer

func (U64) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (U64) Size

func (U64) Size() uintptr

Size returns the byte size for values of this type.

func (U64) TypeName

func (U64) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (U64) WIT

func (p U64) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (U64) WITKind

func (U64) WITKind() string

WITKind returns the WIT kind.

type U8

type U8 struct {
	// contains filtered or unexported fields
}

U8 represents the WIT primitive type u8, an unsigned 8-bit integer. It is equivalent to the Go type uint8. It implements the Node, ABI, Type, and TypeDefKind interfaces.

func (U8) Align

func (U8) Align() uintptr

Align returns the byte alignment for values of this type.

func (U8) Flat

func (U8) Flat() []Type

Flat returns the flattened ABI representation of this type.

func (U8) HasPointer

func (U8) HasPointer() bool

HasPointer returns whether the ABI representation of this type contains a pointer. This will only return true for String.

func (U8) Size

func (U8) Size() uintptr

Size returns the byte size for values of this type.

func (U8) TypeName

func (U8) TypeName() string

TypeName returns the canonical primitive type name in WIT text format.

func (U8) WIT

func (p U8) WIT(_ Node, name string) string

WIT returns the WIT text format for this [_primitive].

func (U8) WITKind

func (U8) WITKind() string

WITKind returns the WIT kind.

type Variant

type Variant struct {
	Cases []Case
	// contains filtered or unexported fields
}

Variant represents a WIT variant type, a tagged/discriminated union. A variant type declares one or more cases. Each case has a name and, optionally, a type of data associated with that case. It implements the Node, ABI, and TypeDefKind interfaces.

func (*Variant) Align

func (v *Variant) Align() uintptr

Align returns the ABI byte alignment for Variant v.

func (*Variant) DecodeField

func (v *Variant) DecodeField(dec codec.Decoder, name string) error

DecodeField implements the codec.FieldDecoder interface to decode a struct or JSON object.

func (*Variant) Enum

func (v *Variant) Enum() *Enum

Enum attempts to represent Variant v as an Enum. This will only succeed if v has no associated types. If v has associated types, then it will return nil.

func (*Variant) Flat

func (v *Variant) Flat() []Type

Flat returns the flattened ABI representation of Variant v.

func (*Variant) HasBorrow

func (v *Variant) HasBorrow() bool

HasBorrow returns whether Variant v contains a Borrow.

func (*Variant) HasPointer

func (v *Variant) HasPointer() bool

HasPointer returns true if Variant v has an associated type that contains a pointer (e.g. string, list).

func (*Variant) Size

func (v *Variant) Size() uintptr

Size returns the ABI byte size for Variant v.

func (Variant) TypeName

func (Variant) TypeName() string

func (*Variant) Types

func (v *Variant) Types() []Type

Types returns the unique associated types in Variant v.

func (*Variant) WIT

func (v *Variant) WIT(ctx Node, name string) string

WIT returns the WIT text format for Variant v.

func (*Variant) WITKind

func (*Variant) WITKind() string

WITKind returns the WIT kind.

type World

type World struct {
	Name    string
	Imports ordered.Map[string, WorldItem]
	Exports ordered.Map[string, WorldItem]

	// The [Package] that this World belongs to. It must be non-nil when fully resolved.
	Package *Package
	Docs    Docs
	// contains filtered or unexported fields
}

A World represents all of the imports and exports of a WebAssembly component. It implements the Node and TypeOwner interfaces.

func (*World) AllFunctions

func (w *World) AllFunctions() iterate.Seq[*Function]

AllFunctions returns a sequence that yields each Function in a World. The sequence stops if yield returns false.

func (*World) WIT

func (w *World) WIT(ctx Node, name string) string

WIT returns the WIT text format for World w.

func (*World) WITKind

func (*World) WITKind() string

WITKind returns the WIT kind.

type WorldItem

type WorldItem interface {
	Node
	// contains filtered or unexported methods
}

A WorldItem is any item that can be exported from or imported into a World, currently either an Interface, TypeDef, or Function. Any WorldItem is also a Node.

Directories

Path Synopsis
Package bindgen generates Go source code from a fully-resolved WIT package.
Package bindgen generates Go source code from a fully-resolved WIT package.

Jump to

Keyboard shortcuts

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