hexicon-vm 0.2.0

The Hexicon virtual machine: runs the games Hexicon builds
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! The virtual machine's state and the helpers everything else builds on.

use std::ops::Deref;
use std::rc::Rc;

use hexicon_host::Host;
use hexicon_image::DebugInfo;

use crate::bif::FunctionSet;
use crate::console::{Formatter, Newline};
use crate::error::{VmError, VmResult, code};
use crate::meta::{MetaKind, MetaTable};
use crate::object::{Object, ObjectTable};
use crate::value::{INVALID_PROP, ObjId, PropId, Value};

/// What happens when a function returns.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Return {
    /// Continue the calling function at the return address.
    Normal,
    /// The function was called by the VM itself, not by byte code. Leave
    /// the interpreter loop and hand the result to the caller.
    Native,
    /// The function implements an overloaded operator. Push its result
    /// and continue at the return address.
    Operator,
    /// The function implements an overloaded operator in a compound
    /// assignment to a local. Store its result in that local of the
    /// caller and continue at the return address.
    OperatorToLocal(u16),
}

/// What the VM was doing when it called into byte code. Stack traces show
/// this in place of a byte-code frame.
#[derive(Debug, Clone)]
pub(crate) struct NativeCall {
    /// A description for error messages.
    pub name: &'static str,
    /// The built-in function making the call, if it is one.
    pub function: Option<(u16, u16)>,
    /// The object whose built-in method is making the call, or `nil`.
    pub self_value: Value,
    /// The number of that method within its built-in class.
    pub method_index: u16,
    /// The arguments the native code was called with, first argument first.
    pub args: Vec<Value>,
    /// Address of the instruction that invoked the native code, or zero.
    pub caller_pc: u32,
    /// How much was on the stack when the native code was called, with
    /// its arguments on top, or zero if that was not recorded. Named
    /// arguments lie under the positional ones.
    pub arg_top: usize,
}

impl NativeCall {
    /// A call with only a description.
    pub(crate) fn named(name: &'static str) -> Self {
        Self {
            name,
            function: None,
            self_value: Value::Nil,
            method_index: 0,
            args: Vec::new(),
            caller_pc: 0,
            arg_top: 0,
        }
    }
}

/// An activation of a byte-code function.
#[derive(Debug, Clone)]
pub(crate) struct Frame {
    /// Code pool address of the function's method header.
    pub func: u32,
    /// What to do when the function returns.
    pub ret: Return,
    /// Code pool address in the caller to continue at.
    pub return_pc: u32,
    /// Stack index of local variable 0. The arguments are just below:
    /// argument `i` (from 0) is at `base - 1 - i`.
    pub base: u32,
    /// Number of arguments the function was called with.
    pub argc: u32,
    /// The property being evaluated, or the invalid property.
    pub prop: PropId,
    /// The object the property was first looked up on, or `nil`.
    pub target: Value,
    /// The object the method was found in, or the invalid ID.
    pub definer: ObjId,
    /// `self`, or `nil` outside a method.
    pub self_value: Value,
    /// The function pointer or object that was invoked.
    pub invokee: Value,
    /// Present when the VM, not byte code, made the call.
    pub native: Option<Box<NativeCall>>,
    /// The `StackFrameRef` that stands for the call, once a stack trace
    /// has asked for one.
    pub frame_ref: Option<ObjId>,
}

impl Predef {
    /// The objects the VM knows by name, which are never garbage.
    pub(crate) fn objects(&self) -> [ObjId; 18] {
        [
            self.gram_alt_info,
            self.gram_alt_token_info,
            self.runtime_error,
            self.stack_info_class,
            self.const_str_obj,
            self.const_lst_obj,
            self.last_prop_obj,
            self.reflection_services,
            self.charset_unknown_exception,
            self.file_not_found_exc,
            self.file_creation_exc,
            self.file_open_exc,
            self.file_io_exc,
            self.file_sync_exc,
            self.file_closed_exc,
            self.file_mode_exc,
            self.file_safety_exc,
            self.file_info,
        ]
    }
}

