Skip to main content

telltale_machine/exec/
mod.rs

1//! Instruction dispatcher split by semantic concern.
2
3use crate::coroutine::Fault;
4use crate::effect::EffectHandler;
5use crate::engine::{ProtocolMachine, StepPack};
6use crate::instr::{Endpoint, Instr};
7use crate::session::SessionId;
8
9pub(crate) mod comm;
10pub(crate) mod control;
11pub(crate) mod guard_effect;
12pub(crate) mod helpers;
13pub(crate) mod ownership;
14pub(crate) mod session;
15pub(crate) mod speculation;
16
17/// Dispatch one instruction to its semantic execution module.
18pub(crate) fn step_instr(
19    machine: &mut ProtocolMachine,
20    coro_idx: usize,
21    ep: &Endpoint,
22    role: &str,
23    sid: SessionId,
24    instr: Instr,
25    handler: &dyn EffectHandler,
26) -> Result<StepPack, Fault> {
27    match instr {
28        Instr::Send { chan, val } => comm::step_send(machine, coro_idx, role, chan, val, handler),
29        Instr::Receive { chan, dst } => {
30            comm::step_receive(machine, coro_idx, role, chan, dst, handler)
31        }
32        Instr::Offer { chan, label } => {
33            comm::step_offer(machine, coro_idx, role, chan, &label, handler)
34        }
35        Instr::Choose { chan, table } => {
36            comm::step_choose(machine, coro_idx, role, chan, &table, handler)
37        }
38        Instr::Open {
39            roles,
40            local_types,
41            handlers,
42            dsts,
43        } => session::step_open(
44            machine,
45            coro_idx,
46            role,
47            &roles,
48            &local_types,
49            &handlers,
50            &dsts,
51        ),
52        Instr::Close { session } => session::step_close(machine, coro_idx, session),
53        Instr::Invoke { action } => {
54            guard_effect::step_invoke(machine, coro_idx, role, action, handler)
55        }
56        Instr::Acquire { layer, dst } => {
57            guard_effect::step_acquire(machine, coro_idx, ep, role, sid, &layer, dst, handler)
58        }
59        Instr::Release { layer, evidence } => {
60            guard_effect::step_release(machine, coro_idx, ep, role, sid, &layer, evidence, handler)
61        }
62        Instr::Fork { ghost } => speculation::step_fork(machine, coro_idx, role, sid, ghost),
63        Instr::Join => speculation::step_join(machine, coro_idx, role, sid),
64        Instr::Abort => speculation::step_abort(machine, coro_idx, role, sid),
65        Instr::Transfer {
66            endpoint,
67            target,
68            bundle,
69        } => ownership::step_transfer(machine, coro_idx, role, sid, endpoint, target, bundle),
70        Instr::Tag { fact, dst } => ownership::step_tag(machine, coro_idx, role, sid, fact, dst),
71        Instr::Check {
72            knowledge,
73            target,
74            dst,
75        } => ownership::step_check(machine, coro_idx, role, sid, knowledge, target, dst),
76        Instr::Set { dst, val } => Ok(control::step_set(dst, val)),
77        Instr::Move { dst, src } => control::step_move(machine, coro_idx, dst, src),
78        Instr::Jump { target } => Ok(control::step_jump(target)),
79        Instr::Spawn { target, args } => control::step_spawn(machine, coro_idx, target, &args),
80        Instr::Yield => Ok(control::step_yield()),
81        Instr::Halt => control::step_halt(machine, ep),
82    }
83}