jetcrab\vm\executor/
stack_manager.rs

1//! # Stack Manager
2//!
3//! Provides concrete implementation of stack operations for the VM executor.
4//! Manages the execution stack and implements the `StackOperations` trait
5//! to provide stack manipulation functionality.
6//!
7//! ## Overview
8//!
9//! The stack manager wraps the low-level `Stack` implementation and provides
10//! a high-level interface for stack operations including:
11//!
12//! - **Push/Pop Operations**: Adding and removing values
13//! - **Inspection**: Peeking at values without modification
14//! - **Management**: Clearing, sizing, and state checking
15//!
16//! ## Usage
17//!
18//! ```rust
19//! use jetcrab::vm::executor::stack_manager::StackManager;
20//! use jetcrab::vm::executor::traits::StackOperations;
21//! use jetcrab::vm::value::Value;
22//!
23//! let mut stack_manager = StackManager::new();
24//! stack_manager.push(Value::Number(42.0));
25//! let value = stack_manager.pop();
26//! ```
27
28use super::StackOperations;
29use crate::vm::stack::Stack;
30use crate::vm::value::Value;
31
32/// Concrete implementation of stack operations for the VM
33///
34/// Wraps the low-level Stack and provides high-level stack management
35/// functionality for the execution engine.
36pub struct StackManager {
37    stack: Stack,
38}
39
40impl StackManager {
41    /// Creates a new stack manager with an empty stack
42    ///
43    /// Initializes the stack manager with a fresh, empty stack
44    /// ready for VM operations.
45    pub fn new() -> Self {
46        Self {
47            stack: Stack::new(),
48        }
49    }
50
51    /// Gets read-only access to the underlying stack
52    ///
53    /// Provides access to the stack for inspection and debugging
54    /// without allowing modifications.
55    pub fn stack(&self) -> &Stack {
56        &self.stack
57    }
58
59    /// Gets mutable access to the underlying stack
60    ///
61    /// Provides direct access to the stack for advanced operations
62    /// and testing purposes.
63    pub fn stack_mut(&mut self) -> &mut Stack {
64        &mut self.stack
65    }
66}
67
68impl StackOperations for StackManager {
69    fn push(&mut self, value: Value) {
70        self.stack.push(value);
71    }
72
73    fn pop(&mut self) -> Option<Value> {
74        self.stack.pop()
75    }
76
77    fn peek(&self) -> Option<&Value> {
78        self.stack.values.last()
79    }
80
81    fn clear(&mut self) {
82        self.stack.values.clear();
83    }
84
85    fn len(&self) -> usize {
86        self.stack.values.len()
87    }
88
89    fn is_empty(&self) -> bool {
90        self.stack.values.is_empty()
91    }
92
93    fn stack_mut(&mut self) -> &mut crate::vm::stack::Stack {
94        &mut self.stack
95    }
96}