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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
//! Property lookup and assignment for every kind of object, and the
//! methods all objects share.

use crate::error::{VmError, VmResult, code};
use crate::meta::MetaKind;
use crate::object::Object;
use crate::program_object::{self, ProgramObject};
use crate::value::{INVALID_PROP, NativeDesc, ObjId, PropId, Value, type_code};
use crate::vm::{NativeCall, Return, Vm};

/// `propDefined` flag: defined at all.
const PROPDEF_ANY: i32 = 1;
/// `propDefined` flag: defined by the object itself.
const PROPDEF_DIRECTLY: i32 = 2;
/// `propDefined` flag: inherited from a superclass.
const PROPDEF_INHERITS: i32 = 3;
/// `propDefined` flag: return the defining object.
const PROPDEF_GET_CLASS: i32 = 4;

/// A property found by a lookup: its value and the object defining it.
pub(crate) type Found = Option<(Value, ObjId)>;

impl Vm {
    /// Run a method the VM implements, under the convention all such
    /// methods share.
    ///
    /// A lookup that does not want the method called (`argc` is `None`)
    /// gets the method's interface back instead. Otherwise the argument
    /// count is checked, the method runs with its arguments on top of the
    /// stack, and the arguments are removed afterward.
    pub(crate) fn native_method(
        &mut self,
        argc: Option<&mut u32>,
        desc: NativeDesc,
        method: impl FnOnce(&mut Vm, u32) -> VmResult<Value>,
    ) -> VmResult<Value> {
        let Some(argc) = argc else {
            return Ok(Value::NativeCode(desc));
        };
        let count = *argc;
        if !desc.accepts(count) {
            return Err(VmError::new(code::WRONG_NUM_OF_ARGS));
        }
        *argc = 0;
        let base = self.stack.len() - count as usize;
        let result = method(self, count)?;
        self.stack.truncate(base);
        Ok(result)
    }

    /// Look up a property on an object. See [`Vm::native_method`] for the
    /// meaning of `argc`.
    pub(crate) fn obj_get_prop(
        &mut self,
        id: ObjId,
        prop: PropId,
        mut argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        prof_scope!(crate::prof::GET_PROP);
        let kind = match self.objects.get(id) {
            None => return Err(VmError::new(code::NIL_DEREF)),
            Some(Object::Program(_)) => MetaKind::ProgramObject,
            Some(Object::IntClassMod(_)) => {
                return Ok(program_object::find_prop(&self.objects, id, prop));
            }
            Some(Object::Class(_)) => return self.class_get_prop(id, prop, argc),
            Some(_) => self.kind_of(id).unwrap_or(MetaKind::Root),
        };
        if kind == MetaKind::ProgramObject
            && let Some(found) = program_object::find_prop(&self.objects, id, prop)
        {
            return Ok(Some(found));
        }
        if let Some(found) =
            self.builtin_get_prop(kind, Value::Obj(id), prop, argc.as_deref_mut())?
        {
            return Ok(Some(found));
        }
        self.root_get_prop(id, prop, argc)
    }

    /// Look for a method among those of a built-in class and the
    /// built-in classes it derives from, short of the root class.
    fn builtin_get_prop(
        &mut self,
        kind: MetaKind,
        self_value: Value,
        prop: PropId,
        mut argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let mut kind = kind;
        while kind != MetaKind::Root {
            let index = self.meta.method_index(kind, prop);
            if index != 0
                && let Some(value) =
                    self.builtin_method(kind, self_value, index, argc.as_deref_mut())?
            {
                return Ok(Some((value, self.meta.class_obj(kind))));
            }
            match kind.parent() {
                Some(parent) => kind = parent,
                None => break,
            }
        }
        Ok(None)
    }

