Skip to main content

harn_vm/value/
core.rs

1use std::collections::HashMap;
2use std::sync::atomic::Ordering;
3use std::sync::Arc;
4use std::{future::Future, pin::Pin};
5
6use crate::harness::VmHarness;
7use crate::mcp::VmMcpClientHandle;
8use crate::BuiltinId;
9
10use super::{
11    VmAtomicHandle, VmChannelHandle, VmClosure, VmError, VmGenerator, VmRange, VmRngHandle,
12    VmStream, VmSyncPermitHandle,
13};
14
15/// An async builtin function for the VM.
16///
17/// Receives an explicit [`crate::vm::AsyncBuiltinCtx`] handle (threaded by the
18/// dispatch loop + the `#[harn_builtin]` macro) so handlers mint child VMs and
19/// forward output through the ctx they were given instead of relying on hidden
20/// task state.
21pub type VmAsyncBuiltinFn = Arc<
22    dyn Fn(
23            crate::vm::AsyncBuiltinCtx,
24            Vec<VmValue>,
25        ) -> Pin<Box<dyn Future<Output = Result<VmValue, VmError>> + Send>>
26        + Send
27        + Sync,
28>;
29
30type Shared<T> = Arc<T>;
31
32/// Backing store for [`VmValue::Dict`]: a persistent, ordered, structurally
33/// shared map.
34///
35/// Replacing the former `BTreeMap` with `imbl::OrdMap` turns the copy-on-write
36/// `Arc::make_mut` clone — performed on every dict mutation whenever the value
37/// is aliased (on the stack, in another local, captured by a closure) — from an
38/// O(n) deep copy of every key and entry into an O(log n) path copy. Ordering
39/// and the read API (`get` / `iter` / `keys` / `values` / `contains_key` /
40/// `range` / `len`) match `BTreeMap`, so dict reads are unchanged. The `Arc`
41/// wrapper is retained so reference identity (`Arc::ptr_eq`) — used by the `===`
42/// operator and `value_identity_key` — keeps its current semantics.
43pub type DictMap = imbl::OrdMap<String, VmValue>;
44
45/// Character count with a byte-length fast path for ASCII text.
46///
47/// Harn exposes string lengths as Unicode scalar counts. ASCII is one byte per
48/// scalar, so cached string `count` / `len` paths can avoid a full iterator
49/// scan without changing behavior for non-ASCII text.
50pub fn string_char_count(text: &str) -> usize {
51    if text.is_ascii() {
52        text.len()
53    } else {
54        text.chars().count()
55    }
56}
57
58/// Indexed runtime layout for a Harn struct instance.
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct StructLayout {
61    struct_name: String,
62    field_names: Vec<String>,
63    field_indexes: HashMap<String, usize>,
64}
65
66impl StructLayout {
67    pub fn new(struct_name: impl Into<String>, field_names: Vec<String>) -> Self {
68        let mut deduped = Vec::with_capacity(field_names.len());
69        let mut field_indexes = HashMap::with_capacity(field_names.len());
70        for field_name in field_names {
71            if field_indexes.contains_key(&field_name) {
72                continue;
73            }
74            let index = deduped.len();
75            field_indexes.insert(field_name.clone(), index);
76            deduped.push(field_name);
77        }
78
79        Self {
80            struct_name: struct_name.into(),
81            field_names: deduped,
82            field_indexes,
83        }
84    }
85
86    pub fn from_map(struct_name: impl Into<String>, fields: &crate::value::DictMap) -> Self {
87        Self::new(struct_name, fields.keys().cloned().collect())
88    }
89
90    pub fn struct_name(&self) -> &str {
91        &self.struct_name
92    }
93
94    pub fn field_names(&self) -> &[String] {
95        &self.field_names
96    }
97
98    pub fn field_index(&self, field_name: &str) -> Option<usize> {
99        if self.field_names.len() <= 8 {
100            return self
101                .field_names
102                .iter()
103                .position(|candidate| candidate == field_name);
104        }
105        self.field_indexes.get(field_name).copied()
106    }
107
108    pub fn with_appended_field(&self, field_name: String) -> Self {
109        if self.field_indexes.contains_key(&field_name) {
110            return self.clone();
111        }
112        let mut field_names = self.field_names.clone();
113        field_names.push(field_name);
114        Self::new(self.struct_name.clone(), field_names)
115    }
116}
117
118/// Runtime payload for a Harn enum variant.
119#[derive(Debug, Clone)]
120pub struct VmEnumVariant {
121    pub enum_name: Shared<str>,
122    pub variant: Shared<str>,
123    pub fields: Shared<Vec<VmValue>>,
124}
125
126impl VmEnumVariant {
127    pub fn has_enum_name(&self, enum_name: &str) -> bool {
128        self.enum_name.as_ref() == enum_name
129    }
130
131    pub fn is_variant(&self, enum_name: &str, variant: &str) -> bool {
132        self.has_enum_name(enum_name) && self.variant.as_ref() == variant
133    }
134}
135
136/// Boxed payload for [`VmValue::BuiltinRefId`].
137///
138/// Pairs the compact [`BuiltinId`] used for direct dispatch with the builtin's
139/// registered name (kept for policy checks, diagnostics, and name-keyed
140/// fallback). Stored behind a `Shared` pointer in the value so the `{ id, name
141/// }` pair does not widen every `VmValue` to its 24-byte footprint.
142#[derive(Debug, Clone)]
143pub struct VmBuiltinRefId {
144    pub id: BuiltinId,
145    pub name: Shared<str>,
146}
147
148/// VM runtime value.
149///
150/// Rare compound payloads use shared pointers so stack/local-slot traffic is
151/// bounded by the common scalar and pointer-sized value shapes. The two
152/// oversized inline payloads — `Range` (a 24-byte `start/end/inclusive`
153/// triple) and `BuiltinRefId` (an id + `Arc<str>` name) — are boxed behind a
154/// `Shared` pointer so no variant exceeds a fat pointer. That keeps `VmValue`
155/// at 24 bytes (down from 32) without inflating the common `Int` / `Float` /
156/// `List` / `Dict` / `String` shapes the interpreter moves on every push,
157/// pop, clone, and local-slot write. Unsafe layouts such as NaN boxing or
158/// tagged pointers are deliberately deferred until Harn has a stronger
159/// object/heap story.
160#[derive(Debug, Clone)]
161pub enum VmValue {
162    Int(i64),
163    Float(f64),
164    /// Exact base-10 decimal (96-bit mantissa, up to 28–29 significant digits)
165    /// for money and other values where binary float rounding is unacceptable.
166    /// Inline (`rust_decimal::Decimal` is `Copy` and 16 bytes, the same width as
167    /// the existing widest variants). Constructed via the `decimal(value)`
168    /// builtin; it is a distinct type from `Int`/`Float` for
169    /// equality/ordering/hashing (a clean island) but promotes `Int` operands
170    /// exactly in arithmetic. See `docs/src/decimal.md`.
171    Decimal(rust_decimal::Decimal),
172    String(Shared<str>),
173    Bytes(Shared<Vec<u8>>),
174    Bool(bool),
175    Nil,
176    List(Shared<Vec<VmValue>>),
177    Dict(Shared<DictMap>),
178    Closure(Shared<VmClosure>),
179    /// Reference to a registered builtin function, used when a builtin name is
180    /// referenced as a value (e.g. `snake_dict.rekey(snake_to_camel)`). The
181    /// contained string is the builtin's registered name.
182    BuiltinRef(Shared<str>),
183    /// Compact builtin reference for callback positions. The boxed
184    /// [`VmBuiltinRefId`] carries the id plus the name for policy,
185    /// diagnostics, and fallback if the ID cannot be used. Boxed so the
186    /// `{ id, name }` pair does not widen every `VmValue`.
187    BuiltinRefId(Shared<VmBuiltinRefId>),
188    Duration(i64),
189    EnumVariant(Shared<VmEnumVariant>),
190    StructInstance {
191        layout: Shared<StructLayout>,
192        fields: Shared<Vec<Option<VmValue>>>,
193    },
194    TaskHandle(Shared<str>),
195    Channel(Shared<VmChannelHandle>),
196    Atomic(Shared<VmAtomicHandle>),
197    Rng(Shared<VmRngHandle>),
198    SyncPermit(Shared<VmSyncPermitHandle>),
199    McpClient(Shared<VmMcpClientHandle>),
200    Set(Shared<Vec<VmValue>>),
201    Generator(Shared<VmGenerator>),
202    Stream(Shared<VmStream>),
203    /// Lazy numeric range. Boxed behind a `Shared` pointer so its 24-byte
204    /// `start/end/inclusive` payload does not set the whole-enum size; cloning
205    /// a range value is then a refcount bump.
206    Range(Shared<VmRange>),
207    /// Lazy iterator handle. Single-pass, fused. See `crate::vm::iter::VmIter`.
208    Iter(crate::vm::iter::VmIterHandle),
209    /// Two-element pair value. Produced by `pair(a, b)`, yielded by the
210    /// Dict iterator source, and (later) by `zip` / `enumerate` combinators.
211    /// Accessed via `.first` / `.second`, and destructurable in
212    /// `for (a, b) in ...` loops.
213    Pair(Shared<(VmValue, VmValue)>),
214    /// Capability handle threaded into `main(harness: Harness)`. The same
215    /// variant carries the root handle and each typed sub-handle (`stdio`,
216    /// `clock`, `fs`, `env`, `random`, `net`) so they share one value shape
217    /// but stay distinguishable via `VmHarness::kind`.
218    Harness(Shared<VmHarness>),
219}
220
221/// Process-wide interned `Arc<str>` for every single-byte ASCII character.
222///
223/// Materializing source text into per-character string values — the supported
224/// idiom for cursor-style scanners (`chars`, `char_at`, `s[i]`) — would
225/// otherwise heap-allocate once per character. Source files are overwhelmingly
226/// ASCII, so interning the 128 single-char strings lets those paths clone a
227/// cheap `Arc` (a refcount bump) instead of allocating, keeping a full-file
228/// scan linear with a low constant factor.
229static ASCII_CHAR_STRINGS: std::sync::LazyLock<[Arc<str>; 128]> = std::sync::LazyLock::new(|| {
230    std::array::from_fn(|byte| {
231        let mut buffer = [0u8; 4];
232        Arc::from((byte as u8 as char).encode_utf8(&mut buffer))
233    })
234});
235
236impl VmValue {
237    /// Canonical `VmValue::String` constructor from anything string-like.
238    ///
239    /// Collapses the ubiquitous `VmValue::String(std::sync::Arc::from(..))`
240    /// spelling to a single call and performs exactly one allocation via
241    /// `Arc::<str>::from(&str)` regardless of whether the input is a `&str`,
242    /// `String`, `&String`, or `Cow<str>`. Prefer this over hand-writing the
243    /// `Arc::from` at call sites.
244    pub fn string(value: impl AsRef<str>) -> Self {
245        VmValue::String(Arc::from(value.as_ref()))
246    }
247
248    /// Builds a `VmValue::String` holding a single character, reusing the
249    /// interned ASCII table (see [`ASCII_CHAR_STRINGS`]) so the common ASCII
250    /// path does not allocate.
251    pub fn char_value(ch: char) -> Self {
252        if ch.is_ascii() {
253            return VmValue::String(Arc::clone(&ASCII_CHAR_STRINGS[ch as usize]));
254        }
255        let mut buffer = [0u8; 4];
256        VmValue::String(Arc::from(ch.encode_utf8(&mut buffer)))
257    }
258
259    /// Materializes a string into a `VmValue::List` of single-character string
260    /// values in one linear pass. Backs both the `chars` builtin and the
261    /// `.chars()` method, and is the cursor-scanner-friendly counterpart to the
262    /// O(n)-per-call `substring` / slice / `s[i]` operations on a `string`.
263    pub fn chars_list(text: &str) -> Self {
264        VmValue::List(Shared::new(text.chars().map(VmValue::char_value).collect()))
265    }
266
267    pub fn enum_variant(
268        enum_name: impl Into<Shared<str>>,
269        variant: impl Into<Shared<str>>,
270        fields: Vec<VmValue>,
271    ) -> Self {
272        VmValue::EnumVariant(Shared::new(VmEnumVariant {
273            enum_name: enum_name.into(),
274            variant: variant.into(),
275            fields: Shared::new(fields),
276        }))
277    }
278
279    pub fn task_handle(id: impl Into<Shared<str>>) -> Self {
280        VmValue::TaskHandle(id.into())
281    }
282
283    /// Construct a boxed [`VmValue::Range`] from a [`VmRange`].
284    pub fn range(range: VmRange) -> Self {
285        VmValue::Range(Shared::new(range))
286    }
287
288    /// Construct a boxed [`VmValue::BuiltinRefId`] from its id and name.
289    pub fn builtin_ref_id(id: BuiltinId, name: impl Into<Shared<str>>) -> Self {
290        VmValue::BuiltinRefId(Shared::new(VmBuiltinRefId {
291            id,
292            name: name.into(),
293        }))
294    }
295
296    /// Construct a [`VmValue::Dict`] from any iterator of `(key, value)`
297    /// entries. Accepts the `BTreeMap` that most builders still assemble (it is
298    /// `IntoIterator<Item = (String, VmValue)>`) and collects it into the
299    /// persistent [`DictMap`], so callers keep their familiar map-building code
300    /// while the stored value gains structural sharing.
301    pub fn dict(entries: impl IntoIterator<Item = (String, VmValue)>) -> Self {
302        VmValue::Dict(Shared::new(entries.into_iter().collect::<DictMap>()))
303    }
304
305    /// Construct a [`VmValue::Dict`] from an already-built [`DictMap`].
306    pub fn dict_map(map: DictMap) -> Self {
307        VmValue::Dict(Shared::new(map))
308    }
309
310    pub fn channel(handle: VmChannelHandle) -> Self {
311        VmValue::Channel(Shared::new(handle))
312    }
313
314    pub fn atomic(handle: VmAtomicHandle) -> Self {
315        VmValue::Atomic(Shared::new(handle))
316    }
317
318    pub fn rng(handle: VmRngHandle) -> Self {
319        VmValue::Rng(Shared::new(handle))
320    }
321
322    pub fn sync_permit(handle: VmSyncPermitHandle) -> Self {
323        VmValue::SyncPermit(Shared::new(handle))
324    }
325
326    pub fn mcp_client(handle: VmMcpClientHandle) -> Self {
327        VmValue::McpClient(Shared::new(handle))
328    }
329
330    pub fn generator(generator: VmGenerator) -> Self {
331        VmValue::Generator(Shared::new(generator))
332    }
333
334    pub fn stream(stream: VmStream) -> Self {
335        VmValue::Stream(Shared::new(stream))
336    }
337
338    pub fn harness(handle: VmHarness) -> Self {
339        VmValue::Harness(Shared::new(handle))
340    }
341
342    pub fn struct_instance(
343        struct_name: impl Into<Shared<str>>,
344        fields: crate::value::DictMap,
345    ) -> Self {
346        Self::struct_instance_from_map(struct_name.into().to_string(), fields)
347    }
348
349    pub fn is_truthy(&self) -> bool {
350        match self {
351            VmValue::Bool(b) => *b,
352            VmValue::Nil => false,
353            VmValue::Int(n) => *n != 0,
354            VmValue::Float(n) => *n != 0.0,
355            VmValue::Decimal(d) => *d != rust_decimal::Decimal::ZERO,
356            VmValue::String(s) => !s.is_empty(),
357            VmValue::Bytes(bytes) => !bytes.is_empty(),
358            VmValue::List(l) => !l.is_empty(),
359            VmValue::Dict(d) => !d.is_empty(),
360            VmValue::Closure(_) => true,
361            VmValue::BuiltinRef(_) => true,
362            VmValue::BuiltinRefId(_) => true,
363            VmValue::Duration(ms) => *ms != 0,
364            VmValue::EnumVariant(_) => true,
365            VmValue::StructInstance { .. } => true,
366            VmValue::TaskHandle(_) => true,
367            VmValue::Channel(_) => true,
368            VmValue::Atomic(_) => true,
369            VmValue::Rng(_) => true,
370            VmValue::SyncPermit(_) => true,
371            VmValue::McpClient(_) => true,
372            VmValue::Set(s) => !s.is_empty(),
373            VmValue::Generator(_) => true,
374            VmValue::Stream(_) => true,
375            // Match Python semantics: range objects are always truthy,
376            // even the empty range (analogous to generators / iterators).
377            VmValue::Range(_) => true,
378            VmValue::Iter(_) => true,
379            VmValue::Pair(_) => true,
380            VmValue::Harness(_) => true,
381        }
382    }
383
384    pub fn type_name(&self) -> &'static str {
385        match self {
386            VmValue::String(_) => "string",
387            VmValue::Bytes(_) => "bytes",
388            VmValue::Int(_) => "int",
389            VmValue::Float(_) => "float",
390            VmValue::Decimal(_) => "decimal",
391            VmValue::Bool(_) => "bool",
392            VmValue::Nil => "nil",
393            VmValue::List(_) => "list",
394            VmValue::Dict(_) => "dict",
395            VmValue::Closure(_) => "closure",
396            VmValue::BuiltinRef(_) => "builtin",
397            VmValue::BuiltinRefId(_) => "builtin",
398            VmValue::Duration(_) => "duration",
399            VmValue::EnumVariant(_) => "enum",
400            VmValue::StructInstance { .. } => "struct",
401            VmValue::TaskHandle(_) => "task_handle",
402            VmValue::Channel(_) => "channel",
403            VmValue::Atomic(_) => "atomic",
404            VmValue::Rng(_) => "rng",
405            VmValue::SyncPermit(_) => "sync_permit",
406            VmValue::McpClient(_) => "mcp_client",
407            VmValue::Set(_) => "set",
408            VmValue::Generator(_) => "generator",
409            VmValue::Stream(_) => "stream",
410            VmValue::Range(_) => "range",
411            VmValue::Iter(_) => "iter",
412            VmValue::Pair(_) => "pair",
413            VmValue::Harness(h) => h.type_name(),
414        }
415    }
416
417    /// Borrows the string contents without allocating when the value is
418    /// already a string. Non-string values are rendered with `display()`,
419    /// matching the coercion callers apply at string boundaries. Hot string
420    /// builtins (regex, split, contains) use this to avoid cloning the
421    /// subject text on every call.
422    pub fn as_str_cow(&self) -> std::borrow::Cow<'_, str> {
423        match self {
424            VmValue::String(s) => std::borrow::Cow::Borrowed(s),
425            other => std::borrow::Cow::Owned(other.display()),
426        }
427    }
428
429    pub fn struct_name(&self) -> Option<&str> {
430        match self {
431            VmValue::StructInstance { layout, .. } => Some(layout.struct_name()),
432            _ => None,
433        }
434    }
435
436    pub fn struct_field(&self, field_name: &str) -> Option<&VmValue> {
437        match self {
438            VmValue::StructInstance { layout, fields } => layout
439                .field_index(field_name)
440                .and_then(|index| fields.get(index))
441                .and_then(Option::as_ref),
442            _ => None,
443        }
444    }
445
446    pub fn struct_fields_map(&self) -> Option<crate::value::DictMap> {
447        match self {
448            VmValue::StructInstance { layout, fields } => {
449                Some(struct_fields_to_map(layout, fields))
450            }
451            _ => None,
452        }
453    }
454
455    pub fn struct_instance_from_map(
456        struct_name: impl Into<String>,
457        fields: crate::value::DictMap,
458    ) -> Self {
459        let layout = Shared::new(StructLayout::from_map(struct_name, &fields));
460        let slots = layout
461            .field_names()
462            .iter()
463            .map(|name| fields.get(name).cloned())
464            .collect();
465        VmValue::StructInstance {
466            layout,
467            fields: Shared::new(slots),
468        }
469    }
470
471    pub fn struct_instance_with_layout(
472        struct_name: impl Into<String>,
473        field_names: Vec<String>,
474        field_values: crate::value::DictMap,
475    ) -> Self {
476        let layout = Shared::new(StructLayout::new(struct_name, field_names));
477        let fields = layout
478            .field_names()
479            .iter()
480            .map(|name| field_values.get(name).cloned())
481            .collect();
482        VmValue::StructInstance {
483            layout,
484            fields: Shared::new(fields),
485        }
486    }
487
488    pub fn struct_instance_with_property(&self, field_name: &str, value: VmValue) -> Option<Self> {
489        let VmValue::StructInstance { layout, fields } = self else {
490            return None;
491        };
492
493        let mut new_fields = fields.as_ref().clone();
494        let layout = match layout.field_index(field_name) {
495            Some(index) => {
496                if index >= new_fields.len() {
497                    new_fields.resize(index + 1, None);
498                }
499                new_fields[index] = Some(value);
500                Shared::clone(layout)
501            }
502            None => {
503                let new_layout = Shared::new(layout.with_appended_field(field_name.to_string()));
504                new_fields.push(Some(value));
505                new_layout
506            }
507        };
508
509        Some(VmValue::StructInstance {
510            layout,
511            fields: Shared::new(new_fields),
512        })
513    }
514
515    pub fn display(&self) -> String {
516        let mut out = String::new();
517        self.write_display(&mut out);
518        out
519    }
520
521    /// Writes the display representation directly into `out`,
522    /// avoiding intermediate Vec<String> allocations for collections.
523    pub fn write_display(&self, out: &mut String) {
524        use std::fmt::Write;
525
526        match self {
527            VmValue::Int(n) => {
528                let _ = write!(out, "{n}");
529            }
530            VmValue::Float(n) => {
531                if *n == (*n as i64) as f64 && n.abs() < 1e15 {
532                    let _ = write!(out, "{n:.1}");
533                } else {
534                    let _ = write!(out, "{n}");
535                }
536            }
537            // Render the decimal at its stored scale (e.g. `1.50` stays `1.50`),
538            // which is what money formatting expects. Equality normalizes scale,
539            // so `1.5` and `1.50` are still equal even though they display
540            // differently.
541            VmValue::Decimal(d) => {
542                let _ = write!(out, "{d}");
543            }
544            VmValue::String(s) => out.push_str(s),
545            VmValue::Bytes(bytes) => {
546                const MAX_PREVIEW_BYTES: usize = 32;
547
548                out.push_str("b\"");
549                for byte in bytes.iter().take(MAX_PREVIEW_BYTES) {
550                    let _ = write!(out, "{byte:02x}");
551                }
552                if bytes.len() > MAX_PREVIEW_BYTES {
553                    let _ = write!(out, "...+{}", bytes.len() - MAX_PREVIEW_BYTES);
554                }
555                out.push('"');
556            }
557            VmValue::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
558            VmValue::Nil => out.push_str("nil"),
559            VmValue::List(items) => {
560                out.push('[');
561                crate::value::recursion::guard_recursion(|| {
562                    for (i, item) in items.iter().enumerate() {
563                        if i > 0 {
564                            out.push_str(", ");
565                        }
566                        item.write_display(out);
567                    }
568                });
569                out.push(']');
570            }
571            VmValue::Dict(map) => {
572                out.push('{');
573                crate::value::recursion::guard_recursion(|| {
574                    for (i, (k, v)) in map.iter().enumerate() {
575                        if i > 0 {
576                            out.push_str(", ");
577                        }
578                        out.push_str(k);
579                        out.push_str(": ");
580                        v.write_display(out);
581                    }
582                });
583                out.push('}');
584            }
585            VmValue::Closure(c) => {
586                let names: Vec<&str> = c.func.param_names().collect();
587                let _ = write!(out, "<fn({})>", names.join(", "));
588            }
589            VmValue::BuiltinRef(name) => {
590                let _ = write!(out, "<builtin {name}>");
591            }
592            VmValue::BuiltinRefId(r) => {
593                let _ = write!(out, "<builtin {}>", r.name);
594            }
595            VmValue::Duration(ms) => {
596                let sign = if *ms < 0 { "-" } else { "" };
597                let abs_ms = ms.unsigned_abs();
598                if abs_ms >= 604_800_000 && abs_ms % 604_800_000 == 0 {
599                    let _ = write!(out, "{}{}w", sign, abs_ms / 604_800_000);
600                } else if abs_ms >= 86_400_000 && abs_ms % 86_400_000 == 0 {
601                    let _ = write!(out, "{}{}d", sign, abs_ms / 86_400_000);
602                } else if abs_ms >= 3_600_000 && abs_ms % 3_600_000 == 0 {
603                    let _ = write!(out, "{}{}h", sign, abs_ms / 3_600_000);
604                } else if abs_ms >= 60_000 && abs_ms % 60_000 == 0 {
605                    let _ = write!(out, "{}{}m", sign, abs_ms / 60_000);
606                } else if abs_ms >= 1000 && abs_ms % 1000 == 0 {
607                    let _ = write!(out, "{}{}s", sign, abs_ms / 1000);
608                } else {
609                    let _ = write!(out, "{sign}{abs_ms}ms");
610                }
611            }
612            VmValue::EnumVariant(enum_variant) => {
613                if enum_variant.fields.is_empty() {
614                    let _ = write!(out, "{}.{}", enum_variant.enum_name, enum_variant.variant);
615                } else {
616                    let _ = write!(out, "{}.{}(", enum_variant.enum_name, enum_variant.variant);
617                    crate::value::recursion::guard_recursion(|| {
618                        for (i, v) in enum_variant.fields.iter().enumerate() {
619                            if i > 0 {
620                                out.push_str(", ");
621                            }
622                            v.write_display(out);
623                        }
624                    });
625                    out.push(')');
626                }
627            }
628            VmValue::StructInstance { layout, fields } => {
629                let _ = write!(out, "{} {{", layout.struct_name());
630                crate::value::recursion::guard_recursion(|| {
631                    for (i, (k, v)) in struct_fields_to_map(layout, fields).iter().enumerate() {
632                        if i > 0 {
633                            out.push_str(", ");
634                        }
635                        out.push_str(k);
636                        out.push_str(": ");
637                        v.write_display(out);
638                    }
639                });
640                out.push('}');
641            }
642            VmValue::TaskHandle(id) => {
643                let _ = write!(out, "<task:{id}>");
644            }
645            VmValue::Channel(ch) => {
646                let _ = write!(out, "<channel:{}>", ch.name);
647            }
648            VmValue::Atomic(a) => {
649                let _ = write!(out, "<atomic:{}>", a.value.load(Ordering::SeqCst));
650            }
651            VmValue::Rng(_) => {
652                out.push_str("<rng>");
653            }
654            VmValue::SyncPermit(p) => {
655                let _ = write!(out, "<sync_permit:{}:{}>", p.kind(), p.key());
656            }
657            VmValue::McpClient(c) => {
658                let _ = write!(out, "<mcp_client:{}>", c.name);
659            }
660            VmValue::Set(items) => {
661                out.push_str("set(");
662                crate::value::recursion::guard_recursion(|| {
663                    for (i, item) in items.iter().enumerate() {
664                        if i > 0 {
665                            out.push_str(", ");
666                        }
667                        item.write_display(out);
668                    }
669                });
670                out.push(')');
671            }
672            VmValue::Generator(g) => {
673                if g.is_done() {
674                    out.push_str("<generator (done)>");
675                } else {
676                    out.push_str("<generator>");
677                }
678            }
679            VmValue::Stream(s) => {
680                if s.is_done() {
681                    out.push_str("<stream (done)>");
682                } else {
683                    out.push_str("<stream>");
684                }
685            }
686            // Print form mirrors source syntax: `1 to 5` / `0 to 3 exclusive`.
687            // `.to_list()` is the explicit path to materialize for display.
688            VmValue::Range(r) => {
689                let _ = write!(out, "{} to {}", r.start, r.end);
690                if !r.inclusive {
691                    out.push_str(" exclusive");
692                }
693            }
694            VmValue::Iter(h) => {
695                if matches!(&*h.lock(), crate::vm::iter::VmIter::Exhausted) {
696                    out.push_str("<iter (exhausted)>");
697                } else {
698                    out.push_str("<iter>");
699                }
700            }
701            VmValue::Harness(h) => {
702                let _ = write!(out, "<{}>", h.type_name());
703            }
704            VmValue::Pair(p) => {
705                out.push('(');
706                crate::value::recursion::guard_recursion(|| {
707                    p.0.write_display(out);
708                    out.push_str(", ");
709                    p.1.write_display(out);
710                });
711                out.push(')');
712            }
713        }
714    }
715
716    /// Get the value as a [`DictMap`] reference, if it's a Dict.
717    pub fn as_dict(&self) -> Option<&DictMap> {
718        if let VmValue::Dict(d) = self {
719            Some(d)
720        } else {
721            None
722        }
723    }
724
725    pub fn as_int(&self) -> Option<i64> {
726        if let VmValue::Int(n) = self {
727            Some(*n)
728        } else {
729            None
730        }
731    }
732
733    pub fn as_bytes(&self) -> Option<&[u8]> {
734        if let VmValue::Bytes(bytes) = self {
735            Some(bytes.as_slice())
736        } else {
737            None
738        }
739    }
740}
741
742pub fn struct_fields_to_map(
743    layout: &StructLayout,
744    fields: &[Option<VmValue>],
745) -> crate::value::DictMap {
746    layout
747        .field_names()
748        .iter()
749        .enumerate()
750        .filter_map(|(index, name)| {
751            fields
752                .get(index)
753                .and_then(Option::as_ref)
754                .map(|value| (name.clone(), value.clone()))
755        })
756        .collect()
757}
758
759/// Sync builtin function for the VM.
760pub type VmBuiltinFn =
761    Arc<dyn Fn(&[VmValue], &mut String) -> Result<VmValue, VmError> + Send + Sync>;