pd-vm 0.22.6

RustScript bytecode compiler and VM
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
//! Focused drop-contract tests verifying that `Stmt::Drop` nodes emitted by the
//! lifetime pass correctly route through the VM's `drop_value_with_contract()`.
//!
//! Coverage targets:
//!   - dead-local single-drop
//!   - branch/loop ordering
//!   - yield/host-op + drop
//!   - closure-capture drop ordering
//!   - native/JIT parity (when cranelift-jit feature is enabled)
//!   - local-slot Null verification after drops
//!   - nested container recursive cleanup
//!   - break/continue with live non-trivial locals
//!   - reset_for_reuse locals-Null contract
#[path = "../common/mod.rs"]
mod common;
use common::*;

use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Compile RustScript source, run to halt, return final drop-contract count.
fn compile_run_drop_count(source: &str) -> u64 {
    let compiled = compile_source(source).expect("compile should succeed");
    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    vm.drop_contract_event_count()
}

/// Compile RustScript source, run to halt, return the Vm for further inspection.
fn compile_run_vm(source: &str) -> Vm {
    let compiled = compile_source(source).expect("compile should succeed");
    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    vm
}

fn new_drop_contract_vm(program: Program) -> Vm {
    let mut vm = Vm::new(program);
    vm.set_drop_contract_events_enabled(true);
    vm
}

fn local_visible_at_current_line(vm: &Vm, name: &str) -> bool {
    let info = vm
        .debug_info()
        .expect("compiled program should include debug info");
    let local = info
        .locals
        .iter()
        .find(|local| local.name == name)
        .unwrap_or_else(|| panic!("expected local '{name}' in debug info"));
    let Some(line) = info.line_for_offset(vm.ip()) else {
        return true;
    };
    if let Some(declared_line) = local.declared_line
        && line < declared_line
    {
        return false;
    }
    if let Some(last_line) = local.last_line
        && line > last_line
    {
        return false;
    }
    true
}

/// Host function that returns Pending on first call, then returns empty result on resume.
struct PendingOnce {
    call_count: Arc<AtomicUsize>,
    op_id: u64,
}

impl HostFunction for PendingOnce {
    fn call(&mut self, _vm: &mut Vm, _args: &[Value]) -> Result<CallOutcome, vm::VmError> {
        self.call_count.fetch_add(1, Ordering::SeqCst);
        Ok(CallOutcome::Pending(self.op_id))
    }
}

// ---------------------------------------------------------------------------
// 1. Dead-local single-drop
// ---------------------------------------------------------------------------

#[test]
fn dead_scalar_local_is_dropped_exactly_once() {
    // A single scalar local that goes dead should trigger exactly one drop
    // contract event.
    let source = r#"
        let a = "hello";
        0;
    "#;
    let drops = compile_run_drop_count(source);
    assert_eq!(drops, 1, "expected exactly one drop event for dead local a");
}

#[test]
fn dead_local_drop_count_increases_with_more_dead_values() {
    // Two independent dead locals should produce strictly more drop events
    // than one.
    let one_dead = r#"
        let a = { x: 1 };
        let b = 1;
        b;
    "#;
    let two_dead = r#"
        let a = { x: 1 };
        let c = { y: 2 };
        let b = 1;
        b;
    "#;
    let drops_one = compile_run_drop_count(one_dead);
    let drops_two = compile_run_drop_count(two_dead);
    assert!(
        drops_two > drops_one,
        "two dead locals should produce more drop events ({drops_two}) than one ({drops_one})"
    );
}

// ---------------------------------------------------------------------------
// 2. Branch / loop ordering
// ---------------------------------------------------------------------------

#[test]
fn if_else_branches_drop_dead_locals() {
    // Each branch introduces a local that should be dropped on the
    // non-taken path's convergence.
    let source = r#"
        let cond = true;
        let mut result = 0;
        if cond {
            let tmp = { payload: [1, 2, 3] };
            result = 10;
        } else {
            let tmp = { payload: [4, 5] };
            result = 20;
        }
        result;
    "#;
    let drops = compile_run_drop_count(source);
    assert!(
        drops > 0,
        "expected drop-contract events for branch-local temporaries, got {drops}"
    );
}

