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
use heck::*;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt::Write;
use std::mem;
use wai_bindgen_gen_core::wai_parser::abi::{
    AbiVariant, Bindgen, Bitcast, Instruction, LiftLower, WasmType,
};
use wai_bindgen_gen_core::{wai_parser::*, Direction, Files, Generator};

#[derive(Default)]
pub struct Js {
    src: Source,
    in_import: bool,
    opts: Opts,
    guest_imports: HashMap<String, Imports>,
    guest_exports: HashMap<String, Exports>,
    sizes: SizeAlign,
    intrinsics: BTreeMap<Intrinsic, String>,
    all_intrinsics: BTreeSet<Intrinsic>,
    needs_get_export: bool,
    imported_resources: BTreeSet<ResourceId>,
    exported_resources: BTreeSet<ResourceId>,
    needs_ty_option: bool,
    needs_ty_result: bool,
}

#[derive(Default)]
struct Imports {
    freestanding_funcs: Vec<(String, Source)>,
    resource_funcs: BTreeMap<ResourceId, Vec<(String, Source)>>,
}

#[derive(Default)]
struct Exports {
    freestanding_funcs: Vec<Source>,
    resource_funcs: BTreeMap<ResourceId, Vec<Source>>,
}

#[derive(Default, Debug, Clone)]
#[cfg_attr(feature = "structopt", derive(structopt::StructOpt))]
pub struct Opts {
    #[cfg_attr(feature = "structopt", structopt(long = "no-typescript"))]
    pub no_typescript: bool,
}

impl Opts {
    pub fn build(self) -> Js {
        let mut r = Js::new();
        r.opts = self;
        r
    }
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
enum Intrinsic {
    ClampGuest,
    ClampHost,
    ClampHost64,
    DataView,
    ValidateGuestChar,
    ValidateHostChar,
    ValidateFlags,
    ValidateFlags64,
    /// Implementation of https://tc39.es/ecma262/#sec-tostring.
    ToString,
    I32ToF32,
    F32ToI32,
    I64ToF64,
    F64ToI64,
    Utf8Decoder,
    Utf8Encode,
    Utf8EncodedLen,
    Slab,
    Promises,
    WithCurrentPromise,
    ThrowInvalidBool,
}

impl Intrinsic {
    fn name(&self) -> &'static str {
        match self {
            Intrinsic::ClampGuest => "clamp_guest",
            Intrinsic::ClampHost => "clamp_host",
            Intrinsic::ClampHost64 => "clamp_host64",
            Intrinsic::DataView => "data_view",
            Intrinsic::ValidateGuestChar => "validate_guest_char",
            Intrinsic::ValidateHostChar => "validate_host_char",
            Intrinsic::ValidateFlags => "validate_flags",
            Intrinsic::ValidateFlags64 => "validate_flags64",
            Intrinsic::ToString => "to_string",
            Intrinsic::F32ToI32 => "f32ToI32",
            Intrinsic::I32ToF32 => "i32ToF32",
            Intrinsic::F64ToI64 => "f64ToI64",
            Intrinsic::I64ToF64 => "i64ToF64",
            Intrinsic::Utf8Decoder => "UTF8_DECODER",
            Intrinsic::Utf8Encode => "utf8_encode",
            Intrinsic::Utf8EncodedLen => "utf8_encoded_len",
            Intrinsic::Slab => "Slab",
            Intrinsic::Promises => "PROMISES",
            Intrinsic::WithCurrentPromise => "with_current_promise",
            Intrinsic::ThrowInvalidBool => "throw_invalid_bool",
        }
    }
}

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

    fn abi_variant(dir: Direction) -> AbiVariant {
        // This generator uses a reversed mapping! In the JS host-side
        // bindings, we don't use any extra adapter layer between guest wasm
        // modules and the host. When the guest imports functions using the
        // `GuestImport` ABI, the host directly implements the `GuestImport`
        // ABI, even though the host is *exporting* functions. Similarly, when
        // the guest exports functions using the `GuestExport` ABI, the host
        // directly imports them with the `GuestExport` ABI, even though the
        // host is *importing* functions.
        match dir {
            Direction::Import => AbiVariant::GuestExport,
            Direction::Export => AbiVariant::GuestImport,
        }
    }

    fn array_ty(&self, iface: &Interface, ty: &Type) -> Option<&'static str> {
        match ty {
            Type::Unit | Type::Bool => None,
            Type::U8 => Some("Uint8Array"),
            Type::S8 => Some("Int8Array"),
            Type::U16 => Some("Uint16Array"),
            Type::S16 => Some("Int16Array"),
            Type::U32 => Some("Uint32Array"),
            Type::S32 => Some("Int32Array"),
            Type::U64 => Some("BigUint64Array"),
            Type::S64 => Some("BigInt64Array"),
            Type::Float32 => Some("Float32Array"),
            Type::Float64 => Some("Float64Array"),
            Type::Char => None,
            Type::Handle(_) => None,
            Type::String => None,
            Type::Id(id) => match &iface.types[*id].kind {
                TypeDefKind::Type(t) => self.array_ty(iface, t),
                _ => None,
            },
        }
    }

    fn print_ty(&mut self, iface: &Interface, ty: &Type) {
        match ty {
            Type::Unit => self.src.ts("void"),
            Type::Bool => self.src.ts("boolean"),
            Type::U8
            | Type::S8
            | Type::U16
            | Type::S16
            | Type::U32
            | Type::S32
            | Type::Float32
            | Type::Float64 => self.src.ts("number"),
            Type::U64 | Type::S64 => self.src.ts("bigint"),
            Type::Char => self.src.ts("string"),
            Type::Handle(id) => self.src.ts(&iface.resources[*id].name.to_camel_case()),
            Type::String => self.src.ts("string"),
            Type::Id(id) => {
                let ty = &iface.types[*id];
                if let Some(name) = &ty.name {
                    return self.src.ts(&name.to_camel_case());
                }
                match &ty.kind {
                    TypeDefKind::Type(t) => self.print_ty(iface, t),
                    TypeDefKind::Tuple(t) => self.print_tuple(iface, t),
                    TypeDefKind::Record(_) => panic!("anonymous record"),
                    TypeDefKind::Flags(_) => panic!("anonymous flags"),
                    TypeDefKind::Enum(_) => panic!("anonymous enum"),
                    TypeDefKind::Union(_) => panic!("anonymous union"),
                    TypeDefKind::Option(t) => {
                        if self.maybe_null(iface, t) {
                            self.needs_ty_option = true;
                            self.src.ts("Option<");
                            self.print_ty(iface, t);
                            self.src.ts(">");
                        } else {
                            self.print_ty(iface, t);
                            self.src.ts(" | null");
                        }
                    }
                    TypeDefKind::Expected(e) => {
                        self.needs_ty_result = true;
                        self.src.ts("Result<");
                        self.print_ty(iface, &e.ok);
                        self.src.ts(", ");
                        self.print_ty(iface, &e.err);
                        self.src.ts(">");
                    }
                    TypeDefKind::Variant(_) => panic!("anonymous variant"),
                    TypeDefKind::List(v) => self.print_list(iface, v),
                    TypeDefKind::Future(_) => todo!("anonymous future"),
                    TypeDefKind::Stream(_) => todo!("anonymous stream"),
                }
            }
        }
    }

    fn print_list(&mut self, iface: &Interface, ty: &Type) {
        match self.array_ty(iface, ty) {
            Some(ty) => self.src.ts(ty),
            None => {
                self.print_ty(iface, ty);
                self.src.ts("[]");
            }
        }
    }

    fn print_tuple(&mut self, iface: &Interface, tuple: &Tuple) {
        self.src.ts("[");
        for (i, ty) in tuple.types.iter().enumerate() {
            if i > 0 {
                self.src.ts(", ");
            }
            self.print_ty(iface, ty);
        }
        self.src.ts("]");
    }

    fn docs_raw(&mut self, docs: &str) {
        self.src.ts("/**\n");
        for line in docs.lines() {
            self.src.ts(&format!(" * {}\n", line));
        }
        self.src.ts(" */\n");
    }

    fn docs(&mut self, docs: &Docs) {
        match &docs.contents {
            Some(docs) => self.docs_raw(docs),
            None => (),
        }
    }

    fn ts_func(&mut self, iface: &Interface, func: &Function) {
        self.docs(&func.docs);

        let mut name_printed = false;
        if let FunctionKind::Static { .. } = &func.kind {
            // static methods in imports are still wired up to an imported host
            // object, but static methods on exports are actually static
            // methods on the resource object.
            if self.in_import {
                name_printed = true;
                self.src.ts(&func.name.to_mixed_case());
            } else {
                self.src.ts("static ");
            }
        }
        if !name_printed {
            self.src.ts(&func.item_name().to_mixed_case());
        }
        self.src.ts("(");

        let param_start = match &func.kind {
            FunctionKind::Freestanding => 0,
            FunctionKind::Static { .. } if self.in_import => 0,
            FunctionKind::Static { .. } => {
                // the 0th argument for exported static methods will be the
                // instantiated interface
                self.src.ts(&iface.name.to_mixed_case());
                self.src.ts(": ");
                self.src.ts(&iface.name.to_camel_case());
                if !func.params.is_empty() {
                    self.src.ts(", ");
                }
                0
            }
            // skip the first parameter on methods which is `this`
            FunctionKind::Method { .. } => 1,
        };

        for (i, (name, ty)) in func.params[param_start..].iter().enumerate() {
            if i > 0 {
                self.src.ts(", ");
            }
            self.src.ts(to_js_ident(&name.to_mixed_case()));
            self.src.ts(": ");
            self.print_ty(iface, ty);
        }
        self.src.ts("): ");
        if func.is_async {
            self.src.ts("Promise<");
        }
        self.print_ty(iface, &func.result);
        if func.is_async {
            self.src.ts(">");
        }
        self.src.ts(";\n");
    }

    fn intrinsic(&mut self, i: Intrinsic) -> String {
        if let Some(name) = self.intrinsics.get(&i) {
            return name.clone();
        }
        // TODO: should select a name that automatically doesn't conflict with
        // anything else being generated.
        self.intrinsics.insert(i, i.name().to_string());
        return i.name().to_string();
    }

    /// Returns whether `null` is a valid value of type `ty`
    fn maybe_null(&self, iface: &Interface, ty: &Type) -> bool {
        self.as_nullable(iface, ty).is_some()
    }

    /// Tests whether `ty` can be represented with `null`, and if it can then
    /// the "other type" is returned. If `Some` is returned that means that `ty`
    /// is `null | <return>`. If `None` is returned that means that `null` can't
    /// be used to represent `ty`.
    fn as_nullable<'a>(&self, iface: &'a Interface, ty: &'a Type) -> Option<&'a Type> {
        let id = match ty {
            Type::Id(id) => *id,
            _ => return None,
        };
        match &iface.types[id].kind {
            // If `ty` points to an `option<T>`, then `ty` can be represented
            // with `null` if `t` itself can't be represented with null. For
            // example `option<option<u32>>` can't be represented with `null`
            // since that's ambiguous if it's `none` or `some(none)`.
            //
            // Note, oddly enough, that `option<option<option<u32>>>` can be
            // represented as `null` since:
            //
            // * `null` => `none`
            // * `{ tag: "none" }` => `some(none)`
            // * `{ tag: "some", val: null }` => `some(some(none))`
            // * `{ tag: "some", val: 1 }` => `some(some(some(1)))`
            //
            // It's doubtful anyone would actually rely on that though due to
            // how confusing it is.
            TypeDefKind::Option(t) => {
                if !self.maybe_null(iface, t) {
                    Some(t)
                } else {
                    None
                }
            }
            TypeDefKind::Type(t) => self.as_nullable(iface, t),
            _ => None,
        }
    }
}