/// The method context a call starts with.
#[derive(Debug, Clone, Copy)]
pub(crate) struct MethodContext {
    pub prop: PropId,
    pub target: Value,
    pub definer: ObjId,
    pub self_value: Value,
    pub invokee: Value,
}

impl MethodContext {
    /// The context of a plain function call.
    pub(crate) fn function(invokee: Value) -> Self {
        Self {
            prop: INVALID_PROP,
            target: Value::Nil,
            definer: ObjId::INVALID,
            self_value: Value::Nil,
            invokee,
        }
    }
}

/// Objects and properties the VM itself refers to. Images name most of
/// them through exported symbols; the rest the VM creates at load time.
#[derive(Debug, Clone, Default)]
pub(crate) struct Predef {
    /// Base class of the exceptions the VM throws.
    pub runtime_error: ObjId,
    /// Property holding a VM exception's message text.
    pub exception_message: PropId,
    /// The constructor property.
    pub construct: PropId,
    /// The finalizer property.
    #[allow(dead_code)]
    pub destruct: PropId,
    /// The highest property ID the compiler assigned.
    pub last_prop: PropId,
    /// Property invoked when an undefined property is called.
    pub prop_not_defined: PropId,
    /// Class of the entries in a stack trace.
    pub stack_info_class: ObjId,
    /// The `length` property.
    pub length: PropId,
    /// A string object standing for all constant strings in method lookups.
    pub const_str_obj: ObjId,
    /// A list object standing for all constant lists in method lookups.
    pub const_lst_obj: ObjId,
    /// A one-element vector holding the last property ID allocated.
    pub last_prop_obj: ObjId,
    /// Operator overload properties.
    pub operator_add: PropId,
    pub operator_sub: PropId,
    pub operator_mul: PropId,
    pub operator_div: PropId,
    pub operator_mod: PropId,
    pub operator_xor: PropId,
    pub operator_shl: PropId,
    pub operator_ashr: PropId,
    pub operator_lshr: PropId,
    pub operator_bit_not: PropId,
    pub operator_bit_and: PropId,
    pub operator_bit_or: PropId,
    pub operator_neg: PropId,
    pub operator_idx: PropId,
    pub operator_setidx: PropId,
    /// The object and property the VM calls to name a value.
    pub reflection_services: ObjId,
    pub reflection_val_to_symbol: PropId,
    /// What the grammar parser sets on the match objects it creates,
    /// and the classes `getGrammarInfo` describes a production with.
    pub gram_first_token: PropId,
    pub gram_last_token: PropId,
    pub gram_token_list: PropId,
    pub gram_token_match_list: PropId,
    pub gram_tag: PropId,
    pub gram_misc_vocab: PropId,
    pub gram_alt_props: PropId,
    pub gram_alt_info: ObjId,
    pub gram_alt_token_info: ObjId,
    /// The methods of a comparator a program writes for a dictionary.
    pub calc_hash: PropId,
    pub match_values: PropId,
    /// The exception class for using a character set the VM lacks.
    pub charset_unknown_exception: ObjId,
    /// The exception classes of the File class.
    pub file_not_found_exc: ObjId,
    pub file_creation_exc: ObjId,
    pub file_open_exc: ObjId,
    pub file_io_exc: ObjId,
    pub file_sync_exc: ObjId,
    pub file_closed_exc: ObjId,
    pub file_mode_exc: ObjId,
    pub file_safety_exc: ObjId,
    /// The exception class for code that would not compile.
    pub compiler_exc: ObjId,
    /// The class that describes a file for `FileName.getFileInfo`.
    pub file_info: ObjId,
    /// The methods of an object a program writes to stand for a file.
    pub filespec_get_filename: PropId,
    pub filespec_close_file: PropId,
    /// Where `specialsToHtml` keeps its state between calls.
    pub string_sth_flags: PropId,
    pub string_sth_tag: PropId,
    /// Property for converting an object to a string.
    pub obj_to_string: PropId,
    /// The iterator methods `for..in` loops call.
    pub iter_get_next: PropId,
    pub iter_next_avail: PropId,
}

