1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::marker::PhantomData;

use crate::machine::instruction::MachineInstruction;
use crate::machine::output_receiver::OutputReceiver;
use crate::machine::register::MachineRegister;
use crate::machine::Machine;

/// Allows the hook to influence the rest of the execution of an instruction.
pub enum HookResult {
    /// Proceed normally.
    Proceed,

    /// Skip the instruction that was about to be executed.
    Skip,

    /// Go to a specific instruction.
    Goto(i64),

    /// Abort the entire program.
    Abort,
}

pub trait PreExecuteHook<I: MachineInstruction> {
    fn run<R: MachineRegister, O: OutputReceiver<R>>(
        &mut self,
        machine: &mut Machine<I, R, O>,
        instruction: &I,
        idx: usize,
    ) -> HookResult;
}

pub struct NoopHook<I: MachineInstruction> {
    _marker: PhantomData<I>,
}

impl<I: MachineInstruction> Default for NoopHook<I> {
    fn default() -> Self {
        NoopHook {
            _marker: Default::default(),
        }
    }
}

impl<I: MachineInstruction> PreExecuteHook<I> for NoopHook<I> {
    fn run<R: MachineRegister, O: OutputReceiver<R>>(
        &mut self,
        _machine: &mut Machine<I, R, O>,
        _instruction: &I,
        _idx: usize,
    ) -> HookResult {
        HookResult::Proceed
    }
}