impl Generator for Js {
    fn preprocess_one(&mut self, iface: &Interface, dir: Direction) {
        let variant = Self::abi_variant(dir);
        self.sizes.fill(iface);
        self.in_import = variant == AbiVariant::GuestImport;
    }

    fn type_record(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        record: &Record,
        docs: &Docs,
    ) {
        self.docs(docs);
        self.src
            .ts(&format!("export interface {} {{\n", name.to_camel_case()));
        for field in record.fields.iter() {
            self.docs(&field.docs);
            let (option_str, ty) = self
                .as_nullable(iface, &field.ty)
                .map_or(("", &field.ty), |ty| ("?", ty));
            self.src
                .ts(&format!("{}{}: ", field.name.to_mixed_case(), option_str));
            self.print_ty(iface, ty);
            self.src.ts(",\n");
        }
        self.src.ts("}\n");
    }

    fn type_tuple(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        tuple: &Tuple,
        docs: &Docs,
    ) {
        self.docs(docs);
        self.src
            .ts(&format!("export type {} = ", name.to_camel_case()));
        self.print_tuple(iface, tuple);
        self.src.ts(";\n");
    }

    fn type_flags(
        &mut self,
        _iface: &Interface,
        _id: TypeId,
        name: &str,
        flags: &Flags,
        docs: &Docs,
    ) {
        self.docs(docs);
        let repr = js_flags_repr(flags);
        let ty = repr.ty();
        let suffix = repr.suffix();
        self.src
            .ts(&format!("export type {} = {ty};\n", name.to_camel_case()));
        let name = name.to_shouty_snake_case();
        for (i, flag) in flags.flags.iter().enumerate() {
            let flag = flag.name.to_shouty_snake_case();
            let ident = format!("{name}_{flag}");
            self.src
                .js(&format!("const {ident} = {}{suffix};\n", 1u128 << i));
            self.src
                .ts(&format!("export const {ident} = {}{suffix};\n", 1u128 << i));
            self.src.export(ident);
        }
    }

    fn type_variant(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        variant: &Variant,
        docs: &Docs,
    ) {
        self.docs(docs);
        self.src
            .ts(&format!("export type {} = ", name.to_camel_case()));
        for (i, case) in variant.cases.iter().enumerate() {
            if i > 0 {
                self.src.ts(" | ");
            }
            self.src
                .ts(&format!("{}_{}", name, case.name).to_camel_case());
        }
        self.src.ts(";\n");
        for case in variant.cases.iter() {
            self.docs(&case.docs);
            self.src.ts(&format!(
                "export interface {} {{\n",
                format!("{}_{}", name, case.name).to_camel_case()
            ));
            self.src.ts("tag: \"");
            self.src.ts(&case.name);
            self.src.ts("\",\n");
            if case.ty != Type::Unit {
                self.src.ts("val: ");
                self.print_ty(iface, &case.ty);
                self.src.ts(",\n");
            }
            self.src.ts("}\n");
        }
    }

    fn type_union(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        union: &Union,
        docs: &Docs,
    ) {
        self.docs(docs);
        let name = name.to_camel_case();
        self.src.ts(&format!("export type {name} = "));
        for i in 0..union.cases.len() {
            if i > 0 {
                self.src.ts(" | ");
            }
            self.src.ts(&format!("{name}{i}"));
        }
        self.src.ts(";\n");
        for (i, case) in union.cases.iter().enumerate() {
            self.docs(&case.docs);
            self.src.ts(&format!("export interface {name}{i} {{\n"));
            self.src.ts(&format!("tag: {i},\n"));
            self.src.ts("val: ");
            self.print_ty(iface, &case.ty);
            self.src.ts(",\n");
            self.src.ts("}\n");
        }
    }

    fn type_option(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        payload: &Type,
        docs: &Docs,
    ) {
        self.docs(docs);
        let name = name.to_camel_case();
        self.src.ts(&format!("export type {name} = "));
        if self.maybe_null(iface, payload) {
            self.needs_ty_option = true;
            self.src.ts("Option<");
            self.print_ty(iface, payload);
            self.src.ts(">");
        } else {
            self.print_ty(iface, payload);
            self.src.ts(" | null");
        }
        self.src.ts(";\n");
    }

    fn type_expected(
        &mut self,
        iface: &Interface,
        _id: TypeId,
        name: &str,
        expected: &Expected,
        docs: &Docs,
    ) {
        self.docs(docs);
        let name = name.to_camel_case();
        self.needs_ty_result = true;
        self.src.ts(&format!("export type {name} = Result<"));
        self.print_ty(iface, &expected.ok);
        self.src.ts(", ");
        self.print_ty(iface, &expected.err);
        self.src.ts(">;\n");
    }

    fn type_enum(
        &mut self,
        _iface: &Interface,
        _id: TypeId,
        name: &str,
        enum_: &Enum,
        docs: &Docs,
    ) {
        // The complete documentation for this enum, including documentation for variants.
        let mut complete_docs = String::new();

        if let Some(docs) = &docs.contents {
            complete_docs.push_str(docs);
            // Add a gap before the `# Variants` section.
            complete_docs.push('\n');
        }

        writeln!(complete_docs, "# Variants").unwrap();

        for case in enum_.cases.iter() {
            writeln!(complete_docs).unwrap();
            writeln!(complete_docs, "## `\"{}\"`", case.name).unwrap();

            if let Some(docs) = &case.docs.contents {
                writeln!(complete_docs).unwrap();
                complete_docs.push_str(docs);
            }
        }

        self.docs_raw(&complete_docs);

        self.src
            .ts(&format!("export type {} = ", name.to_camel_case()));
        for (i, case) in enum_.cases.iter().enumerate() {
            if i != 0 {
                self.src.ts(" | ");
            }
            self.src.ts(&format!("\"{}\"", case.name));
        }
        self.src.ts(";\n");
    }

    fn type_resource(&mut self, _iface: &Interface, ty: ResourceId) {
        if !self.in_import {
            self.exported_resources.insert(ty);
        }
    }

    fn type_alias(&mut self, iface: &Interface, _id: TypeId, name: &str, ty: &Type, docs: &Docs) {
        self.docs(docs);
        self.src
            .ts(&format!("export type {} = ", name.to_camel_case()));
        self.print_ty(iface, ty);
        self.src.ts(";\n");
    }

    fn type_list(&mut self, iface: &Interface, _id: TypeId, name: &str, ty: &Type, docs: &Docs) {
        self.docs(docs);
        self.src
            .ts(&format!("export type {} = ", name.to_camel_case()));
        self.print_list(iface, ty);
        self.src.ts(";\n");
    }

    fn type_builtin(&mut self, iface: &Interface, _id: TypeId, name: &str, ty: &Type, docs: &Docs) {
        drop((iface, _id, name, ty, docs));
    }

    // As with `abi_variant` above, we're generating host-side bindings here
    // so a user "export" uses the "guest import" ABI variant on the inside of
    // this `Generator` implementation.
    fn export(&mut self, iface: &Interface, func: &Function) {
        let prev = mem::take(&mut self.src);

        let sig = iface.wasm_signature(AbiVariant::GuestImport, func);
        let params = (0..sig.params.len())
            .map(|i| format!("arg{}", i))
            .collect::<Vec<_>>();
        self.src
            .js(&format!("function({}) {{\n", params.join(", ")));
        self.ts_func(iface, func);

        let mut f = FunctionBindgen::new(self, false, params);
        iface.call(
            AbiVariant::GuestImport,
            LiftLower::LiftArgsLowerResults,
            func,
            &mut f,
        );

        let FunctionBindgen {
            src,
            needs_memory,
            needs_realloc,
            needs_free,
            ..
        } = f;

        if needs_memory {
            self.needs_get_export = true;
            // TODO: hardcoding "memory"
            self.src.js("const memory = get_export(\"memory\");\n");
        }

        if let Some(name) = needs_realloc {
            self.needs_get_export = true;
            self.src
                .js(&format!("const realloc = get_export(\"{}\");\n", name));
        }

        if let Some(name) = needs_free {
            self.needs_get_export = true;
            self.src
                .js(&format!("const free = get_export(\"{}\");\n", name));
        }
        self.src.js(&src.js);

        if func.is_async {
            // Note that `catch_closure` here is defined by the `CallInterface`
            // instruction.
            self.src.js("}, catch_closure);\n"); // `.then` block
            self.src.js("});\n"); // `with_current_promise` block.
        }
        self.src.js("}");

        let src = mem::replace(&mut self.src, prev);
        let imports = self
            .guest_imports
            .entry(iface.name.to_string())
            .or_default();
        let dst = match &func.kind {
            FunctionKind::Freestanding | FunctionKind::Static { .. } => {
                &mut imports.freestanding_funcs
            }
            FunctionKind::Method { resource, .. } => {
                imports.resource_funcs.entry(*resource).or_default()
            }
        };
        dst.push((func.name.to_string(), src));
    }

