run-rs 0.2.9

Run a subset of Rust as an interpreted script
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
//! The register machine. Executes a compiled `Chunk` against one contiguous
//! register stack. Calls to user functions and closures push a frame record
//! and continue in the same instruction loop, so a script-level call costs no
//! native recursion, no allocation, and no register file copy beyond its
//! arguments. Anything else, methods and std or crate bridges, is delegated to
//! the existing dispatch on `Interp` with already evaluated values.

use std::cell::RefCell;
use std::collections::HashMap;
use std::mem::{replace, take};
use std::rc::Rc;

use anyhow::{Result, anyhow, bail};

use super::Interp;
use super::bytecode::{
    BinKind, BuiltinId, CapSource, Chunk, DISCARD, MacroKind, MethodName, Op, overflow_message,
};
use super::value::{ClosureData, StructShape, Upvalue, Value};
use super::vm_support::{int_of, set_reg, take_range, trace_error};

/// Guard against runaway recursion, since script calls no longer consume the
/// native stack. Depth, not registers, so deep-but-narrow recursion still works.
const MAX_CALL_DEPTH: usize = 100_000;

/// A binding of a generic parameter name to the concrete type a caller passed
/// by turbofish, plus the module that type was named in, so the callee can
/// resolve `from_str::<T>` to the real struct. Empty for a non-generic call.
pub(super) type TypeEnv = Rc<[(Rc<str>, Rc<syn::Type>, u16)]>;

fn empty_type_env() -> TypeEnv {
    Rc::from(Vec::new())
}

fn swap_option<T>(current: &mut Option<T>, next: Option<T>) -> Option<T> {
    match next {
        Some(value) => current.replace(value),
        None => current.take(),
    }
}

/// A suspended caller, restored when the callee returns.
struct Frame {
    chunk: Rc<Chunk>,
    closure: Option<Rc<ClosureData>>,
    ip: usize,
    base: usize,
    dst: u16,
    /// The caller's arg window, so the callee's final parameter values can be
    /// handed back on return for `&mut` argument writebacks.
    abase: u16,
    argc: u16,
    type_env: TypeEnv,
}

