Skip to main content

sema_core/
value.rs

1use std::any::Any;
2use std::cell::{Cell, RefCell};
3use std::collections::BTreeMap;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6use std::rc::Rc;
7
8use hashbrown::HashMap as SpurMap;
9use lasso::{Rodeo, Spur};
10
11use crate::error::SemaError;
12use crate::EvalContext;
13
14// Compile-time check: NaN-boxing requires 64-bit pointers that fit in 48-bit VA space.
15// 32-bit platforms cannot use this representation (pointers don't fit the encoding).
16// wasm32 is exempted because its 32-bit pointers always fit in 45 bits.
17#[cfg(not(any(target_pointer_width = "64", target_arch = "wasm32")))]
18compile_error!("sema-core NaN-boxed Value requires a 64-bit platform (or wasm32)");
19
20// ── String interning ──────────────────────────────────────────────
21
22thread_local! {
23    static INTERNER: RefCell<Rodeo> = RefCell::new(Rodeo::default());
24}
25
26/// Intern a string, returning a Spur key.
27pub fn intern(s: &str) -> Spur {
28    INTERNER.with(|r| r.borrow_mut().get_or_intern(s))
29}
30
31/// Resolve a Spur key back to a String.
32pub fn resolve(spur: Spur) -> String {
33    INTERNER.with(|r| r.borrow().resolve(&spur).to_string())
34}
35
36/// Resolve a Spur and call f with the &str, avoiding allocation.
37pub fn with_resolved<F, R>(spur: Spur, f: F) -> R
38where
39    F: FnOnce(&str) -> R,
40{
41    INTERNER.with(|r| {
42        let interner = r.borrow();
43        f(interner.resolve(&spur))
44    })
45}
46
47/// Return interner statistics: (count, estimated_memory_bytes).
48pub fn interner_stats() -> (usize, usize) {
49    INTERNER.with(|r| {
50        let interner = r.borrow();
51        let count = interner.len();
52        let bytes = count * 16; // approximate: Spur (4 bytes) + average string data
53        (count, bytes)
54    })
55}
56
57/// Compare two Spurs by their resolved string content (lexicographic).
58pub fn compare_spurs(a: Spur, b: Spur) -> std::cmp::Ordering {
59    if a == b {
60        return std::cmp::Ordering::Equal;
61    }
62    INTERNER.with(|r| {
63        let interner = r.borrow();
64        interner.resolve(&a).cmp(interner.resolve(&b))
65    })
66}
67
68// ── Supporting types (unchanged public API) ───────────────────────
69
70/// A native function callable from Sema.
71pub type NativeFnInner = dyn Fn(&EvalContext, &[Value]) -> Result<Value, SemaError>;
72
73pub struct NativeFn {
74    pub name: String,
75    pub func: Box<NativeFnInner>,
76    pub payload: Option<Rc<dyn Any>>,
77}
78
79impl NativeFn {
80    pub fn simple(
81        name: impl Into<String>,
82        f: impl Fn(&[Value]) -> Result<Value, SemaError> + 'static,
83    ) -> Self {
84        Self {
85            name: name.into(),
86            func: Box::new(move |_ctx, args| f(args)),
87            payload: None,
88        }
89    }
90
91    pub fn with_ctx(
92        name: impl Into<String>,
93        f: impl Fn(&EvalContext, &[Value]) -> Result<Value, SemaError> + 'static,
94    ) -> Self {
95        Self {
96            name: name.into(),
97            func: Box::new(f),
98            payload: None,
99        }
100    }
101
102    pub fn with_payload(
103        name: impl Into<String>,
104        payload: Rc<dyn Any>,
105        f: impl Fn(&EvalContext, &[Value]) -> Result<Value, SemaError> + 'static,
106    ) -> Self {
107        Self {
108            name: name.into(),
109            func: Box::new(f),
110            payload: Some(payload),
111        }
112    }
113}
114
115impl fmt::Debug for NativeFn {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "<native-fn {}>", self.name)
118    }
119}
120
121/// A user-defined lambda.
122#[derive(Debug, Clone)]
123pub struct Lambda {
124    pub params: Vec<Spur>,
125    pub rest_param: Option<Spur>,
126    pub body: Vec<Value>,
127    pub env: Env,
128    pub name: Option<Spur>,
129}
130
131/// A macro definition.
132#[derive(Debug, Clone)]
133pub struct Macro {
134    pub params: Vec<Spur>,
135    pub rest_param: Option<Spur>,
136    pub body: Vec<Value>,
137    pub name: Spur,
138}
139
140/// A lazy promise: delay/force with memoization.
141pub struct Thunk {
142    pub body: Value,
143    pub forced: RefCell<Option<Value>>,
144}
145
146impl fmt::Debug for Thunk {
147    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148        if self.forced.borrow().is_some() {
149            write!(f, "<promise (forced)>")
150        } else {
151            write!(f, "<promise>")
152        }
153    }
154}
155
156impl Clone for Thunk {
157    fn clone(&self) -> Self {
158        Thunk {
159            body: self.body.clone(),
160            forced: RefCell::new(self.forced.borrow().clone()),
161        }
162    }
163}
164
165/// A record: tagged product type created by define-record-type.
166#[derive(Debug, Clone)]
167pub struct Record {
168    pub type_tag: Spur,
169    pub fields: Vec<Value>,
170}
171
172/// A message role in a conversation.
173#[derive(Debug, Clone, PartialEq, Eq)]
174pub enum Role {
175    System,
176    User,
177    Assistant,
178    Tool,
179}
180
181impl fmt::Display for Role {
182    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183        match self {
184            Role::System => write!(f, "system"),
185            Role::User => write!(f, "user"),
186            Role::Assistant => write!(f, "assistant"),
187            Role::Tool => write!(f, "tool"),
188        }
189    }
190}
191
192/// A base64-encoded image attachment.
193#[derive(Debug, Clone)]
194pub struct ImageAttachment {
195    pub data: String,
196    pub media_type: String,
197}
198
199/// A single message in a conversation.
200#[derive(Debug, Clone)]
201pub struct Message {
202    pub role: Role,
203    pub content: String,
204    /// Optional image attachments (base64-encoded).
205    pub images: Vec<ImageAttachment>,
206}
207
208/// A prompt: a structured list of messages.
209#[derive(Debug, Clone)]
210pub struct Prompt {
211    pub messages: Vec<Message>,
212}
213
214/// A conversation: immutable history + provider config.
215#[derive(Debug, Clone)]
216pub struct Conversation {
217    pub messages: Vec<Message>,
218    pub model: String,
219    pub metadata: BTreeMap<String, String>,
220}
221
222/// A tool definition for LLM function calling.
223#[derive(Debug, Clone)]
224pub struct ToolDefinition {
225    pub name: String,
226    pub description: String,
227    pub parameters: Value,
228    pub handler: Value,
229}
230
231/// An agent: system prompt + tools + config for autonomous loops.
232#[derive(Debug, Clone)]
233pub struct Agent {
234    pub name: String,
235    pub system: String,
236    pub tools: Vec<Value>,
237    pub max_turns: usize,
238    pub model: String,
239}
240
241// ── NaN-boxing constants ──────────────────────────────────────────
242
243// IEEE 754 double layout:
244//   bit 63:     sign
245//   bits 62-52: exponent (11 bits)
246//   bits 51-0:  mantissa (52 bits), bit 51 = quiet NaN bit
247//
248// Boxed (non-float) values use: sign=1, exp=all 1s, quiet=1
249//   Then bits 50-45 = TAG (6 bits), bits 44-0 = PAYLOAD (45 bits)
250
251/// Mask for checking if a value is boxed (sign + exponent + quiet bit)
252const BOX_MASK: u64 = 0xFFF8_0000_0000_0000;
253
254/// The 45-bit payload mask
255const PAYLOAD_MASK: u64 = (1u64 << 45) - 1; // 0x1FFF_FFFF_FFFF
256
257/// Sign-extension bit for 45-bit signed integers
258const INT_SIGN_BIT: u64 = 1u64 << 44;
259
260/// 6-bit mask for extracting the tag from a boxed value (bits 50-45).
261const TAG_MASK_6BIT: u64 = 0x3F;
262
263/// Canonical quiet NaN (sign=0) — used for NaN float values to avoid collision with boxed
264const CANONICAL_NAN: u64 = 0x7FF8_0000_0000_0000;
265
266// Tags (6 bits, encoded in bits 50-45)
267const TAG_NIL: u64 = 0;
268const TAG_FALSE: u64 = 1;
269const TAG_TRUE: u64 = 2;
270const TAG_INT_SMALL: u64 = 3;
271const TAG_CHAR: u64 = 4;
272const TAG_SYMBOL: u64 = 5;
273const TAG_KEYWORD: u64 = 6;
274const TAG_INT_BIG: u64 = 7;
275const TAG_STRING: u64 = 8;
276const TAG_LIST: u64 = 9;
277const TAG_VECTOR: u64 = 10;
278const TAG_MAP: u64 = 11;
279const TAG_HASHMAP: u64 = 12;
280const TAG_LAMBDA: u64 = 13;
281const TAG_MACRO: u64 = 14;
282pub const TAG_NATIVE_FN: u64 = 15;
283const TAG_PROMPT: u64 = 16;
284const TAG_MESSAGE: u64 = 17;
285const TAG_CONVERSATION: u64 = 18;
286const TAG_TOOL_DEF: u64 = 19;
287const TAG_AGENT: u64 = 20;
288const TAG_THUNK: u64 = 21;
289const TAG_RECORD: u64 = 22;
290const TAG_BYTEVECTOR: u64 = 23;
291
292/// Small-int range: [-2^44, 2^44 - 1] = [-17_592_186_044_416, +17_592_186_044_415]
293const SMALL_INT_MIN: i64 = -(1i64 << 44);
294const SMALL_INT_MAX: i64 = (1i64 << 44) - 1;
295
296// ── Public NaN-boxing constants for VM use ────────────────────────
297
298/// Tag + box combined mask: upper 19 bits (sign + exponent + quiet + 6-bit tag).
299pub const NAN_TAG_MASK: u64 = BOX_MASK | (TAG_MASK_6BIT << 45); // 0xFFFF_E000_0000_0000
300
301/// The expected upper bits for a small int value: BOX_MASK | (TAG_INT_SMALL << 45).
302pub const NAN_INT_SMALL_PATTERN: u64 = BOX_MASK | (TAG_INT_SMALL << 45);
303
304/// Public payload mask (45 bits).
305pub const NAN_PAYLOAD_MASK: u64 = PAYLOAD_MASK;
306
307/// Sign bit within the 45-bit payload (bit 44) — for sign-extending small ints.
308pub const NAN_INT_SIGN_BIT: u64 = INT_SIGN_BIT;
309
310/// Number of payload bits in NaN-boxed values (45).
311pub const NAN_PAYLOAD_BITS: u32 = 45;
312
313// ── Helpers for encoding/decoding ─────────────────────────────────
314
315#[inline(always)]
316fn make_boxed(tag: u64, payload: u64) -> u64 {
317    BOX_MASK | (tag << 45) | (payload & PAYLOAD_MASK)
318}
319
320#[inline(always)]
321fn is_boxed(bits: u64) -> bool {
322    (bits & BOX_MASK) == BOX_MASK
323}
324
325#[inline(always)]
326fn get_tag(bits: u64) -> u64 {
327    (bits >> 45) & TAG_MASK_6BIT
328}
329
330#[inline(always)]
331fn get_payload(bits: u64) -> u64 {
332    bits & PAYLOAD_MASK
333}
334
335#[inline(always)]
336fn ptr_to_payload(ptr: *const u8) -> u64 {
337    let raw = ptr as u64;
338    debug_assert!(raw & 0x7 == 0, "pointer not 8-byte aligned: 0x{:x}", raw);
339    debug_assert!(
340        raw >> 48 == 0,
341        "pointer exceeds 48-bit VA space: 0x{:x}",
342        raw
343    );
344    raw >> 3
345}
346
347#[inline(always)]
348fn payload_to_ptr(payload: u64) -> *const u8 {
349    (payload << 3) as *const u8
350}
351
352// ── ValueView: pattern-matching enum ──────────────────────────────
353
354/// A view of a NaN-boxed Value for pattern matching.
355/// Returned by `Value::view()`. Heap types hold Rc (refcount bumped).
356pub enum ValueView {
357    Nil,
358    Bool(bool),
359    Int(i64),
360    Float(f64),
361    String(Rc<String>),
362    Symbol(Spur),
363    Keyword(Spur),
364    Char(char),
365    List(Rc<Vec<Value>>),
366    Vector(Rc<Vec<Value>>),
367    Map(Rc<BTreeMap<Value, Value>>),
368    HashMap(Rc<hashbrown::HashMap<Value, Value>>),
369    Lambda(Rc<Lambda>),
370    Macro(Rc<Macro>),
371    NativeFn(Rc<NativeFn>),
372    Prompt(Rc<Prompt>),
373    Message(Rc<Message>),
374    Conversation(Rc<Conversation>),
375    ToolDef(Rc<ToolDefinition>),
376    Agent(Rc<Agent>),
377    Thunk(Rc<Thunk>),
378    Record(Rc<Record>),
379    Bytevector(Rc<Vec<u8>>),
380}
381
382// ── The NaN-boxed Value type ──────────────────────────────────────
383
384/// The core Value type for all Sema data.
385/// NaN-boxed: stored as 8 bytes. Floats stored directly,
386/// everything else encoded in quiet-NaN payload space.
387#[repr(transparent)]
388pub struct Value(u64);
389
390// ── Constructors ──────────────────────────────────────────────────
391
392impl Value {
393    // -- Immediate constructors --
394
395    pub const NIL: Value = Value(make_boxed_const(TAG_NIL, 0));
396    pub const TRUE: Value = Value(make_boxed_const(TAG_TRUE, 0));
397    pub const FALSE: Value = Value(make_boxed_const(TAG_FALSE, 0));
398
399    #[inline(always)]
400    pub fn nil() -> Value {
401        Value::NIL
402    }
403
404    #[inline(always)]
405    pub fn bool(b: bool) -> Value {
406        if b {
407            Value::TRUE
408        } else {
409            Value::FALSE
410        }
411    }
412
413    #[inline(always)]
414    pub fn int(n: i64) -> Value {
415        if (SMALL_INT_MIN..=SMALL_INT_MAX).contains(&n) {
416            // Encode as small int (45-bit two's complement)
417            let payload = (n as u64) & PAYLOAD_MASK;
418            Value(make_boxed(TAG_INT_SMALL, payload))
419        } else {
420            // Out of range: heap-allocate
421            let rc = Rc::new(n);
422            let ptr = Rc::into_raw(rc) as *const u8;
423            Value(make_boxed(TAG_INT_BIG, ptr_to_payload(ptr)))
424        }
425    }
426
427    #[inline(always)]
428    pub fn float(f: f64) -> Value {
429        let bits = f.to_bits();
430        if f.is_nan() {
431            // Canonicalize NaN to avoid collision with boxed patterns
432            Value(CANONICAL_NAN)
433        } else {
434            // Check: a non-NaN float could still have the BOX_MASK pattern
435            // This happens for negative infinity and some subnormals — but
436            // negative infinity is 0xFFF0_0000_0000_0000 which does NOT match
437            // BOX_MASK (0xFFF8...) because bit 51 (quiet) is 0.
438            // In IEEE 754, the only values with all exponent bits set AND quiet bit set
439            // are quiet NaNs, which we've already canonicalized above.
440            debug_assert!(
441                !is_boxed(bits),
442                "non-NaN float collides with boxed pattern: {:?} = 0x{:016x}",
443                f,
444                bits
445            );
446            Value(bits)
447        }
448    }
449
450    #[inline(always)]
451    pub fn char(c: char) -> Value {
452        Value(make_boxed(TAG_CHAR, c as u64))
453    }
454
455    #[inline(always)]
456    pub fn symbol_from_spur(spur: Spur) -> Value {
457        let bits: u32 = unsafe { std::mem::transmute(spur) };
458        Value(make_boxed(TAG_SYMBOL, bits as u64))
459    }
460
461    pub fn symbol(s: &str) -> Value {
462        Value::symbol_from_spur(intern(s))
463    }
464
465    #[inline(always)]
466    pub fn keyword_from_spur(spur: Spur) -> Value {
467        let bits: u32 = unsafe { std::mem::transmute(spur) };
468        Value(make_boxed(TAG_KEYWORD, bits as u64))
469    }
470
471    pub fn keyword(s: &str) -> Value {
472        Value::keyword_from_spur(intern(s))
473    }
474
475    // -- Heap constructors --
476
477    fn from_rc_ptr<T>(tag: u64, rc: Rc<T>) -> Value {
478        let ptr = Rc::into_raw(rc) as *const u8;
479        Value(make_boxed(tag, ptr_to_payload(ptr)))
480    }
481
482    pub fn string(s: &str) -> Value {
483        Value::from_rc_ptr(TAG_STRING, Rc::new(s.to_string()))
484    }
485
486    pub fn string_from_rc(rc: Rc<String>) -> Value {
487        Value::from_rc_ptr(TAG_STRING, rc)
488    }
489
490    pub fn list(v: Vec<Value>) -> Value {
491        Value::from_rc_ptr(TAG_LIST, Rc::new(v))
492    }
493
494    pub fn list_from_rc(rc: Rc<Vec<Value>>) -> Value {
495        Value::from_rc_ptr(TAG_LIST, rc)
496    }
497
498    pub fn vector(v: Vec<Value>) -> Value {
499        Value::from_rc_ptr(TAG_VECTOR, Rc::new(v))
500    }
501
502    pub fn vector_from_rc(rc: Rc<Vec<Value>>) -> Value {
503        Value::from_rc_ptr(TAG_VECTOR, rc)
504    }
505
506    pub fn map(m: BTreeMap<Value, Value>) -> Value {
507        Value::from_rc_ptr(TAG_MAP, Rc::new(m))
508    }
509
510    pub fn map_from_rc(rc: Rc<BTreeMap<Value, Value>>) -> Value {
511        Value::from_rc_ptr(TAG_MAP, rc)
512    }
513
514    pub fn hashmap(entries: Vec<(Value, Value)>) -> Value {
515        let map: hashbrown::HashMap<Value, Value> = entries.into_iter().collect();
516        Value::from_rc_ptr(TAG_HASHMAP, Rc::new(map))
517    }
518
519    pub fn hashmap_from_rc(rc: Rc<hashbrown::HashMap<Value, Value>>) -> Value {
520        Value::from_rc_ptr(TAG_HASHMAP, rc)
521    }
522
523    pub fn lambda(l: Lambda) -> Value {
524        Value::from_rc_ptr(TAG_LAMBDA, Rc::new(l))
525    }
526
527    pub fn lambda_from_rc(rc: Rc<Lambda>) -> Value {
528        Value::from_rc_ptr(TAG_LAMBDA, rc)
529    }
530
531    pub fn macro_val(m: Macro) -> Value {
532        Value::from_rc_ptr(TAG_MACRO, Rc::new(m))
533    }
534
535    pub fn macro_from_rc(rc: Rc<Macro>) -> Value {
536        Value::from_rc_ptr(TAG_MACRO, rc)
537    }
538
539    pub fn native_fn(f: NativeFn) -> Value {
540        Value::from_rc_ptr(TAG_NATIVE_FN, Rc::new(f))
541    }
542
543    pub fn native_fn_from_rc(rc: Rc<NativeFn>) -> Value {
544        Value::from_rc_ptr(TAG_NATIVE_FN, rc)
545    }
546
547    pub fn prompt(p: Prompt) -> Value {
548        Value::from_rc_ptr(TAG_PROMPT, Rc::new(p))
549    }
550
551    pub fn prompt_from_rc(rc: Rc<Prompt>) -> Value {
552        Value::from_rc_ptr(TAG_PROMPT, rc)
553    }
554
555    pub fn message(m: Message) -> Value {
556        Value::from_rc_ptr(TAG_MESSAGE, Rc::new(m))
557    }
558
559    pub fn message_from_rc(rc: Rc<Message>) -> Value {
560        Value::from_rc_ptr(TAG_MESSAGE, rc)
561    }
562
563    pub fn conversation(c: Conversation) -> Value {
564        Value::from_rc_ptr(TAG_CONVERSATION, Rc::new(c))
565    }
566
567    pub fn conversation_from_rc(rc: Rc<Conversation>) -> Value {
568        Value::from_rc_ptr(TAG_CONVERSATION, rc)
569    }
570
571    pub fn tool_def(t: ToolDefinition) -> Value {
572        Value::from_rc_ptr(TAG_TOOL_DEF, Rc::new(t))
573    }
574
575    pub fn tool_def_from_rc(rc: Rc<ToolDefinition>) -> Value {
576        Value::from_rc_ptr(TAG_TOOL_DEF, rc)
577    }
578
579    pub fn agent(a: Agent) -> Value {
580        Value::from_rc_ptr(TAG_AGENT, Rc::new(a))
581    }
582
583    pub fn agent_from_rc(rc: Rc<Agent>) -> Value {
584        Value::from_rc_ptr(TAG_AGENT, rc)
585    }
586
587    pub fn thunk(t: Thunk) -> Value {
588        Value::from_rc_ptr(TAG_THUNK, Rc::new(t))
589    }
590
591    pub fn thunk_from_rc(rc: Rc<Thunk>) -> Value {
592        Value::from_rc_ptr(TAG_THUNK, rc)
593    }
594
595    pub fn record(r: Record) -> Value {
596        Value::from_rc_ptr(TAG_RECORD, Rc::new(r))
597    }
598
599    pub fn record_from_rc(rc: Rc<Record>) -> Value {
600        Value::from_rc_ptr(TAG_RECORD, rc)
601    }
602
603    pub fn bytevector(bytes: Vec<u8>) -> Value {
604        Value::from_rc_ptr(TAG_BYTEVECTOR, Rc::new(bytes))
605    }
606
607    pub fn bytevector_from_rc(rc: Rc<Vec<u8>>) -> Value {
608        Value::from_rc_ptr(TAG_BYTEVECTOR, rc)
609    }
610}
611
612// Const-compatible boxed encoding (no function calls)
613const fn make_boxed_const(tag: u64, payload: u64) -> u64 {
614    BOX_MASK | (tag << 45) | (payload & PAYLOAD_MASK)
615}
616
617// ── Accessors ─────────────────────────────────────────────────────
618
619impl Value {
620    /// Get the raw bits (for debugging/testing).
621    #[inline(always)]
622    pub fn raw_bits(&self) -> u64 {
623        self.0
624    }
625
626    /// Construct a Value from raw NaN-boxed bits.
627    ///
628    /// # Safety
629    ///
630    /// Caller must ensure `bits` represents a valid NaN-boxed value.
631    /// For immediate types (nil, bool, int, symbol, keyword, char), this is always safe.
632    /// For heap-pointer types, the encoded pointer must be valid and have its Rc ownership
633    /// accounted for (i.e., the caller must ensure the refcount is correct).
634    #[inline(always)]
635    pub unsafe fn from_raw_bits(bits: u64) -> Value {
636        Value(bits)
637    }
638
639    /// Get the NaN-boxing tag of a boxed value (0-63).
640    /// Returns `None` for non-boxed values (floats).
641    #[inline(always)]
642    pub fn raw_tag(&self) -> Option<u64> {
643        if is_boxed(self.0) {
644            Some(get_tag(self.0))
645        } else {
646            None
647        }
648    }
649
650    /// Borrow the underlying NativeFn without bumping the Rc refcount.
651    /// SAFETY: The returned reference is valid as long as this Value is alive.
652    #[inline(always)]
653    pub fn as_native_fn_ref(&self) -> Option<&NativeFn> {
654        if is_boxed(self.0) && get_tag(self.0) == TAG_NATIVE_FN {
655            Some(unsafe { self.borrow_ref::<NativeFn>() })
656        } else {
657            None
658        }
659    }
660
661    /// Check if this is a float (non-boxed).
662    #[inline(always)]
663    pub fn is_float(&self) -> bool {
664        !is_boxed(self.0)
665    }
666
667    /// Recover an Rc<T> pointer from the payload WITHOUT consuming ownership.
668    /// This increments the refcount (returns a new Rc).
669    #[inline(always)]
670    unsafe fn get_rc<T>(&self) -> Rc<T> {
671        let payload = get_payload(self.0);
672        let ptr = payload_to_ptr(payload) as *const T;
673        Rc::increment_strong_count(ptr);
674        Rc::from_raw(ptr)
675    }
676
677    /// Borrow the underlying T from a heap-tagged Value.
678    /// SAFETY: caller must ensure the tag matches and T is correct.
679    #[inline(always)]
680    unsafe fn borrow_ref<T>(&self) -> &T {
681        let payload = get_payload(self.0);
682        let ptr = payload_to_ptr(payload) as *const T;
683        &*ptr
684    }
685
686    /// Pattern-match friendly view of this value.
687    /// For heap types, this bumps the Rc refcount.
688    pub fn view(&self) -> ValueView {
689        if !is_boxed(self.0) {
690            return ValueView::Float(f64::from_bits(self.0));
691        }
692        let tag = get_tag(self.0);
693        match tag {
694            TAG_NIL => ValueView::Nil,
695            TAG_FALSE => ValueView::Bool(false),
696            TAG_TRUE => ValueView::Bool(true),
697            TAG_INT_SMALL => {
698                let payload = get_payload(self.0);
699                let val = if payload & INT_SIGN_BIT != 0 {
700                    (payload | !PAYLOAD_MASK) as i64
701                } else {
702                    payload as i64
703                };
704                ValueView::Int(val)
705            }
706            TAG_CHAR => {
707                let payload = get_payload(self.0);
708                ValueView::Char(unsafe { char::from_u32_unchecked(payload as u32) })
709            }
710            TAG_SYMBOL => {
711                let payload = get_payload(self.0);
712                let spur: Spur = unsafe { std::mem::transmute(payload as u32) };
713                ValueView::Symbol(spur)
714            }
715            TAG_KEYWORD => {
716                let payload = get_payload(self.0);
717                let spur: Spur = unsafe { std::mem::transmute(payload as u32) };
718                ValueView::Keyword(spur)
719            }
720            TAG_INT_BIG => {
721                let val = unsafe { *self.borrow_ref::<i64>() };
722                ValueView::Int(val)
723            }
724            TAG_STRING => ValueView::String(unsafe { self.get_rc::<String>() }),
725            TAG_LIST => ValueView::List(unsafe { self.get_rc::<Vec<Value>>() }),
726            TAG_VECTOR => ValueView::Vector(unsafe { self.get_rc::<Vec<Value>>() }),
727            TAG_MAP => ValueView::Map(unsafe { self.get_rc::<BTreeMap<Value, Value>>() }),
728            TAG_HASHMAP => {
729                ValueView::HashMap(unsafe { self.get_rc::<hashbrown::HashMap<Value, Value>>() })
730            }
731            TAG_LAMBDA => ValueView::Lambda(unsafe { self.get_rc::<Lambda>() }),
732            TAG_MACRO => ValueView::Macro(unsafe { self.get_rc::<Macro>() }),
733            TAG_NATIVE_FN => ValueView::NativeFn(unsafe { self.get_rc::<NativeFn>() }),
734            TAG_PROMPT => ValueView::Prompt(unsafe { self.get_rc::<Prompt>() }),
735            TAG_MESSAGE => ValueView::Message(unsafe { self.get_rc::<Message>() }),
736            TAG_CONVERSATION => ValueView::Conversation(unsafe { self.get_rc::<Conversation>() }),
737            TAG_TOOL_DEF => ValueView::ToolDef(unsafe { self.get_rc::<ToolDefinition>() }),
738            TAG_AGENT => ValueView::Agent(unsafe { self.get_rc::<Agent>() }),
739            TAG_THUNK => ValueView::Thunk(unsafe { self.get_rc::<Thunk>() }),
740            TAG_RECORD => ValueView::Record(unsafe { self.get_rc::<Record>() }),
741            TAG_BYTEVECTOR => ValueView::Bytevector(unsafe { self.get_rc::<Vec<u8>>() }),
742            _ => unreachable!("invalid NaN-boxed tag: {}", tag),
743        }
744    }
745
746    // -- Typed accessors (ergonomic, avoid full view match) --
747
748    pub fn type_name(&self) -> &'static str {
749        if !is_boxed(self.0) {
750            return "float";
751        }
752        match get_tag(self.0) {
753            TAG_NIL => "nil",
754            TAG_FALSE | TAG_TRUE => "bool",
755            TAG_INT_SMALL | TAG_INT_BIG => "int",
756            TAG_CHAR => "char",
757            TAG_SYMBOL => "symbol",
758            TAG_KEYWORD => "keyword",
759            TAG_STRING => "string",
760            TAG_LIST => "list",
761            TAG_VECTOR => "vector",
762            TAG_MAP => "map",
763            TAG_HASHMAP => "hashmap",
764            TAG_LAMBDA => "lambda",
765            TAG_MACRO => "macro",
766            TAG_NATIVE_FN => "native-fn",
767            TAG_PROMPT => "prompt",
768            TAG_MESSAGE => "message",
769            TAG_CONVERSATION => "conversation",
770            TAG_TOOL_DEF => "tool",
771            TAG_AGENT => "agent",
772            TAG_THUNK => "promise",
773            TAG_RECORD => "record",
774            TAG_BYTEVECTOR => "bytevector",
775            _ => "unknown",
776        }
777    }
778
779    #[inline(always)]
780    pub fn is_nil(&self) -> bool {
781        self.0 == Value::NIL.0
782    }
783
784    #[inline(always)]
785    pub fn is_truthy(&self) -> bool {
786        self.0 != Value::NIL.0 && self.0 != Value::FALSE.0
787    }
788
789    #[inline(always)]
790    pub fn is_bool(&self) -> bool {
791        self.0 == Value::TRUE.0 || self.0 == Value::FALSE.0
792    }
793
794    #[inline(always)]
795    pub fn is_int(&self) -> bool {
796        is_boxed(self.0) && matches!(get_tag(self.0), TAG_INT_SMALL | TAG_INT_BIG)
797    }
798
799    #[inline(always)]
800    pub fn is_symbol(&self) -> bool {
801        is_boxed(self.0) && get_tag(self.0) == TAG_SYMBOL
802    }
803
804    #[inline(always)]
805    pub fn is_keyword(&self) -> bool {
806        is_boxed(self.0) && get_tag(self.0) == TAG_KEYWORD
807    }
808
809    #[inline(always)]
810    pub fn is_string(&self) -> bool {
811        is_boxed(self.0) && get_tag(self.0) == TAG_STRING
812    }
813
814    #[inline(always)]
815    pub fn is_list(&self) -> bool {
816        is_boxed(self.0) && get_tag(self.0) == TAG_LIST
817    }
818
819    #[inline(always)]
820    pub fn is_pair(&self) -> bool {
821        if let Some(items) = self.as_list() {
822            !items.is_empty()
823        } else {
824            false
825        }
826    }
827
828    #[inline(always)]
829    pub fn is_vector(&self) -> bool {
830        is_boxed(self.0) && get_tag(self.0) == TAG_VECTOR
831    }
832
833    #[inline(always)]
834    pub fn is_map(&self) -> bool {
835        is_boxed(self.0) && matches!(get_tag(self.0), TAG_MAP | TAG_HASHMAP)
836    }
837
838    #[inline(always)]
839    pub fn is_lambda(&self) -> bool {
840        is_boxed(self.0) && get_tag(self.0) == TAG_LAMBDA
841    }
842
843    #[inline(always)]
844    pub fn is_native_fn(&self) -> bool {
845        is_boxed(self.0) && get_tag(self.0) == TAG_NATIVE_FN
846    }
847
848    #[inline(always)]
849    pub fn is_thunk(&self) -> bool {
850        is_boxed(self.0) && get_tag(self.0) == TAG_THUNK
851    }
852
853    #[inline(always)]
854    pub fn is_record(&self) -> bool {
855        is_boxed(self.0) && get_tag(self.0) == TAG_RECORD
856    }
857
858    #[inline(always)]
859    pub fn as_int(&self) -> Option<i64> {
860        if !is_boxed(self.0) {
861            return None;
862        }
863        match get_tag(self.0) {
864            TAG_INT_SMALL => {
865                let payload = get_payload(self.0);
866                let val = if payload & INT_SIGN_BIT != 0 {
867                    (payload | !PAYLOAD_MASK) as i64
868                } else {
869                    payload as i64
870                };
871                Some(val)
872            }
873            TAG_INT_BIG => Some(unsafe { *self.borrow_ref::<i64>() }),
874            _ => None,
875        }
876    }
877
878    #[inline(always)]
879    pub fn as_float(&self) -> Option<f64> {
880        if !is_boxed(self.0) {
881            return Some(f64::from_bits(self.0));
882        }
883        match get_tag(self.0) {
884            TAG_INT_SMALL => {
885                let payload = get_payload(self.0);
886                let val = if payload & INT_SIGN_BIT != 0 {
887                    (payload | !PAYLOAD_MASK) as i64
888                } else {
889                    payload as i64
890                };
891                Some(val as f64)
892            }
893            TAG_INT_BIG => Some(unsafe { *self.borrow_ref::<i64>() } as f64),
894            _ => None,
895        }
896    }
897
898    #[inline(always)]
899    pub fn as_bool(&self) -> Option<bool> {
900        if self.0 == Value::TRUE.0 {
901            Some(true)
902        } else if self.0 == Value::FALSE.0 {
903            Some(false)
904        } else {
905            None
906        }
907    }
908
909    pub fn as_str(&self) -> Option<&str> {
910        if is_boxed(self.0) && get_tag(self.0) == TAG_STRING {
911            Some(unsafe { self.borrow_ref::<String>() })
912        } else {
913            None
914        }
915    }
916
917    pub fn as_string_rc(&self) -> Option<Rc<String>> {
918        if is_boxed(self.0) && get_tag(self.0) == TAG_STRING {
919            Some(unsafe { self.get_rc::<String>() })
920        } else {
921            None
922        }
923    }
924
925    pub fn as_symbol(&self) -> Option<String> {
926        self.as_symbol_spur().map(resolve)
927    }
928
929    pub fn as_symbol_spur(&self) -> Option<Spur> {
930        if is_boxed(self.0) && get_tag(self.0) == TAG_SYMBOL {
931            let payload = get_payload(self.0);
932            Some(unsafe { std::mem::transmute::<u32, Spur>(payload as u32) })
933        } else {
934            None
935        }
936    }
937
938    pub fn as_keyword(&self) -> Option<String> {
939        self.as_keyword_spur().map(resolve)
940    }
941
942    pub fn as_keyword_spur(&self) -> Option<Spur> {
943        if is_boxed(self.0) && get_tag(self.0) == TAG_KEYWORD {
944            let payload = get_payload(self.0);
945            Some(unsafe { std::mem::transmute::<u32, Spur>(payload as u32) })
946        } else {
947            None
948        }
949    }
950
951    pub fn as_char(&self) -> Option<char> {
952        if is_boxed(self.0) && get_tag(self.0) == TAG_CHAR {
953            let payload = get_payload(self.0);
954            char::from_u32(payload as u32)
955        } else {
956            None
957        }
958    }
959
960    pub fn as_list(&self) -> Option<&[Value]> {
961        if is_boxed(self.0) && get_tag(self.0) == TAG_LIST {
962            Some(unsafe { self.borrow_ref::<Vec<Value>>() })
963        } else {
964            None
965        }
966    }
967
968    pub fn as_list_rc(&self) -> Option<Rc<Vec<Value>>> {
969        if is_boxed(self.0) && get_tag(self.0) == TAG_LIST {
970            Some(unsafe { self.get_rc::<Vec<Value>>() })
971        } else {
972            None
973        }
974    }
975
976    pub fn as_vector(&self) -> Option<&[Value]> {
977        if is_boxed(self.0) && get_tag(self.0) == TAG_VECTOR {
978            Some(unsafe { self.borrow_ref::<Vec<Value>>() })
979        } else {
980            None
981        }
982    }
983
984    pub fn as_vector_rc(&self) -> Option<Rc<Vec<Value>>> {
985        if is_boxed(self.0) && get_tag(self.0) == TAG_VECTOR {
986            Some(unsafe { self.get_rc::<Vec<Value>>() })
987        } else {
988            None
989        }
990    }
991
992    pub fn as_map_rc(&self) -> Option<Rc<BTreeMap<Value, Value>>> {
993        if is_boxed(self.0) && get_tag(self.0) == TAG_MAP {
994            Some(unsafe { self.get_rc::<BTreeMap<Value, Value>>() })
995        } else {
996            None
997        }
998    }
999
1000    pub fn as_hashmap_rc(&self) -> Option<Rc<hashbrown::HashMap<Value, Value>>> {
1001        if is_boxed(self.0) && get_tag(self.0) == TAG_HASHMAP {
1002            Some(unsafe { self.get_rc::<hashbrown::HashMap<Value, Value>>() })
1003        } else {
1004            None
1005        }
1006    }
1007
1008    pub fn as_lambda_rc(&self) -> Option<Rc<Lambda>> {
1009        if is_boxed(self.0) && get_tag(self.0) == TAG_LAMBDA {
1010            Some(unsafe { self.get_rc::<Lambda>() })
1011        } else {
1012            None
1013        }
1014    }
1015
1016    pub fn as_macro_rc(&self) -> Option<Rc<Macro>> {
1017        if is_boxed(self.0) && get_tag(self.0) == TAG_MACRO {
1018            Some(unsafe { self.get_rc::<Macro>() })
1019        } else {
1020            None
1021        }
1022    }
1023
1024    pub fn as_native_fn_rc(&self) -> Option<Rc<NativeFn>> {
1025        if is_boxed(self.0) && get_tag(self.0) == TAG_NATIVE_FN {
1026            Some(unsafe { self.get_rc::<NativeFn>() })
1027        } else {
1028            None
1029        }
1030    }
1031
1032    pub fn as_thunk_rc(&self) -> Option<Rc<Thunk>> {
1033        if is_boxed(self.0) && get_tag(self.0) == TAG_THUNK {
1034            Some(unsafe { self.get_rc::<Thunk>() })
1035        } else {
1036            None
1037        }
1038    }
1039
1040    pub fn as_record(&self) -> Option<&Record> {
1041        if is_boxed(self.0) && get_tag(self.0) == TAG_RECORD {
1042            Some(unsafe { self.borrow_ref::<Record>() })
1043        } else {
1044            None
1045        }
1046    }
1047
1048    pub fn as_record_rc(&self) -> Option<Rc<Record>> {
1049        if is_boxed(self.0) && get_tag(self.0) == TAG_RECORD {
1050            Some(unsafe { self.get_rc::<Record>() })
1051        } else {
1052            None
1053        }
1054    }
1055
1056    pub fn as_bytevector(&self) -> Option<&[u8]> {
1057        if is_boxed(self.0) && get_tag(self.0) == TAG_BYTEVECTOR {
1058            Some(unsafe { self.borrow_ref::<Vec<u8>>() })
1059        } else {
1060            None
1061        }
1062    }
1063
1064    pub fn as_bytevector_rc(&self) -> Option<Rc<Vec<u8>>> {
1065        if is_boxed(self.0) && get_tag(self.0) == TAG_BYTEVECTOR {
1066            Some(unsafe { self.get_rc::<Vec<u8>>() })
1067        } else {
1068            None
1069        }
1070    }
1071
1072    pub fn as_prompt_rc(&self) -> Option<Rc<Prompt>> {
1073        if is_boxed(self.0) && get_tag(self.0) == TAG_PROMPT {
1074            Some(unsafe { self.get_rc::<Prompt>() })
1075        } else {
1076            None
1077        }
1078    }
1079
1080    pub fn as_message_rc(&self) -> Option<Rc<Message>> {
1081        if is_boxed(self.0) && get_tag(self.0) == TAG_MESSAGE {
1082            Some(unsafe { self.get_rc::<Message>() })
1083        } else {
1084            None
1085        }
1086    }
1087
1088    pub fn as_conversation_rc(&self) -> Option<Rc<Conversation>> {
1089        if is_boxed(self.0) && get_tag(self.0) == TAG_CONVERSATION {
1090            Some(unsafe { self.get_rc::<Conversation>() })
1091        } else {
1092            None
1093        }
1094    }
1095
1096    pub fn as_tool_def_rc(&self) -> Option<Rc<ToolDefinition>> {
1097        if is_boxed(self.0) && get_tag(self.0) == TAG_TOOL_DEF {
1098            Some(unsafe { self.get_rc::<ToolDefinition>() })
1099        } else {
1100            None
1101        }
1102    }
1103
1104    pub fn as_agent_rc(&self) -> Option<Rc<Agent>> {
1105        if is_boxed(self.0) && get_tag(self.0) == TAG_AGENT {
1106            Some(unsafe { self.get_rc::<Agent>() })
1107        } else {
1108            None
1109        }
1110    }
1111}
1112
1113// ── Clone ─────────────────────────────────────────────────────────
1114
1115impl Clone for Value {
1116    #[inline(always)]
1117    fn clone(&self) -> Self {
1118        if !is_boxed(self.0) {
1119            // Float: trivial copy
1120            return Value(self.0);
1121        }
1122        let tag = get_tag(self.0);
1123        match tag {
1124            // Immediates: trivial copy
1125            TAG_NIL | TAG_FALSE | TAG_TRUE | TAG_INT_SMALL | TAG_CHAR | TAG_SYMBOL
1126            | TAG_KEYWORD => Value(self.0),
1127            // Heap pointers: increment refcount
1128            _ => {
1129                let payload = get_payload(self.0);
1130                let ptr = payload_to_ptr(payload);
1131                // Increment refcount based on type
1132                unsafe {
1133                    match tag {
1134                        TAG_INT_BIG => Rc::increment_strong_count(ptr as *const i64),
1135                        TAG_STRING => Rc::increment_strong_count(ptr as *const String),
1136                        TAG_LIST | TAG_VECTOR => {
1137                            Rc::increment_strong_count(ptr as *const Vec<Value>)
1138                        }
1139                        TAG_MAP => Rc::increment_strong_count(ptr as *const BTreeMap<Value, Value>),
1140                        TAG_HASHMAP => Rc::increment_strong_count(
1141                            ptr as *const hashbrown::HashMap<Value, Value>,
1142                        ),
1143                        TAG_LAMBDA => Rc::increment_strong_count(ptr as *const Lambda),
1144                        TAG_MACRO => Rc::increment_strong_count(ptr as *const Macro),
1145                        TAG_NATIVE_FN => Rc::increment_strong_count(ptr as *const NativeFn),
1146                        TAG_PROMPT => Rc::increment_strong_count(ptr as *const Prompt),
1147                        TAG_MESSAGE => Rc::increment_strong_count(ptr as *const Message),
1148                        TAG_CONVERSATION => Rc::increment_strong_count(ptr as *const Conversation),
1149                        TAG_TOOL_DEF => Rc::increment_strong_count(ptr as *const ToolDefinition),
1150                        TAG_AGENT => Rc::increment_strong_count(ptr as *const Agent),
1151                        TAG_THUNK => Rc::increment_strong_count(ptr as *const Thunk),
1152                        TAG_RECORD => Rc::increment_strong_count(ptr as *const Record),
1153                        TAG_BYTEVECTOR => Rc::increment_strong_count(ptr as *const Vec<u8>),
1154                        _ => unreachable!("invalid heap tag in clone: {}", tag),
1155                    }
1156                }
1157                Value(self.0)
1158            }
1159        }
1160    }
1161}
1162
1163// ── Drop ──────────────────────────────────────────────────────────
1164
1165impl Drop for Value {
1166    #[inline(always)]
1167    fn drop(&mut self) {
1168        if !is_boxed(self.0) {
1169            return; // Float
1170        }
1171        let tag = get_tag(self.0);
1172        match tag {
1173            // Immediates: nothing to free
1174            TAG_NIL | TAG_FALSE | TAG_TRUE | TAG_INT_SMALL | TAG_CHAR | TAG_SYMBOL
1175            | TAG_KEYWORD => {}
1176            // Heap pointers: drop the Rc
1177            _ => {
1178                let payload = get_payload(self.0);
1179                let ptr = payload_to_ptr(payload);
1180                unsafe {
1181                    match tag {
1182                        TAG_INT_BIG => drop(Rc::from_raw(ptr as *const i64)),
1183                        TAG_STRING => drop(Rc::from_raw(ptr as *const String)),
1184                        TAG_LIST | TAG_VECTOR => drop(Rc::from_raw(ptr as *const Vec<Value>)),
1185                        TAG_MAP => drop(Rc::from_raw(ptr as *const BTreeMap<Value, Value>)),
1186                        TAG_HASHMAP => {
1187                            drop(Rc::from_raw(ptr as *const hashbrown::HashMap<Value, Value>))
1188                        }
1189                        TAG_LAMBDA => drop(Rc::from_raw(ptr as *const Lambda)),
1190                        TAG_MACRO => drop(Rc::from_raw(ptr as *const Macro)),
1191                        TAG_NATIVE_FN => drop(Rc::from_raw(ptr as *const NativeFn)),
1192                        TAG_PROMPT => drop(Rc::from_raw(ptr as *const Prompt)),
1193                        TAG_MESSAGE => drop(Rc::from_raw(ptr as *const Message)),
1194                        TAG_CONVERSATION => drop(Rc::from_raw(ptr as *const Conversation)),
1195                        TAG_TOOL_DEF => drop(Rc::from_raw(ptr as *const ToolDefinition)),
1196                        TAG_AGENT => drop(Rc::from_raw(ptr as *const Agent)),
1197                        TAG_THUNK => drop(Rc::from_raw(ptr as *const Thunk)),
1198                        TAG_RECORD => drop(Rc::from_raw(ptr as *const Record)),
1199                        TAG_BYTEVECTOR => drop(Rc::from_raw(ptr as *const Vec<u8>)),
1200                        _ => {} // unreachable, but don't panic in drop
1201                    }
1202                }
1203            }
1204        }
1205    }
1206}
1207
1208// ── PartialEq / Eq ────────────────────────────────────────────────
1209
1210impl PartialEq for Value {
1211    fn eq(&self, other: &Self) -> bool {
1212        // Fast path: identical bits
1213        if self.0 == other.0 {
1214            // For floats, NaN != NaN per IEEE, but our canonical NaN is unique,
1215            // so identical bits means equal for all types.
1216            // Exception: need to handle -0.0 == +0.0
1217            if !is_boxed(self.0) {
1218                let f = f64::from_bits(self.0);
1219                // NaN check: if both are canonical NaN (same bits), we say not equal
1220                if f.is_nan() {
1221                    return false;
1222                }
1223                return true;
1224            }
1225            return true;
1226        }
1227        // Different bits: could still be equal for heap types or -0.0/+0.0
1228        match (self.view(), other.view()) {
1229            (ValueView::Nil, ValueView::Nil) => true,
1230            (ValueView::Bool(a), ValueView::Bool(b)) => a == b,
1231            (ValueView::Int(a), ValueView::Int(b)) => a == b,
1232            (ValueView::Float(a), ValueView::Float(b)) => a.to_bits() == b.to_bits(),
1233            (ValueView::String(a), ValueView::String(b)) => a == b,
1234            (ValueView::Symbol(a), ValueView::Symbol(b)) => a == b,
1235            (ValueView::Keyword(a), ValueView::Keyword(b)) => a == b,
1236            (ValueView::Char(a), ValueView::Char(b)) => a == b,
1237            (ValueView::List(a), ValueView::List(b)) => a == b,
1238            (ValueView::Vector(a), ValueView::Vector(b)) => a == b,
1239            (ValueView::Map(a), ValueView::Map(b)) => a == b,
1240            (ValueView::HashMap(a), ValueView::HashMap(b)) => a == b,
1241            (ValueView::Record(a), ValueView::Record(b)) => {
1242                a.type_tag == b.type_tag && a.fields == b.fields
1243            }
1244            (ValueView::Bytevector(a), ValueView::Bytevector(b)) => a == b,
1245            _ => false,
1246        }
1247    }
1248}
1249
1250impl Eq for Value {}
1251
1252// ── Hash ──────────────────────────────────────────────────────────
1253
1254impl Hash for Value {
1255    fn hash<H: Hasher>(&self, state: &mut H) {
1256        match self.view() {
1257            ValueView::Nil => 0u8.hash(state),
1258            ValueView::Bool(b) => {
1259                1u8.hash(state);
1260                b.hash(state);
1261            }
1262            ValueView::Int(n) => {
1263                2u8.hash(state);
1264                n.hash(state);
1265            }
1266            ValueView::Float(f) => {
1267                3u8.hash(state);
1268                f.to_bits().hash(state);
1269            }
1270            ValueView::String(s) => {
1271                4u8.hash(state);
1272                s.hash(state);
1273            }
1274            ValueView::Symbol(s) => {
1275                5u8.hash(state);
1276                s.hash(state);
1277            }
1278            ValueView::Keyword(s) => {
1279                6u8.hash(state);
1280                s.hash(state);
1281            }
1282            ValueView::Char(c) => {
1283                7u8.hash(state);
1284                c.hash(state);
1285            }
1286            ValueView::List(l) => {
1287                8u8.hash(state);
1288                l.hash(state);
1289            }
1290            ValueView::Vector(v) => {
1291                9u8.hash(state);
1292                v.hash(state);
1293            }
1294            ValueView::Record(r) => {
1295                10u8.hash(state);
1296                r.type_tag.hash(state);
1297                r.fields.hash(state);
1298            }
1299            ValueView::Bytevector(bv) => {
1300                11u8.hash(state);
1301                bv.hash(state);
1302            }
1303            _ => {}
1304        }
1305    }
1306}
1307
1308// ── Ord ───────────────────────────────────────────────────────────
1309
1310impl PartialOrd for Value {
1311    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1312        Some(self.cmp(other))
1313    }
1314}
1315
1316impl Ord for Value {
1317    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1318        use std::cmp::Ordering;
1319        fn type_order(v: &Value) -> u8 {
1320            match v.view() {
1321                ValueView::Nil => 0,
1322                ValueView::Bool(_) => 1,
1323                ValueView::Int(_) => 2,
1324                ValueView::Float(_) => 3,
1325                ValueView::Char(_) => 4,
1326                ValueView::String(_) => 5,
1327                ValueView::Symbol(_) => 6,
1328                ValueView::Keyword(_) => 7,
1329                ValueView::List(_) => 8,
1330                ValueView::Vector(_) => 9,
1331                ValueView::Map(_) => 10,
1332                ValueView::HashMap(_) => 11,
1333                ValueView::Record(_) => 12,
1334                ValueView::Bytevector(_) => 13,
1335                _ => 14,
1336            }
1337        }
1338        match (self.view(), other.view()) {
1339            (ValueView::Nil, ValueView::Nil) => Ordering::Equal,
1340            (ValueView::Bool(a), ValueView::Bool(b)) => a.cmp(&b),
1341            (ValueView::Int(a), ValueView::Int(b)) => a.cmp(&b),
1342            (ValueView::Float(a), ValueView::Float(b)) => a.to_bits().cmp(&b.to_bits()),
1343            (ValueView::String(a), ValueView::String(b)) => a.cmp(&b),
1344            (ValueView::Symbol(a), ValueView::Symbol(b)) => compare_spurs(a, b),
1345            (ValueView::Keyword(a), ValueView::Keyword(b)) => compare_spurs(a, b),
1346            (ValueView::Char(a), ValueView::Char(b)) => a.cmp(&b),
1347            (ValueView::List(a), ValueView::List(b)) => a.cmp(&b),
1348            (ValueView::Vector(a), ValueView::Vector(b)) => a.cmp(&b),
1349            (ValueView::Record(a), ValueView::Record(b)) => {
1350                compare_spurs(a.type_tag, b.type_tag).then_with(|| a.fields.cmp(&b.fields))
1351            }
1352            (ValueView::Bytevector(a), ValueView::Bytevector(b)) => a.cmp(&b),
1353            _ => type_order(self).cmp(&type_order(other)),
1354        }
1355    }
1356}
1357
1358// ── Display ───────────────────────────────────────────────────────
1359
1360fn truncate(s: &str, max: usize) -> String {
1361    let mut iter = s.chars();
1362    let prefix: String = iter.by_ref().take(max).collect();
1363    if iter.next().is_none() {
1364        prefix
1365    } else {
1366        format!("{prefix}...")
1367    }
1368}
1369
1370impl fmt::Display for Value {
1371    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1372        match self.view() {
1373            ValueView::Nil => write!(f, "nil"),
1374            ValueView::Bool(true) => write!(f, "#t"),
1375            ValueView::Bool(false) => write!(f, "#f"),
1376            ValueView::Int(n) => write!(f, "{n}"),
1377            ValueView::Float(n) => {
1378                if n.fract() == 0.0 {
1379                    write!(f, "{n:.1}")
1380                } else {
1381                    write!(f, "{n}")
1382                }
1383            }
1384            ValueView::String(s) => write!(f, "\"{s}\""),
1385            ValueView::Symbol(s) => with_resolved(s, |name| write!(f, "{name}")),
1386            ValueView::Keyword(s) => with_resolved(s, |name| write!(f, ":{name}")),
1387            ValueView::Char(c) => match c {
1388                ' ' => write!(f, "#\\space"),
1389                '\n' => write!(f, "#\\newline"),
1390                '\t' => write!(f, "#\\tab"),
1391                '\r' => write!(f, "#\\return"),
1392                '\0' => write!(f, "#\\nul"),
1393                _ => write!(f, "#\\{c}"),
1394            },
1395            ValueView::List(items) => {
1396                write!(f, "(")?;
1397                for (i, item) in items.iter().enumerate() {
1398                    if i > 0 {
1399                        write!(f, " ")?;
1400                    }
1401                    write!(f, "{item}")?;
1402                }
1403                write!(f, ")")
1404            }
1405            ValueView::Vector(items) => {
1406                write!(f, "[")?;
1407                for (i, item) in items.iter().enumerate() {
1408                    if i > 0 {
1409                        write!(f, " ")?;
1410                    }
1411                    write!(f, "{item}")?;
1412                }
1413                write!(f, "]")
1414            }
1415            ValueView::Map(map) => {
1416                write!(f, "{{")?;
1417                for (i, (k, v)) in map.iter().enumerate() {
1418                    if i > 0 {
1419                        write!(f, " ")?;
1420                    }
1421                    write!(f, "{k} {v}")?;
1422                }
1423                write!(f, "}}")
1424            }
1425            ValueView::HashMap(map) => {
1426                let mut entries: Vec<_> = map.iter().collect();
1427                entries.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
1428                write!(f, "{{")?;
1429                for (i, (k, v)) in entries.iter().enumerate() {
1430                    if i > 0 {
1431                        write!(f, " ")?;
1432                    }
1433                    write!(f, "{k} {v}")?;
1434                }
1435                write!(f, "}}")
1436            }
1437            ValueView::Lambda(l) => {
1438                if let Some(name) = &l.name {
1439                    with_resolved(*name, |n| write!(f, "<lambda {n}>"))
1440                } else {
1441                    write!(f, "<lambda>")
1442                }
1443            }
1444            ValueView::Macro(m) => with_resolved(m.name, |n| write!(f, "<macro {n}>")),
1445            ValueView::NativeFn(n) => write!(f, "<native-fn {}>", n.name),
1446            ValueView::Prompt(p) => write!(f, "<prompt {} messages>", p.messages.len()),
1447            ValueView::Message(m) => {
1448                write!(f, "<message {} \"{}\">", m.role, truncate(&m.content, 40))
1449            }
1450            ValueView::Conversation(c) => {
1451                write!(f, "<conversation {} messages>", c.messages.len())
1452            }
1453            ValueView::ToolDef(t) => write!(f, "<tool {}>", t.name),
1454            ValueView::Agent(a) => write!(f, "<agent {}>", a.name),
1455            ValueView::Thunk(t) => {
1456                if t.forced.borrow().is_some() {
1457                    write!(f, "<promise (forced)>")
1458                } else {
1459                    write!(f, "<promise>")
1460                }
1461            }
1462            ValueView::Record(r) => {
1463                with_resolved(r.type_tag, |tag| write!(f, "#<record {tag}"))?;
1464                for field in &r.fields {
1465                    write!(f, " {field}")?;
1466                }
1467                write!(f, ">")
1468            }
1469            ValueView::Bytevector(bv) => {
1470                write!(f, "#u8(")?;
1471                for (i, byte) in bv.iter().enumerate() {
1472                    if i > 0 {
1473                        write!(f, " ")?;
1474                    }
1475                    write!(f, "{byte}")?;
1476                }
1477                write!(f, ")")
1478            }
1479        }
1480    }
1481}
1482
1483// ── Pretty-print ──────────────────────────────────────────────────
1484
1485/// Pretty-print a value with line breaks and indentation when the compact
1486/// representation exceeds `max_width` columns.  Small values that fit in
1487/// one line are returned in the normal compact format.
1488pub fn pretty_print(value: &Value, max_width: usize) -> String {
1489    let compact = format!("{value}");
1490    if compact.len() <= max_width {
1491        return compact;
1492    }
1493    let mut buf = String::new();
1494    pp_value(value, 0, max_width, &mut buf);
1495    buf
1496}
1497
1498/// Render `value` into `buf` at the given `indent` level.  If the compact
1499/// form fits in `max_width - indent` columns we use it; otherwise we break
1500/// the container across multiple lines.
1501fn pp_value(value: &Value, indent: usize, max_width: usize, buf: &mut String) {
1502    let compact = format!("{value}");
1503    let remaining = max_width.saturating_sub(indent);
1504    if compact.len() <= remaining {
1505        buf.push_str(&compact);
1506        return;
1507    }
1508
1509    match value.view() {
1510        ValueView::List(items) => {
1511            pp_seq(items.iter(), '(', ')', indent, max_width, buf);
1512        }
1513        ValueView::Vector(items) => {
1514            pp_seq(items.iter(), '[', ']', indent, max_width, buf);
1515        }
1516        ValueView::Map(map) => {
1517            pp_map(
1518                map.iter().map(|(k, v)| (k.clone(), v.clone())),
1519                indent,
1520                max_width,
1521                buf,
1522            );
1523        }
1524        ValueView::HashMap(map) => {
1525            let mut entries: Vec<_> = map.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
1526            entries.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
1527            pp_map(entries.into_iter(), indent, max_width, buf);
1528        }
1529        _ => buf.push_str(&compact),
1530    }
1531}
1532
1533/// Pretty-print a list or vector.
1534fn pp_seq<'a>(
1535    items: impl Iterator<Item = &'a Value>,
1536    open: char,
1537    close: char,
1538    indent: usize,
1539    max_width: usize,
1540    buf: &mut String,
1541) {
1542    buf.push(open);
1543    let child_indent = indent + 1;
1544    let pad = " ".repeat(child_indent);
1545    for (i, item) in items.enumerate() {
1546        if i > 0 {
1547            buf.push('\n');
1548            buf.push_str(&pad);
1549        }
1550        pp_value(item, child_indent, max_width, buf);
1551    }
1552    buf.push(close);
1553}
1554
1555/// Pretty-print a map (BTreeMap or HashMap).
1556fn pp_map(
1557    entries: impl Iterator<Item = (Value, Value)>,
1558    indent: usize,
1559    max_width: usize,
1560    buf: &mut String,
1561) {
1562    buf.push('{');
1563    let child_indent = indent + 1;
1564    let pad = " ".repeat(child_indent);
1565    for (i, (k, v)) in entries.enumerate() {
1566        if i > 0 {
1567            buf.push('\n');
1568            buf.push_str(&pad);
1569        }
1570        // Key is always compact
1571        let key_str = format!("{k}");
1572        buf.push_str(&key_str);
1573
1574        // Check if the value fits inline after the key
1575        let inline_indent = child_indent + key_str.len() + 1;
1576        let compact_val = format!("{v}");
1577        let remaining = max_width.saturating_sub(inline_indent);
1578
1579        if compact_val.len() <= remaining {
1580            // Fits inline
1581            buf.push(' ');
1582            buf.push_str(&compact_val);
1583        } else if is_compound(&v) {
1584            // Complex value: break to next line indented 2 from key
1585            let nested_indent = child_indent + 2;
1586            let nested_pad = " ".repeat(nested_indent);
1587            buf.push('\n');
1588            buf.push_str(&nested_pad);
1589            pp_value(&v, nested_indent, max_width, buf);
1590        } else {
1591            // Simple value that's just long: keep inline
1592            buf.push(' ');
1593            buf.push_str(&compact_val);
1594        }
1595    }
1596    buf.push('}');
1597}
1598
1599/// Check whether a value is a compound container (list, vector, map, hashmap).
1600fn is_compound(value: &Value) -> bool {
1601    matches!(
1602        value.view(),
1603        ValueView::List(_) | ValueView::Vector(_) | ValueView::Map(_) | ValueView::HashMap(_)
1604    )
1605}
1606
1607// ── Debug ─────────────────────────────────────────────────────────
1608
1609impl fmt::Debug for Value {
1610    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1611        match self.view() {
1612            ValueView::Nil => write!(f, "Nil"),
1613            ValueView::Bool(b) => write!(f, "Bool({b})"),
1614            ValueView::Int(n) => write!(f, "Int({n})"),
1615            ValueView::Float(n) => write!(f, "Float({n})"),
1616            ValueView::String(s) => write!(f, "String({:?})", &**s),
1617            ValueView::Symbol(s) => write!(f, "Symbol({})", resolve(s)),
1618            ValueView::Keyword(s) => write!(f, "Keyword({})", resolve(s)),
1619            ValueView::Char(c) => write!(f, "Char({c:?})"),
1620            ValueView::List(items) => write!(f, "List({items:?})"),
1621            ValueView::Vector(items) => write!(f, "Vector({items:?})"),
1622            ValueView::Map(map) => write!(f, "Map({map:?})"),
1623            ValueView::HashMap(map) => write!(f, "HashMap({map:?})"),
1624            ValueView::Lambda(l) => write!(f, "{l:?}"),
1625            ValueView::Macro(m) => write!(f, "{m:?}"),
1626            ValueView::NativeFn(n) => write!(f, "{n:?}"),
1627            ValueView::Prompt(p) => write!(f, "{p:?}"),
1628            ValueView::Message(m) => write!(f, "{m:?}"),
1629            ValueView::Conversation(c) => write!(f, "{c:?}"),
1630            ValueView::ToolDef(t) => write!(f, "{t:?}"),
1631            ValueView::Agent(a) => write!(f, "{a:?}"),
1632            ValueView::Thunk(t) => write!(f, "{t:?}"),
1633            ValueView::Record(r) => write!(f, "{r:?}"),
1634            ValueView::Bytevector(bv) => write!(f, "Bytevector({bv:?})"),
1635        }
1636    }
1637}
1638
1639// ── Env ───────────────────────────────────────────────────────────
1640
1641/// A Sema environment: a chain of scopes with bindings.
1642#[derive(Debug, Clone)]
1643pub struct Env {
1644    pub bindings: Rc<RefCell<SpurMap<Spur, Value>>>,
1645    pub parent: Option<Rc<Env>>,
1646    pub version: Cell<u64>,
1647}
1648
1649impl Env {
1650    pub fn new() -> Self {
1651        Env {
1652            bindings: Rc::new(RefCell::new(SpurMap::new())),
1653            parent: None,
1654            version: Cell::new(0),
1655        }
1656    }
1657
1658    pub fn with_parent(parent: Rc<Env>) -> Self {
1659        Env {
1660            bindings: Rc::new(RefCell::new(SpurMap::new())),
1661            parent: Some(parent),
1662            version: Cell::new(0),
1663        }
1664    }
1665
1666    fn bump_version(&self) {
1667        self.version.set(self.version.get().wrapping_add(1));
1668    }
1669
1670    pub fn get(&self, name: Spur) -> Option<Value> {
1671        if let Some(val) = self.bindings.borrow().get(&name) {
1672            Some(val.clone())
1673        } else if let Some(parent) = &self.parent {
1674            parent.get(name)
1675        } else {
1676            None
1677        }
1678    }
1679
1680    pub fn get_str(&self, name: &str) -> Option<Value> {
1681        self.get(intern(name))
1682    }
1683
1684    pub fn set(&self, name: Spur, val: Value) {
1685        self.bindings.borrow_mut().insert(name, val);
1686        self.bump_version();
1687    }
1688
1689    pub fn set_str(&self, name: &str, val: Value) {
1690        self.set(intern(name), val);
1691    }
1692
1693    /// Update a binding that already exists in the current scope.
1694    pub fn update(&self, name: Spur, val: Value) {
1695        let mut bindings = self.bindings.borrow_mut();
1696        if let Some(entry) = bindings.get_mut(&name) {
1697            *entry = val;
1698        } else {
1699            bindings.insert(name, val);
1700        }
1701        drop(bindings);
1702        self.bump_version();
1703    }
1704
1705    /// Remove and return a binding from the current scope only.
1706    pub fn take(&self, name: Spur) -> Option<Value> {
1707        let result = self.bindings.borrow_mut().remove(&name);
1708        if result.is_some() {
1709            self.bump_version();
1710        }
1711        result
1712    }
1713
1714    /// Remove and return a binding from any scope in the parent chain.
1715    pub fn take_anywhere(&self, name: Spur) -> Option<Value> {
1716        if let Some(val) = self.bindings.borrow_mut().remove(&name) {
1717            self.bump_version();
1718            Some(val)
1719        } else if let Some(parent) = &self.parent {
1720            parent.take_anywhere(name)
1721        } else {
1722            None
1723        }
1724    }
1725
1726    /// Set a variable in the scope where it's defined (for set!).
1727    pub fn set_existing(&self, name: Spur, val: Value) -> bool {
1728        let mut bindings = self.bindings.borrow_mut();
1729        if let Some(entry) = bindings.get_mut(&name) {
1730            *entry = val;
1731            drop(bindings);
1732            self.bump_version();
1733            true
1734        } else {
1735            drop(bindings);
1736            if let Some(parent) = &self.parent {
1737                parent.set_existing(name, val)
1738            } else {
1739                false
1740            }
1741        }
1742    }
1743}
1744
1745impl Default for Env {
1746    fn default() -> Self {
1747        Self::new()
1748    }
1749}
1750
1751// ── Tests ─────────────────────────────────────────────────────────
1752
1753#[cfg(test)]
1754mod tests {
1755    use super::*;
1756
1757    #[test]
1758    fn test_size_of_value() {
1759        assert_eq!(std::mem::size_of::<Value>(), 8);
1760    }
1761
1762    #[test]
1763    fn test_nil() {
1764        let v = Value::nil();
1765        assert!(v.is_nil());
1766        assert!(!v.is_truthy());
1767        assert_eq!(v.type_name(), "nil");
1768        assert_eq!(format!("{v}"), "nil");
1769    }
1770
1771    #[test]
1772    fn test_bool() {
1773        let t = Value::bool(true);
1774        let f = Value::bool(false);
1775        assert!(t.is_truthy());
1776        assert!(!f.is_truthy());
1777        assert_eq!(t.as_bool(), Some(true));
1778        assert_eq!(f.as_bool(), Some(false));
1779        assert_eq!(format!("{t}"), "#t");
1780        assert_eq!(format!("{f}"), "#f");
1781    }
1782
1783    #[test]
1784    fn test_small_int() {
1785        let v = Value::int(42);
1786        assert_eq!(v.as_int(), Some(42));
1787        assert_eq!(v.type_name(), "int");
1788        assert_eq!(format!("{v}"), "42");
1789
1790        let neg = Value::int(-100);
1791        assert_eq!(neg.as_int(), Some(-100));
1792        assert_eq!(format!("{neg}"), "-100");
1793
1794        let zero = Value::int(0);
1795        assert_eq!(zero.as_int(), Some(0));
1796    }
1797
1798    #[test]
1799    fn test_small_int_boundaries() {
1800        let max = Value::int(SMALL_INT_MAX);
1801        assert_eq!(max.as_int(), Some(SMALL_INT_MAX));
1802
1803        let min = Value::int(SMALL_INT_MIN);
1804        assert_eq!(min.as_int(), Some(SMALL_INT_MIN));
1805    }
1806
1807    #[test]
1808    fn test_big_int() {
1809        let big = Value::int(i64::MAX);
1810        assert_eq!(big.as_int(), Some(i64::MAX));
1811        assert_eq!(big.type_name(), "int");
1812
1813        let big_neg = Value::int(i64::MIN);
1814        assert_eq!(big_neg.as_int(), Some(i64::MIN));
1815
1816        // Just outside small range
1817        let just_over = Value::int(SMALL_INT_MAX + 1);
1818        assert_eq!(just_over.as_int(), Some(SMALL_INT_MAX + 1));
1819    }
1820
1821    #[test]
1822    fn test_float() {
1823        let v = Value::float(3.14);
1824        assert_eq!(v.as_float(), Some(3.14));
1825        assert_eq!(v.type_name(), "float");
1826
1827        let neg = Value::float(-0.5);
1828        assert_eq!(neg.as_float(), Some(-0.5));
1829
1830        let inf = Value::float(f64::INFINITY);
1831        assert_eq!(inf.as_float(), Some(f64::INFINITY));
1832
1833        let neg_inf = Value::float(f64::NEG_INFINITY);
1834        assert_eq!(neg_inf.as_float(), Some(f64::NEG_INFINITY));
1835    }
1836
1837    #[test]
1838    fn test_float_nan() {
1839        let nan = Value::float(f64::NAN);
1840        let f = nan.as_float().unwrap();
1841        assert!(f.is_nan());
1842    }
1843
1844    #[test]
1845    fn test_string() {
1846        let v = Value::string("hello");
1847        assert_eq!(v.as_str(), Some("hello"));
1848        assert_eq!(v.type_name(), "string");
1849        assert_eq!(format!("{v}"), "\"hello\"");
1850    }
1851
1852    #[test]
1853    fn test_symbol() {
1854        let v = Value::symbol("foo");
1855        assert!(v.as_symbol_spur().is_some());
1856        assert_eq!(v.as_symbol(), Some("foo".to_string()));
1857        assert_eq!(v.type_name(), "symbol");
1858        assert_eq!(format!("{v}"), "foo");
1859    }
1860
1861    #[test]
1862    fn test_keyword() {
1863        let v = Value::keyword("bar");
1864        assert!(v.as_keyword_spur().is_some());
1865        assert_eq!(v.as_keyword(), Some("bar".to_string()));
1866        assert_eq!(v.type_name(), "keyword");
1867        assert_eq!(format!("{v}"), ":bar");
1868    }
1869
1870    #[test]
1871    fn test_char() {
1872        let v = Value::char('λ');
1873        assert_eq!(v.as_char(), Some('λ'));
1874        assert_eq!(v.type_name(), "char");
1875    }
1876
1877    #[test]
1878    fn test_list() {
1879        let v = Value::list(vec![Value::int(1), Value::int(2), Value::int(3)]);
1880        assert_eq!(v.as_list().unwrap().len(), 3);
1881        assert_eq!(v.type_name(), "list");
1882        assert_eq!(format!("{v}"), "(1 2 3)");
1883    }
1884
1885    #[test]
1886    fn test_clone_immediate() {
1887        let v = Value::int(42);
1888        let v2 = v.clone();
1889        assert_eq!(v.as_int(), v2.as_int());
1890    }
1891
1892    #[test]
1893    fn test_clone_heap() {
1894        let v = Value::string("hello");
1895        let v2 = v.clone();
1896        assert_eq!(v.as_str(), v2.as_str());
1897        // Both should work after clone
1898        assert_eq!(format!("{v}"), format!("{v2}"));
1899    }
1900
1901    #[test]
1902    fn test_equality() {
1903        assert_eq!(Value::int(42), Value::int(42));
1904        assert_ne!(Value::int(42), Value::int(43));
1905        assert_eq!(Value::nil(), Value::nil());
1906        assert_eq!(Value::bool(true), Value::bool(true));
1907        assert_ne!(Value::bool(true), Value::bool(false));
1908        assert_eq!(Value::string("a"), Value::string("a"));
1909        assert_ne!(Value::string("a"), Value::string("b"));
1910        assert_eq!(Value::symbol("x"), Value::symbol("x"));
1911    }
1912
1913    #[test]
1914    fn test_big_int_equality() {
1915        assert_eq!(Value::int(i64::MAX), Value::int(i64::MAX));
1916        assert_ne!(Value::int(i64::MAX), Value::int(i64::MIN));
1917    }
1918
1919    #[test]
1920    fn test_view_pattern_matching() {
1921        let v = Value::int(42);
1922        match v.view() {
1923            ValueView::Int(n) => assert_eq!(n, 42),
1924            _ => panic!("expected int"),
1925        }
1926
1927        let v = Value::string("hello");
1928        match v.view() {
1929            ValueView::String(s) => assert_eq!(&**s, "hello"),
1930            _ => panic!("expected string"),
1931        }
1932    }
1933
1934    #[test]
1935    fn test_env() {
1936        let env = Env::new();
1937        env.set_str("x", Value::int(42));
1938        assert_eq!(env.get_str("x"), Some(Value::int(42)));
1939    }
1940
1941    #[test]
1942    fn test_native_fn_simple() {
1943        let f = NativeFn::simple("add1", |args| Ok(args[0].clone()));
1944        let ctx = EvalContext::new();
1945        assert!((f.func)(&ctx, &[Value::int(42)]).is_ok());
1946    }
1947
1948    #[test]
1949    fn test_native_fn_with_ctx() {
1950        let f = NativeFn::with_ctx("get-depth", |ctx, _args| {
1951            Ok(Value::int(ctx.eval_depth.get() as i64))
1952        });
1953        let ctx = EvalContext::new();
1954        assert_eq!((f.func)(&ctx, &[]).unwrap(), Value::int(0));
1955    }
1956
1957    #[test]
1958    fn test_drop_doesnt_leak() {
1959        // Create and drop many heap values to check for leaks
1960        for _ in 0..10000 {
1961            let _ = Value::string("test");
1962            let _ = Value::list(vec![Value::int(1), Value::int(2)]);
1963            let _ = Value::int(i64::MAX); // big int
1964        }
1965    }
1966
1967    #[test]
1968    fn test_is_truthy() {
1969        assert!(!Value::nil().is_truthy());
1970        assert!(!Value::bool(false).is_truthy());
1971        assert!(Value::bool(true).is_truthy());
1972        assert!(Value::int(0).is_truthy());
1973        assert!(Value::int(1).is_truthy());
1974        assert!(Value::string("").is_truthy());
1975        assert!(Value::list(vec![]).is_truthy());
1976    }
1977
1978    #[test]
1979    fn test_as_float_from_int() {
1980        assert_eq!(Value::int(42).as_float(), Some(42.0));
1981        assert_eq!(Value::float(3.14).as_float(), Some(3.14));
1982    }
1983}