miruscriptx 2.0.0

MiruScriptX: a complete, general-purpose scripting language written from scratch in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
//! The bytecode virtual machine: it executes a compiled [`Chunk`] on a value
//! stack.
//!
//! This is how MiruScriptX runs programs. Rather than walking the syntax tree,
//! it executes a flat stream of bytecode produced by [`crate::compiler`], which
//! avoids re-walking the tree and re-resolving names on every evaluation.
//!
//! Locals live in stack slots, a call pushes a frame that windows into the same
//! stack, and a closure captures variables as upvalues: shared cells that start
//! out pointing at a live slot and are closed into owned values when that slot
//! goes away. Operators and indexing go through [`crate::ops`].

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use crate::ast::{BinaryOp, UnaryOp};
use crate::builtins::{Args, HostTask, Step};
use crate::chunk::{Chunk, OpCode};
use crate::globals::{Globals, ModuleId, ROOT_MODULE};
use crate::random::Rng;
use crate::suggest::with_suggestion;
use crate::value::{
    Ambient, Arity, Clock, Closure, CompiledFunction, EmptyInput, Input, Keyboard, NativeFn,
    NoClock, NoKeyboard, NoScreen, NoSystem, Output, Screen, System, Upvalue, Value,
};
use crate::MiruError;

/// How deep calls may nest before the VM gives up.
///
/// Frames live on the heap, so runaway recursion does not overflow the machine
/// stack the way a tree walker would; left alone it simply grows until memory
/// runs out, which is a worse failure than an error. This cap turns it into an
/// ordinary runtime error with a line, a column, and a caret. It is far beyond
/// what real code nests, so only genuinely unbounded recursion reaches it.
const MAX_CALL_DEPTH: usize = 10_000;

/// A single active function call: which closure is running, where its
/// instruction pointer sits, and where its window of stack slots begins.
struct CallFrame {
    closure: Rc<Closure>,
    ip: usize,
    slot_base: usize,
}

/// How many bytes a `Call` instruction occupies: the opcode and its argument
/// count. [`CallFrame::call_site`] steps back over exactly this much.
const CALL_INSTRUCTION_LEN: usize = 2;

impl CallFrame {
    /// The source position of the call this frame is suspended at.
    ///
    /// `ip` is where the frame will *resume*, which the `Call` arm sets after
    /// reading the operand, so it points just past the call rather than at it.
    /// A trace wants the position a reader can find in their source, so step
    /// back over the instruction to reach the opcode byte, whose position entry
    /// is the call expression's.
    ///
    /// Returns `None` for a frame that is not suspended at a call: the script
    /// frame before it has executed anything, and any frame whose `ip` has not
    /// advanced past a call. Such a frame contributes no line to a trace rather
    /// than a wrong one.
    fn call_site(&self) -> Option<(usize, usize)> {
        let offset = self.ip.checked_sub(CALL_INSTRUCTION_LEN)?;
        let chunk = &self.closure.function.chunk;
        if OpCode::from_u8(*chunk.code.get(offset)?) != Some(OpCode::Call) {
            return None;
        }
        Some(chunk.position(offset))
    }
}

/// What entering a call did, so the dispatch loop knows how to carry on.
///
/// This says more than the boolean it replaces because a third outcome is
/// coming: a higher-order builtin suspends rather than finishing, and the loop
/// has to tell that apart from a builtin that ran to completion.
enum CallOutcome {
    /// A frame was pushed. The loop must pick up the new one.
    Frame,
    /// A native builtin ran to completion and left its result on the stack.
    /// The loop carries on with the current frame.
    Value,
    /// A higher-order builtin suspended as a task. The loop must drive it.
    Task,
}

/// Where a task's finished value belongs.
enum TaskSink {
    /// On the value stack, where the call expression's result goes. This is the
    /// ordinary case: bytecode called `map`.
    Stack,
    /// To the task beneath this one, which asked for this call. Reached when a
    /// higher-order builtin is another one's callback, as in
    /// `reduce([abs, abs], map, ..)`, where `reduce` passes two arguments and
    /// two is exactly `map`'s arity.
    Parent,
}

/// A higher-order builtin suspended part way through, waiting for a call it
/// asked for to come back.
struct PendingTask {
    task: HostTask,
    /// The frame depth at which a returning frame belongs to this task. A task
    /// is only ever created while a frame is running, so this is at least one,
    /// which is what lets a `resume_depth` of zero mean "no task pending".
    resume_at: usize,
    sink: TaskSink,
    /// Where the call that started this task was written, so an error it raises
    /// points at the `map(..)` rather than at whatever ran last.
    line: usize,
    column: usize,
}

/// What starting a call on a task's behalf did.
enum TaskCall {
    /// A frame was pushed and is running it.
    Pushed,
    /// An ordinary native builtin ran in place; here is its result.
    Value(Value),
    /// The callee was itself a higher-order builtin, so a nested task was
    /// pushed and wants driving.
    Nested,
}

/// A `try` waiting to see whether the expression under it fails.
///
/// Everything here is a mark to rewind to. An error can be raised many frames
/// deeper than the `try` that catches it, so the VM has to be able to put the
/// four things that grew back the way they were: the frame stack, the value
/// stack, the pending higher-order builtins, and the upvalues still pointing at
/// stack slots that are about to go away.
struct Handler {
    frames: usize,
    stack: usize,
    tasks: usize,
    /// Where the frame that installed this resumes: the landing, past the
    /// guarded expression.
    ip: usize,
}

/// What `import` has loaded, and what it is part way through loading.
#[derive(Default)]
struct Loader {
    /// Canonical path to the module's exported map, so a file imported from two
    /// places runs once. Without this a module with a side effect runs twice and
    /// nobody can explain why.
    cache: HashMap<PathBuf, Value>,
    /// The chain currently being loaded, innermost last, as canonical paths
    /// paired with the spec each was reached by. Turns a cycle into an ordinary
    /// error naming the chain rather than a stack overflow.
    loading: Vec<(PathBuf, String)>,
}

/// A stack-based bytecode interpreter.
pub struct Vm {
    stack: Vec<Value>,
    globals: Globals,
    frames: Vec<CallFrame>,
    /// Upvalues that still point at live stack slots, kept so several closures
    /// capturing the same slot share one upvalue, and so they can be closed when
    /// that slot leaves the stack.
    open_upvalues: Vec<(usize, Rc<RefCell<Upvalue>>)>,
    out: Box<dyn Write>,
    /// Where diagnostics go. Standard error in the binary, a buffer in tests.
    ///
    /// A separate sink rather than a flag on `out`, because the point of the
    /// stream is that a caller can redirect one and not the other. A test that
    /// cannot tell them apart is not testing anything.
    err: Box<dyn Write>,
    input: Box<dyn Input>,
    /// The host's file system and command line, if it has one. Absent unless
    /// the embedder supplies it, so nothing gains file access by accident.
    system: Box<dyn System>,
    /// The host's wall clock, on the same terms as `system`: absent until an
    /// embedder supplies one, so no program reads a time the host never gave.
    clock: Box<dyn Clock>,
    /// The host's keyboard, on the same terms again: absent until an embedder
    /// supplies one, so no program reads a key the host never had.
    keyboard: Box<dyn Keyboard>,
    /// The host's terminal, on the same terms: absent until an embedder
    /// supplies one, so no program draws on a screen the host never had.
    screen: Box<dyn Screen>,
    /// The random number generator, or `None` until a program asks for a
    /// number or sets a seed.
    ///
    /// Not a capability of the host, unlike the two above it. The algorithm
    /// belongs to the language, so that a seed gives the same numbers wherever
    /// the program runs. What it starts from is a host question, which is why
    /// it is seeded lazily, from the clock, by `Ambient::rng`.
    rng: Option<Rng>,
    /// The code a program asked to stop with, once it has asked.
    ///
    /// `None` until something calls [`Output::request_exit`]. Whoever runs the
    /// program reads this **before** reporting the error that unwound it,
    /// because a program that ended on purpose has not failed and must not be
    /// reported as though it had.
    exit: Option<i32>,
    /// The position of the instruction being executed, so a builtin can report
    /// an error where the call to it happened.
    line: usize,
    column: usize,
    /// Higher-order builtins suspended waiting for a call, innermost last.
    tasks: Vec<PendingTask>,
    /// `try` expressions currently being evaluated, innermost last.
    handlers: Vec<Handler>,
    loader: Loader,
}

impl Default for Vm {
    fn default() -> Self {
        Vm::new()
    }
}

impl Output for Vm {
    fn write(&mut self, text: &str) {
        let _ = self.out.write_all(text.as_bytes());
    }

    fn write_error(&mut self, text: &str) {
        let _ = self.err.write_all(text.as_bytes());
    }

    fn request_exit(&mut self, code: i32) {
        self.exit = Some(code);
    }
}

impl Vm {
    /// Create a VM that writes to standard output.
    pub fn new() -> Vm {
        Vm::with_output(Box::new(std::io::stdout()))
    }

    /// Create a VM that writes to a custom sink, as the capture helpers do.
    pub fn with_output(out: Box<dyn Write>) -> Vm {
        let mut globals = Globals::new();
        crate::builtins::register(&mut globals);
        Vm {
            stack: Vec::new(),
            globals,
            frames: Vec::new(),
            open_upvalues: Vec::new(),
            out,
            err: Box::new(std::io::stderr()),
            input: Box::new(EmptyInput),
            system: Box::new(NoSystem),
            clock: Box::new(NoClock),
            keyboard: Box::new(NoKeyboard),
            screen: Box::new(NoScreen),
            rng: None,
            exit: None,
            line: 0,
            column: 0,
            tasks: Vec::new(),
            handlers: Vec::new(),
            loader: Loader::default(),
        }
    }

    /// Replace the input source that `input()` reads from.
    pub fn set_input(&mut self, input: Box<dyn Input>) {
        self.input = input;
    }

