brink-runtime 0.0.15

Runtime/VM for executing compiled ink stories
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! TM-4 record opcode implementations (`docs/typed-mode-spec.md` §6;
//! fault semantics from `docs/value-model-spec.md` §11c).
//!
//! Every op here is total in the same sense [`collection_ops`] is: a
//! missing field is a turn-terminating `RuntimeError`, never a silent
//! `Null`. Mutation goes through `Value::record_make_mut` — the take →
//! `make_mut` → write-back RMW discipline (value-model-spec §5): an
//! unshared record mutates in place, a shared one COWs exactly once.
//!
//! `RecordGetDyn`/`RecordSetDyn` resolve a field by name against the
//! record's own shape (looked up in [`Program::struct_shape`] by the
//! record's `ShapeId`) — the by-name field ops every dialect can use
//! correctly, per typed-mode-spec §6 ("gradual: Unknown head defers to
//! runtime field lookup with a turn-terminating fault on missing field").
//! Static-offset field ops (`RecordGet`/`RecordSet`, the strict-mode
//! performance payoff the spec anticipates) are reserved at the format layer
//! but not implemented here — see the PR description's scope note.
//!
//! [`collection_ops`]: crate::collection_ops

use alloc::format;
use alloc::vec::Vec;

use brink_format::{NameId, ShapeId, Value};

use crate::error::RuntimeError;
use crate::program::Program;
use crate::story::Flow;

/// `RecordNew(shape_id)`: pop the shape's declared field count worth of
/// values (in reverse push order — the caller/compiler is responsible for
/// pushing them in shape declaration order), push `Record { shape, fields }`.
pub(crate) fn record_new(
    flow: &mut Flow,
    program: &Program,
    shape_id: u32,
) -> Result<(), RuntimeError> {
    let shape = ShapeId(shape_id);
    let entry = program
        .struct_shape(shape)
        .ok_or(RuntimeError::InvalidShapeId(shape_id))?;
    let field_count = entry.fields.len();
    let mut fields = Vec::with_capacity(field_count);
    for _ in 0..field_count {
        fields.push(flow.pop_value()?);
    }
    fields.reverse();
    flow.value_stack.push(Value::record(shape, fields));
    Ok(())
}

/// `RecordGetDyn(name_id)`: `[record]` → field value, looked up by name in
/// the record's own shape. Turn-terminating fault if the popped value isn't
/// a `Record`, or if its shape has no field by that name.
pub(crate) fn record_get_dyn(
    flow: &mut Flow,
    program: &Program,
    name_id: u16,
) -> Result<(), RuntimeError> {
    let record = flow.pop_value()?;
    // Tower component access (NS-A8): `v.x` / `m.x_axis` parses as the same
    // dynamic field read a struct uses, so the tower's pure accessors hang
    // off this op — glam's component vocabulary, resolved by name. An
    // unknown component on a tower value is a turn-terminating fault, same
    // shape as a missing struct field.
    if crate::value_ops::is_tower(&record) {
        let name = program
            .name_checked(NameId(name_id))
            .ok_or(RuntimeError::InvalidNameId(name_id))?;
        let Some(component) = crate::tower_ops::tower_component(&record, name) else {
            return Err(RuntimeError::TypeError(alloc::format!(
                "`{}` has no component `{name}`",
                type_name(&record)
            )));
        };
        flow.value_stack.push(component);
        return Ok(());
    }
    let value = read_field(program, &record, NameId(name_id))?.clone();
    flow.value_stack.push(value);
    Ok(())
}

/// `RecordSetDyn(name_id)`: `[record, value]` → updated record (take →
/// `make_mut` → write-back), field selected by name. Turn-terminating fault
/// if the popped container isn't a `Record`, or if its shape has no field
/// by that name.
pub(crate) fn record_set_dyn(
    flow: &mut Flow,
    program: &Program,
    name_id: u16,
) -> Result<(), RuntimeError> {
    let value = flow.pop_value()?;
    let mut record = flow.pop_value()?;
    // Tower values (NS-A8) are immutable compounds — components are
    // read-only accessors; writes construct a new value. Explicit fault
    // rather than the generic not-a-record wording.
    if crate::value_ops::is_tower(&record) {
        return Err(RuntimeError::TypeError(alloc::format!(
            "`{}` components are read-only — construct a new value instead",
            type_name(&record)
        )));
    }
    let idx = field_index(program, &record, NameId(name_id))?;
    note_record_mutation(&record);
    let Some(fields) = record.record_make_mut() else {
        return Err(RuntimeError::NotARecord(type_name(&record)));
    };
    #[expect(
        clippy::indexing_slicing,
        reason = "index validated by field_index above"
    )]
    {
        fields[idx] = value;
    }
    flow.value_stack.push(record);
    Ok(())
}

