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
897
898
use std::collections::{HashMap, hash_map};
use std::fmt;
use std::hash::{BuildHasherDefault, Hash, Hasher};
use std::sync::{Arc, OnceLock};

use crate::compiler::TypeSchema;

pub type SharedString = Arc<String>;
pub type SharedBytes = Arc<Vec<u8>>;
pub type SharedArray = Arc<Vec<Value>>;
pub type SharedMap = Arc<VmMap>;

type VmMapStorage = HashMap<MapKey, Value, BuildHasherDefault<StableHasher>>;

/// Runtime map storage for VM values.
///
/// Keys and values may be any runtime [`Value`]. Key equality is hybrid:
/// scalars, strings, and bytes compare by value, while arrays and maps compare by
/// heap-object identity. Float keys use canonicalized IEEE bits: `0.0` /
/// `-0.0` are treated as the same key, while `NaN` keys only compare equal
/// when their bit patterns match. Duplicate inserts overwrite the prior value
/// for the same key.
///
/// Heap-backed keys remain stable after insertion. Values are reference-counted
/// and container writes detach before mutation, so later writes through an
/// alias create a new heap object instead of mutating a key already stored in
/// the map.
#[derive(Clone, Default)]
pub struct VmMap {
    entries: VmMapStorage,
    cached_len: usize,
}

#[derive(Clone, Debug)]
struct MapKey(Value);

pub struct VmMapIter<'a> {
    inner: hash_map::Iter<'a, MapKey, Value>,
}

pub struct VmMapIntoIter {
    inner: hash_map::IntoIter<MapKey, Value>,
}

#[derive(Default)]
pub(crate) struct StableHasher(u64);

impl Hasher for StableHasher {
    fn finish(&self) -> u64 {
        self.0
    }

    fn write(&mut self, bytes: &[u8]) {
        const OFFSET_BASIS: u64 = 0xcbf29ce484222325;
        const PRIME: u64 = 0x100000001b3;

        if self.0 == 0 {
            self.0 = OFFSET_BASIS;
        }
        for byte in bytes {
            self.0 ^= u64::from(*byte);
            self.0 = self.0.wrapping_mul(PRIME);
        }
    }
}

impl VmMap {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_entries(entries: Vec<(Value, Value)>) -> Self {
        let mut out = Self::new();
        for (key, value) in entries {
            out.insert(key, value);
        }
        out
    }

    pub fn len(&self) -> usize {
        debug_assert_eq!(self.cached_len, self.entries.len());
        self.cached_len
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn iter(&self) -> VmMapIter<'_> {
        VmMapIter {
            inner: self.entries.iter(),
        }
    }

    pub fn get(&self, key: &Value) -> Option<&Value> {
        self.entries.get(&MapKey::new(key.clone()))
    }

    pub fn insert(&mut self, key: Value, value: Value) -> Option<Value> {
        let replaced = self.entries.insert(MapKey::new(key), value);
        self.cached_len = self.entries.len();
        replaced
    }

    pub fn remove(&mut self, key: &Value) -> Option<Value> {
        let removed = self.entries.remove(&MapKey::new(key.clone()));
        self.cached_len = self.entries.len();
        removed
    }
}

#[allow(dead_code)]
pub(crate) fn vm_map_len_field_offset() -> usize {
    std::mem::offset_of!(VmMap, cached_len)
}

impl From<Vec<(Value, Value)>> for VmMap {
    fn from(value: Vec<(Value, Value)>) -> Self {
        Self::from_entries(value)
    }
}

impl IntoIterator for VmMap {
    type Item = (Value, Value);
    type IntoIter = VmMapIntoIter;

    fn into_iter(self) -> Self::IntoIter {
        VmMapIntoIter {
            inner: self.entries.into_iter(),
        }
    }
}

