Skip to main content

harn_vm/
value.rs

1use std::collections::BTreeMap;
2use std::rc::Rc;
3use std::sync::atomic::{AtomicBool, AtomicI64, Ordering};
4use std::sync::Arc;
5use std::{cell::RefCell, path::PathBuf};
6
7use crate::chunk::CompiledFunction;
8use crate::mcp::VmMcpClientHandle;
9
10/// An async builtin function for the VM.
11pub type VmAsyncBuiltinFn = Rc<
12    dyn Fn(
13        Vec<VmValue>,
14    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<VmValue, VmError>>>>,
15>;
16
17/// The raw join handle type for spawned tasks.
18pub type VmJoinHandle = tokio::task::JoinHandle<Result<(VmValue, String), VmError>>;
19
20/// A spawned async task handle with cancellation support.
21pub struct VmTaskHandle {
22    pub handle: VmJoinHandle,
23    /// Cooperative cancellation token. Set to true to request graceful shutdown.
24    pub cancel_token: Arc<AtomicBool>,
25}
26
27/// A channel handle for the VM (uses tokio mpsc).
28#[derive(Debug, Clone)]
29pub struct VmChannelHandle {
30    pub name: String,
31    pub sender: Arc<tokio::sync::mpsc::Sender<VmValue>>,
32    pub receiver: Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
33    pub closed: Arc<AtomicBool>,
34}
35
36/// An atomic integer handle for the VM.
37#[derive(Debug, Clone)]
38pub struct VmAtomicHandle {
39    pub value: Arc<AtomicI64>,
40}
41
42/// A generator object: lazily produces values via yield.
43/// The generator body runs as a spawned task that sends values through a channel.
44#[derive(Debug, Clone)]
45pub struct VmGenerator {
46    /// Whether the generator has finished (returned or exhausted).
47    pub done: Rc<std::cell::Cell<bool>>,
48    /// Receiver end of the yield channel (generator sends values here).
49    /// Wrapped in a shared async mutex so recv() can be called without holding
50    /// a RefCell borrow across await points.
51    pub receiver: Rc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
52}
53
54/// VM runtime value.
55#[derive(Debug, Clone)]
56pub enum VmValue {
57    Int(i64),
58    Float(f64),
59    String(Rc<str>),
60    Bool(bool),
61    Nil,
62    List(Rc<Vec<VmValue>>),
63    Dict(Rc<BTreeMap<String, VmValue>>),
64    Closure(Rc<VmClosure>),
65    /// Reference to a registered builtin function, used when a builtin name is
66    /// referenced as a value (e.g. `snake_dict.rekey(snake_to_camel)`). The
67    /// contained string is the builtin's registered name.
68    BuiltinRef(Rc<str>),
69    Duration(u64),
70    EnumVariant {
71        enum_name: String,
72        variant: String,
73        fields: Vec<VmValue>,
74    },
75    StructInstance {
76        struct_name: String,
77        fields: BTreeMap<String, VmValue>,
78    },
79    TaskHandle(String),
80    Channel(VmChannelHandle),
81    Atomic(VmAtomicHandle),
82    McpClient(VmMcpClientHandle),
83    Set(Rc<Vec<VmValue>>),
84    Generator(VmGenerator),
85}
86
87/// A compiled closure value.
88#[derive(Debug, Clone)]
89pub struct VmClosure {
90    pub func: CompiledFunction,
91    pub env: VmEnv,
92    /// Source directory for this closure's originating module.
93    /// When set, `render()` and other source-relative builtins resolve
94    /// paths relative to this directory instead of the entry pipeline.
95    pub source_dir: Option<PathBuf>,
96    /// Module-local named functions that should resolve before builtin fallback.
97    /// This lets selectively imported functions keep private sibling helpers
98    /// without exporting them into the caller's environment.
99    pub module_functions: Option<ModuleFunctionRegistry>,
100    /// Shared, mutable module-level env: holds top-level `var` / `let`
101    /// bindings declared at the module root (caches, counters, lazily
102    /// initialized registries). All closures created from the same
103    /// module import point at the same `Rc<RefCell<VmEnv>>`, so a
104    /// mutation inside one function is visible to every other function
105    /// in that module on subsequent calls. `closure.env` still holds
106    /// the per-closure lexical snapshot (captured function args from
107    /// enclosing scopes, etc.) and is unchanged by this — `module_state`
108    /// is a separate lookup layer consulted after the local env and
109    /// before globals. Created in `import_declarations` after the
110    /// module's init chunk runs, so the initial values from `var x = ...`
111    /// land in it.
112    pub module_state: Option<ModuleState>,
113}
114
115pub type ModuleFunctionRegistry = Rc<RefCell<BTreeMap<String, Rc<VmClosure>>>>;
116pub type ModuleState = Rc<RefCell<VmEnv>>;
117
118/// VM environment for variable storage.
119#[derive(Debug, Clone)]
120pub struct VmEnv {
121    pub(crate) scopes: Vec<Scope>,
122}
123
124#[derive(Debug, Clone)]
125pub(crate) struct Scope {
126    pub(crate) vars: BTreeMap<String, (VmValue, bool)>, // (value, mutable)
127}
128
129impl Default for VmEnv {
130    fn default() -> Self {
131        Self::new()
132    }
133}
134
135impl VmEnv {
136    pub fn new() -> Self {
137        Self {
138            scopes: vec![Scope {
139                vars: BTreeMap::new(),
140            }],
141        }
142    }
143
144    pub fn push_scope(&mut self) {
145        self.scopes.push(Scope {
146            vars: BTreeMap::new(),
147        });
148    }
149
150    pub fn pop_scope(&mut self) {
151        if self.scopes.len() > 1 {
152            self.scopes.pop();
153        }
154    }
155
156    pub fn scope_depth(&self) -> usize {
157        self.scopes.len()
158    }
159
160    pub fn truncate_scopes(&mut self, target_depth: usize) {
161        let min_depth = target_depth.max(1);
162        while self.scopes.len() > min_depth {
163            self.scopes.pop();
164        }
165    }
166
167    pub fn get(&self, name: &str) -> Option<VmValue> {
168        for scope in self.scopes.iter().rev() {
169            if let Some((val, _)) = scope.vars.get(name) {
170                return Some(val.clone());
171            }
172        }
173        None
174    }
175
176    pub fn define(&mut self, name: &str, value: VmValue, mutable: bool) -> Result<(), VmError> {
177        if let Some(scope) = self.scopes.last_mut() {
178            if let Some((_, existing_mutable)) = scope.vars.get(name) {
179                if !existing_mutable && !mutable {
180                    return Err(VmError::Runtime(format!(
181                        "Cannot redeclare immutable variable '{name}' in the same scope (use 'var' for mutable bindings)"
182                    )));
183                }
184            }
185            scope.vars.insert(name.to_string(), (value, mutable));
186        }
187        Ok(())
188    }
189
190    pub fn all_variables(&self) -> BTreeMap<String, VmValue> {
191        let mut vars = BTreeMap::new();
192        for scope in &self.scopes {
193            for (name, (value, _)) in &scope.vars {
194                vars.insert(name.clone(), value.clone());
195            }
196        }
197        vars
198    }
199
200    pub fn assign(&mut self, name: &str, value: VmValue) -> Result<(), VmError> {
201        for scope in self.scopes.iter_mut().rev() {
202            if let Some((_, mutable)) = scope.vars.get(name) {
203                if !mutable {
204                    return Err(VmError::ImmutableAssignment(name.to_string()));
205                }
206                scope.vars.insert(name.to_string(), (value, true));
207                return Ok(());
208            }
209        }
210        Err(VmError::UndefinedVariable(name.to_string()))
211    }
212}
213
214/// VM runtime errors.
215/// Compute Levenshtein edit distance between two strings.
216fn levenshtein(a: &str, b: &str) -> usize {
217    let a: Vec<char> = a.chars().collect();
218    let b: Vec<char> = b.chars().collect();
219    let (m, n) = (a.len(), b.len());
220    let mut prev = (0..=n).collect::<Vec<_>>();
221    let mut curr = vec![0; n + 1];
222    for i in 1..=m {
223        curr[0] = i;
224        for j in 1..=n {
225            let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
226            curr[j] = (prev[j] + 1).min(curr[j - 1] + 1).min(prev[j - 1] + cost);
227        }
228        std::mem::swap(&mut prev, &mut curr);
229    }
230    prev[n]
231}
232
233/// Find the closest match from a list of candidates using Levenshtein distance.
234/// Returns `Some(suggestion)` if a candidate is within `max_dist` edits.
235pub fn closest_match<'a>(name: &str, candidates: impl Iterator<Item = &'a str>) -> Option<String> {
236    let max_dist = match name.len() {
237        0..=2 => 1,
238        3..=5 => 2,
239        _ => 3,
240    };
241    candidates
242        .filter(|c| *c != name && !c.starts_with("__"))
243        .map(|c| (c, levenshtein(name, c)))
244        .filter(|(_, d)| *d <= max_dist)
245        // Prefer smallest distance, then closest length to original, then alphabetical
246        .min_by(|(a, da), (b, db)| {
247            da.cmp(db)
248                .then_with(|| {
249                    let a_diff = (a.len() as isize - name.len() as isize).unsigned_abs();
250                    let b_diff = (b.len() as isize - name.len() as isize).unsigned_abs();
251                    a_diff.cmp(&b_diff)
252                })
253                .then_with(|| a.cmp(b))
254        })
255        .map(|(c, _)| c.to_string())
256}
257
258#[derive(Debug, Clone)]
259pub enum VmError {
260    StackUnderflow,
261    StackOverflow,
262    UndefinedVariable(String),
263    UndefinedBuiltin(String),
264    ImmutableAssignment(String),
265    TypeError(String),
266    Runtime(String),
267    DivisionByZero,
268    Thrown(VmValue),
269    /// Thrown with error category for structured error handling.
270    CategorizedError {
271        message: String,
272        category: ErrorCategory,
273    },
274    Return(VmValue),
275    InvalidInstruction(u8),
276}
277
278/// Error categories for structured error handling in agent orchestration.
279#[derive(Debug, Clone, PartialEq, Eq)]
280pub enum ErrorCategory {
281    /// Network/connection timeout
282    Timeout,
283    /// Authentication/authorization failure
284    Auth,
285    /// Rate limit exceeded
286    RateLimit,
287    /// Tool execution failure
288    ToolError,
289    /// Tool was rejected by the host (not permitted / not in allowlist)
290    ToolRejected,
291    /// Operation was cancelled
292    Cancelled,
293    /// Resource not found
294    NotFound,
295    /// Circuit breaker is open
296    CircuitOpen,
297    /// Generic/unclassified error
298    Generic,
299}
300
301impl ErrorCategory {
302    pub fn as_str(&self) -> &'static str {
303        match self {
304            ErrorCategory::Timeout => "timeout",
305            ErrorCategory::Auth => "auth",
306            ErrorCategory::RateLimit => "rate_limit",
307            ErrorCategory::ToolError => "tool_error",
308            ErrorCategory::ToolRejected => "tool_rejected",
309            ErrorCategory::Cancelled => "cancelled",
310            ErrorCategory::NotFound => "not_found",
311            ErrorCategory::CircuitOpen => "circuit_open",
312            ErrorCategory::Generic => "generic",
313        }
314    }
315
316    pub fn parse(s: &str) -> Self {
317        match s {
318            "timeout" => ErrorCategory::Timeout,
319            "auth" => ErrorCategory::Auth,
320            "rate_limit" => ErrorCategory::RateLimit,
321            "tool_error" => ErrorCategory::ToolError,
322            "tool_rejected" => ErrorCategory::ToolRejected,
323            "cancelled" => ErrorCategory::Cancelled,
324            "not_found" => ErrorCategory::NotFound,
325            "circuit_open" => ErrorCategory::CircuitOpen,
326            _ => ErrorCategory::Generic,
327        }
328    }
329}
330
331/// Create a categorized error conveniently.
332pub fn categorized_error(message: impl Into<String>, category: ErrorCategory) -> VmError {
333    VmError::CategorizedError {
334        message: message.into(),
335        category,
336    }
337}
338
339/// Extract error category from a VmError.
340///
341/// Classification priority:
342/// 1. Explicit CategorizedError variant (set by throw_error or internal code)
343/// 2. Thrown dict with a "category" field (user-created structured errors)
344/// 3. HTTP status code extraction (standard, unambiguous)
345/// 4. Deadline exceeded (VM-internal)
346/// 5. Fallback to Generic
347pub fn error_to_category(err: &VmError) -> ErrorCategory {
348    match err {
349        VmError::CategorizedError { category, .. } => category.clone(),
350        VmError::Thrown(VmValue::Dict(d)) => d
351            .get("category")
352            .map(|v| ErrorCategory::parse(&v.display()))
353            .unwrap_or(ErrorCategory::Generic),
354        VmError::Thrown(VmValue::String(s)) => classify_error_message(s),
355        VmError::Runtime(msg) => classify_error_message(msg),
356        _ => ErrorCategory::Generic,
357    }
358}
359
360/// Classify an error message using HTTP status codes and well-known patterns.
361/// Prefers unambiguous signals (status codes) over substring heuristics.
362fn classify_error_message(msg: &str) -> ErrorCategory {
363    // 1. HTTP status codes — most reliable signal
364    if let Some(cat) = classify_by_http_status(msg) {
365        return cat;
366    }
367    // 2. Well-known error identifiers from major APIs
368    //    (Anthropic, OpenAI, and standard HTTP patterns)
369    if msg.contains("Deadline exceeded") || msg.contains("context deadline exceeded") {
370        return ErrorCategory::Timeout;
371    }
372    if msg.contains("overloaded_error") || msg.contains("api_error") {
373        // Anthropic-specific error types
374        return ErrorCategory::RateLimit;
375    }
376    if msg.contains("insufficient_quota") || msg.contains("billing_hard_limit_reached") {
377        // OpenAI-specific error types
378        return ErrorCategory::RateLimit;
379    }
380    if msg.contains("invalid_api_key") || msg.contains("authentication_error") {
381        return ErrorCategory::Auth;
382    }
383    if msg.contains("not_found_error") || msg.contains("model_not_found") {
384        return ErrorCategory::NotFound;
385    }
386    if msg.contains("circuit_open") {
387        return ErrorCategory::CircuitOpen;
388    }
389    ErrorCategory::Generic
390}
391
392/// Classify errors by HTTP status code if one appears in the message.
393/// This is the most reliable classification method since status codes
394/// are standardized (RFC 9110) and unambiguous.
395fn classify_by_http_status(msg: &str) -> Option<ErrorCategory> {
396    // Extract 3-digit HTTP status codes from common patterns:
397    // "HTTP 429", "status 429", "429 Too Many", "error: 401"
398    for code in extract_http_status_codes(msg) {
399        return Some(match code {
400            401 | 403 => ErrorCategory::Auth,
401            404 | 410 => ErrorCategory::NotFound,
402            408 | 504 | 522 | 524 => ErrorCategory::Timeout,
403            429 | 503 => ErrorCategory::RateLimit,
404            _ => continue,
405        });
406    }
407    None
408}
409
410/// Extract plausible HTTP status codes from an error message.
411fn extract_http_status_codes(msg: &str) -> Vec<u16> {
412    let mut codes = Vec::new();
413    let bytes = msg.as_bytes();
414    for i in 0..bytes.len().saturating_sub(2) {
415        // Look for 3-digit sequences in the 100-599 range
416        if bytes[i].is_ascii_digit()
417            && bytes[i + 1].is_ascii_digit()
418            && bytes[i + 2].is_ascii_digit()
419        {
420            // Ensure it's not part of a longer number
421            let before_ok = i == 0 || !bytes[i - 1].is_ascii_digit();
422            let after_ok = i + 3 >= bytes.len() || !bytes[i + 3].is_ascii_digit();
423            if before_ok && after_ok {
424                if let Ok(code) = msg[i..i + 3].parse::<u16>() {
425                    if (400..=599).contains(&code) {
426                        codes.push(code);
427                    }
428                }
429            }
430        }
431    }
432    codes
433}
434
435impl std::fmt::Display for VmError {
436    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437        match self {
438            VmError::StackUnderflow => write!(f, "Stack underflow"),
439            VmError::StackOverflow => write!(f, "Stack overflow: too many nested calls"),
440            VmError::UndefinedVariable(n) => write!(f, "Undefined variable: {n}"),
441            VmError::UndefinedBuiltin(n) => write!(f, "Undefined builtin: {n}"),
442            VmError::ImmutableAssignment(n) => {
443                write!(f, "Cannot assign to immutable binding: {n}")
444            }
445            VmError::TypeError(msg) => write!(f, "Type error: {msg}"),
446            VmError::Runtime(msg) => write!(f, "Runtime error: {msg}"),
447            VmError::DivisionByZero => write!(f, "Division by zero"),
448            VmError::Thrown(v) => write!(f, "Thrown: {}", v.display()),
449            VmError::CategorizedError { message, category } => {
450                write!(f, "Error [{}]: {}", category.as_str(), message)
451            }
452            VmError::Return(_) => write!(f, "Return from function"),
453            VmError::InvalidInstruction(op) => write!(f, "Invalid instruction: 0x{op:02x}"),
454        }
455    }
456}
457
458impl std::error::Error for VmError {}
459
460impl VmValue {
461    pub fn is_truthy(&self) -> bool {
462        match self {
463            VmValue::Bool(b) => *b,
464            VmValue::Nil => false,
465            VmValue::Int(n) => *n != 0,
466            VmValue::Float(n) => *n != 0.0,
467            VmValue::String(s) => !s.is_empty(),
468            VmValue::List(l) => !l.is_empty(),
469            VmValue::Dict(d) => !d.is_empty(),
470            VmValue::Closure(_) => true,
471            VmValue::BuiltinRef(_) => true,
472            VmValue::Duration(ms) => *ms > 0,
473            VmValue::EnumVariant { .. } => true,
474            VmValue::StructInstance { .. } => true,
475            VmValue::TaskHandle(_) => true,
476            VmValue::Channel(_) => true,
477            VmValue::Atomic(_) => true,
478            VmValue::McpClient(_) => true,
479            VmValue::Set(s) => !s.is_empty(),
480            VmValue::Generator(_) => true,
481        }
482    }
483
484    pub fn type_name(&self) -> &'static str {
485        match self {
486            VmValue::String(_) => "string",
487            VmValue::Int(_) => "int",
488            VmValue::Float(_) => "float",
489            VmValue::Bool(_) => "bool",
490            VmValue::Nil => "nil",
491            VmValue::List(_) => "list",
492            VmValue::Dict(_) => "dict",
493            VmValue::Closure(_) => "closure",
494            VmValue::BuiltinRef(_) => "builtin",
495            VmValue::Duration(_) => "duration",
496            VmValue::EnumVariant { .. } => "enum",
497            VmValue::StructInstance { .. } => "struct",
498            VmValue::TaskHandle(_) => "task_handle",
499            VmValue::Channel(_) => "channel",
500            VmValue::Atomic(_) => "atomic",
501            VmValue::McpClient(_) => "mcp_client",
502            VmValue::Set(_) => "set",
503            VmValue::Generator(_) => "generator",
504        }
505    }
506
507    pub fn display(&self) -> String {
508        match self {
509            VmValue::Int(n) => n.to_string(),
510            VmValue::Float(n) => {
511                if *n == (*n as i64) as f64 && n.abs() < 1e15 {
512                    format!("{:.1}", n)
513                } else {
514                    n.to_string()
515                }
516            }
517            VmValue::String(s) => s.to_string(),
518            VmValue::Bool(b) => (if *b { "true" } else { "false" }).to_string(),
519            VmValue::Nil => "nil".to_string(),
520            VmValue::List(items) => {
521                let inner: Vec<String> = items.iter().map(|i| i.display()).collect();
522                format!("[{}]", inner.join(", "))
523            }
524            VmValue::Dict(map) => {
525                let inner: Vec<String> = map
526                    .iter()
527                    .map(|(k, v)| format!("{k}: {}", v.display()))
528                    .collect();
529                format!("{{{}}}", inner.join(", "))
530            }
531            VmValue::Closure(c) => format!("<fn({})>", c.func.params.join(", ")),
532            VmValue::BuiltinRef(name) => format!("<builtin {name}>"),
533            VmValue::Duration(ms) => {
534                if *ms >= 3_600_000 && ms % 3_600_000 == 0 {
535                    format!("{}h", ms / 3_600_000)
536                } else if *ms >= 60_000 && ms % 60_000 == 0 {
537                    format!("{}m", ms / 60_000)
538                } else if *ms >= 1000 && ms % 1000 == 0 {
539                    format!("{}s", ms / 1000)
540                } else {
541                    format!("{}ms", ms)
542                }
543            }
544            VmValue::EnumVariant {
545                enum_name,
546                variant,
547                fields,
548            } => {
549                if fields.is_empty() {
550                    format!("{enum_name}.{variant}")
551                } else {
552                    let inner: Vec<String> = fields.iter().map(|v| v.display()).collect();
553                    format!("{enum_name}.{variant}({})", inner.join(", "))
554                }
555            }
556            VmValue::StructInstance {
557                struct_name,
558                fields,
559            } => {
560                let inner: Vec<String> = fields
561                    .iter()
562                    .map(|(k, v)| format!("{k}: {}", v.display()))
563                    .collect();
564                format!("{struct_name} {{{}}}", inner.join(", "))
565            }
566            VmValue::TaskHandle(id) => format!("<task:{id}>"),
567            VmValue::Channel(ch) => format!("<channel:{}>", ch.name),
568            VmValue::Atomic(a) => format!("<atomic:{}>", a.value.load(Ordering::SeqCst)),
569            VmValue::McpClient(c) => format!("<mcp_client:{}>", c.name),
570            VmValue::Set(items) => {
571                let inner: Vec<String> = items.iter().map(|i| i.display()).collect();
572                format!("set({})", inner.join(", "))
573            }
574            VmValue::Generator(g) => {
575                if g.done.get() {
576                    "<generator (done)>".to_string()
577                } else {
578                    "<generator>".to_string()
579                }
580            }
581        }
582    }
583
584    /// Get the value as a BTreeMap reference, if it's a Dict.
585    pub fn as_dict(&self) -> Option<&BTreeMap<String, VmValue>> {
586        if let VmValue::Dict(d) = self {
587            Some(d)
588        } else {
589            None
590        }
591    }
592
593    pub fn as_int(&self) -> Option<i64> {
594        if let VmValue::Int(n) = self {
595            Some(*n)
596        } else {
597            None
598        }
599    }
600}
601
602/// Sync builtin function for the VM.
603pub type VmBuiltinFn = Rc<dyn Fn(&[VmValue], &mut String) -> Result<VmValue, VmError>>;
604
605/// Reference / identity equality. For heap-allocated refcounted values
606/// (List/Dict/Set/Closure) returns true only when both operands share the
607/// same underlying `Rc` allocation. For primitive scalars, falls back to
608/// structural equality (since primitives have no distinct identity).
609pub fn values_identical(a: &VmValue, b: &VmValue) -> bool {
610    match (a, b) {
611        (VmValue::List(x), VmValue::List(y)) => Rc::ptr_eq(x, y),
612        (VmValue::Dict(x), VmValue::Dict(y)) => Rc::ptr_eq(x, y),
613        (VmValue::Set(x), VmValue::Set(y)) => Rc::ptr_eq(x, y),
614        (VmValue::Closure(x), VmValue::Closure(y)) => Rc::ptr_eq(x, y),
615        (VmValue::String(x), VmValue::String(y)) => Rc::ptr_eq(x, y) || x == y,
616        (VmValue::BuiltinRef(x), VmValue::BuiltinRef(y)) => x == y,
617        // Primitives: identity collapses to structural equality.
618        _ => values_equal(a, b),
619    }
620}
621
622/// Stable identity key for a value. Different allocations produce different
623/// keys; two values with the same heap identity produce the same key. For
624/// primitives the key is derived from the displayed value plus type name so
625/// logically-equal primitives always compare equal.
626pub fn value_identity_key(v: &VmValue) -> String {
627    match v {
628        VmValue::List(x) => format!("list@{:p}", Rc::as_ptr(x)),
629        VmValue::Dict(x) => format!("dict@{:p}", Rc::as_ptr(x)),
630        VmValue::Set(x) => format!("set@{:p}", Rc::as_ptr(x)),
631        VmValue::Closure(x) => format!("closure@{:p}", Rc::as_ptr(x)),
632        VmValue::String(x) => format!("string@{:p}", x.as_ptr()),
633        VmValue::BuiltinRef(name) => format!("builtin@{name}"),
634        other => format!("{}@{}", other.type_name(), other.display()),
635    }
636}
637
638/// Canonical string form used as the keying material for `hash_value`.
639/// Different types never collide (the type name is prepended) and collection
640/// order is preserved so structurally-equal values always produce the same
641/// key. Not intended for cross-process stability; depends on the in-process
642/// iteration order for collections (Dict uses BTreeMap so keys are sorted).
643pub fn value_structural_hash_key(v: &VmValue) -> String {
644    match v {
645        VmValue::Nil => "nil:".into(),
646        VmValue::Bool(b) => format!("bool:{b}"),
647        VmValue::Int(n) => format!("int:{n}"),
648        VmValue::Float(n) => format!("float:{}", n.to_bits()),
649        VmValue::String(s) => format!("string:{}", s),
650        VmValue::Duration(ms) => format!("duration:{ms}"),
651        VmValue::List(items) => {
652            let inner: Vec<String> = items.iter().map(value_structural_hash_key).collect();
653            format!("list:[{}]", inner.join(","))
654        }
655        VmValue::Dict(map) => {
656            let inner: Vec<String> = map
657                .iter()
658                .map(|(k, v)| format!("{k}={}", value_structural_hash_key(v)))
659                .collect();
660            format!("dict:{{{}}}", inner.join(","))
661        }
662        VmValue::Set(items) => {
663            let mut inner: Vec<String> = items.iter().map(value_structural_hash_key).collect();
664            inner.sort();
665            format!("set:{{{}}}", inner.join(","))
666        }
667        other => format!("{}:{}", other.type_name(), other.display()),
668    }
669}
670
671pub fn values_equal(a: &VmValue, b: &VmValue) -> bool {
672    match (a, b) {
673        (VmValue::Int(x), VmValue::Int(y)) => x == y,
674        (VmValue::Float(x), VmValue::Float(y)) => x == y,
675        (VmValue::String(x), VmValue::String(y)) => x == y,
676        (VmValue::Bool(x), VmValue::Bool(y)) => x == y,
677        (VmValue::Nil, VmValue::Nil) => true,
678        (VmValue::Int(x), VmValue::Float(y)) => (*x as f64) == *y,
679        (VmValue::Float(x), VmValue::Int(y)) => *x == (*y as f64),
680        (VmValue::TaskHandle(a), VmValue::TaskHandle(b)) => a == b,
681        (VmValue::Channel(_), VmValue::Channel(_)) => false, // channels are never equal
682        (VmValue::Atomic(a), VmValue::Atomic(b)) => {
683            a.value.load(Ordering::SeqCst) == b.value.load(Ordering::SeqCst)
684        }
685        (VmValue::List(a), VmValue::List(b)) => {
686            a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| values_equal(x, y))
687        }
688        (VmValue::Dict(a), VmValue::Dict(b)) => {
689            a.len() == b.len()
690                && a.iter()
691                    .zip(b.iter())
692                    .all(|((k1, v1), (k2, v2))| k1 == k2 && values_equal(v1, v2))
693        }
694        (
695            VmValue::EnumVariant {
696                enum_name: a_e,
697                variant: a_v,
698                fields: a_f,
699            },
700            VmValue::EnumVariant {
701                enum_name: b_e,
702                variant: b_v,
703                fields: b_f,
704            },
705        ) => {
706            a_e == b_e
707                && a_v == b_v
708                && a_f.len() == b_f.len()
709                && a_f.iter().zip(b_f.iter()).all(|(x, y)| values_equal(x, y))
710        }
711        (
712            VmValue::StructInstance {
713                struct_name: a_s,
714                fields: a_f,
715            },
716            VmValue::StructInstance {
717                struct_name: b_s,
718                fields: b_f,
719            },
720        ) => {
721            a_s == b_s
722                && a_f.len() == b_f.len()
723                && a_f
724                    .iter()
725                    .zip(b_f.iter())
726                    .all(|((k1, v1), (k2, v2))| k1 == k2 && values_equal(v1, v2))
727        }
728        (VmValue::Set(a), VmValue::Set(b)) => {
729            a.len() == b.len() && a.iter().all(|x| b.iter().any(|y| values_equal(x, y)))
730        }
731        (VmValue::Generator(_), VmValue::Generator(_)) => false, // generators are never equal
732        _ => false,
733    }
734}
735
736pub fn compare_values(a: &VmValue, b: &VmValue) -> i32 {
737    match (a, b) {
738        (VmValue::Int(x), VmValue::Int(y)) => x.cmp(y) as i32,
739        (VmValue::Float(x), VmValue::Float(y)) => {
740            if x < y {
741                -1
742            } else if x > y {
743                1
744            } else {
745                0
746            }
747        }
748        (VmValue::Int(x), VmValue::Float(y)) => {
749            let x = *x as f64;
750            if x < *y {
751                -1
752            } else if x > *y {
753                1
754            } else {
755                0
756            }
757        }
758        (VmValue::Float(x), VmValue::Int(y)) => {
759            let y = *y as f64;
760            if *x < y {
761                -1
762            } else if *x > y {
763                1
764            } else {
765                0
766            }
767        }
768        (VmValue::String(x), VmValue::String(y)) => x.cmp(y) as i32,
769        _ => 0,
770    }
771}