1use crate::value::Value;
6use std::cell::RefCell;
7use std::collections::HashMap;
8use std::rc::Rc;
9
10pub struct EnvironmentPool {
12 pool: Vec<Environment>,
14 max_size: usize,
16}
17
18impl EnvironmentPool {
19 pub fn new() -> Self {
21 Self::with_capacity(50)
22 }
23
24 pub fn with_capacity(max_size: usize) -> Self {
26 EnvironmentPool {
27 pool: Vec::with_capacity(max_size.min(50)),
28 max_size,
29 }
30 }
31
32 pub fn acquire(&mut self) -> Environment {
34 self.pool.pop().unwrap_or_default()
35 }
36
37 pub fn release(&mut self, mut env: Environment) {
39 if self.pool.len() < self.max_size {
40 env.clear();
41 env.parent = None;
42 self.pool.push(env);
43 }
44 }
45
46 pub fn clear(&mut self) {
48 self.pool.clear();
49 }
50}
51
52impl Default for EnvironmentPool {
53 fn default() -> Self {
54 Self::new()
55 }
56}
57
58#[derive(Debug, Clone)]
60pub struct Environment {
61 store: HashMap<String, Value>,
63
64 parent: Option<Rc<RefCell<Environment>>>,
66}
67
68impl Environment {
69 pub fn new() -> Self {
71 Environment {
72 store: HashMap::with_capacity(16), parent: None,
74 }
75 }
76
77 pub fn with_parent(parent: Rc<RefCell<Environment>>) -> Self {
79 Environment {
80 store: HashMap::with_capacity(8), parent: Some(parent),
82 }
83 }
84
85 pub fn set(&mut self, name: String, value: Value) {
87 self.store.insert(name, value);
88 }
89
90 pub fn get(&self, name: &str) -> Option<Value> {
92 if let Some(value) = self.store.get(name) {
94 return Some(value.clone());
95 }
96
97 self.get_from_parent(name)
99 }
100
101 #[inline(never)]
103 fn get_from_parent(&self, name: &str) -> Option<Value> {
104 self.parent.as_ref()?.borrow().get(name)
105 }
106
107 pub fn has(&self, name: &str) -> bool {
109 self.store.contains_key(name) || self.parent.as_ref().is_some_and(|p| p.borrow().has(name))
110 }
111
112 pub fn update(&mut self, name: &str, value: Value) -> bool {
115 if self.store.contains_key(name) {
116 self.store.insert(name.to_string(), value);
117 return true;
118 }
119
120 if let Some(parent) = &self.parent {
121 return parent.borrow_mut().update(name, value);
122 }
123
124 false
125 }
126
127 pub fn keys(&self) -> Vec<String> {
129 self.store.keys().cloned().collect()
130 }
131
132 pub fn clear(&mut self) {
134 self.store.clear();
135 }
136}
137
138impl Default for Environment {
139 fn default() -> Self {
140 Self::new()
141 }
142}