wit

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wit reads WIT -- the WebAssembly Interface Type language -- into the descriptors component.WithImportCustom takes, so a host can register an interface from its .wit file instead of building each signature by hand.

It needs no wazy.Runtime: parsing is pure and touches no engine, so a code generator or a linter can use it on its own.

pkg, err := wit.ParseDir("wit") // wit/*.wit plus wit/deps/
if err != nil {
	return err
}
w := pkg.World("app")
for _, imp := range w.Imports {
	fmt.Println("import", imp.Name)
}
for _, exp := range w.Exports {
	fmt.Println("export", exp.Name)
}

and to register an import from it:

host := pkg.Interface("acme:api/host@1.0.0")
sig, err := host.Func("lookup").Signature(nil)
if err != nil {
	return err
}
opt := component.WithImportCustom(host.Name, "lookup", fn, sig.Desc, sig.Types.Resolver())

Validation

A source is read as wit-parser, the Bytecode Alliance's reference WIT resolver, reads it, and fails where it fails, with its message: syntax errors (with their file:line:col), names that are not strongly unique or not defined, cycles among types, interfaces, worlds or packages, handles of what is not a resource, ill-formed records, variants, enums, flags, tuples and maps, constructor and accessor signatures, returned borrows, and the conflicts an include brings in. Every file needs a package header; a file may also hold nested "package ns:name { ... }" blocks, each a package of its own. A top-level use names an item for its own file, and a package is named with its exact version.

Resources

own<R> and borrow<R> name a resource by the tag the host mints its handles under, which is the host's choice. Signature takes those tags keyed the way component.WithResourceTag is -- the interface that declares the resource, and the resource's name -- so a descriptor and the tag registration agree:

tags := wit.Tags{{Interface: host.Name, Name: "counter"}: counterTag}
sig, err := host.Func("[method]counter.bump").Signature(tags)
opt := component.WithResourceTag(host.Name, "counter", counterTag)

Names

An accessor ("size: get() -> u32;", "size: set(v: u32);") is a Func named "[get]size" or "[set]size", a resource's "[method][get]r.size" and so on, with its Accessor set. An import or export under a label of its own ("import primary: store;") is the interface it implements, copied under the label, as WorldItem.Implements says.

A Signature carries every name a code generator needs alongside the types: FuncDesc params keep their WIT names, the descriptors in the TypeTable keep record field, variant, enum and flags case names, and Signature.TypeNames names each table slot that holds a named WIT type. A resource's constructor, methods and static funcs are grouped under Interface.Resources.

Feature gates

Gates apply as wit-parser applies them. An item marked @since or @deprecated is always read; one marked @unstable(feature = x) is read only when x is enabled with WithFeatures, or every feature is with WithAllFeatures. By default none is, so a parse sees what a toolchain targeting the stable API sees:

pkg, err := wit.ParseDir("wit", wit.WithFeatures("cli-exit-with-code"))

An item left out is gone everywhere: from its interface or world, from its resource, from a use that named it and from an include that would have brought it in. Anything still read that refers to it is an error, as is a type kept by its own gate inside an interface or world that is left out. An @since gate needs a package version no earlier than its own.

A type or use of a world that its gate leaves out is an error, though, as in wit-parser, whose world pass maps the type before it checks the gate.

Example

Parse a WIT source, list a world's imports and exports, and build one import's signature for component.WithImportCustom.

package main

import (
	"fmt"

	_ "embed"
	"github.com/samyfodil/wazy/component/wit"
)

func main() {
	pkg, err := wit.Parse("app.wit", `
package acme:api@1.0.0;

interface host {
  record query { name: string, limit: u32 }
  lookup: func(q: query) -> list<string>;
}

world app {
  import host;
  export run: func() -> string;
}`)
	if err != nil {
		panic(err)
	}
	app := pkg.World("app")
	for _, imp := range app.Imports {
		fmt.Println("import", imp.Name)
	}
	for _, exp := range app.Exports {
		fmt.Println("export", exp.Name)
	}

	host := pkg.Interface("host")
	sig, err := host.Func("lookup").Signature(nil)
	if err != nil {
		panic(err)
	}
	q := sig.Desc.Params[0]
	fmt.Println("lookup param", q.Name, "is a", sig.TypeNames[*q.Type.TypeIndex])
	// Register it with component.WithImportCustom(host.Name, "lookup", fn, sig.Desc, sig.Types.Resolver()).

}
Output:
import acme:api/host@1.0.0
export run
lookup param q is a query

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accessor

