Skip to content

Latest commit

 

History

History
584 lines (489 loc) · 24.4 KB

rontolisp-wit-export.md

File metadata and controls

584 lines (489 loc) · 24.4 KB

rontolisp:wit-export

(rontolisp:wit-export "world.wit" :world name)

Declares that the program implements a WIT world. The world's export items are checked against the program's top-level defuns at compile time and lowered into the rontolisp:wasm-export directives they stand for, so the boundary types are never written by hand and the .wit file and the compiled component cannot drift apart. The WIT is the single source of truth: the world is the program's export list (a hand-written rontolisp:wasm-export alongside it is an error), and for a world of flat types that defines no type of its own the emitted component is byte-identical to the one those hand-written directives would have produced. It is a compile-time directive, not an ordinary function: on the interpreter and JVM backends it runs the same contract check and then returns nil, so the same source runs on every backend. See Implementing a WIT World for the full guide.

Because the directive reads a .wit file from disk, the example is shown statically:

// wit/greeter.wit
package example:greeter;

world greeter {
  /// Greet someone by name.
  export greet: func(who: string) -> string;
}
;;; greet.lisp -- the directive comes last: on the interpreter it sees only the
;;; functions defined so far.
(defun greet (who)
  (concatenate 'string "Hello, " who "!"))

(rontolisp:wit-export "wit/greeter.wit" :world greeter)
rontolisp greet.lisp --component -o greet.wasm
wasmtime run --invoke 'greet("world")' greet.wasm
# "Hello, world!"

Arguments

  • The WIT file path, as a string. A relative path resolves against the directory of the source file that names it, like load. It may name a WIT package directory, read as rontolisp:wit-import reads one.
  • :world — the world to implement, as a bare symbol (spelled the way WIT spells it) or a string. It may be omitted when the file declares exactly one world; when the file declares several, one must be named. For a package directory, the worlds counted and searched are the package's own, not those of its deps/.
  • :reactor — t makes the program a reactor, whose entry points are the world's exports: a --component build lifts no wasi:cli/run, and the first call of an export runs the program's top level, once, before its own body, so an export a host calls directly reads what the top level set (A reactor). Without it a host that calls an export never runs the top level. A WASI command module (-o out.wasm alone, or --native) refuses it; --no-wasi and --no-gc outputs are reactors already. The interpreter and the JVM ignore it.
  • :octets — t hands a list<u8> parameter over as the octets it holds, an (unsigned-byte 8) vector, instead of the string they decode to as UTF-8 (rontolisp:wit-import's option, for the other direction).
  • :envelope — t says a function whose WIT result is a result answers it as (:ok . value) or (:error . value) itself, instead of the ok value with rontolisp:wit-error signalled for the error arm (Compound types). The Clojure front end's rontolisp.wit/export and the Java front end's @WitExport pass it.

Everything else comes from the world: rontolisp:wasm-export's :params, :param-names, :returns and :async are all filled in from it, so the defuns carry no boundary types at all.

Supported WIT types

WIT type Boundary type Lisp value
s8 s16 s32 :s8 :s16 :s32 an integer
u8 u16 u32 :u8 :u16 :u32 an integer
s64 u64 :s64 :u64 an integer; a u64 value of 2^63 or more traps at the boundary
f64 :float a float
bool :bool t or nil
string :string a string
(no result) :void the function's value is discarded

The whole fixed-width integer family crosses, so the canonical component-model tutorial world compiles unedited:

// wit/adder.wit
package docs:adder@0.1.0;

interface add {
  add: func(x: u32, y: u32) -> u32;
}

world adder {
  export add;
}

Each type carries its own range exactly, or the call traps — a negative returned through u32 is refused rather than delivered as 4294967295. The component model has no integer subtyping, so this is not cosmetic: a component that lifted u32 as s32 would be rejected against its own world by wasm-tools component targets, by jco, and by any bindgen-based host.

An async func in the world lifts the export with :async t, so blocking is always legal inside it: I/O inside a sync export usually works too (the asynchronous built-ins complete without blocking when the host accepts immediately), but a host that reports BLOCKED would make it trap, and the async lift removes that residual risk — the WIT states which exports are async rather than leaving it to be guessed.

Compound types

A component's export (--component) carries the other WIT types too, each as the Lisp value rontolisp:wit-import gives it:

WIT type Lisp value
record a keyword plist, (:x 1 :y 2)
enum a keyword, :green
flags a list of the keywords of the labels that are set, (:read :write)
variant the case's keyword, or (keyword . payload) when the case carries one
tuple a list
list<T> a list
list<u8> a string, or an (unsigned-byte 8) vector; a parameter arrives as a string, or as the vector under :octets t
option<T> the value, or nil for none
result<T, E> as the function's result the value is the ok arm; the error arm is a rontolisp:wit-error signalled with the payload
result<T, E> anywhere else (:ok . value) or (:error . value)
f32 char a float, a character
// wit/shapes.wit
package example:shapes@0.1.0;

interface geometry {
  record point { x: s32, y: s32 }
  variant shape { circle(f64), rect(point), dot }

  flip: func(p: point) -> point;
  area: func(s: shape) -> result<f64, string>;
}

world shapes {
  export geometry;
}
(defun flip (p) (list :x (getf p :y) :y (getf p :x)))

(defun area (s)
  (cond ((eq s :dot) (error 'rontolisp:wit-error :payload "a dot has no area"))
        ((eq (car s) :circle) (* 3.0d0 (cdr s) (cdr s)))
        (t (float (* (getf (cdr s) :x) (getf (cdr s) :y)) 1.0d0))))

(rontolisp:wit-export "wit/shapes.wit" :reactor t)
rontolisp shapes.lisp --component -o shapes.wasm
wasmtime run --invoke 'flip({x: 1, y: 2})' shapes.wasm
# {x: 2, y: 1}
wasmtime run --invoke 'area(dot)' shapes.wasm
# err("a dot has no area")

A function whose answer is a result returns the ok value and signals rontolisp:wit-error for the error arm, as a provider does; any other condition it leaves uncaught traps the call. A value the declared type cannot state traps too: a u8 field given 300, a keyword that names no case or no label, a list<T> answered with anything but a list.

The component declares every type its world and its exported interfaces define, an alias of a flat type (type id = u32) among them, and exports an interface of types alone as those types; --no-gc --component does the same for the flat types it carries. The component is therefore the world's (wasm-tools component targets world.wit out.wasm accepts it), --emit-wit prints the types back, and another component can import the interface (examples/wit/shapes composes two).

A type taken from another interface with use is declared as the using scope's own, under the name the clause gives it, and the component imports nothing for it: two value types of one shape are one type in the component model. A type the copied one holds comes with it -- an alias as what it names, a record, a variant or an enum under its own name:

interface geometry {
  record point { x: s32, y: s32 }
  record line { a: point, b: point }
}

interface shapes {
  use geometry.{line};            // declares line, and point with it
  length: func(l: line) -> f64;
}

A world's types are imports of the component, so a use in the world names such a type itself (use geometry.{line, point};). A name the scope already gives another type is refused at the clause; rename it there (point as corner).

Not carried, each a compile error at the export's WIT line: a stream, a future, an error-context (which the component model puts behind a feature that a host enables with a flag) and a handle of a resource the world declares (Resources). An async func carries the same types as any other. A WASI module (-o out.wasm alone) and --no-gc export the flat types alone and refuse a world with any other.

A --no-wasi module exports them too. Its core export takes the canonical ABI's core values, and the --emit-js-glue file lowers and lifts them: each export is an entry point under its label in camelCase, called with the JavaScript values a host import is handed (an object for a record, { tag, val } for a variant, undefined for none, an object of booleans for flags, a BigInt for 64 bits). A result answer returns its ok value and throws the error arm as the glue's WitError. --reentrant refuses such an export.

import { instantiate, WitError } from "./shapes.js";

const lisp = instantiate(new WebAssembly.Module(bytes));
lisp.flip({ x: 1, y: 2 }); // { x: 2, y: 1 }
try {
  lisp.area({ tag: "dot" });
} catch (error) {
  if (error instanceof WitError) console.log(error.payload); // a dot has no area
}

Resources

A resource an exported interface declares is exported by a component (--component, with or without --no-wasi, and --no-gc --component over integers: On the scalar backend) and by a --no-wasi module to its glue (On a --no-wasi module). Its functions are defuns named as rontolisp:wit-import binds them on the other side:

WIT defun Arguments
constructor(...) of counter counter-new the constructor's
a method add: func(...) counter-add the resource, then the method's
merge: static func(...) counter-merge the function's
(a handle is released) counter-drop, optional the resource

The value the constructor answers is the resource: any Lisp value -- a struct, a cons, a closure. The host holds a handle of it, and wherever the WIT says counter, own<counter> or borrow<counter> -- a parameter, a result, a record's field, a list's element -- the function takes or answers that value.

// wit/counters.wit
package example:counters@0.1.0;

interface registry {
  resource counter {
    constructor(name: string, start: u32);
    add: func(by: u32) -> u32;
    merge: static func(a: counter, b: borrow<counter>) -> counter;
  }

  total: func(counters: list<borrow<counter>>) -> u32;
}

world counters {
  export registry;
}
(defstruct counter name value)

(defun counter-new (name start) (make-counter :name name :value start))

(defun counter-add (self by) (incf (counter-value self) by))

(defun counter-merge (a b)
  (incf (counter-value a) (counter-value b))
  a)

(defun total (counters) (reduce #'+ counters :key #'counter-value))

(rontolisp:wit-export "wit/counters.wit" :reactor t)
  • A borrowed handle (borrow<counter>, a method's own resource) lends the value for the call. An owned one (counter, own<counter>) hands it over: the caller's handle is gone, and the function keeps the value, changes it or answers it again. Every owned handle answered is a new handle of the value.
  • counter-drop, when the program defines it, is called with the value each time a holder releases a handle. It is not called for a handle handed over to an export, which released nothing. A value with no handle left is ordinary garbage.
  • A second exported interface may name the resource in a use clause (use registry.{counter};) and take or answer it: it is the same resource, and the component exports the declaring interface first. A type of another interface that holds a handle is refused (name the resource in the clause and define the type where it is used).
  • Two exports that one defun would implement (a method counter.add and a function counter-add) are refused; under a :names table a resource's function is named by its member name ("counter-add").

A resource the component imports

A resource of an interface the world does not export, named in a use clause of an exported interface or of the world itself, is one the component imports: the component imports that interface for it, and a handle is the integer rontolisp:wit-import gives one. The program calls the resource through a wit-import of the interface, which shares the one import.

// wit/auditor.wit
package example:counters@0.1.0;

interface registry {
  resource counter {
    add: func(by: u32) -> u32;
  }
}

interface audit {
  use registry.{counter};

  bump: func(c: borrow<counter>, by: u32) -> u32;
}

world auditor {
  import registry;
  export audit;
}
(rontolisp:wit-import "wit/auditor.wit"
                      :interface "example:counters/registry"
                      :package reg)

(defun bump (c by) (reg:counter-add c by))

(rontolisp:wit-export "wit/auditor.wit" :reactor t)

Composed with the component above, which exports registry, bump adds to a counter that component keeps.

  • A borrowed handle is lent for the call: the export releases it once the function has answered (the component model traps a call that returns still holding one), so the program does not drop it. An owned one is the program's: it keeps it, hands it on, or releases it with the binding's counter-drop. Every owned handle answered goes to the caller.
  • An exported interface's use clause imports the interface whether or not a function names the resource: the interface exports the type. A clause of the world imports it only where a function of the world names a resource it brings.
  • Refused at the WIT line: a resource the world itself declares (its functions are imports of the world, which no wit-import binds: declare it in an interface and name it with use), and a world's own use of a resource of an interface the world exports (the clause imports that interface, so a function of the world would take the imported resource, not the world's own: export the function from an interface).
  • --component --no-wasi refuses such a world, since that component imports nothing, and so does any component for a resource of an interface it imports as part of its own WASI surface (wasi:filesystem/types's descriptor).
  • examples/wit/resource's reporter.lisp implements an interface of this kind, composed between the command and the component that keeps the counters.

On the interpreter and the JVM the directive checks these functions like any other and exports nothing; bound as the provider of the interface, they answer a wit-import of it in process, a handle there being the value itself. examples/wit/resource runs one program both ways.

A WASI module (-o out.wasm alone) and a --no-gc module without --component refuse an interface that declares a resource, and every core module one that uses a resource the component would import. The Java front end exports a resource as a class (Resources of an export), the Clojure one as vars named like these defuns (Resources).

On a --no-wasi module

The --emit-js-glue file makes the resource a class, named in upper camel case, as jco names it: the constructor makes an object, a method is called on it, a static function on the class. Each owned handle the module answers is a new object.

const { Counter } = instantiate(module);
const apples = new Counter("apples", 3);
apples.add(4); // 7
const both = Counter.merge(apples, new Counter("pears", 10)); // takes the first over
both[Symbol.dispose](); // counter-drop runs, as for a component's release
  • An object handed over as an owned handle, and one disposed of, is no handle any more: using it again throws a TypeError. A borrowed one stays the host's.
  • [Symbol.dispose]() (and using) releases the handle; so does the garbage collector, through a FinalizationRegistry, for an object nobody released.
  • The class's functions are entered as a plain call is, so a module whose exports may suspend (an import that may suspend, --host-timer) refuses the glue.

On the scalar backend

--no-gc --component exports a resource whose functions take and answer the flat types. Its values are numbers, so the resource is the integer the constructor answers, and the handle carries that integer: the component keeps no value behind it.

// wit/cells.wit
package example:cells@0.1.0;

interface cells {
  resource cell {
    constructor(v: u32);
    get: func() -> u32;
    plus: static func(a: cell, b: borrow<cell>) -> cell;
  }
  live: func() -> u32;
}

world provider {
  export cells;
}
(defvar *live* 0)

(defun cell-new (v) (setq *live* (+ *live* 1)) v)
(defun cell-get (self) self)
(defun cell-plus (a b) (+ a b))
(defun cell-drop (c) (setq *live* (- *live* 1)))
(defun live () *live*)

(rontolisp:wit-export "wit/cells.wit")
  • The integer is a u32: a constructor answering one outside 0 to 4294967295 traps. A method and a function taking a borrowed handle receive the integer.
  • An owned handle received is the integer too, and the export releases the handle without calling cell-drop: the value moved to the function (plus's a). cell-drop is called with the integer when a holder releases a handle; a resource whose program defines no cell-drop has no destructor.
  • A handle crosses alone, as a parameter or a result. One inside a list, an option, a record or any other compound type is refused with the compound type, and a borrowed handle in a result as on the default backend.
  • A program that prints is refused: every export of a printing --no-gc --component is an async func, which a constructor cannot be. Under --no-wasi printing writes nothing, and the world compiles.

A resource the component imports is taken the same way as on the default backend: the handle is the integer the wit-import binding takes, a borrowed one released once the function has answered. The binding's constructor, methods, static functions and cell-drop take and answer the flat types and the handles of the interface's own resources.

// wit/cells.wit, beside the interface above
interface keep {
  use cells.{cell};
  twice: func(c: borrow<cell>) -> u32;
  spend: func(c: cell) -> u32;
}

world keeper {
  import cells;
  export keep;
}
(rontolisp:wit-import "wit/cells.wit" :interface "example:cells/cells" :package c)

(defun twice (c) (* 2 (c:cell-get c)))
(defun spend (c)
  (let ((v (c:cell-get c)))
    (c:cell-drop c)
    v))

(rontolisp:wit-export "wit/cells.wit" :world keeper)

Composed with the provider above, twice reads a cell it is lent, and spend reads and releases one it is handed.

What it checks

Every violation is a compile error naming the WIT file and the line of the offending export:

  • an export the world declares with no matching defun — wit/greeter.wit:5: export 'greet' has no matching (defun greet ...) in the program
  • an arity mismatch — wit/greeter.wit:5: export 'greet' declares 1 parameter(s), but (defun greet ...) takes 2 (an exported function takes required parameters only: &optional / &rest / &key are rejected)
  • a WIT type the export does not carry: beyond the flat types on a WASI module or under --no-gc (and a handle, alone, on --no-gc --component), and a stream, a future, an error-context or a handle of a resource the world declares anywhere
  • a resource's function with no matching defun (counter-new, counter-add) or another arity, a counter-drop that does not take the one value, an exported interface that declares a resource on a WASI module or a --no-gc module, or that names one the component imports where it is no component, and a printing --no-gc --component program whose world exports a resource
  • a use clause of an exported interface that names a type that holds a handle; one whose type the scope has no name for (a world's) or whose name the scope gives another type; and one of the world naming a resource of an interface the world exports, where a function of the world takes it
  • an export name that is not a component-model label (lower-kebab-case words), a duplicate export (refused by the WIT reader first, in wasm-tools component wit's words: export `a` conflicts with prior func of same name), or the reserved name run (the component's wasi:cli/run entry point)
  • a world with no exports, a :world the file does not declare, or an omitted :world when the file declares several

Because the world is the export list, so is mixing it with the hand-written form: a rontolisp:wasm-export in a program that also has a rontolisp:wit-export, and a rontolisp:http-handler together with a world (a serve-mode component exports only wasi:http/handler@0.3.0).

Limitations

  • Only the world's export side is a contract. import items are ignored (a component's WASI imports come from the build, not from the world), and an inline import name: func(...) is rejected rather than silently dropped — the functions a program calls are bound from an interface with rontolisp:wit-import, or declared by hand with rontolisp:wasm-import (both Preview 1 only). The component you get therefore has a much larger type than the world you wrote: the 6-line world above compiles to a 149-line component type (ten wasi:* imports plus export wasi:cli/run), and calling rontolisp:fetch inside greet silently adds five more. --emit-wit is how you see it.
  • A world exports freestanding functions or an interface defined in the same file: export add; referencing an in-file interface add { ... }, or an inline export ops: interface { ... }, is implemented member by member and produces a real docs:adder/add instance export (see Exporting an interface). An export naming an interface the file does not define (a bare wasi:* reference) is still an error.
  • Two exports of one label -- a function of the world and an interface's, or two interfaces' -- are both the defun the label spells, and a component exports each: wasmtime run --invoke 'docs:adder/add.add@0.1.0(20, 22)' calls the interface's, a bare add(20, 22) the world's. A core module (Preview 1, --no-wasi) exports a function by its label alone and refuses them.
  • :s-expr has no WIT spelling, so an export carrying an arbitrary s-expression still needs a hand-written rontolisp:wasm-export — and therefore a program without a world.
  • On the interpreter the directive is an ordinary form evaluated in order, so it sees only the functions defined so far: put it at the end of the file. (The compile path collects every top-level defun first, so there the position does not matter.)
  • Adding --emit-wit writes the component's real type back out, and its export lines reproduce the world handed in, parameter names included — but by construction, not by coincidence: the world is what those lines are derived from, so they cannot disagree with it. Emitting is worth it for the import side, not as a check on your program (that is wit-export's own job, on every backend). Two deliberate differences from the input file: the /// doc comments are gone (a component's type does not store them), and the emitted world is always package root:component; world root.