monkey-gc 2.0.2

QuickJS-style GC runtime for Monkey (bytecode VM with cycle collector)
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
use std::collections::{HashMap, HashSet};

use byteorder::{BigEndian, ByteOrder};
use compiler::compiler::{BindingDebugInfo, Bytecode, DebugInfo};
use compiler::op_code::Opcode;
use object::builtins::{BuiltIns, BuiltinId};
use object::Object;
use parser::lexer::token::Span;
use serde::Serialize;

use crate::debugger::{collect_hit, DebuggerHit, HitContext, MAX_DEBUGGER_HITS};
use crate::frame::Frame;
use crate::report::{
    empty_value_kind_counts, select_global_roots, summarize_gc_object, GcCollectionReport,
    GlobalRoot,
};
use crate::value::{
    alloc_value, call_builtin_with_output, export_object, get_value, get_value_mut, import_object,
    try_export_object, value_to_string, GcBoundMethod, GcClass, GcClosure, GcInstance, HashKey,
    Value,
};
use crate::{GcHeap, GcId, GcRef};

const STACK_SIZE: usize = 2048;
pub const GLOBAL_SIZE: usize = 65536;
const MAX_FRAMES: usize = 1024;
pub const DEFAULT_INSTRUCTION_BUDGET: usize = 100_000;

/// Runtime failure returned by the established VM and runner APIs.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GcRuntimeError {
    pub message: String,
    pub span: Option<Span>,
}

/// Runtime failure with a stable, machine-readable category.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GcClassifiedRuntimeError {
    pub kind: GcRuntimeErrorKind,
    pub message: String,
    pub span: Option<Span>,
}

impl From<GcClassifiedRuntimeError> for GcRuntimeError {
    fn from(error: GcClassifiedRuntimeError) -> Self {
        Self {
            message: error.message,
            span: error.span,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum GcRuntimeErrorKind {
    Arithmetic,
    Call,
    ExecutionLimit,
    Index,
    Property,
    Stack,
    Type,
    InvalidBytecode,
}

impl GcRuntimeErrorKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Arithmetic => "arithmetic",
            Self::Call => "call",
            Self::ExecutionLimit => "executionLimit",
            Self::Index => "index",
            Self::Property => "property",
            Self::Stack => "stack",
            Self::Type => "type",
            Self::InvalidBytecode => "invalidBytecode",
        }
    }
}

enum CalleeKind {
    Closure(GcClosure),
    Builtin(BuiltinId),
    BoundMethod(GcBoundMethod),
    Class(String),
    Other(String),
}

pub struct GcVM {
    heap: GcHeap,
    constants: Vec<GcRef>,
    stack: Vec<GcRef>,
    sp: usize,
    globals: Vec<GcRef>,
    global_bindings: Vec<BindingDebugInfo>,
    /// One flag per global slot: set once `OpSetGlobal` writes it, so the
    /// debugger can tell a never-executed `let`'s prefilled null from a real
    /// null value. Kept across `load_bytecode` like the globals themselves.
    globals_initialized: Vec<bool>,
    frames: Vec<Frame>,
    frame_index: usize,
    null: GcRef,
    last_popped: GcRef,
    main_debug_info: DebugInfo,
    function_debug_info: HashMap<GcRef, DebugInfo>,
    debugger_hits: Vec<DebuggerHit>,
    /// Hits past `MAX_DEBUGGER_HITS` are counted, not recorded, so a
    /// `debugger;` in a hot loop cannot grow memory without bound.
    dropped_debugger_hits: usize,
    output: Option<String>,
}

impl GcVM {
    pub fn new(bytecode: Bytecode) -> Self {
        let Bytecode {
            instructions,
            constants: object_constants,
            debug_info: main_debug_info,
            function_debug_info: object_function_debug_info,
        } = bytecode;
        let mut heap = GcHeap::new();
        let null = alloc_value(&mut heap, Value::Null);
        let constants = object_constants
            .iter()
            .map(|constant| import_object(&mut heap, constant))
            .collect::<Vec<_>>();
        let function_debug_info = object_function_debug_info
            .into_iter()
            .filter_map(|(index, debug_info)| {
                constants
                    .get(index)
                    .copied()
                    .map(|reference| (reference, debug_info))
            })
            .collect();

        let main_fn = alloc_value(
            &mut heap,
            Value::CompiledFunction(object::CompiledFunction {
                name: String::new(),
                instructions: instructions.data,
                num_locals: 0,
                num_parameters: 0,
            }),
        );
        let main_instructions = compiled_instructions(&heap, main_fn);
        // Frames keep borrowed GcRefs. The initial main_fn allocation is the VM
        // root for these handles; placeholder frames do not take extra refs.
        let main_frame = Frame::new(
            GcClosure {
                func: main_fn,
                free: vec![],
            },
            main_instructions,
            0,
            0,
            0,
        );

        let empty_frame = Frame::new(
            GcClosure {
                func: main_fn,
                free: vec![],
            },
            vec![],
            0,
            0,
            0,
        );

        let mut frames = vec![empty_frame; MAX_FRAMES];
        frames[0] = main_frame;

        let stack = (0..STACK_SIZE).map(|_| heap.dup(null)).collect();
        let globals = (0..GLOBAL_SIZE).map(|_| heap.dup(null)).collect();
        let last_popped = heap.dup(null);

        GcVM {
            heap,
            constants,
            stack,
            sp: 0,
            globals,
            global_bindings: Vec::new(),
            globals_initialized: vec![false; GLOBAL_SIZE],
            frames,
            frame_index: 1,
            null,
            last_popped,
            main_debug_info,
            function_debug_info,
            debugger_hits: Vec::new(),
            dropped_debugger_hits: 0,
            output: None,
        }
    }