type Accessor int

Accessor says whether a Func is a property's getter or setter.

const (
	// NotAccessor is a func of its own.
	NotAccessor Accessor = iota
	// Getter is "name: get() -> T", named "[get]name" -- or for a resource,
	// a "[method][get]r.name" or "[static][get]r.name".
	Getter
	// Setter is "name: set(value: T)", named "[set]name" and so on. A world
	// or interface with a setter has the getter of the same name too.
	Setter
)

type Func

type Func struct {
	// Name is the canonical import name: "lookup", "[constructor]counter",
	// "[method]counter.bump" or "[static]counter.zero", with a [get] or
	// [set] after any of those for an accessor ("[get]size",
	// "[method][set]counter.value").
	Name string
	// Interface is the qualified name of the declaring interface, or "" for a
	// func declared directly in a world.
	Interface string
	// Resource is the resource a Method, Static or Constructor belongs to.
	Resource string
	Kind     FuncKind
	Accessor Accessor
	// contains filtered or unexported fields
}

Func is one WIT func.

func (*Func) Signature

func (f *Func) Signature(tags Tags) (*Signature, error)

Signature builds the func's descriptor, with every own and borrow of a resource carrying that resource's tag from tags. A resource reached by the signature that tags has no entry for is an error; a signature that reaches none needs no tags at all.

type FuncKind

type FuncKind int

FuncKind says where a Func is declared.

const (
	// Freestanding is a func declared directly in an interface or world.
	Freestanding FuncKind = iota
	// Method is a resource method. Its signature starts with an implicit
	// self: borrow<R>.
	Method
	// Static is a resource's static func.
	Static
	// Constructor is a resource's constructor. Unless the WIT declares a
	// result, its signature returns own<R>.
	Constructor
)

type Interface

type Interface struct {
	// Name is what a component imports the interface by, and therefore the
	// iface argument of component.WithImportCustom and WithResourceTag:
	// "acme:api/host@1.0.0". An interface written inline in a world
	// ("import x: interface { ... }") is named by its import name, "x".
	Name string
	// Funcs are every func a component can import from the interface, in
	// declaration order, under its canonical import name: a freestanding
	// "lookup" as well as a resource's "[constructor]counter",
	// "[method]counter.bump" and "[static]counter.zero".
	Funcs []*Func
	// Resources are the resources the interface declares, in declaration
	// order.
	Resources []*Resource
	// contains filtered or unexported fields
}

Interface is a WIT interface: the unit a component imports or exports.

func (*Interface) Func

func (i *Interface) Func(name string) *Func

Func returns the func with the canonical import name name, or nil.

func (*Interface) Types added in v0.8.0

func (i *Interface) Types(tags Tags) (*TypeSet, error)

Types builds every value type the interface defines, whether or not a func of its uses it, with resources carrying their tags from tags.

type Option

type Option func(*config)

Option configures how a parse applies feature gates.

func WithAllFeatures

func WithAllFeatures() Option

WithAllFeatures enables every @unstable feature.

func WithFeatures

func WithFeatures(names ...string) Option

WithFeatures enables the named @unstable features: an item gated by @unstable(feature = name) is read only when name is enabled. It may be given more than once; the names add up.

type Package

type Package struct {
	// Name is the package name as declared, e.g. "acme:api@1.0.0".
	Name string
	// Interfaces are the package's interfaces in declaration order.
	Interfaces []*Interface
	// Worlds are the package's worlds in declaration order.
	Worlds []*World
	// Deps are the other packages read with it, sorted by name: those the
	// deps/ directory holds, and those written as nested
	// "package ns:name { ... }" blocks in its files or theirs.
	Deps []*Package
	// contains filtered or unexported fields
}

Package is one WIT package with every name in it resolved.

func FromComponent added in v0.8.0

func FromComponent(componentBytes []byte, opts ...Option) (*Package, error)

FromComponent reads the WIT of a compiled component: what it imports and exports, as the world "root" of a package "root:component", with each interface in a package of its own, as `wasm-tools component wit` prints it. It is the WIT a host needs to bind a component it did not build.