    /// Call method `index` of built-in class `kind` on a value. Returns
    /// `None` if the class has no such method.
    fn builtin_method(
        &mut self,
        kind: MetaKind,
        self_value: Value,
        index: u16,
        argc: Option<&mut u32>,
    ) -> VmResult<Option<Value>> {
        prof_scope!(crate::prof::NATIVE + ((kind as u32) << 8) + u32::from(index));
        match kind {
            MetaKind::ProgramObject => self.program_method(self_value.obj_id(), index, argc),
            MetaKind::String => self.string_method(self_value, index, argc),
            MetaKind::List => self.list_method(self_value, index, argc),
            MetaKind::Vector => self.vector_method(self_value.obj_id(), index, argc),
            MetaKind::Collection => self.collection_method(self_value, index, argc),
            MetaKind::Iterator => self.iterator_method(self_value.obj_id(), index, argc),
            // These add no methods of their own to the classes they
            // derive from.
            MetaKind::Function
            | MetaKind::IndexedIterator
            | MetaKind::MapIterator
            | MetaKind::WeakMap => Ok(None),
            MetaKind::Map => self.table_method(self_value.obj_id(), index, argc),
            MetaKind::Regex => self.pattern_method(self_value.obj_id(), index, argc),
            MetaKind::StringBuilder => self.string_builder_method(self_value.obj_id(), index, argc),
            MetaKind::CompiledCode => self.compiled_code_method(self_value.obj_id(), index, argc),
            MetaKind::StackFrame => self.stack_frame_method(self_value.obj_id(), index, argc),
            MetaKind::Decimal => self.big_method(self_value.obj_id(), index, argc),
            MetaKind::ByteArray => self.bytes_method(self_value.obj_id(), index, argc),
            MetaKind::CharacterSet => self.charset_method(self_value.obj_id(), index, argc),
            MetaKind::StringComparator => self.strcomp_method(self_value.obj_id(), index, argc),
            MetaKind::Dictionary => self.dict_method(self_value.obj_id(), index, argc),
            MetaKind::GrammarRule => self.gram_method(self_value.obj_id(), index, argc),
            MetaKind::Date => self.date_method(Some(self_value.obj_id()), index, argc),
            MetaKind::TimeZone => self.time_zone_method(self_value.obj_id(), index, argc),
            MetaKind::File => self.file_method(Some(self_value.obj_id()), index, argc),
            MetaKind::FileName => self.file_name_method(Some(self_value.obj_id()), index, argc),
            MetaKind::TemporaryFile => self.temp_file_method(self_value.obj_id(), index, argc),
            other => Err(VmError::unimplemented(format!(
                "method {index} of built-in class {}",
                other.name()
            ))),
        }
    }

    /// Look up a property on a constant string or list.
    pub(crate) fn const_get_prop(
        &mut self,
        constant: Value,
        prop: PropId,
        mut argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let (kind, stand_in) = match constant {
            Value::SString(_) => (MetaKind::String, self.predef.const_str_obj),
            _ => (MetaKind::List, self.predef.const_lst_obj),
        };
        if let Some(found) = self.builtin_get_prop(kind, constant, prop, argc.as_deref_mut())? {
            return Ok(Some(found));
        }
        // Everything else is the same for every string (or list), so a
        // stand-in object answers for the constant.
        self.root_get_prop(stand_in, prop, argc)
    }

    /// Look up one of the methods every object has, then the methods the
    /// program adds to built-in classes with `modify`.
    fn root_get_prop(
        &mut self,
        id: ObjId,
        prop: PropId,
        argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let index = self.meta.method_index(MetaKind::Root, prop);
        if index == 0 {
            return self.find_modifier_prop(id, prop, id, ObjId::INVALID, argc);
        }
        let mut source = self.meta.class_obj(MetaKind::Root);
        let value = self.root_method(id, index, argc, &mut source)?;
        Ok(value.map(|value| (value, source)))
    }

    /// Search the program's modifications to an object's built-in class,
    /// and to the classes that one derives from.
    fn find_modifier_prop(
        &mut self,
        id: ObjId,
        prop: PropId,
        _target: ObjId,
        definer: ObjId,
        _argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let mut definer = definer;
        let mut found_definer = false;
        let mut kind = self.kind_of(id);
        while let Some(k) = kind {
            let Some(class_obj) = self.meta.entry(k).map(|entry| entry.class_obj) else {
                break;
            };
            if !class_obj.is_valid() {
                break;
            }
            let modifier = match self.objects.get(class_obj) {
                Some(Object::Class(class)) => class.modifier,
                _ => ObjId::INVALID,
            };
            let mut skip = false;
            if modifier.is_valid() && definer.is_valid() && !found_definer {
                if modifier == definer || self.is_instance_of(modifier, definer) {
                    found_definer = true;
                } else {
                    skip = true;
                }
            }
            if !skip
                && modifier.is_valid()
                && let Some(found) =
                    program_object::find_inherited_prop(&self.objects, modifier, definer, prop)
            {
                return Ok(Some(found));
            }
            kind = k.parent();
            if found_definer {
                definer = ObjId::INVALID;
            }
        }
        Ok(None)
    }

