deno_node 0.190.0

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

// WASI requires direct filesystem access for host operations.
// The FileSystem trait doesn't provide the low-level primitives needed here.
#![allow(
  clippy::disallowed_methods,
  reason = "WASI needs direct host filesystem access instead of the FileSystem trait"
)]

use std::borrow::Cow;
use std::cell::RefCell;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::path::Path;

use deno_core::GarbageCollected;
use deno_core::OpState;
use deno_core::op2;
use deno_permissions::OpenAccessKind;
use deno_permissions::PermissionsContainer;
use rand::RngCore;

const ERRNO_SUCCESS: i32 = 0;
const ERRNO_ACCES: i32 = 2;
const ERRNO_BADF: i32 = 8;
const ERRNO_FAULT: i32 = 21;
const ERRNO_EXIST: i32 = 20;
const ERRNO_INVAL: i32 = 28;
const ERRNO_IO: i32 = 29;
const ERRNO_ISDIR: i32 = 31;
const ERRNO_LOOP: i32 = 32;
const ERRNO_NOENT: i32 = 44;
const ERRNO_NOSYS: i32 = 52;
const ERRNO_NOTDIR: i32 = 54;
const ERRNO_NOTEMPTY: i32 = 55;
const ERRNO_NOTCAPABLE: i32 = 76;
#[allow(dead_code, reason = "kept for parity with full WASI errno set")]
const ERRNO_PERM: i32 = 63;

const FILETYPE_UNKNOWN: u8 = 0;
const FILETYPE_CHARACTER_DEVICE: u8 = 2;
const FILETYPE_DIRECTORY: u8 = 3;
const FILETYPE_REGULAR_FILE: u8 = 4;
const FILETYPE_SYMBOLIC_LINK: u8 = 7;

const FDFLAGS_APPEND: u16 = 1;

const RIGHTS_FD_READ: u64 = 1 << 1;
const RIGHTS_FD_SEEK: u64 = 1 << 2;
const RIGHTS_FD_DATASYNC: u64 = 1 << 3;
const RIGHTS_FD_SYNC: u64 = 1 << 4;
const RIGHTS_FD_TELL: u64 = 1 << 5;
const RIGHTS_FD_WRITE: u64 = 1 << 6;
const RIGHTS_FD_ADVISE: u64 = 1 << 7;
const RIGHTS_FD_ALLOCATE: u64 = 1 << 8;
const RIGHTS_PATH_CREATE_DIRECTORY: u64 = 1 << 9;
const RIGHTS_PATH_LINK_SOURCE: u64 = 1 << 11;
const RIGHTS_PATH_LINK_TARGET: u64 = 1 << 12;
const RIGHTS_PATH_OPEN: u64 = 1 << 13;
const RIGHTS_FD_READDIR: u64 = 1 << 14;
const RIGHTS_PATH_READLINK: u64 = 1 << 15;
const RIGHTS_PATH_RENAME_SOURCE: u64 = 1 << 16;
const RIGHTS_PATH_RENAME_TARGET: u64 = 1 << 17;
const RIGHTS_PATH_FILESTAT_GET: u64 = 1 << 18;
const RIGHTS_PATH_FILESTAT_SET_TIMES: u64 = 1 << 20;
const RIGHTS_FD_FILESTAT_GET: u64 = 1 << 21;
const RIGHTS_FD_FILESTAT_SET_SIZE: u64 = 1 << 22;
const RIGHTS_FD_FILESTAT_SET_TIMES: u64 = 1 << 23;
const RIGHTS_PATH_SYMLINK: u64 = 1 << 24;
const RIGHTS_PATH_REMOVE_DIRECTORY: u64 = 1 << 25;
const RIGHTS_PATH_UNLINK_FILE: u64 = 1 << 26;

const RIGHTS_DIR: u64 = RIGHTS_FD_READ
  | RIGHTS_FD_SEEK
  | RIGHTS_FD_TELL
  | RIGHTS_FD_SYNC
  | RIGHTS_FD_DATASYNC
  | RIGHTS_FD_ADVISE
  | RIGHTS_PATH_CREATE_DIRECTORY
  | RIGHTS_PATH_LINK_SOURCE
  | RIGHTS_PATH_LINK_TARGET
  | RIGHTS_PATH_OPEN
  | RIGHTS_FD_READDIR
  | RIGHTS_PATH_READLINK
  | RIGHTS_PATH_RENAME_SOURCE
  | RIGHTS_PATH_RENAME_TARGET
  | RIGHTS_PATH_FILESTAT_GET
  | RIGHTS_PATH_FILESTAT_SET_TIMES
  | RIGHTS_PATH_SYMLINK
  | RIGHTS_PATH_REMOVE_DIRECTORY
  | RIGHTS_PATH_UNLINK_FILE;

const RIGHTS_FILE: u64 = RIGHTS_FD_READ
  | RIGHTS_FD_WRITE
  | RIGHTS_FD_SEEK
  | RIGHTS_FD_TELL
  | RIGHTS_FD_SYNC
  | RIGHTS_FD_DATASYNC
  | RIGHTS_FD_ADVISE
  | RIGHTS_FD_ALLOCATE
  | RIGHTS_FD_FILESTAT_GET
  | RIGHTS_FD_FILESTAT_SET_SIZE
  | RIGHTS_FD_FILESTAT_SET_TIMES;

// Rights whose presence indicates the caller intends to mutate the file or
// its metadata (write data, extend it, change its times, fsync, etc.). When
// any of these is requested at path_open, we treat the open as needing
// Deno write permission so a read-only --allow-read can't be turned into a
// write-capable handle.
const RIGHTS_MUTATING: u64 = RIGHTS_FD_WRITE
  | RIGHTS_FD_DATASYNC
  | RIGHTS_FD_SYNC
  | RIGHTS_FD_ALLOCATE
  | RIGHTS_FD_FILESTAT_SET_SIZE
  | RIGHTS_FD_FILESTAT_SET_TIMES
  | RIGHTS_PATH_CREATE_DIRECTORY
  | RIGHTS_PATH_FILESTAT_SET_TIMES
  | RIGHTS_PATH_LINK_TARGET
  | RIGHTS_PATH_SYMLINK
  | RIGHTS_PATH_REMOVE_DIRECTORY
  | RIGHTS_PATH_UNLINK_FILE
  | RIGHTS_PATH_RENAME_SOURCE
  | RIGHTS_PATH_RENAME_TARGET;

const CLOCK_REALTIME: i32 = 0;
const CLOCK_MONOTONIC: i32 = 1;
const CLOCK_PROCESS_CPUTIME: i32 = 2;
const CLOCK_THREAD_CPUTIME: i32 = 3;

const WHENCE_SET: i32 = 0;
const WHENCE_CUR: i32 = 1;
const WHENCE_END: i32 = 2;

const OFLAGS_CREAT: u16 = 1;
const OFLAGS_DIRECTORY: u16 = 2;
const OFLAGS_EXCL: u16 = 4;
const OFLAGS_TRUNC: u16 = 8;

const LOOKUPFLAGS_SYMLINK_FOLLOW: u32 = 1;

// fstflags bits used by *_filestat_set_times
const FSTFLAGS_ATIM: u16 = 1 << 0;
const FSTFLAGS_ATIM_NOW: u16 = 1 << 1;
const FSTFLAGS_MTIM: u16 = 1 << 2;
const FSTFLAGS_MTIM_NOW: u16 = 1 << 3;

// poll_oneoff
const EVENTTYPE_CLOCK: u8 = 0;
const EVENTTYPE_FD_READ: u8 = 1;
const EVENTTYPE_FD_WRITE: u8 = 2;
const SUBCLOCKFLAGS_ABSTIME: u16 = 1;
const EVENT_FD_READWRITE_HANGUP: u16 = 1;

enum FdEntry {
  // Inherited stdio: read/write to the process's std streams.
  Stdin,
  Stdout,
  Stderr,
  // Override stdio backed by a host file (e.g. when the WASI constructor was
  // given `stdin/stdout/stderr` host fds). We dup the host fd at WASI
  // construction time so the host-side fd remains usable after WASI exits
  // and closes its dup.
  HostFile {
    file: std::fs::File,
    is_stdin: bool,
  },
  PreopenDir {
    virtual_path: String,
    real_path: String,
  },
  File {
    file: std::fs::File,
    rights: u64,
    fdflags: u16,
  },
  Dir {
    path: std::path::PathBuf,
    preopen_root_path: std::path::PathBuf,
    rights: u64,
  },
}

struct WasiInner {
  args: Vec<String>,
  env: Vec<(String, String)>,
  fds: Vec<Option<FdEntry>>,
}

impl WasiInner {
  fn get_fd(&self, fd: i32) -> Option<&FdEntry> {
    self.fds.get(fd as usize).and_then(|e| e.as_ref())
  }

  fn get_fd_mut(&mut self, fd: i32) -> Option<&mut FdEntry> {
    self.fds.get_mut(fd as usize).and_then(|e| e.as_mut())
  }

  /// Returns the WASI rights granted to `fd` at path_open time, if the fd
  /// is a regular file or directory we tracked. Stdio and HostFile entries
  /// return None because rights are implied by the entry type rather than
  /// stored explicitly.
  ///
  /// Note: callers pair this with [`has_right`], which treats `None` as
  /// "all rights granted". For inherited stdio that's fine. For HostFile,
  /// the per-op rights gate is intentionally skipped because the Deno
  /// permission for the underlying path was already paid when node:fs
  /// opened the user fd (see [`make_stdio_entry`]'s `FdTable` check);
  /// the OS-level open mode then limits what actually succeeds.
  fn fd_rights(&self, fd: i32) -> Option<u64> {
    match self.get_fd(fd) {
      Some(FdEntry::File { rights, .. })
      | Some(FdEntry::Dir { rights, .. }) => Some(*rights),
      _ => None,
    }
  }