#[test]
fn loop_body_drops_dead_local_each_iteration() {
    // A map constructed inside a loop body and dead-at-the-end should fire
    // the drop contract on every iteration — so the count should scale with
    // iteration count.
    let source_3 = r#"
        let mut i = 0;
        while i < 3 {
            let tmp = { n: i };
            i = i + 1;
        }
        i;
    "#;
    let source_6 = r#"
        let mut i = 0;
        while i < 6 {
            let tmp = { n: i };
            i = i + 1;
        }
        i;
    "#;
    let drops_3 = compile_run_drop_count(source_3);
    let drops_6 = compile_run_drop_count(source_6);
    assert!(
        drops_3 > 0,
        "expected drop events for loop body dead local (3 iters), got {drops_3}"
    );
    assert!(
        drops_6 > drops_3,
        "6-iteration loop ({drops_6}) should produce more drop events than 3-iteration loop ({drops_3})"
    );
}

// ---------------------------------------------------------------------------
// 3. Yield / host-op + drop
// ---------------------------------------------------------------------------

#[test]
fn drop_events_fire_across_host_op_boundary() {
    // Dead local goes out of scope after a host-op wait.  Drop contract
    // must still be honoured.
    let source = r#"
        fn wait();
        let a = { tag: "before-wait" };
        wait();
        let b = { tag: "after-wait" };
        0;
    "#;

    let compiled = compile_source(source).expect("compile should succeed");
    let calls = Arc::new(AtomicUsize::new(0));
    let mut vm = new_drop_contract_vm(compiled.program);
    vm.register_function(Box::new(PendingOnce {
        call_count: Arc::clone(&calls),
        op_id: 800,
    }));

    // First run → Waiting
    let status = vm.run().expect("first run should wait");
    assert_eq!(status, VmStatus::Waiting(800));
    let drops_before = vm.drop_contract_event_count();

    // Complete host op and resume
    vm.complete_host_op(800, Vec::new())
        .expect("complete should succeed");
    let status = vm.resume().expect("resume should halt");
    assert_eq!(status, VmStatus::Halted);

    let drops_after = vm.drop_contract_event_count();
    assert!(
        drops_after > drops_before,
        "drop contract must not regress across host-op resume (before={drops_before}, after={drops_after})"
    );
    assert!(
        drops_after > 0,
        "expected at least one drop event across the wait boundary, got {drops_after}"
    );
}

#[test]
fn cooperative_yield_does_not_duplicate_drops() {
    // Use fuel-limited execution so the VM yields cooperatively. The final
    // drop count should match uninterrupted execution exactly.
    let source = r#"
        let mut i = 0;
        while i < 10 {
            let tmp = { v: i };
            i = i + 1;
        }
        i;
    "#;

    let baseline_compiled = compile_source(source).expect("compile baseline");
    let mut baseline_vm = new_drop_contract_vm(baseline_compiled.program);
    let baseline_status = baseline_vm.run().expect("baseline run should halt");
    assert_eq!(baseline_status, VmStatus::Halted);
    let baseline_drops = baseline_vm.drop_contract_event_count();

    let compiled = compile_source(source).expect("compile should succeed");
    let mut vm = new_drop_contract_vm(compiled.program);
    vm.set_fuel(10);

    let mut total_yields = 0u64;
    loop {
        let status = vm.run().expect("vm should run");
        match status {
            VmStatus::Halted => break,
            VmStatus::Yielded => {
                total_yields += 1;
                assert!(
                    total_yields < 4_096,
                    "low-fuel drop test made no progress after {total_yields} yields"
                );
                vm.recharge_fuel(10).expect("recharge should succeed");
            }
            VmStatus::Waiting(_) => panic!("unexpected waiting"),
        }
    }

    assert!(total_yields > 0, "expected at least one cooperative yield");
    let drops = vm.drop_contract_event_count();
    assert_eq!(
        drops, baseline_drops,
        "yield/resume should preserve exact drop parity with uninterrupted execution"
    );
    // There should be approximately 10 drops (one per iteration for `tmp`)
    // plus a few more for local cleanup.  We just ensure it's bounded —
    // double-drops would inflate the count wildly.
    assert!(
        drops < 100,
        "drop count ({drops}) is suspiciously high — possible double-drop across yield boundary"
    );
    assert!(
        drops > 0,
        "expected some drop events for loop-body dead locals, got 0"
    );
}

