shape-vm 0.3.2

Stack-based bytecode virtual machine for the Shape programming language
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
//! Tests for v2 typed stack operations.
//!
//! Exercises the typed stack path end-to-end: raw native value push/pop
//! roundtrips, typed arithmetic and comparison opcodes, typed field access,
//! numeric coercion, large stack stress, and function call frames with
//! typed locals.

use crate::bytecode::*;
use crate::executor::{VMConfig, VirtualMachine};
use crate::type_tracking::{FrameDescriptor, NativeKind};
use shape_value::FunctionId;
use shape_value::VMError;

/// Helper: create a VM, load a program, execute, return the **raw u64 bits**
/// at the top of stack (ADR-006 ยง2.7.7 โ€” host-tier reads bits directly).
///
/// After Wave-E+5 producer-flip, hand-built programs that push raw native
/// bits (e.g. `Constant::Int(N)` -> raw `i64`, `Constant::Bool` -> raw bool)
/// expose those bits at the top of stack. The host inspects bits directly
/// against the program's declared `top_level_frame.return_kind`.
fn execute_program(program: BytecodeProgram) -> Result<u64, VMError> {
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(program);
    vm.execute_raw(None)
}

/// Helper: like `execute_program`, but stamps the program's
/// `top_level_frame.return_kind` for cross-checking the producing opcode's
/// declared kind.
fn execute_program_typed(
    mut program: BytecodeProgram,
    return_kind: NativeKind,
) -> Result<u64, VMError> {
    let mut frame = program.top_level_frame.unwrap_or_else(FrameDescriptor::new);
    // `FrameDescriptor.return_kind` is `Option<NativeKind>` per ADR-006
    // ยง2.7.8/Q10 (single-slot cell-storage shape โ€” `None` โ‰ก "kind not
    // stamped"). Test helpers always stamp a kind, so wrap in `Some`.
    frame.return_kind = Some(return_kind);
    program.top_level_frame = Some(frame);
    execute_program(program)
}

/// Decode raw u64 bits as `f64` (top-of-stack inspection helper).
#[inline]
fn bits_as_f64(bits: u64) -> f64 {
    f64::from_bits(bits)
}

/// Decode raw u64 bits as `i64` (top-of-stack inspection helper).
#[inline]
fn bits_as_i64(bits: u64) -> i64 {
    bits as i64
}

/// Decode raw u64 bits as `bool` (top-of-stack inspection helper). Per
/// ADR-006 ยง2.7 the bool-kind slot stores 0u64 / 1u64.
#[inline]
fn bits_as_bool(bits: u64) -> bool {
    bits != 0
}

// =========================================================================
// 1. Raw f64 push/pop roundtrip
// =========================================================================

#[test]
fn v2_stack_raw_f64_push_pop_roundtrip() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Number(3.14)],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    assert_eq!(bits, 3.14f64.to_bits(), "f64 bits must match exactly");
}

#[test]
fn v2_stack_raw_f64_special_values() {
    // Positive infinity
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Number(f64::INFINITY)],
        ..Default::default()
    };
    let bits = execute_program(program).unwrap();
    assert_eq!(bits_as_f64(bits), f64::INFINITY);

    // Negative zero
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Number(-0.0f64)],
        ..Default::default()
    };
    let bits = execute_program(program).unwrap();
    let f = bits_as_f64(bits);
    assert!(f.is_sign_negative() && f == 0.0, "negative zero must be preserved");
}

// =========================================================================
// 2. Raw i64 push/pop roundtrip
// =========================================================================

#[test]
fn v2_stack_raw_i64_push_pop_roundtrip() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Int(-42)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), -42, "i64 value must round-trip exactly");
}

#[test]
fn v2_stack_raw_i64_zero() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Int(0)],
        ..Default::default()
    };
    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 0);
}

#[test]
fn v2_stack_raw_i64_large_positive() {
    // i48 max range for inline: 2^47 - 1
    let val = (1i64 << 47) - 1;
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Int(val)],
        ..Default::default()
    };
    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), val);
}

// =========================================================================
// 3. Raw i32 push/pop roundtrip (via NativeScalar)
// =========================================================================