    /// Replace the sink that diagnostics go to. Standard error by default.
    ///
    /// Separate from [`Vm::with_output`] because most callers want the default
    /// and only a test that reads back what a program complained about needs to
    /// change it.
    pub fn set_error_output(&mut self, err: Box<dyn Write>) {
        self.err = err;
    }

    /// The code the program asked to stop with, or `None` if it never asked.
    ///
    /// Read this before reporting the error that ended a run. An exit unwinds
    /// by raising one, so a program that stopped on purpose looks exactly like
    /// a program that failed until this is consulted.
    pub fn exit_code(&self) -> Option<i32> {
        self.exit
    }

    /// Give this VM a file system and a command line.
    ///
    /// Nothing calls this except `miru` itself. A VM that is never given one
    /// refuses every file operation, which is what the playground and any
    /// embedder gets until it decides otherwise.
    pub fn set_system(&mut self, system: Box<dyn System>) {
        self.system = system;
    }

    /// Give this VM a clock.
    ///
    /// On the same terms as [`Vm::set_system`]: a VM that is never given one
    /// refuses `now`, which is what an embedder gets until it decides
    /// otherwise. Unlike a file system, this is a capability the browser
    /// playground does supply, since a page has `Date.now`.
    pub fn set_clock(&mut self, clock: Box<dyn Clock>) {
        self.clock = clock;
    }

    /// Give this VM a keyboard, which `read_key` reads.
    ///
    /// On the same terms as [`Vm::set_clock`]. Unlike a clock, this one the
    /// browser playground does not supply: a page has no keyboard buffer to
    /// take a key from.
    pub fn set_keyboard(&mut self, keyboard: Box<dyn Keyboard>) {
        self.keyboard = keyboard;
    }

    /// Give this VM a terminal, which `clear` and the rest draw on.
    ///
    /// On the same terms as [`Vm::set_keyboard`], and absent in the playground
    /// for the same reason: a page is not a terminal.
    pub fn set_screen(&mut self, screen: Box<dyn Screen>) {
        self.screen = screen;
    }

    /// Flush any buffered output.
    pub fn flush(&mut self) {
        let _ = self.out.flush();
        let _ = self.err.flush();
    }

    /// Compile a program and run it, returning the value of its final
    /// expression.
    ///
    /// Compiling happens here, inside the VM, rather than in the caller, because
    /// the compiler and the VM share state that has to outlive a single program:
    /// a session compiles one input at a time, and the second input must resolve
    /// names the first defined.
    pub fn run(&mut self, program: &[crate::ast::Stmt]) -> Result<Value, MiruError> {
        self.run_from(program, None)
    }

    /// Run a program that came from `path`, which is what an `import` resolves
    /// against.
    ///
    /// Imports are settled here, before the program compiles, so an alias
    /// already holds its module by the time a `GetField` on it runs. `None`
    /// means the program came from a string rather than a file, and an import
    /// then has nothing to resolve against.
    pub fn run_from(
        &mut self,
        program: &[crate::ast::Stmt],
        path: Option<&Path>,
    ) -> Result<Value, MiruError> {
        self.resolve_imports(program, path, ROOT_MODULE)?;
        let script = crate::compiler::Compiler::compile(program, &mut self.globals)?;
        self.interpret(script)
    }

    /// Bind every top-level import in `program`.
    ///
    /// The error for a program with no file lives here rather than in the
    /// compiler, because this is where a path would have been. The compiler
    /// knows nothing about files.
    fn resolve_imports(
        &mut self,
        program: &[crate::ast::Stmt],
        path: Option<&Path>,
        module: ModuleId,
    ) -> Result<(), MiruError> {
        for stmt in program {
            let crate::ast::StmtKind::Import {
                spec,
                alias,
                column,
            } = &stmt.kind
            else {
                continue;
            };
            let Some(base) = path else {
                return Err(MiruError::with_column(
                    stmt.line,
                    *column,
                    "cannot import: this program was not loaded from a file",
                ));
            };
            let value = self.load_module(spec, base, stmt.line, *column)?;
            // `slot_for_declaration`, not `slot_for`: an alias introduces this
            // module's own name. `slot_for` falls through to the builtins, and
            // a builtin's slot is shared by every module, so binding a module
            // as `print` used to overwrite `print` everywhere. See section 7.6
            // of the specification, and the note on `Globals::slot_for`
            // recording v1.0 fixing the same thing for `let`.
            let slot = self
                .globals
                .slot_for_declaration(module, alias)
                .ok_or_else(|| {
                    MiruError::with_column(
                        stmt.line,
                        *column,
                        "too many global variables in one program",
                    )
                })?;
            self.globals.define(slot, value);
        }
        Ok(())
    }

    /// Load the module `spec` names, relative to the file at `base`, and return
    /// its exports as a map.
    fn load_module(
        &mut self,
        spec: &str,
        base: &Path,
        line: usize,
        column: usize,
    ) -> Result<Value, MiruError> {
        let at = |message: String| MiruError::with_column(line, column, message);
        let joined = base.parent().unwrap_or_else(|| Path::new(".")).join(spec);
        // Report a missing file as missing. Canonicalising one that does not
        // exist fails too, but with a message about the wrong thing.
        if !joined.is_file() {
            return Err(at(format!("cannot import '{spec}': no such file")));
        }
        let canonical = joined
            .canonicalize()
            .map_err(|err| at(format!("cannot import '{spec}': {err}")))?;

        // Two spellings of one path share an entry, which is what canonicalising
        // is for.
        if let Some(cached) = self.loader.cache.get(&canonical) {
            return Ok(cached.clone());
        }
        if let Some(start) = self
            .loader
            .loading
            .iter()
            .position(|(path, _)| path == &canonical)
        {
            let mut chain: Vec<&str> = self.loader.loading[start..]
                .iter()
                .map(|(_, spec)| spec.as_str())
                .collect();
            chain.push(spec);
            return Err(at(format!("import cycle: {}", chain.join(" -> "))));
        }

        let source = std::fs::read_to_string(&canonical)
            .map_err(|err| at(format!("cannot import '{spec}': {err}")))?;
        let program = crate::parse_program(&source).map_err(|err| in_file(err, spec))?;

        self.loader
            .loading
            .push((canonical.clone(), spec.to_string()));
        let module = self.globals.new_module();
        let outcome = self.run_module(&program, &canonical, spec, module);
        self.loader.loading.pop();
        outcome.map_err(|err| in_file(err, spec))?;

        let exports = Value::map(self.globals.exports(module));
        self.loader.cache.insert(canonical, exports.clone());
        Ok(exports)
    }

    /// Resolve a module's own imports, compile it, and run it.
    ///
    /// Split out so the caller can pop its loading entry whether this succeeded
    /// or failed. Nothing here re-enters `interpret`: a module's imports are
    /// settled before it compiles, and it runs to completion before control
    /// returns, so no two runs overlap.
    fn run_module(
        &mut self,
        program: &[crate::ast::Stmt],
        canonical: &Path,
        spec: &str,
        module: ModuleId,
    ) -> Result<(), MiruError> {
        self.resolve_imports(program, Some(canonical), module)?;
        let script = crate::compiler::Compiler::compile_file(
            program,
            &mut self.globals,
            module,
            Some(Rc::new(spec.to_string())),
        )?;
        self.interpret(script)?;
        Ok(())
    }

    /// Execute an already compiled program. The whole program is itself a
    /// function (an anonymous script), so running it is just calling that
    /// function and reading back the value it returns.
    ///
    /// Globals persist across calls, which is what lets a session such as the
    /// REPL build up state over many inputs. Everything else is transient: this
    /// returns with the value stack, the frame stack, and the open upvalues
    /// empty, whether the program succeeded or failed. Without that, a program
    /// that fails part way would leave a half-finished frame behind and the next
    /// one would resume into it.
    pub fn interpret(&mut self, script: Rc<CompiledFunction>) -> Result<Value, MiruError> {
        let closure = Rc::new(Closure {
            function: script,
            upvalues: Vec::new(),
        });
        self.frames.push(CallFrame {
            closure,
            ip: 0,
            slot_base: 0,
        });
        let result = self.run_frames();
        if result.is_err() {
            self.frames.clear();
            self.stack.clear();
            self.open_upvalues.clear();
            self.tasks.clear();
            self.handlers.clear();
        }
        debug_assert!(self.frames.is_empty(), "frames left after a program");
        debug_assert!(self.stack.is_empty(), "stack values left after a program");
        result
    }

    /// Run the bytecode loop until the frame at `base_depth` returns, yielding
    /// its result, attaching the call path to any error on the way out.
    ///
    /// This has to happen here rather than where the caller finally receives the
    /// error, because by then the frames are gone: [`Vm::interpret`] clears them
    /// before the error propagates. Here they are still intact.
    fn run_frames(&mut self) -> Result<Value, MiruError> {
        debug_assert!(
            self.tasks.is_empty(),
            "a task was pending when the loop was entered"
        );
        loop {
            match self.run_frames_inner() {
                Ok(value) => return Ok(value),
                Err(mut error) => {
                    // Captured before anything is unwound, because the frames
                    // that hold the path are about to be discarded whether this
                    // is caught or not.
                    error.trace = self.capture_trace();
                    // And the file for the same reason. The position on this
                    // error belongs to whichever file the innermost frame was
                    // compiled from, and only that frame still knows which one
                    // that is: the import that loaded it finished long ago.
                    if error.file.is_none() {
                        if let Some(frame) = self.frames.last() {
                            error.file = frame
                                .closure
                                .function
                                .file
                                .as_ref()
                                .map(|file| file.as_str().to_string());
                        }
                    }
                    // Everything the loop keeps in locals is re-read from
                    // `self` when it starts, so going round again picks up the
                    // rewound state.
                    self.catch(error)?;
                }
            }
        }
    }

