xmachines-runtime
The runtime of xmachines-rs: it runs XState v6 machine definitions, exported as JSON with XState's machineToJSON / machineConfigToJSON (MachineJSON), with the same semantics as XState, and with the guards, actions, delays, computed values and child actors a machine references written in Rust.
This is an independent project, not an official Stately package. It targets xstate@6.0.0-beta.3.
When to use it
Use xmachines-runtime to run a machine inside a Rust program: load its MachineJSON, register the implementations it references, start an actor and drive it with events and the time. Everything else in xmachines-rs is built on it: typed machines from xmachines-build, the WASI components, the Node.js addons and the Kafka service.
- XState-true. The statechart algorithm (
core) is a port of the structural half of XState's own, written against aHosttrait; XState's core test suite runs on it, and differential tests compare every step of fixtures, XState's examples and random machines with XState. - No names on the hot path. Loading validates the definition as strictly as XState's
machineFromJSON(unknown keys and unsupported features are errors with their JSON path) and compiles it: states into a flat arena in document order, targets to indices, every guard, action, delay and code node bound to its implementation once, and event descriptors interned. - The host drives time. The runtime never reads a clock or sleeps:
sendandadvancetake the current time in milliseconds, andnext_deadlinesays when to come back. The crate builds forwasm32-unknown-unknownand has nounsafe, threads or I/O. - XState's actor system.
Systemis a port of XState's pure system API (initialSystemTransition,systemTransition,advanceSystemTime): invoked and spawned children,systemIds, external effects that wait for the host, and timers.Actorwraps a system for its root, the way XState's live actor does. - XState's snapshots.
Actor::persistandActor::restoreread and write XState'sSystemSnapshotJSON, so a running machine moves between this runtime and XState's.Migrationrewrites persisted snapshots for a newer version of a machine.
Example
A machine with a delayed transition, a custom action with params and a guard with params, implemented in Rust:
use ;
use ;
use ;
Typed contexts and events work the same way: any C: Serialize + DeserializeOwned + Clone and any event type implementing EventType (usually a serde enum tagged by type, which xmachines-build generates); serde_json::Value implements both for untyped machines.
What a machine can reference
The Registry holds what the host supplies, by the names a definition uses:
| Registry method | For |
|---|---|
guard, guard_with::<P>, try_guard_with::<P> |
{ "type": name, "params"? } guards; params deserialize into P once, at load time |
action, action_with::<P>, try_action_with::<P> |
custom actions: effects that run after the step, with their params only |
delay, delay_ms, try_delay |
named delays in after |
actor |
the logic of an invoke.src or a spawn: another Machine, step::async_logic, or any system::Logic |
expr, context, context_with |
XState's code nodes in the rust language, { "@code": name, "@lang": "rust" } |
evaluator |
the code nodes of another language (an Evaluator, or HostEvaluator for one in another runtime) |
The try_ methods take implementations that can fail, as one that throws in XState: a guard's or a delay's Err fails the step (the actor keeps its snapshot from before it, in the error status), and an action's fails the actor after its step (the step's snapshot, in the error status; the effects after it do not run). Expressions fail the step by ExprArgs::fail; the root context starts the actor in error by ContextArgs::fail. An Actor handles errors as XState's live actor: actions run inside their steps, an active state's onError recovers from an error (xstate.error.execution), an actor that fails stops its children, a child reports xstate.error.actor to its parent, and a root whose start throws starts in error (with only its error, value() and context() being None, when its initial step threw, as XState's live actor). A System fails the call, as XState's pure system API does. An exception is kept as XState persists an Error: { "@error": "Error", "message" }.
Machine::references lists everything a definition needs from the host, with JSON paths and params, without loading it.
Features
op-counts: counts the runtime's operations per thread (ops), for the cost model ofexamples/cost_model.rs. Off by default; without it every count compiles to nothing.
Contracts
- Input: XState's
MachineJSON, with code as XState's code nodes ({ "@code" | "@expr": source, "@lang": lang }). What is not supported yet is rejected at load time with its JSON path: code nodes in a language with no registered evaluator, andspawn/sendToas JSON actions until XState has its own JSON form for them (Rust code has them throughEnq). - Snapshots: XState's
SystemSnapshotJSON (System::to_json,System::from_json), described bypersisted.schema.jsonof the WIT packagexmachines:runtime;System::compact_viewwrites a compact form that restores the same. - Hosts in other languages:
hostparses the options and writes the values that the WASI components and the Node.js addons pass as JSON text, andHostEvaluatoris the JSON form of an evaluator in another runtime (the WITevaluateimport).
Related crates
xmachines-build: typed Rust modules from machine bundles at build time.xmachines-eval-quickjs: JavaScript code nodes on an embedded QuickJS.xmachines-codec: codecs of events and snapshots.xmachines-runtime-wasiandxmachines-runtime-node: this runtime as a WASI component and as a Node.js addon.xmachines-docs: the guides.
License
MIT