impl<'a> IntoIterator for &'a VmMap {
    type Item = (&'a Value, &'a Value);
    type IntoIter = VmMapIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl fmt::Debug for VmMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl PartialEq for VmMap {
    fn eq(&self, other: &Self) -> bool {
        self.entries == other.entries
    }
}

impl Eq for VmMap {}

impl MapKey {
    fn new(value: Value) -> Self {
        Self(value)
    }

    fn value(&self) -> &Value {
        &self.0
    }

    fn into_value(self) -> Value {
        self.0
    }
}

impl PartialEq for MapKey {
    fn eq(&self, other: &Self) -> bool {
        map_key_eq(&self.0, &other.0)
    }
}

impl Eq for MapKey {}

impl Hash for MapKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        hash_map_key(&self.0, state);
    }
}

impl<'a> Iterator for VmMapIter<'a> {
    type Item = (&'a Value, &'a Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|(key, value)| (key.value(), value))
    }
}

impl Iterator for VmMapIntoIter {
    type Item = (Value, Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .next()
            .map(|(key, value)| (key.into_value(), value))
    }
}

fn hash_map_key(value: &Value, state: &mut impl Hasher) {
    match value {
        Value::Null => {
            6u8.hash(state);
        }
        Value::Int(value) => {
            0u8.hash(state);
            value.hash(state);
        }
        Value::Float(value) => {
            1u8.hash(state);
            canonical_float_key_bits(*value).hash(state);
        }
        Value::Bool(value) => {
            2u8.hash(state);
            value.hash(state);
        }
        Value::String(value) => {
            3u8.hash(state);
            value.hash(state);
        }
        Value::Bytes(value) => {
            4u8.hash(state);
            value.hash(state);
        }
        Value::Array(values) => {
            5u8.hash(state);
            Arc::as_ptr(values).hash(state);
        }
        Value::Map(entries) => {
            6u8.hash(state);
            Arc::as_ptr(entries).hash(state);
        }
    }
}

fn map_key_eq(lhs: &Value, rhs: &Value) -> bool {
    match (lhs, rhs) {
        (Value::Null, Value::Null) => true,
        (Value::Int(lhs), Value::Int(rhs)) => lhs == rhs,
        (Value::Float(lhs), Value::Float(rhs)) => {
            canonical_float_key_bits(*lhs) == canonical_float_key_bits(*rhs)
        }
        (Value::Bool(lhs), Value::Bool(rhs)) => lhs == rhs,
        (Value::String(lhs), Value::String(rhs)) => lhs == rhs,
        (Value::Bytes(lhs), Value::Bytes(rhs)) => lhs == rhs,
        (Value::Array(lhs), Value::Array(rhs)) => Arc::ptr_eq(lhs, rhs),
        (Value::Map(lhs), Value::Map(rhs)) => Arc::ptr_eq(lhs, rhs),
        _ => false,
    }
}

/// Hash a value structurally for VM-internal cache keys.
///
/// The hasher itself is a small deterministic 64-bit FNV-1a-style accumulator.
/// Arrays hash recursively in order and maps hash recursively without caring
/// about entry order, so the result is stable across allocations.
#[allow(dead_code)]
pub(crate) fn hash_value(value: &Value, state: &mut impl Hasher) {
    match value {
        Value::Null => {
            6u8.hash(state);
        }
        Value::Int(value) => {
            0u8.hash(state);
            value.hash(state);
        }
        Value::Float(value) => {
            1u8.hash(state);
            canonical_float_key_bits(*value).hash(state);
        }
        Value::Bool(value) => {
            2u8.hash(state);
            value.hash(state);
        }
        Value::String(value) => {
            3u8.hash(state);
            value.hash(state);
        }
        Value::Bytes(value) => {
            4u8.hash(state);
            value.hash(state);
        }
        Value::Array(values) => {
            5u8.hash(state);
            values.len().hash(state);
            for value in values.iter() {
                hash_value(value, state);
            }
        }
        Value::Map(entries) => {
            6u8.hash(state);
            entries.len().hash(state);
            let mut entry_hashes = entries
                .iter()
                .map(|(key, value)| {
                    let mut entry_hasher = StableHasher::default();
                    hash_value(key, &mut entry_hasher);
                    hash_value(value, &mut entry_hasher);
                    entry_hasher.finish()
                })
                .collect::<Vec<_>>();
            entry_hashes.sort_unstable();
            for entry_hash in entry_hashes {
                entry_hash.hash(state);
            }
        }
    }
}

