diff --git a/component-model/src/design/wit.md b/component-model/src/design/wit.md index df203233..ae2ae51f 100644 --- a/component-model/src/design/wit.md +++ b/component-model/src/design/wit.md @@ -23,6 +23,9 @@ The official WIT specification and history can be found in the [`WebAssembly/com - [Options](#options) - [Results](#results) - [Tuples](#tuples) + - [Maps](#maps) + - [Streams](#streams) + - [Futures](#futures) - [User-defined types](#user-defined-types) - [Records](#records) - [Variants](#variants) @@ -31,13 +34,20 @@ The official WIT specification and history can be found in the [`WebAssembly/com - [Flags](#flags) - [Type aliases](#type-aliases) - [Functions](#functions) + - [Asynchronous functions](#asynchronous-functions) - [Interfaces](#interfaces) - [Using definitions from elsewhere](#using-definitions-from-elsewhere) - [Worlds](#worlds) - [Interfaces from other packages](#interfaces-from-other-packages) - [Inline interfaces](#inline-interfaces) + - [Named imports and exports of interfaces](#named-imports-and-exports-of-interfaces) + - [External IDs](#external-ids) - [Including other worlds](#including-other-worlds) - [Packages](#packages) + - [Top-level `use`](#top-level-use) + - [Nested package definitions](#nested-package-definitions) + - [Feature gates](#feature-gates) + - [Unstable language features](#unstable-language-features) ## Structure of a WIT file @@ -45,7 +55,9 @@ A WIT file contains one or more **interfaces** or **worlds**. An interface or wo > Types and functions can't be defined outside of interfaces or worlds. -A file may optionally start with a **package** declaration. +A file may optionally start with a **package** declaration, +and may contain top-level [`use` statements](#top-level-use) +and [nested package definitions](#nested-package-definitions). ## Comments @@ -196,6 +208,31 @@ tuple // An integer, then a string, then an integer This is similar to tuples in Rust or OCaml. +### Maps + +`map` is a collection of key-value pairs, +where `K` is the key type and `V` is the value type. + +```wit +map // e.g. word counts +map> // customers grouped by account ID +``` + +The key type `K` must be a primitive integer type, `char`, `bool`, or `string`. +The value type `V` may not contain a resource handle, `stream`, `future`, or `error-context`, +either directly or nested inside another type. + +A map is semantically equivalent to a list of key-value tuples, +but bindings generators represent it with the target language's native associative type +(for example, `HashMap` in Rust or `Map` in JavaScript). +If a key appears more than once, the last value wins. +See the [WIT specification][wit-spec-map] for the precise semantics. + +[wit-spec-map]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#:~:text=The%20map%20type%20is%20semantically%20equivalent + +Maps were added after WASI 0.2; +check that your toolchain supports them before using them. + ### Streams `stream` for any type `T` denotes an asynchronous, ordered sequence of values of type `T`. @@ -214,6 +251,9 @@ it can be returned from a function, accepted as a parameter, and passed across component boundaries the same way a primitive type is. A middle component can also hand a stream along without consuming it. +The element type can be omitted (`stream`) for a stream that carries no data +and only signals progress or completion. + Streams were added for WASI 0.3 and are most often paired with a [`future`](#futures) that signals when the stream has terminated; the stream-plus-future tuple shape appears throughout @@ -237,6 +277,7 @@ future> // a future that will resolve to either a r Like `stream`, `future` is a Canonical ABI value rather than a resource, so it travels across component boundaries the same way primitives do. +The value type can be omitted (`future`) for a future that only signals completion. Futures were added for WASI 0.3. They are similar to a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) in JavaScript @@ -357,6 +398,16 @@ As shown in the `blob` example, a resource can contain: a function returning a handle of the containing resource type. The constructor is declared with `constructor`. +A constructor normally has no written return type and implicitly returns the resource. +A constructor that can fail may instead declare a return type of the form `result`, +where `R` is the containing resource type: + +```wit +resource connection { + constructor(address: string) -> result; +} +``` + A method can be rewritten to be a function with a borrowed `self` parameter, and a constructor can be rewritten to a function that returns a value owned by the caller. @@ -432,7 +483,7 @@ lookup: func(store: kv-store, key: string) -> option; ``` To express a function that returns multiple values, -you can use any compound type (such as [tuples](#tuple) or [records](#record)). +you can use any compound type (such as [tuples](#tuples) or [records](#records)). ```wit get-customers-paged: func(cont: continuation-token) -> tuple, continuation-token>; @@ -506,7 +557,32 @@ The `canvas` interface uses the types `dimension` and `point` declared in the `t > For example, `use types.{dimension}` is legal but `use types.dimension` is not. This works across files as long as the files are in the same package (effectively, in the same directory). -For information about using definitions from other packages, see [the specification](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#interfaces-worlds-and-use). +The interface being used may appear before or after the interface that uses it, +but `use` relationships between interfaces must not form a cycle. + +Types can be renamed as they are imported, using `as`: + +```wit +interface canvas { + use types.{dimension as size, point}; + // ... +} +``` + +To use types from an interface in another [package](#packages), +give the fully qualified interface name, +optionally including the package version: + +```wit +interface handler { + use wasi:http/types.{request, response}; + use wasi:clocks/wall-clock@0.2.0.{datetime}; + // ... +} +``` + +To avoid repeating long qualified names, +you can also use a [top-level `use`](#top-level-use). ## Worlds @@ -543,11 +619,15 @@ with two exports, a `printer` interface and a `scan` function. The exported `printer` interface is defined in the same file. The imported `error-reporter` interface is also defined in the same file. From looking at the `error-reporter` interface, -you can see that When a world imports an interface, +you can see that when a world imports an interface, the full interface with types and function declarations needs to be provided, not just the name of the interface. +Within a world, every import name must be unique, and every export name must be unique. +Imports and exports are separate scopes, +so the same name can be both imported and exported. + ### Interfaces from other packages To import and export interfaces defined in other packages, @@ -579,6 +659,54 @@ world toy { } ``` +### Named imports and exports of interfaces + +`import wasi:keyvalue/store;` imports an interface under its own name, `wasi:keyvalue/store`, +so a world can import a given interface only once that way. +To import or export an interface under a name of your choosing, +write the name, a colon, and then the interface: + +```wit +world my-world { + import users: wasi:keyvalue/store; + import catalog: wasi:keyvalue/store; + export my-handler: wasi:http/handler; +} +``` + +Here `users` and `catalog` are two distinct imports, +each with the functions and types of `wasi:keyvalue/store`. +The import or export is named by the identifier before the colon, +and the component records which interface it implements. +This lets a component import or export the same interface more than once, +or give it a more descriptive name. + +> Write a space after the colon, as in `import a: b;`. +> The specification reserves `a:b` without a space for package names, +> so the unspaced form may be rejected by tools. + +### External IDs + +Identifiers in WIT are restricted to `kebab-case`, +so that bindings generators can turn them into idiomatic names in each language. +When the host needs a name that isn't valid kebab-case to resolve an import, +annotate the import or export with `@external-id`: + +```wit +world my-component { + @external-id("https://esm.unpkg.com/slugify@1.6.6") + import slugify: func(text: string) -> string; + + @external-id("user-db-prod:region-a") + import users: wasi:keyvalue/store; +} +``` + +Bindings still use the kebab-case name (`slugify`, `users`), +while the external ID tells the host what to supply. +External IDs can be any string, +and can also be placed on functions, types, and resource methods inside an interface. + ### Including other worlds You can `include` another world. @@ -598,6 +726,26 @@ world glow-in-the-dark-multi-function-device { As with `use` directives, you can `include` worlds from other packages. +If two included worlds import or export the same interface, +the result contains just one copy of it. +However, if they both have an import or export with the same _plain name_ +(a function, inline interface, or [named interface](#named-imports-and-exports-of-interfaces)), +the names conflict, and you must rename one of them with `with`: + +```wit +world world-one { import log: func(msg: string); } +world world-two { import log: func(msg: string); } + +world combined { + include world-one; + include world-two with { log as log2 } +} +``` + +`combined` imports both `log` and `log2`. +`with` can only rename plain names; +an import or export that uses an interface's own name, such as `import wasi:http/handler;`, can't be renamed. + ## Packages A package is a set of interfaces and worlds, @@ -655,4 +803,130 @@ an outgoing handler interface in `outgoing.wit`, and declares the package and defines a world that uses these interfaces in `http.wit`. +### Top-level `use` + +Fully qualified interface names, especially versioned ones, can get repetitive. +A `use` statement at the top level of a file gives an interface from another package a short name +that can be used anywhere in that file: + +```wit +package documentation:example; + +use wasi:http/types@0.2.0; +use wasi:http/incoming-handler@0.2.0 as handler; + +interface my-interface { + use types.{request, response}; +} + +world my-world { + export handler; +} +``` + +Without `as`, the short name is the last part of the interface name (here, `types`). +With `as`, you choose the name, which also lets you refer to +two versions of the same interface in one file: + +```wit +use wasi:http/types@0.2.0 as http-types-v1; +use wasi:http/types@0.3.0 as http-types-v2; +``` + +A top-level `use` only introduces a name; +the file means exactly the same thing as if the fully qualified names were written out. + +### Nested package definitions + +A WIT file can contain the definitions of other packages, +by giving a package declaration followed by a braced block: + +```wit +package documentation:app; + +world app { + import documentation:logging/logger; +} + +package documentation:logging { + interface logger { + log: func(msg: string); + } +} +``` + +The nested definitions supply the contents of other packages inline, +so that tools don't have to find them on the filesystem or in a registry. +A file can contain any number of nested package definitions, +and they don't replace the file's own `package` declaration. + +> Tools often use this form when they bundle a package together with its dependencies, +> for example when printing the WIT embedded in a component with `wasm-tools component wit`. + +## Feature gates + +Items in a WIT package can be annotated with _feature gates_, +which record when each item was added, whether it is still unstable, +and whether it is deprecated. +Gates can be placed on interfaces, worlds, types, functions, +imports and exports, and resource methods. + +```wit +package documentation:calc@0.2.2; + +@since(version = 0.2.0) +interface calc { + @since(version = 0.2.0) + add: func(a: s32, b: s32) -> s32; + + @since(version = 0.2.1) + sub: func(a: s32, b: s32) -> s32; + + @unstable(feature = calc-mul) + mul: func(a: s32, b: s32) -> s32; + + @since(version = 0.2.0) + @deprecated(version = 0.2.2) + add-one: func(a: s32) -> s32; +} +``` + +* `@since(version = X)` means the item was added in version `X` of the package. + A component targeting an earlier version can't use it. + Once an item is marked `@since`, it should not change incompatibly + (following [semantic versioning](https://semver.org/)). +* `@unstable(feature = F)` means the item is part of the unstable feature `F`, + which may change or be removed at any time. + Toolchains hide unstable items unless the feature is explicitly enabled. + For example, `wasm-tools` accepts `--features F` (or `--all-features`). +* `@deprecated(version = X)` means the item should no longer be used as of version `X`. + Tools may warn when a deprecated item is used. + Pair it with the `@since` gate that records when the item was added. + +An item should be gated with either `@since` or `@unstable`, not both. +A package that uses versioned gates must declare its own version +(for example, `package documentation:calc@0.2.2;`). +An item that refers to a gated item should itself be gated compatibly: +for example, a type alias to a `@since(version = 0.2.1)` type shouldn't be marked as available in `0.2.0`. + +A typical lifecycle is that a new item is first added under `@unstable` while its design settles. +When it stabilizes, the `@unstable` gate is replaced with `@since`, naming the release that includes it. +If the item is later deprecated, a `@deprecated` gate is added, +and the item can be removed in the next major version. +WASI packages use these gates extensively. + +## Unstable language features + +The Component Model is still evolving, +and some WIT syntax is specified but not yet enabled by default in tools. +These features may change before they are stabilized: + +* Fixed-length lists, written `list` (for example, `list`). +* Getters and setters, declared with `get` and `set` in place of `func`. +* Nested namespaces and packages in package names, such as `foo:bar:baz/quux`. +* The `error-context` type. + +The [Gated Features section of the specification](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Text.md#gated-features) +lists each feature and its current status. + > For a more formal definition of the WIT language, take a look at the [WIT specification](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md).