/// The text of a string value: a constant in the image or a string object.
#[derive(Debug, Clone)]
pub(crate) enum StrRef {
    /// Bytes `start..start + len` of the constant pool.
    Const(Rc<[u8]>, u32, u32),
    /// The text of a string object.
    Obj(Rc<[u8]>),
}

impl Deref for StrRef {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        match self {
            Self::Const(pool, start, len) => &pool[*start as usize..(*start + *len) as usize],
            Self::Obj(text) => text,
        }
    }
}

/// The elements of a list value: a constant in the image or a list object.
#[derive(Debug, Clone)]
pub(crate) enum ListRef {
    /// A constant list: the pool, the offset of the first element, and the
    /// element count.
    Const(Rc<[u8]>, u32, u32),
    /// The elements of a list object.
    Obj(Rc<[Value]>),
}

impl ListRef {
    /// Number of elements.
    pub(crate) fn len(&self) -> usize {
        match self {
            Self::Const(_, _, len) => *len as usize,
            Self::Obj(items) => items.len(),
        }
    }

    /// The element at a zero-based index. Panics if out of range.
    pub(crate) fn get(&self, index: usize) -> Value {
        match self {
            Self::Const(pool, start, len) => {
                assert!(index < *len as usize);
                let at = *start as usize + index * Value::HOLDER_SIZE;
                Value::from_holder(&pool[at..at + Value::HOLDER_SIZE])
            }
            Self::Obj(items) => items[index],
        }
    }

    /// The elements, in order.
    pub(crate) fn iter(&self) -> impl Iterator<Item = Value> + '_ {
        (0..self.len()).map(|index| self.get(index))
    }

    /// The elements as a vector.
    pub(crate) fn to_vec(&self) -> Vec<Value> {
        self.iter().collect()
    }
}

/// Sizes of the structures in the code pool, which each image states.
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Layout {
    pub method_header: u32,
    pub exception_entry: u32,
    pub debug_line_entry: u32,
    pub debug_table_header: u32,
    /// Size of the header of a local symbol record in a debug table.
    pub debug_local_header: u32,
    /// Size of the header of a frame in a debug table.
    pub debug_frame_header: u32,
}

/// Stack slots available to a program, as in the reference VM's default
/// configuration. Programs can observe it: it decides how deep a runaway
/// recursion gets before it is stopped.
pub(crate) const STACK_LIMIT: usize = 4096;

/// Extra slots released while a stack overflow error is being reported:
/// enough to build its stack trace, which then keeps only the innermost and
/// outermost levels (`OVERFLOW_TRACE_LEVELS` of each).
pub(crate) const STACK_RESERVE: usize = 512;

/// How many of the innermost, and of the outermost, levels the stack trace
/// of a stack overflow keeps.
pub(crate) const OVERFLOW_TRACE_LEVELS: usize = 40;

/// Stack slots the reference VM spends on each call for bookkeeping that
/// Hexicon keeps elsewhere. They still count against the limit.
pub(crate) const FRAME_OVERHEAD: usize = 11;

/// Where a program stands after it has run for a while.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunState {
    /// The program has ended.
    Finished,
    /// The program is waiting for something the host did not have yet:
    /// a line of input, a key, or the end of a pause. The host calls
    /// [`Vm::resume`] when it has it.
    Suspended,
}

/// The most bytes a string can hold.
pub(crate) const MAX_STRING_LEN: usize = 65_535;