    // As with `abi_variant` above, we're generating host-side bindings here
    // so a user "import" uses the "export" ABI variant on the inside of
    // this `Generator` implementation.
    fn import(&mut self, iface: &Interface, func: &Function) {
        let prev = mem::take(&mut self.src);

        let mut params = func
            .params
            .iter()
            .enumerate()
            .map(|(i, _)| format!("arg{}", i))
            .collect::<Vec<_>>();
        let mut sig_start = 0;
        let mut first_is_operand = true;
        let src_object = match &func.kind {
            FunctionKind::Freestanding => "this".to_string(),
            FunctionKind::Static { .. } => {
                self.src.js("static ");
                params.insert(0, iface.name.to_mixed_case());
                first_is_operand = false;
                iface.name.to_mixed_case()
            }
            FunctionKind::Method { .. } => {
                params[0] = "this".to_string();
                sig_start = 1;
                "this._obj".to_string()
            }
        };
        if func.is_async {
            self.src.js("async ");
        }
        self.src.js(&format!(
            "{}({}) {{\n",
            func.item_name().to_mixed_case(),
            params[sig_start..].join(", ")
        ));
        self.ts_func(iface, func);

        if !first_is_operand {
            params.remove(0);
        }
        let mut f = FunctionBindgen::new(self, false, params);
        f.src_object = src_object;
        iface.call(
            AbiVariant::GuestExport,
            LiftLower::LowerArgsLiftResults,
            func,
            &mut f,
        );

        let FunctionBindgen {
            src,
            needs_memory,
            needs_realloc,
            needs_free,
            src_object,
            ..
        } = f;
        if needs_memory {
            // TODO: hardcoding "memory"
            self.src
                .js(&format!("const memory = {}._exports.memory;\n", src_object));
        }

        if let Some(name) = needs_realloc {
            self.src.js(&format!(
                "const realloc = {}._exports[\"{}\"];\n",
                src_object, name
            ));
        }

        if let Some(name) = needs_free {
            self.src.js(&format!(
                "const free = {}._exports[\"{}\"];\n",
                src_object, name
            ));
        }
        self.src.js(&src.js);
        self.src.js("}\n");

        let exports = self
            .guest_exports
            .entry(iface.name.to_string())
            .or_insert_with(Exports::default);

        let func_body = mem::replace(&mut self.src, prev);
        match &func.kind {
            FunctionKind::Freestanding => {
                exports.freestanding_funcs.push(func_body);
            }
            FunctionKind::Static { resource, .. } | FunctionKind::Method { resource, .. } => {
                exports
                    .resource_funcs
                    .entry(*resource)
                    .or_default()
                    .push(func_body);
            }
        }
    }

    fn finish_one(&mut self, iface: &Interface, files: &mut Files) {
        for (module, funcs) in mem::take(&mut self.guest_imports) {
            // TODO: `module.exports` vs `export function`
            let function_name = format!("add{}ToImports", module.to_camel_case());
            self.src.js(&format!(
                "function {function_name}(imports, obj{}) {{\n",
                if self.needs_get_export {
                    ", get_export"
                } else {
                    ""
                },
            ));
            self.src.ts(&format!(
                "export function add{}ToImports(imports: any, obj: {0}{}): void;\n",
                module.to_camel_case(),
                if self.needs_get_export {
                    ", get_export: (name: string) => WebAssembly.ExportValue"
                } else {
                    ""
                },
            ));
            self.src.export(function_name);
            self.src.js(&format!(
                "if (!(\"{0}\" in imports)) imports[\"{0}\"] = {{}};\n",
                module,
            ));

            self.src
                .ts(&format!("export interface {} {{\n", module.to_camel_case()));

            for (name, src) in funcs
                .freestanding_funcs
                .iter()
                .chain(funcs.resource_funcs.values().flatten())
            {
                self.src.js(&format!(
                    "imports[\"{}\"][\"{}\"] = {};\n",
                    module,
                    name,
                    src.js.trim(),
                ));
            }

            for (_, src) in funcs.freestanding_funcs.iter() {
                self.src.ts(&src.ts);
            }

            if !self.imported_resources.is_empty() {
                self.src
                    .js("if (!(\"canonical_abi\" in imports)) imports[\"canonical_abi\"] = {};\n");
            }
            for resource in self.imported_resources.clone() {
                let slab = self.intrinsic(Intrinsic::Slab);
                self.src.js(&format!(
                    "
                        const resources{idx} = new {slab}();
                        imports.canonical_abi[\"resource_drop_{name}\"] = (i) => {{
                            const val = resources{idx}.remove(i);
                            if (obj.drop{camel})
                                obj.drop{camel}(val);
                        }};
                    ",
                    name = iface.resources[resource].name,
                    camel = iface.resources[resource].name.to_camel_case(),
                    idx = resource.index(),
                    slab = slab,
                ));
                self.src.ts(&format!(
                    "drop{}?: (val: {0}) => void;\n",
                    iface.resources[resource].name.to_camel_case()
                ));
            }
            self.src.js("}");
            self.src.ts("}\n");

            for (resource, _) in iface.resources.iter() {
                self.src.ts(&format!(
                    "export interface {} {{\n",
                    iface.resources[resource].name.to_camel_case()
                ));
                if let Some(funcs) = funcs.resource_funcs.get(&resource) {
                    for (_, src) in funcs {
                        self.src.ts(&src.ts);
                    }
                }
                self.src.ts("}\n");
            }
        }
        let imports = mem::take(&mut self.src);