    /// Look up the definition of a property that the current method's
    /// definition overrides, for `inherited`.
    pub(crate) fn obj_inh_prop(
        &mut self,
        id: ObjId,
        prop: PropId,
        target: ObjId,
        definer: ObjId,
        mut argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let mut definer = definer;
        match self.objects.get(id) {
            None => return Err(VmError::new(code::NIL_DEREF)),
            Some(Object::IntClassMod(_)) => {
                return Ok(program_object::find_inherited_prop(
                    &self.objects,
                    target,
                    definer,
                    prop,
                ));
            }
            Some(Object::Program(_)) => {
                let outside_program = matches!(
                    self.objects.get(definer),
                    Some(Object::IntClassMod(_) | Object::Class(_))
                );
                if !outside_program {
                    if let Some(found) =
                        program_object::find_inherited_prop(&self.objects, target, definer, prop)
                    {
                        return Ok(Some(found));
                    }
                    let index = self.meta.method_index(MetaKind::ProgramObject, prop);
                    if index != 0
                        && let Some(value) = self.program_method(id, index, argc.as_deref_mut())?
                    {
                        return Ok(Some((value, self.meta.class_obj(MetaKind::ProgramObject))));
                    }
                    definer = ObjId::INVALID;
                }
            }
            Some(_) => {}
        }
        let index = self.meta.method_index(MetaKind::Root, prop);
        if index != 0 {
            let mut source = self.meta.class_obj(MetaKind::Root);
            let value = self.root_method(id, index, argc, &mut source)?;
            return Ok(value.map(|value| (value, source)));
        }
        self.find_modifier_prop(id, prop, target, definer, argc)
    }

    /// Look up a property on a built-in class object: the class's static
    /// methods, then the program's modifications to it.
    fn class_get_prop(
        &mut self,
        id: ObjId,
        prop: PropId,
        mut argc: Option<&mut u32>,
    ) -> VmResult<Found> {
        let kind = self.class_kind(id);
        if let Some(kind) = kind
            && let Some(value) = self.static_method(kind, prop, argc.as_deref_mut())?
        {
            return Ok(Some((value, id)));
        }
        // The modifier of this class, then of each class it derives from.
        let mut class_obj = id;
        let mut class_kind = kind;
        while class_obj.is_valid() {
            if let Some(Object::Class(class)) = self.objects.get(class_obj) {
                let modifier = class.modifier;
                if modifier.is_valid()
                    && let Some(found) = program_object::find_prop(&self.objects, modifier, prop)
                {
                    return Ok(Some(found));
                }
            }
            class_kind = class_kind.and_then(MetaKind::parent);
            class_obj = class_kind.map_or(ObjId::INVALID, |k| self.meta.class_obj(k));
        }
        self.root_get_prop(id, prop, argc)
    }

    /// The built-in class that a built-in class object represents.
    pub(crate) fn class_kind(&self, id: ObjId) -> Option<MetaKind> {
        match self.objects.get(id)? {
            Object::Class(class) => Some(self.meta.get(class.meta_index as usize)?.kind),
            _ => None,
        }
    }

    /// Call a static method: one invoked on a built-in class itself.
    fn static_method(
        &mut self,
        kind: MetaKind,
        prop: PropId,
        argc: Option<&mut u32>,
    ) -> VmResult<Option<Value>> {
        let index = self.meta.method_index(kind, prop);
        match (kind, index) {
            // ProgramObject.createInstance() and createTransientInstance()
            (MetaKind::ProgramObject, 1 | 3) => self
                .native_method(argc, NativeDesc::fixed(0), |vm, _| {
                    let (id, _) = vm.create_program_object(0, index == 3, None)?;
                    Ok(Value::Obj(id))
                })
                .map(Some),
            // ProgramObject.createInstanceOf() and createTransientInstanceOf()
            (MetaKind::ProgramObject, 4 | 5) => self
                .native_method(argc, NativeDesc::variable(0), |vm, argc| {
                    vm.create_program_object_multi(argc, index == 5)
                        .map(Value::Obj)
                })
                .map(Some),
            (MetaKind::List, index) if index != 0 => self.list_static_method(index, argc),
            (MetaKind::Vector, index) if index != 0 => self.vector_static_method(index, argc),
            (MetaKind::Decimal, index) if index != 0 => self.big_static_method(index, argc),
            (MetaKind::ByteArray, index) if index != 0 => self.bytes_static_method(index, argc),
            (MetaKind::String, index) if index != 0 => self.string_static_method(index, argc),
            (MetaKind::Date, index) if index != 0 => self.date_method(None, index, argc),
            (MetaKind::File, index) if index != 0 => self.file_method(None, index, argc),
            (MetaKind::FileName, index) if index != 0 => self.file_name_method(None, index, argc),
            // BuiltinClass.isBuiltinClass(val)
            (MetaKind::BuiltinClass, 1) => self
                .native_method(argc, NativeDesc::fixed(1), |vm, _| {
                    let is_class = match vm.arg(0) {
                        Value::Obj(id) => matches!(vm.objects.get(id), Some(Object::Class(_))),
                        _ => false,
                    };
                    Ok(Value::logical(is_class))
                })
                .map(Some),
            // A method of a class that is not implemented yet might be a
            // static one. Say so rather than quietly finding nothing.
            (other, index)
                if index != 0
                    && argc.is_some()
                    && !matches!(
                        other,
                        MetaKind::Root
                            | MetaKind::ProgramObject
                            | MetaKind::IntClassMod
                            | MetaKind::BuiltinClass
                            | MetaKind::String
                            | MetaKind::List
                            | MetaKind::Vector
                            | MetaKind::Collection
                            | MetaKind::Iterator
                            | MetaKind::IndexedIterator
                            | MetaKind::Function
                            | MetaKind::Map
                            | MetaKind::WeakMap
                            | MetaKind::MapIterator
                            | MetaKind::Regex
                            | MetaKind::StringBuilder
                            | MetaKind::Decimal
                            | MetaKind::ByteArray
                            | MetaKind::CharacterSet
                            | MetaKind::StringComparator
                            | MetaKind::Dictionary
                            | MetaKind::GrammarRule
                            | MetaKind::File
                            | MetaKind::Date
                            | MetaKind::TimeZone
                            | MetaKind::FileName
                            | MetaKind::TemporaryFile
                    ) =>
            {
                Err(VmError::unimplemented(format!(
                    "method {index} of built-in class {}",
                    other.name()
                )))
            }
            _ => Ok(None),
        }
    }