/// The virtual machine with a program loaded.
pub struct Vm {
    /// The code pool, as one contiguous array, and after it the code
    /// compiled while the program runs.
    pub(crate) code: Rc<[u8]>,
    /// The length of the image's own code.
    pub(crate) image_code_len: usize,
    /// The constant pool, as one contiguous array.
    pub(crate) consts: Rc<[u8]>,
    pub(crate) layout: Layout,
    pub(crate) objects: ObjectTable,
    pub(crate) meta: MetaTable,
    pub(crate) function_sets: Vec<FunctionSet>,
    pub(crate) predef: Predef,
    pub(crate) stack: Vec<Value>,
    pub(crate) frames: Vec<Frame>,
    /// The stack limit in force; raised briefly to report an overflow.
    pub(crate) stack_limit: usize,
    /// Register 0: the result of the last call.
    pub(crate) r0: Value,
    /// Address of the instruction being executed.
    pub(crate) pc: u32,
    /// The function the `say` instructions call, or `nil`.
    pub(crate) say_function: Value,
    /// The method the `say` instructions call on `self`, if it has one.
    pub(crate) say_method: PropId,
    /// The entry point's code address.
    pub(crate) entry_point: u32,
    /// Static initializers still to run: object and property.
    pub(crate) static_initializers: Vec<(ObjId, PropId)>,
    /// Debug records from the image, for stack traces.
    pub(crate) debug: DebugInfo,
    /// Whether the program is running under pre-initialization.
    pub(crate) preinit_mode: bool,
    /// The tables `globalSymbols` returns, once built.
    pub(crate) symbol_table: Option<ObjId>,
    pub(crate) macro_table: Option<ObjId>,
    /// Mathematical constants Decimal has computed so far.
    pub(crate) big_cache: crate::decimal_math::Cache,
    /// The random number generator.
    pub(crate) rng: crate::rng::Rng,
    /// The image's timestamp and file name, which a saved state records
    /// so that it is only restored into the program that made it.
    pub(crate) image_timestamp: Vec<u8>,
    pub(crate) image_name: String,
    /// The VM chose the property for exception messages itself, because
    /// the image did not name one.
    pub(crate) exception_message_synthesized: bool,
    /// How deeply byte code is running inside calls made by the VM
    /// itself: 1 for the program's own code, more inside a callback.
    pub(crate) run_depth: u32,
    /// The collector is running, and must not start again.
    pub(crate) in_gc: bool,
    /// How deep the VM is in converting or comparing values that hold
    /// values: see [`Vm::nested`].
    pub(crate) nesting: u32,
    /// The most recent regular expression match.
    pub(crate) last_match: crate::string::LastMatch,
    /// Savepoints set aside by a restore that keeps them, to be put back
    /// by the restore that returns (see `RestoreMode`).
    pub(crate) undo_stash: Option<crate::object::UndoStash>,
    /// Patterns given as strings, compiled, by their text. Compiling a
    /// pattern always gives the same result, so one is compiled once.
    pub(crate) pattern_cache: std::collections::HashMap<Vec<u8>, Rc<crate::regex::Pattern>>,
    /// Report each VM error on standard error as it is raised.
    pub(crate) trace_errors: bool,
    /// A stack overflow is being reported: its stack trace is shortened.
    pub(crate) reporting_overflow: bool,
    /// Formats what the program displays into lines for the host.
    pub(crate) console: Formatter,
    pub(crate) io: crate::io::IoState,
    pub(crate) host: Box<dyn Host>,
}

impl Vm {
    // ---- the stack ----

    /// Push a value.
    #[inline]
    pub(crate) fn push(&mut self, value: Value) {
        self.stack.push(value);
    }

    /// Pop a value.
    #[inline]
    pub(crate) fn pop(&mut self) -> Value {
        self.stack.pop().unwrap_or(Value::Nil)
    }

    /// The value `depth` slots below the top of the stack.
    #[inline]
    pub(crate) fn top(&self, depth: usize) -> Value {
        // Only damaged code reaches below the bottom; it finds nil.
        match self.stack.len().checked_sub(1 + depth) {
            Some(at) => self.stack[at],
            None => Value::Nil,
        }
    }

    /// The slot `depth` slots below the top of the stack.
    #[inline]
    pub(crate) fn top_mut(&mut self, depth: usize) -> &mut Value {
        // Only damaged code reaches below the bottom; the stack is made
        // deep enough for it, with nil.
        if self.stack.len() <= depth {
            let missing = depth + 1 - self.stack.len();
            self.stack
                .splice(0..0, std::iter::repeat_n(Value::Nil, missing));
        }
        let index = self.stack.len() - 1 - depth;
        &mut self.stack[index]
    }

    /// Remove `count` values from the top of the stack.
    #[inline]
    pub(crate) fn discard(&mut self, count: usize) {
        let len = self.stack.len().saturating_sub(count);
        self.stack.truncate(len);
    }