        for (module, exports) in mem::take(&mut self.guest_exports) {
            let module = module.to_camel_case();
            self.src.ts(&format!("export class {} {{\n", module));
            self.src.js(&format!("class {} {{\n", module));
            self.src.export(module);

            self.src.ts("
               /**
                * The WebAssembly instance that this class is operating with.
                * This is only available after the `instantiate` method has
                * been called.
                */
                instance: WebAssembly.Instance;
            ");

            self.src.ts("
               /**
                * Constructs a new instance with internal state necessary to
                * manage a wasm instance.
                *
                * Note that this does not actually instantiate the WebAssembly
                * instance or module, you'll need to call the `instantiate`
                * method below to \"activate\" this class.
                */
                constructor();
            ");
            if !self.exported_resources.is_empty() {
                self.src.js("constructor() {\n");
                let slab = self.intrinsic(Intrinsic::Slab);
                for r in self.exported_resources.iter() {
                    self.src.js(&format!(
                        "this._resource{}_slab = new {}();\n",
                        r.index(),
                        slab
                    ));
                }
                self.src.js("}\n");
            }

            self.src.ts("
               /**
                * This is a low-level method which can be used to add any
                * intrinsics necessary for this instance to operate to an
                * import object.
                *
                * The `import` object given here is expected to be used later
                * to actually instantiate the module this class corresponds to.
                * If the `instantiate` method below actually does the
                * instantiation then there's no need to call this method, but
                * if you're instantiating manually elsewhere then this can be
                * used to prepare the import object for external instantiation.
                */
                addToImports(imports: any): void;
            ");
            self.src.js("addToImports(imports) {\n");
            let any_async = iface.functions.iter().any(|f| f.is_async);
            if !self.exported_resources.is_empty() || any_async {
                self.src
                    .js("if (!(\"canonical_abi\" in imports)) imports[\"canonical_abi\"] = {};\n");
            }
            for r in self.exported_resources.iter() {
                self.src.js(&format!(
                    "
                        imports.canonical_abi['resource_drop_{name}'] = i => {{
                            this._resource{idx}_slab.remove(i).drop();
                        }};
                        imports.canonical_abi['resource_clone_{name}'] = i => {{
                            const obj = this._resource{idx}_slab.get(i);
                            return this._resource{idx}_slab.insert(obj.clone())
                        }};
                        imports.canonical_abi['resource_get_{name}'] = i => {{
                            return this._resource{idx}_slab.get(i)._wasm_val;
                        }};
                        imports.canonical_abi['resource_new_{name}'] = i => {{
                            const registry = this._registry{idx};
                            return this._resource{idx}_slab.insert(new {class}(i, this));
                        }};
                    ",
                    name = iface.resources[*r].name,
                    idx = r.index(),
                    class = iface.resources[*r].name.to_camel_case(),
                ));
            }
            if any_async {
                let promises = self.intrinsic(Intrinsic::Promises);
                self.src.js(&format!(
                    "
                        imports.canonical_abi['async_export_done'] = (ctx, ptr) => {{
                            {}.remove(ctx)(ptr >>> 0)
                        }};
                    ",
                    promises
                ));
            }
            self.src.js("}\n");

            self.src.ts("
                   /**
                    * Initializes this object with the provided WebAssembly
                    * module/instance.
                    *
                    * This is intended to be a flexible method of instantiating
                    * and completion of the initialization of this class. This
                    * method must be called before interacting with the
                    * WebAssembly object.
                    *
                    * The first argument to this method is where to get the
                    * wasm from. This can be a whole bunch of different types,
                    * for example:
                    *
                    * * A precompiled `WebAssembly.Module`
                    * * A typed array buffer containing the wasm bytecode.
                    * * A `Promise` of a `Response` which is used with
                    *   `instantiateStreaming`
                    * * A `Response` itself used with `instantiateStreaming`.
                    * * An already instantiated `WebAssembly.Instance`
                    *
                    * If necessary the module is compiled, and if necessary the
                    * module is instantiated. Whether or not it's necessary
                    * depends on the type of argument provided to
                    * instantiation.
                    *
                    * If instantiation is performed then the `imports` object
                    * passed here is the list of imports used to instantiate
                    * the instance. This method may add its own intrinsics to
                    * this `imports` object too.
                    */
                    instantiate(
                        module: WebAssembly.Module | BufferSource | Promise<Response> | Response | WebAssembly.Instance,
                        imports?: any,
                    ): Promise<void>;
                ");
            self.src.js("
                async instantiate(module, imports) {
                    imports = imports || {};
                    this.addToImports(imports);
            ");

            // With intrinsics prep'd we can now instantiate the module. JS has
            // a ... variety of methods of instantiation, so we basically just
            // try to be flexible here.
            self.src.js("
                if (module instanceof WebAssembly.Instance) {
                    this.instance = module;
                } else if (module instanceof WebAssembly.Module) {
                    this.instance = await WebAssembly.instantiate(module, imports);
                } else if (module instanceof ArrayBuffer || module instanceof Uint8Array) {
                    const { instance } = await WebAssembly.instantiate(module, imports);
                    this.instance = instance;
                } else {
                    const { instance } = await WebAssembly.instantiateStreaming(module, imports);
                    this.instance = instance;
                }
                this._exports = this.instance.exports;
            ");

            // Exported resources all get a finalization registry, and we
            // created them after instantiation so we can pass the raw wasm
            // export as the destructor callback.
            for r in self.exported_resources.iter() {
                self.src.js(&format!(
                    "this._registry{} = new FinalizationRegistry(this._exports['canonical_abi_drop_{}']);\n",
                    r.index(),
                    iface.resources[*r].name,
                ));
            }
            self.src.js("}\n");

            for func in exports.freestanding_funcs.iter() {
                self.src.js(&func.js);
                self.src.ts(&func.ts);
            }
            self.src.ts("}\n");
            self.src.js("}\n");

            for &ty in self.exported_resources.iter() {
                let class_name = iface.resources[ty].name.to_camel_case();
                self.src.js(&format!(
                    "
                        class {class_name} {{
                            constructor(wasm_val, obj) {{
                                this._wasm_val = wasm_val;
                                this._obj = obj;
                                this._refcnt = 1;
                                obj._registry{idx}.register(this, wasm_val, this);
                            }}

                            clone() {{
                                this._refcnt += 1;
                                return this;
                            }}

                            drop() {{
                                this._refcnt -= 1;
                                if (this._refcnt !== 0)
                                    return;
                                this._obj._registry{idx}.unregister(this);
                                const dtor = this._obj._exports['canonical_abi_drop_{}'];
                                const wasm_val = this._wasm_val;
                                delete this._obj;
                                delete this._refcnt;
                                delete this._wasm_val;
                                dtor(wasm_val);
                            }}
                    ",
                    iface.resources[ty].name,
                    idx = ty.index(),
                ));
                self.src.ts(&format!(
                    "
                        export class {class_name} {{
                            // Creates a new strong reference count as a new
                            // object.  This is only required if you're also
                            // calling `drop` below and want to manually manage
                            // the reference count from JS.
                            //
                            // If you don't call `drop`, you don't need to call
                            // this and can simply use the object from JS.
                            clone(): {class_name};

                            // Explicitly indicate that this JS object will no
                            // longer be used. If the internal reference count
                            // reaches zero then this will deterministically
                            // destroy the underlying wasm object.
                            //
                            // This is not required to be called from JS. Wasm
                            // destructors will be automatically called for you
                            // if this is not called using the JS
                            // `FinalizationRegistry`.
                            //
                            // Calling this method does not guarantee that the
                            // underlying wasm object is deallocated. Something
                            // else (including wasm) may be holding onto a
                            // strong reference count.
                            drop(): void;
                    ",
                ));
                self.src.export(class_name);

                if let Some(funcs) = exports.resource_funcs.get(&ty) {
                    for func in funcs {
                        self.src.js(&func.js);
                        self.src.ts(&func.ts);
                    }
                }

                self.src.ts("}\n");
                self.src.js("}\n");
            }
        }

        let exports = mem::take(&mut self.src);

        if mem::take(&mut self.needs_ty_option) {
            self.src
                .ts("export type Option<T> = { tag: \"none\" } | { tag: \"some\", val; T };\n");
        }
        if mem::take(&mut self.needs_ty_result) {
            self.src.ts(
                "export type Result<T, E> = { tag: \"ok\", val: T } | { tag: \"err\", val: E };\n",
            );
        }

        if !self.intrinsics.is_empty() {
            self.src.js("const { ");
            for (i, (intrinsic, name)) in mem::take(&mut self.intrinsics).into_iter().enumerate() {
                if i > 0 {
                    self.src.js(", ");
                }
                self.src.js(intrinsic.name());
                if intrinsic.name() != name {
                    self.src.js(" as ");
                    self.src.js(&name);
                }
                self.all_intrinsics.insert(intrinsic);
            }
            self.src.js(" } = require('./intrinsics.js');\n");
        }

        self.src.js(&imports.js);
        self.src.ts(&imports.ts);
        self.src.js(&exports.js);
        self.src.ts(&exports.ts);

        let mut exported_items = Vec::new();
        exported_items.extend(exports.exported_items);
        exported_items.extend(imports.exported_items);

        self.src.js(&format!(
            "\nmodule.exports = {{ {} }};\n",
            exported_items.join(", ")
        ));

        let src = mem::take(&mut self.src);
        let name = iface.name.to_kebab_case();
        files.push(&format!("{}.js", name), src.js.as_bytes());
        if !self.opts.no_typescript {
            files.push(&format!("{}.d.ts", name), src.ts.as_bytes());
        }
    }

    fn finish_all(&mut self, files: &mut Files) {
        assert!(self.src.ts.is_empty());
        assert!(self.src.js.is_empty());
        self.print_intrinsics();
        assert!(self.src.ts.is_empty());
        files.push("intrinsics.js", self.src.js.as_bytes());
    }
}

struct FunctionBindgen<'a> {
    gen: &'a mut Js,
    tmp: usize,
    src: Source,
    block_storage: Vec<wai_bindgen_gen_core::Source>,
    blocks: Vec<(String, Vec<String>)>,
    in_import: bool,
    needs_memory: bool,
    needs_realloc: Option<String>,
    needs_free: Option<String>,
    params: Vec<String>,
    src_object: String,
}

impl FunctionBindgen<'_> {
    fn new(gen: &mut Js, in_import: bool, params: Vec<String>) -> FunctionBindgen<'_> {
        FunctionBindgen {
            gen,
            tmp: 0,
            src: Source::default(),
            block_storage: Vec::new(),
            blocks: Vec::new(),
            in_import,
            needs_memory: false,
            needs_realloc: None,
            needs_free: None,
            params,
            src_object: "this".to_string(),
        }
    }

    fn tmp(&mut self) -> usize {
        let ret = self.tmp;
        self.tmp += 1;
        ret
    }

    fn clamp_guest<T>(&mut self, results: &mut Vec<String>, operands: &[String], min: T, max: T)
    where
        T: std::fmt::Display,
    {
        let clamp = self.gen.intrinsic(Intrinsic::ClampGuest);
        results.push(format!("{}({}, {}, {})", clamp, operands[0], min, max));
    }

    fn clamp_host<T>(&mut self, results: &mut Vec<String>, operands: &[String], min: T, max: T)
    where
        T: std::fmt::Display,
    {
        let clamp = self.gen.intrinsic(Intrinsic::ClampHost);
        results.push(format!("{}({}, {}, {})", clamp, operands[0], min, max));
    }

    fn clamp_host64<T>(&mut self, results: &mut Vec<String>, operands: &[String], min: T, max: T)
    where
        T: std::fmt::Display,
    {
        let clamp = self.gen.intrinsic(Intrinsic::ClampHost64);
        results.push(format!("{}({}, {}n, {}n)", clamp, operands[0], min, max));
    }

    fn load(&mut self, method: &str, offset: i32, operands: &[String], results: &mut Vec<String>) {
        self.needs_memory = true;
        let view = self.gen.intrinsic(Intrinsic::DataView);
        results.push(format!(
            "{}(memory).{}({} + {}, true)",
            view, method, operands[0], offset,
        ));
    }

    fn store(&mut self, method: &str, offset: i32, operands: &[String]) {
        self.needs_memory = true;
        let view = self.gen.intrinsic(Intrinsic::DataView);
        self.src.js(&format!(
            "{}(memory).{}({} + {}, {}, true);\n",
            view, method, operands[1], offset, operands[0]
        ));
    }

    fn bind_results(&mut self, amt: usize, results: &mut Vec<String>) {
        match amt {
            0 => {}
            1 => {
                self.src.js("const ret = ");
                results.push("ret".to_string());
            }
            n => {
                self.src.js("const [");
                for i in 0..n {
                    if i > 0 {
                        self.src.js(", ");
                    }
                    self.src.js(&format!("ret{}", i));
                    results.push(format!("ret{}", i));
                }
                self.src.js("] = ");
            }
        }
    }
}

impl Bindgen for FunctionBindgen<'_> {
    type Operand = String;

    fn sizes(&self) -> &SizeAlign {
        &self.gen.sizes
    }

    fn push_block(&mut self) {
        let prev = mem::take(&mut self.src.js);
        self.block_storage.push(prev);
    }

    fn finish_block(&mut self, operands: &mut Vec<String>) {
        let to_restore = self.block_storage.pop().unwrap();
        let src = mem::replace(&mut self.src.js, to_restore);
        self.blocks.push((src.into(), mem::take(operands)));
    }

    fn return_pointer(&mut self, _iface: &Interface, _size: usize, _align: usize) -> String {
        unimplemented!()
    }

    fn is_list_canonical(&self, iface: &Interface, ty: &Type) -> bool {
        self.gen.array_ty(iface, ty).is_some()
    }

    fn emit(
        &mut self,
        iface: &Interface,
        inst: &Instruction<'_>,
        operands: &mut Vec<String>,
        results: &mut Vec<String>,
    ) {
        match inst {
            Instruction::GetArg { nth } => results.push(self.params[*nth].clone()),
            Instruction::I32Const { val } => results.push(val.to_string()),
            Instruction::ConstZero { tys } => {
                for t in tys.iter() {
                    match t {
                        WasmType::I64 => results.push("0n".to_string()),
                        WasmType::I32 | WasmType::F32 | WasmType::F64 => {
                            results.push("0".to_string());
                        }
                    }
                }
            }

            // The representation of i32 in JS is a number, so 8/16-bit values
            // get further clamped to ensure that the upper bits aren't set when
            // we pass the value, ensuring that only the right number of bits
            // are transferred.
            Instruction::U8FromI32 => self.clamp_guest(results, operands, u8::MIN, u8::MAX),
            Instruction::S8FromI32 => self.clamp_guest(results, operands, i8::MIN, i8::MAX),
            Instruction::U16FromI32 => self.clamp_guest(results, operands, u16::MIN, u16::MAX),
            Instruction::S16FromI32 => self.clamp_guest(results, operands, i16::MIN, i16::MAX),
            // Use `>>>0` to ensure the bits of the number are treated as
            // unsigned.
            Instruction::U32FromI32 => {
                results.push(format!("{} >>> 0", operands[0]));
            }
            // All bigints coming from wasm are treated as signed, so convert
            // it to ensure it's treated as unsigned.
            Instruction::U64FromI64 => results.push(format!("BigInt.asUintN(64, {})", operands[0])),
            // Nothing to do signed->signed where the representations are the
            // same.
            Instruction::S32FromI32 | Instruction::S64FromI64 => {
                results.push(operands.pop().unwrap())
            }

            // All values coming from the host and going to wasm need to have
            // their ranges validated, since the host could give us any value.
            Instruction::I32FromU8 => self.clamp_host(results, operands, u8::MIN, u8::MAX),
            Instruction::I32FromS8 => self.clamp_host(results, operands, i8::MIN, i8::MAX),
            Instruction::I32FromU16 => self.clamp_host(results, operands, u16::MIN, u16::MAX),
            Instruction::I32FromS16 => self.clamp_host(results, operands, i16::MIN, i16::MAX),
            Instruction::I32FromU32 => {
                self.clamp_host(results, operands, u32::MIN, u32::MAX);
            }
            Instruction::I32FromS32 => self.clamp_host(results, operands, i32::MIN, i32::MAX),
            Instruction::I64FromU64 => self.clamp_host64(results, operands, u64::MIN, u64::MAX),
            Instruction::I64FromS64 => self.clamp_host64(results, operands, i64::MIN, i64::MAX),

            // The native representation in JS of f32 and f64 is just a number,
            // so there's nothing to do here. Everything wasm gives us is
            // representable in JS.
            Instruction::Float32FromF32 | Instruction::Float64FromF64 => {
                results.push(operands.pop().unwrap())
            }

            Instruction::F32FromFloat32 | Instruction::F64FromFloat64 => {
                // Use a unary `+` to cast to a float.
                results.push(format!("+{}", operands[0]));
            }

            // Validate that i32 values coming from wasm are indeed valid code
            // points.
            Instruction::CharFromI32 => {
                let validate = self.gen.intrinsic(Intrinsic::ValidateGuestChar);
                results.push(format!("{}({})", validate, operands[0]));
            }

            // Validate that strings are indeed 1 character long and valid
            // unicode.
            Instruction::I32FromChar => {
                let validate = self.gen.intrinsic(Intrinsic::ValidateHostChar);
                results.push(format!("{}({})", validate, operands[0]));
            }

            Instruction::Bitcasts { casts } => {
                for (cast, op) in casts.iter().zip(operands) {
                    match cast {
                        Bitcast::I32ToF32 => {
                            let cvt = self.gen.intrinsic(Intrinsic::I32ToF32);
                            results.push(format!("{}({})", cvt, op));
                        }
                        Bitcast::F32ToI32 => {
                            let cvt = self.gen.intrinsic(Intrinsic::F32ToI32);
                            results.push(format!("{}({})", cvt, op));
                        }
                        Bitcast::I64ToF64 => {
                            let cvt = self.gen.intrinsic(Intrinsic::I64ToF64);
                            results.push(format!("{}({})", cvt, op));
                        }
                        Bitcast::F64ToI64 => {
                            let cvt = self.gen.intrinsic(Intrinsic::F64ToI64);
                            results.push(format!("{}({})", cvt, op));
                        }
                        Bitcast::I32ToI64 => results.push(format!("BigInt({})", op)),
                        Bitcast::I64ToI32 => results.push(format!("Number({})", op)),
                        Bitcast::I64ToF32 => {
                            let cvt = self.gen.intrinsic(Intrinsic::I32ToF32);
                            results.push(format!("{}(Number({}))", cvt, op));
                        }
                        Bitcast::F32ToI64 => {
                            let cvt = self.gen.intrinsic(Intrinsic::F32ToI32);
                            results.push(format!("BigInt({}({}))", cvt, op));
                        }
                        Bitcast::None => results.push(op.clone()),
                    }
                }
            }

            Instruction::UnitLower => {}
            Instruction::UnitLift => {
                results.push("undefined".to_string());
            }

            Instruction::BoolFromI32 => {
                let tmp = self.tmp();
                self.src
                    .js(&format!("const bool{} = {};\n", tmp, operands[0]));
                let throw = self.gen.intrinsic(Intrinsic::ThrowInvalidBool);
                results.push(format!(
                    "bool{tmp} == 0 ? false : (bool{tmp} == 1 ? true : {throw}())"
                ));
            }
            Instruction::I32FromBool => {
                results.push(format!("{} ? 1 : 0", operands[0]));
            }

            // These instructions are used with handles when we're implementing
            // imports. This means we interact with the `resources` slabs to
            // translate the wasm-provided index into a JS value.
            Instruction::I32FromOwnedHandle { ty } => {
                self.gen.imported_resources.insert(*ty);
                results.push(format!("resources{}.insert({})", ty.index(), operands[0]));
            }
            Instruction::HandleBorrowedFromI32 { ty } => {
                self.gen.imported_resources.insert(*ty);
                results.push(format!("resources{}.get({})", ty.index(), operands[0]));
            }

            // These instructions are used for handles to objects owned in wasm.
            // This means that they're interacting with a wrapper class defined
            // in JS.
            Instruction::I32FromBorrowedHandle { ty } => {
                let tmp = self.tmp();
                self.src
                    .js(&format!("const obj{} = {};\n", tmp, operands[0]));

                // If this is the `this` argument then it's implicitly already valid
                if operands[0] != "this" {
                    self.src.js(&format!(
                        "if (!(obj{} instanceof {})) ",
                        tmp,
                        iface.resources[*ty].name.to_camel_case()
                    ));
                    self.src.js(&format!(
                        "throw new TypeError('expected instance of {}');\n",
                        iface.resources[*ty].name.to_camel_case()
                    ));
                }
                results.push(format!(
                    "{}._resource{}_slab.insert(obj{}.clone())",
                    self.src_object,
                    ty.index(),
                    tmp,
                ));
            }
            Instruction::HandleOwnedFromI32 { ty } => {
                results.push(format!(
                    "{}._resource{}_slab.remove({})",
                    self.src_object,
                    ty.index(),
                    operands[0],
                ));
            }

            Instruction::RecordLower { record, .. } => {
                // use destructuring field access to get each
                // field individually.
                let tmp = self.tmp();
                let mut expr = "const {".to_string();
                for (i, field) in record.fields.iter().enumerate() {
                    if i > 0 {
                        expr.push_str(", ");
                    }
                    let name = format!("v{}_{}", tmp, i);
                    expr.push_str(&field.name.to_mixed_case());
                    expr.push_str(": ");
                    expr.push_str(&name);
                    results.push(name);
                }
                self.src.js(&format!("{} }} = {};\n", expr, operands[0]));
            }

            Instruction::RecordLift { record, .. } => {
                // records are represented as plain objects, so we
                // make a new object and set all the fields with an object
                // literal.
                let mut result = "{\n".to_string();
                for (field, op) in record.fields.iter().zip(operands) {
                    result.push_str(&format!("{}: {},\n", field.name.to_mixed_case(), op));
                }
                result.push('}');
                results.push(result);
            }

            Instruction::TupleLower { tuple, .. } => {
                // Tuples are represented as an array, sowe can use
                // destructuring assignment to lower the tuple into its
                // components.
                let tmp = self.tmp();
                let mut expr = "const [".to_string();
                for i in 0..tuple.types.len() {
                    if i > 0 {
                        expr.push_str(", ");
                    }
                    let name = format!("tuple{}_{}", tmp, i);
                    expr.push_str(&name);
                    results.push(name);
                }
                self.src.js(&format!("{}] = {};\n", expr, operands[0]));
            }

            Instruction::TupleLift { .. } => {
                // Tuples are represented as an array, so we just shove all
                // the operands into an array.
                results.push(format!("[{}]", operands.join(", ")));
            }

            Instruction::FlagsLower { flags, .. } => {
                let repr = js_flags_repr(flags);
                let validate = match repr {
                    JsFlagsRepr::Number => self.gen.intrinsic(Intrinsic::ValidateFlags),
                    JsFlagsRepr::Bigint => self.gen.intrinsic(Intrinsic::ValidateFlags64),
                };
                let op0 = &operands[0];
                let len = flags.flags.len();
                let n = repr.suffix();
                let tmp = self.tmp();
                let mask = (1u128 << len) - 1;
                self.src.js(&format!(
                    "const flags{tmp} = {validate}({op0}, {mask}{n});\n"
                ));
                match repr {
                    JsFlagsRepr::Number => {
                        results.push(format!("flags{}", tmp));
                    }
                    JsFlagsRepr::Bigint => {
                        for i in 0..flags.repr().count() {
                            let i = 32 * i;
                            results.push(format!("Number((flags{tmp} >> {i}n) & 0xffffffffn)",));
                        }
                    }
                }
            }

            Instruction::FlagsLift { flags, .. } => {
                let repr = js_flags_repr(flags);
                let n = repr.suffix();
                let tmp = self.tmp();
                let operand = match repr {
                    JsFlagsRepr::Number => operands[0].clone(),
                    JsFlagsRepr::Bigint => {
                        self.src.js(&format!("let flags{tmp} = 0n;\n"));
                        for (i, op) in operands.iter().enumerate() {
                            let i = 32 * i;
                            self.src
                                .js(&format!("flags{tmp} |= BigInt({op}) << {i}n;\n",));
                        }
                        format!("flags{tmp}")
                    }
                };
                let validate = match repr {
                    JsFlagsRepr::Number => self.gen.intrinsic(Intrinsic::ValidateFlags),
                    JsFlagsRepr::Bigint => self.gen.intrinsic(Intrinsic::ValidateFlags64),
                };
                let len = flags.flags.len();
                let mask = (1u128 << len) - 1;
                results.push(format!("{validate}({operand}, {mask}{n})"));
            }

            Instruction::VariantPayloadName => results.push("e".to_string()),

            Instruction::VariantLower {
                variant,
                results: result_types,
                name,
                ..
            } => {
                let blocks = self
                    .blocks
                    .drain(self.blocks.len() - variant.cases.len()..)
                    .collect::<Vec<_>>();
                let tmp = self.tmp();
                self.src
                    .js(&format!("const variant{} = {};\n", tmp, operands[0]));

                for i in 0..result_types.len() {
                    self.src.js(&format!("let variant{}_{};\n", tmp, i));
                    results.push(format!("variant{}_{}", tmp, i));
                }

                let expr_to_match = format!("variant{}.tag", tmp);

                self.src.js(&format!("switch ({}) {{\n", expr_to_match));
                for (case, (block, block_results)) in variant.cases.iter().zip(blocks) {
                    self.src
                        .js(&format!("case \"{}\": {{\n", case.name.as_str()));
                    if case.ty != Type::Unit {
                        self.src.js(&format!("const e = variant{}.val;\n", tmp));
                    }
                    self.src.js(&block);

                    for (i, result) in block_results.iter().enumerate() {
                        self.src
                            .js(&format!("variant{}_{} = {};\n", tmp, i, result));
                    }
                    self.src.js("break;\n}\n");
                }
                let variant_name = name.to_camel_case();
                self.src.js("default:\n");
                self.src.js(&format!(
                    "throw new RangeError(\"invalid variant specified for {}\");\n",
                    variant_name
                ));
                self.src.js("}\n");
            }

            Instruction::VariantLift { variant, name, .. } => {
                let blocks = self
                    .blocks
                    .drain(self.blocks.len() - variant.cases.len()..)
                    .collect::<Vec<_>>();

                let tmp = self.tmp();

                self.src.js(&format!("let variant{};\n", tmp));
                self.src.js(&format!("switch ({}) {{\n", operands[0]));
                for (i, (case, (block, block_results))) in
                    variant.cases.iter().zip(blocks).enumerate()
                {
                    self.src.js(&format!("case {}: {{\n", i));
                    self.src.js(&block);

                    self.src.js(&format!("variant{} = {{\n", tmp));
                    self.src.js(&format!("tag: \"{}\",\n", case.name.as_str()));
                    assert!(block_results.len() == 1);
                    if case.ty != Type::Unit {
                        self.src.js(&format!("val: {},\n", block_results[0]));
                    } else {
                        assert_eq!(block_results[0], "undefined");
                    }
                    self.src.js("};\n");
                    self.src.js("break;\n}\n");
                }
                let variant_name = name.to_camel_case();
                self.src.js("default:\n");
                self.src.js(&format!(
                    "throw new RangeError(\"invalid variant discriminant for {}\");\n",
                    variant_name
                ));
                self.src.js("}\n");
                results.push(format!("variant{}", tmp));
            }

            Instruction::UnionLower {
                union,
                results: result_types,
                name,
                ..
            } => {
                let blocks = self
                    .blocks
                    .drain(self.blocks.len() - union.cases.len()..)
                    .collect::<Vec<_>>();
                let tmp = self.tmp();
                let op0 = &operands[0];
                self.src.js(&format!("const union{tmp} = {op0};\n"));

                for i in 0..result_types.len() {
                    self.src.js(&format!("let union{tmp}_{i};\n"));
                    results.push(format!("union{tmp}_{i}"));
                }

                self.src.js(&format!("switch (union{tmp}.tag) {{\n"));
                for (i, (_case, (block, block_results))) in
                    union.cases.iter().zip(blocks).enumerate()
                {
                    self.src.js(&format!("case {i}: {{\n"));
                    self.src.js(&format!("const e = union{tmp}.val;\n"));
                    self.src.js(&block);
                    for (i, result) in block_results.iter().enumerate() {
                        self.src.js(&format!("union{tmp}_{i} = {result};\n"));
                    }
                    self.src.js("break;\n}\n");
                }
                let name = name.to_camel_case();
                self.src.js("default:\n");
                self.src.js(&format!(
                    "throw new RangeError(\"invalid union specified for {name}\");\n",
                ));
                self.src.js("}\n");
            }

            Instruction::UnionLift { union, name, .. } => {
                let blocks = self
                    .blocks
                    .drain(self.blocks.len() - union.cases.len()..)
                    .collect::<Vec<_>>();

                let tmp = self.tmp();

                self.src.js(&format!("let union{tmp};\n"));
                self.src.js(&format!("switch ({}) {{\n", operands[0]));
                for (i, (_case, (block, block_results))) in
                    union.cases.iter().zip(blocks).enumerate()
                {
                    assert!(block_results.len() == 1);
                    let block_result = &block_results[0];
                    self.src.js(&format!(
                        "case {i}: {{
                            {block}
                            union{tmp} = {{
                                tag: {i},
                                val: {block_result},
                            }};
                            break;
                        }}\n"
                    ));
                }
                let name = name.to_camel_case();
                self.src.js("default:\n");
                self.src.js(&format!(
                    "throw new RangeError(\"invalid union discriminant for {name}\");\n",
                ));
                self.src.js("}\n");
                results.push(format!("union{tmp}"));
            }

            Instruction::OptionLower {
                payload,
                results: result_types,
                ..
            } => {
                let (mut some, some_results) = self.blocks.pop().unwrap();
                let (mut none, none_results) = self.blocks.pop().unwrap();

                let tmp = self.tmp();
                self.src
                    .js(&format!("const variant{tmp} = {};\n", operands[0]));

                for i in 0..result_types.len() {
                    self.src.js(&format!("let variant{tmp}_{i};\n"));
                    results.push(format!("variant{tmp}_{i}"));

                    let some_result = &some_results[i];
                    let none_result = &none_results[i];
                    some.push_str(&format!("variant{tmp}_{i} = {some_result};\n"));
                    none.push_str(&format!("variant{tmp}_{i} = {none_result};\n"));
                }

                if self.gen.maybe_null(iface, payload) {
                    self.src.js(&format!(
                        "
                        switch (variant{tmp}.tag) {{
                            case \"none\": {{
                                {none}
                                break;
                            }}
                            case \"some\": {{
                                const e = variant{tmp}.val;
                                {some}
                                break;
                            }}
                            default: {{
                                throw new RangeError(\"invalid variant specified for option\");
                            }}
                        }}
                        "
                    ));
                } else {
                    self.src.js(&format!(
                        "
                        switch (variant{tmp}) {{
                            case null: {{
                                {none}
                                break;
                            }}
                            default: {{
                                const e = variant{tmp};
                                {some}
                                break;
                            }}
                        }}
                        "
                    ));
                }
            }

            Instruction::OptionLift { payload, .. } => {
                let (some, some_results) = self.blocks.pop().unwrap();
                let (none, none_results) = self.blocks.pop().unwrap();
                assert!(none_results.len() == 1);
                assert!(some_results.len() == 1);
                let some_result = &some_results[0];
                assert_eq!(none_results[0], "undefined");

                let tmp = self.tmp();

                self.src.js(&format!("let variant{tmp};\n"));
                self.src.js(&format!("switch ({}) {{\n", operands[0]));

                if self.gen.maybe_null(iface, payload) {
                    self.src.js(&format!(
                        "
                            case 0: {{
                                {none}
                                variant{tmp} = {{ tag: \"none\" }};
                                break;
                            }}
                            case 1: {{
                                {some}
                                variant{tmp} = {{ tag: \"some\", val: {some_result} }};
                                break;
                            }}
                        ",
                    ));
                } else {
                    self.src.js(&format!(
                        "
                            case 0: {{
                                {none}
                                variant{tmp} = null;
                                break;
                            }}
                            case 1: {{
                                {some}
                                variant{tmp} = {some_result};
                                break;
                            }}
                        ",
                    ));
                }
                self.src.js("
                    default:
                        throw new RangeError(\"invalid variant discriminant for option\");
                ");
                self.src.js("}\n");
                results.push(format!("variant{tmp}"));
            }

            Instruction::ExpectedLower {
                results: result_types,
                ..
            } => {
                let (mut err, err_results) = self.blocks.pop().unwrap();
                let (mut ok, ok_results) = self.blocks.pop().unwrap();

                let tmp = self.tmp();
                self.src
                    .js(&format!("const variant{tmp} = {};\n", operands[0]));

                for i in 0..result_types.len() {
                    self.src.js(&format!("let variant{tmp}_{i};\n"));
                    results.push(format!("variant{tmp}_{i}"));

                    let ok_result = &ok_results[i];
                    let err_result = &err_results[i];
                    ok.push_str(&format!("variant{tmp}_{i} = {ok_result};\n"));
                    err.push_str(&format!("variant{tmp}_{i} = {err_result};\n"));
                }

                self.src.js(&format!(
                    "
                    switch (variant{tmp}.tag) {{
                        case \"ok\": {{
                            const e = variant{tmp}.val;
                            {ok}
                            break;
                        }}
                        case \"err\": {{
                            const e = variant{tmp}.val;
                            {err}
                            break;
                        }}
                        default: {{
                            throw new RangeError(\"invalid variant specified for expected\");
                        }}
                    }}
                    "
                ));
            }

            Instruction::ExpectedLift { .. } => {
                let (err, err_results) = self.blocks.pop().unwrap();
                let (ok, ok_results) = self.blocks.pop().unwrap();
                let err_result = &err_results[0];
                let ok_result = &ok_results[0];
                let tmp = self.tmp();
                let op0 = &operands[0];
                self.src.js(&format!(
                    "
                    let variant{tmp};
                    switch ({op0}) {{
                        case 0: {{
                            {ok}
                            variant{tmp} = {{ tag: \"ok\", val: {ok_result} }};
                            break;
                        }}
                        case 1: {{
                            {err}
                            variant{tmp} = {{ tag: \"err\", val: {err_result} }};
                            break;
                        }}
                        default: {{
                            throw new RangeError(\"invalid variant discriminant for expected\");
                        }}
                    }}
                    ",
                ));
                results.push(format!("variant{tmp}"));
            }

            // Lowers an enum in accordance with https://webidl.spec.whatwg.org/#es-enumeration.
            Instruction::EnumLower { name, enum_, .. } => {
                let tmp = self.tmp();

                let to_string = self.gen.intrinsic(Intrinsic::ToString);
                self.src
                    .js(&format!("const val{tmp} = {to_string}({});\n", operands[0]));

                // Declare a variable to hold the result.
                self.src.js(&format!("let enum{tmp};\n"));

                self.src.js(&format!("switch (val{tmp}) {{\n"));
                for (i, case) in enum_.cases.iter().enumerate() {
                    self.src.js(&format!(
                        "\
                        case \"{case}\": {{
                            enum{tmp} = {i};
                            break;
                        }}
                        ",
                        case = case.name
                    ));
                }
                self.src.js(&format!("\
                        default: {{
                            throw new TypeError(`\"${{val{tmp}}}\" is not one of the cases of {name}`);
                        }}
                    }}
                "));

                results.push(format!("enum{tmp}"));
            }

            Instruction::EnumLift { name, enum_, .. } => {
                let tmp = self.tmp();

                self.src.js(&format!("let enum{tmp};\n"));

                self.src.js(&format!("switch ({}) {{\n", operands[0]));
                for (i, case) in enum_.cases.iter().enumerate() {
                    self.src.js(&format!(
                        "\
                        case {i}: {{
                            enum{tmp} = \"{case}\";
                            break;
                        }}
                        ",
                        case = case.name
                    ));
                }
                self.src.js(&format!(
                    "\
                        default: {{
                            throw new RangeError(\"invalid discriminant specified for {name}\");
                        }}
                    }}
                    ",
                    name = name.to_camel_case()
                ));

                results.push(format!("enum{tmp}"));
            }

            Instruction::ListCanonLower { element, realloc } => {
                // Lowering only happens when we're passing lists into wasm,
                // which forces us to always allocate, so this should always be
                // `Some`.
                let realloc = realloc.unwrap();
                self.gen.needs_get_export = true;
                self.needs_memory = true;
                self.needs_realloc = Some(realloc.to_string());
                let tmp = self.tmp();

                let size = self.gen.sizes.size(element);
                let align = self.gen.sizes.align(element);
                self.src
                    .js(&format!("const val{} = {};\n", tmp, operands[0]));
                self.src.js(&format!("const len{} = val{0}.length;\n", tmp));
                self.src.js(&format!(
                    "const ptr{} = realloc(0, 0, {}, len{0} * {});\n",
                    tmp, align, size,
                ));
                // TODO: this is the wrong endianness
                self.src.js(&format!(
                    "(new Uint8Array(memory.buffer, ptr{0}, len{0} * {1})).set(new Uint8Array(val{0}.buffer, val{0}.byteOffset, len{0} * {1}));\n",
                    tmp, size,
                ));
                results.push(format!("ptr{}", tmp));
                results.push(format!("len{}", tmp));
            }
            Instruction::ListCanonLift { element, free, .. } => {
                self.needs_memory = true;
                let tmp = self.tmp();
                self.src
                    .js(&format!("const ptr{} = {};\n", tmp, operands[0]));
                self.src
                    .js(&format!("const len{} = {};\n", tmp, operands[1]));
                // TODO: this is the wrong endianness
                let array_ty = self.gen.array_ty(iface, element).unwrap();
                let result = format!(
                    "new {}(memory.buffer.slice(ptr{}, ptr{1} + len{1} * {}))",
                    array_ty,
                    tmp,
                    self.gen.sizes.size(element),
                );
                let align = self.gen.sizes.align(element);
                match free {
                    Some(free) => {
                        self.needs_free = Some(free.to_string());
                        self.src.js(&format!("const list{} = {};\n", tmp, result));
                        self.src
                            .js(&format!("free(ptr{}, len{0}, {});\n", tmp, align));
                        results.push(format!("list{}", tmp));
                    }
                    None => results.push(result),
                }
            }
            Instruction::StringLower { realloc } => {
                // Lowering only happens when we're passing strings into wasm,
                // which forces us to always allocate, so this should always be
                // `Some`.
                let realloc = realloc.unwrap();
                self.gen.needs_get_export = true;
                self.needs_memory = true;
                self.needs_realloc = Some(realloc.to_string());
                let tmp = self.tmp();

                let encode = self.gen.intrinsic(Intrinsic::Utf8Encode);
                self.src.js(&format!(
                    "const ptr{} = {}({}, realloc, memory);\n",
                    tmp, encode, operands[0],
                ));
                let encoded_len = self.gen.intrinsic(Intrinsic::Utf8EncodedLen);
                self.src.js(&format!("const len{tmp} = {encoded_len}();\n"));
                results.push(format!("ptr{}", tmp));
                results.push(format!("len{}", tmp));
            }
            Instruction::StringLift { free } => {
                self.needs_memory = true;
                let tmp = self.tmp();
                self.src
                    .js(&format!("const ptr{} = {};\n", tmp, operands[0]));
                self.src
                    .js(&format!("const len{} = {};\n", tmp, operands[1]));
                let decoder = self.gen.intrinsic(Intrinsic::Utf8Decoder);
                let result = format!(
                    "{}.decode(new Uint8Array(memory.buffer, ptr{}, len{1}))",
                    decoder, tmp,
                );
                match free {
                    Some(free) => {
                        self.needs_free = Some(free.to_string());
                        self.src.js(&format!("const list{} = {};\n", tmp, result));
                        self.src.js(&format!("free(ptr{}, len{0}, 1);\n", tmp));
                        results.push(format!("list{}", tmp));
                    }
                    None => results.push(result),
                }
            }

            Instruction::ListLower { element, realloc } => {
                let realloc = realloc.unwrap();
                let (body, body_results) = self.blocks.pop().unwrap();
                assert!(body_results.is_empty());
                let tmp = self.tmp();
                let vec = format!("vec{}", tmp);
                let result = format!("result{}", tmp);
                let len = format!("len{}", tmp);
                self.needs_realloc = Some(realloc.to_string());
                let size = self.gen.sizes.size(element);
                let align = self.gen.sizes.align(element);

                // first store our vec-to-lower in a temporary since we'll
                // reference it multiple times.
                self.src.js(&format!("const {} = {};\n", vec, operands[0]));
                self.src.js(&format!("const {} = {}.length;\n", len, vec));

                // ... then realloc space for the result in the guest module
                self.src.js(&format!(
                    "const {} = realloc(0, 0, {}, {} * {});\n",
                    result, align, len, size,
                ));

                // ... then consume the vector and use the block to lower the
                // result.
                self.src
                    .js(&format!("for (let i = 0; i < {}.length; i++) {{\n", vec));
                self.src.js(&format!("const e = {}[i];\n", vec));
                self.src
                    .js(&format!("const base = {} + i * {};\n", result, size));
                self.src.js(&body);
                self.src.js("}\n");

                results.push(result);
                results.push(len);
            }

            Instruction::ListLift { element, free, .. } => {
                let (body, body_results) = self.blocks.pop().unwrap();
                let tmp = self.tmp();
                let size = self.gen.sizes.size(element);
                let align = self.gen.sizes.align(element);
                let len = format!("len{}", tmp);
                self.src.js(&format!("const {} = {};\n", len, operands[1]));
                let base = format!("base{}", tmp);
                self.src.js(&format!("const {} = {};\n", base, operands[0]));
                let result = format!("result{}", tmp);
                self.src.js(&format!("const {} = [];\n", result));
                results.push(result.clone());

                self.src
                    .js(&format!("for (let i = 0; i < {}; i++) {{\n", len));
                self.src
                    .js(&format!("const base = {} + i * {};\n", base, size));
                self.src.js(&body);
                assert_eq!(body_results.len(), 1);
                self.src
                    .js(&format!("{}.push({});\n", result, body_results[0]));
                self.src.js("}\n");

                if let Some(free) = free {
                    self.needs_free = Some(free.to_string());
                    self.src
                        .js(&format!("free({}, {} * {}, {});\n", base, len, size, align,));
                }
            }

            Instruction::IterElem { .. } => results.push("e".to_string()),

            Instruction::IterBasePointer => results.push("base".to_string()),

            Instruction::CallWasm {
                iface: _,
                name,
                sig,
            } => {
                self.bind_results(sig.results.len(), results);
                self.src.js(&self.src_object);
                self.src.js("._exports['");
                self.src.js(name);
                self.src.js("'](");
                self.src.js(&operands.join(", "));
                self.src.js(");\n");
            }

            Instruction::CallWasmAsyncExport {
                module: _,
                name,
                params: _,
                results: wasm_results,
            } => {
                self.bind_results(wasm_results.len(), results);
                let promises = self.gen.intrinsic(Intrinsic::Promises);
                self.src.js(&format!(
                    "\
                        await new Promise((resolve, reject) => {{
                            const promise_ctx = {promises}.insert(val => {{
                                if (typeof val !== 'number')
                                    return reject(val);
                                resolve(\
                    ",
                    promises = promises
                ));

                if !wasm_results.is_empty() {
                    self.src.js("[");
                    let operands = &["val".to_string()];
                    let mut results = Vec::new();
                    for (i, result) in wasm_results.iter().enumerate() {
                        if i > 0 {
                            self.src.js(", ");
                        }
                        let method = match result {
                            WasmType::I32 => "getInt32",
                            WasmType::I64 => "getBigInt64",
                            WasmType::F32 => "getFloat32",
                            WasmType::F64 => "getFloat64",
                        };
                        self.load(method, (i * 8) as i32, operands, &mut results);
                        self.src.js(&results.pop().unwrap());
                    }
                    self.src.js("]");
                }

                // Finish the blocks from above
                self.src.js(");\n"); // `resolve(...)`
                self.src.js("});\n"); // `promises.insert(...)`

                let with = self.gen.intrinsic(Intrinsic::WithCurrentPromise);
                self.src.js(&with);
                self.src.js("(promise_ctx, _prev => {\n");
                self.src.js(&self.src_object);
                self.src.js("._exports['");
                self.src.js(name);
                self.src.js("'](");
                for op in operands {
                    self.src.js(op);
                    self.src.js(", ");
                }
                self.src.js("promise_ctx);\n");
                self.src.js("});\n"); // call to `with`
                self.src.js("});\n"); // `await new Promise(...)`
            }

            Instruction::CallInterface { module: _, func } => {
                let call = |me: &mut FunctionBindgen<'_>| match &func.kind {
                    FunctionKind::Freestanding | FunctionKind::Static { .. } => {
                        me.src.js(&format!(
                            "obj.{}({})",
                            func.name.to_mixed_case(),
                            operands.join(", "),
                        ));
                    }
                    FunctionKind::Method { name, .. } => {
                        me.src.js(&format!(
                            "{}.{}({})",
                            operands[0],
                            name.to_mixed_case(),
                            operands[1..].join(", "),
                        ));
                    }
                };
                let mut bind_results = |me: &mut FunctionBindgen<'_>| match &func.result {
                    Type::Unit => {
                        results.push("".to_string());
                    }
                    _ => {
                        me.src.js("const ret = ");
                        results.push("ret".to_string());
                    }
                };

                if func.is_async {
                    let with = self.gen.intrinsic(Intrinsic::WithCurrentPromise);
                    let promises = self.gen.intrinsic(Intrinsic::Promises);
                    self.src.js(&with);
                    self.src.js("(null, cur_promise => {\n");
                    self.src.js(&format!(
                        "const catch_closure = e => {}.remove(cur_promise)(e);\n",
                        promises
                    ));
                    call(self);
                    self.src.js(".then(e => {\n");
                    match &func.result {
                        Type::Unit => {
                            results.push("".to_string());
                        }
                        _ => {
                            bind_results(self);
                            self.src.js("e;\n");
                        }
                    }
                } else {
                    bind_results(self);
                    call(self);
                    self.src.js(";\n");
                }
            }

            Instruction::Return { amt, func: _ } => match amt {
                0 => {}
                1 => self.src.js(&format!("return {};\n", operands[0])),
                _ => {
                    assert!(self.in_import);
                    self.src.js(&format!("return [{}];\n", operands.join(", ")));
                }
            },

            Instruction::ReturnAsyncImport { .. } => {
                // When we reenter webassembly successfully that means that the
                // host's promise resolved without exception. Take the current
                // promise index saved as part of `CallInterface` and update the
                // `CUR_PROMISE` global with what's currently being executed.
                // This'll get reset once the wasm returns again.
                //
                // Note that the name `cur_promise` used here is introduced in
                // the `CallInterface` codegen above in the closure for
                // `with_current_promise` which we're using here.
                //
                // TODO: hardcoding `__indirect_function_table` and no help if
                // it's not actually defined.
                self.gen.needs_get_export = true;
                let with = self.gen.intrinsic(Intrinsic::WithCurrentPromise);
                self.src.js(&format!(
                    "\
                        {with}(cur_promise, _prev => {{
                            get_export(\"__indirect_function_table\").get({})({});
                        }});
                    ",
                    operands[0],
                    operands[1..].join(", "),
                    with = with,
                ));
            }

            Instruction::I32Load { offset } => self.load("getInt32", *offset, operands, results),
            Instruction::I64Load { offset } => self.load("getBigInt64", *offset, operands, results),
            Instruction::F32Load { offset } => self.load("getFloat32", *offset, operands, results),
            Instruction::F64Load { offset } => self.load("getFloat64", *offset, operands, results),
            Instruction::I32Load8U { offset } => self.load("getUint8", *offset, operands, results),
            Instruction::I32Load8S { offset } => self.load("getInt8", *offset, operands, results),
            Instruction::I32Load16U { offset } => {
                self.load("getUint16", *offset, operands, results)
            }
            Instruction::I32Load16S { offset } => self.load("getInt16", *offset, operands, results),
            Instruction::I32Store { offset } => self.store("setInt32", *offset, operands),
            Instruction::I64Store { offset } => self.store("setBigInt64", *offset, operands),
            Instruction::F32Store { offset } => self.store("setFloat32", *offset, operands),
            Instruction::F64Store { offset } => self.store("setFloat64", *offset, operands),
            Instruction::I32Store8 { offset } => self.store("setInt8", *offset, operands),
            Instruction::I32Store16 { offset } => self.store("setInt16", *offset, operands),

            Instruction::Malloc {
                realloc,
                size,
                align,
            } => {
                self.needs_realloc = Some(realloc.to_string());
                let tmp = self.tmp();
                let ptr = format!("ptr{}", tmp);
                self.src.js(&format!(
                    "const {} = realloc(0, 0, {}, {});\n",
                    ptr, align, size
                ));
                results.push(ptr);
            }

            i => unimplemented!("{:?}", i),
        }
    }
}

