Skip to main content

byteflow/vm/
result.rs

1// Docs ratchet: public items here predate `missing_docs`. Delete this
2// allow once they are documented; new files get the lint by default.
3#![allow(missing_docs)]
4
5use std::sync::Arc;
6use std::time::Duration;
7
8use crate::bytecode::{CapId, Value};
9
10use super::fault::Fault;
11
12/// What happened at the end of a [`crate::Vm::run`] slice.
13///
14/// This is the entire interface between `byteflow-vm` and the scheduler: the
15/// VM never touches threads, mailboxes or timers directly. It runs bytecode
16/// until it either finishes, needs an effect only the scheduler can perform,
17/// or exhausts its instruction budget — then hands one of these back and
18/// stops. That separation is what lets `byteflow-scheduler` move a suspended
19/// `Vm` between worker threads freely: it's just a value sitting in a
20/// `Flow`.
21#[derive(Debug)]
22pub enum VmResult {
23    /// The outermost frame returned/exited. The Flow should terminate
24    /// with this value delivered to `FlowHandle::join`.
25    Complete(Value),
26    /// Cooperative yield or instruction-budget exhaustion. Re-enqueue as
27    /// `Ready` on any worker; resuming picks up at the saved `pc` with no
28    /// register writeback needed.
29    Yield,
30    /// `Sleep` opcode. Register the Flow on the timer wheel; resume with
31    /// a plain `run()` call (no writeback) once it elapses.
32    Sleep(Duration),
33    /// `Spawn` — create a child flow; parent receives a **Cap**
34    /// ([`CapRights::ADDRESSING`](crate::CapRights::ADDRESSING)).
35    Spawn {
36        function: u32,
37        args: Vec<Value>,
38        dest_reg: u8,
39        requested_rights: crate::bytecode::CapRights,
40    },
41    /// `SelfPid` — write a **self Cap**
42    /// ([`CapRights::ADDRESSING`](crate::CapRights::ADDRESSING)) into `dest_reg`.
43    SelfPid { dest_reg: u8 },
44    /// `Send` — Atomic Hop to a **capability** target (requires SEND).
45    Send { target_cap: CapId, message: Value },
46    /// `Receive` / `ReceiveTimeout` / `ReceiveMatch*` (including `*Timeout`).
47    Receive {
48        dest_reg: u8,
49        timeout: Option<Duration>,
50        match_tag: Option<u16>,
51        match_request_id: Option<u64>,
52        /// When set, wait for a hop whose `payload.wire_tag()` equals this
53        /// BFV0 tag (`0..=10`). Independent of [`Self::Receive::match_tag`].
54        match_payload_kind: Option<u8>,
55        /// When set together with [`Self::Receive::match_tag`], the hop's
56        /// payload must be that `Int` (term equality, not a full pattern).
57        match_payload_eq: Option<i64>,
58        /// When set together with [`Self::Receive::match_tag`], the hop's
59        /// payload must equal this value (`ReceiveMatchValue`).
60        match_payload_value: Option<Arc<Value>>,
61        /// When set together with [`Self::Receive::match_tag`], the payload
62        /// must be an `Int` that passes this comparison. Other hops stay queued.
63        match_payload_cmp: Option<(crate::bytecode::IntCmp, i64)>,
64    },
65    /// `TableOpen` — this flow creates its one table and receives a Cap.
66    TableOpen { dest_reg: u8, name: Arc<str> },
67    /// `TableGet` — read one row. Missing key writes `Unit`.
68    TableGet {
69        dest_reg: u8,
70        cap: CapId,
71        key: Value,
72    },
73    /// `TablePut` — write one row. Requires `SEND` on the table Cap.
74    TablePut {
75        cap: CapId,
76        key: Value,
77        value: Value,
78    },
79    /// `SendRemote` — authenticated hop for the host distribution bridge.
80    SendRemote {
81        node: u64,
82        name: Arc<str>,
83        message: Value,
84    },
85    /// `StartSupervisor` — this flow starts its own supervisor.
86    /// `strategy` is `0` one-for-one, `1` one-for-all, `2` rest-for-one.
87    StartSupervisor { strategy: u8 },
88    /// `Ask` / `AskTimeout` — RPC hop to a **capability** target (requires ASK).
89    Ask {
90        dest_reg: u8,
91        target_cap: CapId,
92        request: Value,
93        timeout: Option<Duration>,
94    },
95    /// `Monitor ra, rb` — watch the flow addressed by Cap `r[b]`.
96    Monitor { dest_reg: u8, target_cap: CapId },
97    /// `Demonitor ra` — drop monitor whose ref is `r[a]` (Int).
98    Demonitor { monitor_reg: u8 },
99    /// `Link ra, rb` — bidirectional link with Cap `r[b]`.
100    Link { dest_reg: u8, target_cap: CapId },
101    /// `Unlink ra` — drop link whose id is `r[a]` (Int).
102    Unlink { link_reg: u8 },
103    /// `SetTrapExit ra` — enable/disable link exit trapping from truthy `r[a]`
104    /// (including converting `Normal` linked exits into hops).
105    SetTrapExit { enabled: bool },
106    /// `SetRestartPolicy imm` — update this flow's restart policy (`0..=2`).
107    SetRestartPolicy { policy: u8 },
108    /// `HostAwait` — register a host operation. The worker writes the ticket
109    /// id into `dest_reg` and keeps the flow running.
110    HostAwait { dest_reg: u8, op: u32, args: Value },
111    /// `RegisterName ra` — publish `r[a]` (`Str`) as this flow's name.
112    RegisterName { name: Arc<str> },
113    /// `Whereis ra, rb` — resolve `r[b]` (`Str`) to a SEND Cap or Unit.
114    Whereis { dest_reg: u8, name: Arc<str> },
115    /// `IsAlive ra, rb` — write `Bool` into `r[a]` for Cap `target_cap`.
116    IsAlive { dest_reg: u8, target_cap: CapId },
117    /// `ExitSignal` — BEAM `exit/2`. `reason` is a [`crate::FlowExitReason`] code.
118    ExitSignal { target_cap: CapId, reason: u64 },
119    /// `StartChild` — spawn `function` under this flow's supervisor.
120    StartChild {
121        dest_reg: u8,
122        function: u32,
123        policy: u8,
124    },
125    /// `Delegate ra, rb` — attenuate Cap `r[b]` into `r[a]`.
126    Delegate {
127        dest_reg: u8,
128        src_cap: CapId,
129        want_rights: crate::bytecode::CapRights,
130        want_native_cap: Option<CapId>,
131    },
132    /// A fault occurred; the Flow fails. See [`Fault`].
133    Trap(Fault),
134}