// ---------------------------------------------------------------------------
// 4. Closure-capture drop ordering
// ---------------------------------------------------------------------------

#[test]
fn closure_capture_value_is_dropped() {
    // A closure that captures a non-trivial value.  When the closure local
    // goes dead, the captured value should be dropped via the contract.
    let source = r#"
        fn apply(f, x) {
            f(x);
        }
        let data = { label: "captured" };
        let f = |x| x + 1;
        let result = apply(f, 5);
        result;
    "#;
    let drops = compile_run_drop_count(source);
    assert!(
        drops > 0,
        "expected drop-contract events when closure + captured data go dead, got {drops}"
    );
}

#[test]
fn multiple_captures_drop_in_order() {
    // Two captured values — both should be cleaned up.  We verify total
    // drops are strictly greater than with a single capture.
    let source_single = r#"
        fn apply(f, x) {
            f(x);
        }
        let a = { v: 1 };
        let f = |x| x + 1;
        apply(f, 0);
    "#;
    let source_double = r#"
        fn apply(f, x) {
            f(x);
        }
        let a = { v: 1 };
        let b = { v: 2 };
        let f = |x| x + 1;
        apply(f, 0);
    "#;
    let drops_single = compile_run_drop_count(source_single);
    let drops_double = compile_run_drop_count(source_double);
    assert!(
        drops_double > drops_single,
        "two captured locals should produce more drops ({drops_double}) than one ({drops_single})"
    );
}

// ---------------------------------------------------------------------------
// 5. Native / JIT parity (cranelift-jit feature)
// ---------------------------------------------------------------------------

/// Guard: only run native parity checks on supported platforms.
fn native_jit_supported() -> bool {
    (cfg!(target_arch = "x86_64")
        && (cfg!(target_os = "windows") || (cfg!(unix) && !cfg!(target_os = "macos"))))
        || (cfg!(target_arch = "aarch64")
            && (cfg!(target_os = "linux") || cfg!(target_os = "macos")))
}

#[cfg(feature = "cranelift-jit")]
#[test]
fn native_jit_drop_parity_loop() {
    use vm::JitConfig;

    // Run the same loop program in interpreter-only mode and with JIT.
    // Drop-contract counts should be identical.
    let source = r#"
        let mut i = 0;
        while i < 5 {
            let tmp = { v: i };
            i = i + 1;
        }
        i;
    "#;

    // Interpreter-only run
    let compiled_interp = compile_source(source).expect("compile interp");
    let mut vm_interp = new_drop_contract_vm(compiled_interp.program);
    vm_interp.set_jit_config(JitConfig {
        enabled: false,
        hot_loop_threshold: 1_000,
        max_trace_len: 512,
    });
    let status = vm_interp.run().expect("interp should halt");
    assert_eq!(status, VmStatus::Halted);
    let drops_interp = vm_interp.drop_contract_event_count();

    // JIT run (native if supported, else bytecode JIT)
    let compiled_jit = compile_source(source).expect("compile jit");
    let mut vm_jit = new_drop_contract_vm(compiled_jit.program);
    vm_jit.set_jit_config(JitConfig {
        enabled: native_jit_supported(),
        hot_loop_threshold: 1, // force hot-loop tracing immediately
        max_trace_len: 512,
    });
    let status = vm_jit.run().expect("jit should halt");
    assert_eq!(status, VmStatus::Halted);
    let drops_jit = vm_jit.drop_contract_event_count();

    assert_eq!(
        vm_interp.stack(),
        vm_jit.stack(),
        "interpreter and JIT should produce the same stack"
    );

    // Drop counts should match exactly across interpreter and JIT execution.
    assert!(
        drops_jit == drops_interp,
        "JIT drop count ({drops_jit}) should not exceed interpreter ({drops_interp}) — possible double-drop"
    );
}

