Skip to main content

lex_bytecode/
value.rs

1//! Runtime values.
2
3use crate::program::BodyHash;
4use arrow_array::RecordBatch;
5use indexmap::IndexMap;
6use smol_str::SmolStr;
7use std::collections::{BTreeMap, BTreeSet, VecDeque};
8use std::sync::atomic::AtomicBool;
9use std::sync::{Arc, Mutex};
10
11/// Internal state of a `conc.Actor`. Protected by a `Mutex` so that
12/// the `Lex` handler variant serialises on message delivery (one
13/// message processed at a time, state mutated under the lock). The
14/// `handler` is dispatched on the *calling* VM's thread — no extra
15/// OS thread required — which lets Lex handlers invoke arbitrary
16/// effects (sql, net, …) through the same handler chain.
17///
18/// Serialisation note: the `Native` variant releases the mutex
19/// *before* invoking its closure (`state` is unused for natives —
20/// the "state" is an external resource like a channel), so two
21/// concurrent `conc.tell`s on the same native bridge may invoke
22/// the closure on overlapping threads. Native bridges therefore
23/// need to be internally thread-safe; the `serve_ws_fn_actor`
24/// `mpsc::Sender` bridge is, because `Sender::send` is.
25#[derive(Debug, Clone)]
26pub struct ActorCell {
27    pub state: Value,
28    pub handler: ActorHandler,
29}
30
31/// Two ways an actor's handler can be implemented.
32///
33/// * `Lex(Value::Closure)` is the user-spawned shape from
34///   `conc.spawn(state, fn (s, m) -> (s, r) { … })`. The VM calls
35///   the closure with `(state, msg)` and expects `(new_state, reply)`.
36///
37/// * `Native(...)` is a Rust-side bridge — the actor cell wraps a
38///   `Box<dyn Fn(Value) -> Result<Value, String>>` that lives outside
39///   the VM. The `state` is ignored; the bridge is fire-and-forget
40///   over an out-of-band channel (e.g. a `mpsc::Sender<String>` to
41///   a WebSocket connection — see `lex-runtime::ws::serve_ws_fn_actor`).
42///   `conc.ask` against a native actor returns whatever the bridge
43///   produces; `conc.tell` discards it. v1 is only used internally by
44///   the WS server's outbound-bridge registration; not exposed via the
45///   `conc` builtin surface.
46#[derive(Clone)]
47pub enum ActorHandler {
48    Lex(Value),
49    Native(Arc<NativeActorHandler>),
50}
51
52/// Erased Rust-side handler for `ActorHandler::Native`. Boxed so we
53/// can store any closure that captures (e.g. an `mpsc::Sender`).
54/// Wrapped in `Arc` so cloning an `ActorCell` (which the existing
55/// `conc.tell` flow does — `let handler = guard.handler.clone()`)
56/// is cheap and the closure isn't duplicated.
57pub struct NativeActorHandler {
58    pub send: Box<dyn Fn(Value) -> Result<Value, String> + Send + Sync>,
59}
60
61impl std::fmt::Debug for NativeActorHandler {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        write!(f, "<native actor handler>")
64    }
65}
66
67impl std::fmt::Debug for ActorHandler {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        match self {
70            ActorHandler::Lex(v) => f.debug_tuple("Lex").field(v).finish(),
71            ActorHandler::Native(n) => f.debug_tuple("Native").field(n).finish(),
72        }
73    }
74}
75
76#[derive(Debug, Clone)]
77pub enum Value {
78    Int(i64),
79    Float(f64),
80    Bool(bool),
81    /// String value. `SmolStr` stores strings ≤ 22 bytes inline — no heap
82    /// allocation for identifiers, HTTP methods, status codes, short keys, etc.
83    /// Clone of a short `SmolStr` is a 24-byte stack copy (#389 slice 4).
84    Str(SmolStr),
85    Bytes(Vec<u8>),
86    Unit,
87    List(VecDeque<Value>),
88    Tuple(Vec<Value>),
89    /// Record literal. `shape_id` is the `Program::record_shapes`
90    /// index of the field-name vec the record was built from
91    /// (#462 slice 2), so the `Op::GetField` polymorphic IC can
92    /// match on a single u32 compare instead of walking the
93    /// `IndexMap` by name. Records constructed outside the bytecode
94    /// (JSON decode, SQL row → record, HTTP request mutators, test
95    /// fixtures) have no compile-time shape and carry `NO_SHAPE_ID`
96    /// — the IC unconditionally misses on them and falls through to
97    /// the existing name walk.
98    ///
99    /// `fields` is `Box<IndexMap>` rather than `IndexMap` inline
100    /// because the bare `IndexMap` is ~56B; inlining it plus
101    /// `shape_id` would push `Value`'s enum size from 64B → 72B,
102    /// which measurably regresses the VM stack push/pop loop
103    /// (`Value` is cloned/moved on every push/pop). Boxing keeps
104    /// `Value::Record` at 16B and `Value` at the pre-#462 64B.
105    /// The indirection on every `IndexMap` access costs a few ns
106    /// but the IC drops the field-name string compare on every
107    /// hit, which is the net win on `mono_chain`.
108    ///
109    /// `shape_id` is **not** part of structural equality (see
110    /// `PartialEq` below): two records with identical fields must
111    /// compare equal regardless of provenance, so a JSON-decoded
112    /// record equals a compile-time-built one with the same fields.
113    Record { shape_id: u32, fields: Box<IndexMap<SmolStr, Value>> },
114    /// Frame-local record (#464 step 2). Emitted by
115    /// `Op::AllocStackRecord` at sites the escape analysis proved
116    /// can't outlive the current call frame. `slab_start` indexes
117    /// into `Vm::stack_record_arena`; the `field_count` consecutive
118    /// values starting there are the record's fields, in
119    /// `Program.record_shapes[shape_id]` order (same insertion order
120    /// as `Op::MakeRecord` uses, so the polymorphic-IC offset is
121    /// interoperable with `Value::Record`).
122    ///
123    /// `Op::GetField` is the only consumer that knows how to read
124    /// these — every other observation point (`Op::Return`,
125    /// `Op::Call`, `Op::MakeRecord` as a field value, …) is an
126    /// escape op that the analysis prevents this variant from
127    /// reaching. If a `StackRecord` ever does reach an unexpected
128    /// site (escape-analysis bug), it surfaces as a panic at the
129    /// boundary, not undefined behavior — the arena is plain
130    /// `Vec<Value>` in safe Rust.
131    ///
132    /// Size: 4 (shape_id) + 4 (slab_start) + 2 (field_count) = 10
133    /// bytes payload + tag, comfortably inside the 64B `Value`
134    /// envelope.
135    StackRecord { shape_id: u32, slab_start: u32, field_count: u16 },
136    /// Frame-local tuple (#464 tuple codegen). The stack-alloc
137    /// analogue of `Value::Tuple`, emitted by `Op::AllocStackTuple` at
138    /// sites the escape analysis proved can't outlive the current
139    /// frame. `slab_start` indexes into `Vm::stack_record_arena` (the
140    /// arena is shared with `StackRecord` — both are flat `Value`
141    /// slabs released together on `Op::Return`); the `arity`
142    /// consecutive values starting there are the tuple elements in
143    /// positional order.
144    ///
145    /// Like `StackRecord`, the only consumer that knows how to read
146    /// these is `Op::GetElem` — every other observation point
147    /// (`Return`, `Call`, a `MakeTuple`/`MakeRecord` field value,
148    /// equality, JSON) is an escape op the analysis prevents this
149    /// variant from reaching. An unexpected arrival surfaces as a
150    /// panic at the boundary, not UB (the arena is safe `Vec<Value>`).
151    StackTuple { slab_start: u32, arity: u16 },
152    Variant { name: String, args: Vec<Value> },
153    /// First-class function value (a lambda + its captured locals). The
154    /// function's first `captures.len()` params bind to `captures`; the
155    /// remaining params are supplied at call time.
156    ///
157    /// `fn_id` is a dense compile-time index into `Program::functions`
158    /// for fast dispatch; `body_hash` is the **canonical identity** —
159    /// two closures with identical bytecode bodies compare equal even
160    /// when their `fn_id`s differ (which they will, when the source
161    /// has the same closure literal at two locations). See `PartialEq`
162    /// below and #222 for the rationale.
163    Closure { fn_id: u32, body_hash: BodyHash, captures: Vec<Value> },
164    /// Dense row-major `f64` matrix. A "fast lane" representation that
165    /// avoids the per-element `Value::Float` boxing of `Value::List`.
166    /// Used by Core's native tensor ops (matmul, dot, …) so end-to-end
167    /// matmul perf hits the §13.7 #1 100ms target without paying for
168    /// 2M Value boxings at the call boundary.
169    F64Array { rows: u32, cols: u32, data: Vec<f64> },
170    /// Persistent map keyed by `MapKey` (`Str` or `Int`). Insertion-
171    /// independent equality (sorted by `BTreeMap`'s `Ord`), so two
172    /// maps built from the same pairs in different orders compare
173    /// equal. Restricting keys to two primitive variants keeps
174    /// `Eq + Hash` requirements off `Value` itself, which has
175    /// closures and floats and can't be hashed soundly.
176    Map(BTreeMap<MapKey, Value>),
177    /// Persistent set with the same key-type discipline as `Map`.
178    Set(BTreeSet<MapKey>),
179    /// Double-ended queue. O(1) push/pop on both ends; otherwise
180    /// behaves like `List` for iteration / equality / JSON shape.
181    /// Lex's type system tracks `Deque[T]` separately from `List[T]`
182    /// so users explicitly opt in to deque semantics; the runtime
183    /// uses this dedicated variant rather than backing a deque on top
184    /// of `Value::List` (which would make `push_front` O(n)).
185    Deque(VecDeque<Value>),
186    /// A handle to a `conc.Actor`. The `Arc<Mutex<ActorCell>>` allows
187    /// cheap cloning and safe concurrent access — the mutex serialises
188    /// message delivery so the actor processes one message at a time.
189    /// Two actor handles compare equal iff they point to the same cell
190    /// (identity equality, not structural equality).
191    Actor(Arc<Mutex<ActorCell>>),
192    /// A periodic-tick handle returned by `conc.every` (#445). The
193    /// `AtomicBool` is the cancel flag — `conc.cancel(t)` sets it and
194    /// the background scheduler thread observes it on its next iteration
195    /// and exits. Two ticker handles compare equal iff they point to the
196    /// same cancel flag.
197    Ticker(Arc<AtomicBool>),
198    /// Apache Arrow `RecordBatch` — an unboxed columnar table. The
199    /// "fast lane" representation for `lex-frame` and any future
200    /// dataframe code: a `Value::ArrowTable` with one int64 column
201    /// of N rows is N×8 bytes of contiguous memory, not N
202    /// `Value::Int(_)` enum tags inside a `VecDeque`. Reductions
203    /// (`arrow.col_sum_int`, `arrow.col_mean`, …) execute as one
204    /// Rust call over the flat buffer, bypassing the bytecode VM
205    /// for the inner loop.
206    ///
207    /// `Arc` makes clone cheap (refcount bump) — Arrow tables are
208    /// already immutable so structural sharing across closures is
209    /// safe. Equality is structural over schema + columns.
210    ArrowTable(Arc<RecordBatch>),
211}
212
213/// Manual `PartialEq` for `Value` (#222). Mirrors the auto-derived
214/// implementation for every variant *except* `Closure`, which compares
215/// on `(body_hash, captures)` only — `fn_id` is a dense compile-time
216/// index that is not stable across source-location-equivalent closure
217/// literals, and including it would defeat the canonicality property
218/// the `body_hash` field exists to provide.
219impl PartialEq for Value {
220    fn eq(&self, other: &Self) -> bool {
221        use Value::*;
222        match (self, other) {
223            (Int(a), Int(b)) => a == b,
224            (Float(a), Float(b)) => a == b,
225            (Bool(a), Bool(b)) => a == b,
226            (Str(a), Str(b)) => a == b,
227            (Bytes(a), Bytes(b)) => a == b,
228            (Unit, Unit) => true,
229            (List(a), List(b)) => a == b,
230            (Tuple(a), Tuple(b)) => a == b,
231            (Record { fields: a, .. }, Record { fields: b, .. }) => a == b,
232            // #464 step 2: a `Value::StackRecord` can only reach
233            // generic equality if it crossed an escape boundary the
234            // analysis was supposed to reject. Treat as a soundness
235            // bug: panic rather than silently lie about equality (a
236            // wrong answer would cascade into mis-routed match arms).
237            // Well-typed Lex source never compares records with
238            // `==` via `bin_eq` — record equality, if added, will
239            // get its own opcode with arena-aware comparison.
240            (StackRecord { .. }, _) | (_, StackRecord { .. }) =>
241                panic!("BUG(#464): Value::StackRecord reached generic equality \
242                        — escape analysis should have flagged its allocation site"),
243            // Same soundness contract as StackRecord above.
244            (StackTuple { .. }, _) | (_, StackTuple { .. }) =>
245                panic!("BUG(#464): Value::StackTuple reached generic equality \
246                        — escape analysis should have flagged its allocation site"),
247            (Variant { name: an, args: aa }, Variant { name: bn, args: ba }) =>
248                an == bn && aa == ba,
249            (Closure { body_hash: ah, captures: ac, .. },
250             Closure { body_hash: bh, captures: bc, .. }) =>
251                ah == bh && ac == bc,
252            (F64Array { rows: ar, cols: ac, data: ad },
253             F64Array { rows: br, cols: bc, data: bd }) =>
254                ar == br && ac == bc && ad == bd,
255            (Map(a), Map(b)) => a == b,
256            (Set(a), Set(b)) => a == b,
257            (Deque(a), Deque(b)) => a == b,
258            // Actor identity: same if both handles point to the same cell.
259            (Actor(a), Actor(b)) => Arc::ptr_eq(a, b),
260            // Ticker identity: same if both handles point to the same
261            // cancel flag (one ticker spawn → one flag).
262            (Ticker(a), Ticker(b)) => Arc::ptr_eq(a, b),
263            // Arrow table equality: structural over schema + columns.
264            // RecordBatch implements PartialEq directly.
265            (ArrowTable(a), ArrowTable(b)) => a == b,
266            _ => false,
267        }
268    }
269}
270
271/// Hashable, ordered key for `Value::Map` / `Value::Set`. v1
272/// supports `Str` and `Int`; extending to other primitives or to
273/// records is forward-compatible since the type is not exposed
274/// to user code beyond the surface API.
275#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
276pub enum MapKey {
277    Str(String),
278    Int(i64),
279}
280
281impl MapKey {
282    pub fn from_value(v: &Value) -> Result<Self, String> {
283        match v {
284            Value::Str(s) => Ok(MapKey::Str(s.to_string())),
285            Value::Int(n) => Ok(MapKey::Int(*n)),
286            other => Err(format!(
287                "map/set key must be Str or Int, got {other:?}")),
288        }
289    }
290    pub fn into_value(self) -> Value {
291        match self {
292            MapKey::Str(s) => Value::Str(s.into()),
293            MapKey::Int(n) => Value::Int(n),
294        }
295    }
296    pub fn as_value(&self) -> Value {
297        match self {
298            MapKey::Str(s) => Value::Str(s.as_str().into()),
299            MapKey::Int(n) => Value::Int(*n),
300        }
301    }
302}
303
304impl Value {
305    pub fn as_int(&self) -> i64 {
306        match self { Value::Int(n) => *n, other => panic!("expected Int, got {other:?}") }
307    }
308    pub fn as_float(&self) -> f64 {
309        match self { Value::Float(n) => *n, other => panic!("expected Float, got {other:?}") }
310    }
311    pub fn as_bool(&self) -> bool {
312        match self { Value::Bool(b) => *b, other => panic!("expected Bool, got {other:?}") }
313    }
314    pub fn as_str(&self) -> &str {
315        match self { Value::Str(s) => s, other => panic!("expected Str, got {other:?}") }
316    }
317
318    /// Render this `Value` as a `serde_json::Value` for emission to
319    /// CLI output, the agent API, conformance harness reports, etc.
320    /// Canonical mapping shared across crates; previously every
321    /// boundary had its own copy.
322    ///
323    /// Encoding:
324    /// - `Variant { name, args }` → `{"$variant": name, "args": [...]}`
325    /// - `F64Array { ... }` → `{"$f64_array": true, rows, cols, data}`
326    /// - `Closure { body_hash, .. }` → `"<closure HEX8>"` (first 8 hex
327    ///   chars of the body hash; equivalent closures across source
328    ///   locations render identically — see #222)
329    /// - `Bytes` → `{"$bytes": "deadbeef"}` (lowercase hex). Round-trips
330    ///   through `from_json`. Bare hex strings decode as `Str`, so the
331    ///   marker is required to disambiguate bytes from a string that
332    ///   happens to look like hex.
333    /// - `Map` with all-`Str` keys → JSON object; otherwise array of
334    ///   `[key, value]` pairs (Int keys can't be JSON-object keys)
335    /// - `Set` → JSON array of elements
336    /// - other variants → their natural JSON shape
337    ///
338    /// Note: this form is **not** round-trippable for traces (see
339    /// `lex-trace`'s recorder, which uses a richer marker form).
340    pub fn to_json(&self) -> serde_json::Value {
341        use serde_json::Value as J;
342        match self {
343            Value::Int(n) => J::from(*n),
344            Value::Float(f) => J::from(*f),
345            Value::Bool(b) => J::Bool(*b),
346            Value::Str(s) => J::String(s.to_string()),
347            Value::Bytes(b) => {
348                let hex: String = b.iter().map(|b| format!("{:02x}", b)).collect();
349                let mut m = serde_json::Map::new();
350                m.insert("$bytes".into(), J::String(hex));
351                J::Object(m)
352            }
353            Value::Unit => J::Null,
354            Value::List(items) => J::Array(items.iter().map(Value::to_json).collect()),
355            Value::Tuple(items) => J::Array(items.iter().map(Value::to_json).collect()),
356            Value::Record { fields, .. } => {
357                let mut m = serde_json::Map::new();
358                for (k, v) in fields.iter() { m.insert(k.to_string(), v.to_json()); }
359                J::Object(m)
360            }
361            // #464: should never reach JSON serialization. See PartialEq.
362            Value::StackRecord { .. } =>
363                panic!("BUG(#464): Value::StackRecord reached to_json — \
364                        escape analysis should have prevented escape to a host boundary"),
365            Value::StackTuple { .. } =>
366                panic!("BUG(#464): Value::StackTuple reached to_json — \
367                        escape analysis should have prevented escape to a host boundary"),
368            Value::Variant { name, args } => {
369                let mut m = serde_json::Map::new();
370                m.insert("$variant".into(), J::String(name.clone()));
371                m.insert("args".into(), J::Array(args.iter().map(Value::to_json).collect()));
372                J::Object(m)
373            }
374            Value::Closure { body_hash, .. } => {
375                // Render the first 4 bytes (8 hex chars) of the body
376                // hash. Trace stability follows: equivalent closures
377                // produced from different source locations get the
378                // same string. See #222.
379                let prefix: String = body_hash.iter().take(4)
380                    .map(|b| format!("{b:02x}")).collect();
381                J::String(format!("<closure {prefix}>"))
382            }
383            Value::F64Array { rows, cols, data } => {
384                let mut m = serde_json::Map::new();
385                m.insert("$f64_array".into(), J::Bool(true));
386                m.insert("rows".into(), J::from(*rows));
387                m.insert("cols".into(), J::from(*cols));
388                m.insert("data".into(), J::Array(data.iter().map(|f| J::from(*f)).collect()));
389                J::Object(m)
390            }
391            Value::Map(m) => {
392                let all_str = m.keys().all(|k| matches!(k, MapKey::Str(_)));
393                if all_str {
394                    let mut out = serde_json::Map::new();
395                    for (k, v) in m {
396                        if let MapKey::Str(s) = k {
397                            out.insert(s.clone(), v.to_json());
398                        }
399                    }
400                    J::Object(out)
401                } else {
402                    J::Array(m.iter().map(|(k, v)| {
403                        J::Array(vec![k.as_value().to_json(), v.to_json()])
404                    }).collect())
405                }
406            }
407            Value::Set(s) => J::Array(
408                s.iter().map(|k| k.as_value().to_json()).collect()),
409            Value::Deque(items) => J::Array(items.iter().map(Value::to_json).collect()),
410            Value::Actor(_) => J::String("<actor>".into()),
411            Value::Ticker(_) => J::String("<ticker>".into()),
412            Value::ArrowTable(t) => {
413                // Compact summary: schema + nrows. Full data is intentionally
414                // not emitted — Arrow tables can be GB-scale and a JSON dump
415                // would defeat the point. Callers that need the rows go
416                // through `arrow.row_at` / `arrow.col_to_*_list`.
417                let mut m = serde_json::Map::new();
418                m.insert("$arrow_table".into(), J::Bool(true));
419                m.insert("nrows".into(), J::from(t.num_rows() as i64));
420                m.insert("ncols".into(), J::from(t.num_columns() as i64));
421                let cols: Vec<J> = t
422                    .schema()
423                    .fields()
424                    .iter()
425                    .map(|f| {
426                        let mut o = serde_json::Map::new();
427                        o.insert("name".into(), J::String(f.name().clone()));
428                        o.insert("type".into(), J::String(format!("{}", f.data_type())));
429                        J::Object(o)
430                    })
431                    .collect();
432                m.insert("schema".into(), J::Array(cols));
433                J::Object(m)
434            }
435        }
436    }
437
438    /// Decode a `serde_json::Value` into a `Value`. The inverse of
439    /// [`to_json`](Self::to_json) for the shapes Lex round-trips:
440    ///
441    /// - `{"$variant": "Name", "args": [...]}` → `Value::Variant`
442    /// - `{"$bytes": "deadbeef"}` → `Value::Bytes` (lowercase hex; an
443    ///   odd-length string or non-hex character falls through to
444    ///   `Value::Record`, matching the malformed-`$variant` fallback)
445    /// - JSON object → `Value::Record`
446    /// - JSON array → `Value::List`
447    /// - JSON null → `Value::Unit`
448    /// - JSON string / bool / number → the corresponding scalar
449    ///
450    /// Map, Set, F64Array, and Closure don't round-trip — they decode
451    /// as their natural JSON shape (Object / Array / Object / Str
452    /// respectively), since the CLI / HTTP / VM callers building Values
453    /// from JSON don't have those shapes in their input vocabulary.
454    pub fn from_json(v: &serde_json::Value) -> Value {
455        use serde_json::Value as J;
456        match v {
457            J::Null => Value::Unit,
458            J::Bool(b) => Value::Bool(*b),
459            J::Number(n) => {
460                if let Some(i) = n.as_i64() { Value::Int(i) }
461                else if let Some(f) = n.as_f64() { Value::Float(f) }
462                else { Value::Unit }
463            }
464            J::String(s) => Value::Str(s.as_str().into()),
465            J::Array(items) => Value::List(items.iter().map(Value::from_json).collect::<VecDeque<_>>()),
466            J::Object(map) => {
467                if let (Some(J::String(name)), Some(J::Array(args))) =
468                    (map.get("$variant"), map.get("args"))
469                {
470                    return Value::Variant {
471                        name: name.clone(),
472                        args: args.iter().map(Value::from_json).collect(),
473                    };
474                }
475                if map.len() == 1 {
476                    if let Some(J::String(hex)) = map.get("$bytes") {
477                        if let Some(bytes) = decode_hex(hex) {
478                            return Value::Bytes(bytes);
479                        }
480                    }
481                }
482                let mut out = indexmap::IndexMap::new();
483                for (k, v) in map {
484                    out.insert(k.clone(), Value::from_json(v));
485                }
486                Value::record_dynamic(out)
487            }
488        }
489    }
490
491    /// Build a `Value::Record` whose fields don't come from an
492    /// `Op::MakeRecord` site — JSON decode, SQL row → record, host
493    /// effect handlers, test fixtures, etc. Interns the field-name
494    /// set in the process-global shape registry (#462 slice 3) so
495    /// records with the same set of field names share a stable
496    /// `shape_id` and hit the same IC slot. Two records with the
497    /// same fields in different insertion order share a `shape_id`
498    /// (the registry sorts the field-name vec before lookup),
499    /// matching the existing `Value::Record` structural-equality
500    /// semantics.
501    ///
502    /// Dynamic shape IDs live in the high half of the `u32` range
503    /// (see `crate::shape_registry::DYNAMIC_SHAPE_ID_BASE`) so they
504    /// can't collide with the per-program shape indices emitted by
505    /// `Op::MakeRecord`. Mixed-flavor IC sites (which the slice-2b
506    /// measurement found at exactly zero occurrences) would still
507    /// be correct under the IC's shape-keyed verifier — they'd just
508    /// churn the cache.
509    /// Build a `Record` from a String-keyed host map (JSON decode, SQL
510    /// rows, builtins). Keys are re-collected into interned `SmolStr`
511    /// (#461 field-name interning). The hot bytecode `MakeRecord` path
512    /// builds `SmolStr`-keyed maps directly and never routes through
513    /// here; callers that already hold an interned map use
514    /// `record_interned`.
515    pub fn record_dynamic(fields: IndexMap<String, Value>) -> Value {
516        let shape_id = crate::shape_registry::intern(fields.keys());
517        let fields: IndexMap<SmolStr, Value> =
518            fields.into_iter().map(|(k, v)| (SmolStr::from(k), v)).collect();
519        Value::Record { shape_id, fields: Box::new(fields) }
520    }
521
522    /// Build a `Record` from an already-interned `SmolStr`-keyed map —
523    /// used by the http builder chain, which threads `SmolStr` keys
524    /// through `with_header`/`with_query`/… without round-tripping back
525    /// to `String` (#461).
526    pub fn record_interned(fields: IndexMap<SmolStr, Value>) -> Value {
527        let shape_id = crate::shape_registry::intern(fields.keys());
528        Value::Record { shape_id, fields: Box::new(fields) }
529    }
530}
531
532/// Sentinel `shape_id` for records constructed outside an
533/// `Op::MakeRecord` site (#462 slice 2). `Program::record_shapes`
534/// is bounded by `u32::MAX - 1` in practice (each compile-time
535/// record literal adds one entry), so reserving the top of the
536/// `u32` range as "no shape" keeps `Value::Record.shape_id` a flat
537/// `u32` — the `Op::GetField` IC's hot path is a single u32
538/// compare, no `Option` discriminant.
539pub const NO_SHAPE_ID: u32 = u32::MAX;
540
541/// Lowercase-hex → bytes. Returns `None` for odd length or non-hex chars
542/// (callers fall through to a record decode rather than erroring).
543fn decode_hex(s: &str) -> Option<Vec<u8>> {
544    if !s.len().is_multiple_of(2) { return None; }
545    let mut out = Vec::with_capacity(s.len() / 2);
546    let bytes = s.as_bytes();
547    for pair in bytes.chunks(2) {
548        let hi = (pair[0] as char).to_digit(16)?;
549        let lo = (pair[1] as char).to_digit(16)?;
550        out.push(((hi << 4) | lo) as u8);
551    }
552    Some(out)
553}