impl Interp {
    pub(super) fn exec(
        &self,
        entry: &Rc<Chunk>,
        stack: &mut Vec<Value>,
        entry_upvalues: &[Upvalue],
    ) -> Result<Value> {
        let mut frames: Vec<Frame> = Vec::new();
        let mut local_cells: HashMap<usize, Rc<RefCell<Value>>> = HashMap::new();
        let mut cur = entry.clone();
        let mut cur_clo: Option<Rc<ClosureData>> = None;
        let mut cur_tenv: TypeEnv = empty_type_env();
        let mut base = 0usize;
        let mut ip = 0usize;

        // The dispatch runs inside one immediately called closure so an error
        // can be annotated with the script call chain still held in `frames`
        // and the failing op still addressed by `cur` and `ip`. The closure
        // runs exactly once, so the hot loop itself is unchanged.
        let result = (|| -> Result<Value> {
            // Return `$v` from the current script function: pop back into the
            // caller frame, or leave the VM when this was the entry chunk.
            macro_rules! ret {
                ($v:expr) => {{
                    let v = $v;
                    match frames.pop() {
                        None => return Ok(v),
                        Some(f) => {
                            let callee_base = base;
                            let callee_end = callee_base + cur.num_regs;
                            local_cells
                                .retain(|slot, _| *slot < callee_base || *slot >= callee_end);
                            cur = f.chunk;
                            cur_clo = f.closure;
                            cur_tenv = f.type_env;
                            ip = f.ip;
                            // The callee's final parameter values go back into the
                            // caller's arg window, where a `&mut` argument
                            // writeback emitted by the compiler picks them up.
                            base = f.base;
                            for i in 0..f.argc as usize {
                                let p = take(&mut stack[callee_base + i]);
                                set_reg(&mut stack[base + f.abase as usize + i], p);
                            }
                            set_reg(&mut stack[base + f.dst as usize], v);
                            continue;
                        }
                    }
                }};
            }

            // Enter `$chunk` with `$argc` args taken from the caller window at
            // `$abase`, storing the result into caller register `$dst` on return.
            macro_rules! call {
                ($chunk:expr, $clo:expr, $dst:expr, $abase:expr, $argc:expr) => {
                    call!($chunk, $clo, $dst, $abase, $argc, empty_type_env())
                };
                ($chunk:expr, $clo:expr, $dst:expr, $abase:expr, $argc:expr, $tenv:expr) => {{
                    let callee: Rc<Chunk> = $chunk;
                    if $argc != callee.num_params {
                        bail!(
                            "`{}` expects {} args but got {}",
                            callee.name,
                            callee.num_params,
                            $argc
                        );
                    }
                    if frames.len() >= MAX_CALL_DEPTH {
                        bail!("stack overflow: call depth exceeded {MAX_CALL_DEPTH}");
                    }
                    let nbase = base + cur.num_regs;
                    let need = nbase + callee.num_regs.max(callee.num_params);
                    if stack.len() < need {
                        stack.resize(need, Value::Unit);
                    }
                    for i in 0..$argc {
                        let v = take(&mut stack[base + $abase + i]);
                        set_reg(&mut stack[nbase + i], v);
                    }
                    // Frames are not truncated on return, so clear whatever the
                    // previous occupant left in the non-argument slots.
                    for slot in &mut stack[nbase + $argc..need] {
                        set_reg(slot, Value::Unit);
                    }
                    frames.push(Frame {
                        chunk: replace(&mut cur, callee),
                        closure: swap_option(&mut cur_clo, $clo),
                        type_env: replace(&mut cur_tenv, $tenv),
                        ip: ip + 1,
                        base,
                        dst: $dst,
                        abase: $abase as u16,
                        argc: $argc as u16,
                    });
                    base = nbase;
                    ip = 0;
                    continue;
                }};
            }

            loop {
                if ip >= cur.code.len() {
                    ret!(Value::Unit);
                }
                match &cur.code[ip] {
                    Op::LoadConst { dst, k } => {
                        let v = Value::from_const(&cur.consts[*k as usize]);
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::LoadInt { dst, v } => stack[base + *dst as usize] = Value::Int(*v),
                    Op::LoadBool { dst, v } => stack[base + *dst as usize] = Value::Bool(*v),
                    Op::LoadUnit { dst } => stack[base + *dst as usize] = Value::Unit,
                    Op::LoadGlobal { dst, idx } => {
                        let v = self.global(*idx as usize)?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::LoadUpvalue { dst, idx } => {
                        let upvals: &[Upvalue] = match &cur_clo {
                            Some(c) => &c.captured,
                            None => entry_upvalues,
                        };
                        let value = upvals[*idx as usize].get();
                        set_reg(&mut stack[base + *dst as usize], value);
                    }
                    Op::LoadCell { dst, cell } => {
                        let slot = base + *cell as usize;
                        let Some(value) = local_cells.get(&slot) else {
                            bail!("missing mutable capture cell");
                        };
                        let value = value.borrow().clone();
                        set_reg(&mut stack[base + *dst as usize], value);
                    }
                    Op::StoreCell { cell, src } => {
                        let slot = base + *cell as usize;
                        let Some(value) = local_cells.get(&slot) else {
                            bail!("missing mutable capture cell");
                        };
                        *value.borrow_mut() = stack[base + *src as usize].clone();
                    }
                    Op::StoreUpvalue { idx, src } => {
                        let upvalues: &[Upvalue] = match &cur_clo {
                            Some(closure) => &closure.captured,
                            None => entry_upvalues,
                        };
                        if !upvalues[*idx as usize].set(stack[base + *src as usize].clone()) {
                            bail!("cannot assign to immutable capture");
                        }
                    }
                    Op::Move { dst, src } => {
                        let v = stack[base + *src as usize].clone();
                        set_reg(&mut stack[base + *dst as usize], v);
                    }

                    Op::Bin { dst, a, b, op } => {
                        let v =
                            apply_bin(*op, &stack[base + *a as usize], &stack[base + *b as usize])?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::BinImm { dst, a, imm, op } => {
                        let v = apply_bin_imm(*op, &stack[base + *a as usize], *imm)?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::Un { dst, a, op } => {
                        let v = apply_un(*op, &stack[base + *a as usize])?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }

                    Op::Jump { to } => {
                        let to = *to as usize;
                        if to <= ip {
                            self.run_pending_ctrlc()?;
                        }
                        ip = to;
                        continue;
                    }
                    Op::JumpIfFalse { cond, to } => {
                        if !stack[base + *cond as usize].is_truthy() {
                            ip = *to as usize;
                            continue;
                        }
                    }
                    Op::JumpIfTrue { cond, to } => {
                        if stack[base + *cond as usize].is_truthy() {
                            ip = *to as usize;
                            continue;
                        }
                    }
                    Op::CmpJump { a, b, op, to } => {
                        if !cmp_test(*op, &stack[base + *a as usize], &stack[base + *b as usize])? {
                            ip = *to as usize;
                            continue;
                        }
                    }
                    Op::CmpJumpImm { a, imm, op, to } => {
                        if !cmp_test_imm(*op, &stack[base + *a as usize], *imm)? {
                            ip = *to as usize;
                            continue;
                        }
                    }

                    Op::CallFn {
                        dst,
                        func,
                        base: abase,
                        argc,
                        targ,
                    } => {
                        let (dst, func) = (*dst, *func as usize);
                        let (abase, argc) = (*abase as usize, *argc as usize);
                        let callee = self.functions[func].clone();
                        // Bind the call's turbofish type args to the callee's
                        // generic parameters, resolved in this (caller) module.
                        let tenv: TypeEnv = if *targ != u32::MAX {
                            let targs = &cur.call_type_args[*targ as usize];
                            let module = cur.module;
                            callee
                                .generics
                                .iter()
                                .zip(targs.iter())
                                .map(|(name, ty)| (name.clone(), ty.clone(), module))
                                .collect()
                        } else {
                            empty_type_env()
                        };
                        call!(callee, None, dst, abase, argc, tenv);
                    }
                    Op::CallValue {
                        dst,
                        callee,
                        base: abase,
                        argc,
                    } => {
                        let (dst, callee) = (*dst, *callee as usize);
                        let (abase, argc) = (*abase as usize, *argc as usize);
                        let clo = match &stack[base + callee] {
                            Value::Closure(clo) => clo.clone(),
                            other => bail!("cannot call {}", other.type_name()),
                        };
                        let chunk = clo.chunk.clone();
                        call!(chunk, Some(clo), dst, abase, argc);
                    }
                    Op::CallPath {
                        dst,
                        path,
                        base: abase,
                        argc,
                    } => {
                        let (dst, path) = (*dst, *path as usize);
                        let (abase, argc) = (*abase as usize, *argc as usize);
                        let (segs, coerce) = &cur.paths[path];
                        if let Some(v) = self.internal_path(segs, &stack[base..], abase, argc)? {
                            set_reg(&mut stack[base + dst as usize], v);
                        } else {
                            let args = take_range(stack, base + abase, argc);
                            // Typed json parses straight into the target structs,
                            // no generic tree and no coercion pass afterwards.
                            if let Some(ty) = coerce {
                                let canon = self.canonical(segs);
                                if canon.len() >= 2
                                    && canon[canon.len() - 2] == "serde_json"
                                    && canon[canon.len() - 1] == "from_str"
                                {
                                    let v = self.typed_from_str(
                                        &args,
                                        ty,
                                        cur.module as usize,
                                        &cur_tenv,
                                    )?;
                                    set_reg(&mut stack[base + dst as usize], v);
                                    ip += 1;
                                    continue;
                                }
                            }
                            let mut v = self.dispatch_call(segs, args)?;
                            if let Some(ty) = coerce {
                                v = self.coerce_result(v, ty, cur.module as usize);
                            }
                            set_reg(&mut stack[base + dst as usize], v);
                        }
                    }
                    Op::PathValue { dst, path } => {
                        let (segs, _) = &cur.paths[*path as usize];
                        set_reg(
                            &mut stack[base + *dst as usize],
                            self.eval_path_value(segs)?,
                        );
                    }
                    Op::Method {
                        dst,
                        recv,
                        name,
                        base: abase,
                        argc,
                    } => {
                        let (dst, recv) = (*dst, *recv as usize);
                        let (abase, argc) = (*abase as usize, *argc as usize);
                        let name = &cur.names[*name as usize];
                        let s = base + abase;
                        // Strings are copy on write, so push must edit the Rc in
                        // the receiver register itself. Going through the normal
                        // path would edit a copy and drop the change.
                        // `clone_from` replaces the receiver outright, so it has
                        // to write the register rather than a copy of it.
                        if name.id == BuiltinId::CloneFrom {
                            let src = stack[s..s + argc].first().cloned().unwrap_or(Value::Unit);
                            set_reg(&mut stack[base + recv], src);
                            if dst != DISCARD {
                                set_reg(&mut stack[base + dst as usize], Value::Unit);
                            }
                            ip += 1;
                            continue;
                        }
                        if matches!(name.id, BuiltinId::Push | BuiltinId::PushStr)
                            && matches!(stack[base + recv], Value::Str(_))
                        {
                            let Value::Str(mut buf) = take(&mut stack[base + recv]) else {
                                unreachable!()
                            };
                            {
                                let out = Value::str_make_mut(&mut buf);
                                match (&name.id, stack[s..s + argc].first()) {
                                    (BuiltinId::Push, Some(Value::Char(c))) => out.push(*c),
                                    (BuiltinId::PushStr, Some(arg)) => out.push_str(&arg.display()),
                                    _ => {}
                                }
                            }
                            set_reg(&mut stack[base + recv], Value::Str(buf));
                            if dst != DISCARD {
                                set_reg(&mut stack[base + dst as usize], Value::Unit);
                            }
                            ip += 1;
                            continue;
                        }
                        // Option and Result accessors dominate counting loops, so
                        // their success paths run right here, skipping the whole
                        // dispatch chain. Failure paths fall through and get their
                        // errors from the slow path. Skipped when the script
                        // defines methods, which could shadow these on an enum.
                        if self.methods.is_empty()
                            && matches!(
                                name.id,
                                BuiltinId::Copied | BuiltinId::Unwrap | BuiltinId::UnwrapOr
                            )
                        {
                            // 0 none, 1 clone receiver, 2 clone payload, 3 default
                            let choice = match &stack[base + recv] {
                                Value::Enum {
                                    enum_name, variant, ..
                                } => {
                                    if matches!(name.id, BuiltinId::Copied) {
                                        if &**enum_name == "Option" { 1 } else { 0 }
                                    } else if !matches!(&**enum_name, "Option" | "Result") {
                                        0
                                    } else if matches!(&**variant, "Some" | "Ok") {
                                        2
                                    } else if matches!(name.id, BuiltinId::UnwrapOr) {
                                        3
                                    } else {
                                        0
                                    }
                                }
                                _ => 0,
                            };
                            if choice != 0 {
                                let v = match choice {
                                    1 => stack[base + recv].clone(),
                                    2 => match &stack[base + recv] {
                                        Value::Enum { data, .. } => {
                                            data.first().cloned().unwrap_or(Value::Unit)
                                        }
                                        _ => unreachable!(),
                                    },
                                    _ => {
                                        if argc > 0 {
                                            take(&mut stack[s])
                                        } else {
                                            Value::Unit
                                        }
                                    }
                                };
                                if dst != DISCARD {
                                    set_reg(&mut stack[base + dst as usize], v);
                                }
                                ip += 1;
                                continue;
                            }
                        }
                        // to_string and clone on a string are a refcount bump,
                        // not worth the dispatch walk.
                        if matches!(name.id, BuiltinId::ToString | BuiltinId::Clone)
                            && let Value::Str(v) = &stack[base + recv]
                        {
                            if dst != DISCARD {
                                let v = Value::Str(v.clone());
                                set_reg(&mut stack[base + dst as usize], v);
                            }
                            ip += 1;
                            continue;
                        }
                        // Map get and insert run inline for the same reason as
                        // the Option accessors above. User methods cannot exist
                        // on a HashMap, so no gate is needed.
                        if matches!(
                            name.id,
                            BuiltinId::Get | BuiltinId::Insert | BuiltinId::ContainsKey
                        ) && matches!(stack[base + recv], Value::Map(_))
                            && argc >= 1
                            && base + recv < s
                        {
                            let (lo, hi) = stack.split_at_mut(s);
                            let Value::Map(m) = &lo[base + recv] else {
                                unreachable!()
                            };
                            let v = match name.id {
                                BuiltinId::Insert => {
                                    let k = take(&mut hi[0]).into_key();
                                    let Some(k) = k else { bail!("invalid map key") };
                                    let val = if argc > 1 {
                                        take(&mut hi[1])
                                    } else {
                                        Value::Unit
                                    };
                                    let old = m.borrow_mut().insert(k, val);
                                    if dst == DISCARD {
                                        Value::Unit
                                    } else {
                                        match old {
                                            Some(old) => Value::some(old),
                                            None => Value::none(),
                                        }
                                    }
                                }
                                _ => {
                                    let Some(k) = hi[0].key_ref() else {
                                        bail!("invalid map key")
                                    };
                                    if matches!(name.id, BuiltinId::ContainsKey) {
                                        Value::Bool(m.borrow().get(&k).is_some())
                                    } else {
                                        match m.borrow().get(&k).cloned() {
                                            Some(v) => Value::some(v),
                                            None => Value::none(),
                                        }
                                    }
                                }
                            };
                            if dst != DISCARD {
                                set_reg(&mut stack[base + dst as usize], v);
                            }
                            ip += 1;
                            continue;
                        }
                        // The arg window holds dead temporaries, so methods may
                        // consume them in place without cloning. The window sits
                        // above the receiver register, so the split hands out the
                        // receiver by reference and the args mutably at once.
                        let v = if argc == 0 {
                            self.eval_method(&stack[base + recv], name, &mut [])?
                        } else if base + recv < s {
                            let (lo, hi) = stack.split_at_mut(s);
                            self.eval_method(&lo[base + recv], name, &mut hi[..argc])?
                        } else {
                            let recv_v = stack[base + recv].clone();
                            self.eval_method(&recv_v, name, &mut stack[s..s + argc])?
                        };
                        if dst != DISCARD {
                            set_reg(&mut stack[base + dst as usize], v);
                        }
                    }
                    Op::GetOrDefault {
                        dst,
                        recv,
                        key,
                        default,
                    } => {
                        let (r, k) = (base + *recv as usize, base + *key as usize);
                        let df = base + *default as usize;
                        // Key and default may live in variable registers, so they
                        // are cloned, never taken.
                        let v = match &stack[r] {
                            Value::Map(m) => {
                                let Some(kr) = stack[k].key_ref() else {
                                    bail!("invalid map key")
                                };
                                m.borrow().get(&kr).cloned()
                            }
                            Value::Vec(items) => match &stack[k] {
                                Value::Int(i) => usize::try_from(*i)
                                    .ok()
                                    .and_then(|i| items.borrow().get(i).cloned()),
                                other => bail!("cannot index a vector with {}", other.type_name()),
                            },
                            _ => {
                                let recv_v = stack[r].clone();
                                let get = MethodName {
                                    text: "get".into(),
                                    id: BuiltinId::Get,
                                };
                                let opt =
                                    self.eval_method(&recv_v, &get, &mut [stack[k].clone()])?;
                                let copied = MethodName {
                                    text: "copied".into(),
                                    id: BuiltinId::Copied,
                                };
                                let opt = self.eval_method(&opt, &copied, &mut [])?;
                                let uo = MethodName {
                                    text: "unwrap_or".into(),
                                    id: BuiltinId::UnwrapOr,
                                };
                                Some(self.eval_method(&opt, &uo, &mut [stack[df].clone()])?)
                            }
                        };
                        let v = match v {
                            Some(v) => v,
                            None => stack[df].clone(),
                        };
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::Ret { src } => {
                        let v = take(&mut stack[base + *src as usize]);
                        ret!(v);
                    }

                    Op::MakeVec {
                        dst,
                        base: wbase,
                        count,
                    } => {
                        let (dst, wbase, count) = (*dst, *wbase as usize, *count as usize);
                        let items = take_range(stack, base + wbase, count);
                        set_reg(&mut stack[base + dst as usize], Value::vec(items));
                    }
                    Op::MakeTuple {
                        dst,
                        base: wbase,
                        count,
                    } => {
                        let (dst, wbase, count) = (*dst, *wbase as usize, *count as usize);
                        let items = take_range(stack, base + wbase, count);
                        set_reg(&mut stack[base + dst as usize], Value::tuple(items));
                    }
                    Op::MakeArrayRepeat { dst, val, count } => {
                        let n = match &stack[base + *count as usize] {
                            Value::Int(n) => *n as usize,
                            _ => bail!("array repeat length must be an integer"),
                        };
                        let v = stack[base + *val as usize].clone();
                        set_reg(
                            &mut stack[base + *dst as usize],
                            Value::vec(std::iter::repeat_n(v, n).collect()),
                        );
                    }
                    Op::MakeRange {
                        dst,
                        start,
                        end,
                        inclusive,
                    } => {
                        let s = int_of(&stack[base + *start as usize], "range bound")?;
                        let e = int_of(&stack[base + *end as usize], "range bound")?;
                        set_reg(
                            &mut stack[base + *dst as usize],
                            Value::Range {
                                start: s,
                                end: e,
                                inclusive: *inclusive,
                            },
                        );
                    }
                    Op::IterInit { dst, src } => {
                        let src_v = stack[base + *src as usize].clone();
                        let it = self.iterator_value(src_v)?;
                        set_reg(&mut stack[base + *dst as usize], it);
                    }
                    Op::ForNext { iter, idx, val, to } => {
                        let i = match &stack[base + *idx as usize] {
                            Value::Int(i) => *i,
                            _ => unreachable!("for index is an integer"),
                        };
                        let item = match stack[base + *iter as usize].clone() {
                            Value::Native(iterator) => self.iterator_next(&iterator)?,
                            other => bail!("{} is not an iterator", other.type_name()),
                        };
                        match item {
                            Some(v) => {
                                set_reg(&mut stack[base + *val as usize], v);
                                self.run_pending_ctrlc()?;
                                set_reg(&mut stack[base + *idx as usize], Value::Int(i + 1));
                            }
                            None => {
                                ip = *to as usize;
                                continue;
                            }
                        }
                    }
                    Op::MakeStruct {
                        dst,
                        info,
                        base: wbase,
                    } => {
                        let (dst, wbase) = (*dst, *wbase as usize);
                        let lit = &cur.struct_lits[*info as usize];
                        let written = lit.shape.fields.len();
                        let mut values: Vec<Value> = (0..written)
                            .map(|k| take(&mut stack[base + wbase + k]))
                            .collect();
                        // The shape is prebuilt at compile time and shared by every
                        // instance from this literal. A `..rest` adds fields the
                        // literal did not write, so that case builds a merged shape.
                        let v = if lit.has_rest {
                            let rest = &stack[base + wbase + written];
                            let mut fields = lit.shape.fields.clone();
                            let mut renames = lit.shape.renames.clone();
                            if let Value::Struct(r) = rest {
                                let rvals = r.values.borrow();
                                for (slot, (k, v)) in
                                    r.shape.fields.iter().zip(rvals.iter()).enumerate()
                                {
                                    if lit.shape.slot(k).is_none() {
                                        fields.push(k.clone());
                                        values.push(v.clone());
                                        if !renames.is_empty() {
                                            renames
                                                .push(r.shape.renames.get(slot).cloned().flatten());
                                        }
                                    }
                                }
                            }
                            Value::structure(
                                StructShape::with_renames(lit.shape.name.clone(), fields, renames),
                                values,
                            )
                        } else {
                            Value::structure(lit.shape.clone(), values)
                        };
                        set_reg(&mut stack[base + dst as usize], v);
                    }
                    Op::MakeEnum {
                        dst,
                        info,
                        base: wbase,
                        count,
                    } => {
                        let variant = &cur.enum_variants[*info as usize];
                        let data: Rc<[Value]> =
                            take_range(stack, base + *wbase as usize, *count as usize).into();
                        set_reg(
                            &mut stack[base + *dst as usize],
                            Value::Enum {
                                enum_name: variant.enum_name.clone(),
                                variant: variant.variant.clone(),
                                data,
                            },
                        );
                    }
                    Op::LoadEnum { dst, info } => {
                        let variant = &cur.enum_variants[*info as usize];
                        set_reg(
                            &mut stack[base + *dst as usize],
                            Value::Enum {
                                enum_name: variant.enum_name.clone(),
                                variant: variant.variant.clone(),
                                data: Value::empty_data(),
                            },
                        );
                    }
                    Op::MakeClosure { dst, child } => {
                        let child_chunk = cur.children[*child as usize].clone();
                        let caps = &cur.child_caps[*child as usize];
                        let upvals: &[Upvalue] = match &cur_clo {
                            Some(c) => &c.captured,
                            None => entry_upvalues,
                        };
                        let captured: Vec<Upvalue> = caps
                            .iter()
                            .map(|c| match c {
                                CapSource::Local(reg) => {
                                    Upvalue::Value(stack[base + *reg as usize].clone())
                                }
                                CapSource::Upvalue(idx) | CapSource::MutableUpvalue(idx) => {
                                    upvals[*idx as usize].clone()
                                }
                                CapSource::MutableLocal(reg) => {
                                    let slot = base + *reg as usize;
                                    let value = stack[slot].clone();
                                    let cell = local_cells
                                        .entry(slot)
                                        .or_insert_with(|| Rc::new(RefCell::new(value)))
                                        .clone();
                                    Upvalue::Mutable(cell)
                                }
                            })
                            .collect();
                        set_reg(
                            &mut stack[base + *dst as usize],
                            Value::Closure(Rc::new(ClosureData {
                                chunk: child_chunk,
                                captured,
                            })),
                        );
                    }

                    Op::Index { dst, base: b, key } => {
                        let v =
                            self.index(&stack[base + *b as usize], &stack[base + *key as usize])?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::SetIndex { base: b, key, val } => {
                        self.set_index(
                            &stack[base + *b as usize],
                            &stack[base + *key as usize],
                            stack[base + *val as usize].clone(),
                        )?;
                    }
                    Op::Deref { dst, src } => {
                        let value = match &stack[base + *src as usize] {
                            Value::Ref(reference) => reference
                                .get()
                                .ok_or_else(|| anyhow!("dereference of a dangling reference"))?,
                            value => value.clone(),
                        };
                        set_reg(&mut stack[base + *dst as usize], value);
                    }
                    Op::SetDeref { target, val } => {
                        let Value::Ref(reference) = &stack[base + *target as usize] else {
                            bail!("assignment through a non-reference value");
                        };
                        if !reference.set(stack[base + *val as usize].clone()) {
                            bail!("assignment through a dangling reference");
                        }
                    }
                    Op::GetField {
                        dst,
                        base: b,
                        member,
                    } => {
                        let v = self.get_field(
                            &stack[base + *b as usize],
                            &cur.members[*member as usize],
                        )?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::SetField {
                        base: b,
                        member,
                        val,
                    } => {
                        self.set_field(
                            &stack[base + *b as usize],
                            &cur.members[*member as usize],
                            stack[base + *val as usize].clone(),
                        )?;
                    }

                    Op::Try { dst, src } => {
                        match self.eval_try(stack[base + *src as usize].clone())? {
                            Ok(v) => stack[base + *dst as usize] = v,
                            Err(early) => ret!(early),
                        }
                    }
                    Op::Cast { dst, src, ty } => {
                        let v = self.eval_cast(
                            stack[base + *src as usize].clone(),
                            &cur.casts[*ty as usize],
                        )?;
                        set_reg(&mut stack[base + *dst as usize], v);
                    }
                    Op::NarrowGuard { src, ty, op } => {
                        if let Value::Int(i) = &stack[base + *src as usize] {
                            let (min, max) = ty.bounds();
                            if *i < min || *i > max {
                                bail!("{}", overflow_message(*op));
                            }
                        }
                    }
                    Op::NarrowRemGuard { left, right, ty } => {
                        if let (Value::Int(l), Value::Int(r)) = (
                            &stack[base + *left as usize],
                            &stack[base + *right as usize],
                        ) && *l == ty.bounds().0
                            && *r == -1
                        {
                            bail!("{}", overflow_message(BinKind::Rem));
                        }
                    }
                    Op::Coerce { dst, src, ty } => {
                        let v = self.coerce_value(
                            stack[base + *src as usize].clone(),
                            &cur.casts[*ty as usize],
                            cur.module as usize,
                        );
                        set_reg(&mut stack[base + *dst as usize], v);
                    }

                    Op::TestBind { val, pat, dst } => {
                        let info = &cur.pats[*pat as usize];
                        let value = stack[base + *val as usize].clone();
                        let binds = &info.binds;
                        let matched = {
                            let mut define = |name: &str, v: Value| {
                                if let Some((_, reg)) = binds.iter().find(|(n, _)| n == name) {
                                    set_reg(&mut stack[base + *reg as usize], v);
                                }
                            };
                            try_bind(&info.pat, &value, &mut define)
                        };
                        set_reg(&mut stack[base + *dst as usize], Value::Bool(matched));
                    }

                    Op::Fmt { dst, spec } => {
                        let text = self.render_fmt(&cur, *spec, &stack[base..])?;
                        set_reg(&mut stack[base + *dst as usize], Value::str(text));
                    }
                    Op::MacroCall { kind, dst, spec } => {
                        let text = self.render_fmt(&cur, *spec, &stack[base..])?;
                        match kind {
                            MacroKind::Println => println!("{text}"),
                            MacroKind::Print => print!("{text}"),
                            MacroKind::Eprintln => eprintln!("{text}"),
                            MacroKind::Eprint => eprint!("{text}"),
                            MacroKind::Panic => bail!("panicked: {text}"),
                            MacroKind::Anyhow => {
                                set_reg(
                                    &mut stack[base + *dst as usize],
                                    Value::err(Value::str(text)),
                                );
                            }
                            MacroKind::Bail => {
                                ret!(Value::err(Value::str(text)));
                            }
                        }
                        if !matches!(kind, MacroKind::Anyhow) {
                            set_reg(&mut stack[base + *dst as usize], Value::Unit);
                        }
                    }
                    Op::Spawn { .. } | Op::Await { .. } => {
                        bail!("async is only available under #[tokio::main]")
                    }
                    Op::Dbg {
                        dst,
                        base: wbase,
                        argc,
                    } => {
                        let (dst, wbase, argc) = (*dst, *wbase as usize, *argc as usize);
                        let mut last = Value::Unit;
                        for i in 0..argc {
                            last = stack[base + wbase + i].clone();
                            eprintln!("[dbg] {}", last.debug());
                        }
                        set_reg(&mut stack[base + dst as usize], last);
                    }
                }
                ip += 1;
            }
        })();
        result.map_err(|e| {
            let trace = std::iter::once(frame_line(&cur, ip)).chain(
                frames
                    .iter()
                    .rev()
                    .map(|f| frame_line(&f.chunk, f.ip.saturating_sub(1))),
            );
            trace_error(e, trace)
        })
    }
}

/// One backtrace entry: the function, its file, and the line of the op at
/// `ip`. For a suspended caller `ip` is the call site, not the return address.
fn frame_line(chunk: &Chunk, ip: usize) -> (String, String, u32) {
    let line = chunk.lines.get(ip).copied().unwrap_or(0);
    (chunk.name.clone(), chunk.file.to_string(), line)
}

use super::ops::{apply_bin, apply_bin_imm, apply_un, cmp_test, cmp_test_imm, try_bind};