The Component Model and WIT

32.1 Why Components?

Core Wasm modules only speak in numbers and memory, and each language invents its own ABI.

The Component Model adds:

Rich interface types (strings, lists, records, variants, options, results, resources) via WIT

A standard Canonical ABI for lifting/lowering values between components

Composition: link components written in different languages

Shared-nothing isolation: each component has its own memory Virtualization of interfaces

WASI 0.2+ is defined in terms of components.

32.2 WIT (Wasm Interface Type) Language

bool, s8..s64, u8..u64, f32, f64, char, string

list<T>, tuple<...>, option<T>, result<T, E> record, variant, enum, flags

Handles to stateful objects with methods (own/borrow semantics) future<T>, stream<T> Async (WASI 0.3)

interface, world, package, use, include

// wit/calculator.wit
package example:[email protected];

interface types {
  record point { x: f64, y: f64 }
  variant shape {
    circle(f64),
    rectangle(tuple<f64, f64>),
  }
  enum unit { metric, imperial }
  flags permissions { read, write, execute }
  type user-id = u64;
}

interface ops {
  use types.{point, shape};
  area: func(s: shape) -> f64;
  midpoint: func(a: point, b: point) -> point;
  parse-number: func(input: string) -> result<f64, string>;
  find: func(items: list<string>, needle: string) -> option<u32>;

  resource accumulator {
    constructor(start: f64);
    add: func(value: f64);
    total: func() -> f64;
  }
}

world calculator {
  import wasi:logging/[email protected];   // what the component needs
  export ops;                                  // what it provides
}

32.3 Worlds

A world describes a component's imports (what it needs from the host or other components) and exports (what it provides). Examples: wasi:cli/command, wasi:http/proxy, or your app-specific plugin world.