Skip to main content

datex_core/runtime/execution/
stack_dump.rs

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