#[test]
fn v2_stack_raw_i32_push_pop_roundtrip() {
    // T1-host-tier-marshal-rebuild (R8, 2026-05-23): supervisor RULED
    // REFUSED on a NativeScalar carrier โ€” i32 is the existing
    // `NativeKind::Int32` scalar variant per ADR-006 ยง2.7.5 (no new
    // sum type, no Bool-default fallback). The slot stores the i32 in
    // the low 32 bits, sign-extended into i64 on read.
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let val: i32 = 12345;
    // i32 zero-extends into the low 32 bits of the slot; `as i32` on
    // read recovers the signed value.
    vm.push_kinded(val as u32 as u64, NativeKind::Int32).unwrap();
    let (bits, kind) = vm.pop_kinded().unwrap();
    assert_eq!(kind, NativeKind::Int32, "kind must be Int32");
    assert_eq!(bits as u32 as i32, val, "i32 value must round-trip exactly");
}

#[test]
fn v2_stack_raw_i32_negative() {
    // T1-host-tier-marshal-rebuild: same `NativeKind::Int32` carrier
    // as above for a negative value. Slot bits are the two's-complement
    // i32 pattern zero-extended; `as i32` reinterprets the low 32 bits.
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let val: i32 = -42;
    vm.push_kinded(val as u32 as u64, NativeKind::Int32).unwrap();
    let (bits, kind) = vm.pop_kinded().unwrap();
    assert_eq!(kind, NativeKind::Int32);
    assert_eq!(bits as u32 as i32, val, "negative i32 round-trip");
}

// =========================================================================
// 4. Raw bool push/pop
// =========================================================================

#[test]
fn v2_stack_raw_bool_push_pop_true() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Bool(true)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits));
}

#[test]
fn v2_stack_raw_bool_push_pop_false() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        ],
        constants: vec![Constant::Bool(false)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(!bits_as_bool(bits));
}

// =========================================================================
// 5. Raw pointer push/pop (via NativeScalar::Ptr)
// =========================================================================

#[test]
fn v2_stack_raw_pointer_push_pop() {
    // T1-host-tier-marshal-rebuild (R8, 2026-05-23): supervisor RULED
    // REFUSED on a NativeScalar carrier โ€” heap pointers flow through
    // `NativeKind::Ptr(HeapKind::*)` per ADR-006 ยง2.7.6 / Q8. Pick the
    // String arm: bits = `Arc::into_raw(Arc<HeapValue::String>)`.
    use shape_value::HeapKind;
    use shape_value::heap_value::HeapValue;
    use std::sync::Arc;

    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let arc = Arc::new(HeapValue::String(Arc::new("hello".to_string())));
    let bits_in = Arc::into_raw(arc) as u64;
    vm.push_kinded(bits_in, NativeKind::Ptr(HeapKind::String))
        .unwrap();

    let (bits, kind) = vm.pop_kinded().unwrap();
    assert_eq!(kind, NativeKind::Ptr(HeapKind::String));
    assert_eq!(bits, bits_in, "raw pointer bits must round-trip");

    // Retire the strong-count share so we don't leak โ€” pop_kinded
    // hands the owned share back to the caller per ADR-006 ยง2.7.6 / Q8.
    unsafe {
        let _ = Arc::from_raw(bits as *const HeapValue);
    }
}

#[test]
fn v2_stack_raw_pointer_null() {
    // T1-host-tier-marshal-rebuild: `NativeKind::Null` is the canonical
    // absence-of-value discriminator per ADR-006 ยง2.7 + ยง2.7.5 + ยง2.7.7
    // (R5b-2-bool-null-sentinel-cluster, 2026-05-19). Pre-disposition
    // null was `(0u64, NativeKind::Bool)` which collided with the
    // legitimate `false` bool slot; post-disposition the kind IS the
    // discriminator. A null slot pushes (0u64, Null) and pops the same
    // pair.
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    vm.push_kinded(0u64, NativeKind::Null).unwrap();
    let (bits, kind) = vm.pop_kinded().unwrap();
    assert_eq!(kind, NativeKind::Null);
    assert_eq!(bits, 0);
}

// =========================================================================
// 6. Mixed types on stack
// =========================================================================