/// `RecordGet(offset)`: `[record]` → field value, looked up by flat offset
/// into the record's own field vector (TM-4c, the strict-mode static-offset
/// payoff typed-mode-spec §6 anticipates). Unlike [`record_get_dyn`], this
/// never touches `Program::struct_shapes` — the offset is trusted to
/// already be correct for whatever shape the compiler proved at LIR
/// lowering time, and only the record's own field count is checked, which
/// is exactly the lookup this op exists to skip. Turn-terminating fault if
/// the popped value isn't a `Record`, or the offset is out of range.
pub(crate) fn record_get(flow: &mut Flow, offset: u16) -> Result<(), RuntimeError> {
    let record = flow.pop_value()?;
    let Some((_, fields)) = record.as_record() else {
        return Err(RuntimeError::NotARecord(type_name(&record)));
    };
    let Some(value) = fields.get(offset as usize).cloned() else {
        return Err(RuntimeError::RecordFieldOffsetOutOfRange {
            offset,
            len: fields.len(),
        });
    };
    flow.value_stack.push(value);
    Ok(())
}

/// `RecordSet(offset)`: `[record, value]` → updated record (take →
/// `make_mut` → write-back — identical COW discipline to
/// [`record_set_dyn`]), field selected by flat offset. Turn-terminating
/// fault if the popped container isn't a `Record`, or the offset is out of
/// range.
pub(crate) fn record_set(flow: &mut Flow, offset: u16) -> Result<(), RuntimeError> {
    let value = flow.pop_value()?;
    let mut record = flow.pop_value()?;
    let len = record
        .as_record()
        .map(|(_, fields)| fields.len())
        .ok_or_else(|| RuntimeError::NotARecord(type_name(&record)))?;
    if offset as usize >= len {
        return Err(RuntimeError::RecordFieldOffsetOutOfRange { offset, len });
    }
    note_record_mutation(&record);
    let Some(fields) = record.record_make_mut() else {
        return Err(RuntimeError::NotARecord(type_name(&record)));
    };
    #[expect(
        clippy::indexing_slicing,
        reason = "offset validated against len above"
    )]
    {
        fields[offset as usize] = value;
    }
    flow.value_stack.push(record);
    Ok(())
}

// ── Shared helpers ───────────────────────────────────────────────────────

/// Record a COW-copy event if mutating `record` via the next
/// `record_make_mut()` call will find a shared `Arc` and pay the O(n) copy
/// — see `collection_ops::note_array_mutation` for the full rationale
/// (issue #821 Workstream B seed). No-op unless the `bench-counters`
/// feature is enabled.
#[cfg(feature = "bench-counters")]
#[inline]
fn note_record_mutation(record: &Value) {
    if let Value::Record { fields, .. } = record
        && alloc::sync::Arc::strong_count(fields) > 1
    {
        crate::bench_counters::record_cow_copy();
    }
}
#[cfg(not(feature = "bench-counters"))]
#[inline(always)]
fn note_record_mutation(_record: &Value) {}

fn type_name(v: &Value) -> &'static str {
    match v {
        Value::Int(_) => "int",
        Value::Float(_) => "float",
        Value::Bool(_) => "bool",
        Value::String(_) => "string",
        Value::List(_) => "list",
        Value::DivertTarget(_) => "divert_target",
        Value::VariablePointer(_) => "var_pointer",
        Value::TempPointer { .. } => "temp_pointer",
        Value::Null => "null",
        Value::FragmentRef(_) => "fragment_ref",
        Value::Array(_) => "array",
        Value::Map(_) => "map",
        Value::Record { .. } => "record",
        Value::FnRef(_) | Value::Closure(_) => "fn",
        Value::Handle { .. } => "handle",
        Value::Projection(_) => "projection",
        Value::OptionVal(_) => "option",
        Value::Range { .. } => "range",
        Value::Vec2(_) => "vec2",
        Value::Vec3(_) => "vec3",
        Value::Vec4(_) => "vec4",
        Value::Quat(_) => "quat",
        Value::Mat2(_) => "mat2",
        Value::Mat3(_) => "mat3",
        Value::Mat4(_) => "mat4",
        Value::Weighted(_) => "weighted",
    }
}

/// Resolve `name` to its flat-field index within `record`'s own shape.
/// Fault if `record` isn't a `Record`, or its shape has no such field.
fn field_index(program: &Program, record: &Value, name: NameId) -> Result<usize, RuntimeError> {
    let Some((shape, _)) = record.as_record() else {
        return Err(RuntimeError::NotARecord(type_name(record)));
    };
    let entry = program
        .struct_shape(shape)
        .ok_or(RuntimeError::InvalidShapeId(shape.0))?;
    entry.fields.iter().position(|&f| f == name).ok_or_else(|| {
        // `name` is a caller-provided `NameId` operand — safe-guard the
        // lookup rather than calling `Program::name` (which indexes
        // unconditionally) so a malformed/out-of-range id still produces a
        // clean `RuntimeError`, not a panic.
        let display = program
            .name_table
            .get(name.0 as usize)
            .cloned()
            .unwrap_or_else(|| format!("<name#{}>", name.0));
        RuntimeError::RecordFieldNotFound(display)
    })
}