    /// Argument `index` (from 0) of a native function called with its
    /// arguments on top of the stack.
    #[inline]
    pub(crate) fn arg(&self, index: usize) -> Value {
        self.top(index)
    }

    /// Stack slots in use, counted the way the reference VM counts them.
    pub(crate) fn stack_depth(&self) -> usize {
        self.stack.len() + self.frames.len() * FRAME_OVERHEAD
    }

    /// Fail with a stack overflow unless `slots` more slots are available.
    pub(crate) fn check_stack(&self, slots: usize) -> VmResult<()> {
        if self.stack_depth() + slots > self.stack_limit {
            Err(VmError::new(code::STACK_OVERFLOW))
        } else {
            Ok(())
        }
    }

    // ---- the current frame ----

    /// `self` in the current frame, or the invalid ID.
    pub(crate) fn self_obj(&self) -> ObjId {
        self.frames
            .last()
            .map_or(ObjId::INVALID, |frame| frame.self_value.obj_id())
    }

    // ---- objects ----

    /// Create a string object.
    pub(crate) fn new_string(&mut self, text: impl Into<Rc<[u8]>>) -> Value {
        Value::Obj(self.alloc_obj(Object::String(text.into())))
    }

    /// Create a string object, unless it is longer than a string can be:
    /// a string's length is 16 bits in every saved form, so the
    /// reference allows no more than 65,535 bytes.
    pub(crate) fn try_new_string(&mut self, text: impl Into<Rc<[u8]>>) -> VmResult<Value> {
        let text = text.into();
        if text.len() > MAX_STRING_LEN {
            return Err(VmError::new(code::STR_TOO_LONG));
        }
        Ok(self.new_string(text))
    }

    /// Create a string object from Rust text, unless it is longer than a
    /// string can be.
    pub(crate) fn try_new_str(&mut self, text: &str) -> VmResult<Value> {
        self.try_new_string(text.as_bytes())
    }

    /// Create a string object from Rust text.
    pub(crate) fn new_str(&mut self, text: &str) -> Value {
        self.new_string(text.as_bytes())
    }

    /// Create an object. Once enough objects have been created since
    /// the collector last ran, it runs first, as in the reference VM:
    /// at the creation, with what is reachable then.
    pub(crate) fn alloc_obj(&mut self, object: Object) -> ObjId {
        prof_scope!(crate::prof::ALLOC);
        if self.objects.gc_due() && !self.in_gc {
            // What the new object refers to is in use, though the object
            // is not in the table yet.
            let mut keep = Vec::new();
            crate::object::references_of(&object, &mut keep);
            self.collect_garbage_keeping(true, keep);
        }
        self.objects.alloc(object)
    }

    /// Create a list object.
    pub(crate) fn new_list(&mut self, items: impl Into<Rc<[Value]>>) -> Value {
        Value::Obj(self.alloc_obj(Object::List(items.into())))
    }

    /// The built-in class an object belongs to.
    pub(crate) fn kind_of(&self, id: ObjId) -> Option<MetaKind> {
        Some(match self.objects.get(id)? {
            Object::Program(_) => MetaKind::ProgramObject,
            Object::IntClassMod(_) => MetaKind::IntClassMod,
            Object::String(_) => MetaKind::String,
            Object::List(_) => MetaKind::List,
            Object::Vector(_) => MetaKind::Vector,
            Object::AnonFn(_) => MetaKind::Function,
            Object::IndexedIter { .. } => MetaKind::IndexedIterator,
            Object::Map(table) if table.weak => MetaKind::WeakMap,
            Object::Map(_) => MetaKind::Map,
            Object::MapIter { .. } => MetaKind::MapIterator,
            Object::Pattern { .. } => MetaKind::Regex,
            Object::StringBuilder(_) => MetaKind::StringBuilder,
            Object::CompiledCode(_) => MetaKind::CompiledCode,
            Object::FrameRef(_) => MetaKind::StackFrameRef,
            Object::FrameDesc(_) => MetaKind::StackFrame,
            Object::Decimal(_) => MetaKind::Decimal,
            Object::ByteArray(_) => MetaKind::ByteArray,
            Object::StrComp(_) => MetaKind::StringComparator,
            Object::Dict(_) => MetaKind::Dictionary,
            Object::GrammarRule(_) => MetaKind::GrammarRule,
            Object::CharSet { .. } => MetaKind::CharacterSet,
            Object::FileName(_) => MetaKind::FileName,
            Object::Date { .. } => MetaKind::Date,
            Object::TimeZone(_) => MetaKind::TimeZone,
            Object::File(_) => MetaKind::File,
            Object::TempFile(_) => MetaKind::TemporaryFile,
            Object::Class(_) => MetaKind::BuiltinClass,
            Object::Opaque { meta_index, .. } => self.meta.get(*meta_index as usize)?.kind,
        })
    }