    /// Hand an error to the innermost `try`, or give it back when no `try` can
    /// take it.
    ///
    /// `Ok` means the error became a value on the stack and the dispatch loop
    /// should carry on from the handler's landing. `Err` hands it back to be
    /// reported.
    ///
    /// A `Result` rather than an `Option` on purpose. With an `Option` whose
    /// `None` meant "caught", `self.handlers.pop()?` reads as the obvious
    /// spelling and means exactly the opposite: no handler would report itself
    /// as a successful catch, and the loop would resume over a stack it never
    /// rewound.
    fn catch(&mut self, error: MiruError) -> Result<(), MiruError> {
        if error.fatal {
            return Err(error);
        }
        let Some(handler) = self.handlers.pop() else {
            return Err(error);
        };
        // Upvalues first: closing one reads the stack slot it points at, and
        // those slots are what the next line throws away.
        self.close_upvalues_from(handler.stack);
        self.frames.truncate(handler.frames);
        self.stack.truncate(handler.stack);
        self.tasks.truncate(handler.tasks);
        // After truncating, the top frame is the one that ran the `try`.
        self.frames
            .last_mut()
            .expect("the frame that began the try")
            .ip = handler.ip;
        self.stack.push(Value::Error(Rc::new(error)));
        Ok(())
    }

    /// The call path currently on the frame stack, innermost first.
    ///
    /// Each entry names a function that was entered and the line the call to it
    /// was written on, so it reads as "in add, called from line 6". That pairs
    /// each frame's name with its *caller's* call site, not its own: a frame
    /// knows where it is suspended, which is where it calls onward, so the line
    /// a function was reached by lives one frame out.
    ///
    /// The outermost frame is the script, which nothing called, so the walk
    /// stops before it.
    fn capture_trace(&self) -> Vec<crate::TraceEntry> {
        (1..self.frames.len())
            .rev()
            .filter_map(|index| {
                let (line, _) = self.frames[index - 1].call_site()?;
                Some(crate::TraceEntry {
                    function: self.frames[index].closure.function.name.clone(),
                    line,
                })
            })
            .collect()
    }