    // ---- assignment ----

    /// Set a property of an object.
    pub(crate) fn set_prop(&mut self, id: ObjId, prop: PropId, value: Value) -> VmResult<()> {
        prof_scope!(crate::prof::SET_PROP);
        match self.objects.get_mut_for_prop(id, prop) {
            None => Err(VmError::new(code::NIL_DEREF)),
            Some(Object::Program(object) | Object::IntClassMod(object)) => {
                object.set(prop, value);
                Ok(())
            }
            Some(Object::Class(class)) => {
                // Setting a property of a built-in class sets it on the
                // program's modifier object for the class.
                let modifier = class.modifier;
                if modifier.is_valid() {
                    self.set_prop(modifier, prop, value)
                } else {
                    Err(VmError::new(code::INVALID_SETPROP))
                }
            }
            Some(_) => Err(VmError::new(code::INVALID_SETPROP)),
        }
    }

    // ---- class relationships ----

    /// Whether an object is an instance of a class, directly or through
    /// inheritance.
    pub(crate) fn is_instance_of(&self, id: ObjId, class: ObjId) -> bool {
        match self.objects.get(id) {
            None => false,
            Some(Object::Program(_) | Object::IntClassMod(_)) => {
                program_object::inherits_from(&self.objects, id, class)
                    || self.is_builtin_instance(id, class)
            }
            Some(Object::Class(_)) => {
                // A built-in class is an instance of the classes it
                // derives from.
                let mut kind = self.class_kind(id).and_then(MetaKind::parent);
                while let Some(k) = kind {
                    if self.meta.class_obj(k) == class && class.is_valid() {
                        return true;
                    }
                    kind = k.parent();
                }
                false
            }
            Some(_) => self.is_builtin_instance(id, class),
        }
    }

    /// Whether `class` is the built-in class of an object, or one that
    /// class derives from.
    fn is_builtin_instance(&self, id: ObjId, class: ObjId) -> bool {
        if !matches!(self.objects.get(class), Some(Object::Class(_))) {
            return false;
        }
        let Some(kind) = self.kind_of(id) else {
            return false;
        };
        let class_obj = self.meta.class_obj(kind);
        class_obj == class || (class_obj.is_valid() && self.is_instance_of(class_obj, class))
    }

    /// An object's direct superclasses, as programs see them.
    pub(crate) fn superclasses_of(&self, id: ObjId) -> Vec<ObjId> {
        match self.objects.get(id) {
            Some(Object::Program(object) | Object::IntClassMod(object))
                if !object.superclasses.is_empty() =>
            {
                object.superclasses.clone()
            }
            Some(Object::Class(_)) => match self.class_kind(id).and_then(MetaKind::parent) {
                Some(parent) => vec![self.meta.class_obj(parent)],
                None => Vec::new(),
            },
            // Anything else has one superclass: its built-in class.
            Some(_) => vec![
                self.kind_of(id)
                    .map_or(ObjId::INVALID, |kind| self.meta.class_obj(kind)),
            ],
            None => Vec::new(),
        }
    }