#[test]
fn v2_stack_mixed_types_push_pop_order() {
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    // Push f64, i64, bool in order โ€” kinded API records each kind in
    // the parallel kinds track (ADR-006 ยง2.7.7).
    vm.push_kinded(2.718f64.to_bits(), NativeKind::Float64).unwrap();
    vm.push_kinded(42i64 as u64, NativeKind::Int64).unwrap();
    vm.push_kinded(1u64, NativeKind::Bool).unwrap();

    // Pop in reverse order: bool, i64, f64. Kind track confirms each
    // slot's interpretation.
    let (b_bits, b_kind) = vm.pop_kinded().unwrap();
    let (i_bits, i_kind) = vm.pop_kinded().unwrap();
    let (f_bits, f_kind) = vm.pop_kinded().unwrap();

    assert_eq!(b_kind, NativeKind::Bool, "first pop must be Bool");
    assert!(bits_as_bool(b_bits), "bool must be popped first");
    assert_eq!(i_kind, NativeKind::Int64, "second pop must be Int64");
    assert_eq!(bits_as_i64(i_bits), 42, "i64 must be popped second");
    assert_eq!(f_kind, NativeKind::Float64, "third pop must be Float64");
    assert_eq!(
        f_bits,
        2.718f64.to_bits(),
        "f64 must be popped third"
    );
}

#[test]
fn v2_stack_mixed_types_with_native_scalars() {
    // T1-host-tier-marshal-rebuild (R8, 2026-05-23): supervisor RULED
    // REFUSED on a NativeScalar carrier. The pre-W17 test mixed inline
    // scalars (f64, i64, bool) with NativeScalar-boxed I32 / Ptr; the
    // post-RULED shape uses the existing `NativeKind::Int32` and
    // `NativeKind::Ptr(HeapKind::String)` arms directly per ADR-006
    // ยง2.7.5 / ยง2.7.6 / Q8 โ€” no Bool-default, no new sum-type.
    use shape_value::HeapKind;
    use shape_value::heap_value::HeapValue;
    use std::sync::Arc;

    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let arc = Arc::new(HeapValue::String(Arc::new("hi".to_string())));
    let ptr_bits = Arc::into_raw(arc) as u64;

    // Push f64, i64, bool, i32, ptr โ€” five mixed types in order.
    vm.push_kinded(1.5f64.to_bits(), NativeKind::Float64).unwrap();
    vm.push_kinded(99i64 as u64, NativeKind::Int64).unwrap();
    vm.push_kinded(1u64, NativeKind::Bool).unwrap();
    vm.push_kinded((-7i32) as u32 as u64, NativeKind::Int32).unwrap();
    vm.push_kinded(ptr_bits, NativeKind::Ptr(HeapKind::String))
        .unwrap();

    // Pop in reverse order.
    let (top_bits, top_kind) = vm.pop_kinded().unwrap();
    assert_eq!(top_kind, NativeKind::Ptr(HeapKind::String));
    assert_eq!(top_bits, ptr_bits);
    // Retire the pointer share.
    unsafe {
        let _ = Arc::from_raw(top_bits as *const HeapValue);
    }

    let (i32_bits, i32_kind) = vm.pop_kinded().unwrap();
    assert_eq!(i32_kind, NativeKind::Int32);
    assert_eq!(i32_bits as u32 as i32, -7);

    let (b_bits, b_kind) = vm.pop_kinded().unwrap();
    assert_eq!(b_kind, NativeKind::Bool);
    assert!(bits_as_bool(b_bits));

    let (i_bits, i_kind) = vm.pop_kinded().unwrap();
    assert_eq!(i_kind, NativeKind::Int64);
    assert_eq!(bits_as_i64(i_bits), 99);

    let (f_bits, f_kind) = vm.pop_kinded().unwrap();
    assert_eq!(f_kind, NativeKind::Float64);
    assert_eq!(bits_as_f64(f_bits), 1.5);
}

// =========================================================================
// 7. Typed arithmetic via opcodes (AddInt)
// =========================================================================

#[test]
fn v2_stack_typed_add_int() {
    // 10 + 20 = 30 via AddInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::AddInt),
        ],
        constants: vec![Constant::Int(10), Constant::Int(20)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 30, "AddInt must produce raw i64 result");
}

#[test]
fn v2_stack_typed_sub_int() {
    // 100 - 37 = 63 via SubInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::SubInt),
        ],
        constants: vec![Constant::Int(100), Constant::Int(37)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 63);
}