    /// The bytecode loop itself. See [`Vm::run_frames`], which wraps it.
    fn run_frames_inner(&mut self) -> Result<Value, MiruError> {
        // The depth a returning frame has to reach for something to be waiting
        // for it. Zero means nothing is: the script itself is returning. A task
        // is always created while a frame runs, so its depth is at least one and
        // the two cases cannot be confused.
        //
        // A local rather than a field, so the comparison every `Return` makes
        // stays in a register. It outlives `continue 'frames`, so it is declared
        // out here.
        //
        // Read from the task stack rather than written as zero. Today that is
        // the same number, because nothing is ever pending when this loop is
        // entered: `interpret` pushes one frame and calls straight in. It stops
        // being the same number as soon as the loop can be re-entered part way
        // through a program, which is what catching an error will do.
        let mut resume_depth = self.resume_depth();
        // Two loops, one per frame and one per instruction. The outer loop keeps
        // the current frame's closure, instruction pointer, stack base, and chunk
        // in locals; the inner one runs until a call or return changes frames and
        // jumps back out to pick up the new ones. `closure` is a cloned handle, so
        // `chunk` borrows it rather than `self`, leaving `self` free to mutate as
        // instructions execute.
        //
        // The split is what makes `chunk` a local. Reaching it means following an
        // Rc to the closure and another to its function, and the inner loop would
        // otherwise pay for both on every instruction to arrive at something that
        // only changes when the frame does.
        'frames: loop {
            let frame = self.frames.last().expect("a call frame");
            let closure = Rc::clone(&frame.closure);
            let mut ip = frame.ip;
            let slot_base = frame.slot_base;
            let chunk = &closure.function.chunk;
            loop {
                let op_ip = ip;
                let op = OpCode::decode(chunk.code[ip]);
                ip += 1;
                match op {
                    OpCode::Constant => {
                        let index = chunk.code[ip] as usize;
                        ip += 1;
                        self.stack.push(chunk.constants[index].clone());
                    }
                    OpCode::ConstantLong => {
                        let index = read_u16(chunk, ip) as usize;
                        ip += 2;
                        self.stack.push(chunk.constants[index].clone());
                    }
                    OpCode::Nil => self.stack.push(Value::Nil),
                    OpCode::True => self.stack.push(Value::Bool(true)),
                    OpCode::False => self.stack.push(Value::Bool(false)),
                    OpCode::Negate => self.unary(UnaryOp::Negate, chunk, op_ip)?,
                    OpCode::Not => self.unary(UnaryOp::Not, chunk, op_ip)?,
                    OpCode::Add
                    | OpCode::Subtract
                    | OpCode::Multiply
                    | OpCode::Divide
                    | OpCode::Modulo
                    | OpCode::Equal
                    | OpCode::NotEqual
                    | OpCode::Less
                    | OpCode::Greater
                    | OpCode::LessEqual
                    | OpCode::GreaterEqual => self.binary(binary_op(op), chunk, op_ip)?,
                    OpCode::BinaryConst => {
                        let operator = binary_op(OpCode::decode(chunk.code[ip]));
                        let index = chunk.code[ip + 1] as usize;
                        ip += 2;
                        self.binary_const(operator, index, chunk, op_ip)?;
                    }
                    OpCode::DefineGlobal => {
                        let slot = read_u16(chunk, ip);
                        ip += 2;
                        let value = self.pop();
                        self.globals.define(slot, value);
                    }
                    OpCode::GetGlobal => {
                        let slot = read_u16(chunk, ip);
                        ip += 2;
                        match self.globals.get(slot) {
                            Some(value) => self.stack.push(value.clone()),
                            None => {
                                let name = self.globals.name(slot);
                                let message = with_suggestion(
                                    format!("undefined variable '{name}'"),
                                    name,
                                    self.globals.names_visible_from(slot),
                                );
                                return Err(runtime_error(chunk, op_ip, message));
                            }
                        }
                    }
                    OpCode::SetGlobal => {
                        let slot = read_u16(chunk, ip);
                        ip += 2;
                        let value = self.pop();
                        if !self.globals.assign(slot, value) {
                            let name = self.globals.name(slot);
                            let refusal = format!("cannot assign to undefined variable '{name}'");
                            // A builtin's slot refuses as well, and there the
                            // name is spelled correctly and is right there. A
                            // suggestion would answer a question nobody asked,
                            // and `eprint` is one edit from `print`.
                            let message = if self.globals.is_builtin_slot(slot) {
                                refusal
                            } else {
                                with_suggestion(
                                    refusal,
                                    name,
                                    self.globals.names_visible_from(slot),
                                )
                            };
                            return Err(runtime_error(chunk, op_ip, message));
                        }
                    }
                    OpCode::Jump => {
                        let offset = read_u16(chunk, ip);
                        ip += 2 + offset as usize;
                    }
                    OpCode::JumpIfFalse => {
                        let offset = read_u16(chunk, ip);
                        ip += 2;
                        let taken = self
                            .peek()
                            .condition()
                            .map_err(|m| runtime_error(chunk, op_ip, m))?;
                        if !taken {
                            ip += offset as usize;
                        }
                    }
                    OpCode::JumpIfTrue => {
                        let offset = read_u16(chunk, ip);
                        ip += 2;
                        let taken = self
                            .peek()
                            .condition()
                            .map_err(|m| runtime_error(chunk, op_ip, m))?;
                        if taken {
                            ip += offset as usize;
                        }
                    }
                    OpCode::Loop => {
                        let offset = read_u16(chunk, ip);
                        ip = ip + 2 - offset as usize;
                    }
                    OpCode::BeginTry => {
                        let offset = read_u16(chunk, ip);
                        ip += 2;
                        self.handlers.push(Handler {
                            frames: self.frames.len(),
                            stack: self.stack.len(),
                            tasks: self.tasks.len(),
                            ip: ip + offset as usize,
                        });
                    }
                    OpCode::EndTry => {
                        self.handlers.pop().expect("a handler to end");
                    }
                    OpCode::Truthy => {
                        let value = self.pop();
                        let truth = value
                            .condition()
                            .map_err(|m| runtime_error(chunk, op_ip, m))?;
                        self.stack.push(Value::Bool(truth));
                    }
                    OpCode::GetLocal => {
                        let slot = chunk.code[ip] as usize;
                        ip += 1;
                        let value = self.stack[slot_base + slot].clone();
                        self.stack.push(value);
                    }
                    OpCode::GetLocalLong => {
                        let slot = read_u16(chunk, ip) as usize;
                        ip += 2;
                        let value = self.stack[slot_base + slot].clone();
                        self.stack.push(value);
                    }
                    OpCode::SetLocal => {
                        let slot = chunk.code[ip] as usize;
                        ip += 1;
                        let value = self.pop();
                        self.stack[slot_base + slot] = value;
                    }
                    OpCode::SetLocalLong => {
                        let slot = read_u16(chunk, ip) as usize;
                        ip += 2;
                        let value = self.pop();
                        self.stack[slot_base + slot] = value;
                    }
                    OpCode::Array => {
                        let count = read_u16(chunk, ip) as usize;
                        ip += 2;
                        let start = self.stack.len() - count;
                        let items = self.stack.split_off(start);
                        self.stack.push(Value::array(items));
                    }
                    OpCode::Map => {
                        let count = read_u16(chunk, ip) as usize;
                        ip += 2;
                        let start = self.stack.len() - count * 2;
                        let pairs = self.stack.split_off(start);
                        let mut entries = BTreeMap::new();
                        let mut pairs = pairs.into_iter();
                        while let Some(key) = pairs.next() {
                            let value = pairs.next().expect("a value for every map key");
                            let key = crate::ops::map_key(&key)
                                .map_err(|message| runtime_error(chunk, op_ip, message))?;
                            entries.insert(key, value);
                        }
                        self.stack.push(Value::map(entries));
                    }
                    OpCode::Index => {
                        // The operand byte holds no value; its position entry is the
                        // target expression's, used when the target is not indexable.
                        let target_ip = ip;
                        ip += 1;
                        let index = self.pop();
                        let target = self.pop();
                        let element = index_get(target, &index, chunk, op_ip, target_ip)?;
                        self.stack.push(element);
                    }
                    OpCode::GetField => {
                        // Same shape as Index: the operand byte holds no value,
                        // only the target expression's position, so "cannot read
                        // a field" lands under the target.
                        let target_ip = ip;
                        ip += 1;
                        let name = self.pop();
                        let target = self.pop();
                        let value = field_get(target, &name, chunk, op_ip, target_ip)?;
                        self.stack.push(value);
                    }
                    OpCode::SetIndex => {
                        let target_ip = ip;
                        ip += 1;
                        let index = self.pop();
                        let target = self.pop();
                        let value = self.pop();
                        index_set(target, &index, value, chunk, op_ip, target_ip)?;
                    }
                    OpCode::SetField => {
                        let target_ip = ip;
                        ip += 1;
                        let name = self.pop();
                        let target = self.pop();
                        let value = self.pop();
                        field_set(target, &name, value, chunk, op_ip, target_ip)?;
                    }
                    OpCode::CheckUnpack => {
                        let expected = read_u16(chunk, ip) as usize;
                        ip += 2;
                        // Looked at rather than popped: the array stays as the
                        // hidden local the names are then indexed out of.
                        match self.peek() {
                            Value::Array(items) => {
                                let found = items.borrow().len();
                                if found != expected {
                                    return Err(runtime_error(
                                        chunk,
                                        op_ip,
                                        format!(
                                            "cannot take apart an array of {found} \
                                             with a pattern of {expected}"
                                        ),
                                    ));
                                }
                            }
                            // A caught error names the original failure here as
                            // it does everywhere else it is used, rather than
                            // reporting its type.
                            Value::Error(error) => {
                                let message = format!("unhandled error: {}", error.message);
                                return Err(runtime_error(chunk, op_ip, message));
                            }
                            other => {
                                let message = format!(
                                    "cannot take apart a {}; a pattern needs an array",
                                    other.type_name()
                                );
                                return Err(runtime_error(chunk, op_ip, message));
                            }
                        }
                    }
                    OpCode::IterSnapshot => {
                        let wants_pairs = chunk.code[ip] == 1;
                        ip += 1;
                        let value = self.pop();
                        match value {
                            // A map is iterable only as pairs. With one loop
                            // variable it falls through to the refusal below,
                            // which is deliberate: keys and values are both
                            // reasonable expectations, so picking one silently
                            // makes half of all readers wrong.
                            Value::Map(entries) if wants_pairs => {
                                // The map's own order, which section 4.3 fixes
                                // as sorted. `keys` reads the same order from
                                // the same place, so the two cannot drift.
                                let snapshot: Vec<Value> = entries
                                    .borrow()
                                    .iter()
                                    .map(|(key, value)| {
                                        Value::array(vec![
                                            Value::Str(std::rc::Rc::new(key.clone())),
                                            value.clone(),
                                        ])
                                    })
                                    .collect();
                                self.stack.push(Value::array(snapshot));
                            }
                            Value::Array(items) if wants_pairs => {
                                let snapshot: Vec<Value> = items
                                    .borrow()
                                    .iter()
                                    .enumerate()
                                    .map(|(index, element)| {
                                        Value::array(vec![
                                            Value::Int(index as i64),
                                            element.clone(),
                                        ])
                                    })
                                    .collect();
                                self.stack.push(Value::array(snapshot));
                            }
                            Value::Array(items) => {
                                let snapshot = items.borrow().clone();
                                self.stack.push(Value::array(snapshot));
                            }
                            // A map with one loop variable. Still refused, for
                            // the reason it always was: keys and values are
                            // both reasonable expectations, so choosing either
                            // silently makes half of all readers wrong, and
                            // section 2.3 would then freeze the coin flip.
                            //
                            // What changed in 1.10 is that there is now
                            // somewhere to point. The refusal names it, because
                            // a message that only says no leaves the reader to
                            // guess which of the two the language meant.
                            Value::Map(_) => {
                                return Err(runtime_error(
                                    chunk,
                                    op_ip,
                                    "cannot iterate over a map with one loop variable \
                                     (write 'for key, value in ...' to walk its entries, \
                                     or 'for key in keys(...)' for the keys alone)"
                                        .to_string(),
                                ))
                            }
                            // Iterating a caught error is a use of it, so it
                            // names the original error like every other use.
                            // Without this arm the wildcard below answered
                            // "cannot iterate over a error": true, generic, and
                            // about the type rather than what actually went
                            // wrong. It was the last consumer path v0.9 missed.
                            Value::Error(error) => {
                                return Err(runtime_error(
                                    chunk,
                                    op_ip,
                                    format!("unhandled error: {}", error.message),
                                ))
                            }
                            other => {
                                return Err(runtime_error(
                                    chunk,
                                    op_ip,
                                    format!("cannot iterate over a {}", other.type_name()),
                                ))
                            }
                        }
                    }
                    OpCode::ForNext => {
                        let seq_slot = slot_base + read_u16(chunk, ip) as usize;
                        let jump = read_u16(chunk, ip + 2);
                        ip += 4;
                        let index = match &self.stack[seq_slot + 1] {
                            Value::Int(n) => *n,
                            _ => unreachable!("for-in index is not an integer"),
                        };
                        let length = match &self.stack[seq_slot] {
                            Value::Array(items) => items.borrow().len() as i64,
                            _ => unreachable!("for-in sequence is not an array"),
                        };
                        if index >= length {
                            ip += jump as usize;
                        } else {
                            let element = match &self.stack[seq_slot] {
                                Value::Array(items) => items.borrow()[index as usize].clone(),
                                _ => unreachable!("for-in sequence is not an array"),
                            };
                            self.stack.push(element);
                            self.stack[seq_slot + 1] = Value::Int(index + 1);
                        }
                    }
                    OpCode::Closure => {
                        let index = read_u16(chunk, ip) as usize;
                        ip += 2;
                        let function = Rc::clone(&chunk.functions[index]);
                        let upvalue_count = chunk.code[ip] as usize;
                        ip += 1;
                        let mut upvalues = Vec::with_capacity(upvalue_count);
                        for _ in 0..upvalue_count {
                            let is_local = chunk.code[ip] != 0;
                            let operand = read_u16(chunk, ip + 1) as usize;
                            ip += 3;
                            let upvalue = if is_local {
                                self.capture_upvalue(slot_base + operand)
                            } else {
                                Rc::clone(&closure.upvalues[operand])
                            };
                            upvalues.push(upvalue);
                        }
                        self.stack
                            .push(Value::Closure(Rc::new(Closure { function, upvalues })));
                    }
                    OpCode::GetUpvalue => {
                        let index = chunk.code[ip] as usize;
                        ip += 1;
                        let value = match &*closure.upvalues[index].borrow() {
                            Upvalue::Open(slot) => self.stack[*slot].clone(),
                            Upvalue::Closed(value) => value.clone(),
                        };
                        self.stack.push(value);
                    }
                    OpCode::SetUpvalue => {
                        let index = chunk.code[ip] as usize;
                        ip += 1;
                        let value = self.pop();
                        let upvalue = Rc::clone(&closure.upvalues[index]);
                        match &mut *upvalue.borrow_mut() {
                            Upvalue::Open(slot) => self.stack[*slot] = value,
                            Upvalue::Closed(cell) => *cell = value,
                        };
                    }
                    OpCode::CloseUpvalue => {
                        self.close_upvalues_from(self.stack.len() - 1);
                        self.pop();
                    }
                    OpCode::Call => {
                        let argcount = chunk.code[ip] as usize;
                        ip += 1;
                        // Save where to resume, then enter the callee. A builtin runs
                        // in place and leaves the frame stack untouched.
                        self.frames.last_mut().expect("a call frame").ip = ip;
                        match self.call_at_stack(argcount, chunk, op_ip)? {
                            CallOutcome::Frame => continue 'frames,
                            CallOutcome::Value => continue,
                            CallOutcome::Task => {
                                self.drive_tasks(None)?;
                                resume_depth = self.resume_depth();
                                continue 'frames;
                            }
                        }
                    }
                    OpCode::Pop => {
                        self.pop();
                    }
                    // The last byte of a `match` with no `else`. A value has
                    // reached the end of the arms, and refusing is the answer
                    // the issue asked for: falling through silently would make
                    // a forgotten case look like a working program, which is
                    // the failure `match` was filed to prevent rather than to
                    // introduce.
                    OpCode::NoMatch => {
                        let value = self.pop();
                        return Err(runtime_error(
                            chunk,
                            op_ip,
                            format!("no arm of this match takes {}", value.display()),
                        ));
                    }
                    // The two stack moves compound assignment is built from.
                    // Both are pure rearrangement: nothing is read, written, or
                    // dropped, so neither can fail.
                    OpCode::DupTwo => {
                        let len = self.stack.len();
                        let under = self.stack[len - 2].clone();
                        let top = self.stack[len - 1].clone();
                        self.stack.push(under);
                        self.stack.push(top);
                    }
                    OpCode::Rot3 => {
                        let len = self.stack.len();
                        let top = self.stack.remove(len - 1);
                        self.stack.insert(len - 3, top);
                    }
                    OpCode::Return => {
                        let result = self.pop();
                        let frame = self.frames.pop().expect("a call frame");
                        // Close any upvalues that captured this frame's locals before
                        // they leave the stack.
                        self.close_upvalues_from(frame.slot_base);
                        if self.frames.len() == resume_depth {
                            // Drop the callee and its arguments. What happens to
                            // the result depends on whether anything is waiting.
                            self.stack.truncate(frame.slot_base.saturating_sub(1));
                            if resume_depth == 0 {
                                return Ok(result);
                            }
                            self.drive_tasks(Some(result))?;
                            resume_depth = self.resume_depth();
                            continue 'frames;
                        }
                        // Drop the callee and its arguments and locals, then leave the
                        // return value where the call expression's result belongs.
                        self.stack.truncate(frame.slot_base - 1);
                        self.stack.push(result);
                        continue 'frames;
                    }
                }
            }
        }
    }

    /// Enter a function call: the callee sits just beneath its `argcount`
    /// arguments on the stack. A user function pushes a new frame; a native
    /// builtin runs immediately and leaves its result in place. The outcome
    /// tells the run loop how to carry on.
    fn call_at_stack(
        &mut self,
        argcount: usize,
        chunk: &Chunk,
        op_ip: usize,
    ) -> Result<CallOutcome, MiruError> {
        let callee_slot = self.stack.len() - argcount - 1;
        let callee = self.stack[callee_slot].clone();
        match callee {
            Value::Closure(closure) => {
                if !closure.function.arity.accepts(argcount) {
                    let name = closure.function.name.as_deref().unwrap_or("<anonymous>");
                    return Err(runtime_error(
                        chunk,
                        op_ip,
                        wrong_arity(name, &closure.function.arity, argcount),
                    ));
                }
                if self.frames.len() >= MAX_CALL_DEPTH {
                    return Err(runtime_error(
                        chunk,
                        op_ip,
                        format!("call depth limit of {MAX_CALL_DEPTH} exceeded"),
                    )
                    .as_fatal());
                }
                let slot_base = self.stack.len() - argcount;
                let ip = self.fill_parameters(&closure.function.arity, slot_base, argcount);
                self.frames.push(CallFrame {
                    closure,
                    ip,
                    slot_base,
                });
                Ok(CallOutcome::Frame)
            }
            // An ordinary builtin runs to completion here, so no frame is
            // pushed: the callee and its arguments are replaced by the result.
            Value::Builtin(_) => {
                let args = self.stack.split_off(callee_slot + 1);
                self.stack.pop();
                let (line, column) = chunk.position(op_ip);
                let result = self.call_native(callee, args, line, column)?;
                self.stack.push(result);
                Ok(CallOutcome::Value)
            }
            // A higher-order builtin does not run here at all. It checks its
            // arguments and becomes a task, which the dispatch loop drives by
            // making the calls it asks for. The callee and its arguments come
            // off the stack the same way, so the slot the result belongs in is
            // the top of the stack for as long as the task lasts.
            Value::HostBuiltin(builtin) => {
                let args = self.stack.split_off(callee_slot + 1);
                self.stack.pop();
                let (line, column) = chunk.position(op_ip);
                let mut task = (builtin.func)(args).map_err(|m| runtime_error(chunk, op_ip, m))?;
                // A callback that is an ordinary builtin never needs a frame,
                // so the task never needs to leave this function. Driving it
                // here skips the task stack, the per-element step dispatch, and
                // the enum that carries a result back out. It is the same state
                // machine, just turned by a tighter handle.
                if matches!(task.callback(), Value::Builtin(_)) {
                    let result = self.run_native_task(&mut task, line, column)?;
                    self.stack.push(result);
                    return Ok(CallOutcome::Value);
                }
                self.tasks.push(PendingTask {
                    task,
                    resume_at: self.frames.len(),
                    sink: TaskSink::Stack,
                    line,
                    column,
                });
                Ok(CallOutcome::Task)
            }
            Value::Error(error) => Err(runtime_error(
                chunk,
                op_ip,
                format!("unhandled error: {}", error.message),
            )),
            other => Err(runtime_error(
                chunk,
                op_ip,
                format!("a {} is not callable", other.type_name()),
            )),
        }
    }

    /// Run a native builtin, with `line` and `column` as the position its errors
    /// report. Shared by bytecode calls and calls made from host builtins.
    fn call_native(
        &mut self,
        callee: Value,
        args: Vec<Value>,
        line: usize,
        column: usize,
    ) -> Result<Value, MiruError> {
        // No builtin is handed a caught error, bar the few that exist to
        // inspect one. It is what stops `print(r)` from turning an error the
        // program never dealt with into a line of output that looks
        // deliberate.
        //
        // Checking here covers fifty-seven at once: every builtin that arrives
        // as a `Value::Builtin`, which is the plain ones, the system ones, and
        // the ambient ones.
        // It does not cover the four higher-order builtins, because those
        // become tasks in `call_at_stack` and never reach this function.
        //
        // Those four are still safe, by a different route rather than by this
        // one: a caught error is not an array and is not callable, so they
        // refuse it when they check the types of their arguments. The message
        // names the type instead of naming the unhandled error, which is worth
        // knowing before assuming this guard is what stopped it.
        //
        // `builtin_kind_counts_match_the_comments_that_quote_them` in
        // `src/builtins.rs` holds that number to the registrations.
        let inspects = match &callee {
            Value::Builtin(builtin) => crate::builtins::accepts_error(builtin.name),
            _ => false,
        };
        if !inspects {
            if let Some(message) = args.iter().find_map(crate::ops::unhandled) {
                return Err(MiruError::with_column(line, column, message));
            }
        }
        let (saved_line, saved_column) = (self.line, self.column);
        self.line = line;
        self.column = column;
        let result = match callee {
            Value::Builtin(builtin) => {
                // Each capability is moved out for the duration of the call and
                // put back after. `self` is what gets passed as the output
                // sink, so it cannot also be borrowed for a field at the same
                // time. A system builtin needs no output, but the same swap
                // applies for the same reason.
                let result = match builtin.func {
                    NativeFn::Plain(func) => {
                        let mut input = std::mem::replace(&mut self.input, Box::new(EmptyInput));
                        let result = func(self, &mut *input, args);
                        self.input = input;
                        result
                    }
                    NativeFn::System(func) => {
                        let mut system = std::mem::replace(&mut self.system, Box::new(NoSystem));
                        let result = func(&mut *system, args);
                        self.system = system;
                        result
                    }
                    // No swap here, and the difference is worth stating so the
                    // next reader does not copy one in. The two arms above pass
                    // `self`, or would borrow a field while `self` is passed.
                    // An ambient builtin is handed neither the output sink nor
                    // the input source, so borrowing the field it does need is
                    // an ordinary field borrow and nothing has to be moved out.
                    NativeFn::Ambient(func) => func(
                        &mut Ambient::new(
                            &mut *self.clock,
                            &mut *self.keyboard,
                            &mut *self.screen,
                            &mut self.rng,
                        ),
                        args,
                    ),
                };
                result.map_err(|message| MiruError::with_column(line, column, message))
            }
            // A higher-order builtin never arrives here. `call_at_stack` turns
            // one into a task and `begin_task_call` handles one used as a
            // callback, both before this point.
            Value::HostBuiltin(_) => unreachable!("a higher-order builtin runs as a task"),
            other => Err(MiruError::with_column(
                line,
                column,
                format!("a {} is not callable", other.type_name()),
            )),
        };
        self.line = saved_line;
        self.column = saved_column;
        // An error raised after the program asked to exit is the unwinding of
        // that exit, and `try` must not catch it. The condition is the recorded
        // code rather than the message, so this needs no agreement with what
        // `exit` happens to say and cannot be defeated by a program raising the
        // same words.
        //
        // Getting this wrong is silent rather than loud: the program keeps
        // running with an exit code already set, and its caller is told a
        // result the program never reached.
        if self.exit.is_some() {
            return result.map_err(MiruError::as_fatal);
        }
        result
    }

    /// The frame depth at which something is waiting for a returning frame, or
    /// zero when nothing is.
    fn resume_depth(&self) -> usize {
        self.tasks.last().map_or(0, |pending| pending.resume_at)
    }

    /// Build an error at the position of the call being executed, for a builtin
    /// to report against.
    pub fn call_error(&self, message: String) -> MiruError {
        MiruError::with_column(self.line, self.column, message)
    }

    /// Run a task whose callback is an ordinary builtin all the way through.
    ///
    /// Such a callback never needs a frame, so nothing about this task has to
    /// survive across turns of the dispatch loop and it need not go on the task
    /// stack at all. The callee is cloned once here rather than once per
    /// element, which is the other thing the general path cannot do, since
    /// there the task and the engine are borrowed apart.
    fn run_native_task(
        &mut self,
        task: &mut HostTask,
        line: usize,
        column: usize,
    ) -> Result<Value, MiruError> {
        let callee = task.callback().clone();
        let mut last = None;
        loop {
            let step = task
                .resume(last.take())
                .map_err(|message| MiruError::with_column(line, column, message))?;
            match step {
                Step::Done(value) => return Ok(value),
                Step::Call(args) => {
                    last = Some(self.call_native(callee.clone(), args.into_vec(), line, column)?);
                }
            }
        }
    }

    /// Carry a value to the innermost task and keep going until either a frame
    /// has been pushed to work on its behalf or its finished value has landed
    /// on the stack. Either way the caller resumes the dispatch loop.
    ///
    /// This is the whole trampoline. A higher-order builtin used to loop and
    /// call back into a nested copy of the bytecode loop, one real Rust call per
    /// element; now it says what it wants applied and this hands the answer
    /// back, all on the one loop.
    fn drive_tasks(&mut self, mut last: Option<Value>) -> Result<(), MiruError> {
        loop {
            let pending = self.tasks.last_mut().expect("a pending task");
            // The position of the call that started the task, so a refusal
            // points at the `filter(..)` rather than at whatever ran last.
            let (line, column) = (pending.line, pending.column);
            let step = pending
                .task
                .resume(last.take())
                .map_err(|message| MiruError::with_column(line, column, message))?;
            match step {
                Step::Call(args) => {
                    // The callee lives on the task rather than in the step, so
                    // it is read here. One clone per element either way; the
                    // step just got 32 bytes smaller for moving it this way.
                    let callee = self
                        .tasks
                        .last()
                        .expect("a pending task")
                        .task
                        .callback()
                        .clone();
                    match self.begin_task_call(callee, args)? {
                        TaskCall::Pushed => return Ok(()),
                        TaskCall::Value(value) => last = Some(value),
                        // A nested task starts with nothing to be told, so
                        // `last` stays empty and the next turn of the loop
                        // resumes it.
                        TaskCall::Nested => {}
                    }
                }
                Step::Done(value) => {
                    let finished = self.tasks.pop().expect("a pending task");
                    match finished.sink {
                        TaskSink::Stack => {
                            self.stack.push(value);
                            return Ok(());
                        }
                        TaskSink::Parent => last = Some(value),
                    }
                }
            }
        }
    }

    /// Settle the arguments against the parameters, and say where the function
    /// should start running.
    ///
    /// Two things can be left to do once a call's arguments are on the stack.
    /// Arguments past the named parameters belong in the `rest` array, which is
    /// built here because only the VM can see how many arrived. Parameters the
    /// call left out are filled by bytecode in the function itself, so all this
    /// does for those is pick the entry point that runs the right defaults.
    ///
    /// The caller has already checked [`Arity::accepts`], so `argcount` is
    /// within range and both index calculations are in bounds.
    fn fill_parameters(&mut self, arity: &Arity, slot_base: usize, argcount: usize) -> usize {
        if argcount > arity.named {
            // Only a function with a rest parameter can be here. The extras are
            // already contiguous on top of the stack, in the order they were
            // written, so they come off as the array's elements directly.
            let extras = self.stack.split_off(slot_base + arity.named);
            self.stack.push(Value::array(extras));
            return arity.body;
        }
        arity.entries[argcount - arity.required]
    }

    /// Apply a callee on the innermost task's behalf.
    ///
    /// Errors report at the position of the call that started the task, not
    /// wherever the program happens to be, because an error in `map`'s callback
    /// is about the `map(..)` the reader wrote.
    fn begin_task_call(&mut self, callee: Value, args: Args) -> Result<TaskCall, MiruError> {
        let pending = self.tasks.last().expect("a pending task");
        let (line, column) = (pending.line, pending.column);
        match callee {
            Value::Closure(closure) => {
                let argcount = args.count();
                if !closure.function.arity.accepts(argcount) {
                    let name = closure.function.name.as_deref().unwrap_or("<anonymous>");
                    return Err(MiruError::with_column(
                        line,
                        column,
                        wrong_arity(name, &closure.function.arity, argcount),
                    ));
                }
                if self.frames.len() >= MAX_CALL_DEPTH {
                    return Err(MiruError::with_column(
                        line,
                        column,
                        format!("call depth limit of {MAX_CALL_DEPTH} exceeded"),
                    )
                    .as_fatal());
                }
                // Mirror the bytecode layout: the callee sits beneath its args.
                self.stack.push(Value::Closure(Rc::clone(&closure)));
                let slot_base = self.stack.len();
                args.push_onto(&mut self.stack);
                // The same entry point the bytecode path picks, so a callback
                // with defaults or a rest parameter behaves the same whether
                // `map` called it or a `f(..)` in the source did. Issue #50
                // asked for exactly that, without a special case.
                let ip = self.fill_parameters(&closure.function.arity, slot_base, argcount);
                self.frames.push(CallFrame {
                    closure,
                    ip,
                    slot_base,
                });
                Ok(TaskCall::Pushed)
            }
            // A higher-order builtin as a callback becomes a task of its own,
            // whose value belongs to the task that asked for it rather than to
            // the stack.
            Value::HostBuiltin(builtin) => {
                let task = (builtin.func)(args.into_vec())
                    .map_err(|m| MiruError::with_column(line, column, m))?;
                self.tasks.push(PendingTask {
                    task,
                    resume_at: self.frames.len(),
                    sink: TaskSink::Parent,
                    line,
                    column,
                });
                Ok(TaskCall::Nested)
            }
            other => Ok(TaskCall::Value(self.call_native(
                other,
                args.into_vec(),
                line,
                column,
            )?)),
        }
    }

    fn unary(&mut self, op: UnaryOp, chunk: &Chunk, offset: usize) -> Result<(), MiruError> {
        let value = self.pop();
        let result = crate::ops::unary(op, value).map_err(|m| runtime_error(chunk, offset, m))?;
        self.stack.push(result);
        Ok(())
    }

    #[inline]
    fn binary(&mut self, op: BinaryOp, chunk: &Chunk, offset: usize) -> Result<(), MiruError> {
        let right = self.pop();
        let left = self.pop();
        // Two integers is overwhelmingly the common case in a loop, so handle it
        // here rather than through the general dispatch. Anything the fast path
        // declines, including an overflow, falls through to the shared rules so
        // there is exactly one definition of what each operator means.
        if let (Value::Int(a), Value::Int(b)) = (&left, &right) {
            if let Some(result) = int_binary(op, *a, *b) {
                self.stack.push(result);
                return Ok(());
            }
        }
        let result =
            crate::ops::binary(op, left, right).map_err(|m| runtime_error(chunk, offset, m))?;
        self.stack.push(result);
        Ok(())
    }

    /// Apply a binary operator whose right operand is `constants[index]`.
    ///
    /// The same work as [`Vm::binary`] against a constant that was never pushed.
    /// Reading it out of the pool rather than off the stack is what the fused
    /// instruction buys, and it lets the integer fast path see the constant
    /// without cloning it first.
    #[inline]
    fn binary_const(
        &mut self,
        op: BinaryOp,
        index: usize,
        chunk: &Chunk,
        offset: usize,
    ) -> Result<(), MiruError> {
        let left = self.pop();
        if let (Value::Int(a), Value::Int(b)) = (&left, &chunk.constants[index]) {
            if let Some(result) = int_binary(op, *a, *b) {
                self.stack.push(result);
                return Ok(());
            }
        }
        let right = chunk.constants[index].clone();
        let result =
            crate::ops::binary(op, left, right).map_err(|m| runtime_error(chunk, offset, m))?;
        self.stack.push(result);
        Ok(())
    }

    /// Pop the top of the stack. A missing value means the compiler emitted
    /// unbalanced bytecode, which is a bug in this crate rather than user error.
    fn pop(&mut self) -> Value {
        self.stack.pop().expect("value stack underflow")
    }

    /// Look at the top of the stack without removing it.
    fn peek(&self) -> &Value {
        self.stack.last().expect("value stack underflow")
    }

    /// Return the open upvalue over `slot`, creating one if none exists yet, so
    /// that every closure capturing the same slot shares a single upvalue.
    fn capture_upvalue(&mut self, slot: usize) -> Rc<RefCell<Upvalue>> {
        if let Some((_, upvalue)) = self.open_upvalues.iter().find(|(s, _)| *s == slot) {
            return Rc::clone(upvalue);
        }
        let upvalue = Rc::new(RefCell::new(Upvalue::Open(slot)));
        self.open_upvalues.push((slot, Rc::clone(&upvalue)));
        upvalue
    }

    /// Close every open upvalue at or above `from_slot`, moving each captured
    /// value off the stack and into the upvalue so it outlives the frame.
    fn close_upvalues_from(&mut self, from_slot: usize) {
        let mut index = 0;
        while index < self.open_upvalues.len() {
            let slot = self.open_upvalues[index].0;
            if slot >= from_slot {
                let (_, upvalue) = self.open_upvalues.remove(index);
                let value = self.stack[slot].clone();
                *upvalue.borrow_mut() = Upvalue::Closed(value);
            } else {
                index += 1;
            }
        }
    }
}