fn canonical_float_key_bits(value: f64) -> u64 {
    if value == 0.0 {
        0.0f64.to_bits()
    } else {
        value.to_bits()
    }
}

#[derive(Clone, Debug)]
pub enum Value {
    Null,
    Int(i64),
    Float(f64),
    Bool(bool),
    String(SharedString),
    Bytes(SharedBytes),
    Array(SharedArray),
    Map(SharedMap),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ValueType {
    Unknown = 0,
    Null = 1,
    Int = 2,
    Float = 3,
    Bool = 4,
    String = 5,
    Bytes = 6,
    Array = 7,
    Map = 8,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TypeMap {
    pub strict_types: bool,
    pub local_types: Vec<ValueType>,
    pub local_schemas: Vec<Option<TypeSchema>>,
    pub callable_slots: Vec<bool>,
    pub optional_slots: Vec<bool>,
    pub operand_types: HashMap<usize, (ValueType, ValueType)>,
}

impl Value {
    pub fn string(value: impl Into<String>) -> Self {
        Self::String(Arc::new(value.into()))
    }

    pub fn array(values: Vec<Value>) -> Self {
        Self::Array(Arc::new(values))
    }

    pub fn bytes(value: impl Into<Vec<u8>>) -> Self {
        Self::Bytes(Arc::new(value.into()))
    }

    pub fn map(entries: Vec<(Value, Value)>) -> Self {
        Self::Map(Arc::new(VmMap::from(entries)))
    }

    pub fn into_owned_string(self) -> Result<String, Self> {
        match self {
            Self::String(value) => Ok(unwrap_or_clone_shared(value)),
            other => Err(other),
        }
    }

    pub fn into_owned_array(self) -> Result<Vec<Value>, Self> {
        match self {
            Self::Array(values) => Ok(unwrap_or_clone_shared(values)),
            other => Err(other),
        }
    }

    pub fn into_owned_bytes(self) -> Result<Vec<u8>, Self> {
        match self {
            Self::Bytes(value) => Ok(unwrap_or_clone_shared(value)),
            other => Err(other),
        }
    }

    pub fn into_owned_map(self) -> Result<VmMap, Self> {
        match self {
            Self::Map(entries) => Ok(unwrap_or_clone_shared(entries)),
            other => Err(other),
        }
    }
}

pub(crate) fn unwrap_or_clone_shared<T: Clone>(value: Arc<T>) -> T {
    match Arc::try_unwrap(value) {
        Ok(inner) => inner,
        Err(shared) => (*shared).clone(),
    }
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Null, Self::Null) => true,
            (Self::Int(lhs), Self::Int(rhs)) => lhs == rhs,
            (Self::Float(lhs), Self::Float(rhs)) => lhs == rhs,
            (Self::Bool(lhs), Self::Bool(rhs)) => lhs == rhs,
            (Self::String(lhs), Self::String(rhs)) => lhs == rhs,
            (Self::Bytes(lhs), Self::Bytes(rhs)) => lhs == rhs,
            (Self::Array(lhs), Self::Array(rhs)) => lhs == rhs,
            (Self::Map(lhs), Self::Map(rhs)) => lhs == rhs,
            _ => false,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct HostImport {
    pub name: String,
    pub arity: u8,
    pub return_type: ValueType,
}

#[allow(dead_code)]
#[derive(Debug)]
pub(crate) struct DecodedInstructionData {
    pub(crate) ldc_values: Box<[Option<Value>]>,
    pub(crate) jump_targets: Box<[Option<usize>]>,
    pub(crate) local_indices: Box<[Option<u8>]>,
}

impl DecodedInstructionData {
    fn build(program: &Program) -> Self {
        let mut ldc_values = vec![None; program.code.len()];
        let mut jump_targets = vec![None; program.code.len()];
        let mut local_indices = vec![None; program.code.len()];
        let mut ip = 0usize;
        while ip < program.code.len() {
            let opcode = match OpCode::try_from(program.code[ip]) {
                Ok(opcode) => opcode,
                Err(_) => break,
            };
            match opcode {
                OpCode::Ldc => {
                    if let Some(raw_index) = read_u32_at(&program.code, ip + 1)
                        && let Some(value) = program.constants.get(raw_index as usize)
                    {
                        ldc_values[ip] = Some(value.clone());
                    }
                }
                OpCode::Br | OpCode::Brfalse => {
                    if let Some(target) = read_u32_at(&program.code, ip + 1) {
                        jump_targets[ip] = Some(target as usize);
                    }
                }
                OpCode::Ldloc | OpCode::Stloc => {
                    if let Some(index) = program.code.get(ip + 1).copied() {
                        local_indices[ip] = Some(index);
                    }
                }
                _ => {}
            }
            ip = ip.saturating_add(1 + opcode.operand_len());
        }
        Self {
            ldc_values: ldc_values.into_boxed_slice(),
            jump_targets: jump_targets.into_boxed_slice(),
            local_indices: local_indices.into_boxed_slice(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Program {
    pub constants: Vec<Value>,
    pub code: Vec<u8>,
    pub local_count: usize,
    pub imports: Vec<HostImport>,
    pub debug: Option<crate::debug_info::DebugInfo>,
    pub type_map: Option<TypeMap>,
    #[allow(dead_code)]
    decoded_instruction_data_cache: Arc<OnceLock<Arc<DecodedInstructionData>>>,
    operand_type_hints_cache: Arc<OnceLock<Option<Arc<[u8]>>>>,
}

impl Program {
    pub fn new(constants: Vec<Value>, code: Vec<u8>) -> Self {
        let local_count = infer_local_count_from_code(&code);
        Self {
            constants,
            code,
            local_count,
            imports: Vec::new(),
            debug: None,
            type_map: None,
            decoded_instruction_data_cache: Arc::new(OnceLock::new()),
            operand_type_hints_cache: Arc::new(OnceLock::new()),
        }
    }

    pub fn with_debug(
        constants: Vec<Value>,
        code: Vec<u8>,
        debug: Option<crate::debug_info::DebugInfo>,
    ) -> Self {
        let local_count = infer_local_count_from_code(&code);
        Self {
            constants,
            code,
            local_count,
            imports: Vec::new(),
            debug,
            type_map: None,
            decoded_instruction_data_cache: Arc::new(OnceLock::new()),
            operand_type_hints_cache: Arc::new(OnceLock::new()),
        }
    }

    pub fn with_imports_and_debug(
        constants: Vec<Value>,
        code: Vec<u8>,
        imports: Vec<HostImport>,
        debug: Option<crate::debug_info::DebugInfo>,
    ) -> Self {
        let local_count = infer_local_count_from_code(&code);
        Self {
            constants,
            code,
            local_count,
            imports,
            debug,
            type_map: None,
            decoded_instruction_data_cache: Arc::new(OnceLock::new()),
            operand_type_hints_cache: Arc::new(OnceLock::new()),
        }
    }

    pub fn with_local_count(mut self, local_count: usize) -> Self {
        self.local_count = local_count;
        self
    }

    pub fn with_type_map(mut self, type_map: TypeMap) -> Self {
        self.type_map = Some(type_map);
        self.operand_type_hints_cache = Arc::new(OnceLock::new());
        self
    }

    #[allow(dead_code)]
    pub(crate) fn shared_decoded_instruction_data(&self) -> Arc<DecodedInstructionData> {
        Arc::clone(
            self.decoded_instruction_data_cache
                .get_or_init(|| Arc::new(DecodedInstructionData::build(self))),
        )
    }

    #[allow(dead_code)]
    pub(crate) fn shared_operand_type_hints(&self) -> Option<Arc<[u8]>> {
        self.operand_type_hints_cache
            .get_or_init(|| build_operand_type_hints(self.code.len(), self.type_map.as_ref()))
            .clone()
    }
}

#[allow(dead_code)]
fn build_operand_type_hints(code_len: usize, type_map: Option<&TypeMap>) -> Option<Arc<[u8]>> {
    let type_map = type_map?;
    if type_map.operand_types.is_empty() {
        return None;
    }

    let mut hints = vec![0u8; code_len];
    for (offset, (lhs, rhs)) in &type_map.operand_types {
        let Some(entry) = hints.get_mut(*offset) else {
            continue;
        };
        *entry = (*lhs as u8) | ((*rhs as u8) << 4);
    }
    Some(Arc::from(hints.into_boxed_slice()))
}

#[allow(dead_code)]
fn read_u32_at(code: &[u8], offset: usize) -> Option<u32> {
    let bytes = code.get(offset..offset + 4)?;
    Some(u32::from_le_bytes(bytes.try_into().ok()?))
}

fn infer_local_count_from_code(code: &[u8]) -> usize {
    let mut ip = 0usize;
    let mut max_local_index: Option<u8> = None;

    while let Some(&opcode) = code.get(ip) {
        ip += 1;
        let Ok(opcode) = OpCode::try_from(opcode) else {
            break;
        };
        let operand_len = opcode.operand_len();
        if ip + operand_len > code.len() {
            break;
        }
        match opcode {
            OpCode::Ldloc | OpCode::Stloc => {
                let index = code[ip];
                max_local_index = Some(max_local_index.map_or(index, |prev| prev.max(index)));
            }
            _ => {}
        }
        ip += operand_len;
    }

    max_local_index.map_or(0, |index| index as usize + 1)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum OpCode {
    Nop = 0x00,
    Ret = 0x01,
    Ldc = 0x02,
    Add = 0x03,
    Sub = 0x04,
    Mul = 0x05,
    Div = 0x06,
    Neg = 0x07,
    Ceq = 0x08,
    Clt = 0x09,
    Cgt = 0x0A,
    Br = 0x0B,
    Brfalse = 0x0C,
    Pop = 0x0D,
    Dup = 0x0E,
    Ldloc = 0x0F,
    Stloc = 0x10,
    Call = 0x11,
    Shl = 0x12,
    Shr = 0x13,
    Mod = 0x14,
    And = 0x15,
    Or = 0x16,
    Not = 0x17,
    Lshr = 0x18,
}

impl TryFrom<u8> for OpCode {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            x if x == Self::Nop as u8 => Ok(Self::Nop),
            x if x == Self::Ret as u8 => Ok(Self::Ret),
            x if x == Self::Ldc as u8 => Ok(Self::Ldc),
            x if x == Self::Add as u8 => Ok(Self::Add),
            x if x == Self::Sub as u8 => Ok(Self::Sub),
            x if x == Self::Mul as u8 => Ok(Self::Mul),
            x if x == Self::Div as u8 => Ok(Self::Div),
            x if x == Self::Neg as u8 => Ok(Self::Neg),
            x if x == Self::Ceq as u8 => Ok(Self::Ceq),
            x if x == Self::Clt as u8 => Ok(Self::Clt),
            x if x == Self::Cgt as u8 => Ok(Self::Cgt),
            x if x == Self::Br as u8 => Ok(Self::Br),
            x if x == Self::Brfalse as u8 => Ok(Self::Brfalse),
            x if x == Self::Pop as u8 => Ok(Self::Pop),
            x if x == Self::Dup as u8 => Ok(Self::Dup),
            x if x == Self::Ldloc as u8 => Ok(Self::Ldloc),
            x if x == Self::Stloc as u8 => Ok(Self::Stloc),
            x if x == Self::Call as u8 => Ok(Self::Call),
            x if x == Self::Shl as u8 => Ok(Self::Shl),
            x if x == Self::Shr as u8 => Ok(Self::Shr),
            x if x == Self::Mod as u8 => Ok(Self::Mod),
            x if x == Self::And as u8 => Ok(Self::And),
            x if x == Self::Or as u8 => Ok(Self::Or),
            x if x == Self::Not as u8 => Ok(Self::Not),
            x if x == Self::Lshr as u8 => Ok(Self::Lshr),
            _ => Err(()),
        }
    }
}

impl OpCode {
    pub const fn operand_len(self) -> usize {
        match self {
            Self::Nop
            | Self::Ret
            | Self::Add
            | Self::Sub
            | Self::Mul
            | Self::Div
            | Self::Neg
            | Self::Ceq
            | Self::Clt
            | Self::Cgt
            | Self::Pop
            | Self::Dup
            | Self::Shl
            | Self::Shr
            | Self::Mod
            | Self::And
            | Self::Or
            | Self::Not
            | Self::Lshr => 0,
            Self::Ldc | Self::Br | Self::Brfalse => 4,
            Self::Ldloc | Self::Stloc => 1,
            Self::Call => 3,
        }
    }

    pub fn mnemonic(self) -> &'static str {
        match self {
            OpCode::Nop => "nop",
            OpCode::Ret => "ret",
            OpCode::Ldc => "ldc",
            OpCode::Add => "add",
            OpCode::Sub => "sub",
            OpCode::Mul => "mul",
            OpCode::Div => "div",
            OpCode::Neg => "neg",
            OpCode::Ceq => "ceq",
            OpCode::Clt => "clt",
            OpCode::Cgt => "cgt",
            OpCode::Br => "br",
            OpCode::Brfalse => "brfalse",
            OpCode::Pop => "pop",
            OpCode::Dup => "dup",
            OpCode::Ldloc => "ldloc",
            OpCode::Stloc => "stloc",
            OpCode::Call => "call",
            OpCode::Shl => "shl",
            OpCode::Shr => "shr",
            OpCode::Mod => "mod",
            OpCode::And => "and",
            OpCode::Or => "or",
            OpCode::Not => "not",
            OpCode::Lshr => "lshr",
        }
    }

    pub fn parse_mnemonic(op: &str) -> Option<Self> {
        match op {
            "nop" => Some(OpCode::Nop),
            "ret" => Some(OpCode::Ret),
            "ldc" => Some(OpCode::Ldc),
            "add" => Some(OpCode::Add),
            "sub" => Some(OpCode::Sub),
            "mul" => Some(OpCode::Mul),
            "div" => Some(OpCode::Div),
            "neg" => Some(OpCode::Neg),
            "ceq" => Some(OpCode::Ceq),
            "clt" => Some(OpCode::Clt),
            "cgt" => Some(OpCode::Cgt),
            "br" => Some(OpCode::Br),
            "brfalse" => Some(OpCode::Brfalse),
            "pop" => Some(OpCode::Pop),
            "dup" => Some(OpCode::Dup),
            "ldloc" => Some(OpCode::Ldloc),
            "stloc" => Some(OpCode::Stloc),
            "call" => Some(OpCode::Call),
            "shl" => Some(OpCode::Shl),
            "shr" => Some(OpCode::Shr),
            "mod" => Some(OpCode::Mod),
            "and" => Some(OpCode::And),
            "or" => Some(OpCode::Or),
            "not" => Some(OpCode::Not),
            "lshr" => Some(OpCode::Lshr),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn heap_value_clone_shares_backing() {
        let string = Value::string("hello");
        let string_clone = string.clone();
        let (Value::String(lhs), Value::String(rhs)) = (&string, &string_clone) else {
            panic!("expected string values");
        };
        assert!(Arc::ptr_eq(lhs, rhs));

        let array = Value::array(vec![Value::Int(1), Value::Int(2)]);
        let array_clone = array.clone();
        let (Value::Array(lhs), Value::Array(rhs)) = (&array, &array_clone) else {
            panic!("expected array values");
        };
        assert!(Arc::ptr_eq(lhs, rhs));

        let bytes = Value::bytes([1u8, 2, 3]);
        let bytes_clone = bytes.clone();
        let (Value::Bytes(lhs), Value::Bytes(rhs)) = (&bytes, &bytes_clone) else {
            panic!("expected bytes values");
        };
        assert!(Arc::ptr_eq(lhs, rhs));

        let map = Value::map(vec![(Value::string("k"), Value::Int(9))]);
        let map_clone = map.clone();
        let (Value::Map(lhs), Value::Map(rhs)) = (&map, &map_clone) else {
            panic!("expected map values");
        };
        assert!(Arc::ptr_eq(lhs, rhs));
    }

    #[test]
    fn bytes_map_key_uses_value_lookup() {
        let key = Value::bytes([0x01u8, 0x02, 0x03]);
        let expected = Value::Bool(true);

        let mut map = VmMap::new();
        map.insert(key, expected.clone());

        assert_eq!(
            map.get(&Value::bytes([0x01u8, 0x02, 0x03])),
            Some(&expected)
        );
        assert_eq!(map.get(&Value::bytes([0x01u8, 0x02, 0x04])), None);
    }

    #[test]
    fn composite_map_key_remains_stable_after_alias_detach() {
        let source_key = Value::array(vec![Value::Int(1), Value::Int(2)]);
        let alias = source_key.clone();
        let lookup_key = source_key.clone();
        let expected = Value::string("kept");

        let mut map = VmMap::new();
        map.insert(source_key, expected.clone());

        let mutated_alias = match alias {
            Value::Array(values) => {
                let mut owned = unwrap_or_clone_shared(values);
                owned[0] = Value::Int(9);
                Value::array(owned)
            }
            other => panic!("expected array alias, got {other:?}"),
        };

        assert_eq!(map.get(&lookup_key), Some(&expected));
        assert_eq!(
            map.get(&Value::array(vec![Value::Int(1), Value::Int(2)])),
            None
        );
        assert_eq!(map.get(&mutated_alias), None);
    }

    #[test]
    fn nested_map_keys_use_identity_lookup() {
        let nested_key = Value::map(vec![
            (Value::string("a"), Value::Int(1)),
            (Value::string("b"), Value::Int(2)),
        ]);
        let lookup_key = nested_key.clone();
        let structural_peer = Value::map(vec![
            (Value::string("b"), Value::Int(2)),
            (Value::string("a"), Value::Int(1)),
        ]);
        let expected = Value::Bool(true);

        let mut map = VmMap::new();
        map.insert(nested_key, expected.clone());

        assert_eq!(map.get(&lookup_key), Some(&expected));
        assert_eq!(map.get(&structural_peer), None);
    }

    #[test]
    fn vm_map_cached_len_stays_in_sync() {
        let mut map = VmMap::new();
        assert_eq!(map.len(), 0);

        assert_eq!(map.insert(Value::string("a"), Value::Int(1)), None);
        assert_eq!(map.len(), 1);

        assert_eq!(
            map.insert(Value::string("a"), Value::Int(2)),
            Some(Value::Int(1))
        );
        assert_eq!(map.len(), 1);

        assert_eq!(map.insert(Value::string("b"), Value::Int(3)), None);
        assert_eq!(map.len(), 2);

        assert_eq!(map.remove(&Value::string("missing")), None);
        assert_eq!(map.len(), 2);

        assert_eq!(map.remove(&Value::string("a")), Some(Value::Int(2)));
        assert_eq!(map.len(), 1);
    }
}