    /// Whether an object is a class.
    fn is_class_object(&self, id: ObjId) -> bool {
        match self.objects.get(id) {
            Some(Object::Program(object) | Object::IntClassMod(object)) => object.is_class(),
            Some(Object::Class(_)) => true,
            _ => false,
        }
    }

    /// The properties an object defines directly, as `getPropList` reports
    /// them.
    fn prop_list(&self, id: ObjId) -> Vec<Value> {
        let own = |object: &ProgramObject| -> Vec<Value> {
            object
                .props()
                .iter()
                .map(|entry| Value::Prop(entry.prop))
                .collect()
        };
        match self.objects.get(id) {
            Some(Object::Program(object)) => own(object),
            Some(Object::IntClassMod(object)) => {
                // A modifier's list includes the modifiers it replaced.
                let mut props = own(object);
                if let Some(&base) = object.superclasses.first()
                    && matches!(self.objects.get(base), Some(Object::IntClassMod(_)))
                {
                    props.extend(self.prop_list(base));
                }
                props
            }
            Some(Object::Class(class)) => {
                let mut props: Vec<Value> = self
                    .meta
                    .get(class.meta_index as usize)
                    .map(|entry| {
                        entry
                            .method_props
                            .iter()
                            .filter(|&&prop| prop != INVALID_PROP)
                            .map(|&prop| Value::Prop(prop))
                            .collect()
                    })
                    .unwrap_or_default();
                if class.modifier.is_valid() {
                    props.extend(self.prop_list(class.modifier));
                }
                props
            }
            _ => Vec::new(),
        }
    }

    /// The calling interface of a property: required arguments, optional
    /// arguments, and whether more are accepted. `None` if the property is
    /// not defined.
    pub(crate) fn prop_interface(
        &mut self,
        id: ObjId,
        prop: PropId,
    ) -> VmResult<Option<(i32, i32, bool)>> {
        let Some((value, _)) = self.obj_get_prop(id, prop, None)? else {
            return Ok(None);
        };
        Ok(Some(self.value_interface(value).unwrap_or((0, 0, false))))
    }

    /// The calling interface of a value that can be invoked.
    pub(crate) fn value_interface(&self, value: Value) -> Option<(i32, i32, bool)> {
        let func = match value {
            Value::CodeOfs(func) | Value::FuncPtr(func) => func,
            Value::Obj(id) | Value::ObjX(id) => self.invoker(id)?,
            Value::BifPtr { set, index } | Value::BifPtrX { set, index } => {
                let desc = self.bif_desc(set, index)?;
                return Some((desc.min_args.into(), desc.opt_args.into(), desc.varargs));
            }
            Value::NativeCode(desc) => {
                return Some((desc.min_args.into(), desc.opt_args.into(), desc.varargs));
            }
            _ => return None,
        };
        let header = self.function_header(func).ok()?;
        Some((
            header.min_args as i32,
            header.opt_args as i32,
            header.varargs,
        ))
    }

    // ---- the methods every object has ----

