Skip to main content

datex_core/runtime/execution/
memory_dump.rs

1use crate::{
2    runtime::execution::execution_loop::state::RuntimeExecutionSlots,
3    values::value_container::ValueContainer,
4};
5use itertools::Itertools;
6
7use crate::prelude::*;
8pub struct MemoryDump {
9    pub slots: Vec<(u32, Option<ValueContainer>)>,
10}
11
12#[cfg(feature = "decompiler")]
13impl core::fmt::Display for MemoryDump {
14    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15        for (address, value) in &self.slots {
16            match value {
17                Some(vc) => {
18                    let decompiled = crate::decompiler::decompile_value(
19                        vc,
20                        crate::decompiler::DecompileOptions::colorized(),
21                    );
22                    writeln!(f, "#{address}: {decompiled}")?
23                }
24                None => writeln!(f, "#{address}: <uninitialized>")?,
25            }
26        }
27        if self.slots.is_empty() {
28            writeln!(f, "<no slots allocated>")?;
29        }
30        Ok(())
31    }
32}
33
34impl RuntimeExecutionSlots {
35    /// Returns a memory dump of the current slots and their values.
36    pub fn memory_dump(&self) -> MemoryDump {
37        MemoryDump {
38            slots: self
39                .slots
40                .iter()
41                .map(|(k, v)| (*k, v.clone()))
42                .sorted_by_key(|(k, _)| *k)
43                .collect(),
44        }
45    }
46}