jetcrab\vm\executor/heap_manager.rs
1//! # Heap Manager
2//!
3//! Provides concrete implementation of heap operations for the VM executor.
4//! Manages object allocation, property access, and memory management
5//! through the `HeapOperations` trait implementation.
6//!
7//! ## Overview
8//!
9//! The heap manager wraps the low-level `Heap` implementation and provides
10//! a high-level interface for heap operations including:
11//!
12//! - **Object Allocation**: Creating objects, arrays, and functions
13//! - **Property Access**: Getting and setting object properties
14//! - **Array Operations**: Manipulating array elements
15//! - **Memory Management**: Interfacing with garbage collection
16//!
17//! ## Usage
18//!
19//! ```rust
20//! use jetcrab::vm::executor::heap_manager::HeapManager;
21//! use jetcrab::vm::executor::traits::HeapOperations;
22//! use jetcrab::vm::value::Value;
23//!
24//! let mut heap_manager = HeapManager::new();
25//! let object_id = heap_manager.alloc_object();
26//! heap_manager.set_object_property(object_id, "key".to_string(), Value::Number(42.0));
27//! ```
28
29use super::HeapOperations;
30use crate::vm::bytecode::Bytecode;
31use crate::vm::handle::HeapHandleId;
32use crate::vm::heap::Heap;
33use crate::vm::types::ArraySize;
34use crate::vm::value::Value;
35
36/// Concrete implementation of heap operations for the VM
37///
38/// Wraps the low-level Heap and provides high-level heap management
39/// functionality for object allocation and memory operations.
40pub struct HeapManager {
41 heap: Heap,
42}
43
44impl HeapManager {
45 /// Creates a new heap manager with an empty heap
46 ///
47 /// Initializes the heap manager with a fresh heap ready
48 /// for object allocation and memory management.
49 pub fn new() -> Self {
50 Self { heap: Heap::new() }
51 }
52
53 /// Gets read-only access to the underlying heap
54 ///
55 /// Provides access to the heap for inspection and debugging
56 /// without allowing modifications.
57 pub fn heap(&self) -> &Heap {
58 &self.heap
59 }
60
61 /// Gets mutable access to the underlying heap
62 ///
63 /// Provides direct access to the heap for advanced operations
64 /// and testing purposes.
65 pub fn heap_mut(&mut self) -> &mut Heap {
66 &mut self.heap
67 }
68}
69
70impl HeapOperations for HeapManager {
71 fn alloc_object(&mut self) -> HeapHandleId {
72 self.heap.alloc_object()
73 }
74
75 fn alloc_array(&mut self) -> HeapHandleId {
76 self.heap.alloc_array()
77 }
78
79 fn alloc_function(
80 &mut self,
81 bytecode: Bytecode,
82 arg_count: crate::vm::types::ArgIndex,
83 local_count: crate::vm::types::LocalIndex,
84 ) -> HeapHandleId {
85 self.heap.alloc_function(bytecode, arg_count, local_count)
86 }
87
88 fn get_object_property(&self, handle: HeapHandleId, key: &str) -> Option<&Value> {
89 self.heap.get_object_property(handle, key)
90 }
91
92 fn set_object_property(&mut self, handle: HeapHandleId, key: String, value: Value) {
93 self.heap.set_object_property(handle, key, value);
94 }
95
96 fn set_array_element(&mut self, handle: HeapHandleId, index: ArraySize, value: Value) {
97 self.heap.set_array_element(handle, index, value);
98 }
99
100 fn get_heap(&self) -> &crate::vm::heap::Heap {
101 &self.heap
102 }
103}