    /// Call one of the methods of the root built-in class, `Object`.
    fn root_method(
        &mut self,
        id: ObjId,
        index: u16,
        argc: Option<&mut u32>,
        source: &mut ObjId,
    ) -> VmResult<Option<Value>> {
        let _ = source;
        let value = match index {
            // ofKind(cls)
            1 => self.native_method(argc, NativeDesc::fixed(1), |vm, _| {
                let class = vm.obj_arg(vm.arg(0))?;
                Ok(Value::logical(class == id || vm.is_instance_of(id, class)))
            })?,
            // getSuperclassList()
            2 => self.native_method(argc, NativeDesc::fixed(0), |vm, _| {
                let list: Vec<Value> = vm
                    .superclasses_of(id)
                    .into_iter()
                    .map(Value::obj_or_nil)
                    .collect();
                Ok(vm.new_list(list))
            })?,
            // propDefined(prop, flags?)
            3 => self.native_method(argc, NativeDesc::optional(1, 1), |vm, argc| {
                let prop = vm.prop_arg(vm.arg(0))?;
                let flags = if argc >= 2 {
                    vm.int_arg(vm.arg(1))?
                } else {
                    PROPDEF_ANY
                };
                let found = vm.obj_get_prop(id, prop, None)?;
                let source = found.map(|(_, mut source)| {
                    // A method added with `modify` is reported as defined
                    // by the built-in class it was added to.
                    if matches!(vm.objects.get(source), Some(Object::IntClassMod(_))) {
                        source = vm.class_for_modifier(id, source);
                    }
                    source
                });
                Ok(match flags {
                    PROPDEF_ANY => Value::logical(source.is_some()),
                    PROPDEF_DIRECTLY => Value::logical(source == Some(id)),
                    PROPDEF_INHERITS => Value::logical(source.is_some_and(|s| s != id)),
                    PROPDEF_GET_CLASS => match source {
                        Some(source) if source.is_valid() => Value::Obj(source),
                        Some(_) => Value::Obj(id),
                        None => Value::Nil,
                    },
                    _ => return Err(VmError::new(code::BAD_VAL_BIF)),
                })
            })?,
            // propType(prop)
            4 => self.native_method(argc, NativeDesc::fixed(1), |vm, _| {
                let prop = vm.prop_arg(vm.arg(0))?;
                let Some((value, _)) = vm.obj_get_prop(id, prop, None)? else {
                    return Ok(Value::Nil);
                };
                let code = match value {
                    // A method bound to an object that can be called is
                    // code: an anonymous function or a CompiledCode.
                    Value::ObjX(inner) => match vm.objects.get(inner) {
                        Some(Object::String(_)) => type_code::DSTRING,
                        _ if vm.invoker(inner).is_some() => type_code::CODEOFS,
                        _ => type_code::OBJ,
                    },
                    Value::BifPtrX { .. } => type_code::BIFPTR,
                    other => vm.datatype(other),
                };
                Ok(Value::Int(code))
            })?,
            // getPropList()
            5 => self.native_method(argc, NativeDesc::fixed(0), |vm, _| {
                let props = vm.prop_list(id);
                Ok(vm.new_list(props))
            })?,
            // getPropParams(prop)
            6 => self.native_method(argc, NativeDesc::fixed(1), |vm, _| {
                let prop = vm.prop_arg(vm.arg(0))?;
                let (min, opt, varargs) = vm.prop_interface(id, prop)?.unwrap_or((0, 0, false));
                Ok(vm.new_list(vec![
                    Value::Int(min),
                    Value::Int(opt),
                    Value::logical(varargs),
                ]))
            })?,
            // isClass()
            7 => self.native_method(argc, NativeDesc::fixed(0), |vm, _| {
                Ok(Value::logical(vm.is_class_object(id)))
            })?,
            // propInherited(prop, origTargetObj, definingObj, flags?)
            8 => self.native_method(argc, NativeDesc::optional(3, 1), |vm, argc| {
                let prop = vm.prop_arg(vm.arg(0))?;
                let target = vm.obj_arg(vm.arg(1))?;
                let definer = vm.obj_arg(vm.arg(2))?;
                let flags = if argc >= 4 {
                    vm.int_arg(vm.arg(3))?
                } else {
                    PROPDEF_ANY
                };
                let found = vm.obj_inh_prop(id, prop, target, definer, None)?;
                Ok(match flags {
                    PROPDEF_ANY => Value::logical(found.is_some()),
                    PROPDEF_GET_CLASS => match found {
                        Some((_, source)) if source.is_valid() => Value::Obj(source),
                        Some(_) => Value::Obj(id),
                        None => Value::Nil,
                    },
                    _ => return Err(VmError::new(code::BAD_VAL_BIF)),
                })
            })?,
            // isTransient()
            9 => self.native_method(argc, NativeDesc::fixed(0), |vm, _| {
                Ok(Value::logical(vm.objects.is_transient(id)))
            })?,
            _ => return Ok(None),
        };
        Ok(Some(value))
    }

    /// The built-in class, among those an object belongs to, whose
    /// modifier chain includes a given modifier object.
    fn class_for_modifier(&self, id: ObjId, modifier: ObjId) -> ObjId {
        let mut kind = self.kind_of(id);
        while let Some(k) = kind {
            let class_obj = self.meta.class_obj(k);
            if let Some(Object::Class(class)) = self.objects.get(class_obj)
                && class.modifier.is_valid()
                && (class.modifier == modifier || self.is_instance_of(class.modifier, modifier))
            {
                return class_obj;
            }
            kind = k.parent();
        }
        ObjId::INVALID
    }

    /// The type code of a value as `dataType()` reports it: string and
    /// list objects report as strings and lists.
    pub(crate) fn datatype(&self, value: Value) -> i32 {
        match value {
            Value::Obj(id) => match self.objects.get(id) {
                Some(Object::String(_)) => type_code::SSTRING,
                Some(Object::List(_)) => type_code::LIST,
                _ => type_code::OBJ,
            },
            other => other.type_code(),
        }
    }

    // ---- program-defined objects ----