    // ---- strings and lists ----

    /// The constant string at a pool offset.
    pub(crate) fn const_str(&self, offset: u32) -> StrRef {
        let at = offset as usize;
        match self.consts.get(at..at + 2) {
            Some(b) => {
                let len = u32::from(u16::from_le_bytes([b[0], b[1]]));
                let available = self.consts.len() - at - 2;
                StrRef::Const(self.consts.clone(), offset + 2, len.min(available as u32))
            }
            None => StrRef::Const(self.consts.clone(), 0, 0),
        }
    }

    /// The constant list at a pool offset.
    pub(crate) fn const_list(&self, offset: u32) -> ListRef {
        let at = offset as usize;
        match self.consts.get(at..at + 2) {
            Some(b) => {
                let len = u32::from(u16::from_le_bytes([b[0], b[1]]));
                let available = (self.consts.len() - at - 2) / Value::HOLDER_SIZE;
                ListRef::Const(self.consts.clone(), offset + 2, len.min(available as u32))
            }
            None => ListRef::Const(self.consts.clone(), 0, 0),
        }
    }

    /// The text of a value, if it is a string constant or string object.
    pub(crate) fn as_str(&self, value: Value) -> Option<StrRef> {
        match value {
            Value::SString(offset) => Some(self.const_str(offset)),
            Value::Obj(id) => match self.objects.get(id)? {
                Object::String(text) => Some(StrRef::Obj(text.clone())),
                _ => None,
            },
            _ => None,
        }
    }

    /// The elements of a value, if it is a list constant or list object.
    pub(crate) fn as_list(&self, value: Value) -> Option<ListRef> {
        match value {
            Value::List(offset) => Some(self.const_list(offset)),
            Value::Obj(id) => match self.objects.get(id)? {
                Object::List(items) => Some(ListRef::Obj(items.clone())),
                _ => None,
            },
            _ => None,
        }
    }

    /// The text of a value; anything but a string is an error.
    pub(crate) fn require_str(&self, value: Value) -> VmResult<StrRef> {
        self.as_str(value)
            .ok_or_else(|| VmError::new(code::STRING_VAL_REQD))
    }

    // ---- argument helpers for native code ----

    /// An integer argument.
    pub(crate) fn int_arg(&self, value: Value) -> VmResult<i32> {
        match value {
            Value::Int(n) => Ok(n),
            _ => Err(VmError::new(code::INT_VAL_REQD)),
        }
    }

    /// A property ID argument.
    pub(crate) fn prop_arg(&self, value: Value) -> VmResult<PropId> {
        match value {
            Value::Prop(prop) => Ok(prop),
            _ => Err(VmError::new(code::PROPPTR_VAL_REQD)),
        }
    }

    /// An object argument.
    pub(crate) fn obj_arg(&self, value: Value) -> VmResult<ObjId> {
        match value {
            Value::Obj(id) => Ok(id),
            _ => Err(VmError::new(code::OBJ_VAL_REQD)),
        }
    }

    /// Report each VM error on standard error as it is raised, with the
    /// address of the instruction that caused it. This is a debugging aid
    /// for work on the VM itself.
    pub fn set_error_trace(&mut self, on: bool) {
        self.trace_errors = on;
    }

    /// Write out any text the console is holding for an unfinished line.
    /// Call this when the program ends.
    pub fn flush_output(&mut self) {
        self.flush_console(Newline::None);
    }
}