    /// Replace the current main program while preserving the heap and globals.
    ///
    /// Used by the REPL so later lines can read bindings created earlier. Old
    /// constant-pool owning refs are released; objects still reachable from
    /// globals stay alive.
    pub fn load_bytecode(&mut self, bytecode: Bytecode) {
        let Bytecode {
            instructions,
            constants: object_constants,
            debug_info: main_debug_info,
            function_debug_info: object_function_debug_info,
        } = bytecode;

        self.clear_stack_range(0, self.sp);
        self.sp = 0;

        // Reset the last result so statements that never pop (e.g. a bare
        // `let`) report null instead of the previous program's result.
        self.heap.free(self.last_popped);
        self.last_popped = self.heap.dup(self.null);

        for reference in self.constants.drain(..) {
            self.heap.free(reference);
        }
        self.function_debug_info.clear();

        let old_main = self.frames[0].cl.func;
        self.heap.free(old_main);

        self.constants = object_constants
            .iter()
            .map(|constant| import_object(&mut self.heap, constant))
            .collect::<Vec<_>>();
        self.function_debug_info = object_function_debug_info
            .into_iter()
            .filter_map(|(index, debug_info)| {
                self.constants
                    .get(index)
                    .copied()
                    .map(|reference| (reference, debug_info))
            })
            .collect();
        self.main_debug_info = main_debug_info;

        let main_fn = alloc_value(
            &mut self.heap,
            Value::CompiledFunction(object::CompiledFunction {
                name: String::new(),
                instructions: instructions.data,
                num_locals: 0,
                num_parameters: 0,
            }),
        );
        let main_instructions = compiled_instructions(&self.heap, main_fn);
        let main_frame = Frame::new(
            GcClosure {
                func: main_fn,
                free: vec![],
            },
            main_instructions,
            0,
            0,
            0,
        );
        let empty_frame = Frame::new(
            GcClosure {
                func: main_fn,
                free: vec![],
            },
            vec![],
            0,
            0,
            0,
        );
        self.frames = vec![empty_frame; MAX_FRAMES];
        self.frames[0] = main_frame;
        self.frame_index = 1;

        // Hits describe the program that recorded them; a new program starts
        // clean. Globals and their initialized bits survive, as above.
        self.debugger_hits.clear();
        self.dropped_debugger_hits = 0;
    }

    pub fn heap(&self) -> &GcHeap {
        &self.heap
    }

    pub fn heap_mut(&mut self) -> &mut GcHeap {
        &mut self.heap
    }

    /// Capture `puts`/`print` output in-memory instead of writing to stdout.
    pub fn set_capture_output(&mut self, capture: bool) {
        self.output = capture.then(String::new);
    }

    /// Drain captured output while leaving capture enabled.
    pub fn take_output(&mut self) -> String {
        self.output.as_mut().map(std::mem::take).unwrap_or_default()
    }

    /// Record the compiler's global definition ledger (slot order, rebindings
    /// included), used by GC reports for the named root set (see `GlobalRoot`)
    /// and by debugger snapshots for slot names.
    pub fn set_global_bindings(&mut self, bindings: Vec<BindingDebugInfo>) {
        self.global_bindings = bindings;
    }

    /// One flag per global slot: has `OpSetGlobal` written it? Debugger
    /// snapshots use this to tell a user-assigned null from a never-run `let`.
    pub fn globals_initialized(&self) -> &[bool] {
        &self.globals_initialized
    }

    /// Drain recorded `debugger;` snapshots plus the count of hits dropped
    /// after `MAX_DEBUGGER_HITS`. Recording resumes from zero afterwards.
    pub fn take_debugger_hits(&mut self) -> (Vec<DebuggerHit>, usize) {
        let dropped = std::mem::take(&mut self.dropped_debugger_hits);
        return (std::mem::take(&mut self.debugger_hits), dropped);
    }

    fn record_debugger_hit(&mut self) {
        if self.debugger_hits.len() >= MAX_DEBUGGER_HITS {
            self.dropped_debugger_hits += 1;
            return;
        }
        let hit = collect_hit(HitContext {
            heap: &self.heap,
            frames: &self.frames[..self.frame_index],
            stack: &self.stack,
            sp: self.sp,
            globals: &self.globals,
            global_bindings: &self.global_bindings,
            globals_initialized: &self.globals_initialized,
            main_debug_info: &self.main_debug_info,
            function_debug_info: &self.function_debug_info,
            index: self.debugger_hits.len() + 1,
        });
        self.debugger_hits.push(hit);
    }