    /// Create an instance of a program-defined class from native code and
    /// run its constructor. The constructor arguments are on the stack.
    /// The new object is left in register 0.
    pub(crate) fn create_instance(&mut self, class: ObjId, argc: u32) -> VmResult<()> {
        self.push(Value::Obj(class));
        let (id, _) = self.create_program_object(argc + 1, false, None)?;
        // The constructor leaves the object in register 0 by returning
        // `self`; an object with no constructor needs it set.
        let _ = id;
        Ok(())
    }

    /// Create a program-defined object. The superclass, if `argc` is not
    /// zero, is on top of the stack, with the constructor arguments under
    /// it.
    ///
    /// With a `return_pc`, the constructor's frame is pushed and the
    /// address of its first instruction returned for the interpreter loop
    /// to continue at. Without one, the constructor runs to completion.
    pub(crate) fn create_program_object(
        &mut self,
        argc: u32,
        transient: bool,
        return_pc: Option<u32>,
    ) -> VmResult<(ObjId, u32)> {
        let mut argc = argc;
        let mut superclasses = Vec::new();
        if argc != 0 {
            match self.pop() {
                Value::Nil => {}
                Value::Obj(class) => {
                    if self.objects.is_transient(class) {
                        return Err(VmError::new(code::BAD_DYNAMIC_NEW));
                    }
                    superclasses.push(class);
                }
                _ => return Err(VmError::new(code::OBJ_VAL_REQD_SC)),
            }
            argc -= 1;
        }
        let id = self.alloc_obj(Object::Program(ProgramObject::new(superclasses)));
        if transient {
            self.objects.set_transient(id);
        }
        let construct = self.predef.construct;
        let next_pc = match self.obj_get_prop(id, construct, None)? {
            Some((_, source)) => {
                let (ret, pc, native) = match return_pc {
                    Some(pc) => (Return::Normal, pc, None),
                    None => {
                        let mut native = NativeCall::named("ProgramObject.construct");
                        native.caller_pc = self.pc;
                        (Return::Native, 0, Some(Box::new(native)))
                    }
                };
                self.eval_prop(
                    Value::Obj(source),
                    construct,
                    Value::Obj(id),
                    argc,
                    ret,
                    pc,
                    native,
                )?
            }
            None => {
                if argc != 0 {
                    return Err(VmError::new(code::WRONG_NUM_OF_ARGS));
                }
                self.r0 = Value::Obj(id);
                return_pc.unwrap_or(0)
            }
        };
        Ok((id, next_pc))
    }

    /// Create an object with several superclasses, for `createInstanceOf`.
    /// Each argument is a class, or a list of a class followed by the
    /// arguments for that class's constructor.
    fn create_program_object_multi(&mut self, argc: u32, transient: bool) -> VmResult<ObjId> {
        let args: Vec<Value> = (0..argc as usize).map(|i| self.arg(i)).collect();
        let mut superclasses = Vec::new();
        for &arg in &args {
            let class = match self.as_list(arg) {
                Some(list) if list.len() >= 1 => list.get(0),
                Some(_) => return Err(VmError::new(code::INDEX_OUT_OF_RANGE)),
                None => arg,
            };
            let Value::Obj(class) = class else {
                return Err(VmError::new(code::BAD_TYPE_BIF));
            };
            if !matches!(self.objects.get(class), Some(Object::Program(_))) {
                return Err(VmError::new(code::BAD_TYPE_BIF));
            }
            if self.objects.is_transient(class) {
                return Err(VmError::new(code::BAD_DYNAMIC_NEW));
            }
            superclasses.push(class);
        }
        let id = self.alloc_obj(Object::Program(ProgramObject::new(superclasses.clone())));
        if transient {
            self.objects.set_transient(id);
        }
        // Keep the new object reachable while the constructors run.
        self.push(Value::Obj(id));
        for (&arg, &class) in args.iter().zip(&superclasses) {
            let Some(list) = self.as_list(arg) else {
                continue;
            };
            self.check_stack(list.len())?;
            for index in (1..list.len()).rev() {
                self.push(list.get(index));
            }
            let mut native = NativeCall::named("ProgramObject.constructMulti");
            native.caller_pc = 0;
            self.eval_prop(
                Value::Obj(class),
                self.predef.construct,
                Value::Obj(id),
                list.len() as u32 - 1,
                Return::Native,
                0,
                Some(Box::new(native)),
            )?;
        }
        self.pop();
        Ok(id)
    }