#[cfg(feature = "cranelift-jit")]
#[test]
fn native_jit_drop_parity_branch() {
    use vm::JitConfig;

    let source = r#"
        let cond = true;
        let mut r = 0;
        if cond {
            let tmp = { a: 1 };
            r = 1;
        } else {
            let tmp = { b: 2 };
            r = 2;
        }
        r;
    "#;

    let compiled_interp = compile_source(source).expect("compile interp");
    let mut vm_interp = new_drop_contract_vm(compiled_interp.program);
    vm_interp.set_jit_config(JitConfig {
        enabled: false,
        hot_loop_threshold: 1_000,
        max_trace_len: 512,
    });
    let status = vm_interp.run().expect("interp should halt");
    assert_eq!(status, VmStatus::Halted);
    let drops_interp = vm_interp.drop_contract_event_count();

    let compiled_jit = compile_source(source).expect("compile jit");
    let mut vm_jit = new_drop_contract_vm(compiled_jit.program);
    vm_jit.set_jit_config(JitConfig {
        enabled: native_jit_supported(),
        hot_loop_threshold: 1,
        max_trace_len: 512,
    });
    let status = vm_jit.run().expect("jit should halt");
    assert_eq!(status, VmStatus::Halted);
    let drops_jit = vm_jit.drop_contract_event_count();

    assert_eq!(
        vm_interp.stack(),
        vm_jit.stack(),
        "interpreter and JIT should produce the same stack for branch test"
    );
    assert!(
        drops_jit == drops_interp,
        "JIT drop count (branch) ({drops_jit}) should match interpreter ({drops_interp})"
    );
}
// ---------------------------------------------------------------------------
// 6. Local-slot Null verification after drops
// ---------------------------------------------------------------------------

#[test]
fn dead_local_slot_is_null_after_drop() {
    // Verify the actual local slot holds Value::Null after the liveness pass
    // emits a Stmt::Drop, not just that the counter increments.
    let source = r#"
        let a = { key: "hello" };
        let b = 1;
        b;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let a_index = debug.local_index("a").expect("a binding should exist");
    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(
        vm.locals()[a_index as usize],
        Value::Null,
        "dead local 'a' slot should be Null after drop, got {:?}",
        vm.locals()[a_index as usize]
    );
    assert!(vm.drop_contract_event_count() > 0);
}

#[test]
fn branch_dead_local_slot_is_null_after_convergence() {
    // Both branches allocate a temporary — the taken branch's tmp should be
    // dropped and its slot Null after the if/else merges.
    let source = r#"
        let cond = true;
        let mut result = 0;
        if cond {
            let tmp = { payload: [1, 2, 3] };
            result = 10;
        } else {
            let tmp2 = { payload: [4, 5] };
            result = 20;
        }
        result;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let tmp_index = debug.local_index("tmp").expect("tmp binding should exist");

    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(vm.stack(), &[Value::Int(10)]);
    assert_eq!(
        vm.locals()[tmp_index as usize],
        Value::Null,
        "branch-local 'tmp' should be Null after convergence, got {:?}",
        vm.locals()[tmp_index as usize]
    );
}

#[test]
fn loop_body_dead_local_slot_is_null_after_exit() {
    // After the loop finishes, the loop-body temporary should be Null.
    let source = r#"
        let mut i = 0;
        while i < 3 {
            let tmp = { n: i };
            i = i + 1;
        }
        i;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let tmp_index = debug.local_index("tmp").expect("tmp binding should exist");

    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(vm.stack(), &[Value::Int(3)]);
    assert_eq!(
        vm.locals()[tmp_index as usize],
        Value::Null,
        "loop-body dead local 'tmp' should be Null after loop exit"
    );
}

// ---------------------------------------------------------------------------
// 7. Nested container recursive cleanup
// ---------------------------------------------------------------------------

#[test]
fn nested_container_drop_fires_recursively() {
    // A map containing nested maps and arrays should trigger multiple
    // recursive drop-contract events.
    let source = r#"
        let deep = { inner: { nested: [1, 2, 3], tag: "x" }, outer: [4, 5] };
        let result = 0;
        result;
    "#;
    let drops = compile_run_drop_count(source);
    // The outer map, inner map, inner array, outer array — each non-trivial
    // container is a drop event.  Plus their scalar children.
    assert!(
        drops >= 4,
        "expected at least 4 recursive drop events for nested containers, got {drops}"
    );
}