#[test]
fn v2_stack_typed_mul_int() {
    // 6 * 7 = 42 via MulInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::MulInt),
        ],
        constants: vec![Constant::Int(6), Constant::Int(7)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 42);
}

#[test]
fn v2_stack_typed_add_number() {
    // 1.5 + 2.5 = 4.0 via AddNumber
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::AddNumber),
        ],
        constants: vec![Constant::Number(1.5), Constant::Number(2.5)],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    assert_eq!(bits_as_f64(bits), 4.0);
}

#[test]
fn v2_stack_typed_mul_number() {
    // 3.0 * 4.0 = 12.0 via MulNumber
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::MulNumber),
        ],
        constants: vec![Constant::Number(3.0), Constant::Number(4.0)],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    assert_eq!(bits_as_f64(bits), 12.0);
}

// =========================================================================
// 8. Typed comparison via opcodes (GtNumber, GtInt, LtInt, EqInt)
// =========================================================================

#[test]
fn v2_stack_typed_gt_number_true() {
    // 5.0 > 3.0 = true via GtNumber
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::GtNumber),
        ],
        constants: vec![Constant::Number(5.0), Constant::Number(3.0)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits), "GtNumber 5.0>3.0 must produce raw bool true");
}

#[test]
fn v2_stack_typed_gt_number_false() {
    // 2.0 > 3.0 = false
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::GtNumber),
        ],
        constants: vec![Constant::Number(2.0), Constant::Number(3.0)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(!bits_as_bool(bits));
}

#[test]
fn v2_stack_typed_gt_int() {
    // 10 > 5 = true via GtInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::GtInt),
        ],
        constants: vec![Constant::Int(10), Constant::Int(5)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits));
}

#[test]
fn v2_stack_typed_lt_int() {
    // 3 < 7 = true via LtInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::LtInt),
        ],
        constants: vec![Constant::Int(3), Constant::Int(7)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits));
}

#[test]
fn v2_stack_typed_eq_int() {
    // 42 == 42 via EqInt
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),
            Instruction::simple(OpCode::EqInt),
        ],
        constants: vec![Constant::Int(42), Constant::Int(42)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits));
}

// =========================================================================
// 9. Typed field load (GetFieldTyped on a TypedObject)
// =========================================================================

#[test]
fn v2_stack_typed_field_load_f64() {
    use crate::executor::typed_object_ops::FIELD_TAG_F64;

    // Create a TypedObject with a single f64 field, then load it via GetFieldTyped.
    let mut program = BytecodeProgram::default();
    let schema_id = program.type_schema_registry.register_type(
        "__v2_test_point_x",
        vec![("x".to_string(), shape_runtime::type_schema::FieldType::F64)],
    );
    let schema_u16 = u16::try_from(schema_id).expect("schema id fits u16");

    program.instructions = vec![
        // Push the f64 field value onto the stack
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        // Create the typed object (pops 1 field value, pushes object)
        Instruction::new(
            OpCode::NewTypedObject,
            Some(Operand::TypedObjectAlloc {
                schema_id: schema_u16,
                field_count: 1,
            }),
        ),
        // Load the field back out
        Instruction::new(
            OpCode::GetFieldTyped,
            Some(Operand::TypedField {
                type_id: schema_u16,
                field_idx: 0,
                field_type_tag: FIELD_TAG_F64,
            }),
        ),
    ];
    program.constants = vec![Constant::Number(99.99)];

    let bits = execute_program(program).unwrap();
    assert_eq!(
        bits_as_f64(bits),
        99.99,
        "GetFieldTyped must extract f64 field value"
    );
}

#[test]
fn v2_stack_typed_field_load_i64() {
    use crate::executor::typed_object_ops::FIELD_TAG_I64;

    let mut program = BytecodeProgram::default();
    let schema_id = program.type_schema_registry.register_type(
        "__v2_test_counter",
        vec![("count".to_string(), shape_runtime::type_schema::FieldType::I64)],
    );
    let schema_u16 = u16::try_from(schema_id).expect("schema id fits u16");

    program.instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(
            OpCode::NewTypedObject,
            Some(Operand::TypedObjectAlloc {
                schema_id: schema_u16,
                field_count: 1,
            }),
        ),
        Instruction::new(
            OpCode::GetFieldTyped,
            Some(Operand::TypedField {
                type_id: schema_u16,
                field_idx: 0,
                field_type_tag: FIELD_TAG_I64,
            }),
        ),
    ];
    program.constants = vec![Constant::Int(12345)];

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 12345);
}

