Skip to main content

byteflow/vm/
result.rs

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