Skip to main content

harn_vm/value/
env.rs

1use std::collections::BTreeMap;
2use std::rc::Rc;
3use std::{cell::RefCell, path::PathBuf};
4
5use crate::chunk::CompiledFunctionRef;
6
7use super::{VmError, VmValue};
8
9/// A compiled closure value.
10#[derive(Debug, Clone)]
11pub struct VmClosure {
12    pub func: CompiledFunctionRef,
13    pub env: VmEnv,
14    /// Source directory for this closure's originating module.
15    /// When set, `render()` and other source-relative builtins resolve
16    /// paths relative to this directory instead of the entry pipeline.
17    pub source_dir: Option<PathBuf>,
18    /// Module-local named functions that should resolve before builtin fallback.
19    /// This lets selectively imported functions keep private sibling helpers
20    /// without exporting them into the caller's environment.
21    pub module_functions: Option<ModuleFunctionRegistry>,
22    /// Shared, mutable module-level env: holds top-level `var` / `let`
23    /// bindings declared at the module root (caches, counters, lazily
24    /// initialized registries). All closures created from the same
25    /// module import point at the same `Rc<RefCell<VmEnv>>`, so a
26    /// mutation inside one function is visible to every other function
27    /// in that module on subsequent calls. `closure.env` still holds
28    /// the per-closure lexical snapshot (captured function args from
29    /// enclosing scopes, etc.) and is unchanged by this — `module_state`
30    /// is a separate lookup layer consulted after the local env and
31    /// before globals. Created in `import_declarations` after the
32    /// module's init chunk runs, so the initial values from `var x = ...`
33    /// land in it.
34    pub module_state: Option<ModuleState>,
35}
36
37pub type ModuleFunctionRegistry = Rc<RefCell<BTreeMap<String, Rc<VmClosure>>>>;
38pub type ModuleState = Rc<RefCell<VmEnv>>;
39
40/// VM environment for variable storage.
41#[derive(Debug, Clone)]
42pub struct VmEnv {
43    pub(crate) scopes: Vec<Scope>,
44}
45
46#[derive(Debug, Clone)]
47pub(crate) struct Scope {
48    pub(crate) vars: BTreeMap<String, (VmValue, bool)>, // (value, mutable)
49}
50
51impl Default for VmEnv {
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57impl VmEnv {
58    pub fn new() -> Self {
59        Self {
60            scopes: vec![Scope {
61                vars: BTreeMap::new(),
62            }],
63        }
64    }
65
66    pub fn push_scope(&mut self) {
67        self.scopes.push(Scope {
68            vars: BTreeMap::new(),
69        });
70    }
71
72    pub fn pop_scope(&mut self) {
73        if self.scopes.len() > 1 {
74            self.scopes.pop();
75        }
76    }
77
78    pub fn scope_depth(&self) -> usize {
79        self.scopes.len()
80    }
81
82    pub fn truncate_scopes(&mut self, target_depth: usize) {
83        let min_depth = target_depth.max(1);
84        while self.scopes.len() > min_depth {
85            self.scopes.pop();
86        }
87    }
88
89    pub fn get(&self, name: &str) -> Option<VmValue> {
90        for scope in self.scopes.iter().rev() {
91            if let Some((val, _)) = scope.vars.get(name) {
92                return Some(val.clone());
93            }
94        }
95        None
96    }
97
98    pub(crate) fn contains(&self, name: &str) -> bool {
99        self.scopes
100            .iter()
101            .rev()
102            .any(|scope| scope.vars.contains_key(name))
103    }
104
105    pub fn define(&mut self, name: &str, value: VmValue, mutable: bool) -> Result<(), VmError> {
106        if let Some(scope) = self.scopes.last_mut() {
107            if let Some((_, existing_mutable)) = scope.vars.get(name) {
108                if !existing_mutable && !mutable {
109                    return Err(VmError::Runtime(format!(
110                        "Cannot redeclare immutable variable '{name}' in the same scope (use 'var' for mutable bindings)"
111                    )));
112                }
113            }
114            scope.vars.insert(name.to_string(), (value, mutable));
115        }
116        Ok(())
117    }
118
119    pub fn all_variables(&self) -> BTreeMap<String, VmValue> {
120        let mut vars = BTreeMap::new();
121        for scope in &self.scopes {
122            for (name, (value, _)) in &scope.vars {
123                vars.insert(name.clone(), value.clone());
124            }
125        }
126        vars
127    }
128
129    pub fn assign(&mut self, name: &str, value: VmValue) -> Result<(), VmError> {
130        for scope in self.scopes.iter_mut().rev() {
131            if let Some((_, mutable)) = scope.vars.get(name) {
132                if !mutable {
133                    return Err(VmError::ImmutableAssignment(name.to_string()));
134                }
135                scope.vars.insert(name.to_string(), (value, true));
136                return Ok(());
137            }
138        }
139        Err(VmError::UndefinedVariable(name.to_string()))
140    }
141
142    /// Debugger-only variant of `assign` that rebinds the name even if
143    /// the existing binding was declared with `let`. Pipeline authors
144    /// overwhelmingly use `let`, so a strict mutability check would
145    /// make the DAP `setVariable` request useless for "what-if"
146    /// iteration — which is the whole point of the feature. Preserves
147    /// the original mutability flag so the VM's runtime behavior is
148    /// unchanged after the debugger overrides.
149    pub fn assign_debug(&mut self, name: &str, value: VmValue) -> Result<(), VmError> {
150        for scope in self.scopes.iter_mut().rev() {
151            if let Some((_, mutable)) = scope.vars.get(name) {
152                let mutable = *mutable;
153                scope.vars.insert(name.to_string(), (value, mutable));
154                return Ok(());
155            }
156        }
157        Err(VmError::UndefinedVariable(name.to_string()))
158    }
159}
160
161/// Compute Levenshtein edit distance between two strings.
162fn levenshtein(a: &str, b: &str) -> usize {
163    let a: Vec<char> = a.chars().collect();
164    let b: Vec<char> = b.chars().collect();
165    let (m, n) = (a.len(), b.len());
166    let mut prev = (0..=n).collect::<Vec<_>>();
167    let mut curr = vec![0; n + 1];
168    for i in 1..=m {
169        curr[0] = i;
170        for j in 1..=n {
171            let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
172            curr[j] = (prev[j] + 1).min(curr[j - 1] + 1).min(prev[j - 1] + cost);
173        }
174        std::mem::swap(&mut prev, &mut curr);
175    }
176    prev[n]
177}
178
179/// Find the closest match from a list of candidates using Levenshtein distance.
180/// Returns `Some(suggestion)` if a candidate is within `max_dist` edits.
181pub fn closest_match<'a>(name: &str, candidates: impl Iterator<Item = &'a str>) -> Option<String> {
182    let max_dist = match name.len() {
183        0..=2 => 1,
184        3..=5 => 2,
185        _ => 3,
186    };
187    candidates
188        .filter(|c| *c != name && !c.starts_with("__"))
189        .map(|c| (c, levenshtein(name, c)))
190        .filter(|(_, d)| *d <= max_dist)
191        // Prefer smallest distance, then closest length to original, then alphabetical
192        .min_by(|(a, da), (b, db)| {
193            da.cmp(db)
194                .then_with(|| {
195                    let a_diff = (a.len() as isize - name.len() as isize).unsigned_abs();
196                    let b_diff = (b.len() as isize - name.len() as isize).unsigned_abs();
197                    a_diff.cmp(&b_diff)
198                })
199                .then_with(|| a.cmp(b))
200        })
201        .map(|(c, _)| c.to_string())
202}