#[test]
fn v2_stack_typed_field_load_bool() {
    use crate::executor::typed_object_ops::FIELD_TAG_BOOL;

    let mut program = BytecodeProgram::default();
    let schema_id = program.type_schema_registry.register_type(
        "__v2_test_flag",
        vec![("active".to_string(), shape_runtime::type_schema::FieldType::Bool)],
    );
    let schema_u16 = u16::try_from(schema_id).expect("schema id fits u16");

    program.instructions = vec![
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
        Instruction::new(
            OpCode::NewTypedObject,
            Some(Operand::TypedObjectAlloc {
                schema_id: schema_u16,
                field_count: 1,
            }),
        ),
        Instruction::new(
            OpCode::GetFieldTyped,
            Some(Operand::TypedField {
                type_id: schema_u16,
                field_idx: 0,
                field_type_tag: FIELD_TAG_BOOL,
            }),
        ),
    ];
    program.constants = vec![Constant::Bool(true)];

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits));
}

#[test]
fn v2_stack_typed_field_load_multi_field() {
    use crate::executor::typed_object_ops::{FIELD_TAG_F64, FIELD_TAG_I64};

    // Two-field struct: { x: number, y: int }
    let mut program = BytecodeProgram::default();
    let schema_id = program.type_schema_registry.register_type(
        "__v2_test_point2d",
        vec![
            ("x".to_string(), shape_runtime::type_schema::FieldType::F64),
            ("y".to_string(), shape_runtime::type_schema::FieldType::I64),
        ],
    );
    let schema_u16 = u16::try_from(schema_id).expect("schema id fits u16");

    program.instructions = vec![
        // Push fields in order: x, y
        Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // x = 3.14
        Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // y = 7
        Instruction::new(
            OpCode::NewTypedObject,
            Some(Operand::TypedObjectAlloc {
                schema_id: schema_u16,
                field_count: 2,
            }),
        ),
        // Dup the object so we can access both fields
        Instruction::simple(OpCode::Dup),
        // Load field 0 (x: f64)
        Instruction::new(
            OpCode::GetFieldTyped,
            Some(Operand::TypedField {
                type_id: schema_u16,
                field_idx: 0,
                field_type_tag: FIELD_TAG_F64,
            }),
        ),
        // Swap so the object is on top again
        Instruction::simple(OpCode::Swap),
        // Load field 1 (y: i64)
        Instruction::new(
            OpCode::GetFieldTyped,
            Some(Operand::TypedField {
                type_id: schema_u16,
                field_idx: 1,
                field_type_tag: FIELD_TAG_I64,
            }),
        ),
        // Now stack is: [x_value, y_value] -- add them as numbers via AddNumber
        // First convert y (i64) to number
        Instruction::simple(OpCode::IntToNumber),
        Instruction::simple(OpCode::AddNumber),
    ];
    program.constants = vec![Constant::Number(3.14), Constant::Int(7)];

    let bits = execute_program(program).unwrap();
    let got = bits_as_f64(bits);
    let expected = 3.14 + 7.0;
    assert!(
        (got - expected).abs() < 1e-10,
        "expected {}, got {}",
        expected,
        got,
    );
}

// =========================================================================
// 10. IntToNumber conversion
// =========================================================================

#[test]
fn v2_stack_int_to_number_conversion() {
    // Push i64, convert to f64 via IntToNumber, verify result
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::simple(OpCode::IntToNumber),
        ],
        constants: vec![Constant::Int(42)],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    assert_eq!(bits_as_f64(bits), 42.0, "IntToNumber must convert i64 to f64");
}

#[test]
fn v2_stack_int_to_number_negative() {
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::simple(OpCode::IntToNumber),
        ],
        constants: vec![Constant::Int(-1000)],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    assert_eq!(bits_as_f64(bits), -1000.0);
}

#[test]
fn v2_stack_number_to_int_conversion() {
    // Push f64, convert to i64 via NumberToInt, verify result
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::simple(OpCode::NumberToInt),
        ],
        constants: vec![Constant::Number(7.9)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    // NumberToInt truncates toward zero (Rust `as i64` semantics)
    assert_eq!(bits_as_i64(bits), 7, "NumberToInt must truncate 7.9 to 7");
}

