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