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
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
//! The bytecode interpreter.
//!
//! The machine is the same shape as the reference's: a value stack per frame, a
//! program counter into `js_compile`'s resolved bytecode, and a frame chain. Two
//! properties of that design are worth stating up front because they are visible
//! everywhere below.
//!
//! **Frames are explicit, not recursive.** A JavaScript call pushes a frame onto
//! [`Interp::frames`] and the loop continues; it never recurses in Rust. Deep
//! JavaScript recursion therefore costs heap, not C stack, and the depth limit is
//! ours to enforce rather than the host's to crash.
//!
//! **Exceptions live on the value stack.** `catch` pushes a [`Value::CatchOffset`]
//! marker and an exception is found by walking the frame's stack downwards looking
//! for the innermost one. There is no side table, which is why a `try` inside a
//! loop costs nothing until it throws.
use std::collections::{BTreeMap, BTreeSet};
use js_compile::bytecode::{Atom, ClosureType, Constant, Function as CompiledFunction};
use js_compile::opcode::{OpFormat, Opcode};
use js_gc::{Gc, Heap, WeakGc};
use crate::object::{
Callable, ClosureEnv, CompiledCode, EvalScope, JsObject, LocalVars, NativeFn, ObjectKind,
Property, PropertyKey, array_index, has_own, lookup_key, same_proto,
};
use crate::value::{Cell, Value, number_to_string};
/// Why execution stopped.
#[derive(Debug, Clone)]
pub enum Signal {
/// A JavaScript exception carrying the thrown value.
Throw(Value),
/// The runtime ran out of memory.
OutOfMemory,
}
/// What an execution step produced.
pub type JsResult<T> = std::result::Result<T, Signal>;
/// The result of one instruction.
enum Step {
/// Keep executing the current frame.
Continue,
/// The current frame returned this value.
Return(Value),
/// The frame parked at a suspension point, with the yielded value on its stack.
///
/// Only a generator or an async function can produce this, and only its own frame —
/// a nested ordinary call cannot contain a `yield`. The frame is not popped: it is
/// detached and handed to the generator object, which is what lets it resume later
/// with its locals and operand stack intact.
Suspend(SuspendKind),
}
/// Why a frame parked, which decides how the resumption is delivered.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SuspendKind {
/// `initial_yield`: the body has not started. The first `next()` resumes *after* it
/// without passing a value, because there is no `yield` expression to receive one.
Initial,
/// `yield expr`: the value is on the stack and the caller gets `{value, done:false}`.
Yield,
/// `yield* expr` in a *sync* generator: like `Yield`, but the value is already an
/// `{value, done}` object from the inner iterator and is handed straight back.
YieldStar,
/// `yield* expr` in an *async* generator: the compiled shape extracts `.value` first, so
/// what was yielded is the plain value and still has to be awaited and wrapped —
/// `AsyncGeneratorYield` does `Await(value)` before handing it out. Sharing `YieldStar`
/// made it pass a bare value through as if it were an iterator result, so
/// `async function* g(){ yield* [1] }` answered `{value: {value: 1, done: false}}`.
AsyncYieldStar,
/// `await expr`: the value is a promise the async machinery has to settle first.
Await,
}
/// Everything one JavaScript realm owns.
///
/// A realm is the set of objects a script can reach without naming them: the global
/// object, the prototype chain roots, and the global binding cells.
pub struct Realm {
/// The global object.
pub global: Gc<JsObject>,
/// `Object.prototype`.
pub object_proto: Gc<JsObject>,
/// `Function.prototype`, which is itself callable.
pub function_proto: Gc<JsObject>,
/// `Array.prototype`.
pub array_proto: Gc<JsObject>,
/// Reference cells for globals declared by the script.
///
/// A declared global is read with `get_var` (by name) but written with
/// `put_var_ref` (through the cell), so the cell is what makes a function
/// declaration and its uses agree on one binding.
pub globals: BTreeMap<String, Gc<Cell>>,
/// Names of the globals that came from a *declaration*, which are
/// non-configurable properties of the global object.
pub declared: Vec<String>,
/// Names among the lexical globals that are `const`.
///
/// `const`-ness cannot be read back off the bytecode in every case: a closure that
/// refers to a global `const` gets a `GlobalRef` entry whose flags the forwarding
/// path does not carry, so a store through it would look like a plain `let`. The
/// realm records it once, at declaration, and answers for every reader.
pub lexical_consts: BTreeSet<String>,
/// The error constructors by class name, so a runtime error can throw a real
/// `TypeError` instance rather than a look-alike object.
pub errors: BTreeMap<&'static str, Gc<JsObject>>,
/// `String.prototype`, used by `ToString` on primitives and by the boxing path.
pub string_proto: Option<Gc<JsObject>>,
/// `Number.prototype`.
pub number_proto: Option<Gc<JsObject>>,
/// `Boolean.prototype`.
pub boolean_proto: Option<Gc<JsObject>>,
/// `BigInt.prototype`.
pub bigint_proto: Option<Gc<JsObject>>,
/// `Error.prototype`, the prototype of every error instance.
pub error_proto: Gc<JsObject>,
/// `Date.prototype`, so the constructor can build an instance in the
/// function-call form (`Date()` returns a string) without going through `new`.
pub date_proto: Gc<JsObject>,
/// `%GeneratorPrototype%`, the prototype of every generator object.
pub generator_proto: Gc<JsObject>,
/// `Promise.prototype`, the prototype of every promise.
pub promise_proto: Option<Gc<JsObject>>,
/// `%AsyncGeneratorPrototype%`, the prototype of every `async function*` result.
pub async_generator_proto: Option<Gc<JsObject>>,
/// `Symbol.prototype`.
pub symbol_proto: Option<Gc<JsObject>>,
/// The symbols behind the engine's own hidden slots: `_value`, `_class`,
/// `_date_value`.
///
/// These are *symbol* keys on purpose. A `"_value"` property is visible to
/// `Object.getOwnPropertyNames`, so `new Number(1)` reported an own property the
/// language does not have and `new Error()` listed `_class` beside `message` and
/// `stack` — and a caller that copies every own property (which
/// `Object.create(null, props)` effectively does) tried to write an internal name and
/// threw. A symbol key is invisible to `getOwnPropertyNames`, `Object.keys`, `for…in`
/// and `JSON.stringify`, while remaining an ordinary traced property.
///
/// `RefCell` because interning is memoisation rather than observable state: filling the
/// cache through a shared borrow is a detail, not a mutation of the realm. That is what
/// lets [`Interp::internal_set`] work from `&self`, which `make_error` needs — an error
/// built while reporting a failure must not require an exclusive borrow.
pub internal_slots: std::cell::RefCell<BTreeMap<&'static str, Value>>,
/// The `Symbol.for` registry: `key -> symbol`.
///
/// Scoped to the realm, which is what makes `Symbol.for("k")` answer the same
/// symbol every time within one script while a fresh realm starts empty.
pub symbol_registry: BTreeMap<String, Value>,
/// `RegExp.prototype`, filled in by the `RegExp` builtin.
pub regexp_proto: Option<Gc<JsObject>>,
/// The `RegExp` constructor.
pub regexp_ctor: Option<Gc<JsObject>>,
/// `SuppressedError.prototype`, which `using` builds its aggregates from.
///
/// The prototype rather than the constructor on purpose: a script that replaces
/// `SuppressedError.prototype.constructor` must not be able to divert the engine's own
/// error wrapping, which the `using` test suite checks explicitly.
pub suppressed_error_proto: Option<Gc<JsObject>>,
/// The `DisposableStack` prototype, for `move`'s intrinsic construction.
pub disposable_stack_proto: Option<Gc<JsObject>>,
/// `Map.prototype`.
pub map_proto: Option<Gc<JsObject>>,
/// `Set.prototype`.
pub set_proto: Option<Gc<JsObject>>,
/// Every live `FinalizationRegistry`, held weakly.
///
/// The cleanup callback has to run from a *safe point* — a place where script is not
/// already executing and no engine borrow is held — and the collector cannot enumerate
/// heap objects by type. Registering each registry here gives the drain a list to walk.
///
/// The handles are weak on purpose: a registry the script dropped must be collectable,
/// and it must not be resurrected merely because the drain knew about it. Entries whose
/// target has expired are swept out as the drain walks, so the vector does not grow
/// without bound in a program that creates many short-lived registries.
pub finalization_registries: Vec<WeakGc<JsObject>>,
/// `ArrayBuffer.prototype`.
pub array_buffer_proto: Option<Gc<JsObject>>,
/// `%TypedArray%.prototype`, the prototype every view type inherits from.
pub typed_array_proto: Option<Gc<JsObject>>,
/// One prototype per view type, in [`builtins::typedarray::ELEMENT_TYPES`] order.
///
/// A typed array answers `map`/`filter`/`slice` with a *new view of the same type*, and
/// the result has to carry the per-type prototype — otherwise `r.constructor.name` is
/// `"Object"` and `r instanceof Int32Array` is false.
pub typed_array_protos: Vec<Gc<JsObject>>,
/// `DataView.prototype`.
pub data_view_proto: Option<Gc<JsObject>>,
/// The `AsyncDisposableStack` prototype.
pub async_disposable_stack_proto: Option<Gc<JsObject>>,
/// `Iterator.prototype`.
pub iterator_proto: Option<Gc<JsObject>>,
/// `%IteratorHelperPrototype%`, the prototype every lazy helper object gets.
///
/// It sits between the helper and `Iterator.prototype`, which is how a helper is both
/// an `Iterator` and recognisable as a helper (`Object.prototype.toString` says
/// `[object Iterator Helper]`).
pub iterator_helper_proto: Option<Gc<JsObject>>,
/// `%WrapForValidIteratorPrototype%`, the prototype `Iterator.from` gives an iterable
/// that was not already an `Iterator`.
///
/// Distinct from the helper prototype on purpose: it forwards `next` and `return`
/// untouched and does *not* override `@@toStringTag`, so
/// `Object.prototype.toString.call(Iterator.from(xs))` says `[object Iterator]`.
pub iterator_wrap_proto: Option<Gc<JsObject>>,
/// `%GeneratorFunction.prototype%` — what a generator function inherits from.
pub generator_function_proto: Option<Gc<JsObject>>,
/// `%AsyncFunction.prototype%`.
pub async_function_proto: Option<Gc<JsObject>>,
/// `%AsyncGeneratorFunction.prototype%`.
pub async_generator_function_proto: Option<Gc<JsObject>>,
/// One `%…IteratorPrototype%` per kind, keyed by its `@@toStringTag`.
///
/// Cached because the *identity* is observable: `Object.getPrototypeOf([].values())`
/// has to equal `Object.getPrototypeOf([][Symbol.iterator]())`, and building a fresh
/// prototype per iterator made every array iterator's prototype distinct.
pub iterator_protos: std::collections::BTreeMap<String, Gc<JsObject>>,
/// `Symbol.iterator`, `Symbol.toPrimitive` and the other well-known symbols.
///
/// Held as plain `Value`s rather than looked up on `Symbol` each time: the runtime
/// asks for them on every `for…of` and every `ToPrimitive`, and a realm field is
/// both faster and impossible to break from a script.
well_known_symbols: WellKnownSymbols,
}
/// One binding of the frame that called `eval`, so the eval'd code can read and
/// write it by name.
struct EvalBinding {
/// The name it was declared with.
name: String,
/// Which entry of [`Interp::frames`] owns it. An `eval` sees the whole chain of
/// calling frames, not only the innermost one: `function f(){ eval("a") }` inside
/// a function that declares `a` must find that `a`.
frame: usize,
/// Which table of that frame it lives in.
slot: EvalSlot,
/// The slot index within that table.
index: usize,
}
/// Which table of a frame an [`EvalBinding`] indexes.
#[derive(Clone, Copy)]
enum EvalSlot {
Arg,
Local,
}
/// One activation record.
struct Frame {
/// The bytecode being run.
code: Gc<CompiledCode>,
/// The cells this closure captured.
env: Gc<ClosureEnv>,
/// The `this` binding.
this: Value,
/// Argument slots, sized to the declared parameter count.
args: Vec<Value>,
/// The values the caller actually passed, which is what `arguments` exposes.
passed: Vec<Value>,
/// Local slots.
locals: Vec<Value>,
/// Reference cells for this frame's own captured bindings.
cells: Vec<Gc<Cell>>,
/// The operand stack.
stack: Vec<Value>,
/// Variables a direct `eval` has declared in this call; see [`LocalVars`].
local_vars: Option<Gc<LocalVars>>,
/// Byte offset of the next instruction.
pc: usize,
/// Byte offset of the instruction being executed, which is what a stack trace
/// reports. `pc` has already moved past it by the time an instruction runs.
at: usize,
/// The callee, so a native builtin can tell which constructor it is.
callee: Value,
/// `new.target`: the constructor under `new`, `undefined` for a plain call.
new_target: Value,
/// What this frame costs against [`Interp::stack_limit`], in bytes.
///
/// Cached rather than recomputed because it is charged on every call and a call is the
/// hottest thing in the interpreter.
cost: usize,
/// [`Interp::stack_used`] before this frame was pushed.
///
/// Stored per frame so that *popping* needs no bookkeeping at all: the total is always
/// `top.stack_base + top.cost`, so a `pop`, a `truncate` or an unwinding loop cannot
/// leave the counter wrong. There are eight places that drop frames; one subtraction
/// forgotten in any of them would leak budget until the depth limit moved.
stack_base: usize,
/// Whether this frame's body ended in a `tail_call`.
///
/// The reference's `tail_call` is not a frame-reusing tail call: it calls the callee
/// with its own frame just like `call` does and only skips storing the result
/// (`goto done` in `quickjs.c`). So a runaway tail recursion still hits
/// `InternalError: stack overflow` there, and the flag reproduces that: the frame is
/// pushed as usual, and this marker makes its return value the *caller's* return
/// value without running the instructions after the call.
tail: bool,
}
impl js_gc::Trace for Frame {
fn trace(&self, tracer: &mut js_gc::Tracer) {
self.code.trace(tracer);
self.env.trace(tracer);
self.this.trace(tracer);
for value in &self.args {
value.trace(tracer);
}
for value in &self.passed {
value.trace(tracer);
}
for value in &self.locals {
value.trace(tracer);
}
for cell in &self.cells {
cell.trace(tracer);
}
if let Some(local_vars) = &self.local_vars {
tracer.visit(local_vars);
}
for value in &self.stack {
value.trace(tracer);
}
self.callee.trace(tracer);
self.new_target.trace(tracer);
}
}
/// The magic number `next()` resumes a `yield` with. `0` is falsy, which is the value the
/// emitted `if_false` treats as "keep going".
const RESUME_NEXT: i32 = 0;
/// The magic number `return()` resumes a `yield` with; any non-zero value makes the
/// generator fall into its `return_async`.
const RESUME_RETURN: i32 = 1;
/// The magic number `throw()` resumes a `yield*` with. Unlike `return`, a throw cannot be
/// folded into the same branch: the compiled delegation has to tell `2` apart to know it
/// must forward the exception into the inner iterator rather than finish.
const RESUME_THROW: i32 = 2;
/// One microtask.
///
/// The queue holds *data* rather than closures because its contents are GC values: a
/// closure would be invisible to the collector, and a pending reaction is exactly the
/// kind of thing that must keep its handlers and its promise alive.
pub enum Job {
/// A `then` reaction about to run.
Reaction {
/// The `onFulfilled`/`onRejected` to call, or `None` for the pass-through case.
handler: Option<Value>,
/// The value or reason to call it with.
argument: Value,
/// Whether the source promise rejected, which decides the pass-through kind.
rejected: bool,
/// The promise the answer settles.
promise: Gc<JsObject>,
},
/// Adopt a thenable: call its `then` with the adopting promise's resolve/reject.
Thenable {
/// The `then` that was found on the value.
then: Value,
/// The thenable itself, which becomes `then`'s `this`.
value: Value,
/// The promise being resolved.
promise: Gc<JsObject>,
},
/// A pending `import(specifier)`.
Import {
/// The specifier, already converted to a string.
specifier: String,
/// The module that asked, which the specifier resolves against.
referrer: String,
/// The promise `import()` handed back.
promise: Gc<JsObject>,
},
/// An async generator resuming after the value it awaited settled.
GeneratorResume {
/// The async generator to continue.
generator: Gc<JsObject>,
/// The promise the pending method returned.
promise: Gc<JsObject>,
/// Whether this delivers the value or raises the reason.
kind: ResumeKind,
/// The settled value, or the rejection reason.
value: Value,
},
/// An async function resuming after the value it awaited settled.
Resume {
/// The body, parked in a coroutine.
coroutine: Gc<JsObject>,
/// The promise the async function handed its caller.
promise: Gc<JsObject>,
/// Whether this resumption delivers a value or raises one.
kind: ResumeKind,
/// The settled value, or the rejection reason.
value: Value,
},
}
/// One macro-task: work the event loop owes the script but has not run yet.
///
/// The microtask queue ([`Job`]) is different: those run before the host regains control.
/// A timer is the opposite — it is explicitly "later", and only an event loop can honour
/// that. Keeping it here rather than in the host is what lets `run_one_turn` be pure
/// logic: the engine decides *what* is due, the host decides *how to wait*.
///
/// The callback is a [`Value`], so the [`Gc`] inside it keeps the function alive by
/// reference counting even though nothing else references it — the same rule the `HostFn`
/// documentation states from the other side (a host closure must not hold a `Gc`, so the
/// `Gc` lives here instead).
pub enum Task {
/// A `setTimeout` that is due at `due_at`.
///
/// There is no `setInterval` variant because the reference has no `os.setInterval` at
/// all: `quickjs-libc.c` defines only `setTimeout` and `clearTimeout`, and
/// `typeof os.setInterval` is `"undefined"` under `qjs --std`. A repeat field here
/// would give this engine a timer the reference cannot produce, with nothing to
/// compare it against.
Timer {
/// The id `setTimeout` returned, for `clearTimeout`.
id: i32,
/// What to call when it fires.
callback: Value,
/// When it becomes runnable, as a monotonic instant.
due_at: std::time::Instant,
},
}
/// A descriptor the host has to poll, and which directions have a handler.
///
/// The engine cannot poll for itself: `quickjs-pure` has no dependencies and no platform
/// knowledge, and `poll(2)` is neither. So the loop hands this list to the host, the host
/// blocks on it, and the host reports back through [`Interp::fire_fd`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FdInterest {
/// The descriptor to watch.
pub fd: i32,
/// Whether `os.setReadHandler` registered a callback.
pub read: bool,
/// Whether `os.setWriteHandler` registered a callback.
pub write: bool,
}
/// The callbacks registered for one descriptor, per direction.
#[derive(Default)]
pub(crate) struct FdHandlers {
/// `os.setReadHandler`'s callback.
pub read: Option<Value>,
/// `os.setWriteHandler`'s callback.
pub write: Option<Value>,
}
/// The file name `eval` reports, which the reference does not inherit from the caller.
///
/// Kept distinct from [`EVAL_SCRIPT_FILE`] because the two are named separately in the
/// reference and the names show up in stack traces.
/// The description of the private symbol a class brand is made from.
///
/// The reference builds it from `JS_ATOM_brand`, so it would print as `Symbol(brand)` — in a
/// place no script can reach, since no expression can name a private symbol.
const BRAND_DESCRIPTION: &str = "brand";
/// One validated property lookup.
pub struct FieldCache {
/// The object the lookup was made on, weakly.
object: js_gc::WeakGc<JsObject>,
/// The name, so two different properties of one object do not collide.
key: PropertyKey,
/// Where the value was found in that object's property table.
index: usize,
}
/// How many property lookups are remembered.
///
/// Direct-mapped, so a collision costs a miss and nothing else. 4096 entries is 4096 × (a
/// weak handle, a property key and a `usize`) ≈ 200 KB, which buys a hit rate high enough
/// that `property:monomorphic` is dominated by the loop around the lookup rather than by the
/// lookup itself. Making it bigger stops helping: the working set of a real program's
/// property sites is bounded by its code size.
const FIELD_CACHE_SLOTS: usize = 4096;
/// How much the heap may grow past the last `out of memory` before saying it again.
///
/// The point is that the *handler* has to be able to run: `catch (e) { print(e.message) }`
/// allocates a few hundred bytes, and re-throwing on the very next instruction made the
/// error unobservable from script — a `catch` that cannot catch is worse than no limit,
/// because it looks like it works. The reference gets this for free: it fails inside
/// `js_malloc`, and its printing path happens not to allocate.
///
/// 64 KiB is chosen to be far below any limit worth setting (so a script that keeps
/// allocating is stopped within a rounding error of the limit) and far above what a
/// handler needs.
const OOM_HYSTERESIS: usize = 64 * 1024;
/// The stack budget when the host sets none: the reference's `JS_DEFAULT_STACK_SIZE`.
pub const DEFAULT_STACK_SIZE: usize = 1024 * 1024;
/// The frame-count ceiling that backs up [`DEFAULT_STACK_SIZE`].
///
/// Chosen so that the byte budget is what actually decides for every ordinary
/// `--stack-size` (a frame costs a few hundred bytes, so 1 MB buys a few thousand frames and
/// 32 MB buys tens of thousands), while a deliberately huge budget still cannot allocate
/// millions of frames.
pub const MAX_FRAMES: usize = 200_000;
const EVAL_FILE: &str = "<input>";
/// The file name `std.evalScript` reports.
const EVAL_SCRIPT_FILE: &str = "<evalScript>";
/// A delay in milliseconds as a `Duration`, clamped to something `Instant` accepts.
///
/// Negative and `NaN` both mean "as soon as possible": the reference runs the delay through
/// `JS_ToInt64`, so `setTimeout(f, NaN)` fires immediately rather than being rejected.
fn clamp_delay(delay_ms: f64) -> std::time::Duration {
// `is_nan()` spelled out rather than `!(x > 0.0)`: the latter is a negated comparison
// on a float, which reads as a typo even when it is deliberate. Both NaN and a
// non-positive delay mean "due now".
if delay_ms.is_nan() || delay_ms <= 0.0 {
return std::time::Duration::ZERO;
}
std::time::Duration::from_secs_f64(delay_ms / 1000.0)
}
impl js_gc::Trace for Task {
fn trace(&self, tracer: &mut js_gc::Tracer) {
let Task::Timer { callback, .. } = self;
callback.trace(tracer);
}
}
/// What one turn of the event loop did, so the caller cannot spin by accident.
///
/// A bare `bool` invites the mistake of looping on it without sleeping, which burns a
/// core until the timer is due. Naming the three cases makes the wait explicit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Turn {
/// Something ran. Call again immediately — there may be more ready work.
Ran,
/// Nothing is runnable, but descriptors are registered: the host must poll
/// [`Interp::pending_fds`] for at most this long, then report readiness with
/// [`Interp::fire_fd`].
///
/// A separate variant from [`Turn::Wait`] on purpose. Sleeping instead of polling is
/// the failure this project treats as worst — a handler that silently never fires —
/// so the two cannot be confused by accident.
Poll(std::time::Duration),
/// Nothing is runnable yet, but something is pending. Block at most this long, then
/// call again.
Wait(std::time::Duration),
/// Nothing is pending at all: the loop is done.
Done,
}
impl js_gc::Trace for Job {
fn trace(&self, tracer: &mut js_gc::Tracer) {
match self {
Job::Reaction {
handler,
argument,
promise,
..
} => {
if let Some(handler) = handler {
handler.trace(tracer);
}
argument.trace(tracer);
tracer.visit(promise);
}
Job::Thenable {
then,
value,
promise,
} => {
then.trace(tracer);
value.trace(tracer);
tracer.visit(promise);
}
Job::Resume {
coroutine,
promise,
value,
..
} => {
tracer.visit(coroutine);
tracer.visit(promise);
value.trace(tracer);
}
Job::Import { promise, .. } => tracer.visit(promise),
Job::GeneratorResume {
generator,
promise,
value,
..
} => {
tracer.visit(generator);
tracer.visit(promise);
value.trace(tracer);
}
}
}
}
/// Which of `%GeneratorPrototype%`'s three methods resumed a generator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResumeKind {
/// The generator was just created and is being run to its first suspension.
Start,
/// `next(v)`.
Next,
/// `return(v)`.
Return,
/// `throw(e)`.
Throw,
}
/// A generator object's saved execution.
///
/// The reference keeps this as a `JSAsyncFunctionState` hanging off the object; the shape
/// here is the same, with a whole [`Frame`] standing in for its `JSStackFrame` plus the
/// separately allocated buffers.
pub struct GeneratorState {
/// The detached frame, or `None` once the generator has finished or while it runs.
frame: Option<Frame>,
/// How far it got.
phase: GeneratorPhase,
/// Whether the last suspension was an `await` rather than a `yield`.
///
/// The two resume with *different stack shapes*: a `yield` is re-entered with the
/// value and a magic number, because the emitted code branches on which method
/// resumed it, while an `await` simply continues with its value. Getting this wrong
/// leaves an extra value on the operand stack that the next instruction then
/// consumes — a wrong answer rather than a crash.
parked_on_await: bool,
/// Whether the last suspension was a `yield*` rather than a plain `yield`.
///
/// It decides what a `throw()` from the caller means. At a plain `yield` the exception
/// is raised *at* the yield, so the body's own `try`/`catch` sees it. At a `yield*` it
/// has to be forwarded into the inner iterator's `throw` method instead — which is the
/// `rethrow` branch the compiled delegation emits — because that is the whole point of
/// delegating. Tracking it as part of the phase, the way the reference splits
/// `SUSPENDED_YIELD` from `SUSPENDED_YIELD_STAR`, is what makes the two cases
/// distinguishable; without it `try { yield* xs } catch (e)` swallowed the caller's
/// exception and never reached `xs.throw`.
parked_on_yield_star: bool,
/// Pending `next`/`return`/`throw` calls, oldest first.
///
/// An async generator is a queue, not a re-entrant function: `it.next(); it.next(v)`
/// called back to back must answer in that order, with the second call's value
/// arriving at the *second* `yield`. Running each call straight into the body — which
/// is what this engine did before — delivered the last queued value to whichever
/// suspension happened to be live, so `var a = yield 1; var b = yield a` saw
/// `a === b ===` the final argument.
///
/// The front entry is the one being served; it stays there (with `async_busy` set)
/// until its promise settles, because an `await` inside the body is a suspension of
/// the *driver*, not the end of the request.
requests: std::collections::VecDeque<AsyncRequest>,
/// Whether the front request is in flight.
async_busy: bool,
}
/// How one async-generator request ended.
enum AsyncOutcome {
/// The method answered this `{value, done}`.
Value(Value),
/// The body threw.
Error(Value),
}
/// One queued `next`/`return`/`throw` on an async generator: the reference's
/// `JSAsyncGeneratorRequest`.
pub struct AsyncRequest {
/// Which method was called.
kind: ResumeKind,
/// Its argument.
value: Value,
/// The promise the method answered.
promise: Gc<JsObject>,
}
/// Which of §27.5.3.3's states a generator is in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum GeneratorPhase {
/// Created, `initial_yield` reached, body not entered.
SuspendedStart,
/// Parked at a `yield`.
SuspendedYield,
/// Between a resume and its suspension, which a re-entrant `next()` must refuse.
Executing,
/// Finished; the frame is gone.
Completed,
}
impl js_gc::Trace for GeneratorState {
fn trace(&self, tracer: &mut js_gc::Tracer) {
if let Some(frame) = &self.frame {
frame.trace(tracer);
}
for request in &self.requests {
request.value.trace(tracer);
tracer.visit(&request.promise);
}
}
}
/// The realm's well-known symbols, indexed by the order [`builtins::symbol`] creates
/// them in.
///
/// Each is a field rather than a map entry because the runtime reads them on hot paths
/// (`for…of`, `ToPrimitive`) and because a missing one would be a bug, not a lookup
/// failure — reaching a default here means the builtin did not install, and every
/// accessor panics rather than silently answering `undefined`.
///
/// [`builtins::symbol`]: crate::builtins::symbol
#[derive(Default)]
pub struct WellKnownSymbols {
/// The symbol values, positionally. Empty until `Symbol` installs.
symbols: Vec<Value>,
}
/// Which well-known symbol a caller wants.
///
/// A typed accessor beats passing a name around: a typo is a compile error rather than
/// an `undefined` key that quietly matches nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WellKnown {
/// `Symbol.toPrimitive`.
ToPrimitive,
/// `Symbol.iterator`.
Iterator,
/// `Symbol.match`.
Match,
/// `Symbol.matchAll`.
MatchAll,
/// `Symbol.replace`.
Replace,
/// `Symbol.search`.
Search,
/// `Symbol.split`.
Split,
/// `Symbol.toStringTag`.
ToStringTag,
/// `Symbol.isConcatSpreadable`.
IsConcatSpreadable,
/// `Symbol.hasInstance`.
HasInstance,
/// `Symbol.species`.
Species,
/// `Symbol.unscopables`.
Unscopables,
/// `Symbol.asyncIterator`.
AsyncIterator,
/// `Symbol.dispose` — the well-known name of a disposable resource's disposer.
Disposable,
/// `Symbol.asyncDispose` — the awaited form.
AsyncDisposable,
}
impl WellKnown {
/// The index `Symbol`'s installer creates this symbol at.
fn index(self) -> usize {
match self {
WellKnown::ToPrimitive => 0,
WellKnown::Iterator => 1,
WellKnown::Match => 2,
WellKnown::MatchAll => 3,
WellKnown::Replace => 4,
WellKnown::Search => 5,
WellKnown::Split => 6,
WellKnown::ToStringTag => 7,
WellKnown::IsConcatSpreadable => 8,
WellKnown::HasInstance => 9,
WellKnown::Species => 10,
WellKnown::Unscopables => 11,
WellKnown::AsyncIterator => 12,
WellKnown::Disposable => 13,
WellKnown::AsyncDisposable => 14,
}
}
}
impl Realm {
/// Record the well-known symbols, in the order `Symbol`'s installer made them.
pub fn set_well_known_symbols(&mut self, symbols: Vec<Value>) {
self.well_known_symbols.symbols = symbols;
}
/// A well-known symbol.
pub fn well_known(&self, which: WellKnown) -> Value {
self.well_known_symbols
.symbols
.get(which.index())
.cloned()
.unwrap_or(Value::Undefined)
}
/// The well-known symbol as a property key.
pub fn well_known_key(&self, which: WellKnown) -> PropertyKey {
match self.well_known(which) {
Value::Symbol(symbol) => PropertyKey::Symbol(symbol),
// Unreachable once `Symbol` has installed; an empty string key matches
// nothing, which degrades to "the object has no such method".
_ => PropertyKey::from(String::new()),
}
}
}
/// The interpreter: a realm plus a frame stack.
pub struct Interp<'a> {
/// The heap everything is allocated on.
pub heap: &'a Heap,
/// The realm.
pub realm: Realm,
/// Active frames, outermost first.
frames: Vec<Frame>,
/// A strict-mode `eval`'s own variable scope, while its script runs.
///
/// Strict eval gets a *fresh* variable environment, so a `var` inside it does not
/// become a global. Name lookups still fall through to the realm, which is why
/// `eval("a")` sees a global `a` and `eval("a = 3")` writes it.
eval_scope: Option<BTreeMap<String, Value>>,
/// How deep the interpreter currently is inside an `eval`, so a `var` it declares can
/// be created configurable — the distinction `(1, eval)("var g")` exists to exploit.
eval_depth: usize,
/// The calling frame's own bindings, which the running `eval` can read and write.
///
/// Held separately from [`Interp::eval_scope`] because these already exist: reads
/// come from the frame, and a write is applied back to the frame (or to the cell
/// the frame captured) when the eval finishes. That is what makes
/// `f("a = 3")` visible to the caller as `a === 3`.
eval_bindings: Vec<EvalBinding>,
/// The calling frame's `arguments`, while an `eval` runs.
///
/// Kept apart from [`Interp::eval_scope`] because a sloppy eval has no scope of its
/// own yet still has to see the caller's `arguments`.
eval_arguments: Option<Value>,
/// The `with` objects live at the moment the running `eval` started, innermost first.
///
/// `None` outside an eval, which is what keeps a finished block's object from
/// shadowing ordinary bytecode reads.
eval_with: Option<Vec<Gc<JsObject>>>,
/// The bindings the running direct `eval` makes visible, as name → cell.
///
/// Set for the duration of an eval and then restored, but every closure created
/// during it keeps a handle (see [`ClosureEnv::eval_scope`]), which is what lets
/// `eval("f = () => i")` work after the eval has returned.
active_eval_scope: Option<Gc<EvalScope>>,
/// The frame that called the direct `eval` currently running, if any.
///
/// A sloppy eval declares its `var`s in that frame's variable environment rather than
/// on the global object, which is what keeps
/// `function f(){ eval("var x=1") } f(); x` from defining a global `x` — and what
/// gives each call its own `x`.
eval_caller: Option<Option<usize>>,
/// The caller's `new.target`, while an `eval` runs.
///
/// The eval'd code is a script with no constructor of its own, so it inherits the
/// one the enclosing function was called with.
eval_new_target: Option<Value>,
/// `new.target` for the frame about to be pushed.
///
/// `construct` parks the constructor here and `push_frame` takes it, which is how the
/// value reaches the frame without threading a parameter through every call site.
pending_new_target: Option<Value>,
/// Names of the native functions currently executing, outermost first.
///
/// A native has no [`Frame`] — it runs on the Rust stack — but the reference's
/// trace still names it with `(native)` instead of a position, so the names are
/// tracked separately and stitched into [`Interp::stack_trace`].
///
/// The third field is how many bytecode frames were live when the native was entered,
/// which is what places it *among* them rather than before all of them. Native
/// functions used to be emitted up front on the grounds that they never re-enter
/// JavaScript; `std.evalScript` does, so that reasoning no longer holds.
native_frames: Vec<(String, Gc<JsObject>, usize)>,
/// The native function currently executing, for the ones that have to know *which*
/// function they are — a typed-array constructor reads its element type off itself.
///
/// `current_callee` cannot answer this: a native call pushes no frame of its own, so it
/// reports whatever the enclosing bytecode frame was called with.
current_native: Option<Gc<JsObject>>,
/// Text written by `print` and friends, in order.
///
/// Buffered rather than streamed so a test can inspect it; the CLI flushes it
/// once at the end, which is also when upstream's line buffering becomes
/// visible for the cases compared here.
pub output: String,
/// Where `print` also writes, as it goes. `output` keeps the full log for
/// [`crate::Context::output`]; this is the live copy, flushed per line.
///
/// Both exist on purpose. A host that gives a script a sink sees output *when it
/// happens* — which is what a timer callback's `console.log` has to do, and what the
/// reference does with `fputs` + `fflush` — while embedders and the 595 assertions in
/// this repository keep reading the accumulated `String`. Buffering only (no sink)
/// would show a script's output all at once when it finished.
pub output_sink: Option<Box<dyn std::io::Write>>,
/// Where recent property reads found their value.
///
/// An inline cache, in the only form that is safe without hidden classes: a direct-mapped
/// table remembering *which object* and *which entry of its property table* answered a
/// given name. The entry is re-validated on every hit (the table is a `Vec`, so an index
/// can move; the property can become an accessor), which is what makes a stale entry a
/// miss rather than a wrong answer.
///
/// The object is held weakly: a cache that held it strongly would keep up to
/// `FIELD_CACHE_SLOTS` objects alive for the life of the interpreter, which is a leak a
/// program cannot see and cannot work around.
pub field_cache: Vec<Option<FieldCache>>,
/// The heap total at which `out of memory` was last raised, or 0 when the heap is
/// under its limit. See [`Interp::check_memory`].
pub oom_reported_at: usize,
/// The most stack a JavaScript call chain may use, in bytes of frame state.
///
/// The reference rations the **C stack** here: each call `alloca`s its value slots and
/// `js_check_stack_overflow` compares the stack pointer against `rt->stack_limit`. This
/// interpreter does not recurse in Rust — a call pushes a [`Frame`] instead (see the
/// module comment) — so there is no C stack to run out of, and the C stack is the wrong
/// thing to measure. What a deep recursion actually costs here is *frame state*, so
/// that is what the budget counts: `size_of::<Frame>()` plus the frame's own slots.
///
/// The numbers come out close for the same reason a C frame and a heap frame of this
/// shape cost about the same (both are a few hundred bytes, dominated by the value
/// slots): `qjs --stack-size 1M` bottoms out at depth 1985 on `function f(){ d++; f() }`
/// and this engine lands in the same neighbourhood — measured, not fudged, by
/// `crates/quickjs-cli/tests/limits.rs`.
pub stack_limit: usize,
/// The most frames that may be live at once, whatever the byte budget says.
///
/// A second bound for a reason the reference does not have: our frames cost less than
/// its C frames, so a large `--stack-size` would otherwise buy an unbounded number of
/// them and a `Vec<Frame>` in the hundreds of megabytes. This cap is what keeps
/// `--stack-size 1G` from being a memory bomb.
pub max_frames: usize,
/// The microtask queue, but see `Interp::jobs` — shared with the module machinery.
///
/// Script execution does not return until this has drained, which is what makes
/// `Promise.resolve().then(f)` run `f` before the next statement of the *host* rather
/// than never. Reactions append to it while it drains, so the loop is a `while` and
/// not a `for`.
pub(crate) jobs: std::collections::VecDeque<Job>,
/// Macrotasks: the timers an event loop has to wait for. See [`Task`].
///
/// A `Vec` rather than a heap because the queue is short (a script rarely has more than
/// a handful) and `run_one_turn` has to scan it anyway to find the earliest deadline.
pub(crate) tasks: Vec<Task>,
/// Active `backtrace_barrier` depths: how many bytecode frames were live when the
/// barrier'd evaluation started.
///
/// A trace is truncated to the frames at or inside the innermost barrier, which is what
/// lets a REPL report an error from the line it just evaluated without also reporting
/// its own readline loop.
trace_barriers: Vec<usize>,
/// `os.signal` handlers, keyed by signal number.
///
/// Kept here for the same reason as `fd_handlers`: the collector walks these values, so
/// a handler cannot be freed while it is still registered.
pub(crate) signal_handlers: std::collections::BTreeMap<i32, Value>,
/// `os.setReadHandler`/`os.setWriteHandler` callbacks, keyed by descriptor.
///
/// Kept here rather than in a closure so the collector walks the callbacks: a handler
/// that is only reachable from a host closure could be freed while still registered.
pub(crate) fd_handlers: std::collections::BTreeMap<i32, FdHandlers>,
/// The next timer id.
///
/// Starts at 1 and wraps back to 1 at `INT32_MAX`, matching `js_os_setTimeout`, because
/// the id reaches the script as an `Int32` and `clearTimeout(0)` must never match.
next_timer_id: i32,
/// Errors thrown by timer callbacks, already rendered.
///
/// Kept apart from `unhandled`: that list is rejected promises, and a timer callback
/// that throws has no promise behind it. Both are reported by the host at the end of
/// the loop, but only one is fixed up by a later `.catch`.
timer_errors: Vec<String>,
/// The module registry, keyed by canonical name.
pub modules: std::collections::BTreeMap<String, Gc<crate::module::ModuleRecord>>,
/// How the host turns a specifier into bytes.
pub module_loader: Option<Box<dyn crate::module::ModuleLoader>>,
/// The module the host was asked to run, for `import.meta.main`.
pub main_module_name: Option<String>,
/// Module bodies currently running, innermost last.
///
/// A dynamic `import()` resolves its specifier relative to the module that ran it, and
/// this is the only place that knows which module that is.
pub module_stack: Vec<String>,
/// Rejections that no handler has claimed, with the promise they belong to.
///
/// A `then` registered later prunes its entry, which is why the promise is kept: the
/// reason alone cannot say whether somebody eventually listened.
unhandled: Vec<(Gc<JsObject>, Value)>,
}
impl<'a> Interp<'a> {
/// Build an interpreter over `heap`, with a fresh realm.
pub fn new(heap: &'a Heap) -> Interp<'a> {
let object_proto = JsObject::ordinary(heap, None);
let function_proto = JsObject::function(
heap,
Some(object_proto.clone()),
Callable::Native {
func: |_, _, _| Ok(Value::Undefined),
own_instance: false,
},
);
let array_proto = JsObject::array(heap, Some(object_proto.clone()), 0);
let global = JsObject::ordinary(heap, Some(object_proto.clone()));
let mut interp = Interp {
heap,
realm: Realm {
global,
object_proto,
function_proto,
array_proto,
globals: BTreeMap::new(),
declared: Vec::new(),
lexical_consts: BTreeSet::new(),
errors: BTreeMap::new(),
string_proto: None,
number_proto: None,
boolean_proto: None,
bigint_proto: None,
error_proto: JsObject::ordinary(heap, None),
date_proto: JsObject::ordinary(heap, None),
generator_proto: JsObject::ordinary(heap, None),
promise_proto: None,
async_generator_proto: None,
symbol_proto: None,
symbol_registry: BTreeMap::new(),
internal_slots: std::cell::RefCell::new(BTreeMap::new()),
regexp_proto: None,
regexp_ctor: None,
suppressed_error_proto: None,
disposable_stack_proto: None,
map_proto: None,
set_proto: None,
finalization_registries: Vec::new(),
array_buffer_proto: None,
typed_array_proto: None,
typed_array_protos: Vec::new(),
data_view_proto: None,
async_disposable_stack_proto: None,
iterator_proto: None,
iterator_helper_proto: None,
iterator_wrap_proto: None,
generator_function_proto: None,
async_function_proto: None,
async_generator_function_proto: None,
iterator_protos: std::collections::BTreeMap::new(),
well_known_symbols: WellKnownSymbols::default(),
},
frames: Vec::new(),
eval_scope: None,
eval_depth: 0,
eval_bindings: Vec::new(),
eval_arguments: None,
eval_with: None,
active_eval_scope: None,
eval_caller: None,
pending_new_target: None,
eval_new_target: None,
native_frames: Vec::new(),
current_native: None,
output: String::new(),
output_sink: None,
field_cache: {
let mut cache = Vec::new();
cache.resize_with(FIELD_CACHE_SLOTS, || None);
cache
},
oom_reported_at: 0,
// `JS_DEFAULT_STACK_SIZE`, which is what `qjs` uses when `--stack-size` is
// absent.
stack_limit: DEFAULT_STACK_SIZE,
max_frames: MAX_FRAMES,
jobs: std::collections::VecDeque::new(),
tasks: Vec::new(),
trace_barriers: Vec::new(),
signal_handlers: std::collections::BTreeMap::new(),
fd_handlers: std::collections::BTreeMap::new(),
next_timer_id: 1,
timer_errors: Vec::new(),
modules: std::collections::BTreeMap::new(),
module_loader: None,
main_module_name: None,
module_stack: Vec::new(),
unhandled: Vec::new(),
};
crate::builtins::install(&mut interp);
interp
}
// -- allocation helpers -------------------------------------------------
/// A new string value.
/// A `BigInt` value from a computed big integer.
pub fn bigint(&self, value: crate::bigint::BigInt) -> Value {
Value::BigInt(Gc::new(self.heap, value))
}
/// A string value from bytes that are already WTF-8, which is how a string with an
/// unpaired surrogate reaches a `Value`.
pub fn string_wtf8(&self, text: crate::value::Wtf8) -> Value {
Value::string_wtf8(self.heap, text)
}
/// A new string value, from text that is valid UTF-8.
pub fn string(&self, text: impl Into<String>) -> Value {
Value::string(self.heap, text)
}
/// A new ordinary object with the given prototype.
pub fn new_object(&self, proto: Option<Gc<JsObject>>) -> Gc<JsObject> {
JsObject::ordinary(self.heap, proto)
}
/// A prototype for a built-in iterator, inheriting from `Iterator.prototype`.
///
/// Every built-in iterator has one — `%ArrayIteratorPrototype%`,
/// `%StringIteratorPrototype%` and so on — and the whole point of the chain is that
/// such an iterator *is* an `Iterator`: `[1,2].values().map(f)` resolves `map` by
/// walking up to `Iterator.prototype`. These used to be plain objects hanging off
/// `Object.prototype`, so `.map` was `undefined` and `Object.prototype.toString`
/// said `[object Object]` instead of `[object Array Iterator]`.
///
/// The prototype carries `next`, `[Symbol.iterator]` answering `this`, and the
/// `@@toStringTag` that names it. `next` is passed in because each iterator kind has
/// its own.
pub fn new_iterator_prototype(
&mut self,
tag: &str,
next: crate::object::NativeFn,
) -> Gc<JsObject> {
if let Some(existing) = self.realm.iterator_protos.get(tag) {
return existing.clone();
}
let parent = self.realm.iterator_proto.clone();
let proto = self.new_object(parent);
let next = self.new_native("next", 0, next);
proto.define(
"next",
Property::with_attributes(Value::Object(next), true, false),
);
let key = self.realm.well_known_key(WellKnown::Iterator);
let identity = self.new_native("[Symbol.iterator]", 0, self_iterator);
proto.define_key(
&key,
Property::with_attributes(Value::Object(identity), true, false),
);
let tag_key = self.realm.well_known_key(WellKnown::ToStringTag);
let tag_value = self.string(tag);
proto.define_key(&tag_key, Property::with_attributes(tag_value, false, false));
self.realm
.iterator_protos
.insert(tag.to_string(), proto.clone());
proto
}
/// `[[OwnPropertyKeys]]`, with the `ownKeys` trap when there is one.
///
/// A trap's list is used **as returned**: unlike an ordinary object, whose integer
/// keys the engine sorts, a proxy is specified to hand back exactly what the handler
/// produced. That is why the two branches are not merged.
pub fn object_own_property_keys(
&mut self,
object: &Gc<JsObject>,
) -> JsResult<Vec<PropertyKey>> {
if let Some(keys) = self.proxy_own_keys(object)? {
return Ok(keys);
}
// Integer-like keys ascending, then the string keys in insertion order, then the
// symbols — the order every schema that walks an object expects.
let entries = object.property_entries();
let mut numeric: Vec<u32> = Vec::new();
let mut strings: Vec<PropertyKey> = Vec::new();
let mut symbols: Vec<PropertyKey> = Vec::new();
for (key, _) in entries {
match &key {
// A class-private name is never reported by any enumeration: that is the
// whole mechanism behind `#x`. `Object.getOwnPropertySymbols`,
// `Reflect.ownKeys` and everything built on them (spread, `JSON.stringify`'s
// symbol skip, `Object.assign`) therefore cannot see it.
PropertyKey::Symbol(symbol) if symbol.is_private() => {}
PropertyKey::Symbol(_) => symbols.push(key),
PropertyKey::String(_) => match key.as_str().and_then(array_index) {
Some(index) => {
if !numeric.contains(&index) {
numeric.push(index);
}
}
None => strings.push(key),
},
}
}
numeric.sort_unstable();
let mut keys: Vec<PropertyKey> = numeric
.into_iter()
.map(|index| PropertyKey::from(index.to_string()))
.collect();
keys.extend(strings);
keys.extend(symbols);
Ok(keys)
}
/// The state behind a promise object, or `None` when `object` is not one.
pub fn promise_state(&self, object: &Gc<JsObject>) -> Option<crate::object::PromiseRef> {
object.with(|data| match &data.kind {
ObjectKind::Promise(state) => Some(state.clone()),
_ => None,
})
}
/// A new pending promise.
pub fn new_promise(&mut self) -> Gc<JsObject> {
let proto = self.realm.promise_proto.clone();
let promise = JsObject::ordinary(self.heap, proto);
promise.with_mut(|data| {
data.kind = ObjectKind::Promise(std::rc::Rc::new(std::cell::RefCell::new(
crate::object::PromiseState::default(),
)));
});
promise
}
/// Fulfil `promise`, unless it has already settled.
///
/// Settling is one-shot, and a script can see that: `resolve` called twice, or called
/// after `reject`, changes nothing.
pub fn fulfill_promise(&mut self, promise: &Gc<JsObject>, value: Value) {
let Some(state) = self.promise_state(promise) else {
return;
};
let reactions = {
let mut state = state.borrow_mut();
if !matches!(state.status, crate::object::PromiseStatus::Pending) {
return;
}
state.status = crate::object::PromiseStatus::Fulfilled(value.clone());
std::mem::take(&mut state.reactions)
};
self.queue_reactions(reactions, value, false);
}
/// Reject `promise`, unless it has already settled.
pub fn reject_promise(&mut self, promise: &Gc<JsObject>, reason: Value) {
let Some(state) = self.promise_state(promise) else {
return;
};
let reactions = {
let mut state = state.borrow_mut();
if !matches!(state.status, crate::object::PromiseStatus::Pending) {
return;
}
state.status = crate::object::PromiseStatus::Rejected(reason.clone());
std::mem::take(&mut state.reactions)
};
// Remember it only when nothing is listening *now*; a `then` registered later
// prunes the entry in `perform_then`.
if reactions.is_empty() {
self.unhandled.push((promise.clone(), reason.clone()));
}
self.queue_reactions(reactions, reason, true);
}
/// Queue one job per reaction.
///
/// This is what makes `then` asynchronous even on an already-settled promise:
/// `p.then(f); g()` calls `g` first.
fn queue_reactions(
&mut self,
reactions: Vec<crate::object::PromiseReaction>,
argument: Value,
rejected: bool,
) {
for reaction in reactions {
let handler = if rejected {
reaction.on_rejected.clone()
} else {
reaction.on_fulfilled.clone()
};
self.jobs.push_back(Job::Reaction {
handler,
argument: argument.clone(),
rejected,
promise: reaction.promise,
});
}
}
/// `NewPromiseCapability` as the executor receives it: the promise plus its
/// resolve/reject pair.
///
/// The pair are callable objects rather than Rust closures, because the script gets
/// them and may stash them for later.
pub fn new_promise_capability(&mut self) -> (Gc<JsObject>, Value, Value) {
let promise = self.new_promise();
let resolve = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::PromiseResolve(promise.clone()),
);
let reject = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::PromiseReject(promise.clone()),
);
(promise, Value::Object(resolve), Value::Object(reject))
}
/// `std.evalScript(source, { async: true })`: the completion value delivered through a
/// promise, as `{ value: completion }`.
///
/// The three cases are the reference's, and they differ on purpose:
///
/// * a **syntax** error throws synchronously — `JS_Eval` reports a parse failure as an
/// exception before any promise exists;
/// * a **runtime** error rejects the returned promise;
/// * success resolves it with `{ value }`, where the value is the script's completion
/// value **literally**. It is not awaited: `std.evalScript("Promise.resolve(7)",
/// {async: true})` answers `{value: <the promise>}`, and only an explicit top-level
/// `await` produces `{value: 7}`. That is the reference's `JS_Eval` with
/// `JS_EVAL_FLAG_ASYNC`, which stores the completion value rather than adopting it.
///
/// # What this does not do
///
/// The reference's `JS_EVAL_FLAG_ASYNC` also permits **top-level `await`**: the script
/// is evaluated in an async context, so `await` outside a function is legal. That needs
/// the parser and the suspension machinery to accept a suspended top-level script, and
/// is not implemented — such source is a syntax error here. Everything else about the
/// flag is reproduced.
pub fn eval_in_global_async(&mut self, source: &str, barrier: bool) -> JsResult<Value> {
// The barrier goes up before compiling: a *parse* failure is reported with a trace
// too, and the reference truncates that one at the same place as a runtime failure.
if barrier {
let depth = self.frames.len();
self.trace_barriers.push(depth);
}
// Compiled before the promise exists, so a parse failure propagates as a throw
// instead of being folded into a rejection.
let compiled = match self.compile_global_script(source, EVAL_SCRIPT_FILE, true) {
Ok(compiled) => compiled,
Err(error) => {
if barrier {
self.trace_barriers.pop();
}
return Err(error);
}
};
// An async body answers its own promise, and that promise is what the reference
// hands back — the same object, not a wrapper around it. Its resolution value is
// the `{value}` object the compiled epilogue built.
if compiled.kind.is_async() {
let strict = compiled.strict;
let promise = self.run_eval_script(compiled, strict)?;
if barrier {
self.trace_barriers.pop();
}
return Ok(promise);
}
let (promise, resolve, reject) = self.new_promise_capability();
let strict = compiled.strict;
match self.run_eval_script(compiled, strict) {
Ok(value) => {
// `{ value }` is a plain object, so resolving with it adopts nothing even
// when the completion value is itself a promise.
let wrapped = self.new_object(Some(self.realm.object_proto.clone()));
self.set_property(&wrapped, "value", value)?;
self.call_value(resolve, Value::Undefined, vec![Value::Object(wrapped)])?;
}
Err(Signal::Throw(error)) => {
self.call_value(reject, Value::Undefined, vec![error])?;
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
if barrier {
self.trace_barriers.pop();
}
Ok(Value::Object(promise))
}
/// A promise that resolves after `delay_ms`, which is `os.sleepAsync`.
///
/// Built on [`Interp::schedule_timer`] rather than a second queue, so the resolution is
/// ordered against `os.setTimeout` by the same event loop. The resolver is stored in the
/// timer table, where the collector already scans it.
pub fn sleep_async(&mut self, delay_ms: f64) -> JsResult<Value> {
let (promise, resolve, _reject) = self.new_promise_capability();
let resolve = self.new_host_function(
"resolve",
1,
Box::new(
move |interp: &mut Interp<'_>, _this: Value, args: &[Value]| {
let value = args.first().cloned().unwrap_or(Value::Undefined);
interp.call_value(resolve.clone(), Value::Undefined, vec![value])
},
),
);
// The timer keeps the resolver alive: it is a property of the timer entry, and the
// timer table is part of what the collector walks.
self.schedule_timer(Value::Object(resolve), delay_ms);
Ok(Value::Object(promise))
}
/// `PromiseResolve(value)`: the value itself when it is already a promise, otherwise a
/// new promise resolved with it.
pub fn promise_resolve_value(&mut self, value: Value) -> JsResult<Value> {
if let Value::Object(object) = &value
&& self.promise_state(object).is_some()
{
return Ok(value);
}
let promise = self.new_promise();
self.resolve_promise_with(&promise, value)?;
Ok(Value::Object(promise))
}
/// `ResolvePromise`: fulfil, unless the value is a thenable — then follow it.
pub fn resolve_promise_with(&mut self, promise: &Gc<JsObject>, value: Value) -> JsResult<()> {
// Resolving a promise with itself is a `TypeError`. The check exists because the
// alternative is a chain of self-reactions that never settles.
if let Value::Object(object) = &value
&& object.ptr_eq(promise)
{
// The spec rejects *and* throws, so both the promise and the caller see it.
let thrown = self.type_error("cannot resolve a promise with itself");
let Signal::Throw(error) = thrown.clone() else {
unreachable!("type_error always throws")
};
self.reject_promise(promise, error);
return Err(thrown);
}
let then = match &value {
Value::Object(object) => self.get_property(object, "then")?,
_ => None,
};
let callable_then = matches!(&then, Some(Value::Object(object)) if object.is_callable());
if !callable_then {
// Not a thenable: a non-callable `then` makes the value fulfil as it is.
self.fulfill_promise(promise, value);
return Ok(());
}
// Adopting a thenable is asynchronous — `then` is called from a job, never from
// inside `resolve`, so a script cannot make a promise settle synchronously.
self.jobs.push_back(Job::Thenable {
then: then.expect("checked callable"),
value,
promise: promise.clone(),
});
Ok(())
}
/// `Promise.prototype.then`: register a reaction and answer the derived promise.
pub fn perform_then(
&mut self,
promise: &Gc<JsObject>,
on_fulfilled: Option<Value>,
on_rejected: Option<Value>,
) -> JsResult<Value> {
let Some(state) = self.promise_state(promise) else {
return Err(self.type_error("not a promise"));
};
// Whether a rejection handler is being attached decides whether a recorded
// rejection stops being unhandled, so it is read before the values are moved.
let has_rejection_handler = on_rejected.is_some();
let derived = self.new_promise();
let reaction = crate::object::PromiseReaction {
on_fulfilled,
on_rejected,
promise: derived.clone(),
};
let settled = {
let mut state = state.borrow_mut();
// A rejection somebody reacts to is no longer unhandled.
state.handled = true;
match &state.status {
crate::object::PromiseStatus::Pending => {
state.reactions.push(reaction.clone());
None
}
crate::object::PromiseStatus::Fulfilled(value) => Some((value.clone(), false)),
crate::object::PromiseStatus::Rejected(reason) => Some((reason.clone(), true)),
}
};
// A `catch`/`then` attached to an already-rejected promise, or to one that rejects
// later, means somebody is listening after all.
if has_rejection_handler {
self.unhandled.retain(|(owner, _)| !owner.ptr_eq(promise));
}
if let Some((argument, rejected)) = settled {
let handler = if rejected {
reaction.on_rejected
} else {
reaction.on_fulfilled
};
self.jobs.push_back(Job::Reaction {
handler,
argument,
rejected,
promise: reaction.promise,
});
}
Ok(Value::Object(derived))
}
/// The rejections nobody listened to, formatted the way the reference reports them.
///
/// Each line is `Possibly unhandled promise rejection: <value>` followed by the stack
/// when there is one. The adjective is the reference's: a rejection is only *possibly*
/// unhandled at the point the queue drains, because a `then` may still be attached
/// before the host acts on it.
pub fn take_unhandled_rejections(&mut self) -> Vec<String> {
let entries = std::mem::take(&mut self.unhandled);
// Somebody may have attached a handler after the rejection was recorded, so the
// promise is the authority rather than the list.
let still_unhandled: Vec<Value> = entries
.into_iter()
.filter_map(|(promise, reason)| {
let handled = self
.promise_state(&promise)
.map(|state| state.borrow().handled)
.unwrap_or(true);
(!handled).then_some(reason)
})
.collect();
still_unhandled
.into_iter()
.map(|reason| {
let text = self.describe_exception(&reason);
let stack = match &reason {
Value::Object(object) => self
.get_property(object, "stack")
.ok()
.flatten()
.map(|value| value.to_js_string())
// `stack` carries a trailing newline of its own, which would leave
// a blank line between two reported rejections.
.map(|text| text.trim_end().to_string())
.unwrap_or_default(),
_ => String::new(),
};
if stack.is_empty() {
format!("Possibly unhandled promise rejection: {text}")
} else {
format!("Possibly unhandled promise rejection: {text}\n{stack}")
}
})
.collect()
}
/// Run the microtask queue to exhaustion.
///
/// Jobs queued while draining run in the same drain, FIFO, which is what makes
/// `Promise.resolve().then(f).then(g)` call `f` before `g`.
/// Parse `text` in the extended dialect that `std.parseExtJSON` exposes.
///
/// Comments, unquoted keys, single quotes, trailing commas, `+` and `.1` numbers,
/// `Infinity`/`NaN`, and `0x`/`0o`/`0b` literals — the same relaxations the reference
/// gates behind `JS_PARSE_JSON_EXT`. `JSON.parse` does not see any of them.
pub fn parse_ext_json(&mut self, text: &str) -> JsResult<Value> {
crate::builtins::json::parse_text(self, text, true)
}
/// Schedule `callback` to run after `delay_ms` milliseconds, returning its id.
///
/// A negative or `NaN` delay runs as soon as possible rather than being rejected: the
/// reference passes the delay through `JS_ToInt64`, so `setTimeout(f, NaN)` schedules
/// immediately, and `setTimeout(f, -1)` is ordinary code.
///
/// The callback is called with **no arguments**. The reference stores only the function
/// (`th->func = JS_DupValue(ctx, func)`) and calls it bare, so the `setTimeout(f, 0, a,
/// b)` passthrough that browsers offer does not exist here — verified against `qjs`:
/// extra arguments arrive as `undefined`.
pub fn schedule_timer(&mut self, callback: Value, delay_ms: f64) -> i32 {
let id = self.next_timer_id;
// Wrap rather than grow: the id reaches the script as an `Int32`.
self.next_timer_id = if self.next_timer_id == i32::MAX {
1
} else {
self.next_timer_id + 1
};
let due_at = std::time::Instant::now() + clamp_delay(delay_ms);
self.tasks.push(Task::Timer {
id,
callback,
due_at,
});
id
}
/// Cancel a pending timer. `false` when the id is unknown or already fired, which is
/// not an error — `clearTimeout` on a stale id is a no-op in the reference too.
pub fn cancel_timer(&mut self, id: i32) -> bool {
let before = self.tasks.len();
self.tasks.retain(|task| match task {
Task::Timer { id: task_id, .. } => *task_id != id,
});
self.tasks.len() != before
}
/// How long until the earliest pending timer, or `None` when none is pending.
pub fn next_timeout(&self) -> Option<std::time::Duration> {
let now = std::time::Instant::now();
self.tasks
.iter()
.map(|task| match task {
Task::Timer { due_at, .. } => due_at.saturating_duration_since(now),
})
.min()
}
/// Run one turn of the event loop.
///
/// Structurally this is `js_std_loop`'s body minus the blocking: drain microtasks, then
/// run at most one due timer, then drain again. The blocking belongs to the caller
/// because `quickjs-pure` has no dependencies and no platform knowledge — [`Turn::Wait`]
/// is how it asks.
///
/// One timer per turn rather than all of them: a timer callback may schedule another
/// timer, and running the whole batch would let a self-rescheduling `setTimeout(f, 0)`
/// starve the microtask queue.
pub fn run_one_turn(&mut self) -> JsResult<Turn> {
self.drain_jobs()?;
let now = std::time::Instant::now();
// Earliest due first: timers are not ordered by insertion, and `setTimeout(f, 100)`
// followed by `setTimeout(g, 1)` must run `g` first.
let due = self
.tasks
.iter()
.enumerate()
.filter(|(_, task)| match task {
Task::Timer { due_at, .. } => *due_at <= now,
})
.min_by_key(|(_, task)| match task {
Task::Timer { due_at, .. } => *due_at,
})
.map(|(index, _)| index);
let Some(index) = due else {
// The reference caps its wait at `min_delay = 10000` (ten seconds) when a
// descriptor is registered but no timer is closer, so a script that never
// touches the descriptor still gets control back. Without a cap a handler on a
// descriptor that is never ready would block for as long as the host allows.
let timeout = self
.next_timeout()
.unwrap_or(std::time::Duration::from_secs(10))
.min(std::time::Duration::from_secs(10));
if !self.fd_handlers.is_empty() {
// Blocking on a timer while descriptors are registered would starve them,
// and blocking on the descriptors while a timer is due would delay it; the
// host polls with the earlier of the two deadlines, which this timeout is.
return Ok(Turn::Poll(timeout));
}
return Ok(match self.next_timeout() {
Some(wait) => Turn::Wait(wait),
None => Turn::Done,
});
};
let Task::Timer { callback, .. } = self.tasks.remove(index);
let this = Value::Undefined;
// A throwing timer callback must not stop the loop: the reference reports it and
// keeps going, because the alternative is that one bad timer silently stops every
// later one.
// No arguments: see `schedule_timer` — the reference calls the callback bare.
match self.call_value(callback, this, Vec::new()) {
Ok(_) => {}
Err(Signal::Throw(value)) => {
let text = self.describe_exception(&value);
self.timer_errors.push(text);
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
self.drain_jobs()?;
Ok(Turn::Ran)
}
/// Register or clear one direction of an `os.setReadHandler`/`os.setWriteHandler` pair.
///
/// `callback` of `None` unregisters that direction, and an entry with neither direction
/// left is removed — the reference does the same, so a script can rely on
/// `os.setReadHandler(fd, null)` stopping the loop from waiting on that descriptor.
pub fn set_fd_handler(&mut self, fd: i32, readable: bool, callback: Option<Value>) {
let entry = self.fd_handlers.entry(fd).or_default();
if readable {
entry.read = callback;
} else {
entry.write = callback;
}
if entry.read.is_none() && entry.write.is_none() {
self.fd_handlers.remove(&fd);
}
}
/// Register or clear an `os.signal` handler.
///
/// `None` removes it; the host separately restores the default disposition or sets
/// ignore, because which of those happened is the script's choice (`null` versus
/// `undefined`) and not something this table can express.
pub fn set_signal_handler(&mut self, signal: i32, handler: Option<Value>) {
match handler {
Some(handler) => {
self.signal_handlers.insert(signal, handler);
}
None => {
self.signal_handlers.remove(&signal);
}
}
}
/// Run the handler for `signal`, reporting whether one ran.
///
/// A throwing handler is reported and the loop continues, like a timer's.
pub fn fire_signal(&mut self, signal: i32) -> JsResult<bool> {
let Some(handler) = self.signal_handlers.get(&signal).cloned() else {
return Ok(false);
};
match self.call_value(handler, Value::Undefined, Vec::new()) {
Ok(_) => {}
Err(Signal::Throw(value)) => {
let text = self.describe_exception(&value);
self.timer_errors.push(text);
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
self.drain_jobs()?;
Ok(true)
}
/// The descriptors the host has to poll, and in which direction.
///
/// Empty means the loop has nothing to wait for on any descriptor. The host turns each
/// entry into a `pollfd` with `POLLIN`/`POLLOUT` — the same events the reference's
/// `os_poll` asks for.
pub fn pending_fds(&self) -> Vec<FdInterest> {
self.fd_handlers
.iter()
.map(|(fd, entry)| FdInterest {
fd: *fd,
read: entry.read.is_some(),
write: entry.write.is_some(),
})
.collect()
}
/// Call the handler registered for `fd`, reporting whether one ran.
///
/// `readable`/`writable` come from `poll(2)`. The reference calls **one** handler per
/// poll iteration and then restarts the loop, because the handler may have changed the
/// registration list underneath it; doing the same here keeps a handler that
/// unregisters itself from being called with stale state.
///
/// A throwing handler is reported and the loop continues — a bad handler must not stop
/// every later one, exactly as with timers.
pub fn fire_fd(&mut self, fd: i32, readable: bool, writable: bool) -> JsResult<bool> {
let Some(entry) = self.fd_handlers.get(&fd) else {
return Ok(false);
};
let callback = if readable {
entry.read.clone()
} else if writable {
entry.write.clone()
} else {
None
};
let Some(callback) = callback else {
return Ok(false);
};
self.drain_jobs()?;
match self.call_value(callback, Value::Undefined, Vec::new()) {
Ok(_) => {}
Err(Signal::Throw(value)) => {
let text = self.describe_exception(&value);
self.timer_errors.push(text);
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
self.drain_jobs()?;
Ok(true)
}
/// Take the errors timer callbacks threw, leaving the list empty.
pub fn take_timer_errors(&mut self) -> Vec<String> {
std::mem::take(&mut self.timer_errors)
}
/// Whether anything is still pending: a microtask or a timer.
pub fn has_pending_work(&self) -> bool {
!self.jobs.is_empty() || !self.tasks.is_empty() || !self.fd_handlers.is_empty()
}
/// Whether a timer or a microtask is pending, ignoring descriptor handlers.
pub fn has_pending_timers_or_jobs(&self) -> bool {
!self.jobs.is_empty() || !self.tasks.is_empty()
}
/// Run every queued microtask, including ones queued while draining.
pub fn drain_jobs(&mut self) -> JsResult<()> {
// A self-perpetuating chain (`function f(){ Promise.resolve().then(f) } f()`)
// would spin forever, so the drain is bounded the way the call stack is.
let mut budget = 1_000_000usize;
while let Some(job) = self.jobs.pop_front() {
if budget == 0 {
return Err(Signal::Throw(
self.make_error("InternalError", "promise job queue did not drain"),
));
}
budget -= 1;
self.run_job(job)?;
// `FinalizationRegistry` at the job boundary: this is the safe point where the
// engine may run script that the script itself did not ask for. Doing it here
// rather than from a drop hook is what keeps the callback out of arbitrary
// engine bookkeeping — see `builtins::weak`'s header.
crate::builtins::weak::drain_finalizers(self)?;
}
// A script that queued no jobs may still have dropped targets, so the checkpoint
// runs once more after the queue empties.
crate::builtins::weak::drain_finalizers(self)?;
Ok(())
}
/// Every live `FinalizationRegistry`, as `(handle, state)` pairs.
///
/// The registry list is weak, so a registry the script dropped answers `upgrade` with
/// `None` and is swept out here. This is also where the realm learns that a registry
/// died, which is why it is a mutation rather than a read.
pub fn live_registries(&mut self) -> Vec<(Gc<JsObject>, crate::object::FinalizationState)> {
let handles = std::mem::take(&mut self.realm.finalization_registries);
let mut live = Vec::with_capacity(handles.len());
for handle in handles {
let Some(object) = handle.upgrade() else {
continue;
};
let Some(state) = object.finalization_state() else {
continue;
};
live.push((object.clone(), *state));
self.realm.finalization_registries.push(object.downgrade());
}
live
}
/// Run one microtask.
fn run_job(&mut self, job: Job) -> JsResult<()> {
match job {
Job::Reaction {
handler: None,
argument,
rejected,
promise,
} => {
// No handler: the settlement passes straight through to the derived
// promise, which is why `p.catch(f)` ignores fulfilments.
if rejected {
self.reject_promise(&promise, argument);
} else {
self.fulfill_promise(&promise, argument);
}
Ok(())
}
Job::Reaction {
handler: Some(handler),
argument,
promise,
..
} => match self.call_value(handler, Value::Undefined, vec![argument]) {
Ok(answer) => {
if let Err(Signal::Throw(error)) = self.resolve_promise_with(&promise, answer) {
self.reject_promise(&promise, error);
}
Ok(())
}
Err(Signal::Throw(reason)) => {
self.reject_promise(&promise, reason);
Ok(())
}
Err(other) => Err(other),
},
Job::Thenable {
then,
value,
promise,
} => {
// §27.2.2.2: call the thenable's `then` with this promise's own pair, so
// the value it produces settles `promise`.
// The thenable's `then` gets this promise's own pair, so whatever it
// produces settles `promise`.
let resolve = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::PromiseResolve(promise.clone()),
);
let reject = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::PromiseReject(promise.clone()),
);
match self.call_value(
then,
value,
vec![Value::Object(resolve), Value::Object(reject)],
) {
Ok(_) => Ok(()),
Err(Signal::Throw(reason)) => {
self.reject_promise(&promise, reason);
Ok(())
}
Err(other) => Err(other),
}
}
Job::Resume {
coroutine,
promise,
kind,
value,
} => self.drive_async(&coroutine, &promise, kind, value),
Job::GeneratorResume {
generator,
promise,
kind,
value,
} => self.step_async_generator(&generator, &promise, kind, value),
Job::Import {
specifier,
referrer,
promise,
} => {
self.run_dynamic_import(&specifier, &referrer, &promise);
Ok(())
}
}
}
/// One element of a `Promise` combinator settled.
///
/// The shared state object holds the results array, the number still outstanding, and
/// the combinator's own resolve/reject pair. `all` fills by index so the answer keeps
/// the input order; `race` settles on the first element and ignores the rest, which a
/// one-shot settle gives for free.
fn settle_combinator(
&mut self,
state: &Gc<JsObject>,
index: usize,
fulfilled: bool,
value: Value,
) -> JsResult<()> {
let kind = state
.own_property("kind")
.and_then(|property| property.value)
.map(|value| value.to_int32())
.unwrap_or(0);
let outcome = state
.own_property("outcome")
.and_then(|property| property.value);
let Some(Value::Object(outcome)) = outcome else {
return Ok(());
};
if kind == 3 {
// `race`: the first settlement wins; the rest are ignored by the one-shot rule.
if fulfilled {
self.resolve_promise_with(&outcome, value)?;
} else {
self.reject_promise(&outcome, value);
}
return Ok(());
}
let results = state
.own_property("results")
.and_then(|property| property.value)
.and_then(|value| value.as_object().cloned());
let remaining = state
.own_property("remaining")
.and_then(|property| property.value)
.map(|value| value.to_int32())
.unwrap_or(0);
match kind {
0 => {
// `all`: the first rejection wins outright.
if !fulfilled {
self.reject_promise(&outcome, value);
return Ok(());
}
let Some(results) = &results else {
return Ok(());
};
results.array_set(self.heap, index, value);
}
1 => {
// `allSettled`: never rejects; each slot records what happened.
let Some(results) = &results else {
return Ok(());
};
let record = self.new_object(Some(self.realm.object_proto.clone()));
if fulfilled {
record.define_data("status", self.string("fulfilled"));
record.define_data("value", value);
} else {
record.define_data("status", self.string("rejected"));
record.define_data("reason", value);
}
results.array_set(self.heap, index, Value::Object(record));
}
// `any`: the first fulfilment wins; a full sweep of rejections does not settle
// here — it is handled by the count below.
2 if fulfilled => {
self.resolve_promise_with(&outcome, value)?;
return Ok(());
}
_ => {}
}
let left = remaining - 1;
state.define_data("remaining", Value::Int(left));
if left != 0 {
return Ok(());
}
match kind {
0 | 1 => {
let Some(results) = results else {
return Ok(());
};
self.resolve_promise_with(&outcome, Value::Object(results))?;
}
2 => {
let error = self.make_error("AggregateError", "all promises were rejected");
self.reject_promise(&outcome, error);
}
_ => {}
}
Ok(())
}
/// Build an async function's result: a promise whose body runs as a coroutine.
pub fn new_async_function(
&mut self,
function: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Value> {
let promise = self.new_promise();
let coroutine = self.new_coroutine(function, this, args)?;
if let Err(Signal::Throw(reason)) =
self.drive_async(&coroutine, &promise, ResumeKind::Start, Value::Undefined)
{
self.reject_promise(&promise, reason);
}
Ok(Value::Object(promise))
}
/// The coroutine an async body runs in.
///
/// Generator-shaped and never exposed to the script: what makes `await` work is the
/// same frame-detach-and-resume the generator protocol uses, so this reuses it rather
/// than growing a second state machine. The difference is only in the ending — a
/// `return` fulfils the caller's promise here instead of producing `{value, done}`.
/// Run `code` as an async function body, answering the promise it settles.
///
/// Used for a module with top-level `await`: its body is an async function whose
/// caller is the host, so there is no function object to pass through
/// [`Interp::new_async_function`].
pub fn run_async_body(
&mut self,
code: Gc<CompiledCode>,
env: Gc<ClosureEnv>,
this: Value,
) -> JsResult<Value> {
let promise = self.new_promise();
let coroutine = self.new_coroutine_from(code, env, this, Vec::new())?;
if let Err(Signal::Throw(reason)) =
self.drive_async(&coroutine, &promise, ResumeKind::Start, Value::Undefined)
{
self.reject_promise(&promise, reason);
}
Ok(Value::Object(promise))
}
fn new_coroutine(
&mut self,
function: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Gc<JsObject>> {
let Some(object) = function.as_object().cloned() else {
return Err(self.type_error("not a function"));
};
let Some((code, env)) = bytecode_of(&object) else {
return Err(self.type_error("not a function"));
};
self.new_coroutine_from(code, env, this, args)
}
/// Build a coroutine over an already-resolved body.
fn new_coroutine_from(
&mut self,
code: Gc<CompiledCode>,
env: Gc<ClosureEnv>,
this: Value,
args: Vec<Value>,
) -> JsResult<Gc<JsObject>> {
let function = Value::Undefined;
self.push_frame(code, env, this, args, function)?;
let frame = self.frames.pop().expect("push_frame just pushed it");
let state = GeneratorState {
frame: Some(frame),
phase: GeneratorPhase::SuspendedStart,
parked_on_await: false,
parked_on_yield_star: false,
requests: std::collections::VecDeque::new(),
async_busy: false,
};
let coroutine = self.new_object(Some(self.realm.generator_proto.clone()));
coroutine.with_mut(|data| {
data.kind = ObjectKind::Generator(Box::new(state));
});
Ok(coroutine)
}
/// Step a coroutine: `(value, done)`, where `done == false` means it parked on an
/// `await` and `value` is what it awaited.
fn resume_coroutine(
&mut self,
coroutine: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<(Value, bool)> {
let Some(mut state) = coroutine.with_mut(|data| match &mut data.kind {
ObjectKind::Generator(state) => Some(std::mem::replace(
state.as_mut(),
GeneratorState {
frame: None,
phase: GeneratorPhase::Executing,
parked_on_await: false,
parked_on_yield_star: false,
requests: std::collections::VecDeque::new(),
async_busy: false,
},
)),
_ => None,
}) else {
return Err(self.type_error("not a coroutine"));
};
let result = self.step_generator(&mut state, kind, value);
coroutine.with_mut(|data| {
data.kind = ObjectKind::Generator(Box::new(state));
});
let (value, done, _ready, _reason) = result?;
Ok((value, done))
}
/// Advance an async body and settle its promise with the outcome.
fn drive_async(
&mut self,
coroutine: &Gc<JsObject>,
promise: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<()> {
match self.resume_coroutine(coroutine, kind, value) {
Ok((value, true)) => {
// `Ok` with `done` means the body finished *normally*, even when it was
// resumed by a `throw` — `try { await p } catch {}` completes by
// returning. Only the `Err` arm below is a rejection.
//
// Resolving rather than fulfilling matters: `return promise` in an async
// body adopts the inner promise, so `async function f(){ return
// Promise.resolve(1) }` answers a promise *of 1*, not a promise of a
// promise.
if let Err(Signal::Throw(reason)) = self.resolve_promise_with(promise, value) {
self.reject_promise(promise, reason);
}
Ok(())
}
Ok((value, false)) => {
// Parked on `await`: follow what it awaited and resume with the outcome.
let awaited = match self.promise_resolve_value(value) {
Ok(awaited) => awaited,
Err(Signal::Throw(error)) => {
self.reject_promise(promise, error);
return Ok(());
}
Err(other) => return Err(other),
};
self.hook_await(awaited, coroutine, promise)
}
Err(Signal::Throw(reason)) => {
self.reject_promise(promise, reason);
Ok(())
}
Err(other) => Err(other),
}
}
/// Arrange for `coroutine` to resume when `awaited` settles.
fn hook_await(
&mut self,
awaited: Value,
coroutine: &Gc<JsObject>,
promise: &Gc<JsObject>,
) -> JsResult<()> {
let resumed = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::AwaitResume {
coroutine: coroutine.clone(),
promise: promise.clone(),
kind: ResumeKind::Next,
},
);
let raised = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::AwaitResume {
coroutine: coroutine.clone(),
promise: promise.clone(),
kind: ResumeKind::Throw,
},
);
// `perform_then` on a non-promise would be wrong, and `promise_resolve_value`
// guarantees one; the fallback keeps a mis-shaped value from panicking.
let Value::Object(awaited) = &awaited else {
self.jobs.push_back(Job::Resume {
coroutine: coroutine.clone(),
promise: promise.clone(),
kind: ResumeKind::Next,
value: awaited,
});
return Ok(());
};
self.perform_then(
awaited,
Some(Value::Object(resumed)),
Some(Value::Object(raised)),
)?;
Ok(())
}
/// `[[GetOwnProperty]]` on an ordinary object: the property table, then the two
/// things an engine keeps outside it — array elements and an array's `length`.
fn ordinary_own_property(object: &Gc<JsObject>, key: &PropertyKey) -> Option<Property> {
if let Some(property) = object.own_property_key(key) {
return Some(property);
}
// An array's elements live in their own vector rather than the property table, so
// a descriptor for one has to be synthesized — with the attributes an element has.
if let Some(index) = key.as_str().and_then(array_index)
&& let Some(value) = object.array_get(index as usize)
&& !matches!(value, Value::Hole)
{
return Some(Property::data(value));
}
// `length` is an own property of an array that is not in its property table
// either: `[].hasOwnProperty("length")` is true, and its descriptor is writable
// and non-enumerable.
if key.as_str() == Some("length")
&& let Some(length) = object.array_length()
{
return Some(Property {
value: Some(crate::object::length_value(length)),
writable: object.length_writable(),
enumerable: false,
configurable: false,
getter: None,
setter: None,
});
}
None
}
/// `[[GetOwnProperty]]`, with the `getOwnPropertyDescriptor` trap when there is one.
pub fn object_get_own_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<Option<Property>> {
if let Some(answer) = self.proxy_get_own_property(object, key)? {
return Ok(answer);
}
Ok(Self::ordinary_own_property(object, key))
}
/// `[[DefineOwnProperty]]`, with the `defineProperty` trap when there is one.
pub fn object_define_own_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
property: Property,
) -> JsResult<bool> {
if let Some(accepted) = self.proxy_define_property(object, key, property.clone())? {
return Ok(accepted);
}
// An array index is stored in the element vector, which can hold a plain value and
// nothing else. An accessor — or a data property with non-default attributes —
// therefore has to live in the property table, and the element slot is blanked so
// the table is what a read finds. Without this `Object.defineProperty(a, "0",
// {get})` stored the accessor where no read ever looked, and the array kept
// answering its old value.
// An array's `length` is not stored in the property table, so defining it has to
// run `ArraySetLength`: it truncates or extends the element vector and refuses a
// value that is not a valid index. Without this `Object.defineProperty(a, "length",
// {value: 1})` reported success and changed nothing.
if key.as_str() == Some("length") && object.is_array() {
// `{writable: false}` on `length` does not change the length, but it does make
// it read-only — after which neither a later `{writable: true}` nor a store
// past the end may succeed.
if property.value.is_none() {
if !object.length_writable() && property.writable {
return Ok(false);
}
object.set_length_writable(property.writable);
return Ok(true);
}
if !object.length_writable() {
return Ok(false);
}
}
if key.as_str() == Some("length")
&& object.is_array()
&& let Some(value) = property.value.clone()
{
let requested = value.to_number();
let valid = requested.is_finite()
&& requested >= 0.0
&& requested.fract() == 0.0
&& requested <= u32::MAX as f64;
if !valid {
return Err(self.range_error("invalid array length"));
}
// Truncating deletes the elements above the new length, and a non-configurable
// one cannot be deleted: `Object.defineProperty(a, "3", {configurable: false})`
// makes `a.length = 2` fail rather than quietly drop it.
let current = object.array_length().unwrap_or(0);
let requested = requested as usize;
if requested < current && object.first_undeletable_at_or_above(requested).is_some() {
return Ok(false);
}
object.array_set_length(self.heap, requested);
return Ok(true);
}
let plain = property.writable
&& property.enumerable
&& property.configurable
&& !property.is_accessor();
if let Some(index) = key.as_str().and_then(array_index)
&& object.is_array()
{
// Growing the array needs a writable `length`: after
// `Object.defineProperty(a, "length", {writable: false})` a new index cannot
// be added.
if (index as usize) >= object.array_length().unwrap_or(0) && !object.length_writable() {
return Ok(false);
}
if plain && property.value.is_some() {
// An ordinary element: the vector is where a read looks first, so it has
// to be the vector that changes — unless the index is too far out to be
// worth a dense slot. `a[2**32 - 2] = 1` is legal and costs the reference
// one property, because past its own limit it stores the index as an
// ordinary key. Falling through to `define_key` below is the same choice,
// and the alternative was worse: the store used to be *dropped* and the
// length left at 0, which is a silent wrong answer.
let value = property.value.clone().unwrap_or(Value::Undefined);
object.delete_key(key);
if object.array_set(self.heap, index as usize, value) {
return Ok(true);
}
object.define_key(key, property);
// A sparse store past the dense limit still extends `length`, which is what
// `a[a.length] = v` relies on — but only extends: shortening would delete
// the sparse properties above the index, including this one.
if index as usize + 1 > object.array_length().unwrap_or(0) {
object.array_set_length(self.heap, index as usize + 1);
}
return Ok(true);
}
object.define_key(key, property);
// The slot stays, so `length` does not shrink; a hole is not an element, so
// the table entry is now what a read finds.
object.array_set(self.heap, index as usize, Value::Hole);
return Ok(true);
}
object.define_key(key, property);
Ok(true)
}
/// Build a proxy object over `target`, with its traps read from `handler`.
pub fn new_proxy(&self, target: Gc<JsObject>, handler: Value) -> Gc<JsObject> {
JsObject::proxy(self.heap, target, handler)
}
/// The `revoke` function for a proxy's slots.
pub fn new_revoker(&self, slots: crate::object::ProxyRef) -> Gc<JsObject> {
JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::Revoker(slots),
)
}
/// A new array.
pub fn new_array(&self, elements: Vec<Value>) -> Gc<JsObject> {
let array = JsObject::array(
self.heap,
Some(self.realm.array_proto.clone()),
elements.len(),
);
for (index, element) in elements.into_iter().enumerate() {
array.array_set(self.heap, index, element);
}
array
}
/// A new native function object.
///
/// The observable `name` and `length` are installed as properties here, which is
/// what `Function.prototype.name` and `.length` read; nothing else stores them,
/// so `f.name = "x"` behaves like the writable property it is.
/// A native constructor that builds its own instance after validating its arguments.
///
/// `new` hands it `this = undefined` rather than a pre-made object, so any argument
/// check runs before `new.target.prototype` is read.
pub fn new_native_ctor(
&self,
name: impl Into<String>,
length: u32,
func: NativeFn,
) -> Gc<JsObject> {
let function = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::Native {
func,
own_instance: true,
},
);
function.set_constructible(true);
function.define(
"length",
Property::name_or_length(Value::Int(length as i32)),
);
function.define("name", Property::name_or_length(self.string(name)));
function
}
/// A native constructor that lets `new` build the instance first.
///
/// The ordinary case for a built-in constructor: `new Array(3)` has no argument
/// validation that must precede reading `new.target.prototype`, so the object can be
/// created before the implementation runs. Distinct from [`Interp::new_native`],
/// which makes a *method* and therefore not a constructor at all.
pub fn new_native_constructor(
&self,
name: impl Into<String>,
length: u32,
func: NativeFn,
) -> Gc<JsObject> {
let function = self.new_native(name, length, func);
function.set_constructible(true);
function
}
/// A new host function object: a native closure that carries its own state.
///
/// Same observable surface as [`Interp::new_native`] — `name`, `length` and, for a
/// constructor, `prototype` — but the body is a closure, so it can hold an open-file
/// table or a timer queue. See [`crate::object::HostFn`] for the one rule that makes
/// this safe: the closure must not capture a `Gc`.
pub fn new_host_function(
&self,
name: impl Into<String>,
length: u32,
func: crate::object::HostFn,
) -> Gc<JsObject> {
self.new_host_inner(name.into(), length, func, false)
}
/// A host constructor: `new` hands it `this = undefined`, so it validates its
/// arguments before building an instance, exactly like [`Interp::new_native_ctor`].
pub fn new_host_ctor(
&self,
name: impl Into<String>,
length: u32,
func: crate::object::HostFn,
) -> Gc<JsObject> {
let function = self.new_host_inner(name.into(), length, func, true);
function.set_constructible(true);
// A constructor needs a `prototype` for `new` and for `instanceof`; the body
// installs its own methods on it, so it starts as a plain object.
let proto = JsObject::ordinary(self.heap, Some(self.realm.object_proto.clone()));
function.define(
"prototype",
Property {
value: Some(Value::Object(proto)),
writable: true,
enumerable: false,
configurable: false,
..Property::default()
},
);
function
}
fn new_host_inner(
&self,
name: String,
length: u32,
func: crate::object::HostFn,
own_instance: bool,
) -> Gc<JsObject> {
let function = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::Host {
func: std::rc::Rc::new(std::cell::RefCell::new(func)),
own_instance,
},
);
function.define(
"length",
Property::name_or_length(Value::Int(length as i32)),
);
function.define("name", Property::name_or_length(self.string(name)));
function
}
/// A new native function object.
pub fn new_native(&self, name: impl Into<String>, length: u32, func: NativeFn) -> Gc<JsObject> {
let function = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::Native {
func,
own_instance: false,
},
);
// `length` before `name`: `FunctionCreate` sets `[[ParameterCount]]` first, and
// `Object.getOwnPropertyNames` reports insertion order, so the reference lists
// `length,name` and this listed `name,length`. A bytecode function already gets it
// the right way round; only native ones were built backwards.
function.define(
"length",
Property::name_or_length(Value::Int(length as i32)),
);
function.define("name", Property::name_or_length(self.string(name)));
function
}
/// A new bound function.
pub fn new_bound(&self, target: Value, this: Value, args: Vec<Value>) -> Gc<JsObject> {
// §10.4.1.3 BoundFunctionCreate: a bound function has `[[Call]]` because it was
// made by calling `bind`, and `[[Construct]]` exactly when its target has one.
// `new (function(){}.bind(null))()` works; `new (()=>{}).bind(null)()` does not.
let constructible = target
.as_object()
.is_some_and(|target| target.is_constructor());
let bound = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::Bound { target, this, args },
);
bound.set_constructible(constructible);
bound
}
/// A new reference cell.
pub fn new_cell(&self, value: Value) -> Gc<Cell> {
Gc::new(self.heap, Cell::new(value))
}
// -- running ------------------------------------------------------------
/// Evaluate `code` as a script body and return its completion value.
/// Compile and run `source` for the engine's own use, returning its completion value.
///
/// This is how a builtin can be written in JavaScript: [`crate::builtins::from_async`]
/// needs to be a real async function, and the engine already runs those as coroutines,
/// so compiling the algorithm is cheaper than rebuilding a coroutine by hand.
///
/// Differences from [`Self::eval_in_global`], both deliberate:
///
/// * It does **not** drain the microtask queue. Install runs while the realm is still
/// being built, and the source is an IIFE that only constructs a closure, so there
/// is nothing to drain — and draining here would run script-queued jobs in the
/// middle of `install`.
/// * It reports a syntax error as an `InternalError` throw rather than as the usual
/// `SyntaxError` with a position: a malformed builtin source is this crate's bug,
/// and it should not look like the user wrote something wrong.
pub fn eval_builtin_source(&mut self, source: &str) -> JsResult<Value> {
let program = match js_parse::parse_script(source) {
Ok(program) => program,
Err(error) => {
let message = format!("builtin source does not parse: {error}");
return Err(Signal::Throw(self.make_error("InternalError", &message)));
}
};
let mut code = match js_compile::compile_script(&program) {
Ok(code) => code,
Err(error) => {
let message = format!("builtin source does not compile: {}", error.message);
return Err(Signal::Throw(self.make_error("InternalError", &message)));
}
};
// The frame has to name something, and `<builtin>` is what a stack trace through
// this code should say.
code.set_filename("<builtin>");
let compiled = Gc::new(self.heap, CompiledCode::new(code));
let closure_vars = compiled.code.closure_vars.clone();
let env = self.global_env(&closure_vars);
let this = Value::Object(self.realm.global.clone());
self.execute(compiled, env, this, Vec::new(), Value::Undefined)
}
/// Run an already-parsed script and drain the microtasks it queued.
///
/// The drain is what lets an `async` function's body advance past its first `await`
/// before the host sees the result: nothing else pumps the job queue.
pub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value> {
let compiled = Gc::new(self.heap, CompiledCode::new(code));
let closure_vars = compiled.code.closure_vars.clone();
let env = self.global_env(&closure_vars);
let this = Value::Object(self.realm.global.clone());
let callee = Value::Undefined;
let result = self.execute(compiled, env, this, Vec::new(), callee)?;
// The script is done, so the microtasks it queued run before the host sees the
// result. Without this an `async` function's body would never advance past its
// first `await`, because nothing else drains the queue.
self.drain_jobs()?;
Ok(result)
}
/// Compile and run `source` in global scope, returning its completion value.
///
/// This is what `eval` and `new Function` use. Running it as a script means it
/// sees the global object and nothing else, which is right for `eval("1+1")` and
/// for a `Function` body; a *direct* eval that reads a local variable would need
/// the compiler to hand its variable environment over, which is not implemented.
pub fn eval_in_global(&mut self, source: &str) -> JsResult<Value> {
self.eval_in_global_named(source, EVAL_FILE, false)
}
/// `std.evalScript(source)`: the same evaluation, reported under its own file name.
///
/// The reference names the two separately — `eval` shows `<input>`, `std.evalScript`
/// shows `<evalScript>` — and the name is visible in every stack trace that passes
/// through the code, so they cannot share one constant.
pub fn eval_script_in_global(&mut self, source: &str, barrier: bool) -> JsResult<Value> {
self.eval_in_global_named(source, EVAL_SCRIPT_FILE, barrier)
}
fn eval_in_global_named(&mut self, source: &str, file: &str, barrier: bool) -> JsResult<Value> {
// Marked for the duration, so `declare_var_global` knows a `var` from here is an
// eval-created global binding — configurable, unlike a script's own `var`.
self.eval_depth += 1;
if barrier {
let depth = self.frames.len();
self.trace_barriers.push(depth);
}
let result = self.eval_in_global_inner(source, file);
if barrier {
self.trace_barriers.pop();
}
self.eval_depth -= 1;
result
}
fn eval_in_global_inner(&mut self, source: &str, file: &str) -> JsResult<Value> {
let compiled = self.compile_global_script(source, file, false)?;
let strict = compiled.strict;
self.run_eval_script(compiled, strict)
}
/// Parse and compile `source` as a global script, without running it.
///
/// Split out so [`Interp::eval_in_global_async`] can report a syntax error as a throw —
/// before a promise exists to reject — while still evaluating in the same eval context
/// (direct-eval strictness, `new.target`, `super`).
fn compile_global_script(
&mut self,
source: &str,
file: &str,
async_body: bool,
) -> JsResult<CompiledFunction> {
// A *direct* eval inherits the strictness of the code that called it, which is
// why `eval("x = 1")` in a strict function is a ReferenceError instead of an
// implicit global. The caller's frame is the innermost one here, because a
// native function does not push a frame of its own.
let strict = self.current_strict();
// `eval` runs in the caller's function context, so `new.target` is legal here
// even though the source is a bare script — and so is `super.x` when the caller
// is a method. The eval'd code has no `[[HomeObject]]` of its own; it reads the
// caller's, which is what `special_object 4` already does.
let context = js_parse::EvalContext {
in_method: self.enclosing_home_object().is_some(),
// `super()` belongs to a derived constructor, and an eval'd body is not a
// constructor at all: the caller frame's `this` is already built.
in_derived_constructor: false,
};
// A *global* eval is not a function body: `new.target` and `super` are out of
// reach there. Its `{async: true}` form differs in the other direction — the body
// may `await` — so the two flags are independent and the reference sets both from
// the eval type rather than from the caller.
let parsed = if async_body {
js_parse::parse_suspensible_global_eval(source, strict)
} else {
js_parse::parse_eval_script_in(source, strict, context)
};
let program = match parsed {
Ok(program) => program,
Err(error) => {
// The message is bare: the position lives in `fileName`,
// `lineNumber`, `columnNumber` and the stack frame above, which is where
// the reference puts it. An `invalid source: L:C: ` prefix made
// `e.message` differ from the reference while the stack already agreed.
return Err(Signal::Throw(self.syntax_error_at(
file,
error.span.start.line,
error.span.start.column,
&error.message,
)));
}
};
let mut compiled = match if async_body {
js_compile::compile_script_suspensible(&program)
} else {
js_compile::compile_script(&program)
} {
Ok(compiled) => compiled,
// A construct the compiler does not handle yet is reported the same way a
// parse error is: it happens before anything runs, so the trace is the
// position in the source followed by the caller.
Err(error) => {
return Err(Signal::Throw(self.syntax_error_at(
file,
error.span.start.line,
error.span.start.column,
&error.message,
)));
}
};
compiled.set_filename(file);
Ok(compiled)
}
/// Run an `eval`'d script in the caller's scope.
///
/// Three things have to come from the calling frame, because the eval'd code is a
/// script and would otherwise see only the global object:
///
/// * `this`, so `f.call(1, "this")` evaluates to `1`;
/// * `arguments`, exposed as a binding of the eval's own scope, so
/// `f("arguments.length", 1)` sees the caller's arguments;
/// * a fresh variable environment for `var`, so a strict `eval` does not leak
/// `eval("var v = 1")` into the global object.
///
/// Locals of the calling function are *not* reachable: that needs the compiler to
/// hand its variable environment over, which this milestone does not do.
fn run_eval_script(&mut self, code: CompiledFunction, code_strict: bool) -> JsResult<Value> {
// `this` and `arguments` come from the innermost frame only — those are not
// lexical — while the name bindings come from the whole chain of frames, part
// of it being ordinary scope resolution.
// An arrow has no `this` of its own, so an `eval` inside one inherits the
// enclosing function's receiver — the lexical `this` the arrow was compiled to
// read from its closure.
// `this` and `arguments` come from the innermost *non-arrow* frame, but the
// variable environment is the innermost frame of all: an arrow's own body is where
// `var`s declared by an `eval` inside it belong.
let caller_index = self
.frames
.iter()
.rposition(|frame| !frame.code.code.is_arrow)
.or_else(|| self.frames.len().checked_sub(1));
let env_index = self.frames.len().checked_sub(1);
let caller = caller_index.and_then(|index| self.frames.get(index));
let (this, passed) = match caller {
Some(frame) => (frame.this.clone(), frame.passed.clone()),
// No frame at all: the eval is running at the top level, so there is
// nothing to inherit and the global receiver is right.
None => (Value::Object(self.realm.global.clone()), Vec::new()),
};
// Innermost frame first, so the first binding found for a name is the one that
// shadows the rest. A script frame (`<eval>`) contributes nothing.
let mut bindings: Vec<EvalBinding> = Vec::new();
// The calling function's own bindings, resolved by the compiler at the call
// site. That table is the only reliable answer when a name is declared twice in
// sibling scopes: the flat scan below cannot tell which `i` a `for (let i …)`
// body means, and guessing by declaration order picks the wrong one as soon as
// two loops in a row use the same name.
// Only a *direct* eval sees the caller's scope. The compiler records a site for
// every direct call — a bare `eval(...)` callee — so finding one at the current
// position is the test: `(0, eval)(...)` and `f.call(null, ...)` resolve the
// callee to a value first and leave no site behind.
let mut direct = false;
let mut resolved_here = false;
if let Some(frame) = self.frames.last() {
let (line, column) = frame
.code
.code
.position_at(frame.at as u32)
.unwrap_or_default();
// The nearest site at or before the current position, not an exact match:
// the recorded position of the `call` instruction is the callee's, which
// sits a few columns inside the call expression the compiler recorded. A
// site is unique per call, so "the closest one behind us" identifies it.
if let Some(site) = frame
.code
.code
.eval_sites
.iter()
.filter(|site| (site.line, site.column) <= (line, column))
.max_by_key(|site| (site.line, site.column))
{
direct = true;
resolved_here = true;
for binding in &site.bindings {
// An enclosing function's frame sits `owner` levels below the one
// running the call.
let Some(frame) = self.frames.len().checked_sub(1 + binding.owner as usize)
else {
continue;
};
bindings.push(EvalBinding {
name: binding.name.clone(),
frame,
slot: if binding.is_arg {
EvalSlot::Arg
} else {
EvalSlot::Local
},
index: binding.index as usize,
});
}
}
}
for frame_index in (0..self.frames.len()).rev() {
// Skip the innermost frame's flat scan when the compiler already answered
// for it; an enclosing frame still needs one, because the eval resolves
// those names dynamically.
if resolved_here && frame_index == self.frames.len() - 1 {
continue;
}
let frame = &self.frames[frame_index];
for (index, arg) in frame.code.code.args.iter().enumerate() {
bindings.push(EvalBinding {
name: arg.name.clone(),
frame: frame_index,
slot: EvalSlot::Arg,
index,
});
}
for (index, local) in frame.code.code.vars.iter().enumerate() {
bindings.push(EvalBinding {
name: local.name.clone(),
frame: frame_index,
slot: EvalSlot::Local,
index,
});
}
}
let mut seen = BTreeSet::new();
bindings.retain(|binding| seen.insert(binding.name.clone()));
// The caller's `arguments`, as an array-like. Reading `length` and the indexed
// elements is all the spec's ordinary uses need.
//
// Known divergence: this is bound for a *global* eval too, where there is no
// caller and the reference throws `'arguments' is not defined`. Deciding properly
// needs "does the calling frame have an `arguments` of its own", which is not
// recorded anywhere the runtime can see — the caller of a direct eval has one, a
// script or eval body does not, and `eval(b)` passes the callee as a value so even
// the call-site detection does not answer it. Modelled as a binding in the caller's
// variable environment, this would fall out; that is a larger change than the one
// this comment is attached to.
let arguments = Value::Object(self.new_array(passed));
// Only a *strict* eval gets a variable environment of its own. A sloppy one
// declares its `var`s in the caller's — which for a top-level eval is the global
// object, so `eval("var gv=1")` leaves `gv` globally visible. Deciding this on
// strictness alone is the spec's rule; getting it wrong made every sloppy `var`
// vanish into a scope nobody reads back from.
let previous_scope = if code_strict {
self.eval_scope.replace(BTreeMap::new())
} else {
None
};
let previous_arguments = self.eval_arguments.replace(arguments);
let previous_with = self.eval_with.replace(self.with_objects());
let inherited_new_target = self
.frames
.iter()
.rev()
.find(|frame| !frame.code.code.is_arrow)
.map(|frame| frame.new_target.clone());
let previous_new_target = self
.eval_new_target
.replace(inherited_new_target.unwrap_or(Value::Undefined));
let eval_cells = self.eval_cells_for(&bindings);
let previous_bindings = std::mem::replace(&mut self.eval_bindings, bindings);
// A sloppy direct eval declares its `var`s in the caller's variable environment;
// `declare_eval_local` needs to know which frame that is.
// An indirect eval gets the global environment, so nothing of the caller's is
// recorded as the place its `var`s belong.
let previous_caller = self
.eval_caller
.replace(if direct { env_index } else { None });
// The cell-based view of the same bindings, which is what a closure created
// inside the eval keeps. Only cell-backed bindings can be shared, so a frame
// whose compiler did not put a binding in a cell simply does not appear here —
// reads and writes still work through `eval_bindings`, they just cannot outlive
// the eval.
let previous_eval_scope = self
.active_eval_scope
.replace(Gc::new(self.heap, EvalScope { names: eval_cells }));
let compiled = Gc::new(self.heap, CompiledCode::new(code));
let closure_vars = compiled.code.closure_vars.clone();
let env = self.global_env(&closure_vars);
let callee = Value::Undefined;
// An async body does not run here: it becomes a coroutine and answers a promise,
// which is what makes `await` legal in it. `this` still has to be the caller's
// (for a global eval, the global object), because `this` is not part of `await`.
let result = if compiled.code.kind.is_async() {
self.run_async_body(compiled, env, this)
} else {
self.execute(compiled, env, this, Vec::new(), callee)
};
self.eval_scope = previous_scope;
self.eval_arguments = previous_arguments;
self.eval_with = previous_with;
self.eval_new_target = previous_new_target;
self.eval_bindings = previous_bindings;
self.eval_caller = previous_caller;
self.active_eval_scope = previous_eval_scope;
result
}
/// The cell-backed subset of `bindings`, in the same priority order.
///
/// Both lookup routes have to agree — the eval's own reads go through
/// [`Interp::eval_binding_value`] and the closures it builds go through
/// [`ClosureEnv::eval_scope`] — so they are derived from one resolution rather than
/// scanning the frames twice and disagreeing about a shadowed name. A binding that
/// is not in a cell simply does not appear: reads and writes still work, they just
/// cannot be handed to a closure that outlives the eval.
fn eval_cells_for(&self, bindings: &[EvalBinding]) -> Vec<(String, Gc<Cell>)> {
let mut names: Vec<(String, Gc<Cell>)> = Vec::new();
for binding in bindings {
let Some(frame) = self.frames.get(binding.frame) else {
continue;
};
let cell = match binding.slot {
EvalSlot::Arg => frame
.code
.arg_cell
.get(binding.index)
.copied()
.unwrap_or(-1),
EvalSlot::Local => frame
.code
.loc_cell
.get(binding.index)
.copied()
.unwrap_or(-1),
};
if cell < 0 {
continue;
}
if let Some(cell) = frame.cells.get(cell as usize) {
names.push((binding.name.clone(), cell.clone()));
}
}
names
}
/// A `SyntaxError` that reports where in the source it happened.
///
/// Compile-time failures never run, so they have no frame of their own; the
/// reference still puts the position first in the trace, with no function name and
/// no parentheses, and the caller's frames after it. It also defines `fileName`,
/// `lineNumber` and `columnNumber` on the object (a SpiderMonkey convention the
/// reference borrows), which is how a script reads the position without parsing
/// the trace.
/// A `SyntaxError` object for a `js_parse`/`js_compile` failure.
///
/// The message is bare and the position goes on `stack`/`fileName`/`lineNumber`/
/// `columnNumber`, which is how the reference prints one. A positionless error
/// (`SyntaxError::positionless`) gets neither the trace line nor the position fields —
/// the reference raises it from `js_resolve_module`, where there is no position.
pub(crate) fn syntax_error_plain(
&mut self,
error: &js_parse::SyntaxError,
file: &str,
) -> Value {
if error.positionless {
return self.make_error("SyntaxError", &error.message);
}
self.syntax_error_at(
file,
error.span.start.line,
error.span.start.column,
&error.message,
)
}
fn syntax_error_at(&mut self, file: &str, line: u32, column: u32, message: &str) -> Value {
let blamed = format!(" at {file}:{line}:{column}\n{}", self.stack_trace());
let value = self.make_error("SyntaxError", message);
if let Value::Object(object) = &value {
let text = self.string(blamed);
object.define("stack", Property::with_attributes(text, true, true));
let name = self.string(file);
object.define("fileName", Property::with_attributes(name, true, true));
object.define(
"lineNumber",
Property::with_attributes(Value::Int(line as i32), true, true),
);
object.define(
"columnNumber",
Property::with_attributes(Value::Int(column as i32), true, true),
);
}
value
}
/// Run a compiled script whose completion value is a function, returning it.
///
/// `new Function(...)` compiles `(function(params) { body })` and this runs it to
/// get the closure out, which keeps the constant-pool plumbing in one place.
pub fn run_function_source(&mut self, code: CompiledFunction) -> JsResult<Value> {
self.run_script(code)
}
/// The environment of a script or of any function compiled at global scope:
/// every entry is a global binding, so each one resolves to the realm's cell.
fn global_env(&mut self, closure_vars: &[js_compile::bytecode::ClosureVar]) -> Gc<ClosureEnv> {
let mut cells = Vec::with_capacity(closure_vars.len());
for closure_var in closure_vars {
let cell = match closure_var.closure_type {
ClosureType::GlobalDecl | ClosureType::ModuleDecl | ClosureType::ModuleImport
if closure_var.is_lexical =>
{
// A lexical declaration: the cell is created here holding the
// dead-zone marker, which is what makes the binding exist and
// unreadable before its initialiser runs.
self.declare_global(&closure_var.name, closure_var.is_const);
self.realm.globals[&closure_var.name].clone()
}
ClosureType::GlobalDecl | ClosureType::ModuleDecl | ClosureType::ModuleImport => {
// A `var` or function declaration becomes a property of the global
// object, and it exists from here on — which is why
// `print(v); var v = 1` prints `undefined` instead of raising.
// Declaring it at instantiation time (rather than at the
// initialiser) is also what makes it non-configurable: the store
// then lands on an existing property instead of creating one.
self.declare_var_global(&closure_var.name);
// The cell is a placeholder that nothing indexes; it cannot be
// omitted because the slot has to hold something.
self.new_cell(Value::Undefined)
}
_ => {
// An undeclared global is read by name; the cell is a
// placeholder that nothing indexes.
self.new_cell(Value::Undefined)
}
};
cells.push(cell);
}
Gc::new(
self.heap,
ClosureEnv {
cells,
eval_scope: None,
local_vars: None,
local_phase: 1,
},
)
}
/// Push a frame and run until it returns.
pub(crate) fn execute(
&mut self,
code: Gc<CompiledCode>,
env: Gc<ClosureEnv>,
this: Value,
args: Vec<Value>,
callee: Value,
) -> JsResult<Value> {
let depth = self.frames.len();
self.push_frame(code, env, this, args, callee)?;
loop {
match self.step() {
Ok(Step::Continue) => {}
Ok(Step::Return(value)) => {
self.frames.pop();
// A frame that ended in a `tail_call` hands its value straight to
// its caller: nothing after the call can run, so every frame above
// the callee unwinds with the same value. See [`Frame::tail`] —
// the reference does not reuse frames either, so recursion depth
// and the stack-overflow limit behave the same way.
while self.frames.len() > depth && self.frame().tail {
self.frames.pop();
}
if self.frames.len() == depth {
return Ok(value);
}
// The caller resumes with the value on its stack.
self.frames
.last_mut()
.expect("a caller is live")
.stack
.push(value);
}
Err(Signal::Throw(value)) => {
// A handler lives on the stack of the frame that is throwing —
// `catch` pushed it there — so this frame gets the first chance
// to catch before it is popped.
let value = value;
loop {
if self.frames.len() <= depth {
return Err(Signal::Throw(value));
}
if self.catch_here(value.clone()).is_some() {
break;
}
self.frames.pop();
}
}
// A `yield` in a frame that `execute` is running directly means a
// generator body was entered as an ordinary call, which only happens if
// the generator check in `call_or_push` was bypassed.
Ok(Step::Suspend(_)) => {
return Err(Signal::Throw(self.make_error(
"InternalError",
"a generator body ran as an ordinary call",
)));
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
}
}
/// Install a handler for `value` if this frame has one, resuming at it.
///
/// The scan is the reference's: walk the frame's stack downwards looking for the
/// innermost [`Value::CatchOffset`], discarding whatever sat above it. Offset
/// `0` is an enumerator marker rather than a handler — it exists so that a
/// `break` out of a `for-of` can find and close the iterator — so it is skipped
/// here and the unwinding continues.
fn catch_here(&mut self, value: Value) -> Option<()> {
let frame = self.frames.last_mut()?;
while let Some(top) = frame.stack.pop() {
if let Value::CatchOffset(offset) = top {
if offset == 0 {
continue;
}
frame.stack.push(value);
frame.pc = offset as usize;
return Some(());
}
}
None
}
fn push_frame(
&mut self,
code: Gc<CompiledCode>,
env: Gc<ClosureEnv>,
this: Value,
mut args: Vec<Value>,
callee: Value,
) -> JsResult<()> {
// Two bounds, and the byte budget is the one that decides in practice. The
// reference's message is `stack overflow` on an `InternalError`, not V8's
// `Maximum call stack size exceeded` on a *string* — the difference is visible:
// a script may `catch (e) { e.name }` and only the former answers `InternalError`.
let frame_cost = Self::frame_cost(&code);
let stack_base = self.stack_used();
if self.frames.len() >= self.max_frames
|| stack_base.saturating_add(frame_cost) > self.stack_limit
{
return Err(Signal::Throw(
self.make_error("InternalError", "stack overflow"),
));
}
let arg_count = code.code.args.len();
// How many were actually passed, which `arguments.length` reports and
// `assert(x)` depends on: a call with one argument must not look like a call
// with one argument and an explicit `undefined`.
let passed = args.len();
// Missing arguments are `undefined` in their slots; extra ones are dropped
// from the slots but still reachable through `arguments`.
// What the caller actually passed, for `arguments`. Built as one exact-size copy
// before the resize, rather than `args.clone()` followed by a `drain().collect()`:
// that was two heap allocations per call, on the path that `call:recursion` spends
// all its time in — and for a function that never mentions `arguments`.
let mut passed_values = Vec::with_capacity(passed);
passed_values.extend_from_slice(&args[..passed]);
args.resize(arg_count, Value::Undefined);
args.truncate(arg_count);
let cell_count = code.code.var_ref_count as usize;
let mut cells = Vec::with_capacity(cell_count);
for _ in 0..cell_count {
cells.push(self.new_cell(Value::Undefined));
}
let locals = vec![Value::Undefined; code.code.vars.len()];
// A captured argument lives in its cell, not in the argument slot, so the
// incoming value has to be moved there before the body runs.
for (slot, cell) in code.arg_cell.iter().enumerate() {
if *cell >= 0
&& let Some(value) = args.get(slot).cloned()
{
cells[*cell as usize].set(value);
}
}
// Sloppy mode substitutes the global object for a missing receiver and boxes a
// primitive one, so `function f(){ return typeof this }` called as `f.call(1)`
// reports `object`. Strict mode passes the value through untouched.
let this = if code.code.strict {
this
} else if this.is_nullish() {
Value::Object(self.realm.global.clone())
} else if matches!(this, Value::Object(_)) {
this
} else {
match crate::builtins::this_object(self, &this) {
Ok(boxed) => Value::Object(boxed),
Err(_) => this,
}
};
let stack_depth = code.code.stack_size as usize + 1;
self.frames.push(Frame {
code,
env,
this,
args,
passed: passed_values.drain(..passed).collect(),
locals,
cells,
local_vars: None,
// Exactly the depth the compiler computed, so the operand stack never
// reallocates. `16` was an arbitrary guess, and a loop that pushes more than
// that reallocated on its way in — a malloc per call, on the hottest path in
// the interpreter.
stack: Vec::with_capacity(stack_depth),
pc: 0,
at: 0,
callee,
new_target: self.pending_new_target.take().unwrap_or(Value::Undefined),
cost: frame_cost,
stack_base,
tail: false,
});
Ok(())
}
/// A memory checkpoint: does the heap still fit under its limit?
///
/// Called once per instruction (see [`Interp::step`]). The cost when no limit is set is
/// a `Cell` read and a predictable branch, and the point of asking that often is that
/// `var a = []; for (;;) a.push({})` allocates one object per iteration — a checkpoint
/// only at calls would let it run until the machine died, which is the exact failure
/// `--memory-limit` exists to prevent.
///
/// The failure is a thrown `InternalError`, not a `Signal::OutOfMemory`, because the
/// reference's `js_throw_out_of_memory` produces a catchable exception — and because a
/// script that catches it, drops the reference and carries on is legitimate: measured
/// on `qjs --memory-limit 1M`, the engine recovers.
fn check_memory(&mut self) -> JsResult<()> {
// One `Cell` read and a predictable branch when no limit is set, and an integer
// compare when one is. Containers report their own growth as it happens
// (`Heap::adjust_payload`), so this total is up to date without walking the heap —
// walking it here was the first version and it was O(live) per 256 instructions,
// which turned a 100k-element push loop into a five-minute one.
let Some(limit) = self.heap.memory_limit() else {
return Ok(());
};
let used = self.heap.used_bytes();
if used <= limit {
// Back under the limit — a collection or a dropped reference did it. The next
// time it goes over is a new failure, not a repeat of this one.
self.oom_reported_at = 0;
return Ok(());
}
// Only complain about *growth*. The reference fails in `js_malloc`, so a `catch`
// block runs normally after `out of memory` — it allocates nothing, so nothing
// fails. A check that re-fired on every instruction made the handler itself throw,
// which is a dead end: the error became unobservable from script.
//
// Comparing against the total at the last report gives the same behaviour: the
// handler runs while the total is unchanged, and the very next allocation over the
// limit fails.
if self.oom_reported_at != 0 && used <= self.oom_reported_at.saturating_add(OOM_HYSTERESIS)
{
return Ok(());
}
self.oom_reported_at = used;
Err(Signal::Throw(
self.make_error("InternalError", "out of memory"),
))
}
/// Fail before allocating `payload` bytes, if they would not fit.
///
/// For the allocations whose size comes from script data — `new Array(n)`,
/// `'x'.repeat(n)`, `new Uint8Array(n)` — a checkpoint is too late: the request itself
/// is what takes the machine down. The reference has the same shape, with `js_malloc`
/// answering NULL for the one big request.
pub fn reserve(&mut self, payload: usize) -> JsResult<()> {
if self.heap.fits(payload) {
return Ok(());
}
Err(Signal::Throw(
self.make_error("InternalError", "out of memory"),
))
}
/// Bytes of frame state the live call chain accounts for.
pub fn stack_used(&self) -> usize {
match self.frames.last() {
Some(frame) => frame.stack_base + frame.cost,
None => 0,
}
}
/// What one frame of `code` costs: the frame itself plus the slots it allocates.
///
/// The slot formula is the reference's `alloca_size` (`sizeof(JSValue) * (arg_count +
/// var_count + stack_size) + sizeof(JSVarRef*) * var_ref_count`), plus
/// `size_of::<Frame>()`, which is the part the reference pays as a C stack frame
/// instead. That is the whole difference between the two models: it rations C stack, we
/// ration frame state, and both quantities are dominated by the value slots — which is
/// why the two depths come out comparable rather than merely proportional.
fn frame_cost(code: &CompiledCode) -> usize {
let slots = code.code.args.len() + code.code.vars.len() + code.code.stack_size as usize;
std::mem::size_of::<Frame>()
+ std::mem::size_of::<Value>() * slots
+ std::mem::size_of::<usize>() * code.code.var_ref_count as usize
}
/// The cell a direct `eval` created for the closure entry at `slot`, if any.
///
/// Only names the eval created are here: a name the frame already declared statically
/// is written through to its own slot instead, so this cannot shadow a real local.
fn eval_created_local(&self, slot: usize) -> Option<Gc<Cell>> {
let name = &self.frame().code.code.closure_vars.get(slot)?.name;
let (local_vars, phase) = self.visible_local_vars()?;
if !local_vars.contains(name) {
return None;
}
local_vars.get(name, phase)
}
/// The variables a direct `eval` declared, as the code currently running sees them.
///
/// Inside an eval that is the *caller's* set — the eval's own variable environment
/// is the caller's, which is why `eval("var c = 1")` leaves `c` behind. Everywhere
/// else it is the current frame's, falling back to the frame the closure was created
/// in.
fn visible_local_vars(&self) -> Option<(Gc<LocalVars>, u8)> {
// While an `eval` runs, its own frame is on top but its variable environment is
// the *caller's* — so the caller is what the lookup has to start from.
let (index, phase) = match self.eval_caller.flatten() {
Some(caller) => (
caller,
if self.in_parameter_list_at(caller) {
0
} else {
1
},
),
None => (
self.frames.len().checked_sub(1)?,
if self.in_parameter_list() { 0 } else { 1 },
),
};
let frame = self.frames.get(index)?;
// The frame's own variables, or — for an arrow, which has no variable environment
// of its own — the one it was created in, which `ClosureEnv` carries.
if let Some(local_vars) = frame.local_vars.clone() {
return Some((local_vars, phase));
}
frame
.env
.local_vars
.clone()
.map(|local_vars| (local_vars, frame.env.local_phase))
}
/// Whether the innermost frame is still evaluating its parameter list.
///
/// A direct `eval` that runs there declares its `var`s in the parameter environment,
/// and a closure created there may only see that much of [`LocalVars`].
fn in_parameter_list(&self) -> bool {
self.in_parameter_list_at(self.frames.len().saturating_sub(1))
}
/// [`Interp::in_parameter_list`] for a specific frame.
fn in_parameter_list_at(&self, frame: usize) -> bool {
let Some(frame) = self.frames.get(frame) else {
return false;
};
let code = &frame.code.code;
if !code.has_parameter_expressions {
return false;
}
// `(0, 0)` means the compiler did not record a boundary, so treat the whole
// function as its body and let a direct `eval` act accordingly.
if code.body_start == (0, 0) {
return false;
}
match code.position_at(frame.at as u32) {
Some(position) => position < code.body_start,
None => false,
}
}
fn frame(&self) -> &Frame {
self.frames.last().expect("a frame is live")
}
fn frame_mut(&mut self) -> &mut Frame {
self.frames.last_mut().expect("a frame is live")
}
// -- slot access --------------------------------------------------------
fn read_slot(&self, cell: i32, value: &Value) -> Value {
match cell {
-1 => value.clone(),
index => self
.frame()
.cells
.get(index as usize)
.expect("a cell index in range")
.get(),
}
}
fn get_arg(&self, slot: usize) -> Value {
let frame = self.frame();
let cell = frame.code.arg_cell.get(slot).copied().unwrap_or(-1);
let value = frame.args.get(slot).cloned().unwrap_or(Value::Undefined);
self.read_slot(cell, &value)
}
fn put_arg(&mut self, slot: usize, value: Value) {
let cell = self.frame().code.arg_cell.get(slot).copied().unwrap_or(-1);
if cell == -1 {
if let Some(slot) = self.frame_mut().args.get_mut(slot) {
*slot = value;
}
} else {
self.frame().cells[cell as usize].set(value);
}
}
fn get_loc(&self, slot: usize) -> Value {
let frame = self.frame();
let cell = frame.code.loc_cell.get(slot).copied().unwrap_or(-1);
let value = frame.locals.get(slot).cloned().unwrap_or(Value::Undefined);
self.read_slot(cell, &value)
}
fn put_loc(&mut self, slot: usize, value: Value) {
let cell = self.frame().code.loc_cell.get(slot).copied().unwrap_or(-1);
if cell == -1 {
if let Some(slot) = self.frame_mut().locals.get_mut(slot) {
*slot = value;
}
} else {
self.frame().cells[cell as usize].set(value);
}
}
// -- globals ------------------------------------------------------------
/// The name a global-slot operand refers to.
fn global_name(&self, slot: usize) -> String {
self.frame()
.code
.code
.closure_vars
.get(slot)
.map(|c| c.name.clone())
.unwrap_or_default()
}
/// The facts about a global binding that decide where it lives.
///
/// A script's globals split in two, and the split is observable: `var` and
/// function declarations become properties of the global object (`globalThis.v`
/// works, `delete` refuses, `for-in` lists them), while `let`/`const`/`class`
/// declarations live in the global *lexical* environment and are invisible there
/// (`"c" in globalThis` is false). Keeping the difference in the closure var's
/// `is_lexical` flag means the answer is already in the bytecode.
fn global_binding(&self, slot: usize) -> (String, bool, bool) {
let name = self.global_name(slot);
// "Lexical" is *not* read from the closure var's flag: a `GlobalRef` created
// while forwarding a reference through a nested function is built with the
// flags cleared, so `const c` would look like an ordinary `var` from inside a
// closure. Presence in the realm's lexical table is the same fact, recorded
// once, and every slot sees it.
let lexical = self.realm.globals.contains_key(&name);
let is_const = self.realm.lexical_consts.contains(&name);
(name, lexical, is_const)
}
/// The value of `name` in the innermost active `with` object, if any.
///
/// The active objects are the [`VarKind::With`] locals of every frame still running,
/// innermost frame first and innermost block first within one. Walking frames rather
/// than keeping a separate stack is what makes a closure called from inside a `with`
/// see it, and what keeps the object alive only as long as the block is.
/// The innermost active `with` object that has `name`, if any.
fn with_owner(&mut self, name: &str) -> Option<Gc<JsObject>> {
let objects = self.eval_with.clone().unwrap_or_default();
for object in objects {
let key = self.string(name.to_string());
if self
.has_property(&key, &Value::Object(object.clone()))
.ok()?
{
return Some(object);
}
}
None
}
/// Every active `with` object, innermost first.
fn with_objects(&self) -> Vec<Gc<JsObject>> {
let mut objects = Vec::new();
for frame in self.frames.iter().rev() {
for (index, var) in frame.code.code.vars.iter().enumerate().rev() {
if var.kind != js_compile::VarKind::With {
continue;
}
// A captured binding lives in its cell and not in the slot list, so the
// cell is consulted first — a closure created inside the block is exactly
// what makes the object captured. Before the store runs the slot still
// holds `undefined`, and a `with` object is always boxed, so a non-object
// entry is simply not in scope yet.
let value = frame
.code
.loc_cell
.get(index)
.copied()
.filter(|cell| *cell >= 0)
.and_then(|cell| frame.cells.get(cell as usize))
.map(|cell| cell.get())
.or_else(|| frame.locals.get(index).cloned());
if let Some(Value::Object(object)) = value {
objects.push(object);
}
}
}
objects
}
fn with_lookup(&mut self, name: &str) -> JsResult<Option<Value>> {
for object in self.eval_with.clone().unwrap_or_default() {
let key = self.string(name.to_string());
if self.has_property(&key, &Value::Object(object.clone()))? {
return Ok(Some(self.read_field(&Value::Object(object), name)?));
}
}
Ok(None)
}
fn get_global(&mut self, slot: usize) -> JsResult<Value> {
let (name, lexical, _) = self.global_binding(slot);
if name == "arguments"
&& let Some(arguments) = &self.eval_arguments
{
return Ok(arguments.clone());
}
// A `with` block around the *call site* is part of the scope an `eval` sees, so
// the objects live when the eval started are consulted before anything else. The
// snapshot is taken at entry rather than scanning the frames on every read: a
// block that has already finished still leaves its object in the slot, and a
// bytecode read outside the block must not see it.
if let Some(value) = self.with_lookup(&name)? {
return Ok(value);
}
// A variable a direct `eval` declared shadows everything the compiler resolved,
// including an enclosing function's binding: the eval put it in the caller's own
// variable environment, which is nearer than any captured scope.
if let Some((local_vars, phase)) = self.visible_local_vars()
&& let Some(cell) = local_vars.get(&name, phase)
{
return Ok(cell.get());
}
// The frame that called `eval` is the innermost scope the eval'd code sees, so
// its local `a` wins over a global `a`.
if let Some(value) = self.eval_binding_value(&name) {
return Ok(value);
}
// A closure created by an `eval` resolves its free names against the bindings
// that eval saw, even after the eval (and its frames) are gone.
if let Some(cell) = self
.frame()
.env
.eval_scope
.as_ref()
.and_then(|scope| scope.get(&name))
{
let value = cell.get();
if matches!(value, Value::Uninitialized) {
return Err(self.throw_reference(&name));
}
return Ok(value);
}
// A binding declared by the strict `eval` currently running shadows anything
// further out, so `eval("var v = 1")`'s `v` is found before `globalThis.v`.
if let Some(scope) = &self.eval_scope
&& let Some(value) = scope.get(&name)
{
return Ok(value.clone());
}
if lexical {
// A lexical global is a binding in a side table, so the dead zone is
// visible and it is nowhere on the global object.
if let Some(cell) = self.realm.globals.get(&name) {
let value = cell.get();
if matches!(value, Value::Uninitialized) {
return Err(self.throw_reference(&name));
}
return Ok(value);
}
return Err(self.throw_reference(&name));
}
// A `var` global is an ordinary property, so the prototype chain and the
// global object's own builtins are all reachable through it.
match self.get_property(&self.realm.global.clone(), &name)? {
Some(value) => Ok(value),
None => Err(self.throw_reference(&name)),
}
}
/// Store to a global binding.
///
/// Returns a signal when the store is refused: a `const` global may only be
/// written by its initialiser, and a non-configurable `var` global may not be
/// deleted but is always writable.
fn put_global(&mut self, slot: usize, value: Value) -> JsResult<()> {
let (name, lexical, is_const) = self.global_binding(slot);
// An assignment inside a `with` writes through to the object that supplies the
// name, which for an `eval` means consulting the same chain `get_global` does.
if let Some(owner) = self.with_owner(&name) {
self.set_property_with(&owner, &name, value, DeleteFailure::Ignore)?;
return Ok(());
}
// A variable the running eval *declared* takes the write before any caller
// binding: `eval("var c = 1")` creates `c` in the eval's own variable
// environment even when an enclosing function has a `var c` of its own.
if self.eval_caller.flatten().is_some()
&& let Some((local_vars, phase)) = self.visible_local_vars()
&& local_vars.contains(&name)
&& let Some(cell) = local_vars.get(&name, phase)
{
cell.set(value);
return Ok(());
}
// A binding of the frame that called `eval` takes the write, which is what
// makes `f("a = 3")` change the caller's `a`.
if self.set_eval_binding(&name, &value)? {
return Ok(());
}
// A closure that outlived its `eval` writes through to the same cell it reads.
if let Some(cell) = self
.frame()
.env
.eval_scope
.as_ref()
.and_then(|scope| scope.get(&name))
.cloned()
{
cell.set(value);
return Ok(());
}
// Inside a strict `eval`, a name it declared itself is stored there. An
// *assignment* to a name nobody declared is not a declaration, so it falls
// through: strict mode raises `ReferenceError` for it, which is what
// `eval("1 + (@not_def = 1)")` has to do.
if let Some(scope) = self.eval_scope.as_mut()
&& scope.contains_key(&name)
{
scope.insert(name, value);
return Ok(());
}
if lexical {
if let Some(cell) = self.realm.globals.get(&name).cloned() {
if is_const && !matches!(cell.get(), Value::Uninitialized) {
return Err(self.read_only_global(&name));
}
cell.set(value);
return Ok(());
}
// Writing an undeclared lexical global is a `ReferenceError` in strict
// mode and a silent global creation otherwise; creating the cell here
// keeps both paths from losing the value.
let cell = self.new_cell(value);
self.realm.globals.insert(name, cell);
return Ok(());
}
// A variable a direct `eval` declared in this call shadows the global object, even
// when the global already has a property of that name — `var c = "global"` at the
// top level must not capture `eval("var c = 1")` inside a function.
if let Some((local_vars, phase)) = self.visible_local_vars()
&& local_vars.contains(&name)
&& let Some(cell) = local_vars.get(&name, phase)
{
cell.set(value);
return Ok(());
}
let global = self.realm.global.clone();
match global.own_property(&name) {
// A `var` global is writable, so the store goes straight through — but
// through the property, so `globalThis.v = 3` and `v = 3` agree. A failed
// store (a non-writable or accessor-without-setter global) is a silent
// no-op in sloppy mode and a `TypeError` in strict mode, which is the same
// split every other assignment has.
Some(_) => {
let on_failure = if self.current_strict() {
DeleteFailure::Throw
} else {
DeleteFailure::Ignore
};
self.set_property_with(&global, &name, value, on_failure)
}
// Only an implicit global reaches here. In sloppy mode `x = 1` with no
// declaration creates a *configurable* property, which is what makes it
// deletable and is the one way a global differs from a declared `var`.
// In strict mode the same store is a `ReferenceError` instead: that is the
// whole point of strict mode, and creating the global silently meant
// `"use strict"; x = 1` looked like it worked.
None => {
if self.current_strict() {
return Err(self.throw_reference(&name));
}
// A direct eval declares here rather than on the global object: its
// `var`s belong to the caller's variable environment. `declare_eval_local`
// also covers a plain assignment to a variable an earlier eval made.
if let Some(cell) = self.declare_eval_local(&name) {
cell.set(value);
return Ok(());
}
global.define_data(&name, value);
Ok(())
}
}
}
/// The value of a binding of the frame that called `eval`, if there is one.
fn eval_binding_value(&self, name: &str) -> Option<Value> {
let binding = self
.eval_bindings
.iter()
.find(|binding| binding.name == name)?;
let owner = self.frames.get(binding.frame)?;
let (slot, index) = (binding.slot, binding.index);
Some(match slot {
EvalSlot::Arg => match owner.code.arg_cell.get(index).copied().unwrap_or(-1) {
cell if cell >= 0 => owner
.cells
.get(cell as usize)
.map(|cell| cell.get())
.unwrap_or(Value::Undefined),
_ => owner.args.get(index).cloned().unwrap_or(Value::Undefined),
},
EvalSlot::Local => match owner.code.loc_cell.get(index).copied().unwrap_or(-1) {
cell if cell >= 0 => owner
.cells
.get(cell as usize)
.map(|cell| cell.get())
.unwrap_or(Value::Undefined),
_ => owner.locals.get(index).cloned().unwrap_or(Value::Undefined),
},
})
}
/// Write to a binding of the frame that called `eval`.
///
/// `Ok(false)` means no binding matched and the caller should keep looking. A
/// matched binding can still refuse the store, which is why this is fallible: a
/// `const` is read-only everywhere, and a named function expression's own name is
/// read-only in strict mode.
fn set_eval_binding(&mut self, name: &str, value: &Value) -> JsResult<bool> {
let Some(binding) = self
.eval_bindings
.iter()
.find(|binding| binding.name == name)
else {
return Ok(false);
};
let (slot, index, frame) = (binding.slot, binding.index, binding.frame);
let strict = self.current_strict();
let Some(owner) = self.frames.get_mut(frame) else {
return Ok(false);
};
match slot {
EvalSlot::Arg => match owner.code.arg_cell.get(index).copied().unwrap_or(-1) {
cell if cell >= 0 => {
if let Some(cell) = owner.cells.get(cell as usize) {
cell.set(value.clone());
}
}
_ => {
if let Some(slot) = owner.args.get_mut(index) {
*slot = value.clone();
}
}
},
EvalSlot::Local => {
let declared = owner.code.code.vars.get(index);
// A `const` is read-only even through `eval`. The realm records this for
// globals (see `lexical_consts`), but a local's `const`-ness is only in
// the bytecode, so this is where it has to be enforced.
if declared.is_some_and(|var| var.is_const) {
return Err(self.read_only_local(name));
}
// The name of a named function expression is read-only too, but only
// strict mode reports the attempt: a sloppy `eval("m = 1")` where `m` is
// such a name is ignored, the same as a direct store would be.
if declared.is_some_and(|var| var.kind == js_compile::VarKind::FunctionName) {
return if strict {
Err(self.read_only_local(name))
} else {
Ok(true)
};
}
match owner.code.loc_cell.get(index).copied().unwrap_or(-1) {
cell if cell >= 0 => {
if let Some(cell) = owner.cells.get(cell as usize) {
cell.set(value.clone());
}
}
_ => {
if let Some(slot) = owner.locals.get_mut(index) {
*slot = value.clone();
}
}
}
}
}
Ok(true)
}
/// The `TypeError` an assignment to a read-only binding raises.
fn read_only_local(&self, name: &str) -> Signal {
Signal::Throw(self.make_error("TypeError", &format!("'{name}' is read-only")))
}
/// Declare `name` as a variable of the frame that called the running `eval`.
///
/// `None` when there is no direct eval in progress, or it was called from the top
/// level — then the caller's variable environment *is* the global one and the
/// existing path is right.
fn declare_eval_local(&mut self, name: &str) -> Option<Gc<Cell>> {
let caller = self.eval_caller.flatten()?;
// The bottom frame is the top-level script, whose variable environment *is* the
// global one — so `eval("var x = 1")` at the top level still creates a global.
if caller == 0 {
return None;
}
// A name the eval's *own* variable environment already binds is not a new
// variable: `eval("var arguments = 1")` inside a parameter list writes that list's
// `arguments`, and creating a second binding would leave the first — the one
// closures read — untouched. A binding of an *outer* frame does not count: a
// declaration shadows it rather than writing through.
if self
.eval_bindings
.iter()
.any(|binding| binding.name == name && binding.frame == caller)
{
return None;
}
let phase = if self.in_parameter_list_at(caller) {
0
} else {
1
};
self.frame_local_vars(caller)?;
let local_vars = self.frames.get(caller)?.local_vars.clone()?;
if let Some(cell) = local_vars.get(name, 1) {
return Some(cell);
}
let cell = self.new_cell(Value::Undefined);
Some(local_vars.declare(name, phase, cell))
}
/// Make sure `frame` has a `LocalVars`, creating it on demand.
fn frame_local_vars(&mut self, frame: usize) -> Option<Gc<LocalVars>> {
if let Some(local_vars) = &self.frames.get(frame)?.local_vars {
return Some(local_vars.clone());
}
let local_vars = Gc::new(self.heap, LocalVars::new());
self.frames.get_mut(frame)?.local_vars = Some(local_vars.clone());
Some(local_vars)
}
/// Whether the realm already has a binding for `name`, lexically or as an *own*
/// property of the global object. Used to decide whether a store inside an
/// `eval` writes through to the realm or declares a binding of its own.
///
/// Own rather than inherited: `eval("var toString = 1")` must not be taken for a
/// write to `Object.prototype.toString`, and must not shadow it either.
fn global_knows(&mut self, name: &str) -> bool {
if self.realm.globals.contains_key(name) {
return true;
}
self.realm.global.own_property(name).is_some()
}
/// Whether the code currently running is strict.
///
/// Read off the frame's function, which is where the parser's answer ended up; a
/// native function never runs strict-only checks, and there is no frame at all
/// before the first script.
pub fn current_strict(&self) -> bool {
self.frames
.last()
.is_some_and(|frame| frame.code.code.strict)
}
/// Initialise a declared global, which is what ends its dead zone.
///
/// A `var` global becomes a property of the global object: writable, so a later
/// assignment goes through, and non-configurable, so `delete` refuses.
fn init_global(&mut self, slot: usize, value: Value) -> JsResult<()> {
let (name, lexical, _) = self.global_binding(slot);
// A `var` declared by the strict `eval` currently running belongs to the
// eval's scope, not to the global object: `eval("var v = 1")` must not make
// `typeof v` answer `"number"` afterwards. A name the realm already has is
// initialised in place, which is what a re-declaration inside eval does.
if self.eval_scope.is_some() && !lexical && !self.global_knows(&name) {
self.eval_scope
.as_mut()
.expect("checked above")
.insert(name, value);
return Ok(());
}
if let Some(scope) = self.eval_scope.as_mut()
&& scope.contains_key(&name)
{
scope.insert(name, value);
return Ok(());
}
if lexical {
// The cell exists (created at declaration time) holding the dead-zone
// marker; storing the value is what makes the binding readable.
if let Some(cell) = self.realm.globals.get(&name).cloned() {
cell.set(value);
return Ok(());
}
let cell = self.new_cell(value);
self.realm.globals.insert(name, cell);
return Ok(());
}
let global = self.realm.global.clone();
// Writable and enumerable, but *not* configurable: `delete globalThis.v` has
// to answer `false` and leave the binding in place. `with_attributes` ties
// `configurable` to `writable`, so the property is built field by field.
global.define(
&name,
Property {
value: Some(value),
writable: true,
enumerable: true,
configurable: false,
..Property::default()
},
);
Ok(())
}
/// The `TypeError` a store to a `const` global raises.
fn read_only_global(&self, name: &str) -> Signal {
Signal::Throw(self.make_error("TypeError", &format!("'{name}' is read-only")))
}
// -- properties ---------------------------------------------------------
/// Read `object[key]`, following the prototype chain.
///
/// An inherited accessor runs with the *receiver* as `this`, which is why the
/// lookup returns the owner rather than just the value.
pub fn get_property(&mut self, object: &Gc<JsObject>, key: &str) -> JsResult<Option<Value>> {
self.get_property_key(object, &PropertyKey::from(key))
}
/// [`Interp::get_property`] with a key that may be a symbol.
pub fn get_property_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<Option<Value>> {
self.get_property_key_for(object, None, key)
}
/// [`Interp::get_property_key`], with an explicit `this` for an inherited accessor.
///
/// The receiver matters when the property is found on a *prototype*: a getter has to
/// see the object the read started from. For a primitive that is the primitive
/// itself — `Symbol("x").description` runs the getter with the symbol as `this`,
/// not with `Symbol.prototype` — which is why the receiver cannot be inferred from
/// the owner.
pub fn get_property_key_for(
&mut self,
object: &Gc<JsObject>,
receiver: Option<&Value>,
key: &PropertyKey,
) -> JsResult<Option<Value>> {
// A proxy answers for itself, before any of the shortcuts below: `new
// Proxy([], {get: …})` must not read `length` out of an element vector it does not
// have.
if object.proxy_state().is_some() {
let receiver = receiver
.cloned()
.unwrap_or_else(|| Value::Object(object.clone()));
return self.proxy_get(object, &receiver, key);
}
// The element-vector and `length` shortcuts are string-only: an array index
// has no symbol spelling.
if let Some(text) = key.as_str() {
if let Some(value) = self.own_element(object, text) {
return Ok(Some(value));
}
// `length` is not stored as a property: an array's length is its element
// count, and keeping one copy of it is what makes `push`/`pop` and
// `a.length = n` agree.
if let Some(length) = object.array_length()
&& text == "length"
{
// Not `Value::Int(length as i32)`: see [`length_value`].
return Ok(Some(crate::object::length_value(length)));
}
}
match lookup_key(object, key) {
Some((_, property)) => {
if property.is_accessor() {
match &property.getter {
Some(getter) if !matches!(getter, Value::Undefined) => {
let receiver = receiver
.cloned()
.unwrap_or_else(|| Value::Object(object.clone()));
self.call_value(getter.clone(), receiver, Vec::new())
.map(Some)
}
// An accessor with no getter reads as `undefined`.
_ => Ok(Some(Value::Undefined)),
}
} else {
Ok(property.value)
}
}
None => Ok(None),
}
}
/// `super.key` — start the search at `proto`, but report to `receiver`.
///
/// The difference from [`Interp::get_property`] is exactly the receiver: a getter
/// found on the prototype of a class has to see the *instance* as `this`, and a
/// plain property read off `proto` would lose that.
fn super_read(&mut self, receiver: &Value, proto: &Value, key: &Value) -> JsResult<Value> {
let start = self.require_object(proto)?;
let key = self.propkey(key)?;
// `super.x` on a null prototype (a class extending `null`) resolves to nothing.
if matches!(proto, Value::Null) {
return Ok(Value::Undefined);
}
match lookup_key(&start, &key) {
Some((_, property)) if property.is_accessor() => match &property.getter {
Some(getter) if !matches!(getter, Value::Undefined) => {
self.call_value(getter.clone(), receiver.clone(), Vec::new())
}
_ => Ok(Value::Undefined),
},
Some((_, property)) => Ok(property.value.unwrap_or(Value::Undefined)),
None => Ok(Value::Undefined),
}
}
/// `super.key = value` — a store that starts at `proto` but lands on `receiver`.
///
/// A setter found on the prototype runs with `receiver` as `this`; a data property
/// is shadowed by a new own property on the receiver, the same rule an ordinary
/// assignment follows.
fn super_write(
&mut self,
receiver: &Value,
proto: &Value,
key: &Value,
value: Value,
) -> JsResult<()> {
let target = self.require_object(receiver)?;
let key = self.propkey(key)?;
if !matches!(proto, Value::Null)
&& let Some((owner, property)) = lookup_key(&self.require_object(proto)?, &key)
{
if property.is_accessor() {
if let Some(setter) = &property.setter
&& !matches!(setter, Value::Undefined)
{
self.call_value(setter.clone(), receiver.clone(), vec![value])?;
}
return Ok(());
}
// A data property on the prototype is writable or not; reading `owner` at
// all is only for the attribute check.
if !property.writable {
return Err(self.type_error(format!("'{key}' is read-only")));
}
let _ = owner;
}
self.set_property_with_key(&target, &key, value, DeleteFailure::Throw)
}
/// The object a `super` operation needs, or the `TypeError` the spec raises.
fn require_object(&self, value: &Value) -> JsResult<Gc<JsObject>> {
match value.as_object() {
Some(object) => Ok(object.clone()),
None => Err(Signal::Throw(
self.make_error("TypeError", "value is not an object"),
)),
}
}
/// Build the class object `define_class` produces.
///
/// `[parent, ctor]` becomes `[ctor, proto]`: the constructor becomes a constructor
/// whose prototype property is a fresh object inheriting from the parent's
/// prototype, and whose own prototype is the parent constructor — which is what makes
/// a static method inherited too.
fn define_class(&mut self, name: &str, flags: u32) -> JsResult<(Value, Gc<JsObject>)> {
let ctor = self.pop();
let parent = self.pop();
let Value::Object(function) = &ctor else {
return Err(Signal::Throw(
self.make_error("TypeError", "class constructor must be a function"),
));
};
// A heritage of `null` keeps `Object.prototype` as the constructor's prototype
// but gives the instances no prototype at all.
let (parent_ctor, parent_proto) = if flags & 1 != 0 {
match &parent {
Value::Null => (Some(self.realm.function_proto.clone()), None),
Value::Object(parent_class) => {
if !parent_class.is_callable() {
return Err(Signal::Throw(
self.make_error("TypeError", "parent class must be constructor"),
));
}
let proto = self.get_property(parent_class, "prototype")?;
match proto {
Some(Value::Object(proto)) => (Some(parent_class.clone()), Some(proto)),
Some(Value::Null) => (Some(parent_class.clone()), None),
_ => {
return Err(Signal::Throw(self.make_error(
"TypeError",
"parent prototype must be an object or null",
)));
}
}
}
_ => {
return Err(Signal::Throw(
self.make_error("TypeError", "parent class must be constructor"),
));
}
}
} else {
(
Some(self.realm.function_proto.clone()),
Some(self.realm.object_proto.clone()),
)
};
let proto = self.new_object(parent_proto);
function.set_proto(parent_ctor);
// A class is not callable without `new`, and its `prototype` is not writable —
// both follow from being a class and are what `class A {}; A()` throws on.
function.set_class_constructor(true);
function.set_home_object(None);
self.set_function_name(&ctor, name);
self.define_class_properties(function, &ctor, &proto)?;
Ok((ctor, proto))
}
/// Give the constructor its `prototype` and `length`, and the prototype its
/// `constructor` back-pointer.
fn define_class_properties(
&mut self,
function: &Gc<JsObject>,
ctor: &Value,
proto: &Gc<JsObject>,
) -> JsResult<()> {
proto.define(
"constructor",
Property::with_attributes(ctor.clone(), true, false),
);
function.define(
"prototype",
Property::prototype_of(Value::Object(proto.clone()), false),
);
if let Some((code, _)) = bytecode_of(function) {
let length = code.code.defined_args;
function.define(
"length",
Property::name_or_length(Value::Int(length as i32)),
);
}
Ok(())
}
/// The own array element `key` names, if this object has one.
fn own_element(&self, object: &Gc<JsObject>, key: &str) -> Option<Value> {
let index = array_index(key)?;
// A typed array's elements live in its buffer, not in a property list: reading one
// goes through the view's element type so a bounds check and a detached buffer are
// both handled there.
if object.typed_array_parts().is_some() {
return object.typed_array_element(index as usize);
}
// A hole answers `None`, not `undefined`: it is *not* an own property, so the read
// has to continue up the prototype chain. Mapping it to `undefined` here ended the
// search early, which is why `Array.prototype[1] = 9` could not be seen through
// `[1,,3][1]`.
match object.array_get(index as usize) {
Some(Value::Hole) | None => None,
other => other,
}
}
/// Store `object[key] = value`, honouring setters and inherited properties.
pub fn set_property(&mut self, object: &Gc<JsObject>, key: &str, value: Value) -> JsResult<()> {
self.set_property_with(object, key, value, DeleteFailure::Throw)
}
/// Store a property, choosing what to do when the object refuses.
///
/// A rejected store is a `TypeError` in strict mode and a silent no-op in sloppy
/// mode, which is the one place assignment differs by mode. Native code (the array
/// methods, `JSON`) always asks for the throw.
pub fn set_property_with(
&mut self,
object: &Gc<JsObject>,
key: &str,
value: Value,
on_failure: DeleteFailure,
) -> JsResult<()> {
self.set_property_with_key(object, &PropertyKey::from(key), value, on_failure)
}
/// [`Interp::set_property_with`] with a key that may be a symbol.
pub fn set_property_with_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
value: Value,
on_failure: DeleteFailure,
) -> JsResult<()> {
// A proxy's `set` trap decides, and its `false` means the same thing an
// ordinary refusal means: a `TypeError` in strict code, nothing in sloppy code.
if object.proxy_state().is_some() {
let receiver = Value::Object(object.clone());
if let Some(accepted) = self.proxy_set(object, key, value.clone(), &receiver)? {
return match (accepted, on_failure) {
(true, _) | (false, DeleteFailure::Ignore) => Ok(()),
(false, DeleteFailure::Throw) => {
Err(self.type_error(format!("proxy 'set' refused to write '{key}'")))
}
};
}
}
// The element and `length` shortcuts only exist for string keys: an array
// index has no symbol spelling, and a symbol named `length` is an ordinary
// property.
let text = key.as_str();
// A typed array's elements are a window onto its buffer: the store has to go
// through the view's element type so the value is wrapped (or clamped) and so an
// out-of-range or detached write is a silent no-op, as the spec says.
if let Some(text) = text
&& let Some(index) = array_index(text)
&& let Some(element) = object.typed_array_element_type()
{
// §10.4.5.5: an out-of-bounds index answers `true` *before* `V` is converted,
// so a `valueOf` with a side effect must not run. Checking the length first is
// what keeps `valueOfCalled` at 0 in `key-is-out-of-bounds-receiver-is-…`.
let length = object.typed_array_length().unwrap_or(0);
if index as usize >= length {
return Ok(());
}
// A BigInt view refuses a Number and a numeric one refuses a Symbol; the spec
// makes both a `TypeError` rather than a silent skip. The conversion runs here,
// in `Interp`, because it is script-observable and the storage borrow inside
// `typed_array_set` must not be held across it.
let stored = self.element_value(element, value)?;
if let Some(Err(reason)) = object.typed_array_set(index as usize, stored) {
return Err(self.type_error(reason));
}
return Ok(());
}
if let Some(text) = text
&& let Some(index) = array_index(text)
&& object.is_array()
{
// An accessor defined on an index lives in the property table while the
// element slot is a hole, so the store has to go through the property first.
// Writing the vector directly skipped the setter entirely.
if let Some(existing) = object.own_property_key(key) {
if existing.is_accessor() {
return match existing.setter.clone() {
Some(setter) if !matches!(setter, Value::Undefined) => {
let receiver = Value::Object(object.clone());
self.call_value(setter, receiver, vec![value]).map(|_| ())
}
_ => match on_failure {
DeleteFailure::Throw => Err(self.type_error(format!(
"cannot set property '{text}', it has only a getter"
))),
DeleteFailure::Ignore => Ok(()),
},
};
}
if !existing.writable {
return match on_failure {
DeleteFailure::Throw => {
Err(self.type_error(format!("'{text}' is read-only")))
}
DeleteFailure::Ignore => Ok(()),
};
}
}
{
// Growing the array needs a writable `length`.
if (index as usize) >= object.array_length().unwrap_or(0)
&& !object.length_writable()
{
return match on_failure {
DeleteFailure::Throw => Err(self.type_error("'length' is read-only")),
DeleteFailure::Ignore => Ok(()),
};
}
// Adding an index past the end makes the array longer, which a
// non-extensible array refuses.
if index as usize >= object.array_length().unwrap_or(0) && !object.is_extensible() {
return match on_failure {
DeleteFailure::Throw => Err(self.type_error(format!(
"cannot add property '{key}', object is not extensible"
))),
DeleteFailure::Ignore => Ok(()),
};
}
if object.element_is_locked(index) {
return match on_failure {
DeleteFailure::Throw => {
Err(self.type_error(format!("'{key}' is read-only")))
}
DeleteFailure::Ignore => Ok(()),
};
}
if object.array_set(self.heap, index as usize, value.clone()) {
return Ok(());
}
// Past the dense limit the slot is too far out to be worth one `Value`
// per hole — `a[2**32 - 2] = 1` is legal and the reference answers it in
// constant time by keeping the index as an ordinary property. The store
// used to be *dropped* here, leaving `length` at 0: a silent wrong answer
// to a legal program.
object.define_key(key, Property::with_attributes(value, true, true));
// Extend, never shorten: the index may be *inside* the current length (a
// huge `length` with a sparse store), and shortening would delete the
// sparse properties above it — including the one just stored.
if index as usize + 1 > object.array_length().unwrap_or(0) {
object.array_set_length(self.heap, index as usize + 1);
}
return Ok(());
}
}
if text == Some("length") && object.is_array() {
if !object.length_writable() {
return match on_failure {
DeleteFailure::Throw => Err(self.type_error("'length' is read-only")),
DeleteFailure::Ignore => Ok(()),
};
}
let length = value.to_number();
if length.is_finite() && length >= 0.0 {
let length = length as usize;
// `a.length = n` with `n` below a non-configurable element cannot
// drop that element, so the whole assignment fails. The spec makes
// this a `TypeError` rather than a silent no-op, in any mode.
if let Some(blocked) = object.first_undeletable_at_or_above(length) {
// Sloppy mode stops the truncation at the blocking element
// instead of failing; strict mode raises.
// Both modes stop at the blocking element; they differ in
// whether that is reported. The reference truncates *then*
// throws, so the array is left in the shortened state.
object.array_truncate(self.heap, blocked + 1);
return match on_failure {
DeleteFailure::Throw => {
Err(self.type_error(format!("cannot delete property '{blocked}'")))
}
DeleteFailure::Ignore => Ok(()),
};
}
object.array_set_length(self.heap, length);
return Ok(());
}
}
if !object.is_extensible() && !object.has_own_key(key) && lookup_key(object, key).is_none()
{
return match on_failure {
DeleteFailure::Throw => Err(self.type_error(format!(
"cannot add property {}, object is not extensible",
key
))),
DeleteFailure::Ignore => Ok(()),
};
}
if let Some((owner, property)) = lookup_key(object, key) {
if property.is_accessor() {
if let Some(setter) = &property.setter
&& !matches!(setter, Value::Undefined)
{
let receiver = Value::Object(object.clone());
self.call_value(setter.clone(), receiver, vec![value])?;
}
return Ok(());
}
// An inherited *data* property is shadowed by an own property on the
// receiver; a non-writable one is not.
if owner.ptr_eq(object) && !property.writable {
return match on_failure {
DeleteFailure::Throw => Err(self.type_error(format!("'{key}' is read-only"))),
DeleteFailure::Ignore => Ok(()),
};
}
// An existing writable own property keeps its attributes: only the value
// moves. Redefining instead would reset them, which is how a
// non-configurable global `var` used to become deletable.
if owner.ptr_eq(object) && object.write_value_key(key, value.clone()) {
return Ok(());
}
}
// No own data property: create one, shadowing whatever the prototype holds.
object.define_data_key(key, value);
Ok(())
}
/// Delete `object[key]`.
pub fn delete_property(&mut self, object: &Gc<JsObject>, key: &str) -> JsResult<bool> {
self.delete_property_key(object, &PropertyKey::from(key))
}
/// [`Interp::delete_property`] with a key that may be a symbol.
pub fn delete_property_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<bool> {
// A proxy decides for itself, and may even throw; the invariant checks live in
// `proxy_delete`.
if let Some(answer) = self.proxy_delete(object, key)? {
return Ok(answer);
}
if let Some(index) = key.as_str().and_then(array_index)
&& let Some(length) = object.array_length()
&& (index as usize) < length
{
// An accessor defined on an index lives in the property table rather than the
// element vector, so both have to be cleared: `delete` after
// `Object.defineProperty(a, "1", {get})` used to leave the getter answering.
object.delete_key(key);
// A hole rather than a shift: `delete a[0]` leaves `length` alone and the
// index stops answering `in`.
if object.element_is_undeletable(index) {
return Ok(false);
}
object.array_set(self.heap, index as usize, Value::Hole);
return Ok(true);
}
Ok(object.delete_key(key))
}
// -- proxy traps --------------------------------------------------------
//
// A proxy's internal methods all look the same: ask the handler for the trap, call it
// with the target and the operation's own arguments, then check the answer against
// the target. The checks are not decoration — they are what stops a proxy from
// reporting a `non-configurable` property as missing, which would let a frozen object
// be rewritten by standing in front of it.
/// The target and handler of `object`, or `None` when it is not a proxy.
///
/// A *revoked* proxy is a `TypeError` rather than `None`, because every operation on
/// one has to fail: the slots the traps would have been read from are gone.
fn proxy_parts(&mut self, object: &Gc<JsObject>) -> JsResult<Option<(Gc<JsObject>, Value)>> {
let Some(state) = object.proxy_state() else {
return Ok(None);
};
let state = state.borrow();
match (&state.target, &state.handler) {
(Some(target), Some(handler)) => Ok(Some((target.clone(), handler.clone()))),
_ => Err(self.type_error("cannot use a proxy that has been revoked")),
}
}
/// Call `handler[name]`, or answer `None` when the trap is absent.
///
/// A trap that is present but not callable is a `TypeError` rather than "no trap":
/// the spec checks `IsCallable` and refuses, so `{ get: 1 }` must not quietly fall
/// back to the target.
fn proxy_trap(
&mut self,
handler: &Value,
name: &str,
args: Vec<Value>,
) -> JsResult<Option<Value>> {
let Value::Object(table) = handler else {
return Err(self.type_error("proxy handler is not an object"));
};
let trap = self.get_property(table, name)?.unwrap_or(Value::Undefined);
match trap {
Value::Undefined => Ok(None),
Value::Object(function) if function.is_callable() => {
let answer = self.call_value(Value::Object(function), handler.clone(), args)?;
Ok(Some(answer))
}
_ => Err(self.type_error(format!("proxy trap '{name}' is not a function"))),
}
}
/// Whether `object`'s own property `key` is one the target refuses to lose.
///
/// A non-configurable property is fixed: no trap may hide it, delete it, or change
/// its value behind the target's back. This goes through
/// [`Self::ordinary_own_property`] rather than the property table so that an array's
/// elements and its `length` — both fixed, both outside the table — are covered.
fn is_fixed_property(target: &Gc<JsObject>, key: &PropertyKey) -> Option<Property> {
Self::ordinary_own_property(target, key).filter(|property| !property.configurable)
}
/// `[[Get]]` on a proxy. `Some` means the proxy answered, `None` means "not a proxy".
fn proxy_get(
&mut self,
object: &Gc<JsObject>,
receiver: &Value,
key: &PropertyKey,
) -> JsResult<Option<Value>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let Some(result) = self.proxy_trap(
&handler,
"get",
vec![Value::Object(target.clone()), key_value, receiver.clone()],
)?
else {
// No trap: forward to the target's own `[[Get]]`, keeping the receiver so an
// inherited getter still sees the proxy.
return Ok(Some(
self.get_property_key_for(&target, Some(receiver), key)?
.unwrap_or(Value::Undefined),
));
};
if let Some(fixed) = Self::is_fixed_property(&target, key) {
if fixed.is_accessor() {
// A fixed accessor with no getter must read as `undefined`.
let has_getter = fixed
.getter
.as_ref()
.is_some_and(|getter| !matches!(getter, Value::Undefined));
if !has_getter && !matches!(result, Value::Undefined) {
return Err(self.type_error(format!(
"proxy 'get' must report undefined for the non-configurable property '{key}'"
)));
}
} else if !fixed.writable {
let fixed_value = fixed.value.unwrap_or(Value::Undefined);
if !fixed_value.same_value(&result) {
return Err(self.type_error(format!(
"proxy 'get' disagrees with the non-configurable property '{key}'"
)));
}
}
}
Ok(Some(result))
}
/// `[[Set]]` on a proxy.
///
/// The boolean is the trap's answer: the caller turns `false` into a `TypeError` in
/// strict mode and a silent no-op in sloppy mode, which is the same split an ordinary
/// read-only store has.
fn proxy_set(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
value: Value,
receiver: &Value,
) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let Some(result) = self.proxy_trap(
&handler,
"set",
vec![
Value::Object(target.clone()),
key_value,
value.clone(),
receiver.clone(),
],
)?
else {
// No trap: store on the target, which is where an ordinary proxy write lands.
self.set_property_with_key(&target, key, value, DeleteFailure::Ignore)?;
return Ok(Some(true));
};
if !result.to_boolean() {
return Ok(Some(false));
}
if let Some(fixed) = Self::is_fixed_property(&target, key) {
if fixed.is_accessor() {
let has_setter = fixed
.setter
.as_ref()
.is_some_and(|setter| !matches!(setter, Value::Undefined));
if !has_setter {
return Err(self.type_error(format!(
"proxy 'set' succeeded on the non-configurable accessor '{key}', which has no setter"
)));
}
} else if !fixed.writable {
let fixed_value = fixed.value.unwrap_or(Value::Undefined);
if !fixed_value.same_value(&value) {
return Err(self.type_error(format!(
"proxy 'set' changed the non-configurable property '{key}'"
)));
}
}
}
// A new property cannot appear on a target that refuses new properties.
if Self::ordinary_own_property(&target, key).is_none() && !target.is_extensible() {
return Err(self.type_error(format!(
"proxy 'set' added '{key}' to a non-extensible target"
)));
}
Ok(Some(true))
}
/// `[[HasProperty]]` on a proxy, which is the `in` operator.
fn proxy_has(&mut self, object: &Gc<JsObject>, key: &PropertyKey) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let Some(result) = self.proxy_trap(
&handler,
"has",
vec![Value::Object(target.clone()), key_value],
)?
else {
return Ok(Some(self.has_own_or_inherited(&target, key)?));
};
if !result.to_boolean() && Self::is_fixed_property(&target, key).is_some() {
return Err(self.type_error(format!(
"proxy 'has' denied the non-configurable property '{key}'"
)));
}
// A non-extensible target cannot grow, so every own property it *has* is fixed.
if !result.to_boolean()
&& !target.is_extensible()
&& Self::ordinary_own_property(&target, key).is_some()
{
return Err(self.type_error(format!(
"proxy 'has' denied the own property '{key}' of a non-extensible target"
)));
}
Ok(Some(result.to_boolean()))
}
/// `[[Delete]]` on a proxy.
fn proxy_delete(&mut self, object: &Gc<JsObject>, key: &PropertyKey) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let Some(result) = self.proxy_trap(
&handler,
"deleteProperty",
vec![Value::Object(target.clone()), key_value],
)?
else {
return Ok(Some(self.delete_property_key(&target, key)?));
};
if result.to_boolean() && Self::is_fixed_property(&target, key).is_some() {
return Err(self.type_error(format!(
"proxy 'deleteProperty' removed the non-configurable property '{key}'"
)));
}
if result.to_boolean()
&& !target.is_extensible()
&& Self::ordinary_own_property(&target, key).is_some()
{
return Err(self.type_error(format!(
"proxy 'deleteProperty' removed '{key}' from a non-extensible target"
)));
}
Ok(Some(result.to_boolean()))
}
/// The own keys a proxy reports, after the `ownKeys` trap and its invariants.
fn proxy_own_keys(&mut self, object: &Gc<JsObject>) -> JsResult<Option<Vec<PropertyKey>>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let Some(result) =
self.proxy_trap(&handler, "ownKeys", vec![Value::Object(target.clone())])?
else {
// No trap: the target's own keys, already in the order an object reports.
return Ok(Some(self.object_own_property_keys(&target)?));
};
// `CreateListFromArrayLike` with the string-or-symbol restriction.
let list = self.list_from_array_like(&result, true)?;
let mut keys: Vec<PropertyKey> = Vec::with_capacity(list.len());
for value in list {
let key = self.propkey(&value)?;
if keys.contains(&key) {
return Err(self.type_error(format!(
"proxy 'ownKeys' returned the duplicate key '{key}'"
)));
}
keys.push(key);
}
for (key, property) in target.property_entries() {
if !property.configurable && !keys.contains(&key) {
return Err(self.type_error(format!(
"proxy 'ownKeys' dropped the non-configurable key '{key}'"
)));
}
if !target.is_extensible() && !keys.contains(&key) {
return Err(self.type_error(format!(
"proxy 'ownKeys' dropped '{key}', which a non-extensible target owns"
)));
}
}
if !target.is_extensible() {
let own = target.property_entries().len();
if keys.len() != own {
return Err(
self.type_error("proxy 'ownKeys' does not match its non-extensible target")
);
}
}
Ok(Some(keys))
}
/// `[[GetOwnProperty]]` on a proxy.
fn proxy_get_own_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<Option<Option<Property>>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let Some(result) = self.proxy_trap(
&handler,
"getOwnPropertyDescriptor",
vec![Value::Object(target.clone()), key_value],
)?
else {
// No trap: the target's own descriptor is the answer. This has to go through
// the same synthesis an ordinary object gets, because a proxy's own property
// table is empty by design — reading it directly reported `undefined` for
// every key, including an array's indices and `length`.
return Ok(Some(Self::ordinary_own_property(&target, key)));
};
let answer = if matches!(result, Value::Undefined) {
None
} else {
Some(self.property_from_descriptor(&result)?)
};
let fixed = Self::is_fixed_property(&target, key);
match (&answer, &fixed) {
// A non-configurable property cannot be reported as absent.
(None, Some(_)) => {
return Err(self.type_error(format!(
"proxy 'getOwnPropertyDescriptor' hid the non-configurable property '{key}'"
)));
}
(Some(descriptor), Some(fixed)) => {
if descriptor.configurable {
return Err(self.type_error(format!(
"proxy 'getOwnPropertyDescriptor' made '{key}' configurable"
)));
}
if fixed.is_accessor() != descriptor.is_accessor() {
return Err(self.type_error(format!(
"proxy 'getOwnPropertyDescriptor' changed the kind of '{key}'"
)));
}
if !fixed.is_accessor() && !fixed.writable {
let fixed_value = fixed.value.clone().unwrap_or(Value::Undefined);
let reported = descriptor.value.clone().unwrap_or(Value::Undefined);
if !fixed_value.same_value(&reported) {
return Err(self.type_error(format!(
"proxy 'getOwnPropertyDescriptor' changed the value of '{key}'"
)));
}
}
}
_ => {}
}
Ok(Some(answer))
}
/// `[[DefineOwnProperty]]` on a proxy.
fn proxy_define_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
property: Property,
) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let key_value = self.key_value(key);
let descriptor = self.descriptor_from_property(&property);
let Some(result) = self.proxy_trap(
&handler,
"defineProperty",
vec![Value::Object(target.clone()), key_value, descriptor],
)?
else {
return Ok(None);
};
if !result.to_boolean() {
return Ok(Some(false));
}
let fixed = Self::is_fixed_property(&target, key);
let creating = Self::ordinary_own_property(&target, key).is_none();
if creating && !target.is_extensible() {
return Err(self.type_error(format!(
"proxy 'defineProperty' added '{key}' to a non-extensible target"
)));
}
if let Some(fixed) = fixed {
if property.configurable {
return Err(self.type_error(format!(
"proxy 'defineProperty' made the non-configurable '{key}' configurable"
)));
}
if !fixed.writable && property.writable && !fixed.is_accessor() {
return Err(self.type_error(format!(
"proxy 'defineProperty' made the read-only '{key}' writable"
)));
}
}
Ok(Some(true))
}
/// `[[GetPrototypeOf]]` on a proxy.
fn proxy_get_prototype_of(
&mut self,
object: &Gc<JsObject>,
) -> JsResult<Option<Option<Gc<JsObject>>>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let Some(result) = self.proxy_trap(
&handler,
"getPrototypeOf",
vec![Value::Object(target.clone())],
)?
else {
return Ok(Some(target.proto()));
};
let proto = match &result {
Value::Null => None,
Value::Object(proto) => Some(proto.clone()),
_ => {
return Err(self.type_error("proxy 'getPrototypeOf' must return an object or null"));
}
};
if !target.is_extensible() && !same_proto(proto.as_ref(), target.proto().as_ref()) {
return Err(self.type_error(
"proxy 'getPrototypeOf' changed the prototype of a non-extensible target",
));
}
Ok(Some(proto))
}
/// `[[SetPrototypeOf]]` on a proxy.
fn proxy_set_prototype_of(
&mut self,
object: &Gc<JsObject>,
proto: Option<Gc<JsObject>>,
) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let proto_value = match &proto {
Some(proto) => Value::Object(proto.clone()),
None => Value::Null,
};
let Some(result) = self.proxy_trap(
&handler,
"setPrototypeOf",
vec![Value::Object(target.clone()), proto_value],
)?
else {
target.set_proto(proto);
return Ok(Some(true));
};
if result.to_boolean()
&& !target.is_extensible()
&& !same_proto(proto.as_ref(), target.proto().as_ref())
{
return Err(self.type_error(
"proxy 'setPrototypeOf' changed the prototype of a non-extensible target",
));
}
Ok(Some(result.to_boolean()))
}
/// `[[IsExtensible]]` on a proxy.
fn proxy_is_extensible(&mut self, object: &Gc<JsObject>) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let Some(result) = self.proxy_trap(
&handler,
"isExtensible",
vec![Value::Object(target.clone())],
)?
else {
return Ok(Some(target.is_extensible()));
};
// The answer is not the proxy's to invent: it must agree with the target, or a
// script could learn `false` while the target keeps accepting new properties.
if result.to_boolean() != target.is_extensible() {
return Err(self.type_error("proxy 'isExtensible' disagrees with its target"));
}
Ok(Some(result.to_boolean()))
}
/// `[[PreventExtensions]]` on a proxy.
fn proxy_prevent_extensions(&mut self, object: &Gc<JsObject>) -> JsResult<Option<bool>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
let Some(result) = self.proxy_trap(
&handler,
"preventExtensions",
vec![Value::Object(target.clone())],
)?
else {
target.set_extensible(false);
return Ok(Some(true));
};
if result.to_boolean() && target.is_extensible() {
return Err(self.type_error(
"proxy 'preventExtensions' reported success but the target is still extensible",
));
}
Ok(Some(result.to_boolean()))
}
/// `[[Call]]` on a proxy, through the `apply` trap.
fn proxy_apply(
&mut self,
object: &Gc<JsObject>,
this: Value,
args: Vec<Value>,
) -> JsResult<Option<Value>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
// The trap gets the arguments as one array, which is the shape the spec passes
// and the shape `apply` traps are written against (`args[0]` is the first one).
let arguments = self.new_array(args.clone());
let this_for_trap = this.clone();
let Some(result) = self.proxy_trap(
&handler,
"apply",
vec![
Value::Object(target.clone()),
this_for_trap,
Value::Object(arguments),
],
)?
else {
return Ok(Some(self.call_value(Value::Object(target), this, args)?));
};
Ok(Some(result))
}
/// `[[Construct]]` on a proxy, through the `construct` trap.
fn proxy_construct(
&mut self,
object: &Gc<JsObject>,
args: Vec<Value>,
new_target: &Value,
) -> JsResult<Option<Value>> {
let Some((target, handler)) = self.proxy_parts(object)? else {
return Ok(None);
};
if !target.is_callable() {
return Err(self.type_error("proxy target is not a constructor"));
}
let arguments = self.new_array(args.clone());
let Some(result) = self.proxy_trap(
&handler,
"construct",
vec![
Value::Object(target.clone()),
Value::Object(arguments),
new_target.clone(),
],
)?
else {
return Ok(Some(self.construct(
Value::Object(target),
new_target.clone(),
args,
)?));
};
if !matches!(result, Value::Object(_)) {
return Err(self.type_error("proxy 'construct' must return an object"));
}
Ok(Some(result))
}
/// `[[HasProperty]]` on an ordinary object, which the proxy fallback needs.
pub(crate) fn has_own_or_inherited(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<bool> {
// The object itself first: it may be a proxy, and then the answer is its trap's.
if let Some(answer) = self.proxy_has(object, key)? {
return Ok(answer);
}
// `has_own` rather than `own_property_key`: an array's elements and its derived
// `length` are invisible to the property table, and `Array.prototype` is an array.
if has_own(object, key) {
return Ok(true);
}
// A hole is *not* the end of the search: the chain may still have the key, which
// is why `Array.prototype[4] = 5` makes `4 in [1,2,3,4,]` true.
for ancestor in object.chain() {
// A proxy further up the chain answers for itself.
if let Some(answer) = self.proxy_has(&ancestor, key)? {
return Ok(answer);
}
if has_own(&ancestor, key) {
return Ok(true);
}
}
Ok(false)
}
/// `CreateListFromArrayLike`, optionally restricted to property keys.
///
/// The `ownKeys` trap is the one place a script's array reaches the engine this way,
/// and refusing a non-key element there is what makes `ownKeys: () => [1.5]` a
/// `TypeError` instead of a property named `"1.5"`.
fn list_from_array_like(&mut self, value: &Value, keys_only: bool) -> JsResult<Vec<Value>> {
let object = self.object_from(value)?;
let length = self
.get_property(&object, "length")?
.map(|value| value.to_number())
.unwrap_or(0.0);
if !length.is_finite() || length < 0.0 {
return Err(self.type_error("invalid array-like length"));
}
let length = length as usize;
let mut items = Vec::with_capacity(length.min(1024));
for index in 0..length {
let item = self
.get_property(&object, &index.to_string())?
.unwrap_or(Value::Undefined);
if keys_only && matches!(&item, Value::Symbol(_)) {
items.push(item);
continue;
}
if keys_only && !matches!(&item, Value::String(_)) {
return Err(self.type_error("proxy 'ownKeys' must return strings and symbols"));
}
items.push(item);
}
Ok(items)
}
/// A `Property` from the descriptor object a trap returned.
fn property_from_descriptor(&mut self, value: &Value) -> JsResult<Property> {
let object = self.object_from(value)?;
let field = |interp: &mut Self, name: &str| -> JsResult<Option<Value>> {
interp.get_property(&object, name)
};
let getter = field(self, "get")?;
let setter = field(self, "set")?;
let accessor = getter.is_some() || setter.is_some();
let boolean = |interp: &mut Self, name: &str| -> JsResult<bool> {
Ok(interp
.get_property(&object, name)?
.is_some_and(|value| value.to_boolean()))
};
let enumerable = boolean(self, "enumerable")?;
let configurable = boolean(self, "configurable")?;
if accessor {
if field(self, "value")?.is_some() || field(self, "writable")?.is_some() {
return Err(
self.type_error("a descriptor cannot be both a data property and an accessor")
);
}
Ok(Property {
getter: getter.or(Some(Value::Undefined)),
setter: setter.or(Some(Value::Undefined)),
enumerable,
configurable,
writable: false,
value: None,
})
} else {
Ok(Property {
value: Some(field(self, "value")?.unwrap_or(Value::Undefined)),
writable: boolean(self, "writable")?,
enumerable,
configurable,
getter: None,
setter: None,
})
}
}
/// The descriptor object for a `Property`, which the `defineProperty` trap receives.
fn descriptor_from_property(&mut self, property: &Property) -> Value {
let object = self.new_object(None);
if property.is_accessor() {
let getter = property.getter.clone().unwrap_or(Value::Undefined);
let setter = property.setter.clone().unwrap_or(Value::Undefined);
object.define_data("get", getter);
object.define_data("set", setter);
} else {
let value = property.value.clone().unwrap_or(Value::Undefined);
object.define_data("value", value);
object.define_data("writable", Value::Bool(property.writable));
}
object.define_data("enumerable", Value::Bool(property.enumerable));
object.define_data("configurable", Value::Bool(property.configurable));
Value::Object(object)
}
// -- calling ------------------------------------------------------------
/// Call `callee` with `this` and `args`.
pub fn call_value(&mut self, callee: Value, this: Value, args: Vec<Value>) -> JsResult<Value> {
let Value::Object(function) = &callee else {
// `typeof x === "function"` is not enough to make `x()` legal: only a
// callable object is. `null` and `undefined` need no special wording
// here — their `ToString` already reads the way the reference's does.
let what = format!("{} is not a function", callee.to_js_string());
return Err(Signal::Throw(self.make_error("TypeError", &what)));
};
// A proxy of a function is called through its `apply` trap. It has no
// `ObjectKind::Function` of its own, so this has to come before the dispatch
// below, and a proxy of a non-function has nothing to forward to.
if function.proxy_state().is_some() {
return match self.proxy_apply(function, this, args)? {
Some(result) => Ok(result),
None => Err(Signal::Throw(
self.make_error("TypeError", "proxy target is not callable"),
)),
};
}
let callable = function.with(|data| match &data.kind {
ObjectKind::Function(callable) => match callable {
Callable::Native { func, .. } => Some(NativeCall::Native(*func)),
// The `Rc` is cloned out so the borrow of this object's data is released
// before the closure runs: the closure may touch the same object.
Callable::Host { func, .. } => Some(NativeCall::Host(func.clone())),
Callable::Bound {
target,
this,
args: leading,
} => Some(NativeCall::Bound {
target: target.clone(),
this: this.clone(),
leading: leading.clone(),
}),
Callable::Bytecode { code, env } => {
Some(NativeCall::Bytecode(code.clone(), env.clone()))
}
Callable::Revoker(slots) => Some(NativeCall::Revoker(slots.clone())),
Callable::PromiseResolve(promise) => {
Some(NativeCall::PromiseResolve(promise.clone()))
}
Callable::PromiseReject(promise) => {
Some(NativeCall::PromiseReject(promise.clone()))
}
Callable::AwaitResume {
coroutine,
promise,
kind,
} => Some(NativeCall::AwaitResume {
coroutine: coroutine.clone(),
promise: promise.clone(),
kind: *kind,
}),
Callable::CombinatorSettle {
state,
index,
fulfilled,
} => Some(NativeCall::CombinatorSettle {
state: state.clone(),
index: *index,
fulfilled: *fulfilled,
}),
Callable::FinallyShim {
callback,
fulfilled,
} => Some(NativeCall::FinallyShim {
callback: callback.clone(),
fulfilled: *fulfilled,
}),
Callable::ModuleBinding { cell } => Some(NativeCall::ModuleBinding(cell.clone())),
Callable::CompletedReturn {
generator,
promise,
rejected,
} => Some(NativeCall::CompletedReturn {
generator: generator.clone(),
promise: promise.clone(),
rejected: *rejected,
}),
Callable::YieldResult {
generator,
promise,
done,
} => Some(NativeCall::YieldResult {
generator: generator.clone(),
promise: promise.clone(),
done: *done,
}),
Callable::DisposeStep {
remaining,
promise,
error,
rejected,
} => Some(NativeCall::DisposeStep {
remaining: remaining.clone(),
promise: promise.clone(),
error: error.clone(),
rejected: *rejected,
}),
Callable::GeneratorResume {
generator,
promise,
kind,
} => Some(NativeCall::GeneratorResume {
generator: generator.clone(),
promise: promise.clone(),
kind: *kind,
}),
Callable::SettlePromise {
promise,
value,
rejected,
} => Some(NativeCall::SettlePromise {
promise: promise.clone(),
value: value.clone(),
rejected: *rejected,
}),
},
_ => None,
});
match callable {
Some(NativeCall::Host(func)) => {
// Same frame bookkeeping as a `Native`, so a stack trace shows the host
// function by name rather than stopping at the caller.
let frame_less = function
.own_property("_frame_less")
.and_then(|property| property.value)
.is_some_and(|value| value.to_boolean());
let name = function
.own_property("name")
.and_then(|property| property.value)
.map(|value| value.to_js_string())
.unwrap_or_default();
let outer = if frame_less {
None
} else {
self.native_frames
.push((name, function.clone(), self.frames.len()));
Some(self.current_native.replace(function.clone()))
};
let result = {
let mut body = func.borrow_mut();
body(self, this, &args)
};
if !frame_less {
self.native_frames.pop();
self.current_native = outer.flatten();
}
result
}
Some(NativeCall::Native(func)) => {
// A function marked `_frame_less` is one the reference implements
// without a call frame — `eval` is the only one so far.
let frame_less = function
.own_property("_frame_less")
.and_then(|property| property.value)
.is_some_and(|value| value.to_boolean());
let name = function
.own_property("name")
.and_then(|property| property.value)
.map(|value| value.to_js_string())
.unwrap_or_default();
let outer = if frame_less {
None
} else {
self.native_frames
.push((name, function.clone(), self.frames.len()));
Some(self.current_native.replace(function.clone()))
};
let result = func(self, this, &args);
if !frame_less {
self.native_frames.pop();
self.current_native = outer.flatten();
}
result
}
Some(NativeCall::Revoker(slots)) => {
// Idempotent: revoking twice answers `undefined` again rather than
// complaining, which is what the spec says and what a `finally` block
// relies on.
let mut state = slots.borrow_mut();
state.target = None;
state.handler = None;
Ok(Value::Undefined)
}
Some(NativeCall::PromiseResolve(promise)) => {
// One-shot: a second call is silently ignored, which
// `fulfill_promise`/`reject_promise` enforce by refusing to settle twice.
let value = args.first().cloned().unwrap_or(Value::Undefined);
self.resolve_promise_with(&promise, value)?;
Ok(Value::Undefined)
}
Some(NativeCall::PromiseReject(promise)) => {
let reason = args.first().cloned().unwrap_or(Value::Undefined);
self.reject_promise(&promise, reason);
Ok(Value::Undefined)
}
Some(NativeCall::CombinatorSettle {
state,
index,
fulfilled,
}) => {
let value = args.first().cloned().unwrap_or(Value::Undefined);
self.settle_combinator(&state, index, fulfilled, value)?;
Ok(Value::Undefined)
}
Some(NativeCall::AwaitResume {
coroutine,
promise,
kind,
}) => {
// Resume *inline*, in the reaction that delivered the value. Deferring it
// by another job inserts a tick between every `await` and its
// continuation, which reorders `await` against unrelated `then` chains —
// `f().then(g)` then runs before `f`'s own next statement.
self.drive_async(
&coroutine,
&promise,
kind,
args.first().cloned().unwrap_or(Value::Undefined),
)
.map(|()| Value::Undefined)
}
Some(NativeCall::FinallyShim {
callback,
fulfilled,
}) => {
// §27.2.5.4.1: run `onFinally` with no arguments, then hand back a promise
// that settles with the *original* outcome once the callback's result has
// settled. The callback's own value is discarded — that is the whole
// difference between `finally` and `then`.
let original = args.first().cloned().unwrap_or(Value::Undefined);
let result = match &callback {
Some(callback) => {
self.call_value(callback.clone(), Value::Undefined, Vec::new())?
}
None => Value::Undefined,
};
let resolved = self.promise_resolve_value(result)?;
let Value::Object(resolved) = resolved else {
return Ok(original);
};
// The wait is a chained promise rather than a bare return, so a callback
// that returns a slow promise really does delay the continuation.
let waiting = self.new_promise();
let pass = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::SettlePromise {
promise: waiting.clone(),
value: original,
rejected: !fulfilled,
},
);
let pass = Value::Object(pass);
// Whichever way the callback's promise settles, the original outcome wins.
self.perform_then(&resolved, Some(pass.clone()), Some(pass))?;
Ok(Value::Object(waiting))
}
Some(NativeCall::DisposeStep {
remaining,
promise,
error,
rejected,
}) => {
let error = if rejected {
let reason = args.first().cloned().unwrap_or(Value::Undefined);
self.merge_dispose_error(reason, error)
} else {
error
};
self.dispose_step(&remaining, &promise, error)?;
Ok(Value::Undefined)
}
Some(NativeCall::ModuleBinding(cell)) => Ok(cell.get()),
Some(NativeCall::CompletedReturn {
generator,
promise,
rejected,
}) => {
let value = args.first().cloned().unwrap_or(Value::Undefined);
let outcome = if rejected {
AsyncOutcome::Error(value)
} else {
AsyncOutcome::Value(self.iterator_result(value, true)?)
};
self.settle_async_request(&generator, &promise, outcome)?;
Ok(Value::Undefined)
}
Some(NativeCall::YieldResult {
generator,
promise,
done,
}) => {
let value = args.first().cloned().unwrap_or(Value::Undefined);
let result = self.iterator_result(value, done)?;
self.settle_async_request(&generator, &promise, AsyncOutcome::Value(result))?;
Ok(Value::Undefined)
}
Some(NativeCall::GeneratorResume {
generator,
promise,
kind,
}) => {
// Inline for the same reason as `AwaitResume`: a deferred resume adds a
// tick and reorders the generator against other chains.
self.step_async_generator(
&generator,
&promise,
kind,
args.first().cloned().unwrap_or(Value::Undefined),
)
.map(|()| Value::Undefined)
}
Some(NativeCall::SettlePromise {
promise,
value,
rejected,
}) => {
if rejected {
self.reject_promise(&promise, value);
} else {
self.fulfill_promise(&promise, value);
}
Ok(Value::Undefined)
}
Some(NativeCall::Bound {
target,
this: bound_this,
leading,
}) => {
let mut all = leading;
all.extend(args);
self.call_value(target, bound_this, all)
}
Some(NativeCall::Bytecode(code, env)) => {
if code.code.kind.is_generator() {
return self.new_generator(callee, this, args);
}
if code.code.kind.is_async() {
// Same coroutine machinery as a generator; the difference is only in
// what the completion does — see `new_async_function`.
return self.new_async_function(callee, this, args);
}
self.execute(code, env, this, args, callee)
}
None => Err(Signal::Throw(
self.make_error("TypeError", "not a function"),
)),
}
}
/// Give a freshly built function its own `prototype` object.
///
/// Every function that can be used with `new` carries one, and its
/// `constructor` points back at the function — a reference cycle, which is
/// exactly what the collector's cycle pass exists to reclaim.
fn install_function_prototype(&mut self, function: &Gc<JsObject>) {
let prototype = self.new_object(Some(self.realm.object_proto.clone()));
let constructor = Value::Object(function.clone());
prototype.define(
"constructor",
Property::with_attributes(constructor, true, false),
);
let value = Value::Object(prototype);
function.define("prototype", Property::prototype_of(value, true));
}
/// The object `special_object` builds.
///
/// Only `arguments` is implemented. It is an ordinary object with a `length` and
/// one property per passed argument, which is what `typeof`, `arguments[i]` and
/// `arguments.length` observe. Mapped arguments — sloppy mode's aliasing between
/// `arguments[i]` and the parameter it came from — is *not* implemented, so a
/// write through `arguments` does not reach the parameter.
fn special_object(&mut self, magic: u32) -> JsResult<Value> {
match magic {
0 | 1 => {
let args = self.frame().passed.clone();
let proto = self.realm.object_proto.clone();
let object = self.new_object(Some(proto));
// The marker `Object.prototype.toString` needs: an arguments object is an
// ordinary object apart from reporting `[object Arguments]`, and that has
// no `Symbol.toStringTag` of its own. Set before the indices so it does not
// show up ahead of them in a key listing anyway — it is non-enumerable.
self.internal_set(&object, "_class", self.string("Arguments"));
// `length` is writable and configurable but neither enumerable nor a
// plain data property: `Object.keys(arguments)` lists the indices only,
// which is how `{...arguments}` copies them without the length.
object.define(
"length",
Property::with_attributes(Value::Int(args.len() as i32), true, false),
);
for (index, value) in args.into_iter().enumerate() {
object.define_data(&index.to_string(), value);
}
// An arguments object is iterable: `[...arguments]` and
// `for (const a of arguments)` both work, through the *array* iterator
// because the object is array-like. Without this the whole spread form
// reported "not iterable".
let iterator_key = self.realm.well_known_key(WellKnown::Iterator);
let values = self
.realm
.array_proto
.own_property("values")
.and_then(|property| property.value);
if let Some(values) = values {
object.define_key(
&iterator_key,
Property::with_attributes(values, true, false),
);
}
Ok(Value::Object(object))
}
// `this.active_func`: the constructor `super()` resolves against.
//
// Only ever asked of a *non-arrow* frame. An arrow has no `[[ThisMode]]` of its
// own, so it inherits this from the function it was written in along with `this`
// — and that is a lexical relationship, not a stack one: an arrow that escaped
// the constructor and is called back later (`iter.return = () => super()`) has
// the constructor nowhere on the stack. The compiler therefore reads it out of
// a closure variable of the enclosing function, exactly as the reference does
// with its `this_active_func` local.
2 => Ok(self
.current_callee()
.map_or(Value::Undefined, Value::Object)),
// `new.target`: set by `construct`, `undefined` for an ordinary call, and
// inherited from the caller inside an `eval`. An arrow has no `new.target` of
// its own either, so the lookup walks out to the nearest non-arrow frame for
// the same reason as `this.active_func` above — without that, `super()` inside
// an arrow passed `undefined` as the new target and the parent constructor was
// then rejected for being invoked without `new`.
3 => Ok(self
.eval_new_target
.clone()
.unwrap_or_else(|| self.frame().new_target.clone())),
// `[[HomeObject]]` of the function being run: what `super` is relative to.
// An arrow has none of its own, so the lookup walks out to the enclosing
// method — which is why `super` works inside an arrow inside a method.
4 => Ok(self
.frames
.iter()
.rev()
.find_map(|frame| frame.callee.as_object().and_then(|f| f.home_object()))
.map_or(Value::Undefined, Value::Object)),
// `import.meta`: the innermost module body's meta object. Outside a module
// the compiler never emits this, so an empty fallback is unreachable rather
// than permissive.
6 => {
let name = self.module_stack.last().cloned();
let meta = name
.and_then(|name| self.modules.get(&name).cloned())
.and_then(|module| module.with(|r| r.import_meta.clone()));
Ok(meta.map_or(Value::Undefined, Value::Object))
}
other => Err(Signal::Throw(self.make_error(
"InternalError",
&format!("special_object {other} is not implemented yet"),
))),
}
}
/// `[[HomeObject]]` of the innermost function that has one, if any.
///
/// An arrow has none, so the search walks out to the enclosing method — the same
/// rule `special_object 4` follows, and the reason `super` works inside an arrow
/// inside a method.
fn enclosing_home_object(&self) -> Option<Gc<JsObject>> {
self.frames
.iter()
.rev()
.find_map(|frame| frame.callee.as_object().and_then(|f| f.home_object()))
}
/// The dispose method a `using` declaration should snapshot, or an error.
///
/// `await`-using prefers `Symbol.asyncDispose` and falls back to `Symbol.dispose`, which
/// is what makes a resource that only knows the synchronous protocol usable in both
/// forms.
pub fn using_method_for(&mut self, value: &Value, await_form: bool) -> JsResult<Value> {
// `null` and `undefined` are explicitly allowed and simply dispose to nothing.
if value.is_nullish() {
return Ok(Value::Undefined);
}
let Some(object) = value.as_object() else {
return Err(self.type_error("not an object"));
};
let mut method = if await_form {
let found = self.get_property_key(
object,
&self.realm.well_known_key(WellKnown::AsyncDisposable),
)?;
match found {
Some(found) if !found.is_nullish() => Some(found),
_ => self
.get_property_key(object, &self.realm.well_known_key(WellKnown::Disposable))?,
}
} else {
self.get_property_key(object, &self.realm.well_known_key(WellKnown::Disposable))?
};
let Some(found) = method.take() else {
return Err(self.type_error("value is not disposable"));
};
if found.is_nullish() {
return Err(self.type_error("value is not disposable"));
}
let callable = found.as_object().is_some_and(|object| object.is_callable());
if !callable {
return Err(self.type_error("dispose method is not a function"));
}
Ok(found)
}
/// Call one resource's disposer, folding a failure into `error_state`.
///
/// The value is read through [`Interp::get_loc`] rather than off `locals` directly,
/// because a captured `using` binding lives in its reference cell and the local slot is
/// stale — a closure over the resource is legal and must not change what is disposed.
fn using_dispose_once(&mut self, slot: usize, error_state: Value) -> JsResult<Value> {
let value = self.get_loc(slot);
let method = self.get_loc(slot + 1);
// A resource that was never initialised — its declaration threw — or that is
// `null`/`undefined` has nothing to dispose.
if value.is_nullish() || matches!(value, Value::Uninitialized) {
return Ok(error_state);
}
if matches!(method, Value::Undefined | Value::Uninitialized) {
return Ok(error_state);
}
match self.call_value(method, value, Vec::new()) {
Ok(_) => Ok(error_state),
Err(Signal::Throw(thrown)) => Ok(self.merge_dispose_error(thrown, error_state)),
Err(other) => Err(other),
}
}
/// Combine a disposal failure with whatever was already going wrong.
pub fn merge_dispose_error(&mut self, new_error: Value, error_state: Value) -> Value {
if matches!(error_state, Value::Uninitialized) {
return new_error;
}
self.new_suppressed_error(new_error, error_state)
}
/// A `SuppressedError` instance built straight from its prototype.
///
/// Not through the constructor: a script that rewrites
/// `SuppressedError.prototype.constructor` must not be able to divert the engine's own
/// error wrapping.
pub fn new_suppressed_error(&mut self, error: Value, suppressed: Value) -> Value {
let proto = self
.realm
.suppressed_error_proto
.clone()
.unwrap_or_else(|| self.realm.error_proto.clone());
let object = self.new_object(Some(proto));
object.define("error", Property::with_attributes(error, true, true));
object.define(
"suppressed",
Property::with_attributes(suppressed, true, true),
);
object.define(
"message",
Property::with_attributes(self.string("SuppressedError"), true, true),
);
self.internal_set(&object, "_class", self.string("Error"));
self.add_stack(&object);
Value::Object(object)
}
/// Call `callee`, or push its frame and let the main loop run it.
///
/// This is what keeps deep JavaScript recursion off the Rust stack: a bytecode
/// callee becomes a frame on [`Interp::frames`] and the instruction loop simply
/// continues, so `function f(){ return f() }` costs heap and stops at
/// [`Interp::max_frames`] instead of aborting the process. Only a native callee
/// is entered directly, and natives do not re-enter JavaScript in this
/// milestone.
///
/// `Ok(None)` means a frame was pushed: the caller must not push a result,
/// because the returning frame will.
/// The elements of an array-like value, as a call argument list.
///
/// `apply` accepts any array-like, not only a real array: the reference reads
/// `length` and then each index, so `{length: 2, 0: "a", 1: "b"}` works too.
pub fn array_like_to_args(&mut self, value: &Value) -> JsResult<Vec<Value>> {
let Some(object) = value.as_object().cloned() else {
if value.is_nullish() {
// `f.apply(this, null)` passes no arguments at all.
return Ok(Vec::new());
}
return Err(self.type_error("createListFromArrayLike called on non-object"));
};
let length = match self.get_property(&object, "length")? {
Some(length) => length.to_number(),
None => 0.0,
};
if !length.is_finite() || length <= 0.0 {
return Ok(Vec::new());
}
let length = (length as usize).min(1_000_000);
let mut args = Vec::with_capacity(length);
for index in 0..length {
let element = self.get_property(&object, &index.to_string())?;
args.push(element.unwrap_or(Value::Undefined));
}
Ok(args)
}
/// Turn a call to a generator function into a generator object.
///
/// The frame is built exactly as an ordinary call would build it, then detached and
/// stored in the generator before any body instruction runs — which is what makes
/// `function* g(){ sideEffect() }` inert until `next()`. The one thing that does run
/// is `initial_yield`, the first instruction of every suspending body.
pub fn new_generator(
&mut self,
function: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Value> {
let Some(object) = function.as_object().cloned() else {
return Err(self.type_error("not a generator function"));
};
let Some((code, env)) = bytecode_of(&object) else {
return Err(self.type_error("not a generator function"));
};
let is_async = code.code.kind.is_async();
self.push_frame(code, env, this, args, function)?;
let frame = self.frames.pop().expect("push_frame just pushed it");
// A fresh generator is *suspended at start*: the body has not run, so a
// `return`/`throw` on it finishes without executing anything.
let mut state = GeneratorState {
frame: Some(frame),
phase: GeneratorPhase::SuspendedStart,
parked_on_await: false,
parked_on_yield_star: false,
requests: std::collections::VecDeque::new(),
async_busy: false,
};
// Run to the first suspension. For a generator that is `initial_yield`, so no
// body code runs; for a malformed body it would be the first `yield`.
// An `async function*` answers an async generator: same coroutine, but its
// methods return promises and `for await` reaches it through `Symbol.asyncIterator`.
let proto = if is_async {
self.realm
.async_generator_proto
.clone()
.unwrap_or_else(|| self.realm.generator_proto.clone())
} else {
self.realm.generator_proto.clone()
};
let generator = self.new_object(Some(proto));
match self.step_generator(&mut state, ResumeKind::Start, Value::Undefined) {
Ok(_) => {}
Err(error) => return Err(error),
}
generator.with_mut(|data| {
data.kind = ObjectKind::Generator(Box::new(state));
});
Ok(Value::Object(generator))
}
/// Advance a generator object, as `%GeneratorPrototype%.next` and friends do.
///
/// Returns the `{value, done}` object itself rather than the pair, because a
/// `yield*` suspension already has one — the inner iterator's own result — and the
/// protocol says to hand that straight through rather than nest it.
/// Advance a generator and report *why* it parked.
///
/// The sync `next()` does not need the reason — a `yield` and an `await` both mean
/// "hand this value to the caller" — but an async generator does: an `await` has to
/// settle before it can answer, while a `yield` is the answer.
fn resume_generator_detailed(
&mut self,
generator: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<(Value, bool, bool, Option<SuspendKind>)> {
let Some(mut state) = generator.with_mut(|data| match &mut data.kind {
ObjectKind::Generator(state) => Some(std::mem::replace(
state.as_mut(),
GeneratorState {
frame: None,
phase: GeneratorPhase::Executing,
parked_on_await: false,
parked_on_yield_star: false,
requests: std::collections::VecDeque::new(),
async_busy: false,
},
)),
_ => None,
}) else {
return Err(self.type_error("not a generator"));
};
let result = self.step_generator(&mut state, kind, value);
generator.with_mut(|data| {
data.kind = ObjectKind::Generator(Box::new(state));
});
result
}
/// Advance a generator object, as `%GeneratorPrototype%.next` and friends do.
///
/// Returns the `{value, done}` object itself rather than the pair, because a
/// `yield*` suspension already has one — the inner iterator's own result — and the
/// protocol says to hand that straight through rather than nest it.
pub fn resume_generator(
&mut self,
generator: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<Value> {
let (value, done, ready, _reason) =
self.resume_generator_detailed(generator, kind, value)?;
if ready {
return Ok(value);
}
self.iterator_result(value, done)
}
/// `next`/`return`/`throw` on an async generator, which answer a promise.
///
/// The body is the same coroutine a sync generator runs; what changes is that an
/// `await` inside it has to settle before the method can answer, so the step is
/// driven off the job queue rather than returning inline.
pub fn resume_async_generator(
&mut self,
generator: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<Value> {
let promise = self.new_promise();
// Queue, then serve if nothing else is: `it.next(); it.next(v)` has to answer in
// that order, and the second value must reach the *second* `yield`.
let idle = generator.with_mut(|data| match &mut data.kind {
ObjectKind::Generator(state) => {
state.requests.push_back(AsyncRequest {
kind,
value,
promise: promise.clone(),
});
!state.async_busy
}
_ => false,
});
if idle {
self.next_async_request(generator)?;
}
Ok(Value::Object(promise))
}
/// Serve the front queued request, if the generator is free to run one.
///
/// A finished generator answers from the queue without running anything:
/// `next` is `{undefined, done:true}`, `return(v)` is `{v, done:true}` with `v` awaited
/// first, and `throw(e)` rejects.
fn next_async_request(&mut self, generator: &Gc<JsObject>) -> JsResult<()> {
enum Front {
Idle,
Run(ResumeKind, Value, Gc<JsObject>),
Finished(ResumeKind, Value, Gc<JsObject>),
}
let front = generator.with_mut(|data| match &mut data.kind {
ObjectKind::Generator(state) => {
let Some(request) = state.requests.front() else {
return Front::Idle;
};
// Busy means a request is still in flight — awaiting inside the body, not
// finished. Serving another now would interleave two requests.
if state.async_busy && state.phase != GeneratorPhase::Completed {
return Front::Idle;
}
let kind = request.kind;
let value = request.value.clone();
let promise = request.promise.clone();
state.async_busy = true;
if state.phase == GeneratorPhase::Completed {
Front::Finished(kind, value, promise)
} else {
Front::Run(kind, value, promise)
}
}
_ => Front::Idle,
});
match front {
Front::Idle => Ok(()),
Front::Run(kind, value, promise) => {
self.step_async_generator(generator, &promise, kind, value)
}
Front::Finished(kind, value, promise) => match kind {
ResumeKind::Throw => {
self.settle_async_request(generator, &promise, AsyncOutcome::Error(value))
}
ResumeKind::Next | ResumeKind::Start => {
let result = self.iterator_result(Value::Undefined, true)?;
self.settle_async_request(generator, &promise, AsyncOutcome::Value(result))
}
ResumeKind::Return => {
// `js_async_generator_completed_return`: the value is awaited before it
// becomes `{value, done: true}`, so `it.return(Promise.resolve(1))` on a
// finished generator answers `1`, not a promise.
let resolved = match self.promise_resolve_value(value) {
Ok(resolved) => resolved,
Err(Signal::Throw(reason)) => {
return self.settle_async_request(
generator,
&promise,
AsyncOutcome::Error(reason),
);
}
Err(other) => return Err(other),
};
let done = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::CompletedReturn {
generator: generator.clone(),
promise: promise.clone(),
rejected: false,
},
);
let failed = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::CompletedReturn {
generator: generator.clone(),
promise: promise.clone(),
rejected: true,
},
);
let Value::Object(resolved) = resolved else {
return self.settle_async_request(
generator,
&promise,
AsyncOutcome::Error(Value::Undefined),
);
};
self.perform_then(
&resolved,
Some(Value::Object(done)),
Some(Value::Object(failed)),
)?;
Ok(())
}
},
}
}
/// Settle the front request and start the next one.
///
/// Every path out of [`Interp::step_async_generator`] goes through here, so the
/// "busy" flag and the queue cannot drift: a promise settled without releasing the
/// generator would strand every later request.
fn settle_async_request(
&mut self,
generator: &Gc<JsObject>,
promise: &Gc<JsObject>,
outcome: AsyncOutcome,
) -> JsResult<()> {
let settled = generator.with_mut(|data| match &mut data.kind {
ObjectKind::Generator(state) => {
let front = state.requests.front();
debug_assert!(
front.is_some_and(|request| Gc::ptr_eq(&request.promise, promise)),
"a request settled outside its turn"
);
state.requests.pop_front();
state.async_busy = false;
true
}
_ => false,
});
if settled {
match outcome {
AsyncOutcome::Value(value) => self.fulfill_promise(promise, value),
AsyncOutcome::Error(reason) => self.reject_promise(promise, reason),
}
}
self.next_async_request(generator)
}
/// Dispose a list of `(resource, disposeMethod)` pairs, awaiting each disposer.
///
/// Returns the promise `AsyncDisposableStack.disposeAsync` hands back. The first
/// disposer runs **synchronously** — the reference starts it before returning, which
/// a test can observe by logging after the call — and the rest follow once their
/// predecessor settles.
pub fn dispose_async_all(&mut self, resources: Vec<(Value, Value)>) -> JsResult<Value> {
let promise = self.new_promise();
let mut pairs = Vec::with_capacity(resources.len());
for (value, method) in resources {
let pair = self.new_array(vec![value, method]);
pairs.push(Value::Object(pair));
}
let remaining = self.new_array(pairs);
self.dispose_step(&remaining, &promise, Value::Uninitialized)?;
Ok(Value::Object(promise))
}
/// One link of the `disposeAsync` chain.
///
/// Loops while the disposers finish synchronously, and parks on the first thenable it
/// meets — pushing itself back in as the continuation, with the accumulated error
/// carried along so a later failure can still be folded into the `SuppressedError`.
fn dispose_step(
&mut self,
remaining: &Gc<JsObject>,
promise: &Gc<JsObject>,
mut error: Value,
) -> JsResult<()> {
loop {
let length = remaining.array_length().unwrap_or(0);
if length == 0 {
if matches!(error, Value::Uninitialized) {
self.fulfill_promise(promise, Value::Undefined);
} else {
self.reject_promise(promise, error);
}
return Ok(());
}
let Some(Value::Object(pair)) = remaining
.array_get(length - 1)
.filter(|v| !matches!(v, Value::Hole))
else {
remaining.array_truncate(self.heap, length - 1);
continue;
};
remaining.array_truncate(self.heap, length - 1);
let value = pair.array_get(0).unwrap_or(Value::Undefined);
let Some(Value::Object(method)) = pair.array_get(1) else {
continue;
};
if !method.is_callable() {
continue;
}
match self.call_value(Value::Object(method), value, Vec::new()) {
Ok(result) => {
if !self.is_thenable(&result)? {
// Finished synchronously; the next disposer is the next iteration.
continue;
}
let awaited = match self.promise_resolve_value(result) {
Ok(awaited) => awaited,
Err(Signal::Throw(reason)) => {
self.reject_promise(promise, reason);
return Ok(());
}
Err(other) => return Err(other),
};
let Value::Object(awaited) = awaited else {
continue;
};
let ok = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::DisposeStep {
remaining: remaining.clone(),
promise: promise.clone(),
error: error.clone(),
rejected: false,
},
);
let failed = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::DisposeStep {
remaining: remaining.clone(),
promise: promise.clone(),
error,
rejected: true,
},
);
self.perform_then(
&awaited,
Some(Value::Object(ok)),
Some(Value::Object(failed)),
)?;
return Ok(());
}
Err(Signal::Throw(thrown)) => {
error = self.merge_dispose_error(thrown, error);
}
Err(other) => return Err(other),
}
}
}
/// Whether `value` would be followed by `ResolvePromise` rather than fulfilled as-is.
fn is_thenable(&mut self, value: &Value) -> JsResult<bool> {
let Value::Object(object) = value else {
return Ok(false);
};
Ok(matches!(
self.get_property(object, "then")?,
Some(Value::Object(then)) if then.is_callable()
))
}
/// One step of an async generator, settling `promise` with an `{value, done}` result.
fn step_async_generator(
&mut self,
generator: &Gc<JsObject>,
promise: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<()> {
let stepped = self.resume_generator_detailed(generator, kind, value);
let (value, done, ready, reason) = match stepped {
Ok(stepped) => stepped,
Err(Signal::Throw(reason)) => {
return self.settle_async_request(generator, promise, AsyncOutcome::Error(reason));
}
Err(other) => return Err(other),
};
// Only an `await` has to wait before the method can answer.
if !done && reason == Some(SuspendKind::Await) {
let awaited = match self.promise_resolve_value(value) {
Ok(awaited) => awaited,
Err(Signal::Throw(reason)) => {
return self.settle_async_request(
generator,
promise,
AsyncOutcome::Error(reason),
);
}
Err(other) => return Err(other),
};
let Value::Object(awaited) = awaited else {
return Ok(());
};
let resumed = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::GeneratorResume {
generator: generator.clone(),
promise: promise.clone(),
kind: ResumeKind::Next,
},
);
let raised = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::GeneratorResume {
generator: generator.clone(),
promise: promise.clone(),
kind: ResumeKind::Throw,
},
);
self.perform_then(
&awaited,
Some(Value::Object(resumed)),
Some(Value::Object(raised)),
)?;
return Ok(());
}
// `yield v` in an *async* generator awaits `v`, and so does the `return v` that
// completes it — both arrive as the settled value rather than as the promise
// object. A plain value skips the round trip, which is also what keeps `yield 1`
// from costing an extra tick.
let awaits_value = self.is_thenable(&value)?;
if awaits_value {
let awaited = match self.promise_resolve_value(value) {
Ok(awaited) => awaited,
Err(Signal::Throw(reason)) => {
return self.settle_async_request(
generator,
promise,
AsyncOutcome::Error(reason),
);
}
Err(other) => return Err(other),
};
let Value::Object(awaited) = awaited else {
return Ok(());
};
// A rejected yield throws *at the yield* inside the body, so the rejection
// resumes the generator rather than the method's promise.
let answer = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::YieldResult {
generator: generator.clone(),
promise: promise.clone(),
done,
},
);
let raise = JsObject::function(
self.heap,
Some(self.realm.function_proto.clone()),
Callable::GeneratorResume {
generator: generator.clone(),
promise: promise.clone(),
kind: ResumeKind::Throw,
},
);
self.perform_then(
&awaited,
Some(Value::Object(answer)),
Some(Value::Object(raise)),
)?;
return Ok(());
}
let result = if ready {
// `yield*` handed back the inner iterator's own result object.
value
} else {
self.iterator_result(value, done)?
};
self.settle_async_request(generator, promise, AsyncOutcome::Value(result))
}
/// The `{value, done}` object an iterator method answers with.
pub fn iterator_result(&mut self, value: Value, done: bool) -> JsResult<Value> {
let result = self.new_object(Some(self.realm.object_proto.clone()));
result.define_data("value", value);
result.define_data("done", Value::Bool(done));
Ok(Value::Object(result))
}
/// Advance a generator, returning the `{value, done}` pair the protocol wants.
fn step_generator(
&mut self,
state: &mut GeneratorState,
kind: ResumeKind,
value: Value,
) -> JsResult<(Value, bool, bool, Option<SuspendKind>)> {
match state.phase {
GeneratorPhase::Executing => {
return Err(self.type_error("cannot invoke a running generator"));
}
GeneratorPhase::Completed => {
// A finished generator answers every resumption without running.
return Ok(match kind {
ResumeKind::Throw => return Err(Signal::Throw(value)),
ResumeKind::Return => (value, true, false, None),
ResumeKind::Next | ResumeKind::Start => (Value::Undefined, true, false, None),
});
}
GeneratorPhase::SuspendedStart => {
if kind != ResumeKind::Next && kind != ResumeKind::Start {
// The body never started, so there is nothing to unwind or return
// from: `return(v)` finishes, `throw(e)` rethrows at the caller.
state.frame = None;
state.phase = GeneratorPhase::Completed;
return Ok(match kind {
ResumeKind::Throw => return Err(Signal::Throw(value)),
_ => (value, true, false, None),
});
}
}
GeneratorPhase::SuspendedYield => {}
}
let Some(mut frame) = state.frame.take() else {
return Ok((Value::Undefined, true, false, None));
};
// `next(v)` and `return(v)` re-enter the `yield` with the value *and* the magic
// number that says which method resumed it; the emitted `if_false` branches on
// the magic. `initial_yield` reads neither, and a `throw` does not resume at all.
let mut raised = None;
match kind {
ResumeKind::Next | ResumeKind::Start => {
if state.phase == GeneratorPhase::SuspendedYield {
frame.stack.push(value);
// An `await` resumes with its value alone; only a `yield` gets the
// magic number that says which of `next`/`return` resumed it.
if !state.parked_on_await {
frame.stack.push(Value::Int(RESUME_NEXT));
}
}
}
ResumeKind::Return => {
// A `return` on a not-yet-started generator is handled above, so this is
// always a real resumption.
frame.stack.push(value);
frame.stack.push(Value::Int(RESUME_RETURN));
}
ResumeKind::Throw => {
// At a `yield*` the exception is *data* for the delegation: the compiled
// code resumes with the value and magic `2` and calls the inner iterator's
// `throw`. Everywhere else it is raised at the suspension point, so the
// body's own handler sees it.
if state.phase == GeneratorPhase::SuspendedYield && state.parked_on_yield_star {
frame.stack.push(value);
frame.stack.push(Value::Int(RESUME_THROW));
} else {
raised = Some(value);
}
}
}
state.phase = GeneratorPhase::Executing;
let depth = self.frames.len();
self.frames.push(frame);
// A `throw` unwinds from where the frame parked, exactly as if the exception had
// happened at the `yield`.
if let Some(exception) = raised {
if self.catch_here(exception.clone()).is_none() {
self.frames.pop();
state.phase = GeneratorPhase::Completed;
return Err(Signal::Throw(exception));
}
}
// Only the generator's *own* frame ends the generator. The body may call ordinary
// functions, and those push frames of their own: a `Step::Return` from one of them
// is a nested call finishing, not the generator completing. Miscounting that is
// silent — the generator reports the callee's value as its own result and marks
// itself done — so the depth comparison is load-bearing, not defensive.
let own_frame = depth + 1;
loop {
match self.step() {
Ok(Step::Continue) => {}
Ok(Step::Suspend(reason)) if self.frames.len() == own_frame => {
let mut frame = self.frames.pop().expect("a frame is live");
// `yield` left the value on the stack; `initial_yield` left nothing.
let yielded = if reason == SuspendKind::Initial {
Value::Undefined
} else {
frame.stack.pop().unwrap_or(Value::Undefined)
};
state.frame = Some(frame);
state.parked_on_await = reason == SuspendKind::Await;
state.parked_on_yield_star =
matches!(reason, SuspendKind::YieldStar | SuspendKind::AsyncYieldStar);
state.phase = if reason == SuspendKind::Initial {
GeneratorPhase::SuspendedStart
} else {
GeneratorPhase::SuspendedYield
};
// `yield*` hands the inner iterator's own result object straight
// back, so the caller sees one `{value, done}` rather than a nested
// one — which is why the third field says "already a result".
return Ok((
yielded,
false,
reason == SuspendKind::YieldStar,
Some(reason),
));
}
Ok(Step::Suspend(_)) => {
// A `yield` in a frame other than the generator's would mean a
// generator body entered as an ordinary call, which
// `call_or_push` prevents.
self.unwind_to(depth);
state.phase = GeneratorPhase::Completed;
return Err(Signal::Throw(self.make_error(
"InternalError",
"a generator body ran as an ordinary call",
)));
}
Ok(Step::Return(value)) => {
self.frames.pop();
if self.frames.len() > depth {
// A nested call finished: hand the value back to its caller and
// keep running the generator body.
self.frames
.last_mut()
.expect("a caller is live")
.stack
.push(value);
continue;
}
state.phase = GeneratorPhase::Completed;
// The reference closes the generator's closure variables here; ours
// live in cells the frame owned, which the drop below releases.
return Ok((value, true, false, None));
}
Err(Signal::Throw(exception)) => {
// The frame that threw gets the first chance to catch, exactly as in
// the ordinary call loop; a frame above the generator's is popped and
// the exception carries on down.
let exception = exception;
loop {
if self.frames.len() <= depth {
// The generator's own frame is gone, so nothing here caught it.
state.phase = GeneratorPhase::Completed;
return Err(Signal::Throw(exception));
}
if self.catch_here(exception.clone()).is_some() {
break;
}
self.frames.pop();
}
}
Err(Signal::OutOfMemory) => return Err(Signal::OutOfMemory),
}
}
}
/// Pop frames until `depth` are left.
fn unwind_to(&mut self, depth: usize) {
self.frames.truncate(depth);
}
fn call_or_push(
&mut self,
callee: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Option<Value>> {
if let Value::Object(function) = &callee
&& let Some((code, env)) = bytecode_of(function)
{
// Calling a generator function does not run anything: it captures the
// arguments in a frame and hands back the generator object.
if code.code.kind.is_generator() {
return self.new_generator(callee, this, args).map(Some);
}
// An async function answers a promise and runs its body as a coroutine, so it
// takes the same path as a plain call into it.
if code.code.kind.is_async() {
return self.new_async_function(callee, this, args).map(Some);
}
self.push_frame(code, env, this, args, callee)?;
return Ok(None);
}
self.call_value(callee, this, args).map(Some)
}
/// `new callee(args)`.
///
/// A constructor differs from a plain call in three ways: it gets a fresh object
/// as `this` whose prototype is the constructor's `prototype` property, its
/// return value is *replaced* by that object unless it returns an object itself,
/// and a native constructor is expected to initialise the object it is given
/// rather than build one.
pub fn construct(
&mut self,
callee: Value,
new_target: Value,
args: Vec<Value>,
) -> JsResult<Value> {
// `new` on a bound function constructs its *target*, prepending the arguments
// the bind fixed and ignoring the bound `this` — a bound function is a
// constructor only when its target is one, and `new` always makes a fresh
// object of the target's prototype rather than reusing the bound receiver.
// Without this unwrapping, `constructor1.bind(null, 1)` called under `new`
// ran the body with `this === null` and threw on `this.x = a`.
let mut callee = callee;
let mut new_target = new_target;
let mut args = args;
loop {
let Some(object) = callee.as_object().cloned() else {
return Err(Signal::Throw(
self.make_error("TypeError", "not a constructor"),
));
};
// A proxy of a function constructs through its `construct` trap, and the
// trap's answer is the result directly — no fresh object is made here.
if object.proxy_state().is_some() {
return match self.proxy_construct(&object, args, &new_target)? {
Some(result) => Ok(result),
None => Err(Signal::Throw(
self.make_error("TypeError", "proxy target is not a constructor"),
)),
};
}
// A generator function has no `[[Construct]]`, and the reference names it in
// the message: `new (function* G(){})()` reports "G is not a constructor".
if object.with(|data| match &data.kind {
ObjectKind::Function(Callable::Bytecode { code, .. }) => {
code.code.kind.is_generator()
}
_ => false,
}) {
let name = object
.own_property("name")
.and_then(|property| property.value)
.map(|value| value.to_js_string())
.unwrap_or_default();
return Err(Signal::Throw(
self.make_error("TypeError", &format!("{name} is not a constructor")),
));
}
let bound = object.with(|data| match &data.kind {
ObjectKind::Function(Callable::Bound {
target,
args: leading,
..
}) => Some((target.clone(), leading.clone())),
_ => None,
});
let Some((target, leading)) = bound else {
// Not a bound function, so this object is the callee. §7.2.4
// `IsConstructor` decides whether `new` may proceed, and the two ad-hoc
// refusals above cover only the built-ins the reference also refuses by
// name. Everything else — a method, an arrow, an async function — has no
// `[[Construct]]` either, and answering "yes" here made
// `new (Array.prototype.map)` build an object instead of throwing.
if !object.is_constructor() {
let name = object
.own_property("name")
.and_then(|property| property.value)
.map(|value| value.to_js_string())
.unwrap_or_default();
return Err(Signal::Throw(
self.make_error("TypeError", &format!("{name} is not a constructor")),
));
}
break;
};
let mut all = leading;
all.extend(args);
args = all;
// A bound function that is its own `new.target` stands in for its target:
// `new (C.bind(null))()` makes a `C` instance, so the object has to come
// from `C.prototype`, not from the bound wrapper (which has no `prototype`).
let same_wrapper = match (new_target.as_object(), callee.as_object()) {
(Some(new_target), Some(bound)) => new_target.ptr_eq(bound),
_ => false,
};
if same_wrapper {
new_target = target.clone();
}
callee = target;
}
let Value::Object(function) = &callee else {
unreachable!("the loop above only breaks on an object");
};
// The fresh object's prototype comes from `new.target`, not from the callee: a
// derived constructor reaches here through `super()`, and the instance must be a
// `new.target` instance, so `class D extends C { constructor(){ super() } }` makes
// an object inheriting `D.prototype` rather than `C.prototype`.
let prototype_source = match new_target.as_object() {
Some(target) => target.clone(),
None => function.clone(),
};
// A native constructor builds its own instance, through `new_ctor_instance`, once it
// has validated its arguments. Pre-building one here would read
// `new.target.prototype` too early: `Reflect.construct(Promise, [], badTarget)` has
// to throw `TypeError` for a non-callable executor *before* a throwing `prototype`
// getter runs, and `new DataView(detached, …)` likewise for the detached check.
let is_native = function.native_creates_own_instance();
let this = if is_native {
Value::Undefined
} else {
let prototype = self.get_property(&prototype_source, "prototype")?;
let prototype = match prototype {
Some(Value::Object(proto)) => Some(proto),
// `GetPrototypeFromConstructor`: when the requested `prototype` is not an
// object, the answer is the realm's *intrinsic* prototype for this
// constructor, not `%Object.prototype%`.
_ => {
let fallback = self.get_property(function, "prototype")?;
match fallback {
Some(Value::Object(proto)) => Some(proto),
_ => Some(self.realm.object_proto.clone()),
}
}
};
Value::Object(self.new_object(prototype))
};
// `new.target` inside the body is the constructor that was written, so a bound
// function's target sees itself rather than the bound wrapper.
//
// The save/restore is not cosmetic: a constructor that *throws* leaves through the
// `?`, so assigning `None` after the call would be skipped and the failed
// `new.target` would stay visible to every later native call. The symptom was a
// native constructor refusing a plain call — `var s = Symbol(); new
// FinalizationRegistry(1)` (which throws) followed by `Symbol()` reported
// "FinalizationRegistry is not a constructor".
let outer_new_target = self.pending_new_target.replace(new_target);
let result = self.call_value(callee, this.clone(), args);
self.pending_new_target = outer_new_target;
let result = result?;
// A constructor that returns an object overrides `this`; any other return
// value (including `undefined`) is ignored.
Ok(match result {
Value::Object(_) => result,
_ => this,
})
}
/// A `TypeError`/`ReferenceError`/`RangeError` object, as the value to throw.
///
/// The error classes themselves arrive with the builtins milestone; what
/// matters here is that the thrown value stringifies the way a script printing
/// the caught value expects.
pub fn make_error(&self, name: &str, message: &str) -> Value {
let proto = self
.realm
.errors
.get(name)
.map(|constructor| self.prototype_of(constructor))
.unwrap_or_else(|| self.realm.error_proto.clone());
let error = self.new_object(Some(proto));
// `name` and `message` are own properties of the instance, which is what
// makes `e.name` cheap and `Error.prototype.name` the fallback for a plain
// `Error`.
error.define(
"name",
Property::with_attributes(self.string(name), true, false),
);
error.define(
"message",
Property::with_attributes(self.string(message), true, false),
);
// The internal class, which is what tells a printer this is an error rather than a
// plain object with `name` and `message` properties. The constructor path sets it
// (`builtins/error.rs`); without it here an engine-thrown `TypeError` printed as
// `{ stack: … }` while `new TypeError(...)` printed as `TypeError: …`. It is
// literally `"Error"` in both places — the *name* is read from `name`, which is why
// a `RangeError` still prints as `RangeError`.
self.internal_set(&error, "_class", self.string("Error"));
// The trace is captured where the error is *made*, which is where the reference
// captures it too: `JS_ThrowTypeError` and friends call `build_backtrace` on the
// way out, so a caught engine error already has its stack even if the handler
// never rethrows it.
self.add_stack(&error);
Value::Object(error)
}
/// Give an error object its `stack` property, from the current frame stack.
///
/// Only objects get one — `throw 42` has no stack anywhere — and an object that
/// already has a `stack` keeps it, so an error caught and rethrown, or one whose
/// `stack` a script set, is not rewritten.
pub fn add_stack(&self, object: &Gc<JsObject>) {
self.add_stack_skipping(object, 0);
}
/// [`Interp::add_stack`], omitting the `skip` innermost frames.
///
/// An `Error` constructor passes 1: the reference builds those traces with
/// `JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL`, so `new Error()` does not report the
/// constructor itself, while a `RangeError` raised inside `toISOString` does
/// report `at toISOString (native)`.
pub fn add_stack_skipping(&self, object: &Gc<JsObject>, skip: usize) {
if object.has_own("stack") {
return;
}
let trace = self.stack_trace_skipping(skip);
let text = self.string(trace);
object.define("stack", Property::with_attributes(text, true, true));
}
/// The function object currently being called, if any.
///
/// A native function is a bare `fn` pointer, so it cannot capture which class
/// it belongs to; it reads that from the callee instead. This is also what a
/// stack trace would use.
pub fn current_callee(&self) -> Option<Gc<JsObject>> {
match &self.frames.last()?.callee {
Value::Object(object) => Some(object.clone()),
_ => None,
}
}
/// The `prototype` property of a constructor, falling back to
/// `Object.prototype` for anything that is not shaped like one.
pub fn prototype_of(&self, constructor: &Gc<JsObject>) -> Gc<JsObject> {
constructor
.own_property("prototype")
.and_then(|property| property.value)
.and_then(|value| value.as_object().cloned())
.unwrap_or_else(|| self.realm.object_proto.clone())
}
/// Throw a `TypeError` carrying `message`.
pub fn type_error(&self, message: impl AsRef<str>) -> Signal {
Signal::Throw(self.make_error("TypeError", message.as_ref()))
}
/// Throw a `RangeError` carrying `message`.
pub fn range_error(&self, message: impl AsRef<str>) -> Signal {
Signal::Throw(self.make_error("RangeError", message.as_ref()))
}
/// The `ReferenceError` a read of an uninitialised binding raises.
/// The name of the binding `slot` refers to, for an error message.
fn binding_name(&self, slot: usize, kind: VarSlot) -> String {
match kind {
VarSlot::Loc => self
.frame()
.code
.code
.vars
.get(slot)
.map(|v| v.name.clone()),
VarSlot::Closure => self
.frame()
.code
.code
.closure_vars
.get(slot)
.map(|c| c.name.clone()),
}
.unwrap_or_default()
}
/// Whether `slot` names a `const` binding, which rejects every store after its
/// initialiser.
fn is_read_only(&self, slot: usize, kind: VarSlot) -> bool {
match kind {
VarSlot::Loc => self
.frame()
.code
.code
.vars
.get(slot)
.is_some_and(|v| v.is_const),
VarSlot::Closure => self
.frame()
.code
.code
.closure_vars
.get(slot)
.is_some_and(|c| c.is_const),
}
}
/// The `TypeError` a store to a `const` binding raises.
///
/// `const` is enforced here rather than at compile time because the binding can be
/// assigned from a place the compiler cannot classify — `c = 2` inside a closure
/// is just a `put_var_ref_check`. `PutLocCheck`/`PutVarRefCheck` therefore check
/// both halves of what "checked store" means: the dead zone is over, and the
/// binding is writable.
fn read_only(&self, slot: usize, kind: VarSlot) -> Signal {
let name = self.binding_name(slot, kind);
Signal::Throw(self.make_error("TypeError", &format!("'{name}' is read-only")))
}
fn dead_zone(&self, slot: usize, kind: VarSlot) -> Signal {
let name = match kind {
VarSlot::Loc => self
.frame()
.code
.code
.vars
.get(slot)
.map(|v| v.name.clone()),
VarSlot::Closure => self
.frame()
.code
.code
.closure_vars
.get(slot)
.map(|c| c.name.clone()),
}
.unwrap_or_default();
Signal::Throw(self.make_error(
"ReferenceError",
&format!("cannot access '{name}' before initialization"),
))
}
fn throw_reference(&self, name: &str) -> Signal {
Signal::Throw(self.make_error("ReferenceError", &format!("'{name}' is not defined")))
}
// -- the instruction loop -----------------------------------------------
fn step(&mut self) -> JsResult<Step> {
// Decode inside a scope that borrows the frame, and keep only the `Copy` fields.
// The obvious version cloned the `Gc<CompiledCode>` first — a reference-count
// increment and decrement on *every instruction* — which showed up as
// `dispatch:empty-loop` running 25× slower than the reference.
let (pc, decoded) = {
let frame = self
.frames
.last()
.expect("step() is only called with a live frame");
let bytes = &frame.code.code.code;
let pc = frame.pc;
if pc >= bytes.len() {
return Ok(Step::Return(Value::Undefined));
}
(pc, decode(bytes, pc))
};
let Some((op, a, b, next)) = decoded else {
return Err(Signal::Throw(
self.make_error("InternalError", "undecodable bytecode"),
));
};
{
// One borrow for both writes: `at` is where a stack trace points and `pc` is
// where execution resumes, and they are always set together.
let frame = self
.frames
.last_mut()
.expect("the frame was live a moment ago");
frame.at = pc;
frame.pc = next;
}
self.check_memory()?;
self.execute_instruction(op, a, b, next)
}
fn push(&mut self, value: Value) {
self.frame_mut().stack.push(value);
}
fn pop(&mut self) -> Value {
self.frame_mut().stack.pop().unwrap_or(Value::Undefined)
}
fn pop_n(&mut self, count: usize) -> Vec<Value> {
let frame = self.frame_mut();
let start = frame.stack.len().saturating_sub(count);
frame.stack.split_off(start)
}
fn peek(&self, back: usize) -> Value {
let frame = self.frame();
frame
.stack
.get(frame.stack.len().wrapping_sub(1 + back))
.cloned()
.unwrap_or(Value::Undefined)
}
#[allow(clippy::too_many_lines)]
fn execute_instruction(&mut self, op: Opcode, a: u32, b: u32, next: usize) -> JsResult<Step> {
use Opcode::*;
match op {
// -- constants and stack shuffling -----------------------------
Nop => {}
PushMinus1 => self.push(Value::Int(-1)),
Push0 | Push1 | Push2 | Push3 | Push4 | Push5 | Push6 | Push7 => {
let value = match op {
Push0 => 0,
Push1 => 1,
Push2 => 2,
Push3 => 3,
Push4 => 4,
Push5 => 5,
Push6 => 6,
_ => 7,
};
self.push(Value::Int(value));
}
PushI8 | PushI16 | PushI32 => self.push(Value::Int(a as i32)),
PushBigintI32 => {
// The immediate form of a `BigInt` literal; the reference has the same
// split between this and a constant-pool entry.
let value = crate::bigint::BigInt::from_i64(a as i64);
self.push(self.bigint(value));
}
PushTrue => self.push(Value::Bool(true)),
PushFalse => self.push(Value::Bool(false)),
PushEmptyString => self.push(self.string("")),
Undefined => self.push(Value::Undefined),
Null => self.push(Value::Null),
Object => {
let proto = self.realm.object_proto.clone();
self.push(Value::Object(self.new_object(Some(proto))));
}
PushThis => {
let this = self.frame().this.clone();
self.push(this);
}
PushConst | PushConst8 => {
let value = self.constant(a as usize)?;
self.push(value);
}
PushAtomValue => {
let text = self.atom_text(a as usize);
self.push(self.string(text));
}
Dup => {
let value = self.peek(0);
self.push(value);
}
Dup2 => {
// `a b -> a b a b`
let b = self.peek(0);
let a = self.peek(1);
self.push(a);
self.push(b);
}
Dup3 => {
// `a b c -> a b c a b c`, which is how a super store keeps the receiver,
// prototype and key while reading the current value through them.
let c = self.peek(0);
let b = self.peek(1);
let a = self.peek(2);
self.push(a);
self.push(b);
self.push(c);
}
Swap => {
// `a b -> b a`
let bottom = self.pop();
let top = self.pop();
self.push(bottom);
self.push(top);
}
Perm3 => {
// `a b c -> b a c`, the reference's reordering for a spread method
// call: `obj func array` becomes the `func this array` that `apply`
// takes.
let c = self.pop();
let b = self.pop();
let a = self.pop();
self.push(b);
self.push(a);
self.push(c);
}
Dup1 => {
let value = self.peek(1);
self.push(value);
}
Drop => {
self.pop();
}
Nip => {
// `a b -> b`
let top = self.pop();
self.pop();
self.push(top);
}
Perm4 => {
// `obj prop a b -> a obj prop b`, the reference's reordering for a
// postfix update of a computed element: it puts the *old* value
// underneath the receiver and key so `put_array_el` can consume them
// and leave the old value as the expression's result.
let d = self.pop();
let c = self.pop();
let b = self.pop();
let a = self.pop();
self.push(c);
self.push(a);
self.push(b);
self.push(d);
}
Insert2 => {
// `obj a -> a obj a`
let value = self.pop();
let object = self.pop();
self.push(value.clone());
self.push(object);
self.push(value);
}
Insert3 => {
// `obj prop a -> a obj prop a`
let value = self.pop();
let key = self.pop();
let object = self.pop();
self.push(value.clone());
self.push(object);
self.push(key);
self.push(value);
}
Insert4 => {
// `this obj prop a -> a this obj prop a`
//
// `put_super_value` consumes all four and yields nothing, so the copy
// underneath is what leaves the assignment an expression value.
let value = self.pop();
let key = self.pop();
let object = self.pop();
let receiver = self.pop();
self.push(value.clone());
self.push(receiver);
self.push(object);
self.push(key);
self.push(value);
}
Swap2 => {
// `a b c d -> c d a b`
let d = self.pop();
let c = self.pop();
let b = self.pop();
let a = self.pop();
self.push(c);
self.push(d);
self.push(a);
self.push(b);
}
Rot3l => {
// `x a b -> a b x`
let b = self.pop();
let a = self.pop();
let x = self.pop();
self.push(a);
self.push(b);
self.push(x);
}
Rot3r => {
// `a b c -> c a b`
let c = self.pop();
let b = self.pop();
let a_value = self.pop();
self.push(c);
self.push(a_value);
self.push(b);
}
Rest => {
// `...rest` in a parameter list: the arguments from index `a` on, as a
// real array. It takes nothing off the stack — the arguments are in the
// frame, not on it.
let frame = self.frame();
let from = a as usize;
let collected: Vec<Value> = frame.passed.iter().skip(from).cloned().collect();
let array = self.new_array(collected);
self.push(Value::Object(array));
}
// -- slots ------------------------------------------------------
GetLoc | GetLoc8 | GetLoc0 | GetLoc1 | GetLoc2 | GetLoc3 => {
let slot = self.slot_operand(op, a);
let value = self.get_loc(slot);
self.push(value);
}
PutLoc | PutLoc8 | PutLoc0 | PutLoc1 | PutLoc2 | PutLoc3 => {
let slot = self.slot_operand(op, a);
let value = self.pop();
self.put_loc(slot, value);
}
SetLoc | SetLoc8 | SetLoc0 | SetLoc1 | SetLoc2 | SetLoc3 => {
let slot = self.slot_operand(op, a);
let value = self.pop();
self.put_loc(slot, value.clone());
self.push(value);
}
// The `_check` forms are the temporal dead zone: they throw when the
// slot still holds [`Value::Uninitialized`]. Only `get_loc_check`
// *reads*, so only the two writes may pop — mishandling that quietly
// consumes the value the caller pushed just before.
GetLocCheck => {
let slot = self.slot_operand(op, a);
let value = self.get_loc(slot);
if matches!(value, Value::Uninitialized) {
return Err(self.dead_zone(slot, VarSlot::Loc));
}
self.push(value);
}
PutLocCheck | SetLocCheck => {
let slot = self.slot_operand(op, a);
let value = self.pop();
if matches!(value, Value::Uninitialized) {
return Err(self.dead_zone(slot, VarSlot::Loc));
}
// The initialiser goes through `put_loc`, not this, so any store that
// reaches here is a later assignment — which a `const` refuses.
if self.is_read_only(slot, VarSlot::Loc) {
return Err(self.read_only(slot, VarSlot::Loc));
}
if op == SetLocCheck {
self.put_loc(slot, value.clone());
self.push(value);
} else {
self.put_loc(slot, value);
}
}
PutLocCheckInit => {
// The one store that *initialises* a lexical slot: `this` in a derived
// constructor, which only exists after `super()` has run. A second
// initialisation is the error the reference reports as
// "'this' can be initialized only once".
let slot = self.slot_operand(op, a);
let value = self.pop();
if !matches!(self.get_loc(slot), Value::Uninitialized) {
return Err(Signal::Throw(self.make_error(
"ReferenceError",
"'this' can be initialized only once",
)));
}
self.put_loc(slot, value);
}
CheckCtor => {
// A class constructor may only run under `new`, which is what makes
// `class A {}; A()` a TypeError.
if matches!(self.frame().new_target, Value::Undefined) {
return Err(Signal::Throw(self.make_error(
"TypeError",
"class constructors must be invoked with 'new'",
)));
}
}
InitCtor => {
// The body a derived class gets when it declares no constructor:
// forward every argument to the parent constructor, keeping `new.target`.
if matches!(self.frame().new_target, Value::Undefined) {
return Err(Signal::Throw(self.make_error(
"TypeError",
"class constructors must be invoked with 'new'",
)));
}
let Some(callee) = self.current_callee() else {
return Err(Signal::Throw(
self.make_error("TypeError", "no constructor to inherit from"),
));
};
let parent = callee.proto().map_or(Value::Null, Value::Object);
let new_target = self.frame().new_target.clone();
let args = self.frame().passed.clone();
let result = self.construct(parent, new_target, args)?;
self.push(result);
}
SetLocUninitialized => {
let slot = self.slot_operand(op, a);
self.put_loc(slot, Value::Uninitialized);
}
CloseLoc => {
// "Close" a binding: give it a *fresh* cell holding the current
// value. Closures created before this point keep the old cell and the
// ones created after see the new one, which is what makes every
// iteration of `for (let i ...)` capture its own `i`, and what freezes
// a block-scoped binding when the block ends.
//
// Clearing the slot instead (which this used to do) broke both: a
// closure over a captured block variable read `undefined`, and a loop
// terminated after one iteration.
let slot = self.slot_operand(op, a);
let index = self.frame().code.loc_cell.get(slot).copied().unwrap_or(-1);
if index >= 0 {
let current = self.frame().cells[index as usize].get();
let fresh = self.new_cell(current);
if let Some(cell) = self.frame_mut().cells.get_mut(index as usize) {
*cell = fresh;
}
}
}
GetArg | GetArg0 | GetArg1 | GetArg2 | GetArg3 => {
let slot = self.slot_operand(op, a);
let value = self.get_arg(slot);
self.push(value);
}
PutArg | PutArg0 | PutArg1 | PutArg2 | PutArg3 => {
let slot = self.slot_operand(op, a);
let value = self.pop();
self.put_arg(slot, value);
}
SetArg | SetArg0 | SetArg1 | SetArg2 | SetArg3 => {
let slot = self.slot_operand(op, a);
let value = self.pop();
self.put_arg(slot, value.clone());
self.push(value);
}
// -- closure variables ------------------------------------------
GetVarRef | GetVarRef0 | GetVarRef1 | GetVarRef2 | GetVarRef3 => {
let slot = self.slot_operand(op, a);
// A name a direct `eval` created shadows the binding the compiler chose.
// `function t(){ var c = "outer"; function f(a = eval("var c = 1")) { } }`
// leaves `t`'s `c` alone but makes every read inside `f` see the eval's,
// and only a runtime lookup can know that.
if let Some(cell) = self.eval_created_local(slot) {
self.push(cell.get());
return self.step();
}
let value = self.cell_value(slot)?;
self.push(value);
}
GetVarRefCheck => {
let slot = self.slot_operand(op, a);
let value = self.cell_value(slot)?;
if matches!(value, Value::Uninitialized) {
return Err(self.dead_zone(slot, VarSlot::Closure));
}
self.push(value);
}
PutVarRef | PutVarRef0 | PutVarRef1 | PutVarRef2 | PutVarRef3 | PutVarRefCheck => {
let slot = self.slot_operand(op, a);
let value = self.pop();
if let Some(cell) = self.eval_created_local(slot) {
cell.set(value);
return self.step();
}
// Same rule as `PutLocCheck`: a checked store to a captured binding
// refuses a `const` once its initialiser has run.
if op == PutVarRefCheck && self.is_read_only(slot, VarSlot::Closure) {
return Err(self.read_only(slot, VarSlot::Closure));
}
self.set_cell(slot, value)?;
}
SetVarRef | SetVarRef0 | SetVarRef1 | SetVarRef2 | SetVarRef3 => {
let slot = self.slot_operand(op, a);
let value = self.pop();
if let Some(cell) = self.eval_created_local(slot) {
cell.set(value.clone());
self.push(value);
return self.step();
}
self.set_cell(slot, value.clone())?;
self.push(value);
}
// -- globals ----------------------------------------------------
GetVar => {
let value = self.get_global(a as usize)?;
self.push(value);
}
GetVarUndef => {
let value = self
.get_global(a as usize)
.or_else(|_| Ok(Value::Undefined))?;
self.push(value);
}
DeleteVar => {
// `delete name` for a name that is not a declared binding. A lexical
// global lives in the reference table and cannot be deleted; anything
// reachable only as a property of the global object is *implicit* —
// `x = 1` with no declaration — and goes away if it is configurable,
// which is what `delete` on an implicit global answers.
let name = self.atom_text(a as usize);
let global = self.realm.global.clone();
let deleted = if self.realm.globals.contains_key(&name) {
false
} else if global.own_property(&name).is_some() {
self.delete_property(&global, &name)?
} else {
true
};
self.push(Value::Bool(deleted));
}
PutVar => {
let value = self.pop();
self.put_global(a as usize, value)?;
}
PutVarInit => {
let slot = a as usize;
let value = self.pop();
self.init_global(slot, value)?;
}
// -- arithmetic -------------------------------------------------
Add => self.binary_add()?,
Sub | Mul | Div | Mod | Pow | And | Or | Xor | Shl | Sar | Shr => {
let raw_right = self.pop();
let raw_left = self.pop();
// BigInt arithmetic is a separate path, and not only for the type of the
// result: `ToNumber` of a bigint throws, so mixing the two must be an error
// rather than a silent conversion.
if matches!(raw_left, Value::BigInt(_)) || matches!(raw_right, Value::BigInt(_)) {
let left = self.primitive_of(raw_left)?;
let right = self.primitive_of(raw_right)?;
let value = self.bigint_arithmetic(op, left, right)?;
self.push(value);
} else {
let right = self.numeric(raw_right)?;
let left = self.numeric(raw_left)?;
self.push(arithmetic(op, left, right)?);
}
}
Neg => {
let raw = self.pop();
match raw {
// A bigint negates exactly, however wide it is.
Value::BigInt(big) => {
let negated = big.negate();
self.push(self.bigint(negated));
}
other => {
let value = self.numeric(other)?;
self.push(negate(value));
}
}
}
Plus => {
let value = self.pop();
// `+x` is `ToNumber`, and `ToNumber` refuses a bigint: unlike `++`,
// unary plus is defined to produce a *number*, which a bigint cannot
// become without loss. It used to fall through to `NaN`.
if matches!(value, Value::BigInt(_)) {
return Err(self.type_error("cannot convert bigint to number"));
}
let numeric = self.numeric_of(value)?;
self.push(numeric);
}
Inc => {
let raw = self.pop();
let (_, updated) = self.advance_numeric(raw, true)?;
self.push(updated);
}
Dec => {
let raw = self.pop();
let (_, updated) = self.advance_numeric(raw, false)?;
self.push(updated);
}
PostInc | PostDec => {
// `x -> x x+1`: the *new* value ends up on top, so the store the
// compiler emits right after (`put_var`) writes the incremented
// one, while the old value stays underneath for the expression's
// own result. Getting this backwards makes `i++` a no-op, which
// turns `while (i < 3)` into an infinite loop.
let raw = self.pop();
let (old, updated) = self.advance_numeric(raw, op == PostInc)?;
self.push(old);
self.push(updated);
}
Not => {
let raw = self.pop();
match raw {
// `~x` on a bigint is `-x - 1`, exact at any width.
Value::BigInt(big) => {
let inverted = big.bitnot();
self.push(self.bigint(inverted));
}
other => {
let value = self.numeric(other)?;
self.push(Value::Int(!value.to_int32()));
}
}
}
Lnot => {
let value = self.pop();
self.push(Value::Bool(!value.to_boolean()));
}
// -- comparison -------------------------------------------------
Lt | Lte | Gt | Gte => {
let right = self.pop();
let left = self.pop();
let answer = self.relational(op, left, right);
self.push(answer);
}
Eq | Neq => {
let right = self.pop();
let left = self.pop();
let equal = self.abstract_equals(left, right)?;
self.push(Value::Bool(if op == Eq { equal } else { !equal }));
}
StrictEq => {
let right = self.pop();
let left = self.pop();
self.push(Value::Bool(left.strict_equals(&right)));
}
StrictNeq => {
let right = self.pop();
let left = self.pop();
self.push(Value::Bool(!left.strict_equals(&right)));
}
IsUndefined => {
// `=== undefined`, which is what a destructuring default tests. It is
// not `IsUndefinedOrNull`: `[a = 1] = [null]` keeps `null`.
let value = self.pop();
self.push(Value::Bool(matches!(value, Value::Undefined)));
}
IsUndefinedOrNull => {
let value = self.pop();
self.push(Value::Bool(value.is_nullish()));
}
Typeof => {
let value = self.pop();
self.push(self.string(value.type_of()));
}
ToObject => {
// A destructuring *object* pattern starts here, which is why
// `var {} = null` throws. Only `undefined` and `null` are refused; a
// primitive is boxed.
let value = self.pop();
let object = self.object_from(&value)?;
self.push(Value::Object(object));
}
Instanceof => {
let constructor = self.pop();
let object = self.pop();
let answer = self.instance_of(&object, &constructor)?;
self.push(Value::Bool(answer));
}
In => {
let object = self.pop();
let key = self.pop();
let answer = self.has_property(&key, &object)?;
self.push(Value::Bool(answer));
}
// -- control flow -----------------------------------------------
Goto | Goto8 | Goto16 => {
self.frame_mut().pc = a as usize;
}
IfTrue | IfTrue8 => {
let value = self.pop();
if value.to_boolean() {
self.frame_mut().pc = a as usize;
}
}
IfFalse | IfFalse8 => {
let value = self.pop();
if !value.to_boolean() {
self.frame_mut().pc = a as usize;
}
}
Catch => {
// Pushes the handler target onto the value stack; `unwind` finds it.
self.push(Value::CatchOffset(a));
}
Gosub => {
self.push(Value::ReturnAddress(next as u32));
self.frame_mut().pc = a as usize;
}
Ret => {
let value = self.pop();
match value {
Value::ReturnAddress(address) => self.frame_mut().pc = address as usize,
_ => {
return Err(Signal::Throw(
self.make_error("InternalError", "invalid return address"),
));
}
}
}
NipCatch => {
// `catch_offset ... ret_val -> ret_val`: drop everything down to and
// including the marker, keeping the top value.
let top = self.pop();
loop {
match self.pop() {
Value::CatchOffset(_) | Value::Undefined => break,
_ => {}
}
if self.frame().stack.is_empty() {
break;
}
}
self.push(top);
}
Throw => {
let value = self.pop();
return Err(Signal::Throw(value));
}
ThrowError => {
let name = self.atom_text(a as usize);
let (class, message) = match b {
0 => ("TypeError", format!("'{name}' is read-only")),
1 => ("SyntaxError", format!("redeclaration of '{name}'")),
2 => ("ReferenceError", format!("'{name}' is not initialized")),
// `delete super.x` is a syntax-level mistake the reference reports
// with this wording, not a property-access failure.
3 => (
"ReferenceError",
"unsupported reference to 'super'".to_string(),
),
4 => (
"TypeError",
"iterator does not have a throw method".to_string(),
),
5 => (
"ReferenceError",
"invalid assignment left-hand side".to_string(),
),
_ => ("TypeError", format!("invalid operation on '{name}'")),
};
return Err(Signal::Throw(self.make_error(class, &message)));
}
InitialYield => return Ok(Step::Suspend(SuspendKind::Initial)),
Yield => return Ok(Step::Suspend(SuspendKind::Yield)),
YieldStar => return Ok(Step::Suspend(SuspendKind::YieldStar)),
AsyncYieldStar => return Ok(Step::Suspend(SuspendKind::AsyncYieldStar)),
Await => return Ok(Step::Suspend(SuspendKind::Await)),
ReturnAsync => {
// The reference's `return_async` returns `undefined` and lets the resume
// machinery pick the real value off `sp[-1]`, which is how it tells a
// completion from a suspension. Carrying the value explicitly says the
// same thing without the implicit stack slot.
let value = self.pop();
return Ok(Step::Return(value));
}
Return => {
let value = self.pop();
return Ok(Step::Return(value));
}
ReturnUndef => return Ok(Step::Return(Value::Undefined)),
// -- calls ------------------------------------------------------
Call | Call0 | Call1 | Call2 | Call3 => {
let argc = self.argc_operand(op, a);
let args = self.pop_n(argc);
let callee = self.pop();
// A plain call has no receiver; the callee decides what to do with
// `undefined` (a sloppy function substitutes the global object).
if let Some(result) = self.call_or_push(callee, Value::Undefined, args)? {
self.push(result);
}
}
CallConstructor => {
// `constructor this_obj args… -> object`. The duplicated
// constructor is `new.target`; this milestone does not expose it,
// so the extra copy is simply consumed here.
let argc = a as usize;
let args = self.pop_n(argc);
let new_target = self.pop();
let callee = self.pop();
let result = self.construct(callee, new_target, args)?;
self.push(result);
}
CallMethod => {
let argc = a as usize;
let args = self.pop_n(argc);
let callee = self.pop();
let this = self.pop();
if let Some(result) = self.call_or_push(callee, this, args)? {
self.push(result);
}
}
Apply => {
// `func this array -> result`, the spread-call form: the compiler
// builds one array of arguments, and all that is left here is to
// unpack it. Magic 1 is `new` (`apply 1`), which constructs.
let array = self.pop();
let this = self.pop();
let callee = self.pop();
let args = self.array_like_to_args(&array)?;
if a & 1 != 0 {
let result = self.construct(callee, this, args)?;
self.push(result);
} else if let Some(result) = self.call_or_push(callee, this, args)? {
self.push(result);
}
}
TailCall | TailCallMethod => {
// `tail_call` is `call` whose result is returned immediately — the
// reference jumps straight to `done` instead of storing the value and
// running on. The callee still gets its own frame, so deep tail
// recursion hits the stack limit exactly like plain recursion does;
// marking this frame lets the return path unwind without needing
// instructions after the call.
let argc = if op == TailCallMethod {
a as usize
} else {
self.argc_operand(op, a)
};
let args = self.pop_n(argc);
let callee = self.pop();
let this = if op == TailCallMethod {
self.pop()
} else {
Value::Undefined
};
if let Value::Object(function) = &callee
&& let Some((code, env)) = bytecode_of(function)
{
// A generator or async body is not run here at all: calling it only
// *creates* the generator/coroutine, so it must not inherit this
// frame. Doing so made `function f(){ return g() }` push `g`'s body
// onto `f`'s frame, and the first `yield`/`await` then reported
// "a generator body ran as an ordinary call".
if code.code.kind.is_generator() || code.code.kind.is_async() {
let value = self.call_value(callee, this, args)?;
return Ok(Step::Return(value));
}
self.frame_mut().tail = true;
self.push_frame(code, env, this, args, callee)?;
return Ok(Step::Continue);
}
// A native callee has no frame to reuse, so its value is simply
// returned.
let value = self.call_value(callee, this, args)?;
return Ok(Step::Return(value));
}
// -- objects ----------------------------------------------------
SpecialObject => {
let value = self.special_object(a)?;
self.push(value);
}
PrivateSymbol => {
// `private_symbol "#x"`: the key a class's `#x` property lives under. A fresh
// symbol each time it is evaluated, which is why the compiler evaluates it
// once (at class definition) and stores it in a class-scope binding.
let name = self.atom_text(a as usize);
let symbol = Gc::new(self.heap, crate::value::JsSymbol::new_private(&name));
self.push(Value::Symbol(symbol));
}
GetPrivateField => {
// `obj symbol -> value`. A missing private field is a TypeError, which is what
// makes `#x` inaccessible on any object not built by this class.
let symbol = self.pop();
let object = self.pop();
let value = self.read_private_field(&object, &symbol)?;
self.push(value);
}
PutPrivateField => {
// `obj value symbol ->`, and the store is a plain assignment to a property the
// class already created, so there is no setter to consult.
let symbol = self.pop();
let value = self.pop();
let object = self.pop();
self.write_private_field(&object, &symbol, value)?;
}
DefinePrivateField => {
// `obj symbol value -> obj`: a field initialiser creates the property, and
// redefining one is a TypeError ("already exists").
let value = self.pop();
let symbol = self.pop();
let object = self.pop();
self.define_private_field(&object, &symbol, value)?;
self.push(object);
}
PrivateIn => {
// `obj key -> bool`: `#x in o`. The second operand decides which question is
// being asked, which is the reference's `js_operator_private_in`: a *symbol*
// is a field, so this is an own-property test; a *function* is a method or
// accessor, so this is a brand check.
let key = self.pop();
let object = self.pop();
let Some(target) = object.as_object() else {
return Err(Signal::Throw(
self.make_error("TypeError", "invalid 'in' operand"),
));
};
let present = match &key {
Value::Object(_) => self.check_brand(target, &key)?,
_ => target.own_property_key(&self.private_key(&key)?).is_some(),
};
self.push(Value::Bool(present));
}
AddBrand => {
// `obj home ->`: create the class brand on `home` if it has none, then give
// `obj` that brand. `obj` may be `null`, which is the instance-brand case at
// class definition: only the prototype's copy of the symbol is wanted.
let home = self.pop();
let object = self.pop();
self.add_brand(&object, &home)?;
}
CheckBrand => {
// `obj func -> obj func`: does `obj` carry the brand of `func`'s home object?
// Both stay, because the caller needs them — `nip` halves a read into its
// value and `call_method` uses the pair as receiver and callee.
let function = self.peek(0);
let object = self.peek(1);
let Some(target) = object.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
if !self.check_brand(target, &function)? {
return Err(Signal::Throw(
self.make_error("TypeError", "invalid brand on object"),
));
}
}
GetLength => {
// `get_length` is the `.length` read; an array and a string answer
// from their own length, anything else from the property.
let target = self.pop();
let value = match &target {
Value::Object(object) => match object.array_length() {
Some(length) => crate::object::length_value(length),
None => self.read_field(&target, "length")?,
},
Value::String(text) => Value::Int(text.utf16_len() as i32),
_ => self.read_field(&target, "length")?,
};
self.push(value);
}
GetField | GetField2 => {
let object = self.pop();
let key = self.atom_key(a as usize);
let value = self.read_field_cached(&object, &key, next)?;
if op == GetField2 {
self.push(object);
}
self.push(value);
}
GetArrayEl | GetArrayEl2 | GetArrayEl3 => {
let key = self.pop();
let object = self.pop();
let key = self.propkey(&key)?;
let value = self.read_field_key(&object, &key)?;
if matches!(op, GetArrayEl2 | GetArrayEl3) {
self.push(object);
}
if op == GetArrayEl3 {
// The key goes back in its *value* form, which the compiler stores
// into the target of a destructuring assignment.
self.push(match &key {
PropertyKey::Symbol(symbol) => Value::Symbol(symbol.clone()),
PropertyKey::String(text) => self.string_wtf8(text.clone()),
});
}
self.push(value);
}
PutField => {
let value = self.pop();
let object = self.pop();
let key = self.atom_key(a as usize);
self.write_field_key(&object, &key, value)?;
}
PutArrayEl => {
let value = self.pop();
let key = self.pop();
let object = self.pop();
let key = self.propkey(&key)?;
self.write_field_key(&object, &key, value)?;
}
DefineField => {
let value = self.pop();
let object = self.pop();
let key = self.atom_text(a as usize);
// No naming here. `{m: function(){}}` is named by the *compiler*, which
// emits `set_name` — and only where `NamedEvaluation` applies, so
// `{m: (0, function(){})}` stays anonymous. Naming every function that
// reaches a property definition got that case wrong, and the compiler is
// the only place that can tell the two apart.
if let Some(target) = object.as_object() {
// Only an *array* stores into the element vector. A numeric key
// on an ordinary object is an ordinary property, which is what
// makes `{1: 'a'}` iterate as a key rather than disappear.
let stored = target.is_array()
&& array_index(&key).is_some_and(|index| {
target.array_set(self.heap, index as usize, value.clone())
});
if !stored {
target.define_data(&key, value);
}
}
self.push(object);
}
DefineArrayEl => {
// `obj key value -> obj key`. The *value* form of the key is what goes
// back on the stack — the compiler stores it into the pattern's binding —
// so the symbol is not stringified on the way through.
let value = self.pop();
let key_value = self.pop();
let object = self.peek(0);
let key = self.propkey(&key_value)?;
self.write_field_key(&object, &key, value)?;
self.push(key_value);
}
SetHomeObject => {
// `obj func -> obj func`: records the object a method was defined on so
// `super` inside it resolves against that object's prototype.
let function = self.peek(0);
let home = self.peek(1);
if let (Value::Object(function), Value::Object(home)) = (&function, &home) {
function.set_home_object(Some(home.clone()));
}
}
Regexp => {
// `pattern flags -> regexp`. A literal compiles here rather than through the
// constructor, which is both what the reference's dedicated opcode is for
// and what keeps `/a/g.source` from being affected by a patched `RegExp`.
let flags = self.pop();
let pattern = self.pop();
let pattern = self.to_js_string(&pattern)?;
let flags = self.to_js_string(&flags)?;
let object = crate::builtins::regexp::new_regexp(self, &pattern, &flags)?;
self.push(Value::Object(object));
}
UsingDisposeInit => {
// The error state a disposal accumulates into. Uninitialised means "no
// failure yet"; anything else is the exception being propagated.
self.push(Value::Uninitialized);
}
UsingCheck => {
// `value -> value, method`: validate and snapshot the disposer now, so a
// later change to `Symbol.dispose` cannot change what gets called, and so a
// value that is not disposable fails before the body runs.
let value = self.pop();
let method = self.using_method_for(&value, a as u8 == 1)?;
self.push(value);
self.push(method);
}
UsingDispose => {
// `error_state -> error_state'`: dispose the variable at `slot` — its value
// is `slot`, its snapshotted method `slot + 1` — folding any failure into
// the pending error.
let slot = self.slot_operand(op, a);
let error_state = self.pop();
let merged = self.using_dispose_once(slot, error_state)?;
self.push(merged);
}
UsingDisposeMerge => {
let new_error = self.pop();
let error_state = self.pop();
let merged = self.merge_dispose_error(new_error, error_state);
self.push(merged);
}
UsingDisposeEnd => {
// A disposal that failed replaces the block's normal completion, so the
// error state becomes a real throw here rather than at the call site.
let error_state = self.pop();
if !matches!(error_state, Value::Uninitialized) {
return Err(Signal::Throw(error_state));
}
}
UsingDisposeAsync => {
// `await using` needs a promise to await and this runtime has no
// `Promise` builtin, so the disposer is called synchronously and its result
// discarded. That is correct for a disposer that returns nothing and is
// reported as a gap rather than silently skipped.
let slot = self.slot_operand(op, a);
let error_state = self.using_dispose_once(slot, Value::Uninitialized)?;
self.push(error_state);
}
GetSuper => {
// `obj -> obj's prototype`: the starting point of a `super` lookup.
let object = self.pop();
let proto = match &object {
Value::Object(object) => object.proto().map_or(Value::Null, Value::Object),
_ => Value::Null,
};
self.push(proto);
}
GetSuperValue => {
// `receiver proto key -> value`, searching from `proto` but reporting to
// `receiver`, which is what `super.g` must do for a getter to see `this`.
//
// Three values in, one out: the receiver is *consumed*, not left under the
// result. Leaving it there was invisible for `super.x` in `return` or
// statement position (the result is what gets popped) and corrupted the
// frame everywhere else — `print(super.x)` left the class where the call
// opcode looks for the receiver and called the class itself.
let key = self.pop();
let proto = self.pop();
let receiver = self.pop();
let value = self.super_read(&receiver, &proto, &key)?;
self.push(value);
}
PutSuperValue => {
// `receiver proto key value ->`: a store that starts at `proto` but
// assigns to `receiver`.
let value = self.pop();
let key = self.pop();
let proto = self.pop();
let receiver = self.pop();
self.super_write(&receiver, &proto, &key, value)?;
}
DefineClass => {
// `parent ctor -> ctor proto`: the constructor becomes the class and a
// fresh prototype object lands on top for the methods to go on.
let name = self.atom_text(a as usize);
let (ctor, proto) = self.define_class(&name, b)?;
self.push(ctor);
self.push(Value::Object(proto));
}
DefineClassComputed => {
// `parent ctor key -> ctor proto key`: the computed name stays put so
// `define_field`-style consumers can still read it.
let key = self.pop();
let name = self.propkey_of(&key)?.to_string_lossy().into_owned();
let (ctor, proto) = self.define_class(&name, b)?;
self.push(ctor);
self.push(Value::Object(proto));
self.push(key);
}
DefineMethod => {
let function = self.pop();
let object = self.pop();
let key = self.atom_text(a as usize);
// A method's `[[HomeObject]]` is the object it was defined on, which is
// what `super` inside it resolves against.
if let (Value::Object(function), Value::Object(object)) = (&function, &object) {
function.set_home_object(Some(object.clone()));
}
self.set_function_name(&function, &key_with_accessor_prefix(&key, b));
if let Some(array) = object.as_object() {
define_function_property(array, &PropertyKey::from(key.as_str()), function, b);
}
self.push(object);
}
DefineMethodComputed => {
let function = self.pop();
let key = self.pop();
let object = self.pop();
let key = self.propkey(&key)?;
self.set_function_name(&function, &key_with_accessor_prefix(&key.to_string(), a));
if let Some(array) = object.as_object() {
define_function_property(array, &key, function, a);
}
self.push(object);
}
SetProto => {
let proto = self.pop();
let object = self.pop();
if let Some(target) = object.as_object() {
target.set_proto(proto.as_object().cloned());
}
self.push(object);
}
CopyDataProperties => {
let exclusion = self.pop();
let source = self.pop();
let target = self.peek(0);
self.copy_properties(&target, &source, &exclusion)?;
self.push(source);
self.push(exclusion);
}
ToPropkey => {
// A symbol survives unchanged: this is `ToPropertyKey`, not `ToString`,
// and stringifying here would turn `{[Symbol("b")]: 2}` into an
// ordinary `"Symbol(b)"` property.
let value = self.pop();
let key = match self.propkey(&value)? {
PropertyKey::Symbol(symbol) => Value::Symbol(symbol),
PropertyKey::String(text) => self.string_wtf8(text),
};
self.push(key);
}
SetName => {
let function = self.peek(0);
let name = self.atom_text(a as usize);
self.set_function_name(&function, &name);
}
SetNameComputed => {
let key = self.peek(1);
let function = self.peek(0);
let name = self.propkey_of(&key)?.to_string_lossy().into_owned();
self.set_function_name(&function, &name);
}
// -- arrays -----------------------------------------------------
ArrayFrom => {
let count = a as usize;
let elements = self.pop_n(count);
let array = self.new_array(elements);
self.push(Value::Object(array));
}
Append => {
// `array index iterable -> array new_index`
let iterable = self.pop();
self.pop();
let array = self.peek(0);
let added = self.append_enumerate(&array, &iterable)?;
self.push(Value::Int(added));
}
// -- iteration --------------------------------------------------
ForInStart => {
let value = self.pop();
let record = self.for_in_record(&value)?;
self.push(Value::Object(record));
}
ForInNext => {
// `record -> record key done`. The flag is *done*, not "found":
// the compiler tests it with `if_false` to jump back into the body,
// so reporting `true` while keys remain would end the loop at once.
let record = self.peek(0);
match self.for_in_advance(&record)? {
Some(key) => {
let text = self.string(key);
self.push(text);
self.push(Value::Bool(false));
}
None => {
self.push(Value::Undefined);
self.push(Value::Bool(true));
}
}
}
ForOfStart => {
let value = self.pop();
let iterator = self.get_iterator(&value)?;
self.push(iterator.object);
self.push(iterator.next);
self.push(Value::CatchOffset(0));
}
ForOfNext => {
// `iter next catch_offset -> iter next catch_offset value done`.
//
// The operand is how many values sit *above* the iterator triple: a
// destructuring rest element keeps its target array and running index
// there while draining the iterator, so the triple is not at the top.
let extra = a as usize;
let iterator_slot = 2 + extra;
// An exhausted iterator is replaced by `undefined` (see below), so a
// second step must not call `next` again — `Array.from` and a
// destructuring pattern both keep stepping past the end.
let (value, done) = if matches!(self.peek(iterator_slot), Value::Undefined) {
(Value::Undefined, true)
} else {
let iterator = Iterator {
object: self.peek(iterator_slot),
next: self.peek(1 + extra),
};
self.iterator_step(&iterator)?
};
if done {
// The result's `value` is meaningless once the iterator is done:
// `function* g(){ return 0 } var [x] = g()` leaves `x` undefined
// rather than binding the generator's return value.
let top = self.frame().stack.len() - 1 - iterator_slot;
self.frame_mut().stack[top] = Value::Undefined;
self.push(Value::Undefined);
self.push(Value::Bool(true));
} else {
self.push(value);
self.push(Value::Bool(false));
}
}
IteratorNext => {
// `iter next catch val -> iter next catch val'`: replace the argument with
// the result of `next(val)`. The triple below stays put, which is why the
// three instructions of the `yield*` loop all address the stack the same way.
let value = self.pop();
let object = self.peek(2);
let next = self.peek(1);
let result = self.call_value(next, object, vec![value])?;
self.push(result);
}
IteratorCall => {
// `iter next catch val -> iter next catch val' flag`.
//
// Bit 0 picks `.throw` over `.return`, bit 1 calls it with no argument.
// The flag is `true` when the method was missing, which is how the
// caller learns that the inner iterator cannot take part in the
// unwinding.
let value = self.pop();
let object = self.peek(2);
let key = if a & 1 != 0 { "throw" } else { "return" };
let method = self.read_field(&object, key)?;
if matches!(method, Value::Undefined | Value::Null) {
self.push(value);
self.push(Value::Bool(true));
} else {
let argument = if a & 2 != 0 { Vec::new() } else { vec![value] };
let result = self.call_value(method, object, argument)?;
self.push(result);
self.push(Value::Bool(false));
}
}
IteratorCheckObject => {
if !matches!(self.peek(0), Value::Object(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "iterator must return an object"),
));
}
}
IteratorClose => {
// `iter_obj next catch_offset ->`: abandon a `for of` loop, which means
// calling the iterator's `return` so it can release whatever it holds.
//
// The catch offset goes first, so an exception from `return` is not caught
// by the loop being abandoned. This used to pop the three operands and call
// nothing at all, which made `break` out of a `for of` invisible to an
// iterator with a `return` method.
self.pop();
self.pop();
let iterator = self.pop();
if !matches!(iterator, Value::Undefined) {
let method = self.read_field(&iterator, "return")?;
if !matches!(method, Value::Undefined | Value::Null) {
let result = self.call_value(method, iterator.clone(), Vec::new())?;
// §7.4.6: a `return` that answers a non-object is a TypeError.
if !matches!(result, Value::Object(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "iterator must return an object"),
));
}
}
}
}
Import => {
// `import(specifier)`: the options slot is on top and is not read yet.
let _options = self.pop();
let specifier = self.pop();
let promise = self.dynamic_import(specifier)?;
self.push(promise);
}
ForAwaitOfStart => {
// Same triple as `for of`, but from `Symbol.asyncIterator` with a
// `Symbol.iterator` fallback.
let value = self.pop();
let iterator = self.get_async_iterator(&value)?;
self.push(iterator.object);
self.push(iterator.next);
self.push(Value::CatchOffset(0));
}
ForAwaitOfNext => {
// `for await` steps the iterator and leaves the *result* wrapped in a
// promise, so the `await` the compiler emits right after this can suspend
// on it. Unwrapping happens in `iterator_get_value_done`.
let extra = a as usize;
let iterator_slot = 2 + extra;
let result = if matches!(self.peek(iterator_slot), Value::Undefined) {
Value::Undefined
} else {
let iterator = Iterator {
object: self.peek(iterator_slot),
next: self.peek(1 + extra),
};
let next = iterator.next.clone();
self.call_value(next, iterator.object, Vec::new())?
};
let promise = self.promise_resolve_value(result)?;
self.push(promise);
}
IteratorGetValueDone => {
// `result -> value done`. A missing result object means the iterator was
// already exhausted, which the loop treats as `done`.
let result = self.pop();
let done = match &result {
Value::Object(object) => self
.get_property(object, "done")?
.is_some_and(|value| value.to_boolean()),
_ => true,
};
if done {
// Clear the iterator object so the `iterator_close` the loop emits on
// its way out has nothing to close: `AsyncIteratorClose` must *not* run
// on normal completion, and this is where a normal completion is
// recognised — the same trick `for_of_next` uses, which also replaces
// the slot with `undefined`.
//
// The layout is fixed because only `for await` emits this opcode:
// `iter next catch [result]`, so the iterator is three below the top.
let depth = self.frame().stack.len();
if depth >= 3 {
self.frame_mut().stack[depth - 3] = Value::Undefined;
}
self.push(Value::Undefined);
self.push(Value::Bool(true));
} else {
let Value::Object(object) = &result else {
self.push(Value::Undefined);
self.push(Value::Bool(true));
return Ok(Step::Continue);
};
let value = self
.get_property(object, "value")?
.unwrap_or(Value::Undefined);
self.push(value);
self.push(Value::Bool(false));
}
}
Delete => {
// `delete obj[key]`, where the key was just pushed. Deleting an array
// index leaves a *hole* rather than closing the gap, and a
// non-configurable property answers `false` and stays.
let key = self.pop();
let target = self.pop();
let key = self.propkey(&key)?;
let text_label = key.as_str().unwrap_or("").to_string();
let deleted = match &target {
Value::Object(object) => {
let object = object.clone();
self.delete_property_key(&object, &key)?
}
// A string's `length` and its index properties are
// non-configurable, so deleting one answers `false`.
Value::String(text) => {
!(text_label == "length"
|| array_index(&text_label)
.is_some_and(|index| (index as usize) < text.utf16_len()))
}
// "delete null.a" is a TypeError, not a no-op: the operator needs an
// object to delete from, and only a *boxable* primitive has one.
Value::Undefined | Value::Null => {
return Err(Signal::Throw(
self.make_error("TypeError", "cannot convert to object"),
));
}
// Any other primitive has no own properties; deleting nothing
// succeeds.
_ => true,
};
self.push(Value::Bool(deleted));
}
// -- exceptions and bookkeeping ---------------------------------
Fclosure | Fclosure8 => {
let value = self.closure(a as usize)?;
self.push(value);
}
other => {
let name = other.info().name;
return Err(Signal::Throw(self.make_error(
"InternalError",
&format!("{name} is not implemented yet"),
)));
}
}
Ok(Step::Continue)
}
// -- operand helpers ----------------------------------------------------
/// The slot index an operand names, taking short forms into account.
fn slot_operand(&self, op: Opcode, a: u32) -> usize {
use Opcode::*;
match op {
GetLoc0 | PutLoc0 | SetLoc0 | GetArg0 | PutArg0 | SetArg0 | GetVarRef0 | PutVarRef0
| SetVarRef0 => 0,
GetLoc1 | PutLoc1 | SetLoc1 | GetArg1 | PutArg1 | SetArg1 | GetVarRef1 | PutVarRef1
| SetVarRef1 => 1,
GetLoc2 | PutLoc2 | SetLoc2 | GetArg2 | PutArg2 | SetArg2 | GetVarRef2 | PutVarRef2
| SetVarRef2 => 2,
GetLoc3 | PutLoc3 | SetLoc3 | GetArg3 | PutArg3 | SetArg3 | GetVarRef3 | PutVarRef3
| SetVarRef3 => 3,
_ => a as usize,
}
}
/// The argument count of a call, taking `call0`…`call3` into account.
fn argc_operand(&self, op: Opcode, a: u32) -> usize {
use Opcode::*;
match op {
Call0 => 0,
Call1 => 1,
Call2 => 2,
Call3 => 3,
_ => a as usize,
}
}
/// The atom's name as a property key.
///
/// A `PropertyKey` rather than a `String` because that is what every caller needs, and
/// because `Wtf8` is reference-counted: building the key is a refcount bump, while
/// `String::clone` was a heap allocation and a copy — per property access.
fn atom_key(&self, index: usize) -> PropertyKey {
self.frame()
.code
.code
.atoms
.get(index)
.map(|atom| match atom {
Atom::Str(text) => PropertyKey::String(crate::value::Wtf8::from_rc_str(text)),
Atom::Index(value) => {
PropertyKey::String(crate::value::Wtf8::new(value.to_string()))
}
// `JS_ATOM_NULL`: no name. The opcodes that take one (the generic
// `throw_error`, `delete super.x`) never read it.
Atom::Null => PropertyKey::String(crate::value::Wtf8::new(String::new())),
})
.unwrap_or_else(|| PropertyKey::String(crate::value::Wtf8::new(String::new())))
}
fn atom_text(&self, index: usize) -> String {
self.frame()
.code
.code
.atoms
.get(index)
.map(|atom| match atom {
Atom::Str(text) => text.clone(),
Atom::Index(value) => value.to_string(),
// `JS_ATOM_NULL`: no name. The opcodes that take one (the generic
// `throw_error`, `delete super.x`) never read it.
Atom::Null => String::new(),
})
.unwrap_or_default()
}
fn constant(&mut self, index: usize) -> JsResult<Value> {
let constant = self.frame().code.code.cpool.get(index).cloned();
Ok(match &constant {
Some(Constant::Number(number)) => Value::Float(*number),
Some(Constant::String(text)) => self.string(text.clone()),
Some(Constant::BigInt { digits, radix }) => {
match crate::bigint::BigInt::from_radix(digits, *radix) {
Some(big) => self.bigint(big),
// A literal the parser accepted is always parseable; a failure here
// would be a compiler bug, so it reads as an `InternalError`.
None => {
return Err(Signal::Throw(
self.make_error("InternalError", "unparseable bigint literal"),
));
}
}
}
Some(Constant::Function(_)) => {
return Err(Signal::Throw(
self.make_error("InternalError", "a bare function constant"),
));
}
// A tagged template's call site: a frozen array of the cooked chunks with a
// frozen `raw` array beside it, which is what `String.raw` reads back.
Some(Constant::Template { cooked, raw }) => {
let cooked_values: Vec<Value> = cooked
.iter()
.map(|chunk| match chunk {
Some(text) => self.string(text.clone()),
None => Value::Undefined,
})
.collect();
let raw_values: Vec<Value> =
raw.iter().map(|text| self.string(text.clone())).collect();
let raw_array = self.new_array(raw_values);
raw_array.set_extensible(false);
let template = self.new_array(cooked_values);
template.define(
"raw",
Property::with_attributes(Value::Object(raw_array), false, false),
);
template.set_extensible(false);
Value::Object(template)
}
_ => Value::Undefined,
})
}
/// The reference cell a closure-variable slot names.
///
/// Two kinds of binding share this addressing: a genuine closure variable
/// (captured from an enclosing function) lives in this frame's environment,
/// while a *declared global* lives in the realm so that a script, a function
/// declaration and their uses all see one binding. A plain `Global` — an
/// undeclared name that may be satisfied by the global object — has no cell at
/// all, which is how `typeof x` still works for a name that does not exist.
/// The reference cell a closure-variable slot names.
///
/// The environment is resolved once, when the closure is built, and this is
/// just the lookup afterwards. A *declared global* is in there too, pointing at
/// the realm's cell, so a script, a hoisted function declaration and their uses
/// all share one binding. A plain `Global` — a name the script never declared —
/// is read and written through `get_var`/`put_var` by name instead, which is
/// what lets the global object satisfy it.
fn var_ref_cell(&self, slot: usize) -> Option<Gc<Cell>> {
self.frame().env.cells.get(slot).cloned()
}
/// Whether `slot` refers to a global binding through the reference table.
///
/// A global's cell in the closure environment only exists so the slot has
/// something to point at; the binding itself lives on the global object (a `var`)
/// or in the realm's lexical table (a `let`). Reads and writes therefore have to
/// be routed, not performed on the placeholder.
fn is_global_slot(&self, slot: usize) -> bool {
self.frame()
.code
.code
.closure_vars
.get(slot)
.is_some_and(|c| {
// A module's declarations and imports are *not* globals: their cells are
// the module environment's, filled by the host, so the placeholder routing
// must not apply to them.
matches!(
c.closure_type,
ClosureType::GlobalDecl | ClosureType::Global | ClosureType::GlobalRef
)
})
}
fn cell_value(&mut self, slot: usize) -> JsResult<Value> {
if self.is_global_slot(slot) {
return self.get_global(slot);
}
Ok(self
.var_ref_cell(slot)
.map(|cell| cell.get())
.unwrap_or(Value::Undefined))
}
fn set_cell(&mut self, slot: usize, value: Value) -> JsResult<()> {
// A function declaration is hoisted by storing into a global reference before
// the body runs, so this path is also what puts it on the global object.
if self.is_global_slot(slot) {
return self.put_global(slot, value);
}
if let Some(cell) = self.var_ref_cell(slot) {
cell.set(value);
}
Ok(())
}
// -- closures -----------------------------------------------------------
/// Build the closure for the function constant `index`.
///
/// Every entry in the child's closure table names where its binding lives in
/// *this* frame: a local or argument slot that was marked captured, or a cell
/// this frame is itself holding. That is how a variable three functions deep
/// stays one binding.
fn closure(&mut self, index: usize) -> JsResult<Value> {
let Some(Constant::Function(function)) = self.frame().code.code.cpool.get(index).cloned()
else {
return Err(Signal::Throw(
self.make_error("InternalError", "fclosure of a non-function"),
));
};
let mut cells = Vec::with_capacity(function.closure_vars.len());
for closure_var in &function.closure_vars {
let cell = match closure_var.closure_type {
ClosureType::Arg => {
let slot = closure_var.var_idx as usize;
self.frame().code.arg_cell.get(slot).copied().unwrap_or(-1)
}
ClosureType::Local => {
let slot = closure_var.var_idx as usize;
self.frame().code.loc_cell.get(slot).copied().unwrap_or(-1)
}
ClosureType::Ref | ClosureType::GlobalRef => {
// A `Ref` names a cell in this frame's own closure table; a
// `GlobalRef` also starts there, then resolves by name below.
let slot = closure_var.var_idx as usize;
if let Some(cell) = self.frame().env.cells.get(slot) {
cells.push(cell.clone());
continue;
}
-1
}
_ => -1,
};
if cell >= 0 {
let cell = self.frame().cells[cell as usize].clone();
cells.push(cell);
continue;
}
// A global: the cell is created on demand and shared through the realm,
// so every function sees one binding.
let name = closure_var.name.clone();
let cell = match self.realm.globals.get(&name) {
Some(cell) => cell.clone(),
None => {
let cell = self.new_cell(Value::Undefined);
self.realm.globals.insert(name.clone(), cell.clone());
cell
}
};
cells.push(cell);
}
let name = function.name.clone();
// `.length` is the arity before any default or rest parameter, not the number
// of slots a pattern parameter needs.
let length = function.defined_args;
// `[[Construct]]` per §10.2.1: an ordinary function has it, a generator, an async
// function and an arrow function do not. A *method* also does not, but that is not
// known here — the method is marked when the `[[HomeObject]]` is attached (see
// `set_home_object`), which is the moment it becomes a method.
let function_kind = function.kind;
let constructible =
!function.is_arrow && function_kind == js_compile::bytecode::FunctionKind::Normal;
let code = Gc::new(self.heap, CompiledCode::new(*function));
// A closure built inside an `eval` inherits the eval's view of the enclosing
// bindings, so it can still read them once the eval has returned.
let eval_scope = self
.active_eval_scope
.clone()
.or_else(|| self.frame().env.eval_scope.clone());
// A closure sees the variables a direct `eval` declared in the frame it was
// created in — the ones declared before *and after* it, so the same object is
// shared; only the phase bounds which of them are visible.
let local_vars = self.visible_local_vars().map(|(vars, _)| vars);
let local_phase = if self.in_parameter_list() { 0 } else { 1 };
let env = Gc::new(
self.heap,
ClosureEnv {
cells,
eval_scope,
local_vars,
local_phase,
},
);
// A generator or async function inherits from `%GeneratorFunction.prototype%` or
// `%AsyncFunction.prototype%`, not straight from `Function.prototype`. That link is
// the only thing that carries `Symbol.toStringTag`, so without it
// `delete g.constructor.prototype[Symbol.toStringTag]` has nothing to delete and
// `Object.prototype.toString` keeps reporting `GeneratorFunction`.
let proto = match function_kind {
js_compile::bytecode::FunctionKind::AsyncGenerator => self
.realm
.async_generator_function_proto
.clone()
.or_else(|| self.realm.function_proto.clone().into()),
js_compile::bytecode::FunctionKind::Generator => self
.realm
.generator_function_proto
.clone()
.or_else(|| self.realm.function_proto.clone().into()),
js_compile::bytecode::FunctionKind::Async => self
.realm
.async_function_proto
.clone()
.or_else(|| self.realm.function_proto.clone().into()),
js_compile::bytecode::FunctionKind::Normal => self.realm.function_proto.clone().into(),
};
let object = JsObject::function(self.heap, proto, Callable::Bytecode { code, env });
object.set_constructible(constructible);
object.define(
"length",
Property::name_or_length(Value::Int(length as i32)),
);
// Defined even when empty: `(function(){}).name` is `""`, not absent, and a test
// that checks the own-property descriptor fails if the property is missing.
object.define("name", Property::name_or_length(self.string(name)));
// Only a function that can be constructed has a `prototype`: §10.2.5 makes it
// when `[[Construct]]` is created, so a generator, an async function and an arrow
// function have none of their own. Installing it unconditionally gave
// `Array.fromAsync` (an async function) an own `prototype` property, which
// `builtin.js` checks for absence.
if constructible {
self.install_function_prototype(&object);
}
Ok(Value::Object(object))
}
// -- conversions used by the operators ----------------------------------
fn numeric_of(&mut self, value: Value) -> JsResult<Value> {
// `+x` is `ToNumber`, which for a string parses it and for an object runs
// `ToPrimitive` first. Returning a string unchanged would let `+"1"` stay a
// string, which then behaves differently in every later comparison.
match value {
Value::Int(_) | Value::Float(_) => Ok(value),
other => Ok(Value::Float(self.to_js_number(&other)?)),
}
}
/// `++`/`--`, which step a bigint by `1n` and everything else by `1`.
///
/// Returns the *converted* old value and the new one, because the two callers need
/// different halves: `++x` yields the new value, while `x++` yields the old one and
/// still stores the new. The old value is `ToNumeric(x)`, not `x` — `a.x++` where
/// `a.x` is `true` evaluates to `1`, so returning the raw operand handed back a
/// boolean.
///
/// A bigint stays a bigint here: `ToNumeric` keeps the type, so routing `i++` through
/// the number path turned it into `NaN`, and every `for (i = 0n; i < n; i++)` loop in
/// a bigint algorithm then ran exactly once.
fn advance_numeric(&mut self, value: Value, up: bool) -> JsResult<(Value, Value)> {
if let Value::BigInt(big) = &value {
let one = crate::bigint::BigInt::from_i64(1);
let next = if up { big.add(&one) } else { big.sub(&one) };
return Ok((value, self.bigint(next)));
}
let old = self.numeric(value)?;
let new = if up {
increment(old.clone())
} else {
decrement(old.clone())
};
Ok((old, new))
}
fn numeric(&mut self, value: Value) -> JsResult<Value> {
// `ToNumber` on an object runs `ToPrimitive` first, which is how
// `2 * {valueOf(){return 3}}` sees the 3 — and how a `valueOf` that throws is
// heard at all. Reading the object's own tag instead (`other.to_number()`)
// turned the whole operand into `NaN` and swallowed the exception.
match value {
Value::Int(_) | Value::Float(_) => Ok(value),
other => Ok(Value::Float(self.to_js_number(&other)?)),
}
}
/// The operand of a `BigInt` operation, or the `TypeError` that mixing raises.
fn bigint_operand(&self, value: &Value) -> JsResult<crate::bigint::BigInt> {
match value {
Value::BigInt(big) => Ok((**big).clone()),
// The reference words this the same way for every operator, so a failing
// script reads identically; the operator itself adds nothing a caller can use.
_ => Err(Signal::Throw(
self.make_error("TypeError", "cannot convert bigint to number"),
)),
}
}
/// Arithmetic where at least one operand is a `BigInt`.
///
/// The two numeric types never mix, so a bigint paired with anything else is an error
/// rather than a conversion: `1n + 1` throws, and so does `1n < 1` only in the sense
/// that the relational operators handle it separately.
fn bigint_arithmetic(&mut self, op: Opcode, left: Value, right: Value) -> JsResult<Value> {
use Opcode::*;
let a = self.bigint_operand(&left)?;
let b = self.bigint_operand(&right)?;
let result = match op {
Add => a.add(&b),
Sub => a.sub(&b),
Mul => a.mul(&b),
Div | Mod => {
let Some((quotient, remainder)) = a.div_rem(&b) else {
return Err(Signal::Throw(
self.make_error("RangeError", "division by zero"),
));
};
if op == Div { quotient } else { remainder }
}
Pow => {
let Some(value) = a.pow(&b) else {
return Err(Signal::Throw(self.make_error(
"RangeError",
"BigInt exponent must be non-negative and not absurdly large",
)));
};
value
}
And => a.bitand(&b),
Or => a.bitor(&b),
Xor => a.bitxor(&b),
Shl | Sar => {
// A shift count is a `BigInt` here, and both directions are exact; a
// negative count would be the other direction, which the spec forbids.
let Some(count) = b.to_i64() else {
return Err(Signal::Throw(
self.make_error("RangeError", "BigInt shift count is too large"),
));
};
if count < 0 {
return Err(Signal::Throw(self.make_error(
"RangeError",
"BigInt shift count must be non-negative",
)));
}
if op == Shl {
a.shl(count as u64)
} else {
a.shr(count as u64)
}
}
// `>>>` is defined only for 32-bit integers, so a bigint has no unsigned shift.
_ => {
return Err(Signal::Throw(self.make_error(
"TypeError",
"BigInts have no unsigned right shift, use >> instead",
)));
}
};
Ok(self.bigint(result))
}
/// `<`, `<=`, `>`, `>=`, which accept a `BigInt` and a Number alongside each other.
///
/// Unlike arithmetic, relation is defined across the two numeric types: the comparison
/// is on the mathematical values, and nothing is rounded first.
fn relational(&mut self, op: Opcode, left: Value, right: Value) -> Value {
use Opcode::*;
let ordering = match (&left, &right) {
(Value::String(a), Value::String(b)) => {
Some(a.to_string_lossy().cmp(&b.to_string_lossy()))
}
(Value::BigInt(a), Value::BigInt(b)) => Some(a.cmp(b)),
// A bigint against a number compares the *mathematical* values, so `2n < 2.5`
// is true even though `2.5` has no bigint form.
(Value::BigInt(a), other) if !matches!(other, Value::BigInt(_)) => {
let b = other.to_number();
if b.is_nan() {
None
} else {
compare_bigint_and_f64(a, b)
}
}
(other, Value::BigInt(b)) if !matches!(other, Value::BigInt(_)) => {
let a = other.to_number();
if a.is_nan() {
None
} else {
compare_bigint_and_f64(b, a).map(std::cmp::Ordering::reverse)
}
}
_ => left.to_number().partial_cmp(&right.to_number()),
};
let _ = self;
let answer = match (op, ordering) {
(_, None) => false,
(Lt, Some(ordering)) => ordering.is_lt(),
(Lte, Some(ordering)) => ordering.is_le(),
(Gt, Some(ordering)) => ordering.is_gt(),
(Gte, Some(ordering)) => ordering.is_ge(),
_ => false,
};
Value::Bool(answer)
}
fn binary_add(&mut self) -> JsResult<()> {
let right = self.pop();
let left = self.pop();
let left = self.primitive_of(left)?;
let right = self.primitive_of(right)?;
// `+` concatenates when either operand is a string *after* `ToPrimitive`, and
// that check comes before the bigint one: `"" + 1n` is a string, not a
// mixed-type error.
if matches!(left, Value::String(_)) || matches!(right, Value::String(_)) {
// `ToString` on both sides, which rejects a symbol: `"" + Symbol()` is a
// `TypeError`, the one place a symbol and a string do not mix.
// Keep the operands as strings rather than as bytes: `JsString::concat` decides
// between a copy and a rope from their lengths, and a rope is what turns
// `text += "abc"` in a loop from quadratic into linear.
if let (Value::String(left), Value::String(right)) = (&left, &right) {
// `None` is the length ceiling (`JS_STRING_LEN_MAX`), which the reference
// reports as `InternalError: string too long`.
let Some(joined) = crate::value::JsString::concat(self.heap, left, right) else {
return Err(Signal::Throw(
self.make_error("InternalError", "string too long"),
));
};
self.push(Value::String(Gc::new(self.heap, joined)));
return Ok(());
}
// At least one operand was not a string, so `ToString` it. This rejects a
// symbol, which is the one place a symbol and a string do not mix.
let left = self.checked_to_wtf8(&left)?;
let right = self.checked_to_wtf8(&right)?;
// Not `format!`: `Wtf8::concat` is what keeps a surrogate pair split across the
// join canonical, so `"\uD800" + "\uDC00"` equals `"\u{10000}"`.
let text = left.concat(&right);
self.push(self.string_wtf8(text));
return Ok(());
}
// The two numeric types never mix, and `+` is no exception: `1n + 1` is an error,
// not a conversion.
if matches!(left, Value::BigInt(_)) || matches!(right, Value::BigInt(_)) {
let value = self.bigint_arithmetic(Opcode::Add, left, right)?;
self.push(value);
return Ok(());
}
let left = self.numeric(left)?;
let right = self.numeric(right)?;
self.push(add_numbers(left, right));
Ok(())
}
fn primitive_of(&mut self, value: Value) -> JsResult<Value> {
self.to_primitive(value, Hint::Default)
}
/// `ToPrimitive`, which is what makes `"" + obj` produce `obj.toString()`.
///
/// The order depends on the hint: the "number" order tries `valueOf` first and
/// `toString` second, the "string" order the other way round, and the default
/// hint uses the number order. A result that is still an object is a `TypeError`,
/// because no built-in `valueOf`/`toString` returns one.
pub fn to_primitive(&mut self, value: Value, hint: Hint) -> JsResult<Value> {
let Value::Object(object) = &value else {
return Ok(value);
};
// A Date overrides the default hint to mean "string", which is what
// `Date.prototype[Symbol.toPrimitive]` does with hint `"default"`. Without
// this, `"" + new Date()` would print the milliseconds, not the date.
let hint = if hint == Hint::Default && is_date_object(self, object) {
Hint::String
} else {
hint
};
// `Symbol.toPrimitive` wins over `valueOf`/`toString` when it exists, and it is
// handed the hint as a string. This is the hook a Date uses for its default hint
// and a script uses to take over conversion entirely.
let key = self.realm.well_known_key(WellKnown::ToPrimitive);
if let Some(Value::Object(function)) = self.get_property_key(object, &key)? {
if !function.is_callable() {
return Err(self.type_error("Symbol.toPrimitive is not a function"));
}
let hint = match hint {
Hint::String => "string",
Hint::Number => "number",
Hint::Default => "default",
};
let hint = self.string(hint);
let result = self.call_value(
Value::Object(function),
Value::Object(object.clone()),
vec![hint],
)?;
if matches!(result, Value::Object(_)) {
return Err(self.type_error("cannot convert object to primitive value"));
}
return Ok(result);
}
let order: [&str; 2] = match hint {
Hint::String => ["toString", "valueOf"],
Hint::Number | Hint::Default => ["valueOf", "toString"],
};
let receiver = object.clone();
for method in order {
if let Some(function) = self.get_property(&receiver, method)?
&& matches!(&function, Value::Object(object) if object.is_callable())
{
let result =
self.call_value(function, Value::Object(receiver.clone()), Vec::new())?;
if !matches!(result, Value::Object(_)) {
return Ok(result);
}
}
}
Err(self.type_error("cannot convert object to primitive value"))
}
/// `ToString` with the object case going through `ToPrimitive`.
pub fn to_js_string(&mut self, value: &Value) -> JsResult<String> {
if matches!(value, Value::Object(_)) {
let primitive = self.to_primitive(value.clone(), Hint::String)?;
return Ok(primitive.to_js_string());
}
Ok(value.to_js_string())
}
/// `ToString` that keeps the string's code units instead of flattening them.
///
/// This is what `+` needs: `String.fromCharCode(0xD800) + "a"` must keep the unpaired
/// surrogate, and a lossy `String` in between would turn it into U+FFFD.
pub fn checked_to_wtf8(&mut self, value: &Value) -> JsResult<crate::value::Wtf8> {
if matches!(value, Value::Symbol(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "cannot convert symbol to string"),
));
}
// An object runs `ToPrimitive` first, which may itself produce a symbol.
let value = self.to_primitive(value.clone(), Hint::String)?;
match value {
Value::Symbol(_) => Err(Signal::Throw(
self.make_error("TypeError", "cannot convert symbol to string"),
)),
Value::String(text) => Ok(text.wtf8().clone()),
other => Ok(crate::value::Wtf8::new(other.to_js_string())),
}
}
/// `ToNumber` with the object case going through `ToPrimitive`.
pub fn to_js_number(&mut self, value: &Value) -> JsResult<f64> {
let value = if matches!(value, Value::Object(_)) {
self.to_primitive(value.clone(), Hint::Number)?
} else {
value.clone()
};
// `ToNumber` refuses a BigInt: there is no lossless number for a 2^70 integer, so
// the spec makes this a `TypeError` rather than `NaN`. `Value::to_number` answers
// `NaN` to stay total, and the operators check for the bigint themselves — but
// `Math.max(1n)`, `new Int32Array(1n)` and every other `ToNumber` caller needs to
// refuse, and answering `NaN` made them silently wrong instead.
// Both are checked *after* `ToPrimitive`, so a boxed Symbol or BigInt is refused
// too: `ToNumber(Object(Symbol()))` unwraps to the symbol and then fails.
match value {
Value::BigInt(_) => Err(self.type_error("cannot convert bigint to number")),
Value::Symbol(_) => Err(self.type_error("cannot convert symbol to number")),
other => Ok(other.to_number()),
}
}
/// `ToNumber` or `ToBigInt`, whichever `element` stores, as a primitive ready for
/// [`ElementType::write`].
///
/// This is separated from the write because both conversions are observable script —
/// `ToPrimitive` calls `valueOf` — and a conversion that runs script must not happen
/// with a buffer's `RefCell` borrowed. Every element-write path calls this first:
/// the constructor's initialiser list, `fill`/`set`/`sort`'s write-back, `DataView`'s
/// setters, and a plain `a[i] = v`.
pub(crate) fn element_value(
&mut self,
element: crate::builtins::typedarray::ElementType,
value: Value,
) -> JsResult<Value> {
// `ToPrimitive` with the number hint, which is what turns `{valueOf(){return 5}}`
// into `5` and `{valueOf(){return 1n}}` into a BigInt. A primitive passes through.
let primitive = match &value {
Value::Object(_) => self.to_primitive(value, Hint::Number)?,
_ => value,
};
if element.is_bigint() {
let big = crate::builtins::bigint::to_bigint(self, &primitive)?;
Ok(Value::BigInt(Gc::new(self.heap, big)))
} else {
// `ToNumber` refuses a BigInt and a Symbol rather than answering `NaN`.
Ok(Value::Float(self.to_js_number(&primitive)?))
}
}
/// `ToPropertyKey`: what a computed member access uses as its key.
///
/// A symbol is returned as itself, never stringified — that is what keeps
/// `o[sym]` and `o["Symbol(a)"]` apart. Everything else becomes its string form.
pub fn propkey(&mut self, value: &Value) -> JsResult<PropertyKey> {
match value {
Value::Symbol(symbol) => Ok(PropertyKey::Symbol(symbol.clone())),
// An object key runs `ToPropertyKey`, which is `ToPrimitive` *then* a symbol
// check: a wrapper whose `toString` answers a symbol makes a symbol key, not
// the string `"Symbol()"`. Going through `propkey_of` here would stringify it.
Value::Object(_) => match self.to_primitive(value.clone(), Hint::String)? {
Value::Symbol(symbol) => Ok(PropertyKey::Symbol(symbol)),
Value::String(text) => Ok(PropertyKey::String(text.wtf8().clone())),
other => Ok(PropertyKey::String(crate::value::Wtf8::new(
other.to_js_string(),
))),
},
_ => Ok(PropertyKey::String(self.propkey_of(value)?)),
}
}
/// `ToPropertyKey` for the callers that can only use a string key.
///
/// Fallible on purpose: an object key runs `ToPrimitive`, and that can throw —
/// `{ toString(){ throw new Error('ts') } }` used as a template substitution is a
/// throw, not `[object Object]`. Swallowing it made `` `${o}` `` report the string
/// `[object Object]` and carry on, which is the worst kind of wrong answer: the
/// program continues with a value nobody asked for.
fn propkey_of(&mut self, value: &Value) -> JsResult<crate::value::Wtf8> {
use crate::value::Wtf8;
Ok(match value {
// A string key keeps its code units. Going through a lossy `String` would make
// `o["\uD800"]` and `o["\uFFFD"]` the same property.
Value::String(text) => text.wtf8().clone(),
Value::Int(_) | Value::Float(_) => Wtf8::new(number_to_string(value.to_number())),
// An object key runs `ToPropertyKey`, which needs `ToPrimitive` first;
// falling back to its `ToString` would print `[object Object]`.
Value::Object(_) => match self.to_primitive(value.clone(), Hint::String)? {
Value::String(text) => text.wtf8().clone(),
other => Wtf8::new(other.to_js_string()),
},
other => Wtf8::new(other.to_js_string()),
})
}
// -- object operations used by instructions -----------------------------
/// Read `object[key]`, returning `undefined` when the property is missing.
///
/// Unlike [`Interp::get_property`] this takes a runtime `Value`, so it works on a
/// primitive (looking through its prototype) as well as an object — which is what
/// `get_field`/`get_array_el` need and what a builtin reading a property off an
/// argument needs too.
pub fn read_field(&mut self, object: &Value, key: &str) -> JsResult<Value> {
self.read_field_key(object, &PropertyKey::from(key))
}
/// [`Interp::read_field`] with a key that may be a symbol.
///
/// A symbol key never reaches the string shortcuts: a string's `length` and its
/// indices are string properties, and a symbol named `length` is a different
/// property that a string primitive cannot have.
/// [`Interp::read_field_key`], with an inline cache in front of it.
///
/// Only own *data* properties of ordinary objects are cached. Everything else — a
/// prototype hit, a getter, a proxy, a string primitive, a missing property — goes
/// straight through, because those are the cases whose answer depends on more than "the
/// object and the name", and caching them would need the shape machinery this engine
/// deliberately does not have.
///
/// A hit is re-validated: the object must still be the same one (weakly, so a collected
/// and reallocated object is a miss, not a wrong answer) and the property table must
/// still have this key at this index as a plain value.
pub fn read_field_cached(
&mut self,
object: &Value,
key: &PropertyKey,
slot: usize,
) -> JsResult<Value> {
let index = Self::field_cache_slot(object, key, slot);
let Value::Object(target) = object else {
return self.read_field_key(object, key);
};
if target.proxy_state().is_some() {
return self.read_field_key(object, key);
}
if let Some(cached) = &self.field_cache[index]
&& target.upgrades_to(&cached.object)
&& cached.key == *key
&& let Some(value) = target.cached_data_property(cached.index, key)
{
return Ok(value);
}
let value = self.read_field_key(object, key)?;
// Only a successful own-data lookup is worth remembering. `own_property_index`
// declines an accessor, which is exactly the case that must not be cached.
if let Some((found, _)) = target.own_property_index(key) {
self.field_cache[index] = Some(FieldCache {
object: target.downgrade(),
key: key.clone(),
index: found,
});
}
Ok(value)
}
/// Which cache slot a lookup uses.
///
/// Mixes the object's address, the name and the site, so that two properties of one
/// object, and one property of two objects, land in different slots — which is what lets
/// a *polymorphic* site hold both of its shapes at once instead of evicting itself every
/// other iteration.
fn field_cache_slot(object: &Value, key: &PropertyKey, site: usize) -> usize {
let mut hash = match object {
Value::Object(object) => object.address(),
_ => 0,
};
hash = hash
.wrapping_mul(0x9e37_79b9_7f4a_7c15)
.rotate_left(17)
.wrapping_add(key.hash_code());
hash = hash.wrapping_add(site.wrapping_mul(31));
// The table is a power of two, so the low bits are the slot.
hash & (FIELD_CACHE_SLOTS - 1)
}
/// Read `key` from `object`, without the inline cache.
///
/// Use [`Interp::read_field_cached`] from an opcode handler: this one always walks the
/// prototype chain, while the cached form answers an own data property from memory.
pub fn read_field_key(&mut self, object: &Value, key: &PropertyKey) -> JsResult<Value> {
let text = key.as_str().unwrap_or("");
let Value::Object(object) = object else {
// Reading a property of `undefined`/`null` is the classic TypeError.
if object.is_nullish() {
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("cannot read property '{key}' of {}", object.to_js_string()),
)));
}
// A string has `length` and index access without being boxed.
if let Value::String(value) = object {
if text == "length" {
return Ok(Value::Int(value.utf16_len() as i32));
}
if let Some(index) = array_index(text) {
return Ok(match value.wtf8().unit_at(index as usize) {
Some(unit) => self.string_wtf8(unit),
None => Value::Undefined,
});
}
}
// A property of a primitive is looked up on its prototype: this is what
// makes `"a".toUpperCase()` reach `String.prototype` without boxing.
let prototype = match object {
Value::String(_) => self.realm.string_proto.clone(),
Value::Int(_) | Value::Float(_) => self.realm.number_proto.clone(),
Value::Bool(_) => self.realm.boolean_proto.clone(),
// A symbol's methods live on `Symbol.prototype`, reached the same way —
// `sym.description` reads through the prototype, not off the symbol.
Value::Symbol(_) => self.realm.symbol_proto.clone(),
// …and a bigint's on `BigInt.prototype`, which is how
// `(255n).toString(16)` reaches its method.
Value::BigInt(_) => self.realm.bigint_proto.clone(),
_ => None,
};
if let Some(prototype) = prototype
&& let Some(value) = self.get_property_key_for(&prototype, Some(object), key)?
{
return Ok(value);
}
return Ok(Value::Undefined);
};
Ok(self
.get_property_key(object, key)?
.unwrap_or(Value::Undefined))
}
/// The `#name` a private symbol stands for, for error messages.
///
/// The reference reports `#x` (with the `#`), and formats it through the atom the symbol
/// was made from.
fn private_name(symbol: &Value) -> Option<String> {
match symbol {
Value::Symbol(symbol) if symbol.is_private() => Some(
symbol
.description()
.map(|text| text.to_string())
.unwrap_or_default(),
),
_ => None,
}
}
/// Resolve the private-key operand to a [`PropertyKey`], or reject it.
///
/// Every private-field opcode takes the symbol the class stored in its own scope; a
/// non-symbol here would be a compiler bug, and the reference guards it with
/// `JS_ThrowTypeErrorNotASymbol`.
fn private_key(&mut self, symbol: &Value) -> JsResult<PropertyKey> {
match symbol {
Value::Symbol(symbol) if symbol.is_private() => Ok(PropertyKey::Symbol(symbol.clone())),
_ => Err(Signal::Throw(self.make_error("TypeError", "not a symbol"))),
}
}
/// `JS_DefinePrivateField`: `obj.#name = value` from a field initialiser.
///
/// Three early errors, all from the reference: a non-object receiver, a non-extensible
/// one, and a field that already exists — the last is what `class C { #x = 1; #x = 2 }`
/// would hit if the parser did not reject it first.
fn define_private_field(
&mut self,
object: &Value,
symbol: &Value,
value: Value,
) -> JsResult<()> {
let key = self.private_key(symbol)?;
let Some(target) = object.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
if !target.is_extensible() {
return Err(Signal::Throw(
self.make_error("TypeError", "object is not extensible"),
));
}
if target.own_property_key(&key).is_some() {
let name = Self::private_name(symbol).unwrap_or_default();
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("private class field '{name}' already exists"),
)));
}
target.define_data_key(&key, value);
Ok(())
}
/// `JS_GetPrivateField`: only an own property, never the prototype chain.
fn read_private_field(&mut self, object: &Value, symbol: &Value) -> JsResult<Value> {
let key = self.private_key(symbol)?;
let Some(target) = object.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
match target.own_property_key(&key) {
Some(property) => Ok(property.value.unwrap_or(Value::Undefined)),
None => {
let name = Self::private_name(symbol).unwrap_or_default();
Err(Signal::Throw(self.make_error(
"TypeError",
&format!("private class field '{name}' does not exist"),
)))
}
}
}
/// `JS_SetPrivateField`: the store half of the same rule.
fn write_private_field(
&mut self,
object: &Value,
symbol: &Value,
value: Value,
) -> JsResult<()> {
let key = self.private_key(symbol)?;
let Some(target) = object.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
if target.write_value_key(&key, value) {
return Ok(());
}
let name = Self::private_name(symbol).unwrap_or_default();
Err(Signal::Throw(self.make_error(
"TypeError",
&format!("private class field '{name}' does not exist"),
)))
}
/// The `in` half: does `object` have this private field?
/// `JS_AddBrand`: make `home` the owner of a brand symbol, and give that symbol to `obj`.
///
/// The symbol is created once per class — the first `add_brand` on a given home object
/// makes it, every later one reuses it, which is what ties the prototype, the constructor
/// and each instance to one identity. `obj` being `null` is not an error: the class-level
/// emit for the *instance* brand passes null because all it wants is the symbol.
fn add_brand(&mut self, object: &Value, home: &Value) -> JsResult<()> {
let Some(home_object) = home.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
let symbol = match home_object.private_brand() {
Some(symbol) => symbol,
None => {
let symbol = Gc::new(
self.heap,
crate::value::JsSymbol::new_private(BRAND_DESCRIPTION),
);
home_object.set_private_brand(symbol.clone());
symbol
}
};
let Some(target) = object.as_object() else {
// Not an object: nothing to brand. The reference does the same, and `undefined`
// here would otherwise turn a field initialiser into a TypeError.
return Ok(());
};
if !target.is_extensible() {
return Err(Signal::Throw(
self.make_error("TypeError", "object is not extensible"),
));
}
if target.carries_brand(&symbol) {
return Err(Signal::Throw(
self.make_error("TypeError", "private method is already present"),
));
}
target.add_brand(symbol);
Ok(())
}
/// `JS_CheckBrand`: does `object` carry the brand of `function`'s home object?
///
/// A `false` answer is not an exception — `#m in o` reports it as a value — so the caller
/// decides whether to throw.
fn check_brand(&mut self, object: &Gc<JsObject>, function: &Value) -> JsResult<bool> {
let Some(function) = function.as_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
let Some(home) = function.home_object() else {
return Err(Signal::Throw(self.make_error("TypeError", "not an object")));
};
let Some(brand) = home.private_brand() else {
return Err(Signal::Throw(
self.make_error("TypeError", "expecting <brand> private field"),
));
};
Ok(object.carries_brand(&brand))
}
fn write_field_key(&mut self, object: &Value, key: &PropertyKey, value: Value) -> JsResult<()> {
let Value::Object(object) = object else {
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("cannot set property '{key}' of {}", object.to_js_string()),
)));
};
// `put_field`/`put_array_el` are plain assignments, so the mode decides.
let on_failure = if self.frame().code.code.strict {
DeleteFailure::Throw
} else {
DeleteFailure::Ignore
};
self.set_property_with_key(object, key, value, on_failure)
}
/// `ToString`, as the spec defines it: a symbol is a `TypeError`.
///
/// [`Value::to_js_string`] cannot fail, so it answers the descriptive form for a
/// symbol. That is right for `String(sym)` and for error messages, and wrong for
/// everything the spec routes through `ToString` — `"" + sym`, `${sym}`,
/// `Number.parseInt(sym)` — so those callers use this instead.
pub fn checked_to_string(&mut self, value: &Value) -> JsResult<String> {
if matches!(value, Value::Symbol(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "cannot convert symbol to string"),
));
}
// An object runs `ToPrimitive` first, which may itself produce a symbol —
// `({[Symbol.toPrimitive]: () => sym}) + ""` has to throw too.
let value = self.to_primitive(value.clone(), Hint::String)?;
if matches!(value, Value::Symbol(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "cannot convert symbol to string"),
));
}
Ok(value.to_js_string())
}
/// `ToPropertyKey` for a builtin that takes a key argument.
///
/// Fallible: a key whose `toString` throws has to propagate that error, both for
/// `Reflect.get({}, {toString(){ throw 1 }})` (`return-abrupt-from-property-key`) and
/// for a template substitution. This used to answer `"[object Object]"` on a throw,
/// which made `` `${o}` `` and the like carry on with a value nobody asked for.
pub fn property_key(&mut self, value: &Value) -> JsResult<PropertyKey> {
self.propkey(value)
}
/// `ToUint32` with `ArraySetLength`'s refusal, for an array's `length`.
///
/// `ToUint32` is `ToNumber` *then* the modulo, but an object that yields a fraction or
/// `NaN` falls through to `ToString` and is parsed as decimal digits. That is why
/// `{toString: () => "2"}` is a legal length while `{valueOf: () => 2.5}` is a
/// `RangeError` — and why this cannot be a plain `ToNumber`.
pub fn array_length_of(&mut self, value: &Value) -> JsResult<u32> {
let number = match value {
Value::Int(number) => *number as f64,
Value::Float(number) => *number,
// `null`/`undefined`/`false` convert through `ToPrimitive` and then arithmetically:
// `null` is 0, `false` is 0, `true` is 1. Refusing them made
// `Object.defineProperty(a, "length", {value: null})` a `RangeError` where the
// reference answers 0.
Value::Null | Value::Bool(_) => value.to_number(),
_ => {
let primitive = self.to_primitive(value.clone(), Hint::Number)?;
match primitive {
Value::Int(number) => number as f64,
Value::Float(number) => number,
other => {
// `ToString` then `ToNumber`, so a string length goes through the
// full numeric-literal grammar: `"0x00B"` is 11 and `" 5 "` is 5.
// `Value::to_number` on the string already implements exactly that.
let text = self.to_js_string(&other)?;
crate::value::string_to_number(&text)
}
}
}
};
// `ToUint32` of anything outside `0 ..= 2^32-1` that is still an integer is a
// modulo in the spec, but `ArraySetLength` refuses it outright: `a.length = 2**32`
// is a `RangeError`, not `0`.
if !number.is_finite()
|| number.fract() != 0.0
|| !(0.0..=u32::MAX as f64).contains(&number)
{
return Err(self.range_error("invalid array length"));
}
Ok(number as u32)
}
/// A [`PropertyKey`] as the `Value` a script sees — what `Reflect.ownKeys` and
/// `Object.getOwnPropertySymbols` put in their result array.
pub fn key_value(&self, key: &PropertyKey) -> Value {
match key {
PropertyKey::String(text) => self.string_wtf8(text.clone()),
PropertyKey::Symbol(symbol) => Value::Symbol(symbol.clone()),
}
}
/// Fill in a String exotic object: `_value`, `length` and the index properties.
///
/// `new String("ab")` used to get only `_value`, so `new String("ab").length` was
/// `undefined`, `new String("ab")[0]` was `undefined`, and every `Array.prototype`
/// method called on a box saw an array-like of length 0 — 36 test262 files exercise
/// exactly that. `ToObject` on a *primitive* string built the indices correctly, so
/// the two paths had drifted; this is the one both now use.
///
/// The indices are *code units*, so an astral character occupies two of them — the
/// same rule `s.length` and `s[i]` follow.
/// Give an already-created object the internal slot that makes it a wrapper of
/// `value`, so the primitive's prototype methods work on it.
///
/// Used for `%Boolean.prototype%`, `%Number.prototype%` and `%String.prototype%`,
/// which are wrappers of their own type holding the empty value.
pub(crate) fn init_primitive_wrapper(&mut self, object: &Gc<JsObject>, value: Value) {
self.internal_set(object, "_value", value);
}
pub(crate) fn init_string_wrapper(
&mut self,
object: &Gc<JsObject>,
text: &Gc<crate::value::JsString>,
) {
object.define(
"length",
Property::with_attributes(Value::Int(text.utf16_len() as i32), false, false),
);
for index in 0..text.utf16_len() {
if let Some(unit) = text.wtf8().unit_at(index) {
object.define(
&index.to_string(),
// Own, non-writable, enumerable, non-configurable — §10.4.3.4.
Property::with_attributes(self.string_wtf8(unit), false, true),
);
}
}
object.define_key(
&self.internal_slot("_value"),
Property::with_attributes(Value::String(text.clone()), false, false),
);
}
/// `ToObject`: box a primitive, refuse `undefined` and `null`.
///
/// A destructuring *object* pattern starts with this, which is why `var {} = null`
/// throws. The wrapper carries its primitive in `_value` — the same slot
/// `new Number(x)` and friends fill in — so `valueOf` and the prototype methods work
/// on a boxed value no constructor ever built.
pub(crate) fn object_from(&mut self, value: &Value) -> JsResult<Gc<JsObject>> {
let (prototype, primitive) = match value {
Value::Object(object) => return Ok(object.clone()),
Value::String(text) => {
let object = self.new_object(self.realm.string_proto.clone());
self.init_string_wrapper(&object, text);
return Ok(object);
}
Value::Int(_) | Value::Float(_) => (self.realm.number_proto.clone(), value.clone()),
Value::Bool(_) => (self.realm.boolean_proto.clone(), value.clone()),
// A symbol and a bigint box into wrappers whose methods live on
// `Symbol.prototype` and `BigInt.prototype` — `Object(sym).description` and
// `Object(1n).toString(2)` both go through the wrapper. Falling into the
// branch below instead produced an object with *no* prototype, so those
// methods were unreachable on a box.
Value::Symbol(_) => (self.realm.symbol_proto.clone(), value.clone()),
Value::BigInt(_) => (self.realm.bigint_proto.clone(), value.clone()),
Value::Undefined | Value::Null => {
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("cannot convert {} to object", value.to_js_string()),
)));
}
_ => (None, value.clone()),
};
let object = self.new_object(prototype);
self.internal_set(&object, "_value", primitive);
Ok(object)
}
/// `a == b`, the coercing comparison.
///
/// An object operand is converted with `ToPrimitive` *through the interpreter*,
/// which is what makes `[] == ""` true: `[]` becomes `""` via
/// `Array.prototype.toString`. [`Value::loose_equals`] cannot do that on its own
/// because the conversion is allowed to call script code, so the two primitive
/// operands are handed back to it.
fn abstract_equals(&mut self, left: Value, right: Value) -> JsResult<bool> {
if !matches!(left, Value::Object(_)) && !matches!(right, Value::Object(_)) {
return Ok(left.loose_equals(&right));
}
// `null == undefined` holds even though neither is an object.
if matches!(left, Value::Undefined | Value::Null)
&& matches!(right, Value::Undefined | Value::Null)
{
return Ok(true);
}
if matches!(left, Value::Undefined | Value::Null)
|| matches!(right, Value::Undefined | Value::Null)
{
return Ok(false);
}
// Two objects compare by identity. Only an object *against a primitive* is
// converted, which is why `{} == {}` is false while `[] == ""` is true.
if let (Value::Object(a), Value::Object(b)) = (&left, &right) {
return Ok(a.ptr_eq(b));
}
let left = self.to_primitive(left, Hint::Default)?;
let right = self.to_primitive(right, Hint::Default)?;
Ok(left.loose_equals(&right))
}
fn has_property(&mut self, key: &Value, object: &Value) -> JsResult<bool> {
let Value::Object(object) = object else {
return Err(Signal::Throw(
self.make_error("TypeError", "cannot use 'in' on a non-object"),
));
};
let key = self.propkey(key)?;
// `in` is `[[HasProperty]]`, over the whole chain and through a proxy's `has`
// trap. One implementation, so an array's derived `length` and its inherited holes
// cannot be right in one caller and wrong in another.
self.has_own_or_inherited(object, &key)
}
fn instance_of(&mut self, object: &Value, constructor: &Value) -> JsResult<bool> {
let Value::Object(target) = object else {
return Ok(false);
};
let Value::Object(function) = constructor else {
return Err(Signal::Throw(self.make_error(
"TypeError",
"right-hand side of 'instanceof' is not callable",
)));
};
let prototype = self.get_property(function, "prototype")?;
let Some(Value::Object(prototype)) = prototype else {
return Ok(false);
};
// `instanceof` walks `[[GetPrototypeOf]]`, so a proxy answers through its trap —
// which is also what makes `proxyOfInstance instanceof C` work.
let mut current = Some(target.clone());
while let Some(object) = current {
if object.ptr_eq(&prototype) {
return Ok(true);
}
current = self.object_prototype_of(&object)?;
}
Ok(false)
}
/// `[[GetPrototypeOf]]`, with the proxy trap when there is one.
pub fn object_prototype_of(&mut self, object: &Gc<JsObject>) -> JsResult<Option<Gc<JsObject>>> {
if let Some(proto) = self.proxy_get_prototype_of(object)? {
return Ok(proto);
}
Ok(object.proto())
}
/// `[[SetPrototypeOf]]`, with the proxy trap when there is one.
pub fn object_set_prototype_of(
&mut self,
object: &Gc<JsObject>,
proto: Option<Gc<JsObject>>,
) -> JsResult<bool> {
if let Some(answer) = self.proxy_set_prototype_of(object, proto.clone())? {
return Ok(answer);
}
object.set_proto(proto);
Ok(true)
}
/// `[[IsExtensible]]`, with the proxy trap when there is one.
pub fn object_is_extensible(&mut self, object: &Gc<JsObject>) -> JsResult<bool> {
if let Some(answer) = self.proxy_is_extensible(object)? {
return Ok(answer);
}
Ok(object.is_extensible())
}
/// `[[PreventExtensions]]`, with the proxy trap when there is one.
pub fn object_prevent_extensions(&mut self, object: &Gc<JsObject>) -> JsResult<bool> {
if let Some(answer) = self.proxy_prevent_extensions(object)? {
return Ok(answer);
}
object.set_extensible(false);
Ok(true)
}
fn copy_properties(
&mut self,
target: &Value,
source: &Value,
exclusion: &Value,
) -> JsResult<()> {
// `CopyDataProperties` starts with `ToObject(source)`, so a *string* primitive
// does contribute: its index properties are own, enumerable and writable, which
// is why `{..."ab"}` is `{"0":"a","1":"b"}`. Numbers and booleans box to objects
// with no own properties, so they copy nothing, and spreading `undefined`/`null`
// is a no-op.
let Some(target) = target.as_object() else {
return Ok(());
};
let source_object = match source {
Value::Object(object) => object.clone(),
Value::String(text) => {
// Untaken keys are the string's indices, which are code units.
for index in 0..text.utf16_len() {
if let Some(unit) = text.wtf8().unit_at(index) {
target.define_data(&index.to_string(), self.string_wtf8(unit));
}
}
return Ok(());
}
_ => return Ok(()),
};
// An object *rest* element passes the keys already taken by the literal part as
// an array of strings; a spread passes `null`, which excludes nothing. Only the
// string keys of the exclusion array are meaningful, so an array is read by its
// elements rather than by its own property names.
let mut excluded: Vec<crate::value::Wtf8> = Vec::new();
if let Value::Object(list) = exclusion
&& let Some(length) = list.array_length()
{
for index in 0..length {
if let Some(value) = list.array_get(index) {
excluded.push(self.propkey_of(&value)?);
}
}
}
let source = &source_object;
// Through `[[OwnPropertyKeys]]` and `[[Get]]`, so `{...proxy}` asks the handler.
// Reading the property table directly made every spread of a proxy empty.
let keys = self.object_own_property_keys(source)?;
for as_key in keys {
if as_key.is_symbol() {
// A spread copies string-keyed properties only.
continue;
}
if excluded.contains(&crate::value::Wtf8::new(
as_key.as_str().unwrap_or_default(),
)) {
continue;
}
let enumerable = self
.object_get_own_property(source, &as_key)?
.is_some_and(|property| property.enumerable);
if !enumerable {
continue;
}
let value = self
.get_property_key(source, &as_key)?
.unwrap_or(Value::Undefined);
target.define_data_key(&as_key, value);
}
Ok(())
}
// -- iteration ---------------------------------------------------------
/// One object's own keys in enumeration order: array indices ascending, then the
/// rest in insertion order.
///
/// An index is ASCII, so a key with no `&str` form is never one. The flag says
/// whether the key is enumerable, which the caller resolves per key because an
/// array's elements are not in the same store as its named properties.
fn order_own_keys(own: Vec<(crate::value::Wtf8, bool)>) -> Vec<(crate::value::Wtf8, bool)> {
// An index can arrive twice, once from the array's element store and once from
// its property map, so the numeric keys go through a map that keeps the
// enumerable answer if either source gave one.
let mut numeric: std::collections::BTreeMap<u32, bool> = std::collections::BTreeMap::new();
for (key, enumerable) in &own {
if let Some(index) = key.as_str().and_then(array_index) {
let entry = numeric.entry(index).or_insert(false);
*entry = *entry || *enumerable;
}
}
let mut ordered: Vec<(crate::value::Wtf8, bool)> = numeric
.into_iter()
.map(|(index, enumerable)| (crate::value::Wtf8::new(index.to_string()), enumerable))
.collect();
ordered.extend(
own.into_iter()
.filter(|(key, _)| key.as_str().and_then(array_index).is_none()),
);
ordered
}
/// Build the key list a `for-in` loop walks.
///
/// The order is the specification's: own properties before inherited ones, and
/// within one object the integer-like keys in ascending numeric order ahead of the
/// string keys in insertion order. A shadowed key only appears once, and a key that
/// is a non-enumerable own property anywhere nearer is skipped entirely.
fn for_in_record(&mut self, value: &Value) -> JsResult<Gc<JsObject>> {
let record = self.new_object(None);
let mut keys: Vec<crate::value::Wtf8> = Vec::new();
// A proxy supplies its own key list, and — the reason this is not just another
// branch of the ordinary walk — *re-decides* whether each key is enumerable as
// the loop goes. `record.__proxy` marks that, and `for_in_advance` re-reads the
// descriptor there.
if let Value::Object(object) = value
&& object.proxy_state().is_some()
{
let own = self
.proxy_own_keys(object)?
.unwrap_or_else(|| object.own_property_keys());
let mut ordered: Vec<PropertyKey> = Vec::with_capacity(own.len());
let mut numeric: Vec<u32> = Vec::new();
let mut rest: Vec<PropertyKey> = Vec::new();
for key in own {
match key.as_str().and_then(array_index) {
Some(index) => numeric.push(index),
None => rest.push(key),
}
}
numeric.sort_unstable();
ordered.extend(
numeric
.into_iter()
.map(|index| PropertyKey::from(index.to_string())),
);
ordered.extend(rest);
for (index, key) in ordered.into_iter().enumerate() {
record.define_data(&index.to_string(), self.key_value(&key));
}
record.define_data("__index", Value::Int(0));
// The mark that makes `for_in_advance` re-ask the proxy per key.
record.define_data("__proxy", Value::Object(object.clone()));
return Ok(record);
}
if let Value::Object(object) = value {
let mut chain = vec![object.clone()];
chain.extend(object.chain());
// Every own string key the walk has seen, enumerable or not. A
// non-enumerable own property still hides a same-named enumerable one
// further up the chain, so `Object.defineProperty(a, "x", {value: 1})`
// keeps `x` out of the loop even when the prototype has one.
let mut visited: Vec<crate::value::Wtf8> = Vec::new();
for object in chain {
let mut own: Vec<(crate::value::Wtf8, bool)> = object
.own_property_keys()
.iter()
.filter_map(|key| key.as_wtf8().cloned())
.map(|key| {
let enumerable = object
.own_property(&key.to_string_lossy())
.map(|property| property.enumerable)
.unwrap_or(false);
(key, enumerable)
})
.collect();
if let Some(length) = object.array_length() {
own.retain(|(key, _)| key.as_str() != Some("length"));
for index in 0..length {
if object.array_get(index).is_some() {
own.push((crate::value::Wtf8::new(index.to_string()), true));
}
}
}
// Integer-like keys come first in ascending order, then the rest in
// insertion order — and the ordering is *per object*, not for the chain
// as a whole: one pass over every collected key lifted a prototype's
// `"4"` above the target's own `x`/`y`, which is not the order
// `for…in` reports.
for (key, enumerable) in Self::order_own_keys(own) {
if visited.contains(&key) {
continue;
}
visited.push(key.clone());
if enumerable {
keys.push(key);
}
}
}
}
for (index, key) in keys.into_iter().enumerate() {
record.define_data(&index.to_string(), self.string_wtf8(key));
}
record.define_data("__index", Value::Int(0));
Ok(record)
}
/// The next key of a `for…in` record, `None` when the loop is done.
///
/// For a proxy the descriptor is re-read on *every* step rather than filtered once up
/// front: the spec's `EnumerateObjectProperties` asks `[[GetOwnProperty]]` per key as
/// the loop reaches it, so a handler that stops reporting a key partway through drops
/// it from the rest of the iteration. Snapshotting the list instead kept yielding a
/// key the handler had already withdrawn.
fn for_in_advance(&mut self, record: &Value) -> JsResult<Option<String>> {
let Value::Object(record) = record else {
return Ok(None);
};
let proxy = record
.own_property("__proxy")
.and_then(|property| property.value);
loop {
let index = record
.own_property("__index")
.and_then(|property| property.value)
.map(|value| value.to_int32())
.unwrap_or(0);
let Some(candidate) = record
.own_property(&index.to_string())
.and_then(|property| property.value)
else {
return Ok(None);
};
record.define_data("__index", Value::Int(index + 1));
let text = candidate.to_js_string();
let Some(Value::Object(proxy)) = &proxy else {
return Ok(Some(text));
};
// The candidate is already a key string here, so the lookup does not need a
// second `ToPropertyKey` conversion.
let key = PropertyKey::from(text.as_str());
let live = match self.proxy_get_own_property(proxy, &key)? {
Some(descriptor) => descriptor,
// Not a proxy after all, or it lost its state: fall back to the target.
None => proxy.own_property_key(&key),
};
if live.is_some_and(|property| property.enumerable) {
return Ok(Some(text));
}
// Withdrawn or non-enumerable: this key is skipped, and the loop looks at the
// next one.
}
}
/// The iterator for `for-of`: the object and its `next` method.
///
/// An object that already carries a `next` method is used as it is — that is what
/// `arr.values()` returns — and only arrays and strings get the built-in
/// sequence iterator.
/// Whether `value` has a callable `Symbol.iterator`, i.e. `GetMethod` succeeds.
///
/// Callers that must choose between the iterable and the array-like protocol — the
/// typed-array constructor is the main one — need to ask *before* committing to one.
pub fn has_iterator(&mut self, value: &Value) -> JsResult<bool> {
let key = self.realm.well_known_key(WellKnown::Iterator);
let method = self.read_field_key(value, &key)?;
if matches!(method, Value::Undefined | Value::Null) {
return Ok(false);
}
if !matches!(&method, Value::Object(object) if object.is_callable()) {
return Err(Signal::Throw(
self.make_error("TypeError", "Symbol.iterator is not callable"),
));
}
Ok(true)
}
/// Collect everything an iterable yields, closing it if the walk is abandoned.
///
/// The cap is a guard against a generator that never ends: an unbounded collect would
/// be an out-of-memory abort rather than a catchable error.
pub fn collect_iterable(&mut self, value: &Value) -> JsResult<Vec<Value>> {
const MAX_ITEMS: usize = 1 << 24;
let iterator = self.get_iterator(value)?;
let mut out = Vec::new();
loop {
let (element, done) = self.iterator_step(&iterator)?;
if done {
return Ok(out);
}
out.push(element);
if out.len() > MAX_ITEMS {
return Err(self.range_error("iterable is too long to collect"));
}
}
}
/// `OrdinaryCreateFromConstructor(new.target, default_proto)`.
///
/// A native constructor calls this once it has validated its arguments, which is the
/// order the spec uses for `Promise`, the typed arrays and `DataView`: a bad argument is
/// a `TypeError` *before* `new.target.prototype` is read, so a throwing getter there is
/// never reached. The prototype comes from `new.target` when it is an object, and from
/// `default_proto` otherwise.
pub fn new_ctor_instance(&mut self, default_proto: Gc<JsObject>) -> JsResult<Gc<JsObject>> {
let Some(target) = self.pending_new_target.clone() else {
return Ok(self.new_object(Some(default_proto)));
};
let Some(target) = target.as_object().cloned() else {
return Ok(self.new_object(Some(default_proto)));
};
let proto = match self.get_property(&target, "prototype")? {
Some(Value::Object(proto)) => proto,
_ => default_proto,
};
Ok(self.new_object(Some(proto)))
}
/// Whether this call is a construction, i.e. `new.target` is defined.
///
/// A native constructor receives `this = undefined` (it builds its own instance), so it
/// cannot use "`this` is not an object" to tell `Ctor()` from `new Ctor()` — this can.
/// The native function currently executing, if any.
pub fn current_native(&self) -> Option<Gc<JsObject>> {
self.current_native.clone()
}
/// Whether this call is a construction, i.e. `new.target` is defined.
pub fn is_constructing(&self) -> bool {
self.pending_new_target.is_some()
}
/// The `TypeError` a constructor too primitive to have a wrapper raises.
///
/// `BigInt` and `Symbol` are *constructors* as far as `[[Construct]]` is concerned —
/// the reference creates both with `JS_CFUNC_constructor_or_func` — and the refusal
/// happens inside the native function, which is why the trace carries an
/// `at BigInt (native)` frame. The message names `new.target`, not the callee: a
/// `class B extends BigInt {}` constructed as `new B(1)` reports `B is not a
/// constructor`, and an unnamed one reports the leading space of `" is not a
/// constructor"` because the reference formats the empty name as it is.
///
/// It is also the answer for a `new.target` that cannot be named at all — not a
/// function, or a throwing `name` getter — where the reference falls back to the bare
/// `not a constructor`.
pub fn not_a_constructor_error(&mut self) -> Signal {
let target = self.pending_new_target.clone().unwrap_or(Value::Undefined);
self.not_a_constructor_error_for(target)
}
/// The same `TypeError` for an explicit target, for the callers that check
/// `IsConstructor` themselves — `Reflect.construct` does, on its third argument.
pub fn not_a_constructor_error_for(&mut self, target: Value) -> Signal {
let target = match target {
Value::Object(target) if target.is_callable() => target,
_ => return self.type_error("not a constructor"),
};
let name = match self.get_property(&target, "name") {
Ok(Some(value)) => value.to_js_string(),
_ => return self.type_error("not a constructor"),
};
self.type_error(format!("{name} is not a constructor"))
}
/// Whether the pending `new.target` needs its own `prototype` honoured by the constructor.
///
/// True whenever `new.target` is not the callee itself, or when it has no usable
/// `prototype` — the cases where reading the property is observable at all.
pub fn new_target_is_custom(&mut self, callee: &Gc<JsObject>) -> bool {
match &self.pending_new_target {
Some(Value::Object(target)) => !target.ptr_eq(callee),
Some(_) => true,
None => false,
}
}
/// The symbol key for one of the engine's own hidden slots.
///
/// Cached on the realm, so every object of a kind shares one key and a slot can be
/// read back by name.
pub fn internal_slot(&self, name: &'static str) -> PropertyKey {
if let Some(Value::Symbol(symbol)) = self.realm.internal_slots.borrow().get(name) {
return PropertyKey::Symbol(symbol.clone());
}
let symbol = Gc::new(
self.heap,
crate::value::JsSymbol::new(Some(name.to_string())),
);
self.realm
.internal_slots
.borrow_mut()
.insert(name, Value::Symbol(symbol.clone()));
PropertyKey::Symbol(symbol)
}
/// Read a hidden slot, or `None` when the object has none of that name.
pub fn internal_get(&self, object: &Gc<JsObject>, name: &'static str) -> Option<Value> {
let slots = self.realm.internal_slots.borrow();
let Value::Symbol(symbol) = slots.get(name)? else {
return None;
};
object
.own_property_key(&PropertyKey::Symbol(symbol.clone()))
.and_then(|property| property.value)
}
/// Define a hidden slot.
pub fn internal_set(&self, object: &Gc<JsObject>, name: &'static str, value: Value) {
let key = self.internal_slot(name);
object.define_key(&key, Property::with_attributes(value, false, false));
}
/// `GetIterator(value)`: the real `Symbol.iterator` protocol.
///
/// The method is looked up on the value (through its prototype), called with no
/// arguments, and its result becomes the iterator. A generator gets here through
/// `%GeneratorPrototype%[Symbol.iterator]`, which answers `this`; an array through
/// `Array.prototype[Symbol.iterator]`, which builds a one-shot index iterator.
///
/// This used to be a shortcut — "has a callable `next`, or is an array" — because
/// there were no symbols to key the protocol on. Now that there are, the shortcut is
/// gone: a `for…of` over `null`, over a plain object, or over an object whose
/// `Symbol.iterator` is not callable all report the same `TypeError` the reference
/// does, and a user-defined iterable works without a special case.
/// `GetIterator(value, async)`: `Symbol.asyncIterator`, falling back to
/// `Symbol.iterator`.
///
/// The fallback is what makes `for await (x of [1,2])` work: a plain synchronous
/// iterable is legal there, and each value it produces is simply already settled by
/// the time the `await` around it runs.
fn get_async_iterator(&mut self, value: &Value) -> JsResult<Iterator> {
let async_key = self.realm.well_known_key(WellKnown::AsyncIterator);
let method = self.read_field_key(value, &async_key)?;
// §7.4.2 GetIterator with `async` reads `@@asyncIterator` first, and only falls back
// to the synchronous `@@iterator` when that is `undefined` or `null`. A method that
// is *present but not callable* is a TypeError — it does not mean "try the other
// one". Treating it as absent read `Symbol.iterator` too and ran whatever it found,
// which is the opposite of what the test's `get [Symbol.iterator]() { throw }`
// guard is there to catch.
let absent = matches!(method, Value::Undefined | Value::Null);
if !absent {
let Value::Object(object) = &method else {
return Err(Signal::Throw(
self.make_error("TypeError", "value is not iterable"),
));
};
if !object.is_callable() {
return Err(Signal::Throw(
self.make_error("TypeError", "value is not iterable"),
));
}
}
if absent {
return self.get_iterator(value);
}
let iterator = self.call_value(method, value.clone(), Vec::new())?;
if !matches!(iterator, Value::Object(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "iterator is not an object"),
));
}
let next = self.read_field(&iterator, "next")?;
Ok(Iterator {
object: iterator,
next,
})
}
pub(crate) fn get_iterator(&mut self, value: &Value) -> JsResult<Iterator> {
let key = self.realm.well_known_key(WellKnown::Iterator);
let method = self.read_field_key(value, &key)?;
let Value::Object(function) = &method else {
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("{} is not iterable", value.to_js_string()),
)));
};
if !function.is_callable() {
return Err(Signal::Throw(self.make_error(
"TypeError",
&format!("{} is not iterable", value.to_js_string()),
)));
}
let iterator = self.call_value(method, value.clone(), Vec::new())?;
// The spec requires an object here; a primitive iterator has no `next` to call
// and would fail one step later with a confusing message.
if !matches!(iterator, Value::Object(_)) {
return Err(Signal::Throw(
self.make_error("TypeError", "iterator is not an object"),
));
}
let next = self.read_field(&iterator, "next")?;
Ok(Iterator {
object: iterator,
next,
})
}
/// Drain anything with a `Symbol.iterator` method, or answer `None` when the value is
/// not iterable.
///
/// Shared so that `Array.from`, the `Promise` combinators and `for…of` all agree on
/// what "iterable" means. A `Map` and a `Set` have no `length`, so an array-like-only
/// reader silently produced an empty array for both — `Array.from(new Set([1,2]))`
/// was `[]`.
pub fn iterable_values(&mut self, value: &Value) -> JsResult<Option<Vec<Value>>> {
let key = self.realm.well_known_key(WellKnown::Iterator);
let method = self.read_field_key(value, &key)?;
if !matches!(&method, Value::Object(object) if object.is_callable()) {
return Ok(None);
}
let iterator = self.get_iterator(value)?;
let mut out = Vec::new();
loop {
let (element, done) = self.iterator_step(&iterator)?;
if done {
return Ok(Some(out));
}
out.push(element);
}
}
/// One step of an internal iterator: the value and whether it is exhausted.
pub(crate) fn iterator_step(&mut self, iterator: &Iterator) -> JsResult<(Value, bool)> {
// A real iterator (`next` is a function) answers with a result object.
if matches!(&iterator.next, Value::Object(object) if object.is_callable()) {
let result =
self.call_value(iterator.next.clone(), iterator.object.clone(), Vec::new())?;
let Some(result) = result.as_object().cloned() else {
return Err(self.type_error("iterator result is not an object"));
};
let done = self
.get_property(&result, "done")?
.is_some_and(|value| value.to_boolean());
let value = self
.get_property(&result, "value")?
.unwrap_or(Value::Undefined);
return Ok((value, done));
}
// An iterator whose `next` is not callable is treated as exhausted, which is
// what `for (x of {})` relies on: `for_of_start` has already refused the ones
// that are not iterable at all.
Ok((Value::Undefined, true))
}
/// `[...iterable]`: append everything the iterator yields, and answer the next
/// index.
///
/// This goes through the same iterator machinery `for-of` uses rather than copying
/// an array's elements, because a spread source is any iterable: a string spreads
/// into its characters and an array iterator spreads into its results. Handling
/// only arrays made `[..."ab"]` come out empty with no error at all.
fn append_enumerate(&mut self, array: &Value, iterable: &Value) -> JsResult<i32> {
let Some(target) = array.as_object().cloned() else {
return Ok(0);
};
let start = target.array_length().unwrap_or(0);
let mut added = 0;
let iterator = self.get_iterator(iterable)?;
loop {
let (value, done) = self.iterator_step(&iterator)?;
if done {
break;
}
target.array_set(self.heap, start + added, value);
added += 1;
}
Ok((start + added) as i32)
}
/// How an uncaught exception is reported.
///
/// The reference reports a pending exception with `JS_PrintValue`, the *printer*, not
/// with `ToString` — `js_std_dump_error` is three lines that end in
/// `JS_PrintValue(stderr, …)`. The difference is visible for every value that is not
/// already a string: `throw "s"` prints with its quotes, `throw {a:1}` prints
/// `{ a: 1 }`, `throw [1,2]` prints `[ 1, 2 ]`, and an error prints its stack.
///
/// `ToString` was the wrong primitive here — it is what `error.message` is for, not
/// what a reporter is for. An earlier note claimed `JS_PrintValue` "ends up calling
/// `toString`"; it does call it, but only for objects of a class it does not know how
/// to walk, and never for errors, arrays or plain objects.
///
/// A printer that fails must not take the reporter down with it, so that case falls
/// back to the `ToString` rendering.
pub fn describe_exception(&mut self, value: &Value) -> String {
match crate::print::sprint(self, value) {
Ok(text) => text,
Err(_) => self.to_js_string(value).unwrap_or_else(|_| display(value)),
}
}
/// The file name of whatever is running, innermost frame first.
///
/// The reference's `JS_GetScriptOrModuleName(ctx, 0)`: it walks the stack and returns the
/// innermost frame's file name, without distinguishing a module body from a plain
/// script. `import()` uses it as the base for a relative specifier, which is why
/// `import('./x.js')` inside a script resolves next to the script and not next to the
/// process's working directory.
pub fn script_or_module_name(&self) -> Option<String> {
self.frames
.iter()
.rev()
.find_map(|frame| frame.code.code.filename.clone())
}
/// The current call stack, formatted the way `qjs` prints `error.stack`.
///
/// One ` at name (file:line:col)` line per active frame, innermost first. The
/// position is the one recorded for the instruction each frame is stopped on, which
/// is why the compiler builds a pc-to-position table per function: without it a
/// trace could only name the function, not the line inside it.
///
/// A frame whose code has no positions prints just `(file)`, matching the
/// reference's output for stripped bytecode.
pub fn stack_trace(&self) -> String {
self.stack_trace_skipping(0)
}
/// [`Interp::stack_trace`] with the `skip` innermost frames left out.
fn stack_trace_skipping(&self, skip: usize) -> String {
// One ordered list, innermost first, so `skip` counts the same thing whether the
// frames it leaves out are native or bytecode. Indices rather than references: a
// `Frame` borrows from `self`, and a mixed list of borrows cannot name that lifetime.
let depth = self.frames.len();
let mut order: Vec<(bool, usize)> = Vec::with_capacity(depth + self.native_frames.len());
// A native entered while `at` bytecode frames were live was called *from* the
// innermost of those, so it belongs between frame `at` and frame `at - 1`. Natives
// entered at the current depth are therefore innermost of all.
let push_natives = |order: &mut Vec<(bool, usize)>, wanted: usize| {
for (index, (_, _, at)) in self.native_frames.iter().enumerate().rev() {
if *at == wanted {
order.push((true, index));
}
}
};
push_natives(&mut order, depth);
for index in (0..depth).rev() {
order.push((false, index));
push_natives(&mut order, index);
}
// A `backtrace_barrier` eval hides its caller: keep only what is at or inside the
// barrier. Natives entered at exactly the barrier depth are the eval entry point
// itself (`evalScript (native)`), and the reference leaves those out too.
if let Some(barrier) = self.trace_barriers.last().copied() {
order.retain(|(is_native, index)| match is_native {
true => *index == usize::MAX || self.native_frames[*index].2 > barrier,
false => *index >= barrier,
});
}
let mut out = String::new();
for (is_native, index) in order.into_iter().skip(skip) {
match is_native {
true => {
out.push_str(" at ");
out.push_str(&self.native_frames[index].0);
out.push_str(" (native)\n");
}
false => {
let frame = &self.frames[index];
let code = &frame.code.code;
let file = code.filename.as_deref().unwrap_or("<input>");
// The name comes from the function *object*, not from the compiled code:
// an anonymous function expression is named by the property it is stored
// in (`{valueOf: function(){}}`), which only the object knows.
let bound_name = match &frame.callee {
Value::Object(object) => {
object
.own_property("name")
.and_then(|property| match property.value {
Some(Value::String(text)) if !text.wtf8().is_empty() => {
Some(text.to_string_lossy().into_owned())
}
_ => None,
})
}
_ => None,
};
// No callee object means the body was entered by the host, so the
// name has to come from the compiled code. Two cases differ:
//
// * an *unnamed* function of any kind — the reference reads the
// `name` property, finds it empty, and prints `<anonymous>`;
// * a *module* body, which has no `name` property at all: the
// reference builds that function object with `js_closure2` and
// never runs `js_function_set_properties` (`js_create_module_bytecode_function`),
// so a trace reaching it also prints `<anonymous>` — while a
// *script* body prints `<eval>`, its `name`.
let unnamed = code.name.is_empty() || code.module.is_some();
out.push_str(" at ");
match bound_name.as_deref() {
Some(name) => out.push_str(name),
None if unnamed => out.push_str("<anonymous>"),
None => out.push_str(&code.name),
}
match code.position_at(frame.at as u32) {
Some((line, column)) => {
out.push_str(&format!(" ({file}:{line}:{column})\n"));
}
None => out.push_str(&format!(" ({file})\n")),
}
}
}
}
out
}
/// Give an anonymous function the name of the property it is stored in.
///
/// `{m: function(){}}.m.name` is `"m"`. The name property is not writable, not
/// enumerable and not configurable, and a function that already has a real name
/// keeps it.
///
/// "Already named" means a *non-empty* name: every function object is created with
/// a `name` of `""` so the property exists on all of them, and treating that empty
/// string as a name left every anonymous function nameless — which is how a stack
/// trace came out as `at (file:1:2)` instead of `at valueOf (file:1:2)`.
fn set_function_name(&mut self, function: &Value, name: &str) {
let Value::Object(object) = function else {
return;
};
// `SetFunctionName` applies to *functions* and classes only, and only when they are
// anonymous. Without the callability guard, `{value: someObject}` renamed
// `someObject` to `"value"` — silently mutating an unrelated object that merely sat
// on the right-hand side of a property. That is how a `zipKeyed` result object
// acquired a `name` property, and it applied to `Object.defineProperty` too, which
// has no `NamedEvaluation` at all.
if !object.is_callable() {
return;
}
if let Some(Value::String(existing)) = object.own_property("name").and_then(|p| p.value)
&& !existing.wtf8().is_empty()
{
return;
}
let text = self.string(name);
object.define("name", Property::name_or_length(text));
}
// -- global declarations -----------------------------------------------
/// Declare a global binding, creating its reference cell.
/// Declare a `var` or function global, whose home is the global object.
///
/// Writable and enumerable but not configurable: `globalThis.v` reads it,
/// `for-in` lists it, `delete` refuses. A name that is already there — a built-in
/// such as `print`, or a redeclaration — is left alone.
pub fn declare_var_global(&mut self, name: &str) {
// A strict `eval`'s `var` belongs to the eval's own scope: declaring it on the
// global object is exactly the leak `typeof my_var === "undefined"` after
// `eval("var my_var=2")` rules out.
if let Some(scope) = self.eval_scope.as_mut() {
scope.entry(name.to_string()).or_insert(Value::Undefined);
return;
}
// A sloppy direct eval declares into its *caller's* variable environment. Doing
// this on the global object instead made `eval("var c = 1")` inside a parameter
// list overwrite a global `c`, and made two calls to the same function share one
// binding.
if let Some(cell) = self.declare_eval_local(name) {
cell.set(Value::Undefined);
return;
}
let global = self.realm.global.clone();
if global.own_property(name).is_some() {
return;
}
// A `var` in a *direct* declaration is non-configurable, but one introduced by an
// eval — `(1, eval)("var g")` — is created with `configurable: true`, which is the
// whole point of that trick and what lets `Object.defineProperty` reshape it
// afterwards. `eval_scope` is `Some` while an eval is running.
let from_eval = self.eval_depth > 0 || self.eval_scope.is_some();
global.define(
name,
Property {
value: Some(Value::Undefined),
writable: true,
enumerable: true,
configurable: from_eval,
..Property::default()
},
);
}
/// Declare a *lexical* global binding, which starts in its dead zone.
///
/// Only `let`/`const`/`class` need this: a `var` global is a property of the
/// global object and is created when its initialiser runs.
pub fn declare_global(&mut self, name: &str, is_const: bool) {
if is_const {
self.realm.lexical_consts.insert(name.to_string());
}
if !self.realm.globals.contains_key(name) {
// `Uninitialized` rather than `Undefined`: the binding exists but is not
// readable until its initialiser runs, and that is what `let x` before
// `x = 1` has to observe.
let cell = self.new_cell(Value::Uninitialized);
self.realm.globals.insert(name.to_string(), cell);
self.realm.declared.push(name.to_string());
}
}
}
/// The bytecode and environment of a function object, if it is a bytecode function.
///
/// Extracted before a call so no borrow of the object is held across it.
fn bytecode_of(function: &Gc<JsObject>) -> Option<(Gc<CompiledCode>, Gc<ClosureEnv>)> {
function.with(|data| match &data.kind {
ObjectKind::Function(Callable::Bytecode { code, env }) => Some((code.clone(), env.clone())),
_ => None,
})
}
/// A native call target, extracted before the call so no borrow is held.
enum NativeCall {
Native(NativeFn),
/// A host closure. The `Rc` was cloned out of the function object, so calling it does
/// not hold that object's `RefCell` borrow.
Host(std::rc::Rc<std::cell::RefCell<crate::object::HostFn>>),
Bytecode(Gc<CompiledCode>, Gc<ClosureEnv>),
Bound {
target: Value,
this: Value,
leading: Vec<Value>,
},
/// `Proxy.revocable`'s `revoke`: clear the proxy's slots and answer `undefined`.
Revoker(crate::object::ProxyRef),
/// A promise's `resolve`.
PromiseResolve(Gc<JsObject>),
/// A promise's `reject`.
PromiseReject(Gc<JsObject>),
/// One continuation of an `await`.
AwaitResume {
coroutine: Gc<JsObject>,
promise: Gc<JsObject>,
kind: ResumeKind,
},
/// One element of a `Promise` combinator settling.
CombinatorSettle {
state: Gc<JsObject>,
index: usize,
fulfilled: bool,
},
/// One of `Promise.prototype.finally`'s two shims.
FinallyShim {
callback: Option<Value>,
fulfilled: bool,
},
/// Settle a promise with a fixed outcome.
SettlePromise {
promise: Gc<JsObject>,
value: Value,
rejected: bool,
},
/// Continue an async generator after its `await` settled.
GeneratorResume {
generator: Gc<JsObject>,
promise: Gc<JsObject>,
kind: ResumeKind,
},
/// Read a module namespace's binding out of its cell.
ModuleBinding(Gc<Cell>),
/// Finish a completed async generator's `return(v)` once `v` has settled.
CompletedReturn {
generator: Gc<JsObject>,
promise: Gc<JsObject>,
rejected: bool,
},
/// Answer a pending async-generator method with `{value, done}`.
YieldResult {
generator: Gc<JsObject>,
promise: Gc<JsObject>,
done: bool,
},
/// One step of `AsyncDisposableStack.disposeAsync`.
DisposeStep {
remaining: Gc<JsObject>,
promise: Gc<JsObject>,
error: Value,
rejected: bool,
},
}
/// What a store does when the object refuses the write.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum DeleteFailure {
/// Raise a `TypeError`, which is what strict mode and every built-in does.
Throw,
/// Ignore the write, which is what a sloppy-mode assignment does.
Ignore,
}
/// Whether `object` is a `Date` instance, by the class tag it carries.
///
/// Lives here rather than in `builtins::date` so `ToPrimitive` can ask without the
/// builtins module depending on the interpreter's conversion order.
fn is_date_object(interp: &Interp<'_>, object: &Gc<JsObject>) -> bool {
interp
.internal_get(object, "_class")
.is_some_and(|value| value.to_js_string() == "Date")
}
/// Which type `ToPrimitive` prefers when it converts an object.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Hint {
/// The default hint, which behaves like [`Hint::Number`].
Default,
/// `Number(obj)` and the arithmetic operators.
Number,
/// `String(obj)` and `ToPropertyKey`.
String,
}
/// Which table a binding slot indexes, for the messages that name it.
#[derive(Clone, Copy)]
enum VarSlot {
Loc,
Closure,
}
/// `%IteratorPrototype%[Symbol.iterator]`, which answers the iterator itself.
fn self_iterator(_interp: &mut Interp<'_>, this: Value, _args: &[Value]) -> JsResult<Value> {
Ok(this)
}
/// An internal iterator: the sequence plus a cursor.
pub(crate) struct Iterator {
pub(crate) object: Value,
pub(crate) next: Value,
}
/// The name an accessor function gets: `get a` for a getter, `set a` for a setter,
/// and plain `a` for a method.
///
/// The reference prefixes accessors (`js_method_set_properties`), and the prefix is
/// visible: `{get a(){}}.a.name` is `"get a"`, and a stack trace from inside a getter
/// reads `at get a (file:1:2)`.
fn key_with_accessor_prefix(key: &str, flags: u32) -> String {
match flags & 3 {
1 => format!("get {key}"),
2 => format!("set {key}"),
_ => key.to_string(),
}
}
/// Define a method or accessor property, per the `define_method` flag byte.
///
/// The low two bits pick value/getter/setter and bit 2 marks the property
/// enumerable.
///
/// A getter and a setter arrive as two separate `define_method` instructions, and the
/// second one must only fill in its own half: `{get z(){…}, set z(v){…}}` defines
/// `z` twice, and replacing the property wholesale dropped the getter, so `a.z` read
/// `undefined`. The reference passes `JS_PROP_HAS_GET`/`JS_PROP_HAS_SET` to
/// `JS_DefineProperty`, which leaves the other half alone.
fn define_function_property(object: &Gc<JsObject>, key: &PropertyKey, function: Value, flags: u32) {
let enumerable = flags & 4 != 0;
let kind = flags & 3;
let existing = object.own_property_key(key);
let property = match kind {
1 => Property {
getter: Some(function),
// The setter half, if this literal already defined one.
setter: existing
.filter(Property::is_accessor)
.and_then(|p| p.setter),
enumerable,
configurable: true,
..Property::default()
},
2 => Property {
getter: existing
.filter(Property::is_accessor)
.and_then(|p| p.getter),
setter: Some(function),
enumerable,
configurable: true,
..Property::default()
},
_ => Property {
value: Some(function),
writable: true,
enumerable,
configurable: true,
..Property::default()
},
};
object.define_key(key, property);
}
// ---------------------------------------------------------------------------
// Operand decoding
// ---------------------------------------------------------------------------
/// Decode the instruction at `pc` into `(opcode, operand a, operand b, next pc)`.
///
/// Jump operands are resolved to absolute byte offsets here, because the encoding
/// stores them relative to the byte after the opcode and every use site would
/// otherwise repeat that arithmetic.
fn decode(code: &[u8], pc: usize) -> Option<(Opcode, u32, u32, usize)> {
let op = Opcode::from_u8(*code.get(pc)?)?;
let info = op.info();
let base = pc + 1;
let read_u8 = |offset: usize| -> u32 { code.get(base + offset).copied().unwrap_or(0) as u32 };
let read_u16 = |offset: usize| -> u32 {
let mut value = 0u32;
for index in 0..2 {
value = (value << 8) | code.get(base + offset + index).copied().unwrap_or(0) as u32;
}
value
};
let read_u32 = |offset: usize| -> u32 {
let mut value = 0u32;
for index in 0..4 {
value = (value << 8) | code.get(base + offset + index).copied().unwrap_or(0) as u32;
}
value
};
let jump = |offset: usize, width: usize| -> u32 {
let raw = match width {
1 => read_u8(offset) as u8 as i8 as i64,
2 => read_u16(offset) as u16 as i16 as i64,
_ => read_u32(offset) as i32 as i64,
};
(base as i64 + raw) as u32
};
let (a, b) = match info.format {
OpFormat::None
| OpFormat::NoneInt
| OpFormat::NoneLoc
| OpFormat::NoneArg
| OpFormat::NoneVarRef
| OpFormat::Npopx => (0, 0),
OpFormat::U8 | OpFormat::Loc8 | OpFormat::Const8 => (read_u8(0), 0),
OpFormat::I8 => (read_u8(0) as u8 as i8 as i32 as u32, 0),
OpFormat::Label8 => (jump(0, 1), 0),
OpFormat::U16 | OpFormat::Npop | OpFormat::Loc | OpFormat::Arg | OpFormat::VarRef => {
(read_u16(0), 0)
}
OpFormat::I16 => (read_u16(0) as u16 as i16 as i32 as u32, 0),
OpFormat::Label16 => (jump(0, 2), 0),
OpFormat::NpopU16 => (read_u16(0), read_u16(2)),
OpFormat::U32 | OpFormat::Const | OpFormat::Atom => (read_u32(0), 0),
OpFormat::I32 | OpFormat::Label => (
if info.format == OpFormat::Label {
jump(0, 4)
} else {
read_u32(0) as i32 as u32
},
0,
),
OpFormat::AtomU8 | OpFormat::AtomLabelU8 => (read_u32(0), read_u8(4)),
OpFormat::AtomU16 | OpFormat::AtomLabelU16 | OpFormat::LabelU16 => {
(read_u32(0), read_u16(4))
}
};
Some((op, a, b, pc + info.size as usize))
}
/// How `print` renders a value: the reference's `JS_PrintValue`.
///
/// This is *not* `ToString`. `print({a:1})` shows `{ a: 1 }` and `print([1])` shows
/// `[ 1 ]`, because the host printer dumps the structure rather than converting it.
/// The format is reproduced closely because the test suite compares output text:
///
/// * an empty container has two spaces inside its brackets — `[ ]`, `{ }`;
/// * nesting stops at depth 2 and prints `[Array]` / `[Object]`;
/// * a cycle prints `[circular N]` with `N` the stack index of the object;
/// * an array with an interior hole prints as `idx: value` pairs, while holes at the
/// end print as `<N empty item(s)>`;
/// * a string is quoted inside a container but raw at the top level, which is why
/// `print("x")` and `print(["x"])` differ.
pub fn debug_display(interp: &Interp<'_>, value: &Value) -> String {
// A string argument is printed raw: `js_print` converts it with `ToString`
// instead of dumping it, and the quoting only appears inside a container.
if let Value::String(text) = value {
return text.to_string_lossy().into_owned();
}
let mut printer = Printer {
interp,
out: String::new(),
stack: Vec::new(),
};
printer.value(value, 0);
printer.out
}
/// The internal class name an object carries, when it has one.
fn class_of(interp: &Interp<'_>, object: &Gc<JsObject>) -> Option<String> {
interp
.internal_get(object, "_class")
.map(|value| value.to_js_string())
}
/// A string property, looked up the way `GetProperty` does: own first, then the
/// prototype chain. Needed because the print formatter holds the interpreter
/// immutably and so cannot run getters.
fn inherited(object: &Gc<JsObject>, name: &str) -> Option<String> {
let mut current = Some(object.clone());
// Bounded, because a prototype cycle would otherwise loop forever.
for _ in 0..100 {
let node = current?;
if let Some(property) = node.own_property(name)
&& let Some(value) = property.value
{
return Some(value.to_js_string());
}
current = node.with(|data| data.proto.clone());
}
None
}
/// The `JS_PrintValue` state: output, and the objects currently being printed.
struct Printer<'a, 'rt> {
/// Kept for the class-name lookups that typed arrays and `Date` will need.
#[allow(dead_code)]
interp: &'a Interp<'rt>,
out: String,
stack: Vec<Gc<JsObject>>,
}
impl Printer<'_, '_> {
/// Depth past which a container is summarised by its class name.
const MAX_DEPTH: usize = 2;
fn value(&mut self, value: &Value, level: usize) {
match value {
Value::Undefined
| Value::Uninitialized
| Value::Hole
| Value::CatchOffset(_)
| Value::ReturnAddress(_) => self.out.push_str("undefined"),
Value::Symbol(symbol) => self.out.push_str(&symbol.to_display_string()),
// The printer writes a bigint the way the language spells one, so a dump is
// unambiguous about which kind of number it is showing.
Value::BigInt(big) => {
self.out.push_str(&big.to_decimal_string());
self.out.push('n');
}
Value::Null => self.out.push_str("null"),
Value::Bool(true) => self.out.push_str("true"),
Value::Bool(false) => self.out.push_str("false"),
Value::Int(number) => self.out.push_str(&number.to_string()),
Value::Float(number) => {
self.out
.push_str(&crate::value::number_to_string_for_print(*number));
}
Value::String(text) => self.print_string(&text.to_string_lossy()),
Value::Object(object) => self.object(object, level),
}
}
fn object(&mut self, object: &Gc<JsObject>, level: usize) {
// A cycle is reported rather than followed; the index is where the object
// already sits in the print stack, which is what the reference shows.
if let Some(index) = self.stack.iter().position(|seen| seen.ptr_eq(object)) {
self.out.push_str(&format!("[circular {index}]"));
return;
}
if object.is_callable() {
let name = object
.own_property("name")
.and_then(|property| property.value)
.map(|value| value.to_js_string())
.unwrap_or_default();
if name.is_empty() {
self.out.push_str("[Function (anonymous)]");
} else {
self.out.push_str(&format!("[Function {name}]"));
}
return;
}
if level >= Self::MAX_DEPTH {
let class = if object.is_array() { "Array" } else { "Object" };
self.out.push_str(&format!("[{class}]"));
return;
}
// `Date` and `Error` print as themselves rather than as bags of properties:
// the ISO string, and `Name: message` followed by the trace.
if let Some(class) = class_of(self.interp, object) {
if class == "Date" {
self.date(object);
return;
}
if class == "Error" {
self.error(object);
return;
}
}
self.stack.push(object.clone());
if object.is_array() {
self.array(object, level);
} else {
self.plain_object(object, level);
}
self.stack.pop();
}
/// A Date prints as its ISO string; an invalid one falls through to the plain
/// object form, the way the reference falls back when `toISOString` throws.
fn date(&mut self, object: &Gc<JsObject>) {
let value = self
.interp
.internal_get(object, "_date_value")
.map(|value| value.to_number());
match value.and_then(crate::builtins::date::iso_string) {
Some(text) => self.out.push_str(&text),
None => self.plain_object(object, 0),
}
}
/// `Name: message`, then the trace with its trailing newline removed.
fn error(&mut self, object: &Gc<JsObject>) {
let name = inherited(object, "name").unwrap_or_else(|| "Error".to_string());
self.out.push_str(&name);
if let Some(message) = inherited(object, "message")
&& !message.is_empty()
{
self.out.push_str(": ");
self.out.push_str(&message);
}
if let Some(stack) = inherited(object, "stack") {
self.out.push('\n');
self.out.push_str(stack.trim_end_matches('\n'));
}
}
fn array(&mut self, object: &Gc<JsObject>, level: usize) {
let elements = object.array_elements().unwrap_or_default();
self.out.push_str("[ ");
// An interior hole means the array is "sparse": the reference prints those
// as `index: value` pairs and says nothing about the holes themselves, while
// a hole run at the end becomes a single `<N empty items>` note.
let last_present = elements
.iter()
.rposition(|value| !matches!(value, Value::Hole));
let has_interior_hole = match last_present {
Some(last) => elements[..last]
.iter()
.any(|value| matches!(value, Value::Hole)),
None => false,
};
let mut first = true;
if has_interior_hole {
let mut index = 0;
while index < elements.len() {
let value = &elements[index];
if matches!(value, Value::Hole) {
index += 1;
continue;
}
if !first {
self.out.push_str(", ");
}
first = false;
self.out.push_str(&format!("{index}: "));
self.value(value, level + 1);
index += 1;
}
} else {
for (index, value) in elements.iter().enumerate() {
if matches!(value, Value::Hole) {
continue;
}
if !first {
self.out.push_str(", ");
}
first = false;
let _ = index;
self.value(value, level + 1);
}
if let Some(last) = last_present {
let trailing = elements.len() - last - 1;
if trailing > 0 {
if !first {
self.out.push_str(", ");
}
self.out.push_str(&format!(
"<{trailing} empty item{}>",
if trailing > 1 { "s" } else { "" }
));
}
} else if !elements.is_empty() {
let count = elements.len();
self.out.push_str(&format!(
"<{count} empty item{}>",
if count > 1 { "s" } else { "" }
));
}
}
self.out.push_str(" ]");
}
fn plain_object(&mut self, object: &Gc<JsObject>, level: usize) {
self.out.push_str("{ ");
let mut first = true;
for key in object.own_keys() {
let Some(property) = object.own_property_key(&PropertyKey::from(key.clone())) else {
continue;
};
if !property.enumerable {
continue;
}
if !first {
self.out.push_str(", ");
}
first = false;
self.out
.push_str(&js_compile::disasm::print_atom(&Atom::from_text(
&key.to_string_lossy(),
)));
self.out.push_str(": ");
// An accessor has no value to dump; the reference names the half that
// exists instead of calling it.
if property.is_accessor() {
let text = match (property.getter.is_some(), property.setter.is_some()) {
(true, true) => "[Getter/Setter]",
(false, true) => "[Setter]",
_ => "[Getter]",
};
self.out.push_str(text);
} else {
self.value(&property.value.unwrap_or(Value::Undefined), level + 1);
}
}
self.out.push_str(" }");
}
/// A string inside a container, quoted with the escapes `js_print_string1` uses.
fn print_string(&mut self, text: &str) {
self.out.push('"');
for character in text.chars() {
match character {
'"' => self.out.push_str("\\\""),
'\\' => self.out.push_str("\\\\"),
'\n' => self.out.push_str("\\n"),
'\r' => self.out.push_str("\\r"),
'\t' => self.out.push_str("\\t"),
c if (c as u32) < 0x20 => {
self.out.push_str(&format!("\\u{:04x}", c as u32));
}
c => self.out.push(c),
}
}
self.out.push('"');
}
}
/// How `print` renders a value.
///
/// `ToString`, except that negative zero keeps its sign — the reference's host
/// printer does that while `String(-0)` is `"0"`.
pub fn display(value: &Value) -> String {
match value {
Value::Float(number) => crate::value::number_to_string_for_print(*number),
Value::Uninitialized | Value::CatchOffset(_) | Value::ReturnAddress(_) => {
"undefined".to_string()
}
other => other.to_js_string(),
}
}
// ---------------------------------------------------------------------------
// Numeric operators
// ---------------------------------------------------------------------------
/// `a + b` keeping the `Int` representation when it fits.
fn add_numbers(left: Value, right: Value) -> Value {
match (&left, &right) {
(Value::Int(a), Value::Int(b)) => match a.checked_add(*b) {
Some(sum) => Value::Int(sum),
None => Value::Float(*a as f64 + *b as f64),
},
_ => Value::Float(left.to_number() + right.to_number()),
}
}
/// The arithmetic operators other than `+`, which needs string concatenation.
fn arithmetic(op: Opcode, left: Value, right: Value) -> JsResult<Value> {
use Opcode::*;
let (a, b) = (left.to_number(), right.to_number());
Ok(match op {
Sub => match (&left, &right) {
(Value::Int(x), Value::Int(y)) => match x.checked_sub(*y) {
Some(value) => Value::Int(value),
None => Value::Float(a - b),
},
_ => Value::Float(a - b),
},
Mul => Value::Float(a * b),
Div => Value::Float(a / b),
Mod => Value::Float(js_mod(a, b)),
Pow => Value::Float(a.powf(b)),
And => Value::Int(left.to_int32() & right.to_int32()),
Or => Value::Int(left.to_int32() | right.to_int32()),
Xor => Value::Int(left.to_int32() ^ right.to_int32()),
Shl => Value::Int(left.to_int32().wrapping_shl(right.to_uint32() & 31)),
Sar => Value::Int(left.to_int32().wrapping_shr(right.to_uint32() & 31)),
// `>>>` is the *unsigned* shift, so its result is a uint32 and can exceed
// `i32::MAX`: `(1<<31) >>> 0` is `2147483648`, not `-2147483648`. A value that
// fits the small-integer representation stays an `Int`; the rest become a
// float, which is how the reference keeps `n >>> 0` non-negative.
Shr => {
let shifted = left.to_uint32() >> (right.to_uint32() & 31);
match i32::try_from(shifted) {
Ok(value) => Value::Int(value),
Err(_) => Value::Float(shifted as f64),
}
}
_ => Value::Float(a),
})
}
/// `%`, which follows the sign of the dividend rather than Rust's.
fn js_mod(left: f64, right: f64) -> f64 {
if right == 0.0 || left.is_nan() || right.is_nan() || left.is_infinite() {
return f64::NAN;
}
if right.is_infinite() {
return left;
}
let remainder = left % right;
// `-1 % 3` is `-1` in both languages, so only the exact-zero case needs care:
// the spec makes the sign match the dividend.
if remainder == 0.0 {
0.0f64.copysign(left)
} else {
remainder
}
}
fn negate(value: Value) -> Value {
match value {
Value::Int(0) => Value::Float(-0.0),
Value::Int(i) => Value::Int(-i),
other => Value::Float(-other.to_number()),
}
}
fn increment(value: Value) -> Value {
match value {
Value::Int(i) => match i.checked_add(1) {
Some(next) => Value::Int(next),
None => Value::Float(i as f64 + 1.0),
},
other => Value::Float(other.to_number() + 1.0),
}
}
fn decrement(value: Value) -> Value {
match value {
Value::Int(i) => match i.checked_sub(1) {
Some(next) => Value::Int(next),
None => Value::Float(i as f64 - 1.0),
},
other => Value::Float(other.to_number() - 1.0),
}
}
/// Compare a `BigInt` with an `f64` on their mathematical values.
///
/// Neither side is rounded to the other's precision: the number is split into its integer
/// part (compared as an exact bigint) and its fraction, which is what decides the answer
/// when the integer parts are equal. An infinite value is larger than every bigint.
fn compare_bigint_and_f64(big: &crate::bigint::BigInt, number: f64) -> Option<std::cmp::Ordering> {
use std::cmp::Ordering;
if number.is_nan() {
return None;
}
if number.is_infinite() {
return Some(if number > 0.0 {
Ordering::Less
} else {
Ordering::Greater
});
}
let truncated = number.trunc();
// A number wider than `i128` still has an exact integer part, but no bigint form we can
// build cheaply; the sign of the difference is all that is needed, so compare against
// the bigint's own magnitude instead.
if truncated.abs() >= 1.7e38 {
let big_as_f64 = big.to_f64();
return big_as_f64.partial_cmp(&number);
}
let integer = crate::bigint::BigInt::from_f64_truncated(truncated);
match big.cmp(&integer) {
Ordering::Equal => {
// Same integer part: the fraction decides, and a negative number's fraction is
// below the integer (`-1.5` truncates to `-1`).
if number > truncated {
Some(Ordering::Less)
} else if number < truncated {
Some(Ordering::Greater)
} else {
Some(Ordering::Equal)
}
}
other => Some(other),
}
}