  fn alloc_fd(&mut self, entry: FdEntry) -> i32 {
    for (i, slot) in self.fds.iter_mut().enumerate() {
      if slot.is_none() {
        *slot = Some(entry);
        return i as i32;
      }
    }
    let fd = self.fds.len() as i32;
    self.fds.push(Some(entry));
    fd
  }

  fn resolve_preopen_path(
    &self,
    dirfd: i32,
    path: &str,
    permissions: &PermissionsContainer,
    access_kind: OpenAccessKind,
  ) -> Result<std::path::PathBuf, i32> {
    self.resolve_preopen_path_ex(
      dirfd,
      path,
      permissions,
      access_kind,
      /* follow_symlinks */ true,
    )
  }

  /// Resolves a path under a preopen, optionally following symlinks for the
  /// final component. The result is canonicalized when the path exists.
  ///
  /// Returns ERRNO_NOTCAPABLE if the resolved path escapes the preopen
  /// root (matches Node/uvwasi behavior for sandbox violations such as
  /// `../outside.txt` or symlinks that point outside the preopen).
  /// Returns ERRNO_LOOP when canonicalization hits a symlink loop.
  fn resolve_preopen_path_ex(
    &self,
    dirfd: i32,
    path: &str,
    permissions: &PermissionsContainer,
    access_kind: OpenAccessKind,
    follow_symlinks: bool,
  ) -> Result<std::path::PathBuf, i32> {
    let (real_root, dir_base) = match self.get_fd(dirfd) {
      Some(FdEntry::PreopenDir { real_path, .. }) => {
        (real_path.as_str().to_string(), None)
      }
      Some(FdEntry::Dir {
        path: dir_path,
        preopen_root_path,
        ..
      }) => (
        preopen_root_path.to_string_lossy().into_owned(),
        Some(dir_path.clone()),
      ),
      _ => return Err(ERRNO_BADF),
    };

    let base =
      dir_base.unwrap_or_else(|| std::path::PathBuf::from(real_root.clone()));
    let resolved = base.join(path);
    let canonical_base = canonicalize_io(&real_root)?;

    let canonical_resolved =
      canonicalize_lookup(&resolved, follow_symlinks, &canonical_base)?;

    if !canonical_resolved.starts_with(&canonical_base) {
      return Err(ERRNO_NOTCAPABLE);
    }

    permissions
      .check_open(
        Cow::Owned(canonical_resolved.clone()),
        access_kind,
        Some("node:wasi"),
      )
      .map_err(|_| ERRNO_ACCES)?;

    Ok(canonical_resolved)
  }
}

/// Canonicalize a path, mapping io errors to WASI errno.
fn canonicalize_io<P: AsRef<Path>>(p: P) -> Result<std::path::PathBuf, i32> {
  std::fs::canonicalize(p).map_err(|e| io_err_to_errno(&e))
}

/// Best-effort canonicalization used by path lookup.
///
/// When `follow_symlinks` is false and the final path component is a symlink,
/// the link itself is the target (used by path_open with
/// LOOKUPFLAGS_SYMLINK_FOLLOW=0). The link must still resolve to a path
/// rooted in the preopen, otherwise ERRNO_NOTCAPABLE is returned by the
/// caller after a starts_with check.
fn canonicalize_lookup(
  resolved: &std::path::Path,
  follow_symlinks: bool,
  canonical_base: &std::path::Path,
) -> Result<std::path::PathBuf, i32> {
  // When the path exists and we can canonicalize, that's the most accurate
  // answer. Canonicalizing follows symlinks, which is the default WASI
  // lookup mode (LOOKUPFLAGS_SYMLINK_FOLLOW).
  let try_canonical = if follow_symlinks {
    Some(std::fs::canonicalize(resolved))
  } else {
    // Canonicalize the parent and append the final component literally.
    if let (Some(parent), Some(name)) =
      (resolved.parent(), resolved.file_name())
    {
      match std::fs::canonicalize(parent) {
        Ok(p) => Some(Ok(p.join(name))),
        Err(e) => Some(Err(e)),
      }
    } else {
      Some(std::fs::canonicalize(resolved))
    }
  };

  match try_canonical {
    Some(Ok(p)) => Ok(p),
    Some(Err(e)) => {
      // Detect ELOOP via raw_os_error so we don't depend on the unstable
      // ErrorKind::FilesystemLoop variant.
      if is_loop_error(&e) {
        return Err(ERRNO_LOOP);
      }
      // When the path doesn't exist, fall back to canonicalizing the parent
      // and joining the file_name — used by path_open with O_CREAT for
      // files that don't yet exist.
      let parent = resolved.parent().ok_or(ERRNO_NOENT)?;
      let parent_canonical = std::fs::canonicalize(parent).map_err(|pe| {
        if is_loop_error(&pe) {
          ERRNO_LOOP
        } else {
          io_err_to_errno(&pe)
        }
      })?;
      let filename = resolved.file_name().ok_or(ERRNO_INVAL)?;
      let joined = parent_canonical.join(filename);
      // Even for a non-existent target we sanity-check the resolved parent
      // is rooted in the preopen so we never permit writes outside.
      if !joined.starts_with(canonical_base) {
        return Err(ERRNO_NOTCAPABLE);
      }
      Ok(joined)
    }
    None => Err(ERRNO_NOENT),
  }
}

fn is_loop_error(e: &std::io::Error) -> bool {
  let Some(code) = e.raw_os_error() else {
    return false;
  };
  #[cfg(unix)]
  {
    code == libc::ELOOP
  }
  #[cfg(windows)]
  {
    // ERROR_CANT_RESOLVE_FILENAME (1921) is what Windows returns when a
    // reparse-point chain is too deep.
    code == 1921
  }
  #[cfg(not(any(unix, windows)))]
  {
    let _ = code;
    false
  }
}

/// Whether the fd's stored rights bitset includes every bit in `needed`.
/// Stdio / HostFile entries don't carry an explicit rights field — they
/// return `None` from `fd_rights` and we let those fall through to the
/// per-op entry-type checks. For File/Dir entries the rights were granted
/// at path_open time and intersected with the caller's request.
fn has_right(rights: Option<u64>, needed: u64) -> bool {
  match rights {
    Some(r) => r & needed == needed,
    None => true,
  }
}

fn io_err_to_errno(e: &std::io::Error) -> i32 {
  use std::io::ErrorKind;
  match e.kind() {
    ErrorKind::NotFound => ERRNO_NOENT,
    ErrorKind::PermissionDenied => ERRNO_ACCES,
    ErrorKind::AlreadyExists => ERRNO_EXIST,
    ErrorKind::InvalidInput => ERRNO_INVAL,
    ErrorKind::IsADirectory => ERRNO_ISDIR,
    ErrorKind::NotADirectory => ERRNO_NOTDIR,
    ErrorKind::DirectoryNotEmpty => ERRNO_NOTEMPTY,
    _ => ERRNO_IO,
  }
}

pub struct WasiContext {
  inner: RefCell<WasiInner>,
  permissions: PermissionsContainer,
}

// SAFETY: WasiContext contains no pointers to trace
unsafe impl GarbageCollected for WasiContext {
  fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}

  fn get_name(&self) -> &'static std::ffi::CStr {
    c"WasiContext"
  }
}

fn get_memory_slice(memory: &[u8], offset: i32, len: i32) -> Option<&[u8]> {
  let start = offset as usize;
  let end = start.checked_add(len as usize)?;
  memory.get(start..end)
}

fn get_memory_slice_mut(
  memory: &mut [u8],
  offset: i32,
  len: i32,
) -> Option<&mut [u8]> {
  let start = offset as usize;
  let end = start.checked_add(len as usize)?;
  memory.get_mut(start..end)
}