// =========================================================================
// 11. Large stack test
// =========================================================================

#[test]
fn v2_stack_large_push_pop_1000() {
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let count = 1000usize;

    // Push 1000 f64 values via the kinded API (ADR-006 ยง2.7.7).
    for i in 0..count {
        let f = i as f64 * 1.1;
        vm.push_kinded(f.to_bits(), NativeKind::Float64).unwrap();
    }

    // Pop all 1000 in reverse order and verify
    for i in (0..count).rev() {
        let (bits, kind) = vm.pop_kinded().unwrap();
        assert_eq!(kind, NativeKind::Float64, "slot {} kind mismatch", i);
        let expected = i as f64 * 1.1;
        assert_eq!(
            bits,
            expected.to_bits(),
            "stack slot {} mismatch: expected {}, got {}",
            i,
            expected,
            bits_as_f64(bits),
        );
    }
}

#[test]
fn v2_stack_large_mixed_types_500() {
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    // Push alternating f64 and i64 values via the kinded API
    // (ADR-006 ยง2.7.7).
    for i in 0..500usize {
        if i % 2 == 0 {
            vm.push_kinded((i as f64).to_bits(), NativeKind::Float64).unwrap();
        } else {
            vm.push_kinded(i as u64, NativeKind::Int64).unwrap();
        }
    }

    // Pop in reverse and verify kind track matches the producing kind.
    for i in (0..500usize).rev() {
        let (bits, kind) = vm.pop_kinded().unwrap();
        if i % 2 == 0 {
            assert_eq!(kind, NativeKind::Float64, "slot {} expected Float64", i);
            assert_eq!(bits_as_f64(bits), i as f64, "f64 mismatch at index {}", i);
        } else {
            assert_eq!(kind, NativeKind::Int64, "slot {} expected Int64", i);
            assert_eq!(bits_as_i64(bits), i as i64, "i64 mismatch at index {}", i);
        }
    }
}

// =========================================================================
// 12. Stack frame with raw locals
// =========================================================================

#[test]
fn v2_stack_frame_with_typed_locals() {
    // Simulate a function call with typed locals using StoreLocal/LoadLocal.
    //
    // Bytecode layout:
    //   0: Call func_0 with 0 args  (sets up call frame with 3 locals)
    //   1: (after return, result is on stack)
    //
    //   func_0 (entry_point=2):
    //     2: PushConst 0  (3.14)
    //     3: StoreLocal 0          -- local[0] = 3.14 (f64)
    //     4: PushConst 1  (42)
    //     5: StoreLocal 1          -- local[1] = 42 (i64)
    //     6: PushConst 2  (true)
    //     7: StoreLocal 2          -- local[2] = true (bool)
    //     8: LoadLocal 0           -- push local[0] (f64)
    //     9: LoadLocal 1           -- push local[1] (i64)
    //    10: IntToNumber            -- convert i64 to f64
    //    11: AddNumber              -- 3.14 + 42.0
    //    12: ReturnValue

    // Layout:
    //   0: PushConst(3)   -- push arg count (0.0)
    //   1: Call func_0    -- calls function, return_ip = 2
    //   2: Jump +11       -- skip over function body (ip after dispatch = 3, 3+11=14 = past end)
    //
    //   func_0 body (entry_point = 3):
    //   3: PushConst 0    -- 3.14
    //   4: StoreLocal 0
    //   5: PushConst 1    -- 42
    //   6: StoreLocal 1
    //   7: PushConst 2    -- true
    //   8: StoreLocal 2
    //   9: LoadLocal 0    -- f64
    //  10: LoadLocal 1    -- i64
    //  11: IntToNumber
    //  12: AddNumber      -- 3.14 + 42.0
    //  13: ReturnValue
    let program = BytecodeProgram {
        instructions: vec![
            // Main code
            Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),          // 0: arg count
            Instruction::new(OpCode::Call, Some(Operand::Function(FunctionId(0)))), // 1
            Instruction::new(OpCode::Jump, Some(Operand::Offset(11))),             // 2: skip func body
            // Function body (entry_point = 3)
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),          // 3: 3.14
            Instruction::new(OpCode::StoreLocal, Some(Operand::Local(0))),         // 4
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),          // 5: 42
            Instruction::new(OpCode::StoreLocal, Some(Operand::Local(1))),         // 6
            Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),          // 7: true
            Instruction::new(OpCode::StoreLocal, Some(Operand::Local(2))),         // 8
            Instruction::new(OpCode::LoadLocal, Some(Operand::Local(0))),          // 9: load f64
            Instruction::new(OpCode::LoadLocal, Some(Operand::Local(1))),          // 10: load i64
            Instruction::simple(OpCode::IntToNumber),                              // 11
            Instruction::simple(OpCode::AddNumber),                                // 12: 3.14 + 42.0
            Instruction::simple(OpCode::ReturnValue),                              // 13
        ],
        constants: vec![
            Constant::Number(3.14),  // 0
            Constant::Int(42),       // 1
            Constant::Bool(true),    // 2
            Constant::Number(0.0),   // 3 -- arg count
        ],
        functions: vec![Function {
            name: "__v2_test_locals".to_string(),
            arity: 0,
            param_names: vec![],
            locals_count: 3,
            entry_point: 3,
            body_length: 11,
            is_closure: false,
            captures_count: 0,
            is_async: false,
            ref_params: vec![],
            ref_mutates: vec![],
            mutable_captures: vec![],
            frame_descriptor: None,
            osr_entry_points: vec![],
            mir_data: None,
        }],
        ..Default::default()
    };

    let bits = execute_program(program).unwrap();
    let got = bits_as_f64(bits);
    let expected = 3.14 + 42.0;
    assert!(
        (got - expected).abs() < 1e-10,
        "function with typed locals: expected {}, got {}",
        expected,
        got,
    );
}

