TExcelTAD
Home › WebAssembly › The Component Model

The Component Model

Core WebAssembly modules only exchange numbers. Every language invents its own convention for strings, lists and records (you've seen several: pointer + length, zero-terminated, UTF-16 with a header). That makes it hard for a Rust module to call a Go module, or for a runtime to host plug-ins written in any language.

The Component Model fixes this by adding a layer on top of core modules:

Concept What it is
WIT (Wasm Interface Type) an IDL for describing interfaces with rich types: string, list<T>, record, variant, option, result, resource
Component a wrapper around one or more core modules, whose imports and exports are typed by WIT
Canonical ABI the precise rules for how WIT values are laid out in linear memory and passed as core values
World the full set of imports and exports a component has, e.g. wasi:cli/command or wasi:http/proxy

WASI 0.2 and later are defined entirely as WIT interfaces.

A WIT interface

wit
package example:shop@0.1.0;

interface pricing {
  record item {
    name: string,
    cents: u32,
    quantity: u32,
  }

  variant discount {
    none,
    percent(u8),
    fixed(u32),
  }

  total: func(items: list<item>, discount: discount) -> result<u32, string>;
}

world checkout {
  export pricing;
  import wasi:clocks/wall-clock@0.2.0;
}

Bindings generators (wit-bindgen for Rust, C, Go, …; componentize-py for Python; jco for JavaScript) turn this into native types in each language. A Rust implementation just writes fn total(items: Vec<Item>, discount: Discount) -> Result<u32, String>.

Lifting and lowering

Underneath, the component still runs core Wasm. The canonical ABI says how to lower a high-level value into core values and memory (for a call into the component) and lift it back (for results). A string is lowered to (ptr, len) of UTF-8 bytes allocated with the component's exported cabi_realloc. A list<item> becomes (ptr, len) pointing at an array of records, each laid out like a C struct. The JavaScript below lowers a list of item records the way the canonical ABI specifies:

js · try it yourself
// record item { name: string, cents: u32, quantity: u32 }
// layout of one record: name.ptr u32 @0, name.len u32 @4, cents u32 @8, quantity u32 @12 -> 16 bytes, align 4
const memory = new ArrayBuffer(256);
const view = new DataView(memory);
let heap = 8;
function cabiRealloc(size, align) {             // what the component exports
  heap = Math.ceil(heap / align) * align;
  const ptr = heap;
  heap += size;
  return ptr;
}

function lowerString(s) {
  const bytes = new TextEncoder().encode(s);
  const ptr = cabiRealloc(bytes.length, 1);
  new Uint8Array(memory, ptr, bytes.length).set(bytes);
  return [ptr, bytes.length];
}

function lowerItems(items) {
  const base = cabiRealloc(items.length * 16, 4);
  items.forEach((item, i) => {
    const at = base + i * 16;
    const [p, n] = lowerString(item.name);
    view.setUint32(at, p, true);
    view.setUint32(at + 4, n, true);
    view.setUint32(at + 8, item.cents, true);
    view.setUint32(at + 12, item.quantity, true);
  });
  return [base, items.length];                  // the two core i32 arguments
}

const [ptr, len] = lowerItems([
  { name: 'tea', cents: 450, quantity: 2 },
  { name: 'scone', cents: 325, quantity: 1 },
]);
console.log('core args:', ptr, len);

// the component's core code would then read the records like this:
let total = 0;
for (let i = 0; i < len; i++) {
  const at = ptr + i * 16;
  const name = new TextDecoder().decode(new Uint8Array(memory, view.getUint32(at, true), view.getUint32(at + 4, true)));
  const line = view.getUint32(at + 8, true) * view.getUint32(at + 12, true);
  console.log(name.padEnd(6), line);
  total += line;
}
console.log('total', total);
core args: 8 2
tea    900
scone  325
total 1225

You never write this by hand: the generated bindings do it on both sides, and the runtime can even copy values directly between two components' memories, because each side's layout is known.

The core view

From the inside, a component's core module simply exports functions that take those flattened arguments. For total, the core signature would be roughly (param $items_ptr i32) (param $items_len i32) (param $disc_tag i32) (param $disc_val i32) (result i32), with the result<u32, string> written to a return area in memory. Here's the core logic of such an export, reading the records lowered above:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (memory 1)
  ;; two records at 16: {name ptr/len, cents, quantity}; their names live elsewhere
  (data (i32.const 16) "\00\00\00\00\03\00\00\00\c2\01\00\00\02\00\00\00")   ;; tea: 450 x 2
  (data (i32.const 32) "\00\00\00\00\05\00\00\00\45\01\00\00\01\00\00\00")   ;; scone: 325 x 1

  ;; discount tag: 0 = none, 1 = percent(val), 2 = fixed(val)
  (func $total (param $ptr i32) (param $len i32) (param $tag i32) (param $val i32) (result i32)
    (local $sum i32)
    (block $done (loop $next
      (br_if $done (i32.eqz (local.get $len)))
      (local.set $sum (i32.add (local.get $sum)
        (i32.mul (i32.load offset=8 (local.get $ptr)) (i32.load offset=12 (local.get $ptr)))))
      (local.set $ptr (i32.add (local.get $ptr) (i32.const 16)))
      (local.set $len (i32.sub (local.get $len) (i32.const 1)))
      (br $next)))
    (block $none (block $fixed (block $percent
      (br_table $none $percent $fixed (local.get $tag)))
      ;; percent
      (return (i32.sub (local.get $sum)
        (i32.div_u (i32.mul (local.get $sum) (local.get $val)) (i32.const 100)))))
      ;; fixed
      (return (i32.sub (local.get $sum) (local.get $val))))
    (local.get $sum))

  (func (export "main")
    (call $log (call $total (i32.const 16) (i32.const 2) (i32.const 0) (i32.const 0)))
    (call $log (call $total (i32.const 16) (i32.const 2) (i32.const 1) (i32.const 10)))
    (call $log (call $total (i32.const 16) (i32.const 2) (i32.const 2) (i32.const 225)))))
1225
1103
1000

The variant became a tag (0, 1, 2) plus a payload, dispatched with br_table.

Tooling

bash
cargo component new pricing --lib       # Rust component project
cargo component build --release
wasm-tools component wit target/wasm32-wasip1/release/pricing.wasm   # print its WIT
wasm-tools compose app.wasm -d pricing.wasm -o composed.wasm         # link components
wasmtime serve http-handler.wasm        # run a wasi:http/proxy component
npx jco transpile pricing.wasm -o out/  # run a component in Node/browsers

Why it matters

  • Language interop: a component written in Rust can be used from Python, JavaScript or Go with natural types.
  • Safe composition: components don't share memory; each has its own, and values are copied across the boundary.
  • Portable plug-ins: hosts like Wasmtime, Spin, wasmCloud and Envoy accept components for a given world.

Browsers don't run components natively yet; jco transpiles them into core modules plus JS glue.

Test yourself

What is WIT?

  1. A Wasm instruction
  2. An interface definition language for component imports and exports
  3. A JavaScript test framework
  4. A binary section

What does the canonical ABI define?

  1. CPU instruction encodings
  2. How high-level WIT values are lowered into core Wasm values and memory and lifted back
  3. The JavaScript API
  4. Compression of .wasm files

Do two components share one linear memory?

  1. Yes, always
  2. No, each has its own memory and values are copied across the boundary
  3. Only if written in the same language
  4. Only in browsers