/// Apply a binary operator to two integers, or decline.
///
/// This exists only to skip the general operator dispatch on the case a loop
/// spends its time in. It declines rather than guesses: overflow, division and
/// modulo by zero, and anything not covered here all return `None`, and the
/// caller falls back to [`crate::ops::binary`], which stays the single
/// definition of what every operator means and which error it raises.
#[inline]
fn int_binary(op: BinaryOp, a: i64, b: i64) -> Option<Value> {
    let value = match op {
        BinaryOp::Add => Value::Int(a.checked_add(b)?),
        BinaryOp::Subtract => Value::Int(a.checked_sub(b)?),
        BinaryOp::Multiply => Value::Int(a.checked_mul(b)?),
        // Zero divisors are an error, not a result, so leave them to the
        // general path rather than reproducing the message here.
        BinaryOp::Divide if b != 0 => Value::Int(a.checked_div(b)?),
        BinaryOp::Modulo if b != 0 => Value::Int(a.checked_rem(b)?),
        BinaryOp::Divide | BinaryOp::Modulo => return None,
        BinaryOp::Equal => Value::Bool(a == b),
        BinaryOp::NotEqual => Value::Bool(a != b),
        BinaryOp::Less => Value::Bool(a < b),
        BinaryOp::Greater => Value::Bool(a > b),
        BinaryOp::LessEqual => Value::Bool(a <= b),
        BinaryOp::GreaterEqual => Value::Bool(a >= b),
    };
    Some(value)
}