fn read_i32(memory: &[u8], offset: i32) -> Option<i32> {
  let bytes = get_memory_slice(memory, offset, 4)?;
  Some(i32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}

fn write_i32(memory: &mut [u8], offset: i32, value: i32) -> Option<()> {
  let slice = get_memory_slice_mut(memory, offset, 4)?;
  slice.copy_from_slice(&value.to_le_bytes());
  Some(())
}

fn write_u32(memory: &mut [u8], offset: i32, value: u32) -> Option<()> {
  write_i32(memory, offset, value as i32)
}

fn read_u32(memory: &[u8], offset: i32) -> Option<u32> {
  read_i32(memory, offset).map(|v| v as u32)
}

fn write_u64(memory: &mut [u8], offset: i32, value: u64) -> Option<()> {
  let slice = get_memory_slice_mut(memory, offset, 8)?;
  slice.copy_from_slice(&value.to_le_bytes());
  Some(())
}

fn write_u16(memory: &mut [u8], offset: i32, value: u16) -> Option<()> {
  let slice = get_memory_slice_mut(memory, offset, 2)?;
  slice.copy_from_slice(&value.to_le_bytes());
  Some(())
}

fn write_u8(memory: &mut [u8], offset: i32, value: u8) -> Option<()> {
  let slice = get_memory_slice_mut(memory, offset, 1)?;
  slice[0] = value;
  Some(())
}

fn read_string(memory: &[u8], offset: i32, len: i32) -> Option<String> {
  let bytes = get_memory_slice(memory, offset, len)?;
  String::from_utf8(bytes.to_vec()).ok()
}

fn parse_iovs(
  memory: &[u8],
  iovs_ptr: i32,
  iovs_len: i32,
) -> Option<Vec<(u32, u32)>> {
  let mut iovs = Vec::with_capacity(iovs_len as usize);
  for i in 0..iovs_len {
    let base = iovs_ptr + i * 8;
    let addr = read_u32(memory, base)?;
    let len = read_u32(memory, base + 4)?;
    iovs.push((addr, len));
  }
  Some(iovs)
}

fn gather_iov_bufs(
  memory: &[u8],
  iovs_ptr: i32,
  iovs_len: i32,
) -> Option<Vec<Vec<u8>>> {
  let mut bufs = Vec::with_capacity(iovs_len as usize);
  for i in 0..iovs_len {
    let base = iovs_ptr + i * 8;
    let addr = read_u32(memory, base)?;
    let len = read_u32(memory, base + 4)?;
    if len == 0 {
      bufs.push(Vec::new());
    } else {
      bufs.push(get_memory_slice(memory, addr as i32, len as i32)?.to_vec());
    }
  }
  Some(bufs)
}

fn monotonic_nanos() -> u64 {
  static START: std::sync::OnceLock<std::time::Instant> =
    std::sync::OnceLock::new();
  let start = START.get_or_init(std::time::Instant::now);
  start.elapsed().as_nanos() as u64
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum WasiError {
  #[class(type)]
  #[error("{0}")]
  Type(String),
  #[class(inherit)]
  #[error(transparent)]
  Permission(#[from] deno_permissions::PermissionCheckError),
}

#[op2]
impl WasiContext {
  #[constructor]
  #[cppgc]
  fn new(
    state: &mut OpState,
    #[serde] args: Vec<String>,
    #[serde] env_pairs: Vec<(String, String)>,
    #[serde] preopens: Vec<(String, String)>,
    #[smi] stdin_fd: i32,
    #[smi] stdout_fd: i32,
    #[smi] stderr_fd: i32,
    _return_on_exit: bool,
  ) -> Result<WasiContext, WasiError> {
    let permissions = state.borrow_mut::<PermissionsContainer>().clone();

    // Check read and write permissions for each preopened directory
    for (_virtual_path, real_path) in &preopens {
      permissions.check_open(
        Cow::Borrowed(Path::new(real_path)),
        OpenAccessKind::ReadWrite,
        Some("node:wasi"),
      )?;
    }

    // Build the three stdio slots, honoring user-supplied host fds. A user
    // fd of 0/1/2 means "use the process std stream"; any other fd is the
    // OS fd (Unix) or CRT fd (Windows) of a file the user opened via
    // node:fs and wants WASI to read/write through.
    let stdin = make_stdio_entry(state, stdin_fd, /*is_stdin*/ true)
      .unwrap_or(FdEntry::Stdin);
    let stdout = make_stdio_entry(state, stdout_fd, /*is_stdin*/ false)
      .unwrap_or(FdEntry::Stdout);
    let stderr = make_stdio_entry(state, stderr_fd, /*is_stdin*/ false)
      .unwrap_or(FdEntry::Stderr);

    let mut fds: Vec<Option<FdEntry>> =
      vec![Some(stdin), Some(stdout), Some(stderr)];

    for (virtual_path, real_path) in preopens {
      fds.push(Some(FdEntry::PreopenDir {
        virtual_path,
        real_path,
      }));
    }

    Ok(WasiContext {
      inner: RefCell::new(WasiInner {
        args,
        env: env_pairs,
        fds,
      }),
      permissions,
    })
  }

  #[fast]
  fn args_get(
    &self,
    #[smi] argv_ptr: i32,
    #[smi] argv_buf_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let mut buf_offset = argv_buf_ptr;
    for (i, arg) in inner.args.iter().enumerate() {
      let ptr_offset = argv_ptr + (i as i32) * 4;
      if write_u32(memory, ptr_offset, buf_offset as u32).is_none() {
        return ERRNO_FAULT;
      }
      let arg_bytes = arg.as_bytes();
      let Some(dest) =
        get_memory_slice_mut(memory, buf_offset, arg_bytes.len() as i32 + 1)
      else {
        return ERRNO_FAULT;
      };
      dest[..arg_bytes.len()].copy_from_slice(arg_bytes);
      dest[arg_bytes.len()] = 0;
      buf_offset += arg_bytes.len() as i32 + 1;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn args_sizes_get(
    &self,
    #[smi] argc_ptr: i32,
    #[smi] argv_buf_size_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let argc = inner.args.len() as u32;
    let buf_size: u32 = inner.args.iter().map(|a| a.len() as u32 + 1).sum();
    if write_u32(memory, argc_ptr, argc).is_none()
      || write_u32(memory, argv_buf_size_ptr, buf_size).is_none()
    {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn environ_get(
    &self,
    #[smi] environ_ptr: i32,
    #[smi] environ_buf_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let mut buf_offset = environ_buf_ptr;
    for (i, (key, value)) in inner.env.iter().enumerate() {
      let ptr_offset = environ_ptr + (i as i32) * 4;
      if write_u32(memory, ptr_offset, buf_offset as u32).is_none() {
        return ERRNO_FAULT;
      }
      let entry = format!("{key}={value}");
      let entry_bytes = entry.as_bytes();
      let Some(dest) =
        get_memory_slice_mut(memory, buf_offset, entry_bytes.len() as i32 + 1)
      else {
        return ERRNO_FAULT;
      };
      dest[..entry_bytes.len()].copy_from_slice(entry_bytes);
      dest[entry_bytes.len()] = 0;
      buf_offset += entry_bytes.len() as i32 + 1;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn environ_sizes_get(
    &self,
    #[smi] environc_ptr: i32,
    #[smi] environ_buf_size_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let count = inner.env.len() as u32;
    // key=value\0  ->  key.len() + 1 + value.len() + 1
    let buf_size: u32 = inner
      .env
      .iter()
      .map(|(k, v)| k.len() as u32 + 1 + v.len() as u32 + 1)
      .sum();
    if write_u32(memory, environc_ptr, count).is_none()
      || write_u32(memory, environ_buf_size_ptr, buf_size).is_none()
    {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn clock_res_get(
    &self,
    #[smi] clock_id: i32,
    #[smi] resolution_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let res: u64 = match clock_id {
      CLOCK_REALTIME | CLOCK_PROCESS_CPUTIME | CLOCK_THREAD_CPUTIME => 1_000,
      CLOCK_MONOTONIC => 1,
      _ => return ERRNO_INVAL,
    };
    if write_u64(memory, resolution_ptr, res).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn clock_time_get(
    &self,
    #[smi] clock_id: i32,
    #[smi] _precision: i32,
    #[smi] time_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let time_ns: u64 = match clock_id {
      CLOCK_REALTIME => {
        match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)
        {
          Ok(d) => d.as_nanos() as u64,
          Err(_) => return ERRNO_IO,
        }
      }
      CLOCK_MONOTONIC | CLOCK_PROCESS_CPUTIME | CLOCK_THREAD_CPUTIME => {
        monotonic_nanos()
      }
      _ => return ERRNO_INVAL,
    };
    if write_u64(memory, time_ptr, time_ns).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn random_get(
    &self,
    #[smi] buf_ptr: i32,
    #[smi] buf_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(dest) = get_memory_slice_mut(memory, buf_ptr, buf_len) else {
      return ERRNO_FAULT;
    };
    rand::thread_rng().fill_bytes(dest);
    ERRNO_SUCCESS
  }

  // Returns the exit code; JS side decides whether to throw or exit.
  #[fast]
  fn proc_exit(&self, #[smi] code: i32) -> i32 {
    code
  }

  #[fast]
  fn proc_raise(&self, #[smi] _sig: i32) -> i32 {
    ERRNO_NOSYS
  }

  #[fast]
  fn fd_write(
    &self,
    #[smi] fd: i32,
    #[smi] iovs_ptr: i32,
    #[smi] iovs_len: i32,
    #[smi] nwritten_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(bufs) = gather_iov_bufs(memory, iovs_ptr, iovs_len) else {
      return ERRNO_FAULT;
    };

    let mut inner = self.inner.borrow_mut();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_WRITE) {
      return ERRNO_NOTCAPABLE;
    }
    let mut total_written: u32 = 0;

    match inner.get_fd_mut(fd) {
      Some(FdEntry::Stdout) => {
        for buf in &bufs {
          if !buf.is_empty() {
            let _ = std::io::stdout().write_all(buf);
            total_written += buf.len() as u32;
          }
        }
        let _ = std::io::stdout().flush();
      }
      Some(FdEntry::Stderr) => {
        for buf in &bufs {
          if !buf.is_empty() {
            let _ = std::io::stderr().write_all(buf);
            total_written += buf.len() as u32;
          }
        }
        let _ = std::io::stderr().flush();
      }
      Some(FdEntry::HostFile { file, is_stdin }) if !*is_stdin => {
        for buf in &bufs {
          if !buf.is_empty() {
            match file.write(buf) {
              Ok(n) => total_written += n as u32,
              Err(e) => return io_err_to_errno(&e),
            }
          }
        }
      }
      Some(FdEntry::File { file, .. }) => {
        for buf in &bufs {
          if !buf.is_empty() {
            match file.write(buf) {
              Ok(n) => total_written += n as u32,
              Err(e) => return io_err_to_errno(&e),
            }
          }
        }
      }
      _ => return ERRNO_BADF,
    }

    if write_u32(memory, nwritten_ptr, total_written).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_read(
    &self,
    #[smi] fd: i32,
    #[smi] iovs_ptr: i32,
    #[smi] iovs_len: i32,
    #[smi] nread_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(iovs) = parse_iovs(memory, iovs_ptr, iovs_len) else {
      return ERRNO_FAULT;
    };

    let mut inner = self.inner.borrow_mut();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_READ) {
      return ERRNO_NOTCAPABLE;
    }
    let mut total_read: u32 = 0;

    match inner.get_fd_mut(fd) {
      Some(FdEntry::Stdin) => {
        for (buf_addr, buf_len) in &iovs {
          if *buf_len == 0 {
            continue;
          }
          let Some(dest) =
            get_memory_slice_mut(memory, *buf_addr as i32, *buf_len as i32)
          else {
            return ERRNO_FAULT;
          };
          match std::io::stdin().read(dest) {
            Ok(n) => {
              total_read += n as u32;
              if (n as u32) < *buf_len {
                break;
              }
            }
            Err(e) => return io_err_to_errno(&e),
          }
        }
      }
      Some(FdEntry::HostFile { file, is_stdin }) if *is_stdin => {
        for (buf_addr, buf_len) in &iovs {
          if *buf_len == 0 {
            continue;
          }
          let mut temp = vec![0u8; *buf_len as usize];
          match file.read(&mut temp) {
            Ok(n) => {
              let Some(dest) =
                get_memory_slice_mut(memory, *buf_addr as i32, n as i32)
              else {
                return ERRNO_FAULT;
              };
              dest.copy_from_slice(&temp[..n]);
              total_read += n as u32;
              if (n as u32) < *buf_len {
                break;
              }
            }
            Err(e) => return io_err_to_errno(&e),
          }
        }
      }
      Some(FdEntry::File { file, .. }) => {
        for (buf_addr, buf_len) in &iovs {
          if *buf_len == 0 {
            continue;
          }
          let mut temp = vec![0u8; *buf_len as usize];
          match file.read(&mut temp) {
            Ok(n) => {
              let Some(dest) =
                get_memory_slice_mut(memory, *buf_addr as i32, n as i32)
              else {
                return ERRNO_FAULT;
              };
              dest.copy_from_slice(&temp[..n]);
              total_read += n as u32;
              if (n as u32) < *buf_len {
                break;
              }
            }
            Err(e) => return io_err_to_errno(&e),
          }
        }
      }
      _ => return ERRNO_BADF,
    }

    if write_u32(memory, nread_ptr, total_read).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_seek(
    &self,
    #[smi] fd: i32,
    #[number] offset: i64,
    #[smi] whence: i32,
    #[smi] newoffset_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let seek_from = match whence {
      WHENCE_SET => SeekFrom::Start(offset as u64),
      WHENCE_CUR => SeekFrom::Current(offset),
      WHENCE_END => SeekFrom::End(offset),
      _ => return ERRNO_INVAL,
    };

    let mut inner = self.inner.borrow_mut();
    match inner.get_fd_mut(fd) {
      Some(FdEntry::File { file, .. }) => match file.seek(seek_from) {
        Ok(pos) => {
          if write_u64(memory, newoffset_ptr, pos).is_none() {
            return ERRNO_FAULT;
          }
          ERRNO_SUCCESS
        }
        Err(e) => io_err_to_errno(&e),
      },
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_close(&self, #[smi] fd: i32) -> i32 {
    let mut inner = self.inner.borrow_mut();
    if fd < 0 || fd as usize >= inner.fds.len() {
      return ERRNO_BADF;
    }
    if inner.fds[fd as usize].is_none() {
      return ERRNO_BADF;
    }
    // Drop the entry (closes file handles via Drop)
    inner.fds[fd as usize] = None;
    ERRNO_SUCCESS
  }

  // fdstat: filetype(u8) + pad(1) + flags(u16) + pad(4)
  //       + rights_base(u64) + rights_inheriting(u64) = 24 bytes
  #[fast]
  fn fd_fdstat_get(
    &self,
    #[smi] fd: i32,
    #[smi] buf: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let Some(dest) = get_memory_slice_mut(memory, buf, 24) else {
      return ERRNO_FAULT;
    };
    dest.fill(0);

    match inner.get_fd(fd) {
      Some(FdEntry::Stdin) => {
        write_u8(memory, buf, FILETYPE_CHARACTER_DEVICE);
        write_u64(memory, buf + 8, RIGHTS_FD_READ);
      }
      Some(FdEntry::Stdout | FdEntry::Stderr) => {
        write_u8(memory, buf, FILETYPE_CHARACTER_DEVICE);
        write_u64(memory, buf + 8, RIGHTS_FD_WRITE);
      }
      Some(FdEntry::HostFile { is_stdin, .. }) => {
        // Backing file inherits the rights of the stdio slot it replaces.
        write_u8(memory, buf, FILETYPE_REGULAR_FILE);
        if *is_stdin {
          write_u64(memory, buf + 8, RIGHTS_FD_READ | RIGHTS_FD_SEEK);
        } else {
          write_u64(memory, buf + 8, RIGHTS_FD_WRITE | RIGHTS_FD_SEEK);
        }
      }
      Some(FdEntry::PreopenDir { .. }) => {
        write_u8(memory, buf, FILETYPE_DIRECTORY);
        write_u64(memory, buf + 8, RIGHTS_DIR);
        write_u64(memory, buf + 16, RIGHTS_FILE | RIGHTS_DIR);
      }
      Some(FdEntry::Dir { rights, .. }) => {
        write_u8(memory, buf, FILETYPE_DIRECTORY);
        write_u64(memory, buf + 8, *rights);
        write_u64(memory, buf + 16, RIGHTS_FILE | RIGHTS_DIR);
      }
      Some(FdEntry::File {
        rights, fdflags, ..
      }) => {
        write_u8(memory, buf, FILETYPE_REGULAR_FILE);
        write_u16(memory, buf + 2, *fdflags);
        write_u64(memory, buf + 8, *rights);
      }
      None => return ERRNO_BADF,
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_fdstat_set_flags(&self, #[smi] _fd: i32, #[smi] _flags: i32) -> i32 {
    ERRNO_SUCCESS
  }

  // prestat: type(u32) + name_len(u32) = 8 bytes
  #[fast]
  fn fd_prestat_get(
    &self,
    #[smi] fd: i32,
    #[smi] prestat_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    match inner.get_fd(fd) {
      Some(FdEntry::PreopenDir { virtual_path, .. }) => {
        if write_u32(memory, prestat_ptr, 0).is_none()
          || write_u32(memory, prestat_ptr + 4, virtual_path.len() as u32)
            .is_none()
        {
          return ERRNO_FAULT;
        }
        ERRNO_SUCCESS
      }
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_prestat_dir_name(
    &self,
    #[smi] fd: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    match inner.get_fd(fd) {
      Some(FdEntry::PreopenDir { virtual_path, .. }) => {
        let vpath = virtual_path.as_bytes();
        let n = std::cmp::min(vpath.len(), path_len as usize);
        let Some(dest) = get_memory_slice_mut(memory, path_ptr, n as i32)
        else {
          return ERRNO_FAULT;
        };
        dest.copy_from_slice(&vpath[..n]);
        ERRNO_SUCCESS
      }
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_tell(
    &self,
    #[smi] fd: i32,
    #[smi] offset_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let mut inner = self.inner.borrow_mut();
    match inner.get_fd_mut(fd) {
      Some(FdEntry::File { file, .. }) => match file.stream_position() {
        Ok(pos) => {
          if write_u64(memory, offset_ptr, pos).is_none() {
            return ERRNO_FAULT;
          }
          ERRNO_SUCCESS
        }
        Err(e) => io_err_to_errno(&e),
      },
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_sync(&self, #[smi] fd: i32) -> i32 {
    let inner = self.inner.borrow();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_SYNC) {
      return ERRNO_NOTCAPABLE;
    }
    match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => match file.sync_all() {
        Ok(()) => ERRNO_SUCCESS,
        Err(e) => io_err_to_errno(&e),
      },
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_datasync(&self, #[smi] fd: i32) -> i32 {
    let inner = self.inner.borrow();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_DATASYNC) {
      return ERRNO_NOTCAPABLE;
    }
    match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => match file.sync_data() {
        Ok(()) => ERRNO_SUCCESS,
        Err(e) => io_err_to_errno(&e),
      },
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_advise(
    &self,
    #[smi] _fd: i32,
    #[number] _offset: i64,
    #[number] _len: i64,
    #[smi] _advice: i32,
  ) -> i32 {
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_allocate(
    &self,
    #[smi] fd: i32,
    #[number] offset: i64,
    #[number] len: i64,
  ) -> i32 {
    let inner = self.inner.borrow();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_ALLOCATE) {
      return ERRNO_NOTCAPABLE;
    }
    match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => {
        let required = (offset as u64).saturating_add(len as u64);
        match file.metadata() {
          Ok(meta) if meta.len() >= required => ERRNO_SUCCESS,
          Ok(_) => match file.set_len(required) {
            Ok(()) => ERRNO_SUCCESS,
            Err(e) => io_err_to_errno(&e),
          },
          Err(e) => io_err_to_errno(&e),
        }
      }
      _ => ERRNO_BADF,
    }
  }

  // filestat: dev(u64) + ino(u64) + filetype(u8) + pad(7)
  //        + nlink(u64) + size(u64) + atim(u64) + mtim(u64) + ctim(u64)
  //        = 64 bytes
  #[fast]
  fn fd_filestat_get(
    &self,
    #[smi] fd: i32,
    #[smi] buf: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    let Some(dest) = get_memory_slice_mut(memory, buf, 64) else {
      return ERRNO_FAULT;
    };
    dest.fill(0);

    match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => match file.metadata() {
        Ok(meta) => {
          write_filestat(memory, buf, &meta);
          ERRNO_SUCCESS
        }
        Err(e) => io_err_to_errno(&e),
      },
      Some(FdEntry::PreopenDir { real_path, .. }) => {
        match std::fs::metadata(real_path) {
          Ok(meta) => {
            write_filestat(memory, buf, &meta);
            ERRNO_SUCCESS
          }
          Err(e) => io_err_to_errno(&e),
        }
      }
      Some(FdEntry::Dir { path, .. }) => match std::fs::metadata(path) {
        Ok(meta) => {
          write_filestat(memory, buf, &meta);
          ERRNO_SUCCESS
        }
        Err(e) => io_err_to_errno(&e),
      },
      Some(FdEntry::Stdin | FdEntry::Stdout | FdEntry::Stderr) => {
        write_u8(memory, buf + 16, FILETYPE_CHARACTER_DEVICE);
        ERRNO_SUCCESS
      }
      Some(FdEntry::HostFile { file, .. }) => match file.metadata() {
        Ok(meta) => {
          write_filestat(memory, buf, &meta);
          ERRNO_SUCCESS
        }
        Err(e) => io_err_to_errno(&e),
      },
      None => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_filestat_set_size(&self, #[smi] fd: i32, #[number] size: i64) -> i32 {
    let inner = self.inner.borrow();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_FILESTAT_SET_SIZE) {
      return ERRNO_NOTCAPABLE;
    }
    match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => match file.set_len(size as u64) {
        Ok(()) => ERRNO_SUCCESS,
        Err(e) => io_err_to_errno(&e),
      },
      _ => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_filestat_set_times(
    &self,
    #[smi] fd: i32,
    #[number] atim: i64,
    #[number] mtim: i64,
    #[smi] fst_flags: i32,
  ) -> i32 {
    let inner = self.inner.borrow();
    // futimens on a read-only handle still succeeds at the OS level when
    // the caller owns the file, so this rights check is the only guard
    // against using read-only access to mutate atime/mtime.
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_FILESTAT_SET_TIMES) {
      return ERRNO_NOTCAPABLE;
    }
    let file_ref = match inner.get_fd(fd) {
      Some(FdEntry::File { file, .. }) => file,
      _ => return ERRNO_BADF,
    };
    let meta = match file_ref.metadata() {
      Ok(m) => m,
      Err(e) => return io_err_to_errno(&e),
    };
    let (atime, mtime) = match resolve_filestat_times_from_meta(
      &meta,
      atim as u64,
      mtim as u64,
      fst_flags as u16,
    ) {
      Ok(v) => v,
      Err(e) => return e,
    };
    match filetime::set_file_handle_times(file_ref, Some(atime), Some(mtime)) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn fd_renumber(&self, #[smi] from: i32, #[smi] to: i32) -> i32 {
    let mut inner = self.inner.borrow_mut();
    if from < 0
      || from as usize >= inner.fds.len()
      || inner.fds[from as usize].is_none()
    {
      return ERRNO_BADF;
    }
    while inner.fds.len() <= to as usize {
      inner.fds.push(None);
    }
    inner.fds[to as usize] = inner.fds[from as usize].take();
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_readdir(
    &self,
    #[smi] fd: i32,
    #[smi] buf_ptr: i32,
    #[smi] buf_len: i32,
    #[number] cookie: i64,
    #[smi] bufused_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_READDIR) {
      return ERRNO_NOTCAPABLE;
    }
    let dir_path = match inner.get_fd(fd) {
      Some(FdEntry::PreopenDir { real_path, .. }) => {
        std::path::PathBuf::from(real_path)
      }
      Some(FdEntry::Dir { path, .. }) => path.clone(),
      _ => return ERRNO_BADF,
    };

    let entries = match std::fs::read_dir(&dir_path) {
      Ok(entries) => entries,
      Err(e) => return io_err_to_errno(&e),
    };

    let mut buf_offset = 0i32;
    let mut entry_index: i64 = 0;
    // dirent: d_next(u64) + d_ino(u64) + d_namlen(u32) + d_type(u8)
    //       + pad(3) = 24 bytes, followed by name bytes
    const DIRENT_SIZE: i32 = 24;

    for entry_result in entries {
      let entry = match entry_result {
        Ok(e) => e,
        Err(e) => return io_err_to_errno(&e),
      };

      entry_index += 1;
      if entry_index <= cookie {
        continue;
      }

      if buf_offset + DIRENT_SIZE > buf_len {
        break;
      }

      let name = entry.file_name();
      let name_bytes = name.as_encoded_bytes();
      let abs_offset = buf_ptr + buf_offset;

      write_u64(memory, abs_offset, entry_index as u64);
      write_u64(memory, abs_offset + 8, 0);
      write_u32(memory, abs_offset + 16, name_bytes.len() as u32);
      let file_type = match entry.file_type() {
        Ok(ft) if ft.is_dir() => FILETYPE_DIRECTORY,
        Ok(ft) if ft.is_file() => FILETYPE_REGULAR_FILE,
        Ok(ft) if ft.is_symlink() => FILETYPE_SYMBOLIC_LINK,
        _ => FILETYPE_UNKNOWN,
      };
      write_u8(memory, abs_offset + 20, file_type);
      buf_offset += DIRENT_SIZE;

      let name_write_len =
        std::cmp::min(name_bytes.len() as i32, buf_len - buf_offset);
      if name_write_len > 0
        && let Some(dest) =
          get_memory_slice_mut(memory, buf_ptr + buf_offset, name_write_len)
      {
        dest.copy_from_slice(&name_bytes[..name_write_len as usize]);
      }
      buf_offset += name_bytes.len() as i32;
    }

    if write_u32(
      memory,
      bufused_ptr,
      std::cmp::min(buf_offset, buf_len) as u32,
    )
    .is_none()
    {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn path_open(
    &self,
    #[smi] dirfd: i32,
    #[smi] dirflags: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[smi] oflags: i32,
    #[number] fs_rights_base: i64,
    #[number] _fs_rights_inheriting: i64,
    #[smi] fdflags: i32,
    #[smi] fd_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };

    let oflags = oflags as u16;
    let fdflags_u16 = fdflags as u16;
    let rights = fs_rights_base as u64;
    // Any right that would let the caller mutate file state or metadata
    // requires Deno write permission. RIGHTS_FD_WRITE alone is not enough
    // because RIGHTS_FD_FILESTAT_SET_TIMES (utimens via fd) succeeds at the
    // OS level even on read-only handles when the caller owns the file.
    let needs_write_perm = rights & RIGHTS_MUTATING != 0;
    // Distinct from the permission gate: only actually open the OS file in
    // write mode when the caller asked to write data or wants O_CREAT etc.
    // Asking for SET_TIMES alone shouldn't force the underlying open to
    // demand OS write access.
    let wants_os_write = rights & RIGHTS_FD_WRITE != 0;
    let creates = oflags & OFLAGS_CREAT != 0
      || oflags & OFLAGS_EXCL != 0
      || oflags & OFLAGS_TRUNC != 0;
    let follow_symlinks = (dirflags as u32) & LOOKUPFLAGS_SYMLINK_FOLLOW != 0;

    let access_kind = if needs_write_perm || creates {
      OpenAccessKind::ReadWrite
    } else {
      OpenAccessKind::Read
    };

    let mut inner = self.inner.borrow_mut();
    let resolved = match inner.resolve_preopen_path_ex(
      dirfd,
      &path_str,
      &self.permissions,
      access_kind,
      follow_symlinks,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };

    if oflags & OFLAGS_DIRECTORY != 0 || resolved.is_dir() {
      if oflags & OFLAGS_DIRECTORY != 0 && !resolved.is_dir() {
        return ERRNO_NOTDIR;
      }
      let preopen_root_path = match inner.get_fd(dirfd) {
        Some(FdEntry::PreopenDir { real_path, .. }) => {
          match std::fs::canonicalize(real_path) {
            Ok(path) => path,
            Err(e) => return io_err_to_errno(&e),
          }
        }
        Some(FdEntry::Dir {
          preopen_root_path, ..
        }) => preopen_root_path.clone(),
        _ => return ERRNO_BADF,
      };
      // Grant only the requested rights (intersected with the maximum a
      // directory fd can hold). The earlier permissions.check_open already
      // gated Read vs ReadWrite based on the requested rights, so we mustn't
      // hand out rights beyond what the caller asked for.
      let granted = rights & RIGHTS_DIR;
      let new_fd = inner.alloc_fd(FdEntry::Dir {
        path: resolved,
        preopen_root_path,
        rights: granted,
      });
      if write_i32(memory, fd_ptr, new_fd).is_none() {
        return ERRNO_FAULT;
      }
      return ERRNO_SUCCESS;
    }

    let mut opts = std::fs::OpenOptions::new();
    opts.read(true);

    if oflags & OFLAGS_CREAT != 0 {
      opts.create(true).write(true);
    }
    if oflags & OFLAGS_EXCL != 0 {
      opts.create_new(true).write(true);
    }
    if oflags & OFLAGS_TRUNC != 0 {
      opts.truncate(true).write(true);
    }
    if fdflags_u16 & FDFLAGS_APPEND != 0 {
      opts.append(true);
    }
    if wants_os_write {
      opts.write(true);
    }

    match opts.open(&resolved) {
      Ok(file) => {
        // Hand out only the requested rights, intersected with what a file
        // fd can hold. Each mutating fd_* op re-checks the corresponding
        // right below so the caller can't escalate by, say, opening with
        // RIGHTS_FD_READ and then calling fd_filestat_set_times.
        let granted = rights & RIGHTS_FILE;
        let new_fd = inner.alloc_fd(FdEntry::File {
          file,
          rights: granted,
          fdflags: fdflags_u16,
        });
        if write_i32(memory, fd_ptr, new_fd).is_none() {
          return ERRNO_FAULT;
        }
        ERRNO_SUCCESS
      }
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_create_directory(
    &self,
    #[smi] dirfd: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    let resolved = match inner.resolve_preopen_path(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Write,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::create_dir(&resolved) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_remove_directory(
    &self,
    #[smi] dirfd: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    let resolved = match inner.resolve_preopen_path(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Write,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::remove_dir(&resolved) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_unlink_file(
    &self,
    #[smi] dirfd: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    let resolved = match inner.resolve_preopen_path(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Write,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::remove_file(&resolved) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_rename(
    &self,
    #[smi] old_dirfd: i32,
    #[smi] old_path_ptr: i32,
    #[smi] old_path_len: i32,
    #[smi] new_dirfd: i32,
    #[smi] new_path_ptr: i32,
    #[smi] new_path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(old_path) = read_string(memory, old_path_ptr, old_path_len) else {
      return ERRNO_FAULT;
    };
    let Some(new_path) = read_string(memory, new_path_ptr, new_path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    let old_resolved = match inner.resolve_preopen_path(
      old_dirfd,
      &old_path,
      &self.permissions,
      OpenAccessKind::ReadWrite,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    let new_resolved = match inner.resolve_preopen_path(
      new_dirfd,
      &new_path,
      &self.permissions,
      OpenAccessKind::ReadWrite,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::rename(&old_resolved, &new_resolved) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_filestat_get(
    &self,
    #[smi] dirfd: i32,
    #[smi] flags: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[smi] filestat_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let follow_symlinks = (flags as u32) & LOOKUPFLAGS_SYMLINK_FOLLOW != 0;
    let inner = self.inner.borrow();
    let resolved = match inner.resolve_preopen_path_ex(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Read,
      follow_symlinks,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    let Some(dest) = get_memory_slice_mut(memory, filestat_ptr, 64) else {
      return ERRNO_FAULT;
    };
    dest.fill(0);
    let meta_result = if follow_symlinks {
      std::fs::metadata(&resolved)
    } else {
      std::fs::symlink_metadata(&resolved)
    };
    match meta_result {
      Ok(meta) => {
        write_filestat(memory, filestat_ptr, &meta);
        ERRNO_SUCCESS
      }
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_readlink(
    &self,
    #[smi] dirfd: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[smi] buf_ptr: i32,
    #[smi] buf_len: i32,
    #[smi] bufused_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    // readlink must NOT follow the final symlink: we want to read the link
    // target as stored on disk. Following would resolve to the underlying
    // file and std::fs::read_link would reject it as InvalidInput.
    let resolved = match inner.resolve_preopen_path_ex(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Read,
      /* follow_symlinks */ false,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::read_link(&resolved) {
      Ok(target) => {
        let target_bytes = target.as_os_str().as_encoded_bytes();
        // Best-effort reverse of the normalization in path_symlink: on
        // Windows we rewrote forward slashes to backslashes so the OS
        // could resolve relative symlinks, so here we flip them back to
        // give WASI callers a POSIX-style target. This is biased toward
        // the test-wasi-symlinks round-trip case — a symlink created
        // outside WASI with literal backslashes in its target will have
        // its bytes rewritten to forward slashes too, which is a known
        // limitation we accept to keep the WASI surface POSIX-shaped.
        #[cfg(windows)]
        let owned = target_bytes
          .iter()
          .map(|b| if *b == b'\\' { b'/' } else { *b })
          .collect::<Vec<u8>>();
        #[cfg(windows)]
        let target_bytes: &[u8] = &owned;
        let n = std::cmp::min(target_bytes.len(), buf_len as usize);
        let Some(dest) = get_memory_slice_mut(memory, buf_ptr, n as i32) else {
          return ERRNO_FAULT;
        };
        dest.copy_from_slice(&target_bytes[..n]);
        if write_u32(memory, bufused_ptr, n as u32).is_none() {
          return ERRNO_FAULT;
        }
        ERRNO_SUCCESS
      }
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_symlink(
    &self,
    #[smi] old_path_ptr: i32,
    #[smi] old_path_len: i32,
    #[smi] dirfd: i32,
    #[smi] new_path_ptr: i32,
    #[smi] new_path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(old_path) = read_string(memory, old_path_ptr, old_path_len) else {
      return ERRNO_FAULT;
    };
    let Some(new_path) = read_string(memory, new_path_ptr, new_path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    let new_resolved = match inner.resolve_preopen_path(
      dirfd,
      &new_path,
      &self.permissions,
      OpenAccessKind::Write,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    #[cfg(unix)]
    {
      match std::os::unix::fs::symlink(&old_path, &new_resolved) {
        Ok(()) => ERRNO_SUCCESS,
        Err(e) => io_err_to_errno(&e),
      }
    }
    #[cfg(not(unix))]
    {
      // Windows stores the link target verbatim in the reparse point. WASI
      // callers pass POSIX-style targets with forward slashes (the C
      // create_symlink test calls `symlink("./input-in-subdir.txt", …)`),
      // and Windows can't resolve those — opens through the link fail.
      // Normalize to backslashes here so subsequent path_open succeeds.
      // If the existing target is a directory, fall back to symlink_dir so
      // resolution doesn't break on directory targets.
      let target = old_path.replace('/', "\\");
      let target_path = std::path::PathBuf::from(&target);
      let abs_target = if target_path.is_absolute() {
        target_path.clone()
      } else if let Some(parent) = new_resolved.parent() {
        parent.join(&target_path)
      } else {
        target_path.clone()
      };
      let is_dir = std::fs::metadata(&abs_target)
        .map(|m| m.is_dir())
        .unwrap_or(false);
      let result = if is_dir {
        std::os::windows::fs::symlink_dir(&target, &new_resolved)
      } else {
        std::os::windows::fs::symlink_file(&target, &new_resolved)
      };
      match result {
        Ok(()) => ERRNO_SUCCESS,
        Err(e) => io_err_to_errno(&e),
      }
    }
  }

  #[fast]
  fn path_filestat_set_times(
    &self,
    #[smi] dirfd: i32,
    #[smi] flags: i32,
    #[smi] path_ptr: i32,
    #[smi] path_len: i32,
    #[number] atim: i64,
    #[number] mtim: i64,
    #[smi] fst_flags: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(path_str) = read_string(memory, path_ptr, path_len) else {
      return ERRNO_FAULT;
    };
    let follow_symlinks = (flags as u32) & LOOKUPFLAGS_SYMLINK_FOLLOW != 0;
    let inner = self.inner.borrow();
    let resolved = match inner.resolve_preopen_path_ex(
      dirfd,
      &path_str,
      &self.permissions,
      OpenAccessKind::Write,
      follow_symlinks,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    // Mirror path_filestat_get: honor LOOKUPFLAGS_SYMLINK_FOLLOW for both the
    // metadata lookup we use to fill in absent atim/mtim and for the
    // utimensat-equivalent call. Otherwise a caller passing flags=0 would
    // silently traverse symlinks and modify the target's times.
    let meta = if follow_symlinks {
      std::fs::metadata(&resolved)
    } else {
      std::fs::symlink_metadata(&resolved)
    };
    let meta = match meta {
      Ok(m) => m,
      Err(e) => return io_err_to_errno(&e),
    };
    let (atime, mtime) = match resolve_filestat_times_from_meta(
      &meta,
      atim as u64,
      mtim as u64,
      fst_flags as u16,
    ) {
      Ok(v) => v,
      Err(e) => return e,
    };
    let result = if follow_symlinks {
      filetime::set_file_times(&resolved, atime, mtime)
    } else {
      filetime::set_symlink_file_times(&resolved, atime, mtime)
    };
    match result {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  #[fast]
  fn path_link(
    &self,
    #[smi] old_dirfd: i32,
    #[smi] _old_flags: i32,
    #[smi] old_path_ptr: i32,
    #[smi] old_path_len: i32,
    #[smi] new_dirfd: i32,
    #[smi] new_path_ptr: i32,
    #[smi] new_path_len: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(old_path) = read_string(memory, old_path_ptr, old_path_len) else {
      return ERRNO_FAULT;
    };
    let Some(new_path) = read_string(memory, new_path_ptr, new_path_len) else {
      return ERRNO_FAULT;
    };
    let inner = self.inner.borrow();
    // The Deno permission check inside `resolve_preopen_path` is what
    // actually gates the OS-level hardlink; the per-fd rights check here
    // is purely for parity with the other path_* ops that consult the
    // dirfd's stored rights (`path_create_directory`, `path_unlink_file`,
    // etc.). Skipping it would be safe but inconsistent.
    if !has_right(inner.fd_rights(old_dirfd), RIGHTS_PATH_LINK_SOURCE)
      || !has_right(inner.fd_rights(new_dirfd), RIGHTS_PATH_LINK_TARGET)
    {
      return ERRNO_NOTCAPABLE;
    }
    let old_resolved = match inner.resolve_preopen_path(
      old_dirfd,
      &old_path,
      &self.permissions,
      OpenAccessKind::Read,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    let new_resolved = match inner.resolve_preopen_path(
      new_dirfd,
      &new_path,
      &self.permissions,
      OpenAccessKind::Write,
    ) {
      Ok(p) => p,
      Err(e) => return e,
    };
    match std::fs::hard_link(&old_resolved, &new_resolved) {
      Ok(()) => ERRNO_SUCCESS,
      Err(e) => io_err_to_errno(&e),
    }
  }

  // poll_oneoff blocks the current thread until at least one of the
  // requested subscriptions fires. We support:
  //   * SUBSCRIPTION_CLOCK on monotonic/realtime: both relative deadlines
  //     (used by wasi-libc's sleep()) and absolute (SUBCLOCKFLAGS_ABSTIME).
  //   * SUBSCRIPTION_FD_READ on regular files (returns immediately with
  //     remaining bytes) and stdin (returns immediately with hangup set,
  //     matching the behavior wasi-libc maps to POLLHUP|POLLIN — this is
  //     what test-wasi-poll.js expects when stdin is not a TTY).
  //   * SUBSCRIPTION_FD_WRITE on stdout/stderr/regular files (always ready).
  // Behavior for unrecognized subscription types is to emit an EINVAL event.
  #[fast]
  fn poll_oneoff(
    &self,
    #[smi] in_ptr: i32,
    #[smi] out_ptr: i32,
    #[smi] nsubscriptions: i32,
    #[smi] nevents_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    if nsubscriptions <= 0 {
      return ERRNO_INVAL;
    }

    let subs = match read_subscriptions(memory, in_ptr, nsubscriptions) {
      Ok(s) => s,
      Err(e) => return e,
    };

    let inner = self.inner.borrow();
    let mut events: Vec<PollEvent> = Vec::new();
    // Earliest deadline across all CLOCK subscriptions. None = no deadline.
    let mut deadline: Option<std::time::Instant> = None;

    for sub in &subs {
      match sub.tag {
        EVENTTYPE_CLOCK => {
          let now = std::time::Instant::now();
          let due = if sub.clock_flags & SUBCLOCKFLAGS_ABSTIME != 0 {
            // Absolute deadline against the clock id. For monotonic this is
            // nanoseconds since process start; for realtime we approximate
            // by computing how much wall time remains.
            let now_ns = match sub.clock_id {
              CLOCK_REALTIME => std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos() as u64)
                .unwrap_or(0),
              _ => monotonic_nanos(),
            };
            if sub.clock_timeout > now_ns {
              now + std::time::Duration::from_nanos(sub.clock_timeout - now_ns)
            } else {
              now
            }
          } else {
            now + std::time::Duration::from_nanos(sub.clock_timeout)
          };
          deadline = Some(match deadline {
            Some(d) => d.min(due),
            None => due,
          });
        }
        EVENTTYPE_FD_READ => {
          let (errno, hangup, nbytes) = match inner.get_fd(sub.fd) {
            Some(FdEntry::Stdin) => {
              // wasi-libc maps EVENT_FD_READWRITE_HANGUP into the POLLHUP
              // bit of revents. test-wasi-poll.js asserts POLLHUP|POLLIN
              // when stdin is not a tty.
              (ERRNO_SUCCESS, EVENT_FD_READWRITE_HANGUP, 0u64)
            }
            Some(FdEntry::File { file, .. }) => {
              // metadata().len() doesn't need &mut, but stream_position does.
              // Use a non-mutating estimate (file length) when we can't seek.
              let nb = file.metadata().ok().map(|m| m.len()).unwrap_or(0);
              (ERRNO_SUCCESS, 0, nb)
            }
            Some(_) => (ERRNO_INVAL, 0, 0),
            None => (ERRNO_BADF, 0, 0),
          };
          events.push(PollEvent {
            userdata: sub.userdata,
            error: errno as u16,
            ty: EVENTTYPE_FD_READ,
            nbytes,
            flags: hangup,
          });
        }
        EVENTTYPE_FD_WRITE => {
          let (errno, nbytes) = match inner.get_fd(sub.fd) {
            Some(FdEntry::Stdout | FdEntry::Stderr) => (ERRNO_SUCCESS, 0u64),
            Some(FdEntry::File { .. }) => (ERRNO_SUCCESS, 0u64),
            Some(_) => (ERRNO_INVAL, 0),
            None => (ERRNO_BADF, 0),
          };
          events.push(PollEvent {
            userdata: sub.userdata,
            error: errno as u16,
            ty: EVENTTYPE_FD_WRITE,
            nbytes,
            flags: 0,
          });
        }
        _ => {
          events.push(PollEvent {
            userdata: sub.userdata,
            error: ERRNO_INVAL as u16,
            ty: sub.tag,
            nbytes: 0,
            flags: 0,
          });
        }
      }
    }

    // Drop the borrow before we sleep — sleeping while holding the RefCell
    // would deadlock any reentrant op (though poll_oneoff doesn't reenter
    // today, this future-proofs the implementation).
    drop(inner);

    // If no FD subscription fired and a clock deadline was set, wait it out.
    // The clock event itself only fires after the wait, signaled below.
    //
    // NOTE: this intentionally blocks the V8 isolate thread for the full
    // clock subscription. WASI's poll_oneoff is a synchronous host call, so
    // we can't yield back to the event loop here without breaking the
    // contract. Callers passing long clock timeouts will hang JS until the
    // deadline elapses — that's the inherent cost of running a synchronous
    // WASI program on a single-threaded runtime, not a bug.
    if events.is_empty()
      && let Some(due) = deadline
    {
      let now = std::time::Instant::now();
      if due > now {
        std::thread::sleep(due - now);
      }
    }

    // If we waited and had clock subscriptions, emit a single clock event so
    // the libc poll() shim returns. We pick the userdata of the first clock
    // subscription, which is sufficient for callers like wasi-libc's sleep
    // that use a single subscription.
    if events.is_empty()
      && let Some(clock_sub) = subs.iter().find(|s| s.tag == EVENTTYPE_CLOCK)
    {
      events.push(PollEvent {
        userdata: clock_sub.userdata,
        error: 0,
        ty: EVENTTYPE_CLOCK,
        nbytes: 0,
        flags: 0,
      });
    }

    let nevents = events.len() as u32;
    for (i, ev) in events.iter().enumerate() {
      let base = out_ptr + (i as i32) * 32;
      if write_u64(memory, base, ev.userdata).is_none()
        || write_u16(memory, base + 8, ev.error).is_none()
        || write_u8(memory, base + 10, ev.ty).is_none()
        || write_u64(memory, base + 16, ev.nbytes).is_none()
        || write_u16(memory, base + 24, ev.flags).is_none()
      {
        return ERRNO_FAULT;
      }
    }
    if write_u32(memory, nevents_ptr, nevents).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn sched_yield(&self) -> i32 {
    std::thread::yield_now();
    ERRNO_SUCCESS
  }

  #[fast]
  fn sock_recv(
    &self,
    #[smi] _fd: i32,
    #[smi] _ri_data_ptr: i32,
    #[smi] _ri_data_len: i32,
    #[smi] _ri_flags: i32,
    #[smi] _ro_datalen_ptr: i32,
    #[smi] _ro_flags_ptr: i32,
    #[buffer] _memory: &mut [u8],
  ) -> i32 {
    ERRNO_NOSYS
  }

  #[fast]
  fn sock_send(
    &self,
    #[smi] _fd: i32,
    #[smi] _si_data_ptr: i32,
    #[smi] _si_data_len: i32,
    #[smi] _si_flags: i32,
    #[smi] _so_datalen_ptr: i32,
    #[buffer] _memory: &mut [u8],
  ) -> i32 {
    ERRNO_NOSYS
  }

  #[fast]
  fn sock_shutdown(&self, #[smi] _fd: i32, #[smi] _how: i32) -> i32 {
    ERRNO_NOSYS
  }

  // Sockets are not represented in our WASI FdTable, so treat any fd as
  // either an unknown fd (ERRNO_BADF) or a regular fd of the wrong type
  // (ERRNO_NOTSOCK). Returning BADF matches Node/uvwasi behavior for
  // accept() against a non-existent fd, which is what node's
  // test-wasi-sock.js exercises.
  #[fast]
  fn sock_accept(
    &self,
    #[smi] fd: i32,
    #[smi] _flags: i32,
    #[smi] _fd_ptr: i32,
    #[buffer] _memory: &mut [u8],
  ) -> i32 {
    let inner = self.inner.borrow();
    match inner.get_fd(fd) {
      // ERRNO_NOTSOCK (57) for an fd that exists but isn't a socket.
      Some(_) => 57,
      None => ERRNO_BADF,
    }
  }

  #[fast]
  fn fd_pread(
    &self,
    #[smi] fd: i32,
    #[smi] iovs_ptr: i32,
    #[smi] iovs_len: i32,
    #[number] offset: i64,
    #[smi] nread_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(iovs) = parse_iovs(memory, iovs_ptr, iovs_len) else {
      return ERRNO_FAULT;
    };

    let mut inner = self.inner.borrow_mut();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_READ) {
      return ERRNO_NOTCAPABLE;
    }
    let mut total_read: u32 = 0;

    match inner.get_fd_mut(fd) {
      Some(FdEntry::File { file, .. }) => {
        let cur = match file.stream_position() {
          Ok(pos) => pos,
          Err(e) => return io_err_to_errno(&e),
        };
        if let Err(e) = file.seek(SeekFrom::Start(offset as u64)) {
          return io_err_to_errno(&e);
        }
        for (buf_addr, buf_len) in &iovs {
          if *buf_len == 0 {
            continue;
          }
          let mut temp = vec![0u8; *buf_len as usize];
          match file.read(&mut temp) {
            Ok(n) => {
              let Some(dest) =
                get_memory_slice_mut(memory, *buf_addr as i32, n as i32)
              else {
                let _ = file.seek(SeekFrom::Start(cur));
                return ERRNO_FAULT;
              };
              dest.copy_from_slice(&temp[..n]);
              total_read += n as u32;
              if (n as u32) < *buf_len {
                break;
              }
            }
            Err(e) => {
              let _ = file.seek(SeekFrom::Start(cur));
              return io_err_to_errno(&e);
            }
          }
        }
        let _ = file.seek(SeekFrom::Start(cur));
      }
      _ => return ERRNO_BADF,
    }

    if write_u32(memory, nread_ptr, total_read).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_pwrite(
    &self,
    #[smi] fd: i32,
    #[smi] iovs_ptr: i32,
    #[smi] iovs_len: i32,
    #[number] offset: i64,
    #[smi] nwritten_ptr: i32,
    #[buffer] memory: &mut [u8],
  ) -> i32 {
    let Some(bufs) = gather_iov_bufs(memory, iovs_ptr, iovs_len) else {
      return ERRNO_FAULT;
    };

    let mut inner = self.inner.borrow_mut();
    if !has_right(inner.fd_rights(fd), RIGHTS_FD_WRITE) {
      return ERRNO_NOTCAPABLE;
    }
    let mut total_written: u32 = 0;

    match inner.get_fd_mut(fd) {
      Some(FdEntry::File { file, .. }) => {
        let cur = match file.stream_position() {
          Ok(pos) => pos,
          Err(e) => return io_err_to_errno(&e),
        };
        if let Err(e) = file.seek(SeekFrom::Start(offset as u64)) {
          return io_err_to_errno(&e);
        }
        for buf in &bufs {
          if !buf.is_empty() {
            match file.write(buf) {
              Ok(n) => total_written += n as u32,
              Err(e) => {
                let _ = file.seek(SeekFrom::Start(cur));
                return io_err_to_errno(&e);
              }
            }
          }
        }
        let _ = file.seek(SeekFrom::Start(cur));
      }
      _ => return ERRNO_BADF,
    }

    if write_u32(memory, nwritten_ptr, total_written).is_none() {
      return ERRNO_FAULT;
    }
    ERRNO_SUCCESS
  }

  #[fast]
  fn fd_fdstat_set_rights(
    &self,
    #[smi] _fd: i32,
    #[number] _fs_rights_base: i64,
    #[number] _fs_rights_inheriting: i64,
  ) -> i32 {
    ERRNO_SUCCESS
  }
}

/// Resolve the atime/mtime tuple for `*_filestat_set_times`, applying
/// the WASI `_NOW` and absent-bit semantics. When neither the bit for the
/// explicit time nor the `_NOW` bit is set for a given component, the
/// existing time on the file is preserved.
fn resolve_filestat_times_from_meta(
  meta: &std::fs::Metadata,
  atim_ns: u64,
  mtim_ns: u64,
  fst_flags: u16,
) -> Result<(filetime::FileTime, filetime::FileTime), i32> {
  // Both *_NOW and the explicit bit set is invalid in WASI.
  if (fst_flags & FSTFLAGS_ATIM != 0 && fst_flags & FSTFLAGS_ATIM_NOW != 0)
    || (fst_flags & FSTFLAGS_MTIM != 0 && fst_flags & FSTFLAGS_MTIM_NOW != 0)
  {
    return Err(ERRNO_INVAL);
  }

  let now = std::time::SystemTime::now();

  let atime = if fst_flags & FSTFLAGS_ATIM_NOW != 0 {
    filetime::FileTime::from_system_time(now)
  } else if fst_flags & FSTFLAGS_ATIM != 0 {
    filetime::FileTime::from_unix_time(
      (atim_ns / 1_000_000_000) as i64,
      (atim_ns % 1_000_000_000) as u32,
    )
  } else {
    filetime::FileTime::from_last_access_time(meta)
  };

  let mtime = if fst_flags & FSTFLAGS_MTIM_NOW != 0 {
    filetime::FileTime::from_system_time(now)
  } else if fst_flags & FSTFLAGS_MTIM != 0 {
    filetime::FileTime::from_unix_time(
      (mtim_ns / 1_000_000_000) as i64,
      (mtim_ns % 1_000_000_000) as u32,
    )
  } else {
    filetime::FileTime::from_last_modification_time(meta)
  };

  Ok((atime, mtime))
}

/// Build an FdEntry for one of the WASI stdio slots from a user-supplied
/// host fd. Returns None for fd 0/1/2 (use inherited process stdio) and
/// for any fd we can't dup or resolve. The dup'd file owns its own OS
/// handle so the user-side fd stays open after WASI exits.
///
/// Permissions: we only accept fds that node:fs has registered in
/// `deno_io::FdTable`, so an opener went through Deno's permission system
/// (op_node_open_sync). Naked OS fds inherited from the parent process or
/// internal Deno opens are rejected so the WASI constructor can't become
/// an escape hatch around `--allow-read` / `--allow-write`.
fn make_stdio_entry(
  state: &mut OpState,
  user_fd: i32,
  is_stdin: bool,
) -> Option<FdEntry> {
  match user_fd {
    0..=2 => None,
    fd if fd < 0 => None,
    fd => {
      if !state.borrow::<deno_io::FdTable>().contains(fd) {
        return None;
      }
      dup_user_fd_to_file(state, fd)
        .map(|file| FdEntry::HostFile { file, is_stdin })
    }
  }
}

/// Duplicate a user-supplied node:fs fd into a `std::fs::File`. The dup
/// gives us an independent OS handle that we close on WASI fd_close /
/// drop, leaving the user's original fd untouched.
fn dup_user_fd_to_file(
  state: &mut OpState,
  user_fd: i32,
) -> Option<std::fs::File> {
  // Try the host fd path first. node:fs.openSync hands the caller the
  // real OS fd (Unix) or CRT fd (Windows), so libc::dup is the most direct
  // way to capture an independent owning handle.
  #[cfg(unix)]
  {
    use std::os::fd::FromRawFd;
    // Use F_DUPFD_CLOEXEC instead of plain dup so the dup'd fd is marked
    // close-on-exec. A WASI program that spawns a child process would
    // otherwise leak this host fd into the child, which is exactly the
    // kind of escape hatch we just closed off in make_stdio_entry.
    // SAFETY: fcntl(F_DUPFD_CLOEXEC, 0) is always safe to call. It either
    // returns -1 on error or a fresh fd (>=0) owned by the caller.
    let new_fd = unsafe { libc::fcntl(user_fd, libc::F_DUPFD_CLOEXEC, 0) };
    if new_fd >= 0 {
      // SAFETY: new_fd was just returned by fcntl(F_DUPFD_CLOEXEC), so it's
      // a fresh OS file descriptor we now own. Wrapping it in std::fs::File
      // transfers ownership to the returned File, which closes the fd on
      // Drop.
      return Some(unsafe { std::fs::File::from_raw_fd(new_fd) });
    }
    let _ = state;
    None
  }
  #[cfg(windows)]
  {
    use std::os::windows::io::FromRawHandle;
    // SAFETY: _get_osfhandle on a CRT fd either returns INVALID_HANDLE_VALUE
    // (-1) or a kernel HANDLE. We duplicate that handle so the resulting
    // OwnedHandle / File owns an independent kernel handle.
    let raw_handle = unsafe { libc::get_osfhandle(user_fd) };
    if raw_handle == -1 {
      let _ = state;
      return None;
    }
    use windows_sys::Win32::Foundation::CloseHandle;
    use windows_sys::Win32::Foundation::DUPLICATE_SAME_ACCESS;
    use windows_sys::Win32::Foundation::DuplicateHandle;
    use windows_sys::Win32::System::Threading::GetCurrentProcess;
    let mut dup_handle = std::ptr::null_mut();
    // SAFETY: raw_handle is a valid kernel handle from the CRT.
    let ok = unsafe {
      DuplicateHandle(
        GetCurrentProcess(),
        raw_handle as _,
        GetCurrentProcess(),
        &mut dup_handle,
        0,
        0,
        DUPLICATE_SAME_ACCESS,
      )
    };
    if ok == 0 {
      let _ = state;
      return None;
    }
    let file =
      // SAFETY: dup_handle is a fresh duplicated handle owned by us.
      unsafe { std::fs::File::from_raw_handle(dup_handle as _) };
    let _ = CloseHandle;
    let _ = state;
    Some(file)
  }
}

struct PollSubscription {
  userdata: u64,
  tag: u8,
  clock_id: i32,
  clock_timeout: u64,
  #[allow(
    dead_code,
    reason = "WASI subscription field, not currently consulted"
  )]
  clock_precision: u64,
  clock_flags: u16,
  fd: i32,
}

struct PollEvent {
  userdata: u64,
  error: u16,
  ty: u8,
  nbytes: u64,
  flags: u16,
}

fn read_subscriptions(
  memory: &[u8],
  in_ptr: i32,
  nsubscriptions: i32,
) -> Result<Vec<PollSubscription>, i32> {
  let mut subs = Vec::with_capacity(nsubscriptions as usize);
  // subscription layout:
  //   userdata: u64                                   offset 0
  //   subscription_u {
  //     tag: u8 (+7 pad)                              offset 8
  //     u {                                           offset 16
  //       clock { id u32, _pad u32, timeout u64,
  //               precision u64, flags u16 (+6 pad) }
  //       fd_readwrite { file_descriptor u32 }
  //     }
  //   }
  // total size: 48 bytes
  for i in 0..nsubscriptions {
    let base = in_ptr + i * 48;
    let userdata = read_u64(memory, base).ok_or(ERRNO_FAULT)?;
    let tag = *memory.get((base + 8) as usize).ok_or(ERRNO_FAULT)?;
    let u_base = base + 16;
    let (clock_id, clock_timeout, clock_precision, clock_flags, fd) = match tag
    {
      EVENTTYPE_CLOCK => {
        let id = read_u32(memory, u_base).ok_or(ERRNO_FAULT)? as i32;
        let timeout = read_u64(memory, u_base + 8).ok_or(ERRNO_FAULT)?;
        let precision = read_u64(memory, u_base + 16).ok_or(ERRNO_FAULT)?;
        let flags = read_u16(memory, u_base + 24).ok_or(ERRNO_FAULT)?;
        (id, timeout, precision, flags, 0)
      }
      EVENTTYPE_FD_READ | EVENTTYPE_FD_WRITE => {
        let fd = read_u32(memory, u_base).ok_or(ERRNO_FAULT)? as i32;
        (0, 0, 0, 0, fd)
      }
      _ => (0, 0, 0, 0, 0),
    };
    subs.push(PollSubscription {
      userdata,
      tag,
      clock_id,
      clock_timeout,
      clock_precision,
      clock_flags,
      fd,
    });
  }
  Ok(subs)
}

fn read_u64(memory: &[u8], offset: i32) -> Option<u64> {
  let bytes = get_memory_slice(memory, offset, 8)?;
  Some(u64::from_le_bytes(bytes.try_into().ok()?))
}

fn read_u16(memory: &[u8], offset: i32) -> Option<u16> {
  let bytes = get_memory_slice(memory, offset, 2)?;
  Some(u16::from_le_bytes([bytes[0], bytes[1]]))
}

fn write_filestat(memory: &mut [u8], offset: i32, meta: &std::fs::Metadata) {
  use std::time::UNIX_EPOCH;

  let filetype = if meta.is_dir() {
    FILETYPE_DIRECTORY
  } else if meta.is_file() {
    FILETYPE_REGULAR_FILE
  } else if meta.file_type().is_symlink() {
    FILETYPE_SYMBOLIC_LINK
  } else {
    FILETYPE_UNKNOWN
  };
  write_u8(memory, offset + 16, filetype);
  write_u64(memory, offset + 24, 1); // nlink
  write_u64(memory, offset + 32, meta.len());

  if let Ok(Ok(d)) = meta.accessed().map(|t| t.duration_since(UNIX_EPOCH)) {
    write_u64(memory, offset + 40, d.as_nanos() as u64);
  }
  if let Ok(Ok(d)) = meta.modified().map(|t| t.duration_since(UNIX_EPOCH)) {
    write_u64(memory, offset + 48, d.as_nanos() as u64);
  }
  if let Ok(Ok(d)) = meta.created().map(|t| t.duration_since(UNIX_EPOCH)) {
    write_u64(memory, offset + 56, d.as_nanos() as u64);
  }
}