func Parse

func Parse(filename, src string, opts ...Option) (*Package, error)

Parse reads one WIT source: a package, with any nested "package ns:name { ... }" blocks, which become its Deps. Names resolve within it. filename is used only in error messages.

func ParseDir

func ParseDir(dir string, opts ...Option) (*Package, error)

ParseDir reads the WIT package in dir -- every .wit file directly in it -- together with dir/deps/, where each dependency is a directory of .wit files or a single .wit file. Uses resolve across all of them.

func ParseFS

func ParseFS(fsys fs.FS, dir string, opts ...Option) (*Package, error)

ParseFS is ParseDir over an fs.FS, such as an embed.FS.

func ParseFile

func ParseFile(path string, opts ...Option) (*Package, error)

ParseFile reads the single WIT file at path, as Parse does.

func (*Package) Interface

func (p *Package) Interface(name string) *Interface

Interface returns the interface called name: either its short name within this package ("host") or its qualified name in this package or any of its Deps ("acme:api/host@1.0.0"). It returns nil when there is none.

func (*Package) World

func (p *Package) World(name string) *World

World returns the world called name in this package, or nil.

type Resource

type Resource struct {
	// Interface is the qualified name of the declaring interface; with Name,
	// it is the key component.WithResourceTag and Tags use.
	Interface string
	Name      string
	// Funcs are the constructor, methods and static funcs in declaration
	// order. Each is also listed in the declaring Interface.Funcs.
	Funcs []*Func
}

Resource is a WIT resource and the funcs declared on it.

type ResourceKey

type ResourceKey struct {
	Interface string
	Name      string
}

ResourceKey identifies a resource the way component.WithResourceTag does: the qualified name of the interface that declares it, and its name.

type Signature

type Signature struct {
	Desc  component.FuncDesc
	Types *component.TypeTable
	// TypeNames maps each Types index holding a named WIT type -- a record,
	// variant, enum, flags, an alias to a composite, or a handle to a named
	// resource -- to that name. Anonymous types (list<u8>, option<T>, ...)
	// have none.
	TypeNames map[uint32]string
	// TypeOwners maps the same indices to the qualified name of the interface
	// defining the type, "" for a world's own.
	TypeOwners map[uint32]string
}

Signature is one func's descriptor, ready for component.WithImportCustom:

component.WithImportCustom(iface, f.Name, fn, sig.Desc, sig.Types.Resolver())

type Tags

type Tags map[ResourceKey]uint32

Tags are the host's resource tags, by resource.

type TypeSet added in v0.8.0

type TypeSet struct {
	// Refs are the interface's value types -- records, variants, enums,
	// flags and aliases, not resources -- in declaration order.
	Refs       []component.TypeRef
	Types      *component.TypeTable
	TypeNames  map[uint32]string
	TypeOwners map[uint32]string
}

TypeSet is the value types an interface defines, built into one table as a Signature's are.

type World

type World struct {
	// Name is the world's name as declared, e.g. "app".
	Name string
	// Imports and Exports are the world's own, in declaration order, then
	// what each include brings in -- renamed as its "with { a as b }" says, which
	// may rename a func, an interface written inline (with its funcs and
	// resources, and so the interface its resources' tags are keyed by), or
	// one of the world's types, a resource renaming its funcs too -- and
	// elaborated as wit-parser elaborates a world:
	// an interface that uses another's types brings that one in too, right
	// before itself -- as an import, or for an export that the world does
	// not export, also as an import. The interfaces the world's own uses
	// name come first.
	Imports []WorldItem
	Exports []WorldItem
}

World is a WIT world: the imports and exports of one kind of component.

type WorldItem

type WorldItem struct {
	// Name is the name the component imports or exports the item by: an
	// interface's qualified name, or a func's, inline interface's or
	// label's own name.
	Name string
	// Exactly one of Interface and Func is set.
	Interface *Interface
	Func      *Func
	// Implements is, for an interface under a label of its own
	// ("import primary: store;"), the qualified name of the interface it
	// implements. Its Interface is that one's copy, named by the label as an
	// inline interface is.
	Implements string
}

WorldItem is one import or export of a world: an interface or a func.

Jump to

Keyboard shortcuts

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