#[test]
fn nested_container_slot_is_null_after_drop() {
    let source = r#"
        let deep = { inner: { nested: [1, 2, 3] } };
        let x = 0;
        x;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let deep_index = debug
        .local_index("deep")
        .expect("deep binding should exist");

    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(
        vm.locals()[deep_index as usize],
        Value::Null,
        "nested container local 'deep' slot should be Null after drop"
    );
}

// ---------------------------------------------------------------------------
// 8. Break / continue with live non-trivial locals
// ---------------------------------------------------------------------------

#[test]
fn break_drops_live_locals_in_scope() {
    // When `break` exits a loop mid-iteration, any non-trivial locals
    // constructed before the break should be dropped.
    let source = r#"
        let mut result = 0;
        let mut i = 0;
        while i < 10 {
            let heavy = { data: [i, i + 1, i + 2] };
            if i == 2 {
                result = 99;
                break;
            }
            i = i + 1;
        }
        result;
    "#;
    let vm = compile_run_vm(source);
    assert_eq!(vm.stack(), &[Value::Int(99)]);
    let drops = vm.drop_contract_event_count();
    assert!(
        drops > 0,
        "expected drop-contract events for locals alive at break, got {drops}"
    );
}

#[test]
fn continue_drops_remaining_dead_locals() {
    // A local constructed before `continue` should be cleaned up on each
    // skipped iteration.
    let source = r#"
        let mut sum = 0;
        let mut i = 0;
        while i < 5 {
            i = i + 1;
            let tmp = { v: i };
            if i == 3 {
                continue;
            }
            sum = sum + i;
        }
        sum;
    "#;
    let vm = compile_run_vm(source);
    // sum = 1 + 2 + 4 + 5 = 12 (skip i==3)
    assert_eq!(vm.stack(), &[Value::Int(12)]);
    let drops = vm.drop_contract_event_count();
    assert!(
        drops >= 5,
        "expected at least 5 drop events (one per iteration for tmp), got {drops}"
    );
}

// ---------------------------------------------------------------------------
// 9. reset_for_reuse locals-Null contract
// ---------------------------------------------------------------------------

#[test]
fn reset_for_reuse_clears_all_locals_to_null() {
    let source = r#"
        let a = { name: "first" };
        let b = [1, 2, 3];
        let c = "hello";
        null;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);

    vm.reset_for_reuse();
    assert_eq!(
        vm.drop_contract_event_count(),
        0,
        "reset_for_reuse should reset drop accounting for the next run"
    );

    // After reset, every local slot must be Null.
    for (i, local) in vm.locals().iter().enumerate() {
        assert_eq!(
            *local,
            Value::Null,
            "local slot {i} should be Null after reset_for_reuse, got {local:?}"
        );
    }
    // Stack must also be empty.
    assert!(
        vm.stack().is_empty(),
        "stack should be empty after reset_for_reuse, got {:?}",
        vm.stack()
    );
}

// ---------------------------------------------------------------------------
// 10. Host-op boundary: dead-local visibility and drop verification
// ---------------------------------------------------------------------------

#[test]
fn drop_events_across_host_op_hide_dead_local_before_wait() {
    // Dead local goes out of scope before a host-op wait. Verify both the
    // drop counter and that the local is hidden at the wait line.
    let source = r#"
        fn wait();
        let a = { tag: "before-wait" };
        let marker = 0;
        wait();
        marker;
    "#;

    let compiled = compile_source(source).expect("compile should succeed");
    let calls = Arc::new(AtomicUsize::new(0));
    let mut vm = new_drop_contract_vm(compiled.program);
    vm.register_function(Box::new(PendingOnce {
        call_count: Arc::clone(&calls),
        op_id: 900,
    }));

    // First run → Waiting; 'a' should already be dropped (dead before wait).
    let status = vm.run().expect("first run should wait");
    assert_eq!(status, VmStatus::Waiting(900));
    assert_eq!(
        vm.drop_contract_event_count(),
        3,
        "dead local should have triggered its recursive drop before the wait boundary"
    );
    assert!(
        !local_visible_at_current_line(&vm, "a"),
        "dead local should not remain visible while waiting at the host call"
    );

    // Complete and resume.
    vm.complete_host_op(900, Vec::new())
        .expect("complete should succeed");
    let status = vm.resume().expect("resume should halt");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(
        vm.drop_contract_event_count(),
        4,
        "resume should only account for the final live marker drop"
    );
    assert!(
        !local_visible_at_current_line(&vm, "a"),
        "dead local should remain hidden after resume"
    );
}