/// Read a big-endian two-byte operand at `ip`.
fn read_u16(chunk: &Chunk, ip: usize) -> u16 {
    ((chunk.code[ip] as u16) << 8) | (chunk.code[ip + 1] as u16)
}

/// Map a binary opcode to its AST operator. Panics on a non-binary opcode, which
/// only the VM's own dispatch can cause.
fn binary_op(op: OpCode) -> BinaryOp {
    match op {
        OpCode::Add => BinaryOp::Add,
        OpCode::Subtract => BinaryOp::Subtract,
        OpCode::Multiply => BinaryOp::Multiply,
        OpCode::Divide => BinaryOp::Divide,
        OpCode::Modulo => BinaryOp::Modulo,
        OpCode::Equal => BinaryOp::Equal,
        OpCode::NotEqual => BinaryOp::NotEqual,
        OpCode::Less => BinaryOp::Less,
        OpCode::Greater => BinaryOp::Greater,
        OpCode::LessEqual => BinaryOp::LessEqual,
        OpCode::GreaterEqual => BinaryOp::GreaterEqual,
        other => unreachable!("not a binary opcode: {}", other.name()),
    }
}

/// Read `target.name` for a map, or fail.
///
/// The difference from [`index_get`] is the whole reason this exists: a field
/// that is not there is an error, where a map key that is not there reads as
/// `nil`. Indexing is a lookup that may miss; field access asserts the field is
/// present, so a mistyped module member fails where it is written rather than
/// turning into a `nil` that surfaces somewhere else.
fn field_get(
    target: Value,
    name: &Value,
    chunk: &Chunk,
    access_ip: usize,
    target_ip: usize,
) -> Result<Value, MiruError> {
    // The compiler always emits a string constant here, so this only guards a
    // hand-built chunk. It is an error rather than a panic because the VM is
    // driven directly by tests and by the disassembler's readers.
    let key = match name {
        Value::Str(key) => key,
        other => {
            return Err(runtime_error(
                chunk,
                access_ip,
                format!("a field name must be a string, not a {}", other.type_name()),
            ))
        }
    };
    match target {
        Value::Map(entries) => {
            let entries = entries.borrow();
            match entries.get(key.as_str()) {
                Some(value) => Ok(value.clone()),
                None => {
                    let message = with_suggestion(
                        format!("no field '{key}'"),
                        key,
                        entries.keys().map(String::as_str),
                    );
                    Err(runtime_error(chunk, access_ip, message))
                }
            }
        }
        // The one thing a program may do with an error besides ask its type.
        // Reading is not using: it is how a program decides what to do, so it
        // has to be allowed or the guard would forbid the very thing it exists
        // to make people do.
        Value::Error(error) => error_field(&error, key, chunk, access_ip),
        other => Err(runtime_error(
            chunk,
            target_ip,
            format!("cannot read a field of a {}", other.type_name()),
        )),
    }
}

