jetcrab\vm\heap/
entries.rs

1use crate::vm::bytecode::Bytecode;
2use crate::vm::types::{ArgIndex, LocalIndex};
3use crate::vm::value::Value;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
7pub enum HeapEntry {
8    Object(HashMap<String, Value>),
9    Array(Vec<Value>),
10    Function {
11        bytecode: Bytecode,
12        arg_count: ArgIndex,
13        local_count: LocalIndex,
14        closure_vars: HashMap<String, Value>,
15    },
16    String(String),
17}
18
19impl HeapEntry {
20    pub fn size(&self) -> usize {
21        match self {
22            HeapEntry::Object(obj) => obj.len(),
23            HeapEntry::Array(arr) => arr.len(),
24            HeapEntry::Function { .. } => 1,
25            HeapEntry::String(s) => s.len(),
26        }
27    }
28
29    pub fn memory_usage(&self) -> usize {
30        match self {
31            HeapEntry::Object(obj) => {
32                let mut size = 0;
33                for (key, value) in obj {
34                    size += key.len() + std::mem::size_of_val(value);
35                }
36                size
37            }
38            HeapEntry::Array(arr) => {
39                let mut size = 0;
40                for value in arr {
41                    size += std::mem::size_of_val(value);
42                }
43                size
44            }
45            HeapEntry::Function { bytecode, closure_vars, .. } => {
46                let mut size = bytecode.instructions.len() * std::mem::size_of::<crate::vm::instructions::Instruction>();
47                for (key, value) in closure_vars {
48                    size += key.len() + std::mem::size_of_val(value);
49                }
50                size
51            }
52            HeapEntry::String(s) => s.len(),
53        }
54    }
55
56    pub fn is_object(&self) -> bool {
57        matches!(self, HeapEntry::Object(_))
58    }
59
60    pub fn is_array(&self) -> bool {
61        matches!(self, HeapEntry::Array(_))
62    }
63
64    pub fn is_function(&self) -> bool {
65        matches!(self, HeapEntry::Function { .. })
66    }
67
68    pub fn is_string(&self) -> bool {
69        matches!(self, HeapEntry::String(_))
70    }
71
72    pub fn as_object(&self) -> Option<&HashMap<String, Value>> {
73        match self {
74            HeapEntry::Object(obj) => Some(obj),
75            _ => None,
76        }
77    }
78
79    pub fn as_object_mut(&mut self) -> Option<&mut HashMap<String, Value>> {
80        match self {
81            HeapEntry::Object(obj) => Some(obj),
82            _ => None,
83        }
84    }
85
86    pub fn as_array(&self) -> Option<&Vec<Value>> {
87        match self {
88            HeapEntry::Array(arr) => Some(arr),
89            _ => None,
90        }
91    }
92
93    pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>> {
94        match self {
95            HeapEntry::Array(arr) => Some(arr),
96            _ => None,
97        }
98    }
99
100    pub fn as_function(&self) -> Option<(&Bytecode, &ArgIndex, &LocalIndex, &HashMap<String, Value>)> {
101        match self {
102            HeapEntry::Function { bytecode, arg_count, local_count, closure_vars } => {
103                Some((bytecode, arg_count, local_count, closure_vars))
104            }
105            _ => None,
106        }
107    }
108
109    pub fn as_string(&self) -> Option<&String> {
110        match self {
111            HeapEntry::String(s) => Some(s),
112            _ => None,
113        }
114    }
115}
116
117impl Default for HeapEntry {
118    fn default() -> Self {
119        HeapEntry::Object(HashMap::new())
120    }
121}