    pub fn collect_garbage(&mut self) -> GcCollectionReport {
        // Read the named global slots before collecting. Named slots are VM
        // roots, so every object listed here survives the cycle collector.
        // Reports show one root per visible name — for a rebound name the
        // highest slot wins, matching what source-level code can still read —
        // sorted by name for determinism.
        let mut visible: HashMap<&str, usize> = HashMap::new();
        for binding in &self.global_bindings {
            visible.insert(binding.name.as_str(), binding.slot);
        }
        let mut named: Vec<(&str, usize)> = visible.into_iter().collect();
        named.sort_unstable();
        let global_roots = named
            .into_iter()
            .filter(|(_, slot)| *slot < self.globals.len())
            .map(|(name, slot)| GlobalRoot {
                name: name.to_string(),
                object_id: self.globals[slot].0,
            })
            .collect();
        let before_kinds = self.heap.value_kinds_by_id();
        let before = self.heap.snapshot();
        let diagnostics = self.heap.run_gc_with_stats_bundle();
        let after = self.heap.snapshot();
        let mut collected_by_value_kind = empty_value_kind_counts();
        for (id, kind) in before_kinds {
            if !self.heap.runtime().object_exists(id) {
                *collected_by_value_kind.entry(kind).or_default() += 1;
            }
        }
        let mut objects = diagnostics.objects;
        let cataloged: HashSet<GcId> = objects.iter().map(|object| object.id).collect();
        let (global_roots, omitted_global_roots) = select_global_roots(global_roots, &cataloged);
        // The phase budgets pick the catalog without looking at the root set,
        // so a kept root can name an object the catalog dropped. Summarize
        // those now — named objects survived the collection, so they still
        // exist. This keeps every reported root resolvable.
        let mut uncataloged: Vec<GcId> = global_roots
            .iter()
            .map(|root| root.object_id)
            .filter(|id| !cataloged.contains(id))
            .collect();
        uncataloged.sort_unstable();
        uncataloged.dedup();
        for id in uncataloged {
            objects.push(summarize_gc_object(self.heap.runtime(), id));
        }
        objects.sort_unstable_by_key(|object| object.id);
        GcCollectionReport {
            before,
            after,
            objects,
            global_roots,
            omitted_global_roots,
            phases: diagnostics.phases,
            collected_by_value_kind,
        }
    }

    /// Every raise site names its error category directly; a message is never
    /// parsed to recover one.
    fn runtime_error(
        &self,
        kind: GcRuntimeErrorKind,
        message: impl Into<String>,
    ) -> GcClassifiedRuntimeError {
        let frame = &self.frames[self.frame_index - 1];
        let debug_info = if self.frame_index == 1 {
            Some(&self.main_debug_info)
        } else {
            self.function_debug_info.get(&frame.cl.func)
        };
        let span = debug_info.and_then(|debug_info| {
            (frame.ip >= 0)
                .then_some(frame.ip as usize)
                .and_then(|pc| debug_info.span_for_pc(pc).cloned())
        });
        GcClassifiedRuntimeError {
            kind,
            message: message.into(),
            span,
        }
    }

    pub fn run(&mut self) {
        self.run_with_budget(usize::MAX)
            .expect("GC VM execution failed");
    }

    pub fn run_with_budget(&mut self, instruction_budget: usize) -> Result<(), GcRuntimeError> {
        self.run_with_budget_classified(instruction_budget)
            .map_err(Into::into)
    }