/// The fields an error has, in the order the specification lists them.
///
/// Named here so a message can offer one back. The match below is still what
/// reads them, and `an_error_answers_for_every_field_this_list_names` holds the
/// two together.
const ERROR_FIELDS: [&str; 5] = ["message", "line", "column", "file", "trace"];

/// Read one of an error's fields.
///
/// A closed set rather than a map, so a misspelling fails the way `m.nope`
/// does instead of quietly reading `nil`.
fn error_field(
    error: &MiruError,
    key: &str,
    chunk: &Chunk,
    access_ip: usize,
) -> Result<Value, MiruError> {
    let value = match key {
        "message" => Value::Str(Rc::new(error.message.clone())),
        "line" => Value::Int(error.line as i64),
        "column" => Value::Int(error.column as i64),
        // `nil` rather than an empty string when the error came from the file
        // being run, so "no file" is distinguishable from a file named "".
        "file" => match &error.file {
            Some(file) => Value::Str(Rc::new(file.clone())),
            None => Value::Nil,
        },
        // The call path the error came through, innermost first, worded as
        // the terminal prints it. Knowing an error happened is much less
        // useful than knowing where it came from, and a caught error would
        // otherwise be the one place that knowledge is thrown away.
        "trace" => {
            let entries: Vec<Value> = error
                .trace
                .iter()
                .map(|entry| {
                    let name = entry.function.as_deref().unwrap_or("<anonymous>");
                    Value::Str(Rc::new(format!(
                        "in {name}, called from line {}",
                        entry.line
                    )))
                })
                .collect();
            Value::array(entries)
        }
        other => {
            let message = with_suggestion(
                format!("an error has no field '{other}'"),
                other,
                ERROR_FIELDS,
            );
            return Err(runtime_error(chunk, access_ip, message));
        }
    };
    Ok(value)
}

/// Assign `target.name = value` for a map, or fail for anything else.
///
/// Where [`field_get`] insists the name is already there, this does not: a
/// field that does not exist is created, exactly as `target["name"] = value`
/// creates a key. The asymmetry is deliberate. Reading a name that is not there
/// is almost always a misspelling, and assigning to one almost never is.
fn field_set(
    target: Value,
    name: &Value,
    value: Value,
    chunk: &Chunk,
    access_ip: usize,
    target_ip: usize,
) -> Result<(), MiruError> {
    // As in `field_get`, the compiler always emits a string constant here, so
    // this only guards a hand-built chunk.
    let key = match name {
        Value::Str(key) => key,
        other => {
            return Err(runtime_error(
                chunk,
                access_ip,
                format!("a field name must be a string, not a {}", other.type_name()),
            ))
        }
    };
    match target {
        Value::Map(entries) => {
            entries.borrow_mut().insert(key.to_string(), value);
            Ok(())
        }
        Value::Error(error) => Err(runtime_error(
            chunk,
            target_ip,
            format!("unhandled error: {}", error.message),
        )),
        other => Err(runtime_error(
            chunk,
            target_ip,
            format!("cannot assign a field of a {}", other.type_name()),
        )),
    }
}

/// Read `target[index]` for an array or map, or fail for anything else.
///
/// A bad index or key is the index expression's fault and is reported at
/// `index_ip`; an unindexable target is the target's fault and is reported at
/// `target_ip`, so the caret lands under the part actually at fault.
fn index_get(
    target: Value,
    index: &Value,
    chunk: &Chunk,
    index_ip: usize,
    target_ip: usize,
) -> Result<Value, MiruError> {
    match target {
        Value::Array(items) => {
            let len = items.borrow().len();
            let idx = crate::ops::array_index(index, len)
                .map_err(|message| runtime_error(chunk, index_ip, message))?;
            let element = items.borrow()[idx].clone();
            Ok(element)
        }
        Value::Map(entries) => {
            // Borrowed, not copied: this only looks the key up, and a `BTreeMap`
            // keyed by `String` accepts a `&str`. Copying here allocated a
            // string per read and dropped it on the next line.
            let key = crate::ops::map_key_ref(index)
                .map_err(|message| runtime_error(chunk, index_ip, message))?;
            Ok(entries.borrow().get(key).cloned().unwrap_or(Value::Nil))
        }
        // A one-character string, because there is no character type and
        // adding one would be a far larger change than this. An index past
        // the end refuses rather than giving `nil`: an absent map key is
        // already `nil`, and joining the two would leave a program unable to
        // tell "past the end" from "held nothing".
        Value::Str(text) => {
            let found = crate::ops::string_index(index, &text)
                .map_err(|message| runtime_error(chunk, index_ip, message))?;
            Ok(Value::Str(std::rc::Rc::new(found)))
        }
        Value::Error(error) => Err(runtime_error(
            chunk,
            target_ip,
            format!("unhandled error: {}", error.message),
        )),
        other => Err(runtime_error(
            chunk,
            target_ip,
            format!("cannot index a {}", other.type_name()),
        )),
    }
}

/// Assign `target[index] = value` for an array or map, or fail for anything
/// else, attributing each error as [`index_get`] does.
fn index_set(
    target: Value,
    index: &Value,
    value: Value,
    chunk: &Chunk,
    index_ip: usize,
    target_ip: usize,
) -> Result<(), MiruError> {
    match target {
        Value::Array(items) => {
            let len = items.borrow().len();
            let idx = crate::ops::array_index(index, len)
                .map_err(|message| runtime_error(chunk, index_ip, message))?;
            items.borrow_mut()[idx] = value;
            Ok(())
        }
        Value::Map(entries) => {
            let key = crate::ops::map_key(index)
                .map_err(|message| runtime_error(chunk, index_ip, message))?;
            entries.borrow_mut().insert(key, value);
            Ok(())
        }
        Value::Error(error) => Err(runtime_error(
            chunk,
            target_ip,
            format!("unhandled error: {}", error.message),
        )),
        other => Err(runtime_error(
            chunk,
            target_ip,
            format!("cannot index-assign to a {}", other.type_name()),
        )),
    }
}

/// Build a runtime error at the source position of the byte at `offset`.
/// Tag an error with the module it came from, unless a deeper one already did.
///
/// The innermost file wins, so an error three imports down names the file it is
/// actually in rather than the one at the top of the chain.
fn in_file(mut error: MiruError, spec: &str) -> MiruError {
    if error.file.is_none() {
        error.file = Some(spec.to_string());
    }
    error
}

/// What to say when a call brings the wrong number of arguments.
///
/// Written once for both call paths. The old wording was
/// `expects 1 argument(s) but received 2`, which is what a message says when
/// nobody has decided between the two spellings; a function can now want a
/// range or a minimum as well, so there was a count to fix and three shapes to
/// add at the same time.
fn wrong_arity(name: &str, arity: &Arity, received: usize) -> String {
    format!(
        "function {name} expects {} but received {received}",
        arity.describe()
    )
}