fn read_field<'a>(
    program: &Program,
    record: &'a Value,
    name: NameId,
) -> Result<&'a Value, RuntimeError> {
    let idx = field_index(program, record, name)?;
    let Some((_, fields)) = record.as_record() else {
        return Err(RuntimeError::NotARecord(type_name(record)));
    };
    #[expect(
        clippy::indexing_slicing,
        reason = "index validated by field_index above"
    )]
    Ok(&fields[idx])
}

// ── Tests ────────────────────────────────────────────────────────────────
//
// Same "op function, not full VM" granularity `collection_ops` uses — these
// exercise the primitives directly against hand-built `Program`/`Value`
// trees, one level below full bytecode.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::output::OutputBuffer;
    use crate::program::{LinkedContainer, StructShapeEntry};
    use crate::story::PendingTerminal;
    use brink_format::{CountingFlags, DefinitionId, DefinitionTag};
    use std::collections::HashMap;

    fn test_flow() -> Flow {
        Flow {
            threads: Vec::new(),
            value_stack: Vec::new(),
            output: OutputBuffer::new(),
            pending_choices: Vec::new(),
            current_tags: Vec::new(),
            in_tag: false,
            skipping_choice: false,
            did_safe_exit: false,
            did_unsafe_yield: false,
            ran_out_of_content_cause: crate::RanOutOfContentCause::default(),
            exec_mode: crate::story::ExecMode::default(),
            pure_callback: crate::story::PureCallbackState::default(),
            next_block_id: 0,
            pending_terminal: PendingTerminal::default(),
        }
    }

    /// A minimal `Program` with a name table (`"x"` = 0, `"y"` = 1) and one
    /// declared struct shape `Point { x, y }` at `ShapeId(0)`.
    fn point_program() -> Program {
        Program {
            containers: vec![LinkedContainer {
                id: DefinitionId::new(DefinitionTag::Address, 0),
                bytecode: vec![],
                counting_flags: CountingFlags::empty(),
                path_hash: 0,
                param_count: 0,
                params: Vec::new(),
                scope_table_idx: 0,
            }],
            address_map: HashMap::new(),
            scope_ids: vec![DefinitionId::new(DefinitionTag::Address, 0)],
            source_checksum: 0,
            globals: vec![],
            global_map: HashMap::new(),
            name_table: vec!["x".to_string(), "y".to_string()],
            address_by_path: HashMap::new(),
            root_idx: 0,
            list_literals: vec![],
            literal_pool: vec![],
            list_item_map: HashMap::new(),
            list_defs: vec![],
            list_def_map: HashMap::new(),
            external_fns: HashMap::new(),
            local_scope_defaults: Vec::new(),
            struct_shapes: vec![StructShapeEntry {
                name: NameId(0),
                fields: vec![NameId(0), NameId(1)],
            }],
            private_defs: Vec::new(),
            alias_table: Vec::new(),
        }
    }

    fn push_args(flow: &mut Flow, args: Vec<Value>) {
        for v in args {
            flow.value_stack.push(v);
        }
    }

    #[test]
    fn record_new_constructs_in_shape_order() {
        let program = point_program();
        let mut flow = test_flow();
        push_args(&mut flow, vec![Value::Float(1.0), Value::Float(2.0)]);
        record_new(&mut flow, &program, 0).unwrap();
        let result = flow.pop_value().unwrap();
        let (shape, fields) = result.as_record().unwrap();
        assert_eq!(shape, ShapeId(0));
        assert_eq!(fields.as_slice(), &[Value::Float(1.0), Value::Float(2.0)]);
    }

    #[test]
    fn record_new_unknown_shape_faults() {
        let program = point_program();
        let mut flow = test_flow();
        let err = record_new(&mut flow, &program, 99).unwrap_err();
        assert_eq!(err, RuntimeError::InvalidShapeId(99));
    }

    #[test]
    fn record_get_dyn_reads_field_by_name() {
        let program = point_program();
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record]);
        record_get_dyn(&mut flow, &program, 1).unwrap(); // "y"
        assert_eq!(flow.pop_value().unwrap(), Value::Float(2.0));
    }

    #[test]
    fn record_get_dyn_missing_field_faults() {
        let program = point_program();
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record]);
        let err = record_get_dyn(&mut flow, &program, 5).unwrap_err();
        assert!(matches!(err, RuntimeError::RecordFieldNotFound(_)));
    }

    #[test]
    fn record_get_dyn_non_record_faults() {
        let program = point_program();
        let mut flow = test_flow();
        push_args(&mut flow, vec![Value::Int(1)]);
        let err = record_get_dyn(&mut flow, &program, 0).unwrap_err();
        assert_eq!(err, RuntimeError::NotARecord("int"));
    }

    #[test]
    fn record_set_dyn_writes_field_by_name() {
        let program = point_program();
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record, Value::Float(9.0)]);
        record_set_dyn(&mut flow, &program, 1).unwrap(); // "y"
        let result = flow.pop_value().unwrap();
        let (_, fields) = result.as_record().unwrap();
        assert_eq!(fields.as_slice(), &[Value::Float(1.0), Value::Float(9.0)]);
    }

    #[test]
    fn record_set_dyn_missing_field_faults() {
        let program = point_program();
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record, Value::Float(9.0)]);
        let err = record_set_dyn(&mut flow, &program, 7).unwrap_err();
        assert!(matches!(err, RuntimeError::RecordFieldNotFound(_)));
    }

    // ── Static-offset ops (TM-4c) ───────────────────────────────────────────

    #[test]
    fn record_get_reads_field_by_offset() {
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record]);
        record_get(&mut flow, 1).unwrap();
        assert_eq!(flow.pop_value().unwrap(), Value::Float(2.0));
    }

    #[test]
    fn record_get_out_of_range_offset_faults() {
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record]);
        let err = record_get(&mut flow, 5).unwrap_err();
        assert_eq!(
            err,
            RuntimeError::RecordFieldOffsetOutOfRange { offset: 5, len: 2 }
        );
    }

    #[test]
    fn record_get_non_record_faults() {
        let mut flow = test_flow();
        push_args(&mut flow, vec![Value::Int(1)]);
        let err = record_get(&mut flow, 0).unwrap_err();
        assert_eq!(err, RuntimeError::NotARecord("int"));
    }

    #[test]
    fn record_set_writes_field_by_offset() {
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record, Value::Float(9.0)]);
        record_set(&mut flow, 1).unwrap();
        let result = flow.pop_value().unwrap();
        let (_, fields) = result.as_record().unwrap();
        assert_eq!(fields.as_slice(), &[Value::Float(1.0), Value::Float(9.0)]);
    }

    #[test]
    fn record_set_out_of_range_offset_faults() {
        let mut flow = test_flow();
        let record = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        push_args(&mut flow, vec![record, Value::Float(9.0)]);
        let err = record_set(&mut flow, 7).unwrap_err();
        assert_eq!(
            err,
            RuntimeError::RecordFieldOffsetOutOfRange { offset: 7, len: 2 }
        );
    }

    #[test]
    fn record_set_non_record_faults() {
        let mut flow = test_flow();
        push_args(&mut flow, vec![Value::Int(1), Value::Float(9.0)]);
        let err = record_set(&mut flow, 0).unwrap_err();
        assert_eq!(err, RuntimeError::NotARecord("int"));
    }

    #[test]
    fn record_set_cows_when_shared() {
        let original = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        let snapshot = original.clone();
        let mut flow = test_flow();
        push_args(&mut flow, vec![original, Value::Float(9.0)]);
        record_set(&mut flow, 0).unwrap();
        let mutated = flow.pop_value().unwrap();
        assert_eq!(
            snapshot,
            Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]),
            "snapshot unmutated"
        );
        assert_eq!(
            mutated,
            Value::record(ShapeId(0), vec![Value::Float(9.0), Value::Float(2.0)])
        );
    }

    // ── COW / sharing law ─────────────────────────────────────────────────

    #[test]
    fn record_set_dyn_cows_when_shared() {
        let program = point_program();
        let original = Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]);
        let snapshot = original.clone();
        let mut flow = test_flow();
        push_args(&mut flow, vec![original, Value::Float(9.0)]);
        record_set_dyn(&mut flow, &program, 0).unwrap(); // "x"
        let mutated = flow.pop_value().unwrap();
        assert_eq!(
            snapshot,
            Value::record(ShapeId(0), vec![Value::Float(1.0), Value::Float(2.0)]),
            "snapshot unmutated"
        );
        assert_eq!(
            mutated,
            Value::record(ShapeId(0), vec![Value::Float(9.0), Value::Float(2.0)])
        );
    }

    #[test]
    fn record_equality_requires_matching_shape() {
        let a = Value::record(ShapeId(0), vec![Value::Int(1)]);
        let b = Value::record(ShapeId(1), vec![Value::Int(1)]);
        assert_ne!(a, b, "same fields, different shape must not be equal");
        let c = Value::record(ShapeId(0), vec![Value::Int(1)]);
        assert_eq!(a, c);
    }
}