#[test]
fn v2_stack_frame_locals_isolation() {
    // Verify that locals in a function frame don't corrupt the caller's stack.
    //
    // Push a sentinel value, call function that writes to locals, return,
    // verify sentinel is still intact.
    //
    //   0: PushConst 0  (sentinel = 999)
    //   1: Call func_0 with 0 args
    //   2: Pop (discard function result)
    //   -- sentinel remains on stack as final result
    //
    //   func_0 (entry_point=3):
    //     3: PushConst 1 (77)
    //     4: StoreLocal 0
    //     5: PushConst 2 (-1)
    //     6: StoreLocal 1
    //     7: LoadLocal 0
    //     8: ReturnValue

    // Layout:
    //   0: PushConst(0)   -- sentinel = 999
    //   1: PushConst(3)   -- arg count = 0.0
    //   2: Call func_0    -- return_ip = 3
    //   3: Pop            -- discard function result (77), sentinel remains
    //   4: Jump +6        -- skip function body (ip after dispatch = 5, 5+6 = 11 = past end)
    //
    //   func_0 body (entry_point = 5):
    //   5: PushConst 1    -- 77
    //   6: StoreLocal 0
    //   7: PushConst 2    -- -1
    //   8: StoreLocal 1
    //   9: LoadLocal 0    -- push 77
    //  10: ReturnValue
    let program = BytecodeProgram {
        instructions: vec![
            // Main code
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),          // 0: sentinel
            Instruction::new(OpCode::PushConst, Some(Operand::Const(3))),          // 1: arg count
            Instruction::new(OpCode::Call, Some(Operand::Function(FunctionId(0)))), // 2
            Instruction::simple(OpCode::Pop),                                      // 3: discard result
            Instruction::new(OpCode::Jump, Some(Operand::Offset(6))),              // 4: skip func body
            // Function body (entry_point = 5)
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))),          // 5: 77
            Instruction::new(OpCode::StoreLocal, Some(Operand::Local(0))),         // 6
            Instruction::new(OpCode::PushConst, Some(Operand::Const(2))),          // 7: -1
            Instruction::new(OpCode::StoreLocal, Some(Operand::Local(1))),         // 8
            Instruction::new(OpCode::LoadLocal, Some(Operand::Local(0))),          // 9: push 77
            Instruction::simple(OpCode::ReturnValue),                              // 10
        ],
        constants: vec![
            Constant::Int(999),    // 0: sentinel
            Constant::Int(77),     // 1: function local value
            Constant::Int(-1),     // 2: second local value
            Constant::Number(0.0), // 3: arg count
        ],
        functions: vec![Function {
            name: "__v2_test_isolation".to_string(),
            arity: 0,
            param_names: vec![],
            locals_count: 2,
            entry_point: 5,
            body_length: 6,
            is_closure: false,
            captures_count: 0,
            is_async: false,
            ref_params: vec![],
            ref_mutates: vec![],
            mutable_captures: vec![],
            frame_descriptor: None,
            osr_entry_points: vec![],
            mir_data: None,
        }],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(
        bits_as_i64(bits),
        999,
        "sentinel value must survive function call frame"
    );
}

