Skip to main content

Crate xmachines_machine_wasi

Crate xmachines_machine_wasi 

Source
Expand description

§xmachines-machine-wasi

Typed WASI 0.2 components for one machine each: the machine’s guards, actions, delays and invoked actors are Rust compiled into the component (no call into the host per guard), and its events are checked against the machine’s generated types at the boundary.

§When to use it

Use it when a machine and its implementations are known at build time and should ship as one component: the host imports nothing but WASI and only drives actors (and settles the invoked work the machine leaves to it). The generic component of xmachines-runtime-wasi loads any definition at run time and calls the host for every guard and action; this one is built per machine. The same machine builds a Node.js addon with xmachines-machine-node.

§Example

A cdylib crate built for wasm32-wasip2 implements TypedMachine of xmachines-machine and calls export_machine!:

use serde_json::Value;
use xmachines_machine::TypedMachine;
use xmachines_runtime::{Machine, Registry};

struct Toggle;

impl TypedMachine for Toggle {
    type Context = Value;
    type Event = Value;
    const DEFINITION: &'static str = r#"{
        "id": "toggle", "initial": "off",
        "states": { "off": { "on": { "toggle": { "target": "on" } } }, "on": {} }
    }"#;
    const SCHEMAS: &'static str = "{}";

    fn machine() -> Result<Machine<Value, Value>, String> {
        Machine::from_json_str(Self::DEFINITION, &Registry::new()).map_err(|e| e.to_string())
    }
}

xmachines_machine_wasi::export_machine!(Toggle);

With codegen, Context, Event, DEFINITION, SCHEMAS and machine() come from the generated module, and the implementations are the generated trait’s (examples/checkout-wasi):

cargo build -p checkout-wasi --target wasm32-wasip2 --release   # about 1.1 MB

The component-model exports are compiled only for wasm32; a native build type-checks the crate, as above.

§Contract

The world xmachines:machine/machine of the WIT package xmachines:machine@0.3.0 (wit/machine.wit) exports:

  • info: the machine’s id ((machine) when the definition has none, the runtime’s id for such a machine and its root actor), version, definition and schemas (the bundle’s JSON Schemas);
  • actors: the machine and actor resources of xmachines:runtime, the machine loaded without a definition (load(options), with maxSteps, maxTimers and observeSystemEmitted; start(options, now); restore(snapshot); each error the contract’s { "code", "message" }, WORKFLOW_INVALID_INPUT or WORKFLOW_INVALID_MACHINE of a load, WORKFLOW_MACHINE_FAILED or WORKFLOW_INVALID_INPUT of a start, WORKFLOW_INVALID_SNAPSHOT of a restore): the component holds its one machine, compiled once per instance, and a machine resource adds its load options. The actor is the same (apply, whose error is a rejection { "rejected": { "message", "data"? } } or a fault { "code", "message" }; can, whether the root would select a transition for the event, an empty one among them; persist, take-logs, take-system-emitted, stop); an event input that is not an event of the machine refuses the chunk as WORKFLOW_INVALID_INPUT.

Custom actions are compiled in, but for the host actions the machine’s bundle names (hostActions): the registry xmachines-build generates leaves them to the host, as work of kind action it settles done.

Values cross as JSON text, since WIT has no maps, no recursive types and no unknown, which JSON Schemas of contexts and events use freely, and the events usually arrive as JSON anyway (Kafka records, HTTP bodies). The package’s own JSON Schemas (load options, info.schemas) are in wit/schemas/; the values its actor shares with xmachines:runtime use that package’s.

§Runs: the record protocol

export_machine! also exports xmachines:workflow/machine-runs@0.1.0: runs of the component’s machine on the record protocol, run.open(options) (no definition: the machine is compiled in), apply(chunk) with its records out, can, checkpoint, as the generic runtime’s runs. An event that is not an event of the machine is a fault, WORKFLOW_INVALID_INPUT.

§Pins

WASI 0.2 (wasm32-wasip2) and wit-bindgen 0.63.

§License

MIT

Macros§

export_machine
Exports info, actors and xmachines:workflow/machine-runs for a TypedMachine.

Structs§

ActorResource
The actor resource of a TypedMachine: a JsonActor behind the resource’s calls. A component instance is single-threaded, and its implementations are Rust that cannot call back into it, so the cell is never borrowed twice.
MachineResource
The machine resource of a TypedMachine: a JsonMachine, the machine compiled once per component instance with the load options of this resource.
RunResource
The run resource of xmachines:workflow/machine-runs for a TypedMachine: a run of its machine, compiled once per component instance, a chunk at a time, records out.