// ---------------------------------------------------------------------------
// 11. Tighter cooperative-yield double-drop bound
// ---------------------------------------------------------------------------

#[test]
fn cooperative_yield_drop_count_is_bounded_tightly() {
    // Same as the existing cooperative_yield test but with a tighter bound
    // to catch subtle double-drop regressions.
    let source = r#"
        let mut i = 0;
        while i < 10 {
            let tmp = { v: i };
            i = i + 1;
        }
        i;
    "#;

    let compiled = compile_source(source).expect("compile should succeed");
    let mut vm = new_drop_contract_vm(compiled.program);
    vm.set_fuel(10);

    let mut yields = 0u64;

    loop {
        let status = vm.run().expect("vm should run");
        match status {
            VmStatus::Halted => break,
            VmStatus::Yielded => {
                yields = yields.saturating_add(1);
                assert!(
                    yields < 4_096,
                    "low-fuel drop bound test made no progress after {yields} yields"
                );
                vm.recharge_fuel(10).expect("recharge should succeed");
            }
            VmStatus::Waiting(_) => panic!("unexpected waiting"),
        }
    }

    let drops = vm.drop_contract_event_count();
    // 10 iterations × 1 tmp map + scalars inside.  A reasonable upper bound
    // is 40 (accounting for map keys/values).  Much above that signals a bug.
    assert!(
        drops <= 50,
        "drop count ({drops}) exceeds tight bound of 50 — possible double-drop across yield"
    );
    assert!(
        drops >= 10,
        "expected at least 10 drop events (one per loop tmp), got {drops}"
    );
}

// ---------------------------------------------------------------------------
// 12. Overwrite of mutable local fires drop
// ---------------------------------------------------------------------------

#[test]
fn mutable_local_overwrite_drops_previous_and_nullifies() {
    // When a mutable local is reassigned, the previous value should be
    // dropped and the new value should be in place.
    let source = r#"
        let mut val = { first: [1, 2] };
        val = { second: [3] };
        val;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let val_index = debug.local_index("val").expect("val binding should exist");

    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    let drops = vm.drop_contract_event_count();
    assert!(
        drops > 0,
        "expected drop events for the first value of 'val' after overwrite, got {drops}"
    );
    // After halt, the local should still hold the second value (it was the TOS result
    // so it gets moved to stack, and the local is Null due to Ldloc semantics).
    // Verify either the stack has the right value or the local is Null.
    assert!(
        vm.locals()[val_index as usize] == Value::Null
            || matches!(&vm.locals()[val_index as usize], Value::Map(_)),
        "val should be Null (consumed) or still hold the second map, got {:?}",
        vm.locals()[val_index as usize]
    );
}

// ---------------------------------------------------------------------------
// 13. All locals Null after clean halt (program-level cleanup)
// ---------------------------------------------------------------------------

#[test]
fn all_locals_null_after_halt_for_simple_program() {
    // After a program halts, every local that was consumed during execution
    // or dropped by liveness should be Null.
    let source = r#"
        let a = "hello";
        let b = { x: 1 };
        let c = [1, 2, 3];
        let result = 42;
        result;
    "#;
    let compiled = compile_source(source).expect("compile should succeed");
    let debug = compiled
        .program
        .debug
        .as_ref()
        .expect("debug info should exist");
    let a_index = debug.local_index("a").expect("a should exist");
    let b_index = debug.local_index("b").expect("b should exist");
    let c_index = debug.local_index("c").expect("c should exist");

    let mut vm = new_drop_contract_vm(compiled.program);
    let status = vm.run().expect("vm should run");
    assert_eq!(status, VmStatus::Halted);
    assert_eq!(vm.stack(), &[Value::Int(42)]);

    // a, b, c are all dead after 'result' is computed — liveness should drop them.
    assert_eq!(
        vm.locals()[a_index as usize],
        Value::Null,
        "a should be Null"
    );
    assert_eq!(
        vm.locals()[b_index as usize],
        Value::Null,
        "b should be Null"
    );
    assert_eq!(
        vm.locals()[c_index as usize],
        Value::Null,
        "c should be Null"
    );
}