custom-wit

command
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 7 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.

README

Implementing your own WIT interface, host-side

This example defines a WIT interface of its own and implements it in Go. No WASI involved.

The interface (testdata/store.wit) is deliberately not a toy — it uses the shapes real interfaces use:

package example:store@1.0.0;

interface kv {
  resource bucket {
    constructor(name: string);
    put: func(key: string, value: list<u8>);
    get: func(key: string) -> option<list<u8>>;
  }
  variant error { no-such-bucket, denied(string) }
  list-keys: func(b: borrow<bucket>) -> result<list<string>, error>;
}

A resource with a constructor and methods, an option return, a result whose error arm is a variant, and lists in both directions.

Run it:

go run .
host: created bucket "cache" (rep 1)
guest: hello wazy|missing=true|keys=greeting,target

What to look at

TypeTable builds the signatures. A WIT type is a graph, not a tree: result<list<string>, error> needs its arms to be nameable from the result, which is what a type table's TypeRefs do. The sugar (List, Option, Result, Variant, Own, Borrow, Func) means you never write a TypeRef yourself.

WithImportCustom registers them. The simpler WithImport takes a flat list of types and cannot express a composite whose children are composites, which is most of this interface.

WithResourceTag is not optional. Any interface with a resource needs it. The guest drops its owned handle through a canon carrying the component binary's own type index, while the host mints handles under a tag it chose; this maps one to the other. Leave it out and everything works until the first drop, which then fails with a cross-type error that doesn't mention tags.

Values have shapes. A record and a tuple arrive as []Value in declaration order; a variant is a VariantValue{Disc, Payload}; a result is a ResultValue{IsErr, Payload}; option is nil for none or the inner value for some; list<u8> arrives as []byte. Note that returning a Go error from a host func traps the guest — a WIT-declared failure is a ResultValue with IsErr set, which is an ordinary value the guest handles.

Testing host funcs without a guest

Compiling a component per error branch is impractical. component/componenttest applies the same Options and hands back the registered funcs so they can be called directly. It does not exercise the Canonical ABI, so keep at least one real-guest test per interface — like this example — and use the harness for the branches underneath it.

Rebuilding the guest

testdata/store.wasm is a Rust component built against the same .wit:

cargo build --release --target wasm32-wasip2

with wit-bindgen generating the bindings from wit/store.wit.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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