// =========================================================================
// Additional edge case tests
// =========================================================================

#[test]
fn v2_stack_underflow_on_empty() {
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    let err = vm.pop_kinded().unwrap_err();
    assert!(
        matches!(err, VMError::StackUnderflow),
        "pop on empty stack must return StackUnderflow, got {:?}",
        err
    );
}

#[test]
fn v2_stack_dup_preserves_type() {
    // Verify Dup works correctly with typed values
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))),
            Instruction::simple(OpCode::Dup),
            // Stack: [42, 42] -- add them
            Instruction::simple(OpCode::AddInt),
        ],
        constants: vec![Constant::Int(42)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 84, "Dup + AddInt: 42 + 42 = 84");
}

#[test]
fn v2_stack_swap_preserves_types() {
    // Push i64 then f64, swap, verify order after pop. Kind track
    // tracks each slot's type in lockstep (ADR-006 ยง2.7.7).
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(BytecodeProgram::default());

    vm.push_kinded(10i64 as u64, NativeKind::Int64).unwrap();
    vm.push_kinded(2.5f64.to_bits(), NativeKind::Float64).unwrap();

    // Manually swap: pop both with kind, then push back in reversed order.
    let (b_bits, b_kind) = vm.pop_kinded().unwrap(); // 2.5 / Float64
    let (a_bits, a_kind) = vm.pop_kinded().unwrap(); // 10  / Int64
    vm.push_kinded(b_bits, b_kind).unwrap();
    vm.push_kinded(a_bits, a_kind).unwrap();

    // Now top should be i64(10), beneath is f64(2.5)
    let (top_bits, top_kind) = vm.pop_kinded().unwrap();
    assert_eq!(top_kind, NativeKind::Int64);
    assert_eq!(bits_as_i64(top_bits), 10);
    let (bot_bits, bot_kind) = vm.pop_kinded().unwrap();
    assert_eq!(bot_kind, NativeKind::Float64);
    assert_eq!(bits_as_f64(bot_bits), 2.5);
}

#[test]
fn v2_stack_chained_typed_arithmetic() {
    // Compute (10 + 20) * 3 = 90 using typed int opcodes
    let program = BytecodeProgram {
        instructions: vec![
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // 10
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // 20
            Instruction::simple(OpCode::AddInt),                          // 30
            Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 3
            Instruction::simple(OpCode::MulInt),                          // 90
        ],
        constants: vec![Constant::Int(10), Constant::Int(20), Constant::Int(3)],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Int64).unwrap();
    assert_eq!(bits_as_i64(bits), 90);
}

#[test]
fn v2_stack_typed_comparison_chain() {
    // (5 > 3) == true, then use the bool result
    let program = BytecodeProgram {
        instructions: vec![
            // First comparison: 5 > 3 = true
            Instruction::new(OpCode::PushConst, Some(Operand::Const(0))), // 5.0
            Instruction::new(OpCode::PushConst, Some(Operand::Const(1))), // 3.0
            Instruction::simple(OpCode::GtNumber),
            // Second comparison: 1 < 2 = true
            Instruction::new(OpCode::PushConst, Some(Operand::Const(2))), // 1
            Instruction::new(OpCode::PushConst, Some(Operand::Const(3))), // 2
            Instruction::simple(OpCode::LtInt),
            // Both should be true; AND them via logical And
            Instruction::simple(OpCode::And),
        ],
        constants: vec![
            Constant::Number(5.0),
            Constant::Number(3.0),
            Constant::Int(1),
            Constant::Int(2),
        ],
        ..Default::default()
    };

    let bits = execute_program_typed(program, NativeKind::Bool).unwrap();
    assert!(bits_as_bool(bits), "chain: (5>3) AND (1<2) must be true");
}