impl Js {
    fn print_intrinsics(&mut self) {
        if self.all_intrinsics.contains(&Intrinsic::I32ToF32)
            || self.all_intrinsics.contains(&Intrinsic::F32ToI32)
        {
            self.src.js("
                const I32_TO_F32_I = new Int32Array(1);
                const I32_TO_F32_F = new Float32Array(I32_TO_F32_I.buffer);
            ");
        }
        if self.all_intrinsics.contains(&Intrinsic::I64ToF64)
            || self.all_intrinsics.contains(&Intrinsic::F64ToI64)
        {
            self.src.js("
                const I64_TO_F64_I = new BigInt64Array(1);
                const I64_TO_F64_F = new Float64Array(I64_TO_F64_I.buffer);
            ");
        }

        if self.all_intrinsics.contains(&Intrinsic::Promises) {
            self.all_intrinsics.insert(Intrinsic::Slab);
        }

        for i in mem::take(&mut self.all_intrinsics) {
            self.print_intrinsic(i);
        }

        self.src.js(&format!(
            "\nmodule.exports = {{ {} }};\n",
            self.src.exported_items.join(", ")
        ));
    }

    fn print_intrinsic(&mut self, i: Intrinsic) {
        self.src.export(i.name());
        match i {
            Intrinsic::ClampGuest => self.src.js("
                function clamp_guest(i, min, max) {
                    if (i < min || i > max) \
                        throw new RangeError(`must be between ${min} and ${max}`);
                    return i;
                }
            "),
            Intrinsic::ClampHost => self.src.js("
                function clamp_host(i, min, max) {
                    if (!Number.isInteger(i)) \
                        throw new TypeError(`must be an integer`);
                    if (i < min || i > max) \
                        throw new RangeError(`must be between ${min} and ${max}`);
                    return i;
                }
            "),

            Intrinsic::DataView => self.src.js("
                let DATA_VIEW = new DataView(new ArrayBuffer());

                function data_view(mem) {
                    if (DATA_VIEW.buffer !== mem.buffer) \
                        DATA_VIEW = new DataView(mem.buffer);
                    return DATA_VIEW;
                }
            "),

            Intrinsic::ClampHost64 => self.src.js("
                function clamp_host64(i, min, max) {
                    if (typeof i !== 'bigint') \
                        throw new TypeError(`must be a bigint`);
                    if (i < min || i > max) \
                        throw new RangeError(`must be between ${min} and ${max}`);
                    return i;
                }
            "),

            Intrinsic::ValidateGuestChar => self.src.js("
                function validate_guest_char(i) {
                    if ((i > 0x10ffff) || (i >= 0xd800 && i <= 0xdfff)) \
                        throw new RangeError(`not a valid char`);
                    return String.fromCodePoint(i);
                }
            "),

            // TODO: this is incorrect. It at least allows strings of length > 0
            // but it probably doesn't do the right thing for unicode or invalid
            // utf16 strings either.
            Intrinsic::ValidateHostChar => self.src.js("
                function validate_host_char(s) {
                    if (typeof s !== 'string') \
                        throw new TypeError(`must be a string`);
                    return s.codePointAt(0);
                }
            "),

            Intrinsic::ValidateFlags => self.src.js("
                function validate_flags(flags, mask) {
                    if (!Number.isInteger(flags)) \
                        throw new TypeError('flags were not an integer');
                    if ((flags & ~mask) != 0)
                        throw new TypeError('flags have extraneous bits set');
                    return flags;
                }
            "),

            Intrinsic::ValidateFlags64 => self.src.js("
                function validate_flags64(flags, mask) {
                    if (typeof flags !== 'bigint')
                        throw new TypeError('flags were not a bigint');
                    if ((flags & ~mask) != 0n)
                        throw new TypeError('flags have extraneous bits set');
                    return flags;
                }
            "),

            Intrinsic::ToString => self.src.js("
                function to_string(val) {
                    if (typeof val === 'symbol') {
                        throw new TypeError('symbols cannot be converted to strings');
                    } else {
                        // Calling `String` almost directly calls `ToString`, except that it also allows symbols,
                        // which is why we have the symbol-rejecting branch above.
                        //
                        // Definition of `String`: https://tc39.es/ecma262/#sec-string-constructor-string-value
                        return String(val);
                    }
                }
            "),

            Intrinsic::I32ToF32 => self.src.js("
                function i32ToF32(i) {
                    I32_TO_F32_I[0] = i;
                    return I32_TO_F32_F[0];
                }
            "),
            Intrinsic::F32ToI32 => self.src.js("
                function f32ToI32(f) {
                    I32_TO_F32_F[0] = f;
                    return I32_TO_F32_I[0];
                }
            "),
            Intrinsic::I64ToF64 => self.src.js("
                function i64ToF64(i) {
                    I64_TO_F64_I[0] = i;
                    return I64_TO_F64_F[0];
                }
            "),
            Intrinsic::F64ToI64 => self.src.js("
                function f64ToI64(f) {
                    I64_TO_F64_F[0] = f;
                    return I64_TO_F64_I[0];
                }
            "),

            Intrinsic::Utf8Decoder => self
                .src
                .js("const UTF8_DECODER = new TextDecoder('utf-8');\n"),

            Intrinsic::Utf8EncodedLen => self.src.js("
                let UTF8_ENCODED_LEN = 0;

                function utf8_encoded_len() {
                    return UTF8_ENCODED_LEN;
                }
            "),

            Intrinsic::Utf8Encode => self.src.js("
                const UTF8_ENCODER = new TextEncoder('utf-8');

                function utf8_encode(s, realloc, memory) {
                    if (typeof s !== 'string') \
                        throw new TypeError('expected a string');

                    if (s.length === 0) {
                        UTF8_ENCODED_LEN = 0;
                        return 1;
                    }

                    let alloc_len = 0;
                    let ptr = 0;
                    let writtenTotal = 0;
                    while (s.length > 0) {
                        ptr = realloc(ptr, alloc_len, 1, alloc_len + s.length);
                        alloc_len += s.length;
                        const { read, written } = UTF8_ENCODER.encodeInto(
                            s,
                            new Uint8Array(memory.buffer, ptr + writtenTotal, alloc_len - writtenTotal),
                        );
                        writtenTotal += written;
                        s = s.slice(read);
                    }
                    if (alloc_len > writtenTotal)
                        ptr = realloc(ptr, alloc_len, 1, writtenTotal);
                    UTF8_ENCODED_LEN = writtenTotal;
                    return ptr;
                }
            "),

            Intrinsic::Slab => self.src.js("
                class Slab {
                    constructor() {
                        this.list = [];
                        this.head = 0;
                    }

                    insert(val) {
                        if (this.head >= this.list.length) {
                            this.list.push({
                                next: this.list.length + 1,
                                val: undefined,
                            });
                        }
                        const ret = this.head;
                        const slot = this.list[ret];
                        this.head = slot.next;
                        slot.next = -1;
                        slot.val = val;
                        return ret;
                    }

                    get(idx) {
                        if (idx >= this.list.length)
                            throw new RangeError('handle index not valid');
                        const slot = this.list[idx];
                        if (slot.next === -1)
                            return slot.val;
                        throw new RangeError('handle index not valid');
                    }

                    remove(idx) {
                        const ret = this.get(idx); // validate the slot
                        const slot = this.list[idx];
                        slot.val = undefined;
                        slot.next = this.head;
                        this.head = idx;
                        return ret;
                    }
                }
            "),

            Intrinsic::Promises => self.src.js("const PROMISES = new Slab();\n"),
            Intrinsic::WithCurrentPromise => self.src.js("
                let CUR_PROMISE = null;
                function with_current_promise(val, closure) {
                    const prev = CUR_PROMISE;
                    CUR_PROMISE = val;
                    try {
                        closure(prev);
                    } finally {
                        CUR_PROMISE = prev;
                    }
                }
            "),
            Intrinsic::ThrowInvalidBool => self.src.js("
                function throw_invalid_bool() {
                    throw new RangeError(\"invalid variant discriminant for bool\");
                }
            "),
        }
    }
}

pub fn to_js_ident(name: &str) -> &str {
    match name {
        "in" => "in_",
        "import" => "import_",
        "export" => "export_",
        "module" => "module_",
        s => s,
    }
}

#[derive(Default)]
struct Source {
    js: wai_bindgen_gen_core::Source,
    ts: wai_bindgen_gen_core::Source,
    exported_items: Vec<String>,
}

impl Source {
    fn js(&mut self, s: &str) {
        self.js.push_str(s);
    }
    fn ts(&mut self, s: &str) {
        self.ts.push_str(s);
    }
    fn export(&mut self, name: impl Into<String>) {
        self.exported_items.push(name.into());
    }
}

enum JsFlagsRepr {
    Number,
    Bigint,
}

impl JsFlagsRepr {
    fn ty(&self) -> &'static str {
        match self {
            JsFlagsRepr::Number => "number",
            JsFlagsRepr::Bigint => "bigint",
        }
    }
    fn suffix(&self) -> &'static str {
        match self {
            JsFlagsRepr::Number => "",
            JsFlagsRepr::Bigint => "n",
        }
    }
}

fn js_flags_repr(f: &Flags) -> JsFlagsRepr {
    match f.repr() {
        FlagsRepr::U8 | FlagsRepr::U16 | FlagsRepr::U32(1) => JsFlagsRepr::Number,
        FlagsRepr::U32(_) => JsFlagsRepr::Bigint,
    }
}