fn runtime_error(chunk: &Chunk, offset: usize, message: impl Into<String>) -> MiruError {
    let (line, column) = chunk.position(offset);
    MiruError::with_column(line, column, message)
}

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

    /// Assemble a chunk from a compact list of ops (each with a dummy position),
    /// where `Constant` is written as its own op followed by pushing the value.
    fn run(build: impl FnOnce(&mut Chunk)) -> Result<Value, MiruError> {
        let mut chunk = Chunk::new();
        build(&mut chunk);
        chunk.write_op(OpCode::Return, 1, 1);
        let script = Rc::new(CompiledFunction {
            name: None,
            arity: Arity {
                required: 0,
                named: 0,
                rest: false,
                entries: vec![0],
                body: 0,
            },
            chunk,
            file: None,
        });
        Vm::new().interpret(script)
    }

    /// A map value, for the field tests.
    fn map_of(pairs: &[(&str, Value)]) -> Value {
        let mut entries = BTreeMap::new();
        for (key, value) in pairs {
            entries.insert((*key).to_string(), value.clone());
        }
        Value::map(entries)
    }

    /// A caught error, which no program can produce yet. Putting one in the
    /// constant pool is how these tests reach the paths that must refuse it.
    fn caught_error() -> Value {
        Value::Error(Rc::new(MiruError::with_column(1, 9, "division by zero")))
    }

    /// The message every refusal gives, which names the error being held
    /// rather than the type it happens to be.
    const REFUSED: &str = "unhandled error: division by zero";

    #[test]
    fn a_conditional_refuses_a_caught_error_rather_than_reading_it_as_false() {
        // The dangerous one. `if` and `while` ask a value whether it is truthy,
        // and every value answers, so an error would take the else branch and
        // look like an ordinary falsy value.
        for op in [OpCode::JumpIfFalse, OpCode::JumpIfTrue, OpCode::Truthy] {
            let error = run(|chunk| {
                constant(chunk, caught_error());
                chunk.write_op(op, 4, 6);
                if op != OpCode::Truthy {
                    chunk.write(0, 4, 6);
                    chunk.write(0, 4, 6);
                }
            })
            .err()
            .expect("a conditional must refuse an error");
            assert_eq!(error.message, REFUSED, "{op:?} accepted an error");
            // Reported where the misuse is written, not where the error was.
            assert_eq!((error.line, error.column), (4, 6), "{op:?}");
        }
    }

    #[test]
    fn an_error_answers_for_every_field_this_list_names() {
        // `ERROR_FIELDS` is what a message offers back, and the match in
        // `error_field` is what actually reads one. Nothing makes them agree,
        // so a field added to one and not the other would suggest a name that
        // then fails, or fail on a name it never offers.
        for field in ERROR_FIELDS {
            run(|chunk| {
                constant(chunk, caught_error());
                get_field(chunk, field);
            })
            .unwrap_or_else(|error| panic!("'{field}' is listed but not read: {}", error.message));
        }
        assert_eq!(ERROR_FIELDS.len(), 5, "the guarantee promises five fields");
    }

    #[test]
    fn an_error_answers_for_its_own_fields_and_nothing_else() {
        // Reading is not using. A program has to be able to ask what went
        // wrong, so this is the one door the guard leaves open, and it opens
        // exactly as far as a closed set of names.
        let value = run(|chunk| {
            constant(chunk, caught_error());
            get_field(chunk, "message");
        })
        .expect("an error answers for its own fields");
        assert!(value
            .equals(&Value::Str(Rc::new("division by zero".to_string())))
            .unwrap());

        // A misspelling fails rather than reading nil, the same bargain field
        // access makes on a map.
        let error = run(|chunk| {
            constant(chunk, caught_error());
            get_field(chunk, "mesage");
        })
        .err()
        .expect("an unknown field must be refused");
        assert_eq!(
            error.message,
            "an error has no field 'mesage'. Did you mean 'message'?"
        );

        // Indexing is not reading a field, and stays refused: `r["message"]`
        // would answer nil for a name that is not there, which is the whole
        // thing GetField exists to avoid.
        let error = run(|chunk| {
            constant(chunk, caught_error());
            constant(chunk, Value::Int(0));
            chunk.write_op(OpCode::Index, 1, 1);
            chunk.write(0, 1, 1);
        })
        .err()
        .expect("indexing an error must be refused");
        assert_eq!(error.message, REFUSED);
    }

    #[test]
    fn a_caught_error_is_not_callable_and_is_not_an_argument() {
        let error = run(|chunk| {
            constant(chunk, caught_error());
            chunk.write_op(OpCode::Call, 1, 1);
            chunk.write(0, 1, 1);
        })
        .err()
        .expect("calling an error must be refused");
        assert_eq!(error.message, REFUSED);

        // A builtin that accepts anything, which print() and str() do. Without a
        // guard this would have handed on an error the program never dealt
        // with, as though it were a result.
        fn echo(
            _out: &mut dyn Output,
            _input: &mut dyn Input,
            args: Vec<Value>,
        ) -> Result<Value, String> {
            Ok(args.into_iter().next().unwrap_or(Value::Nil))
        }
        let error = run(|chunk| {
            constant(
                chunk,
                Value::Builtin(crate::value::Builtin {
                    name: "echo",
                    func: NativeFn::Plain(echo),
                }),
            );
            constant(chunk, caught_error());
            chunk.write_op(OpCode::Call, 1, 1);
            chunk.write(1, 1, 1);
        })
        .err()
        .expect("a builtin must not be handed an error");
        assert_eq!(error.message, REFUSED);
    }

    /// Emit `target.name`, with the position-only operand byte the opcode
    /// carries.
    fn get_field(chunk: &mut Chunk, name: &str) {
        constant(chunk, Value::Str(Rc::new(name.to_string())));
        chunk.write_op(OpCode::GetField, 1, 1);
        chunk.write(0, 1, 1);
    }

    #[test]
    fn a_field_reads_the_value_stored_under_that_name() {
        let value = run(|chunk| {
            constant(chunk, map_of(&[("a", Value::Int(1)), ("b", Value::Int(2))]));
            get_field(chunk, "b");
        })
        .expect("runs");
        assert_eq!(value.repr(), "2");
    }

    #[test]
    fn a_field_that_is_not_there_is_an_error_where_an_index_would_be_nil() {
        // This one difference is the whole reason the opcode exists beside
        // Index. `m["nope"]` is a lookup that may miss and reads as nil;
        // `m.nope` asserts the field is there.
        let error = run(|chunk| {
            constant(chunk, map_of(&[("a", Value::Int(1))]));
            get_field(chunk, "nope");
        })
        .err()
        .expect("an error");
        assert_eq!(error.message, "no field 'nope'");
    }

    #[test]
    fn a_field_of_a_non_map_names_the_type() {
        let error = run(|chunk| {
            constant(chunk, Value::Int(5));
            get_field(chunk, "a");
        })
        .err()
        .expect("an error");
        assert_eq!(error.message, "cannot read a field of a int");
    }

    fn constant(chunk: &mut Chunk, value: Value) {
        let index = chunk.add_constant(value) as u8;
        chunk.write_op(OpCode::Constant, 1, 1);
        chunk.write(index, 1, 1);
    }

    #[test]
    fn a_frame_reports_the_call_it_is_suspended_at() {
        // Hand-assembled so the layout is known exactly: a wrong offset cannot
        // pass by landing on the right position by accident.
        let mut chunk = Chunk::new();
        chunk.write_op(OpCode::Nil, 1, 1);
        chunk.write_op(OpCode::Nil, 1, 1);
        let call = chunk.code.len();
        chunk.write_op(OpCode::Call, 7, 3);
        chunk.write(0, 7, 3);
        chunk.write_op(OpCode::Return, 8, 1);

        let function = Rc::new(CompiledFunction {
            name: Some("outer".to_string()),
            arity: Arity {
                required: 0,
                named: 0,
                rest: false,
                entries: vec![0],
                body: 0,
            },
            chunk,
            file: None,
        });
        let closure = Rc::new(Closure {
            function,
            upvalues: Vec::new(),
        });

        // Suspended at the call. `ip` points past it, at where the frame will
        // resume, and the reported position is still the call's own.
        let frame = CallFrame {
            closure: Rc::clone(&closure),
            ip: call + CALL_INSTRUCTION_LEN,
            slot_base: 0,
        };
        assert_eq!(frame.call_site(), Some((7, 3)));

        // Not suspended at a call: report nothing rather than a wrong line.
        let fresh = CallFrame {
            closure,
            ip: 0,
            slot_base: 0,
        };
        assert_eq!(fresh.call_site(), None);
    }

    #[test]
    fn evaluates_arithmetic() {
        let result = run(|chunk| {
            constant(chunk, Value::Int(2));
            constant(chunk, Value::Int(3));
            chunk.write_op(OpCode::Add, 1, 1);
            constant(chunk, Value::Int(4));
            chunk.write_op(OpCode::Multiply, 1, 1);
        })
        .unwrap();
        assert!(result.equals(&Value::Int(20)).unwrap());
    }

    #[test]
    fn evaluates_unary_and_comparison() {
        let negated = run(|chunk| {
            constant(chunk, Value::Int(5));
            chunk.write_op(OpCode::Negate, 1, 1);
        })
        .unwrap();
        assert!(negated.equals(&Value::Int(-5)).unwrap());

        let compared = run(|chunk| {
            constant(chunk, Value::Int(1));
            constant(chunk, Value::Int(2));
            chunk.write_op(OpCode::Less, 1, 1);
        })
        .unwrap();
        assert!(compared.equals(&Value::Bool(true)).unwrap());
    }

    #[test]
    fn pushes_literals() {
        let value = run(|chunk| {
            chunk.write_op(OpCode::True, 1, 1);
            chunk.write_op(OpCode::Not, 1, 1);
        })
        .unwrap();
        assert!(value.equals(&Value::Bool(false)).unwrap());
    }

    #[test]
    fn reports_a_runtime_error_with_position() {
        let error = run(|chunk| {
            constant(chunk, Value::Int(1));
            constant(chunk, Value::Int(0));
            chunk.write_op(OpCode::Divide, 7, 9);
        })
        .err()
        .unwrap();
        assert_eq!(error.line, 7);
        assert_eq!(error.column, 9);
        assert_eq!(error.message, "division by zero");
    }

    /// The two streams reach two different places.
    ///
    /// This is the whole reason `err` is a separate sink rather than a flag on
    /// `out`. A single buffer holding both would pass any test that only checks
    /// the text is somewhere, and would still be useless to a caller trying to
    /// redirect one and keep the other.
    #[test]
    fn output_and_diagnostics_go_to_different_sinks() {
        let out = Rc::new(RefCell::new(Vec::<u8>::new()));
        let err = Rc::new(RefCell::new(Vec::<u8>::new()));
        let mut vm = Vm::with_output(Box::new(SharedSink(Rc::clone(&out))));
        vm.set_error_output(Box::new(SharedSink(Rc::clone(&err))));

        vm.write("to stdout\n");
        vm.write_error("to stderr\n");
        vm.flush();

        assert_eq!(String::from_utf8_lossy(&out.borrow()), "to stdout\n");
        assert_eq!(String::from_utf8_lossy(&err.borrow()), "to stderr\n");
    }

    /// Nothing has asked to exit until something asks.
    #[test]
    fn an_exit_code_is_absent_until_it_is_requested() {
        let mut vm = Vm::new();
        assert_eq!(vm.exit_code(), None);
        vm.request_exit(3);
        assert_eq!(vm.exit_code(), Some(3));
        // The last request wins rather than the first, which is what a caller
        // would expect if two ever raced. Nothing calls this twice today.
        vm.request_exit(0);
        assert_eq!(vm.exit_code(), Some(0));
    }

    /// A `Write` sink over a shared buffer, so a test can read back what the VM
    /// sent to each stream.
    struct SharedSink(Rc<RefCell<Vec<u8>>>);

    impl Write for SharedSink {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            self.0.borrow_mut().extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }
}