    /// Call one of the methods of the `ProgramObject` built-in class.
    fn program_method(
        &mut self,
        id: ObjId,
        index: u16,
        argc: Option<&mut u32>,
    ) -> VmResult<Option<Value>> {
        let value = match index {
            // createInstance(...) and createTransientInstance(...)
            1 | 3 => self.native_method(argc, NativeDesc::variable(0), |vm, argc| {
                // The constructor consumes the arguments.
                vm.push(Value::Obj(id));
                let (new, _) = vm.create_program_object(argc + 1, index == 3, None)?;
                Ok(Value::Obj(new))
            })?,
            // createClone()
            2 => self.native_method(argc, NativeDesc::fixed(0), |vm, _| {
                let Some(original) = vm.objects.program_object(id) else {
                    return Err(VmError::new(code::OBJ_VAL_REQD));
                };
                let mut clone =
                    ProgramObject::with_room(original.superclasses.clone(), original.props().len());
                for entry in original.props() {
                    clone.set(entry.prop, entry.value);
                }
                Ok(Value::Obj(vm.alloc_obj(Object::Program(clone))))
            })?,
            // createInstanceOf(...) and createTransientInstanceOf(...)
            4 | 5 => self.native_method(argc, NativeDesc::variable(0), |vm, argc| {
                vm.create_program_object_multi(argc, index == 5)
                    .map(Value::Obj)
            })?,
            // setSuperclassList(lst)
            6 => self.native_method(argc, NativeDesc::fixed(1), |vm, _| {
                vm.set_superclass_list(id, vm.arg(0))?;
                Ok(Value::Nil)
            })?,
            // getMethod(prop)
            7 => self.native_method(argc, NativeDesc::fixed(1), |vm, _| {
                let prop = vm.prop_arg(vm.arg(0))?;
                Ok(match program_object::find_prop(&vm.objects, id, prop) {
                    Some((Value::CodeOfs(func), _)) => Value::FuncPtr(func),
                    Some((Value::DString(offset), _)) => Value::SString(offset),
                    Some((Value::ObjX(object), _)) => Value::Obj(object),
                    Some((Value::BifPtrX { set, index }, _)) => Value::BifPtr { set, index },
                    _ => Value::Nil,
                })
            })?,
            // setMethod(prop, method)
            8 => self.native_method(argc, NativeDesc::fixed(2), |vm, _| {
                let prop = vm.prop_arg(vm.arg(0))?;
                let method = match vm.arg(1) {
                    Value::FuncPtr(func) => Value::CodeOfs(func),
                    Value::Obj(object)
                        if matches!(vm.objects.get(object), Some(Object::String(_)))
                            || vm.invoker(object).is_some() =>
                    {
                        Value::ObjX(object)
                    }
                    Value::BifPtr { set, index } => Value::BifPtrX { set, index },
                    Value::SString(offset) => Value::DString(offset),
                    _ => return Err(VmError::new(code::BAD_TYPE_BIF)),
                };
                vm.set_prop(id, prop, method)?;
                Ok(Value::Nil)
            })?,
            _ => return Ok(None),
        };
        Ok(Some(value))
    }

    /// Replace an object's superclass list.
    fn set_superclass_list(&mut self, id: ObjId, list: Value) -> VmResult<()> {
        let Some(count) = self.list_like_len(list)? else {
            return Err(VmError::new(code::BAD_TYPE_BIF));
        };
        if count < 1 {
            return Err(VmError::new(code::BAD_VAL_BIF));
        }
        let mut superclasses = Vec::new();
        let first = self.list_like_get(list, 1)?;
        // A list of just `ProgramObject` means no superclasses at all.
        let root_only = count == 1
            && first == Value::Obj(self.meta.class_obj(MetaKind::ProgramObject))
            && first.obj_id().is_valid();
        if !root_only {
            for index in 1..=count {
                let Value::Obj(class) = self.list_like_get(list, index)? else {
                    return Err(VmError::new(code::BAD_VAL_BIF));
                };
                if !matches!(self.objects.get(class), Some(Object::Program(_)))
                    || self.is_instance_of(class, id)
                {
                    return Err(VmError::new(code::BAD_VAL_BIF));
                }
                superclasses.push(class);
            }
        }
        match self.objects.program_object_mut(id) {
            Some(object) => {
                object.modified = true;
                object.set_superclasses(superclasses);
            }
            None => return Err(VmError::new(code::OBJ_VAL_REQD)),
        }
        // Any object that inherits from this one may have a search order
        // that is now out of date.
        for other in self.objects.ids() {
            if let Some(object) = self.objects.program_object(other)
                && object.search_path_contains(id)
            {
                object.invalidate_search_path();
            }
        }
        Ok(())
    }
}