jetcrab\vm\executor/
variable_manager.rs

1//! # Variable Manager
2//!
3//! Provides concrete implementation of variable management for the VM executor.
4//! Manages local and global variable storage and implements the `VariableManager`
5//! trait for variable access and manipulation.
6//!
7//! ## Overview
8//!
9//! The variable manager provides a simple array-based storage system for
10//! both local and global variables, supporting:
11//!
12//! - **Local Variables**: Function-scoped variables with limited lifetime
13//! - **Global Variables**: Program-wide variables accessible across functions
14//! - **Variable Access**: Getting and setting variables by index
15//! - **Mutable Access**: Direct access to variable storage for advanced operations
16//!
17//! ## Storage Model
18//!
19//! Variables are stored in fixed-size vectors, with each variable slot
20//! identified by a numeric index. Undefined variables are represented
21//! as `Value::Undefined`.
22//!
23//! ## Usage
24//!
25//! ```rust
26//! use jetcrab::vm::executor::variable_manager::VariableManagerImpl;
27//! use jetcrab::vm::executor::traits::VariableManager;
28//! use jetcrab::vm::value::Value;
29//!
30//! let mut var_manager = VariableManagerImpl::new();
31//! var_manager.set_local(0, Value::Number(42.0));
32//! let value = var_manager.get_local(0);
33//! ```
34
35use crate::vm::value::Value;
36use super::VariableManager;
37
38/// Concrete implementation of variable management for the VM
39///
40/// Provides array-based storage for local and global variables
41/// with indexed access and modification capabilities.
42pub struct VariableManagerImpl {
43    locals: Vec<Value>,
44    globals: Vec<Value>,
45}
46
47impl VariableManagerImpl {
48    /// Creates a new variable manager with empty variable storage
49    ///
50    /// Initializes both local and global variable storage with
51    /// 32 slots each, all set to `Value::Undefined`.
52    pub fn new() -> Self {
53        Self {
54            locals: vec![Value::Undefined; 32],
55            globals: vec![Value::Undefined; 32],
56        }
57    }
58
59    pub fn locals(&self) -> &[Value] {
60        &self.locals
61    }
62
63    pub fn locals_mut(&mut self) -> &mut [Value] {
64        &mut self.locals
65    }
66
67    pub fn globals(&self) -> &[Value] {
68        &self.globals
69    }
70
71    pub fn globals_mut(&mut self) -> &mut [Value] {
72        &mut self.globals
73    }
74}
75
76impl VariableManager for VariableManagerImpl {
77    fn get_local(&self, idx: usize) -> Option<&Value> {
78        self.locals.get(idx)
79    }
80
81    fn set_local(&mut self, idx: usize, value: Value) {
82        if let Some(slot) = self.locals.get_mut(idx) {
83            *slot = value;
84        }
85    }
86
87    fn get_global(&self, idx: usize) -> Option<&Value> {
88        self.globals.get(idx)
89    }
90
91    fn set_global(&mut self, idx: usize, value: Value) {
92        if let Some(slot) = self.globals.get_mut(idx) {
93            *slot = value;
94        }
95    }
96
97    fn get_local_mut(&mut self, idx: usize) -> Option<&mut Value> {
98        self.locals.get_mut(idx)
99    }
100
101    fn get_global_mut(&mut self, idx: usize) -> Option<&mut Value> {
102        self.globals.get_mut(idx)
103    }
104}