    /// Execute with a budget and retain the category assigned at the raise site.
    pub fn run_with_budget_classified(
        &mut self,
        instruction_budget: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let mut executed = 0;
        while self.current_frame().ip < self.current_frame().instructions.len() as i32 - 1 {
            self.current_frame().ip += 1;
            let ip = self.current_frame().ip as usize;
            if executed >= instruction_budget {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::ExecutionLimit,
                    format!("instruction limit exceeded (budget: {})", instruction_budget),
                ));
            }
            executed += 1;
            let ins = self.current_frame().instructions.clone();
            let op = *ins.get(ip).unwrap();
            let opcode = Opcode::from_repr(op).ok_or_else(|| {
                self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    format!("unknown opcode 0x{:02x}", op),
                )
            })?;

            match opcode {
                Opcode::OpConst => {
                    let const_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let constant = self.constant(const_index)?;
                    self.dup_and_push(constant)?;
                }
                Opcode::OpAdd | Opcode::OpSub | Opcode::OpMul | Opcode::OpDiv => {
                    self.execute_binary_operation(opcode)?;
                }
                Opcode::OpPop => {
                    self.pop_discard()?;
                }
                Opcode::OpTrue => {
                    self.alloc_and_push(Value::Boolean(true))?;
                }
                Opcode::OpFalse => {
                    self.alloc_and_push(Value::Boolean(false))?;
                }
                Opcode::OpEqual
                | Opcode::OpNotEqual
                | Opcode::OpGreaterThan
                | Opcode::OpLessThan => {
                    self.execute_comparison(opcode)?;
                }
                Opcode::OpMinus => {
                    self.execute_minus_operation()?;
                }
                Opcode::OpBang => {
                    self.execute_bang_operation()?;
                }
                Opcode::OpJump => {
                    let pos = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip = pos as i32 - 1;
                }
                Opcode::OpJumpNotTruthy => {
                    let pos = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let condition = self.pop_owned()?;
                    if !is_truthy(&self.heap, condition) {
                        self.current_frame().ip = pos as i32 - 1;
                    }
                    self.heap.free(condition);
                }
                Opcode::OpNull => {
                    self.dup_and_push(self.null)?;
                }
                Opcode::OpGetGlobal => {
                    let global_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    self.dup_and_push(self.globals[global_index])?;
                }
                Opcode::OpSetGlobal => {
                    let global_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let value = self.pop_owned()?;
                    self.heap.free(self.globals[global_index]);
                    self.globals[global_index] = value;
                    self.globals_initialized[global_index] = true;
                }
                Opcode::OpArray => {
                    let count = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let start = self.stack_base_for(count)?;
                    let elements = self.build_array(start, self.sp);
                    let array = alloc_value(&mut self.heap, Value::Array(elements));
                    self.clear_stack_range(start, self.sp);
                    self.sp = start;
                    self.push_raw(array)?;
                }
                Opcode::OpHash => {
                    let count = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let start = self.stack_base_for(count)?;
                    let elements = self.build_hash(start, self.sp)?;
                    let hash = alloc_value(&mut self.heap, Value::Hash(elements));
                    self.clear_stack_range(start, self.sp);
                    self.sp = start;
                    self.push_raw(hash)?;
                }
                Opcode::OpIndex => {
                    let (index, left) = self.pop_owned_pair()?;
                    let result = self.execute_index_operation(left, index);
                    self.heap.free(index);
                    self.heap.free(left);
                    result?;
                }
                Opcode::OpReturnValue => {
                    let return_value = self.pop_owned()?;
                    if self.frame_index == 1 {
                        // A top-level return ends the program with this value
                        // as its result, matching the interpreter backend.
                        self.clear_stack_range(0, self.sp);
                        self.sp = 0;
                        self.heap.free(self.last_popped);
                        self.last_popped = return_value;
                        break;
                    }
                    let frame = self.pop_frame();
                    let new_sp = frame.base_pointer - 1;
                    self.clear_stack_range(new_sp, self.sp);
                    self.sp = new_sp;
                    self.push_raw(return_value)?;
                }
                Opcode::OpReturn => {
                    if self.frame_index == 1 {
                        self.clear_stack_range(0, self.sp);
                        self.sp = 0;
                        self.heap.free(self.last_popped);
                        self.last_popped = self.heap.dup(self.null);
                        break;
                    }
                    let frame = self.pop_frame();
                    let new_sp = frame.base_pointer - 1;
                    self.clear_stack_range(new_sp, self.sp);
                    self.sp = new_sp;
                    self.dup_and_push(self.null)?;
                }
                Opcode::OpCall => {
                    let num_args = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    self.execute_call(num_args)?;
                }
                Opcode::OpSetLocal => {
                    let local_index = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    let base = self.current_frame().base_pointer;
                    let slot = self.local_slot(base, local_index)?;
                    let value = self.pop_owned()?;
                    self.heap.free(self.stack[slot]);
                    self.stack[slot] = value;
                    // get_mut: hostile bytecode can index past num_locals; the
                    // write above is bounded by STACK_SIZE, the bitset is not.
                    if let Some(flag) = self.current_frame().initialized.get_mut(local_index) {
                        *flag = true;
                    }
                }
                Opcode::OpGetLocal => {
                    let local_index = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    let base = self.current_frame().base_pointer;
                    let slot = self.local_slot(base, local_index)?;
                    self.dup_and_push(self.stack[slot])?;
                }
                Opcode::OpGetBuiltin => {
                    let built_index = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    let definition = BuiltIns.get(built_index).ok_or_else(|| {
                        self.runtime_error(
                            GcRuntimeErrorKind::InvalidBytecode,
                            format!("builtin index {} out of range", built_index),
                        )
                    })?;
                    self.alloc_and_push(Value::Builtin(definition.id))?;
                }
                Opcode::OpClosure => {
                    let const_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    let num_free = ins[ip + 3] as usize;
                    self.current_frame().ip += 3;
                    self.push_closure(const_index, num_free)?;
                }
                Opcode::OpGetFree => {
                    let free_index = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    let free_var = self.current_frame().cl.free.get(free_index).copied();
                    let free_var = free_var.ok_or_else(|| {
                        self.runtime_error(
                            GcRuntimeErrorKind::InvalidBytecode,
                            format!("free variable index {} out of range", free_index),
                        )
                    })?;
                    self.dup_and_push(free_var)?;
                }
                Opcode::OpCurrentClosure => {
                    let current = self.current_frame().cl.clone();
                    self.alloc_and_push(Value::Closure(current))?;
                }
                Opcode::OpClass => {
                    let name_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let name = self.constant_string(name_index)?;
                    self.alloc_and_push(Value::Class(GcClass {
                        name,
                        constructor: None,
                        methods: HashMap::new(),
                    }))?;
                }
                Opcode::OpMethod => {
                    let name_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    let kind = ins[ip + 3];
                    self.current_frame().ip += 3;
                    let name = self.constant_string(name_index)?;
                    let method = self.pop_owned()?;
                    if self.sp == 0 {
                        self.heap.free(method);
                        return Err(
                            self.runtime_error(GcRuntimeErrorKind::Stack, "stack underflow")
                        );
                    }
                    let class = self.stack[self.sp - 1];
                    let result = self.install_method(class, name, method, kind == 1);
                    self.heap.free(method);
                    result?;
                }
                Opcode::OpGetProperty => {
                    let name_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let name = self.constant_string(name_index)?;
                    let receiver = self.pop_owned()?;
                    let value = self.get_property(receiver, &name);
                    self.heap.free(receiver);
                    self.push_raw(value?)?;
                }
                Opcode::OpSetProperty => {
                    let name_index = BigEndian::read_u16(&ins[ip + 1..ip + 3]) as usize;
                    self.current_frame().ip += 2;
                    let name = self.constant_string(name_index)?;
                    let (value, receiver) = self.pop_owned_pair()?;
                    let result = self.set_property(receiver, name, value);
                    self.heap.free(value);
                    self.heap.free(receiver);
                    result?;
                }
                Opcode::OpNew => {
                    let num_args = ins[ip + 1] as usize;
                    self.current_frame().ip += 1;
                    self.execute_new(num_args)?;
                }
                Opcode::OpDebugger => {
                    // No stack effect; completion semantics stay transparent.
                    self.record_debugger_hit();
                }
            }
        }
        Ok(())
    }

    pub fn last_popped_stack_elm(&self) -> Option<GcRef> {
        Some(self.last_popped)
    }

    pub fn export_last_result(&self) -> Option<Object> {
        self.last_popped_stack_elm()
            .map(|reference| export_object(&self.heap, reference))
    }

    pub fn try_export_last_result(&self) -> Result<Object, String> {
        try_export_object(&self.heap, self.last_popped)
    }

    pub fn last_result_string(&self) -> String {
        value_to_string(&self.heap, self.last_popped)
    }

    fn alloc_and_push(&mut self, value: Value) -> Result<(), GcClassifiedRuntimeError> {
        let reference = alloc_value(&mut self.heap, value);
        self.push_raw(reference)
    }

    fn dup_and_push(&mut self, reference: GcRef) -> Result<(), GcClassifiedRuntimeError> {
        let duplicated = self.heap.dup(reference);
        self.push_raw(duplicated)
    }

    fn push_raw(&mut self, value: GcRef) -> Result<(), GcClassifiedRuntimeError> {
        if self.sp >= STACK_SIZE {
            let error = self.runtime_error(GcRuntimeErrorKind::Stack, "stack limit exceeded");
            self.heap.free(value);
            return Err(error);
        }
        let old = self.stack[self.sp];
        self.stack[self.sp] = value;
        self.heap.free(old);
        self.sp += 1;
        Ok(())
    }

    /// Move the top stack slot's owned reference to the caller.
    ///
    /// The caller must either free the returned ref or store it in another
    /// owning location. The vacated stack slot is reset to a null ref.
    fn pop_owned(&mut self) -> Result<GcRef, GcClassifiedRuntimeError> {
        if self.sp == 0 {
            return Err(self.runtime_error(GcRuntimeErrorKind::Stack, "stack underflow"));
        }
        self.sp -= 1;
        let value = self.stack[self.sp];
        self.stack[self.sp] = self.heap.dup(self.null);
        Ok(value)
    }

    fn pop_discard(&mut self) -> Result<(), GcClassifiedRuntimeError> {
        let value = self.pop_owned()?;
        self.heap.free(self.last_popped);
        self.last_popped = value;
        Ok(())
    }

    /// Pop the top two owned references (top first). On underflow neither
    /// reference leaks.
    fn pop_owned_pair(&mut self) -> Result<(GcRef, GcRef), GcClassifiedRuntimeError> {
        let top = self.pop_owned()?;
        match self.pop_owned() {
            Ok(below) => Ok((top, below)),
            Err(error) => {
                self.heap.free(top);
                Err(error)
            }
        }
    }

    /// Bounds-checked `sp - count` for opcodes that consume `count` stack
    /// slots.
    fn stack_base_for(&self, count: usize) -> Result<usize, GcClassifiedRuntimeError> {
        self.sp
            .checked_sub(count)
            .ok_or_else(|| self.runtime_error(GcRuntimeErrorKind::Stack, "stack underflow"))
    }

    fn local_slot(
        &self,
        base: usize,
        local_index: usize,
    ) -> Result<usize, GcClassifiedRuntimeError> {
        let slot = base + local_index;
        if slot >= STACK_SIZE {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::InvalidBytecode,
                format!("local index {} out of range", local_index),
            ));
        }
        Ok(slot)
    }

    fn clear_stack_range(&mut self, start: usize, end: usize) {
        for index in start..end {
            let old = self.stack[index];
            self.stack[index] = self.heap.dup(self.null);
            self.heap.free(old);
        }
    }

    fn execute_binary_operation(&mut self, opcode: Opcode) -> Result<(), GcClassifiedRuntimeError> {
        let (right, left) = self.pop_owned_pair()?;
        let left_value = get_value(&self.heap, left).clone();
        let right_value = get_value(&self.heap, right).clone();
        let result = match (&left_value, &right_value) {
            (Value::Integer(l), Value::Integer(r)) => match opcode {
                Opcode::OpAdd => Ok(Value::Integer(l.wrapping_add(*r))),
                Opcode::OpSub => Ok(Value::Integer(l.wrapping_sub(*r))),
                Opcode::OpMul => Ok(Value::Integer(l.wrapping_mul(*r))),
                Opcode::OpDiv if *r != 0 => {
                    l.checked_div(*r).map(Value::Integer).ok_or_else(|| {
                        (GcRuntimeErrorKind::Arithmetic, "integer overflow in division".to_string())
                    })
                }
                Opcode::OpDiv => {
                    Err((GcRuntimeErrorKind::Arithmetic, "division by zero".to_string()))
                }
                _ => unreachable!(),
            },
            (Value::String(l), Value::String(r)) if opcode == Opcode::OpAdd => {
                Ok(Value::String(l.to_string() + r))
            }
            _ => Err((
                GcRuntimeErrorKind::Type,
                format!(
                    "unsupported binary operation for {} and {}",
                    value_to_string(&self.heap, left),
                    value_to_string(&self.heap, right)
                ),
            )),
        };
        self.heap.free(left);
        self.heap.free(right);
        match result {
            Ok(value) => self.alloc_and_push(value),
            Err((kind, message)) => Err(self.runtime_error(kind, message)),
        }
    }

    fn execute_comparison(&mut self, opcode: Opcode) -> Result<(), GcClassifiedRuntimeError> {
        let (right, left) = self.pop_owned_pair()?;
        let result = match (get_value(&self.heap, left), get_value(&self.heap, right)) {
            (Value::Integer(l), Value::Integer(r)) => match opcode {
                Opcode::OpEqual => Some(l == r),
                Opcode::OpNotEqual => Some(l != r),
                Opcode::OpGreaterThan => Some(l > r),
                Opcode::OpLessThan => Some(l < r),
                _ => unreachable!(),
            },
            (Value::Boolean(l), Value::Boolean(r)) => match opcode {
                Opcode::OpEqual => Some(l == r),
                Opcode::OpNotEqual => Some(l != r),
                _ => None,
            },
            (Value::String(l), Value::String(r)) => match opcode {
                Opcode::OpEqual => Some(l == r),
                Opcode::OpNotEqual => Some(l != r),
                _ => None,
            },
            (Value::Null, Value::Null) => match opcode {
                Opcode::OpEqual => Some(true),
                Opcode::OpNotEqual => Some(false),
                _ => None,
            },
            (Value::Class(_), Value::Class(_))
            | (Value::Instance(_), Value::Instance(_))
            | (Value::BoundMethod(_), Value::BoundMethod(_)) => match opcode {
                Opcode::OpEqual => Some(left == right),
                Opcode::OpNotEqual => Some(left != right),
                _ => None,
            },
            _ => None,
        };
        let message = if result.is_none() {
            Some(format!(
                "unsupported comparison for {} and {}",
                value_to_string(&self.heap, left),
                value_to_string(&self.heap, right)
            ))
        } else {
            None
        };
        self.heap.free(left);
        self.heap.free(right);
        if let Some(result) = result {
            self.alloc_and_push(Value::Boolean(result))
        } else {
            Err(self.runtime_error(GcRuntimeErrorKind::Type, message.unwrap()))
        }
    }

    fn execute_minus_operation(&mut self) -> Result<(), GcClassifiedRuntimeError> {
        let operand = self.pop_owned()?;
        let negated = match get_value(&self.heap, operand) {
            Value::Integer(value) => Some(value.wrapping_neg()),
            _ => None,
        };
        let message = negated.is_none().then(|| {
            format!("unsupported type for negation: {}", value_to_string(&self.heap, operand))
        });
        self.heap.free(operand);
        if let Some(negated) = negated {
            self.alloc_and_push(Value::Integer(negated))
        } else {
            Err(self.runtime_error(GcRuntimeErrorKind::Type, message.unwrap()))
        }
    }

    fn execute_bang_operation(&mut self) -> Result<(), GcClassifiedRuntimeError> {
        let operand = self.pop_owned()?;
        let result = match get_value(&self.heap, operand) {
            Value::Boolean(l) => !l,
            _ => false,
        };
        self.heap.free(operand);
        self.alloc_and_push(Value::Boolean(result))
    }

    fn build_array(&mut self, start: usize, end: usize) -> Vec<GcRef> {
        let mut elements = Vec::with_capacity(end - start);
        for i in start..end {
            elements.push(self.stack[i]);
        }
        elements
    }

    fn build_hash(
        &mut self,
        start: usize,
        end: usize,
    ) -> Result<HashMap<HashKey, GcRef>, GcClassifiedRuntimeError> {
        let mut elements = HashMap::new();
        for i in (start..end).step_by(2) {
            let key_ref = self.stack[i];
            let key = HashKey::from_value(get_value(&self.heap, key_ref)).ok_or_else(|| {
                self.runtime_error(
                    GcRuntimeErrorKind::Index,
                    format!(
                        "hash key must be hashable, got {}",
                        value_to_string(&self.heap, key_ref)
                    ),
                )
            })?;
            elements.insert(key, self.stack[i + 1]);
        }
        Ok(elements)
    }

    fn execute_index_operation(
        &mut self,
        left: GcRef,
        index: GcRef,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let left_value = get_value(&self.heap, left).clone();
        let index_value = get_value(&self.heap, index).clone();
        match (&left_value, &index_value) {
            (Value::Array(array), Value::Integer(i)) => self.execute_array_index(array, *i),
            (Value::Hash(hash), _) => self.execute_hash_index(hash, &index_value),
            _ => Err(self.runtime_error(
                GcRuntimeErrorKind::Index,
                format!(
                    "unsupported index operation for {} and {}",
                    value_to_string(&self.heap, left),
                    value_to_string(&self.heap, index)
                ),
            )),
        }
    }

    fn execute_array_index(
        &mut self,
        array: &[GcRef],
        index: i64,
    ) -> Result<(), GcClassifiedRuntimeError> {
        if index < array.len() as i64 && index >= 0 {
            self.dup_and_push(array[index as usize])
        } else {
            self.dup_and_push(self.null)
        }
    }

    fn execute_hash_index(
        &mut self,
        hash: &HashMap<HashKey, GcRef>,
        index: &Value,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let key = HashKey::from_value(index).ok_or_else(|| {
            self.runtime_error(GcRuntimeErrorKind::Index, "unsupported hash index key")
        })?;
        match hash.get(&key) {
            Some(value) => self.dup_and_push(*value),
            None => self.dup_and_push(self.null),
        }
    }

    fn current_frame(&mut self) -> &mut Frame {
        &mut self.frames[self.frame_index - 1]
    }

    fn push_frame(&mut self, frame: Frame) -> Result<(), GcClassifiedRuntimeError> {
        if self.frame_index >= MAX_FRAMES {
            return Err(self.runtime_error(GcRuntimeErrorKind::Stack, "frame limit exceeded"));
        }
        self.frames[self.frame_index] = frame;
        self.frame_index += 1;
        Ok(())
    }

    fn pop_frame(&mut self) -> Frame {
        self.frame_index -= 1;
        self.frames[self.frame_index].clone()
    }

    fn execute_call(&mut self, num_args: usize) -> Result<(), GcClassifiedRuntimeError> {
        let callee_slot = self.stack_base_for(num_args + 1)?;
        let callee = self.stack[callee_slot];
        match callee_kind(&self.heap, callee) {
            CalleeKind::Closure(closure) => self.call_closure(closure, num_args),
            CalleeKind::Builtin(builtin) => self.call_builtin(builtin, num_args),
            CalleeKind::BoundMethod(bound) => self.call_bound_method(bound, num_args),
            CalleeKind::Class(name) => Err(self.runtime_error(
                GcRuntimeErrorKind::Call,
                format!("class {} must be constructed with new", name),
            )),
            CalleeKind::Other(value) => {
                Err(self.runtime_error(GcRuntimeErrorKind::Call, format!("cannot call {}", value)))
            }
        }
    }

    fn call_closure(
        &mut self,
        closure: GcClosure,
        num_args: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let compiled = match get_value(&self.heap, closure.func) {
            Value::CompiledFunction(f) => f.clone(),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "closure without compiled function",
                ))
            }
        };
        if compiled.num_parameters != num_args {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::Call,
                format!(
                    "wrong number of arguments: want={}, got={}",
                    compiled.num_parameters, num_args
                ),
            ));
        }

        // checked_add: num_locals comes from bytecode, so it can be an
        // arbitrary usize, not just a compiler-emitted small count. Validate
        // it before Frame::new allocates the per-local initialized bitset.
        let base_pointer = self.sp - num_args;
        let next_sp = base_pointer
            .checked_add(compiled.num_locals)
            .filter(|next_sp| *next_sp <= STACK_SIZE)
            .ok_or_else(|| self.runtime_error(GcRuntimeErrorKind::Stack, "stack limit exceeded"))?;
        let frame = Frame::new(
            closure,
            compiled.instructions,
            base_pointer,
            compiled.num_locals,
            compiled.num_parameters,
        );
        self.sp = next_sp;
        self.push_frame(frame)
    }

    fn call_builtin(
        &mut self,
        builtin: BuiltinId,
        num_args: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let base = self.sp - num_args - 1;
        let args = self.stack[self.sp - num_args..self.sp].to_vec();
        let result = call_builtin_with_output(
            &mut self.heap,
            builtin,
            &args,
            self.null,
            self.output.as_mut(),
        );
        self.clear_stack_range(base, self.sp);
        self.sp = base;
        self.push_raw(result)
    }

    fn push_closure(
        &mut self,
        const_index: usize,
        num_free: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let func = self.constant(const_index)?;
        if !matches!(get_value(&self.heap, func), Value::CompiledFunction(_)) {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::InvalidBytecode,
                format!("cannot build closure over {}", value_to_string(&self.heap, func)),
            ));
        }
        let start = self.stack_base_for(num_free)?;
        let mut free = Vec::with_capacity(num_free);
        for i in 0..num_free {
            free.push(self.stack[start + i]);
        }
        let closure = alloc_value(
            &mut self.heap,
            Value::Closure(GcClosure {
                func,
                free,
            }),
        );
        self.clear_stack_range(start, self.sp);
        self.sp = start;
        self.push_raw(closure)
    }

    fn constant(&self, index: usize) -> Result<GcRef, GcClassifiedRuntimeError> {
        self.constants.get(index).copied().ok_or_else(|| {
            self.runtime_error(
                GcRuntimeErrorKind::InvalidBytecode,
                format!("constant index {} out of range", index),
            )
        })
    }

    fn constant_string(&self, index: usize) -> Result<String, GcClassifiedRuntimeError> {
        let constant = self.constant(index)?;
        match get_value(&self.heap, constant) {
            Value::String(value) => Ok(value.clone()),
            value => Err(self.runtime_error(
                GcRuntimeErrorKind::InvalidBytecode,
                format!("expected string constant, got {}", value),
            )),
        }
    }

    fn install_method(
        &mut self,
        class: GcRef,
        name: String,
        method: GcRef,
        constructor: bool,
    ) -> Result<(), GcClassifiedRuntimeError> {
        if !matches!(get_value(&self.heap, class), Value::Class(_)) {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::InvalidBytecode,
                format!("cannot install method on {}", value_to_string(&self.heap, class)),
            ));
        }
        let owned_method = self.heap.dup(method);
        let old_method = match get_value_mut(&mut self.heap, class) {
            Value::Class(class) => {
                if constructor {
                    class.constructor.replace(owned_method)
                } else {
                    class.methods.insert(name, owned_method)
                }
            }
            _ => unreachable!(),
        };
        if let Some(old_method) = old_method {
            self.heap.free(old_method);
        }
        Ok(())
    }

    fn get_property(
        &mut self,
        receiver: GcRef,
        name: &str,
    ) -> Result<GcRef, GcClassifiedRuntimeError> {
        let (class, field) = match get_value(&self.heap, receiver) {
            Value::Instance(instance) => (instance.class, instance.fields.get(name).copied()),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::Property,
                    format!(
                        "cannot read property '{}' of {}",
                        name,
                        value_to_string(&self.heap, receiver)
                    ),
                ))
            }
        };
        if let Some(field) = field {
            return Ok(self.heap.dup(field));
        }

        let (class_name, method) = match get_value(&self.heap, class) {
            Value::Class(class) => (class.name.clone(), class.methods.get(name).copied()),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "instance has invalid class",
                ))
            }
        };
        match method {
            Some(method) => Ok(alloc_value(
                &mut self.heap,
                Value::BoundMethod(GcBoundMethod {
                    receiver,
                    method,
                    name: name.to_string(),
                }),
            )),
            None => Err(self.runtime_error(
                GcRuntimeErrorKind::Property,
                format!("property '{}' does not exist on {}", name, class_name),
            )),
        }
    }

    fn set_property(
        &mut self,
        receiver: GcRef,
        name: String,
        value: GcRef,
    ) -> Result<(), GcClassifiedRuntimeError> {
        if !matches!(get_value(&self.heap, receiver), Value::Instance(_)) {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::Property,
                format!(
                    "cannot set property '{}' of {}",
                    name,
                    value_to_string(&self.heap, receiver)
                ),
            ));
        }
        let owned_value = self.heap.dup(value);
        let old_value = match get_value_mut(&mut self.heap, receiver) {
            Value::Instance(instance) => instance.fields.insert(name, owned_value),
            _ => unreachable!(),
        };
        if let Some(old_value) = old_value {
            self.heap.free(old_value);
        }
        Ok(())
    }

    fn execute_new(&mut self, num_args: usize) -> Result<(), GcClassifiedRuntimeError> {
        let base = self.stack_base_for(num_args + 1)?;
        let class_reference = self.stack[base];
        let (class_name, constructor) = match get_value(&self.heap, class_reference) {
            Value::Class(class) => (class.name.clone(), class.constructor),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::Call,
                    format!("cannot construct {}", value_to_string(&self.heap, class_reference)),
                ))
            }
        };

        let Some(constructor) = constructor else {
            if num_args != 0 {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::Call,
                    format!(
                        "wrong number of arguments for {}.constructor: want=0, got={}",
                        class_name, num_args
                    ),
                ));
            }
            let instance = alloc_value(
                &mut self.heap,
                Value::Instance(GcInstance {
                    class: class_reference,
                    fields: HashMap::new(),
                }),
            );
            self.clear_stack_range(base, self.sp);
            self.sp = base;
            return self.push_raw(instance);
        };

        let closure = match get_value(&self.heap, constructor) {
            Value::Closure(closure) => closure.clone(),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "constructor is not a closure",
                ))
            }
        };
        let compiled = match get_value(&self.heap, closure.func) {
            Value::CompiledFunction(function) => function.clone(),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "constructor closure has invalid function",
                ))
            }
        };
        let expected = compiled.num_parameters.saturating_sub(1);
        if expected != num_args {
            return Err(self.runtime_error(
                GcRuntimeErrorKind::Call,
                format!(
                    "wrong number of arguments for {}.constructor: want={}, got={}",
                    class_name, expected, num_args
                ),
            ));
        }

        let instance = alloc_value(
            &mut self.heap,
            Value::Instance(GcInstance {
                class: class_reference,
                fields: HashMap::new(),
            }),
        );
        self.rewrite_receiver_call(constructor, instance, num_args)?;
        self.call_closure(closure, num_args + 1)
    }

    fn call_bound_method(
        &mut self,
        bound: GcBoundMethod,
        num_args: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let closure = match get_value(&self.heap, bound.method) {
            Value::Closure(closure) => closure.clone(),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "bound method is not a closure",
                ))
            }
        };
        let compiled = match get_value(&self.heap, closure.func) {
            Value::CompiledFunction(function) => function.clone(),
            _ => {
                return Err(self.runtime_error(
                    GcRuntimeErrorKind::InvalidBytecode,
                    "method closure has invalid function",
                ))
            }
        };
        let expected = compiled.num_parameters.saturating_sub(1);
        if expected != num_args {
            let class_name = match get_value(&self.heap, bound.receiver) {
                Value::Instance(instance) => match get_value(&self.heap, instance.class) {
                    Value::Class(class) => class.name.clone(),
                    _ => "<invalid class>".to_string(),
                },
                _ => "<invalid receiver>".to_string(),
            };
            return Err(self.runtime_error(
                GcRuntimeErrorKind::Call,
                format!(
                    "wrong number of arguments for {}.{}: want={}, got={}",
                    class_name, bound.name, expected, num_args
                ),
            ));
        }
        let receiver = self.heap.dup(bound.receiver);
        self.rewrite_receiver_call(bound.method, receiver, num_args)?;
        self.call_closure(closure, num_args + 1)
    }

    /// Takes ownership of `receiver` and frees it if the stack cannot hold
    /// the rewritten call layout.
    fn rewrite_receiver_call(
        &mut self,
        callable: GcRef,
        receiver: GcRef,
        num_args: usize,
    ) -> Result<(), GcClassifiedRuntimeError> {
        let base = self.sp - num_args - 1;
        if base + num_args + 2 > STACK_SIZE {
            let error = self.runtime_error(GcRuntimeErrorKind::Stack, "stack limit exceeded");
            self.heap.free(receiver);
            return Err(error);
        }
        let callable = self.heap.dup(callable);
        let borrowed_arguments = self.stack[self.sp - num_args..self.sp].to_vec();
        let arguments = borrowed_arguments
            .into_iter()
            .map(|argument| self.heap.dup(argument))
            .collect::<Vec<_>>();
        self.clear_stack_range(base, self.sp);
        self.sp = base;
        self.push_raw(callable)?;
        self.push_raw(receiver)?;
        for argument in arguments {
            self.push_raw(argument)?;
        }
        Ok(())
    }
}

fn is_truthy(heap: &GcHeap, condition: GcRef) -> bool {
    match get_value(heap, condition) {
        Value::Boolean(b) => *b,
        Value::Null => false,
        _ => true,
    }
}

fn callee_kind(heap: &GcHeap, reference: GcRef) -> CalleeKind {
    match get_value(heap, reference) {
        Value::Closure(closure) => CalleeKind::Closure(closure.clone()),
        Value::Builtin(builtin) => CalleeKind::Builtin(*builtin),
        Value::BoundMethod(bound) => CalleeKind::BoundMethod(bound.clone()),
        Value::Class(class) => CalleeKind::Class(class.name.clone()),
        _ => CalleeKind::Other(value_to_string(heap, reference)),
    }
}

fn compiled_instructions(heap: &GcHeap, func: GcRef) -> Vec<u8> {
    match get_value(heap, func) {
        Value::CompiledFunction(f) => f.instructions.clone(),
        _ => panic!("expected compiled function"),
    }
}