wasm-rquickjs 0.4.4

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
import * as pathModule from 'node:path';
import * as pathPosix from 'node:path/posix';
import * as pathWin32 from 'node:path/win32';
import * as fsModule from 'node:fs';
import * as util from 'node:util';
import * as buffer from 'node:buffer';
import * as os from 'node:os';
import * as events from 'node:events';
import * as stream from 'node:stream';
import * as streamPromises from 'node:stream/promises';
import * as streamConsumers from 'node:stream/consumers';
import * as streamWeb from 'node:stream/web';
import * as crypto from 'node:crypto';
import * as child_process from 'node:child_process';
import * as string_decoder from 'node:string_decoder';
import * as processModule from 'node:process';
import * as assert from 'node:assert';
import * as assertStrict from 'node:assert/strict';
import * as fsPromises from 'node:fs/promises';
import * as nodeTest from 'node:test';
import * as querystring from 'node:querystring';
import * as punycode from 'node:punycode';
import * as nodeUrl from 'node:url';
import * as vm from 'node:vm';
import * as timers from 'node:timers';
import * as timersPromises from 'node:timers/promises';
import * as consoleMod from 'node:console';
import * as async_hooks from 'node:async_hooks';
import * as cluster from 'node:cluster';
import * as constants from 'node:constants';
import * as dgram from 'node:dgram';
import * as diagnostics_channel from 'node:diagnostics_channel';
import * as dns from 'node:dns';
import * as dnsPromises from 'node:dns/promises';
import * as domain from 'node:domain';
import * as httpCommon from 'node:_http_common';
import * as httpAgent from 'node:_http_agent';
import * as http from 'node:http';
import * as http2 from 'node:http2';
import * as https from 'node:https';
import * as net from 'node:net';
import * as perf_hooks from 'node:perf_hooks';
import * as readline from 'node:readline';
import * as readlinePromises from 'node:readline/promises';
import * as repl from 'node:repl';
import * as trace_events from 'node:trace_events';
import * as tls from 'node:tls';
import * as tty from 'node:tty';
import * as v8 from 'node:v8';
import * as worker_threads from 'node:worker_threads';
import * as zlib from 'node:zlib';
import * as sqlite from 'node:sqlite';
import * as internalHttp from '__wasm_rquickjs_builtin/internal/http';
import { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS } from '__wasm_rquickjs_builtin/internal/errors';
import * as internalErrors from '__wasm_rquickjs_builtin/internal/errors';
import * as internalFsUtils from '__wasm_rquickjs_builtin/internal/fs/utils';
import * as internalUrl from '__wasm_rquickjs_builtin/internal/url';
import * as internalUtil from '__wasm_rquickjs_builtin/internal/util';
import * as internalUtilDebuglog from '__wasm_rquickjs_builtin/internal/util/debuglog';
import * as internalWebstreamsUtil from '__wasm_rquickjs_builtin/internal/webstreams/util';
import * as internalStreamsAddAbortSignal from '__wasm_rquickjs_builtin/internal/streams/add-abort-signal';
import * as internalStreamsState from '__wasm_rquickjs_builtin/internal/streams/state';
import * as internalTestBinding from '__wasm_rquickjs_builtin/internal/test/binding';
import { eval_with_filename as _evalWithFilename, require_esm as _requireEsm } from '__wasm_rquickjs_builtin/vm_native';
import {
    transform_typescript as transformTypeScriptNative,
    transform_typescript_module as transformTypeScriptModuleNative,
    test_observability_enabled as testObservabilityEnabledNative,
} from '__wasm_rquickjs_builtin/typescript_native';

const objectPrototypeHasOwnProperty = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
const objectDefineProperty = Object.defineProperty.bind(Object);
const objectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
const stringFromCodePoint = String.fromCodePoint.bind(String);
const numberParseInt = Number.parseInt.bind(Number);
const wasmRquickjsModuleGlobalThis = globalThis;
const wasmRquickjsModulePromiseResolve = Promise.resolve.bind(Promise);
const wasmRquickjsModuleEval = eval;

function cjsFacadeHasOwnProperty(value, key) {
    return objectPrototypeHasOwnProperty(value, key);
}

Object.defineProperty(globalThis, '__wasm_rquickjs_cjs_facade_has_own', {
    value: cjsFacadeHasOwnProperty,
    writable: false,
    configurable: false,
});

// CJS require() should return the default export (the "module object") when one
// exists, not the ESM namespace wrapper.  When the default export is a function
// or object, named exports are also attached to it so that both
// `require('mod')()` and `const { namedExport } = require('mod')` work — this
// mirrors Node.js CJS/ESM interop behaviour.
function cjsExport(ns) {
    if (!ns || ns.default === undefined) return ns;
    const def = ns.default;
    if (typeof def === 'function' || (typeof def === 'object' && def !== null)) {
        const keys = Object.keys(ns);
        for (let i = 0; i < keys.length; i++) {
            const k = keys[i];
            if (k !== 'default' && !(k in def)) {
                def[k] = ns[k];
            }
        }
    }
    return def;
}

// Precompute cjsExport results once per namespace to avoid redundant calls
const pathCjs = cjsExport(pathModule);
const pathPosixCjs = cjsExport(pathPosix);
const pathWin32Cjs = cjsExport(pathWin32);
const fsCjs = cjsExport(fsModule);
const fsPromisesCjs = cjsExport(fsPromises);
const utilCjs = cjsExport(util);
const bufferCjs = cjsExport(buffer);
const osCjs = cjsExport(os);
const eventsCjs = cjsExport(events);
const streamCjs = cjsExport(stream);
const streamPromisesCjs = cjsExport(streamPromises);
const streamConsumersCjs = cjsExport(streamConsumers);
const streamWebCjs = cjsExport(streamWeb);
const childProcessCjs = cjsExport(child_process);
const stringDecoderCjs = cjsExport(string_decoder);
const processCjs = cjsExport(processModule);
const assertCjs = cjsExport(assert);
const assertStrictCjs = cjsExport(assertStrict);
const nodeTestCjs = cjsExport(nodeTest);
const querystringCjs = cjsExport(querystring);
const punycodeCjs = cjsExport(punycode);
const nodeUrlCjs = cjsExport(nodeUrl);
const vmCjs = cjsExport(vm);
const timersCjs = cjsExport(timers);
const timersPromisesCjs = cjsExport(timersPromises);
const consoleCjs = cjsExport(consoleMod);
const asyncHooksCjs = cjsExport(async_hooks);
const clusterCjs = cjsExport(cluster);
const constantsCjs = cjsExport(constants);
const dgramCjs = cjsExport(dgram);
const diagnosticsChannelCjs = cjsExport(diagnostics_channel);
const moduleRequireTrace = diagnostics_channel.tracingChannel('module.require');
const moduleImportTrace = diagnostics_channel.tracingChannel('module.import');
const dnsCjs = cjsExport(dns);
const dnsPromisesCjs = cjsExport(dnsPromises);
const domainCjs = cjsExport(domain);
const httpCommonCjs = cjsExport(httpCommon);
const httpAgentCjs = cjsExport(httpAgent);
const httpCjs = cjsExport(http);
const http2Cjs = cjsExport(http2);
const httpsCjs = cjsExport(https);
const netCjs = cjsExport(net);
const perfHooksCjs = cjsExport(perf_hooks);
const readlineCjs = cjsExport(readline);
const readlinePromisesCjs = cjsExport(readlinePromises);
const replCjs = cjsExport(repl);
const traceEventsCjs = cjsExport(trace_events);
const tlsCjs = cjsExport(tls);
const ttyCjs = cjsExport(tty);
const v8Cjs = cjsExport(v8);
const workerThreadsCjs = cjsExport(worker_threads);
const zlibCjs = cjsExport(zlib);
const sqliteCjs = cjsExport(sqlite);
const internalHttpCjs = cjsExport(internalHttp);
const internalFsUtilsCjs = cjsExport(internalFsUtils);
const internalUrlCjs = cjsExport(internalUrl);
const internalErrorsCjs = cjsExport(internalErrors);
const internalUtilCjs = cjsExport(internalUtil);
const internalUtilDebuglogCjs = cjsExport(internalUtilDebuglog);
const internalWebstreamsUtilCjs = cjsExport(internalWebstreamsUtil);
const internalStreamsAddAbortSignalCjs = cjsExport(internalStreamsAddAbortSignal);
const internalStreamsStateCjs = cjsExport(internalStreamsState);
const internalTestBindingCjs = cjsExport(internalTestBinding);

const utilTypes = (utilCjs && utilCjs.types) || {};

const cryptoCjs = (() => {
    const out = {};
    const keys = Object.keys(crypto);
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        out[key] = crypto[key];
    }

    ['pseudoRandomBytes', 'prng', 'rng'].forEach((name) => {
        if (Object.prototype.hasOwnProperty.call(out, name)) {
            Object.defineProperty(out, name, {
                value: out[name],
                writable: true,
                configurable: true,
                enumerable: false,
            });
        }
    });

    return out;
})();

// Build the builtin module map with both bare and node:-prefixed keys.
// Helper to register a module under both 'name' and 'node:name'.
function registerBuiltin(map, name, value) {
    map[name] = value;
    map['node:' + name] = value;
}

const builtinModuleMap = {};
registerBuiltin(builtinModuleMap, 'path', pathCjs);
registerBuiltin(builtinModuleMap, 'path/posix', pathPosixCjs);
registerBuiltin(builtinModuleMap, 'path/win32', pathWin32Cjs);
registerBuiltin(builtinModuleMap, 'fs', fsCjs);
registerBuiltin(builtinModuleMap, 'fs/promises', fsPromisesCjs);
builtinModuleMap['internal/fs/promises'] = fsPromisesCjs;
registerBuiltin(builtinModuleMap, 'util', utilCjs);
registerBuiltin(builtinModuleMap, 'sys', utilCjs);
registerBuiltin(builtinModuleMap, 'buffer', bufferCjs);
registerBuiltin(builtinModuleMap, 'os', osCjs);
registerBuiltin(builtinModuleMap, 'events', eventsCjs);
registerBuiltin(builtinModuleMap, 'stream', streamCjs);
registerBuiltin(builtinModuleMap, 'stream/promises', streamPromisesCjs);
registerBuiltin(builtinModuleMap, 'stream/consumers', streamConsumersCjs);
registerBuiltin(builtinModuleMap, 'stream/web', streamWebCjs);
registerBuiltin(builtinModuleMap, 'crypto', cryptoCjs);
registerBuiltin(builtinModuleMap, 'child_process', childProcessCjs);
registerBuiltin(builtinModuleMap, 'string_decoder', stringDecoderCjs);
registerBuiltin(builtinModuleMap, 'process', processCjs);
registerBuiltin(builtinModuleMap, 'assert', assertCjs);
registerBuiltin(builtinModuleMap, 'assert/strict', assertStrictCjs);
registerBuiltin(builtinModuleMap, 'test', nodeTestCjs);
registerBuiltin(builtinModuleMap, 'querystring', querystringCjs);
registerBuiltin(builtinModuleMap, 'punycode', punycodeCjs);
registerBuiltin(builtinModuleMap, 'url', nodeUrlCjs);
registerBuiltin(builtinModuleMap, 'vm', vmCjs);
registerBuiltin(builtinModuleMap, 'timers', timersCjs);
registerBuiltin(builtinModuleMap, 'timers/promises', timersPromisesCjs);
Object.defineProperty(builtinModuleMap, 'console', {
    get() {
        const c = globalThis.console;
        if (c && consoleMod.Console) c.Console = consoleMod.Console;
        return c || consoleCjs;
    },
    configurable: true,
    enumerable: true,
});
Object.defineProperty(builtinModuleMap, 'node:console', {
    get() {
        return builtinModuleMap['console'];
    },
    configurable: true,
    enumerable: true,
});
registerBuiltin(builtinModuleMap, 'async_hooks', asyncHooksCjs);
registerBuiltin(builtinModuleMap, 'cluster', clusterCjs);
registerBuiltin(builtinModuleMap, 'constants', constantsCjs);
registerBuiltin(builtinModuleMap, 'dgram', dgramCjs);
registerBuiltin(builtinModuleMap, 'diagnostics_channel', diagnosticsChannelCjs);
registerBuiltin(builtinModuleMap, 'dns', dnsCjs);
registerBuiltin(builtinModuleMap, 'dns/promises', dnsPromisesCjs);
registerBuiltin(builtinModuleMap, 'domain', domainCjs);
registerBuiltin(builtinModuleMap, '_http_common', httpCommonCjs);
registerBuiltin(builtinModuleMap, '_http_agent', httpAgentCjs);
registerBuiltin(builtinModuleMap, 'http', httpCjs);
registerBuiltin(builtinModuleMap, 'http2', http2Cjs);
registerBuiltin(builtinModuleMap, 'https', httpsCjs);
registerBuiltin(builtinModuleMap, 'net', netCjs);
registerBuiltin(builtinModuleMap, 'perf_hooks', perfHooksCjs);
registerBuiltin(builtinModuleMap, 'readline', readlineCjs);
registerBuiltin(builtinModuleMap, 'readline/promises', readlinePromisesCjs);
registerBuiltin(builtinModuleMap, 'repl', replCjs);
registerBuiltin(builtinModuleMap, 'tls', tlsCjs);
registerBuiltin(builtinModuleMap, 'trace_events', traceEventsCjs);
registerBuiltin(builtinModuleMap, 'tty', ttyCjs);
registerBuiltin(builtinModuleMap, 'v8', v8Cjs);
registerBuiltin(builtinModuleMap, 'worker_threads', workerThreadsCjs);
registerBuiltin(builtinModuleMap, 'zlib', zlibCjs);
builtinModuleMap['node:sqlite'] = sqliteCjs;
registerBuiltin(builtinModuleMap, 'util/types', utilTypes);
registerBuiltin(builtinModuleMap, '_stream_readable', streamCjs && streamCjs.Readable);
registerBuiltin(builtinModuleMap, '_stream_writable', streamCjs && streamCjs.Writable);
registerBuiltin(builtinModuleMap, '_stream_duplex', streamCjs && streamCjs.Duplex);
registerBuiltin(builtinModuleMap, '_stream_transform', streamCjs && streamCjs.Transform);
registerBuiltin(builtinModuleMap, '_stream_passthrough', streamCjs && streamCjs.PassThrough);
builtinModuleMap['internal/http'] = internalHttpCjs;
builtinModuleMap['internal/fs/utils'] = internalFsUtilsCjs;
builtinModuleMap['internal/url'] = internalUrlCjs;
builtinModuleMap['internal/errors'] = internalErrorsCjs;
builtinModuleMap['internal/util'] = internalUtilCjs;
builtinModuleMap['internal/util/debuglog'] = internalUtilDebuglogCjs;
builtinModuleMap['internal/webstreams/util'] = internalWebstreamsUtilCjs;
builtinModuleMap['internal/streams/add-abort-signal'] = internalStreamsAddAbortSignalCjs;
builtinModuleMap['internal/streams/state'] = internalStreamsStateCjs;
builtinModuleMap['internal/test/binding'] = internalTestBindingCjs;

// --- Module mock registry (used by node:test mock.module()) ---
const _moduleMockRegistry = Object.create(null);
const _moduleMockRegistryById = Object.create(null);
let _moduleMockNextId = 1;
Object.defineProperty(globalThis, '__wasm_rquickjs_module_mocks', {
    value: _moduleMockRegistry,
    writable: false,
    configurable: false,
});

function _mockCanonicalKey(specifier, base) {
    if (typeof specifier === 'object' && specifier !== null && typeof specifier.href === 'string') {
        specifier = specifier.href;
    }
    if (typeof specifier !== 'string') return null;

    const builtinSpecifier = builtinResolveSpecifier(specifier);
    if (builtinSpecifier !== undefined) {
        return 'builtin:' + builtinSpecifier.slice(5);
    }

    // file:// URL
    if (specifier.startsWith('file://')) {
        try {
            const filePath = nodeUrl.fileURLToPath(specifier);
            return 'path:' + pathModule.resolve(filePath);
        } catch (e) {
            return 'path:' + specifier;
        }
    }

    // Absolute path
    if (specifier.startsWith('/')) {
        return 'path:' + pathModule.resolve(specifier);
    }

    // Relative path — resolve against base (from ESM resolver) or current module context
    if (specifier.startsWith('./') || specifier.startsWith('../')) {
        let baseDir = '/';
        if (typeof base === 'string' && base) {
            try {
                if (base.startsWith('file://')) {
                    baseDir = pathModule.dirname(nodeUrl.fileURLToPath(base));
                } else {
                    baseDir = pathModule.dirname(base);
                }
            } catch (e) {
                // fall through to context
            }
        }
        if (baseDir === '/') {
            const ctx = globalThis.__wasm_rquickjs_current_module;
            if (ctx && ctx.filename) {
                baseDir = pathModule.dirname(ctx.filename);
            }
        }
        return 'path:' + pathModule.resolve(baseDir, specifier);
    }

    // Bare specifier (could be node_modules)
    return 'bare:' + specifier;
}

function _detectMockModuleKind(canonicalKey) {
    if (!canonicalKey) return 'esm';
    if (canonicalKey.startsWith('builtin:')) return 'cjs';
    if (!canonicalKey.startsWith('path:')) return 'esm';
    const filename = canonicalKey.slice(5);
    if (filename.endsWith('.mjs') || filename.endsWith('.mts')) return 'esm';
    // Default to CJS for .js, .cjs, and everything else
    return 'cjs';
}

function _materializeCjsMock(entry) {
    let result;
    const hasDefault = 'defaultExport' in entry;
    const hasNamed = entry.namedExports !== undefined;

    if (hasDefault) {
        result = entry.defaultExport;
    } else {
        result = {};
    }

    if (hasNamed) {
        if (result === null || (typeof result !== 'object' && typeof result !== 'function')) {
            const err = new Error('Cannot create mock: named exports cannot be applied to non-object defaultExport');
            err.code = 'ERR_INVALID_STATE';
            throw err;
        }
        const keys = Object.keys(entry.namedExports);
        for (let i = 0; i < keys.length; i++) {
            result[keys[i]] = entry.namedExports[keys[i]];
        }
    }

    return result;
}

function _registerModuleMock(specifier, options) {
    const key = _mockCanonicalKey(specifier);
    if (!key) return null;

    if (_moduleMockRegistry[key]) {
        const err = new Error('The module is already mocked');
        err.code = 'ERR_INVALID_STATE';
        throw err;
    }

    const id = _moduleMockNextId++;
    const kind = _detectMockModuleKind(key);
    const entry = {
        id: id,
        canonicalKey: key,
        specifier: specifier,
        kind: kind,
        namedExports: options.namedExports,
        cache: options.cache !== undefined ? options.cache : false,
        _cachedCjsResult: undefined,
        _cachedCjsReady: false,
    };
    if ('defaultExport' in options) {
        entry.defaultExport = options.defaultExport;
    }

    _moduleMockRegistry[key] = entry;
    _moduleMockRegistryById[id] = entry;

    return {
        canonicalKey: key,
        id: id,
        restore: function() {
            delete _moduleMockRegistry[key];
            delete _moduleMockRegistryById[id];
            // Clean up ESM storage
            const storageKey = '__wasm_rquickjs_mock_data_' + id;
            delete globalThis[storageKey];
            entry._cachedCjsResult = undefined;
            entry._cachedCjsReady = false;
        }
    };
}

function _resolveRequireMock(id) {
    // CJS require() does not support file:// URLs — don't intercept them
    if (typeof id === 'string' && id.startsWith('file://')) return null;
    const key = _mockCanonicalKey(id);
    if (!key) return null;
    return _moduleMockRegistry[key] || null;
}

function _hasImportMock(specifier, base) {
    const key = _mockCanonicalKey(specifier, base);
    return !!(key && _moduleMockRegistry[key]);
}

Object.defineProperty(globalThis, '__wasm_rquickjs_mock_canonical_key', {
    value: _mockCanonicalKey,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_register_module_mock', {
    value: _registerModuleMock,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_resolve_require_mock', {
    value: _resolveRequireMock,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_materialize_cjs_mock', {
    value: _materializeCjsMock,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_has_import_mock', {
    value: _hasImportMock,
    writable: false,
    configurable: false,
});

function traceModuleRequire(id, parentFilename, fn) {
    if (globalThis.__wasm_rquickjs_suppress_module_require_diagnostics) {
        return fn();
    }
    if (!moduleRequireTrace.hasSubscribers) {
        return fn();
    }
    return moduleRequireTrace.traceSync(fn, {
        id,
        parentFilename,
    });
}

function traceModuleImport(url, parentFilename, fn) {
    if (!parentFilename || !moduleImportTrace.hasSubscribers) return fn();
    return moduleImportTrace.tracePromise(fn, {
        url,
        parentURL: nodeUrl.pathToFileURL(parentFilename).href,
    });
}

function dynamicImportWithTrace(parentFilename, baseUrl, specifier, options, hasOptions, importer) {
    const url = String(specifier);
    if (hasOptions) {
        const parsedOptions = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_import_attr_read_options(options);
        return traceModuleImport(
            url,
            parentFilename,
            async () => {
                return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_import_attr_dynamic_import_parsed(
                    baseUrl,
                    url,
                    parsedOptions,
                    true,
                    importer,
                );
            },
        );
    }
    return traceModuleImport(
        url,
        parentFilename,
        async () => wasmRquickjsModuleGlobalThis.__wasm_rquickjs_import_attr_dynamic_import(
            baseUrl,
            url,
            undefined,
            true,
            importer,
        ),
    );
}

function dynamicImportReaction(fn) {
    return wasmRquickjsModulePromiseResolve().then(fn);
}

Object.defineProperty(globalThis, '__wasm_rquickjs_trace_module_import', {
    value: traceModuleImport,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_dynamic_import_with_trace', {
    value: dynamicImportWithTrace,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_dynamic_import_reaction', {
    value: dynamicImportReaction,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_prepare_cjs_eval_source', {
    value: prepareCjsEvalSource,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_native_eval', {
    value: wasmRquickjsModuleEval,
    writable: false,
    configurable: false,
});
function withSuppressedModuleRequireDiagnostics(fn) {
    const previous = globalThis.__wasm_rquickjs_suppress_module_require_diagnostics;
    globalThis.__wasm_rquickjs_suppress_module_require_diagnostics = true;
    try {
        return fn();
    } finally {
        if (previous === undefined) {
            delete globalThis.__wasm_rquickjs_suppress_module_require_diagnostics;
        } else {
            globalThis.__wasm_rquickjs_suppress_module_require_diagnostics = previous;
        }
    }
}
Object.defineProperty(globalThis, '__wasm_rquickjs_with_suppressed_module_require_diagnostics', {
    value: withSuppressedModuleRequireDiagnostics,
    writable: false,
    configurable: false,
});

// Lookup mock entry by ID (for ESM source generation)
function getMockModuleEntry(mockId) {
    return _moduleMockRegistryById[mockId] || null;
}

Object.defineProperty(globalThis, '__wasm_rquickjs_get_mock_module_entry', {
    value: getMockModuleEntry,
    writable: false,
    configurable: false,
});

// Generate ESM module source for a mock entry (called from Rust MockModuleLoader)
function getMockModuleSource(mockId) {
    const entry = _moduleMockRegistryById[mockId];
    if (!entry) {
        throw new Error('Mock entry not found for id: ' + mockId);
    }
    return _generateMockEsmSource(entry);
}

Object.defineProperty(globalThis, '__wasm_rquickjs_get_mock_module_source', {
    value: getMockModuleSource,
    writable: false,
    configurable: false,
});

function _generateMockEsmSource(entry) {
    const storageKey = '__wasm_rquickjs_mock_data_' + entry.id;
    globalThis[storageKey] = entry;

    const lines = [];
    lines.push('var __entry = globalThis["' + storageKey + '"];');
    lines.push('var __named = __entry.namedExports;');
    lines.push('var __hasDefault = "defaultExport" in __entry;');

    if (entry.kind === 'cjs') {
        // CJS-style mock: default export is the materialized object with named exports applied
        lines.push('var __result;');
        lines.push('if (__hasDefault) { __result = __entry.defaultExport; } else { __result = {}; }');
        lines.push('if (__named) {');
        lines.push('  if (__result === null || (typeof __result !== "object" && typeof __result !== "function")) {');
        lines.push('    await Promise.reject(new Error("Cannot create mock: named exports cannot be applied to non-object defaultExport"));');
        lines.push('  }');
        lines.push('  var __nk = Object.keys(__named);');
        lines.push('  for (var __i = 0; __i < __nk.length; __i++) { __result[__nk[__i]] = __named[__nk[__i]]; }');
        lines.push('}');
        lines.push('export default __result;');
        // Also export named entries individually for ESM consumers
        if (entry.namedExports) {
            const nkeys = Object.keys(entry.namedExports);
            for (let i = 0; i < nkeys.length; i++) {
                const k = nkeys[i];
                if (k === 'default') continue;
                if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(k)) {
                    lines.push('export var ' + k + ' = __named["' + k + '"];');
                }
            }
        }
    } else {
        // ESM-style mock: named exports are independent, default is separate
        if (entry.namedExports) {
            const nkeys = Object.keys(entry.namedExports);
            for (let i = 0; i < nkeys.length; i++) {
                const k = nkeys[i];
                if (k === 'default') {
                    lines.push('export default __named["default"];');
                } else if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(k)) {
                    lines.push('export var ' + k + ' = __named["' + k + '"];');
                }
            }
        }
        // Add default export if not already handled via namedExports
        if (!entry.namedExports || !entry.namedExports.hasOwnProperty('default')) {
            lines.push('var __defVal = __hasDefault ? __entry.defaultExport : undefined;');
            lines.push('export { __defVal as default };');
        }
    }

    return lines.join('\n');
}

// Self-reference will be added after the module object is created (see bottom of file)

function setFromArray(values, mapper) {
    const set = new Set();
    for (let i = 0; i < values.length; i++) {
        set.add(mapper ? mapper(values, i) : values[i]);
    }
    return set;
}

// Modules that require the 'node:' prefix (cannot be required as bare specifiers)
const schemelessBlockList = setFromArray(['test', 'sqlite']);

const builtinModuleNames = Object.keys(builtinModuleMap).filter(
    (name) => !name.startsWith('node:') && !name.startsWith('internal/') &&
        !name.startsWith('_') && !schemelessBlockList.has(name)
);

const publicBuiltinIdSet = new Set();
for (const name of Object.keys(builtinModuleMap)) {
    if (name.startsWith('node:')) {
        publicBuiltinIdSet.add(name.slice(5));
    } else if (!name.startsWith('internal/')) {
        publicBuiltinIdSet.add(name);
    }
}

function builtinResolveSpecifier(id) {
    if (typeof id !== 'string') return undefined;
    if (id.startsWith('node:')) {
        return publicBuiltinIdSet.has(id.slice(5)) ? id : undefined;
    }
    if (id.startsWith('internal/') || schemelessBlockList.has(id)) {
        return undefined;
    }
    return publicBuiltinIdSet.has(id) ? 'node:' + id : undefined;
}

function isBuiltin(id) {
    return builtinResolveSpecifier(id) !== undefined;
}

function builtinModuleForSpecifier(id) {
    if (typeof id !== 'string' || schemelessBlockList.has(id)) return undefined;
    if (objectPrototypeHasOwnProperty(builtinModuleMap, id)) return builtinModuleMap[id];
    return undefined;
}

function requireBuiltinModule(id) {
    const builtin = builtinModuleForSpecifier(id);
    if (builtin !== undefined) return builtin;
    if (typeof id === 'string' && id.startsWith('node:')) {
        const err = new Error('No such built-in module: ' + id);
        err.code = 'ERR_UNKNOWN_BUILTIN_MODULE';
        throw err;
    }
    return undefined;
}

Object.defineProperty(globalThis, '__wasm_rquickjs_import_meta_resolve_builtin', {
    value: builtinResolveSpecifier,
    writable: false,
    configurable: false,
});

// Runtime-local CommonJS cache: resolved absolute path -> Module object. Like
// Node's require.cache, it does not stat or hash files after first load. A new
// QuickJS runtime (including every wasm-rquickjs execution job) starts empty;
// within one runtime callers must delete the entry to observe an updated file.
const moduleCache = Object.create(null);
let moduleExportsInitialized = false;

function currentModuleCache() {
    return moduleExportsInitialized ? moduleExports._cache : moduleCache;
}

function shouldPreserveSymlinks(isMainModuleLoad) {
    return rustHasExecArgvFlag(isMainModuleLoad ? '--preserve-symlinks-main' : '--preserve-symlinks');
}

function toCjsCanonicalFilename(filename, isMainModuleLoad) {
    if (shouldPreserveSymlinks(isMainModuleLoad)) return filename;
    return fsModule.realpathSync.native(filename);
}

function tryReadFile(filename) {
    try {
        return fsModule.readFileSync(filename, 'utf8');
    } catch (e) {
        return null;
    }
}

const packageJsonParseCache = Object.create(null);

function readPackageJson(pkgJsonPath) {
    if (Object.prototype.hasOwnProperty.call(packageJsonParseCache, pkgJsonPath)) {
        return packageJsonParseCache[pkgJsonPath];
    }
    const content = tryReadFile(pkgJsonPath);
    if (content === null) return null;
    const entry = { path: pkgJsonPath, content, pkg: JSON.parse(content) };
    packageJsonParseCache[pkgJsonPath] = entry;
    return entry;
}

// Shared require.extensions registry (mirrors Node.js Module._extensions)
const requireExtensions = Object.create(null);
const defaultJsExtensionHandler = function _defaultJs(mod, filename) { /* built-in */ };
const defaultJsonExtensionHandler = function _defaultJson(mod, filename) { /* built-in */ };
const defaultNodeExtensionHandler = function _defaultNode(mod, filename) { /* built-in */ };
requireExtensions['.js'] = defaultJsExtensionHandler;
requireExtensions['.json'] = defaultJsonExtensionHandler;
requireExtensions['.node'] = defaultNodeExtensionHandler;
const _defaultExtHandlers = setFromArray([
    defaultJsExtensionHandler,
    defaultJsonExtensionHandler,
    defaultNodeExtensionHandler,
]);

function cjsPathCacheObject() {
    return moduleExports._pathCache;
}

function cjsPathCacheValue(key) {
    return cjsPathCacheObject()[key];
}

function cjsSetPathCacheValue(key, filename) {
    cjsPathCacheObject()[key] = filename;
}

function cjsSetPathCacheResolvedFilename(key, filename) {
    cjsSetPathCacheValue(key, toCjsCanonicalFilename(filename, false));
}

function cjsCachedPathResolution(filename) {
    if (!filename) return null;
    return { filename, __wasmPathCacheHit: true };
}

function cjsPathCacheKey(id, lookupPaths) {
    return id + '\x00' + lookupPaths.join('\x00');
}

function findLongestRegisteredExtension(filename) {
    const name = pathModule.basename(filename);
    let startIndex = 0;
    let index;
    while ((index = name.indexOf('.', startIndex)) !== -1) {
        startIndex = index + 1;
        if (index === 0) continue; // Skip leading dot (dotfiles)
        const ext = name.slice(index);
        if (requireExtensions[ext]) return ext;
    }
    return '.js';
}

function getPackageScopeInfo(filename) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_package_scope_info !== 'function') {
        throw new Error('Internal CJS package scope classifier is not initialized');
    }
    const scope = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_package_scope_info(filename);
    return scope == null ? null : scope;
}

function isPathDirectory(filename) {
    try {
        return fsModule.statSync(filename).isDirectory();
    } catch (_) {
        return false;
    }
}

function loadAsFile(candidate, skipExact) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_file_candidate !== 'function') {
        throw new Error('Internal CJS file candidate resolver is not initialized');
    }
    const filename = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_file_candidate(
        candidate,
        Object.keys(requireExtensions),
        !skipExact,
    );
    if (filename === null || filename === undefined) {
        return null;
    }
    return { filename: String(filename) };
}

function loadAsDirectory(candidate, id, parentDir, seen) {
    seen = seen || Object.create(null);
    if (seen[candidate]) return null;
    seen[candidate] = true;

    const pkgJsonPath = pathModule.join(candidate, 'package.json');
    let packageJsonEntry;
    let invalidMain = null;
    try {
        packageJsonEntry = readPackageJson(pkgJsonPath);
    } catch (e) {
        const pkgErr = new Error(
            'Invalid package config ' + pkgJsonPath +
            ' while resolving "' + id + '" from ' + parentDir + '.' +
            (e.message ? ' ' + e.message : '')
        );
        pkgErr.code = 'ERR_INVALID_PACKAGE_CONFIG';
        throw pkgErr;
    }
    if (packageJsonEntry !== null) {
        let pkg;
        pkg = packageJsonEntry.pkg;

        if (Object.prototype.hasOwnProperty.call(pkg, 'main') && typeof pkg.main === 'string' && pkg.main.length > 0) {
            const mainPath = pathModule.resolve(candidate, pkg.main);
            let resolved = loadAsFile(mainPath, false);
            if (resolved !== null) return resolved;
            resolved = loadAsDirectory(mainPath, id, parentDir, seen);
            if (resolved !== null) return resolved;
            invalidMain = { field: pkg.main, path: mainPath };
        }
    }

    const indexResolved = loadAsFile(pathModule.join(candidate, 'index'), true);
    if (indexResolved !== null) {
        emitInvalidMainWarning(pkgJsonPath, invalidMain);
        return indexResolved;
    }
    if (invalidMain !== null) {
        const err = new Error("Cannot find module '" + invalidMain.path + "'. Please verify that the package.json has a valid \"main\" entry");
        err.code = 'MODULE_NOT_FOUND';
        err.path = pkgJsonPath;
        err.requestPath = id;
        throw err;
    }
    return null;
}

function emitInvalidMainWarning(pkgJsonPath, invalidMain) {
    if (invalidMain === null) return;
    const processObject = globalThis.process;
    if (!processObject || typeof processObject.emitWarning !== 'function') return;
    processObject.emitWarning(
        "Invalid 'main' field in '" + pathModule.toNamespacedPath(pkgJsonPath) + "' of '" + invalidMain.field + "'. Please either fix that or report it to the module author",
        'DeprecationWarning',
        'DEP0128'
    );
}

function packageConditions(mode) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_package_global_conditions !== 'function') {
        throw new Error('Internal package condition provider is not initialized');
    }
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_package_global_conditions(mode);
}

function cjsPackageConditions() {
    return packageConditions('cjs-analysis');
}

function esmPackageConditions() {
    return packageConditions('import');
}

function loaderHookConditions() {
    return packageConditions('loader');
}

function resolvePackageWithRustBridge(parentURL, specifier, conditions, mode, missingProviderMessage) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_loader_default_resolve_package !== 'function') {
        throw new Error(missingProviderMessage);
    }
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_loader_default_resolve_package(
        parentURL,
        specifier,
        conditions,
        mode,
    );
}

function makeModuleNotFoundError(id) {
    const err = new Error("Cannot find module '" + id + "'");
    err.code = 'MODULE_NOT_FOUND';
    return err;
}

function makeCjsModuleNotFoundFromErrModuleNotFound(err, fallbackId) {
    const cjsErr = new Error(
        err && typeof err.message === 'string'
            ? err.message
            : "Cannot find module '" + fallbackId + "'"
    );
    cjsErr.code = 'MODULE_NOT_FOUND';
    return cjsErr;
}

function makeCjsResolutionState() {
    return { exactFileCache: Object.create(null) };
}

function resolveExactPackageFile(filename, resolution) {
    if (resolution && Object.prototype.hasOwnProperty.call(resolution.exactFileCache, filename)) {
        const cached = resolution.exactFileCache[filename];
        if (cached !== null) return cached;
        throw makeModuleNotFoundError(filename);
    }
    let exists = false;
    try {
        exists = fsModule.statSync(filename).isFile();
    } catch (_) {}
    if (resolution) {
        resolution.exactFileCache[filename] = exists ? { filename } : null;
    }
    if (exists) return { filename };
    throw makeModuleNotFoundError(filename);
}

function resolvePackageFileFromRustResult(resolved, resolution) {
    if (!resolved || !resolved.url || !String(resolved.url).startsWith('file://')) return undefined;
    return resolveExactPackageFile(nodeUrl.fileURLToPath(String(resolved.url)), resolution);
}

function resolvePackageExportsEntry(parts, packageDir, conditions, resolution) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_exports !== 'function') {
        throw new Error('Internal CJS package exports resolver is not initialized');
    }
    let resolved;
    try {
        resolved = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_exports(
            packageDir,
            parts.name,
            parts.subpath,
            conditions || cjsPackageConditions(),
        );
    } catch (err) {
        if (err && err.code === 'ERR_MODULE_NOT_FOUND') {
            throw makeCjsModuleNotFoundFromErrModuleNotFound(err, parts.name);
        }
        throw err;
    }
    resolved = resolvePackageFileFromRustResult(resolved, resolution);
    if (!resolved) return undefined;
    resolved.packageDir = packageDir;
    return resolved;
}

function resolvePackageSelfReference(parts, parentDir, conditions, resolution) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_self_reference !== 'function') {
        throw new Error('Internal CJS package self-reference resolver is not initialized');
    }
    let resolved;
    try {
        resolved = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_self_reference(
            parentDir,
            parts.name,
            parts.subpath,
            conditions || cjsPackageConditions(),
        );
    } catch (err) {
        if (err && err.code === 'ERR_MODULE_NOT_FOUND') {
            throw makeCjsModuleNotFoundFromErrModuleNotFound(err, parts.name);
        }
        throw err;
    }
    const resolvedFile = resolvePackageFileFromRustResult(resolved, resolution);
    if (!resolvedFile) return undefined;
    if (typeof resolved.packageDir === 'string' && resolved.packageDir.length > 0) {
        resolvedFile.packageDir = resolved.packageDir;
    }
    return resolvedFile;
}

function readCjsPackageCandidate(filename, packageDir) {
    return { filename, packageDir };
}

function cjsPackageExtensionKeys() {
    return Object.keys(requireExtensions);
}

function resolveCjsPackageFallbacks(parts, pkgDir, id, fromPart) {
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_fallback !== 'function') {
        throw new Error('Internal CJS package fallback resolver is not initialized');
    }
    const resolved = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_cjs_resolve_package_fallback(
        pkgDir,
        parts.subpath,
        cjsPackageExtensionKeys(),
        id,
        fromPart,
    );
    if (resolved === null || resolved === undefined) return null;
    return readCjsPackageCandidate(String(resolved.filename), String(resolved.packageDir || pkgDir));
}

function resolvePackageImports(id, parentFilename, conditions, resolution) {
    let resolved;
    try {
        resolved = resolvePackageWithRustBridge(
            nodeUrl.pathToFileURL(parentFilename).href,
            id,
            conditions || cjsPackageConditions(),
            'cjs-analysis',
            'Internal package resolver is not initialized',
        );
    } catch (err) {
        if (err && err.code === 'ERR_MODULE_NOT_FOUND') {
            throw makeCjsModuleNotFoundFromErrModuleNotFound(err, id);
        }
        throw err;
    }
    const resolvedFile = resolvePackageFileFromRustResult(resolved, resolution);
    if (!resolvedFile) {
        throw new Error('Internal package resolver did not resolve package import ' + JSON.stringify(id));
    }
    return resolvedFile;
}

function resolveCjsPackageImportOrNodeModules(id, parentDir, parentFilename, parentLookupPaths, resolution) {
    if (typeof parentFilename !== 'string') {
        throw makeModuleNotFoundError(id);
    }
    resolution = resolution || makeCjsResolutionState();
    try {
        return resolvePackageImports(id, parentFilename, cjsPackageConditions(), resolution);
    } catch (err) {
        if (!err || err.code !== 'ERR_PACKAGE_IMPORT_NOT_DEFINED') {
            throw err;
        }
        const nmResolved = resolveFromNodeModules(id, parentDir, parentFilename, undefined, parentLookupPaths, resolution);
        if (nmResolved) return nmResolved;
        if (err.__wasmNoImportsField === true) {
            throw makeModuleNotFoundError(id);
        }
        throw err;
    }
}

function resolveFilename(id, parentDir) {
    const hasTrailingSlash = /\/$/.test(id);
    const forceDirectory = hasTrailingSlash || /(?:^|\/)\.\.?$/.test(id);
    const candidate = pathModule.isAbsolute(id)
        ? pathModule.normalize(id)
        : pathModule.resolve(parentDir, id);

    let resolved = null;
    if (!forceDirectory) {
        resolved = loadAsFile(candidate, false);
        if (resolved !== null) return resolved;
    }

    if (forceDirectory || isPathDirectory(candidate)) {
        resolved = loadAsDirectory(candidate, id, parentDir);
        if (resolved !== null) return resolved;
    }

    const err = new Error("Cannot find module '" + id + "' from '" + parentDir + "'");
    err.code = 'MODULE_NOT_FOUND';
    throw err;
}

function addRequireStackToModuleNotFound(err, request, parentFilename) {
    if (!err || err.code !== 'MODULE_NOT_FOUND' || typeof parentFilename !== 'string') return err;
    if (typeof err.path === 'string' && typeof err.requestPath === 'string') return err;
    err.requireStack = [parentFilename];
    err.message = "Cannot find module '" + request + "'\nRequire stack:\n- " + parentFilename;
    return err;
}

function hasAllowNativesSyntaxFlag() {
    const runtimeFlags = globalThis.__wasm_rquickjs_v8_runtime_flags;
    if (runtimeFlags && runtimeFlags.allowNativesSyntax === true) {
        return true;
    }

    const processObject = globalThis.process;
    if (!processObject || !Array.isArray(processObject.execArgv)) {
        return false;
    }

    let enabled = false;
    for (let i = 0; i < processObject.execArgv.length; i++) {
        const arg = String(processObject.execArgv[i]).replace(/_/g, '-');
        if (arg === '--allow-natives-syntax') {
            enabled = true;
            continue;
        }

        if (arg === '--noallow-natives-syntax' || arg === '--no-allow-natives-syntax') {
            enabled = false;
        }
    }

    return enabled;
}

function stripV8OptimizationIntrinsics(source) {
    if (!hasAllowNativesSyntaxFlag()) {
        return source;
    }

    // QuickJS cannot parse V8-native `%...` syntax used in eval strings.
    // These intrinsics only force optimization and are semantically no-ops.
    return source
        .replace(/eval\(\s*(['"])%PrepareFunctionForOptimization\([^'"\\\r\n]*\)\1\s*\)\s*;?/g, 'undefined;')
        .replace(/eval\(\s*(['"])%OptimizeFunctionOnNextCall\([^'"\\\r\n]*\)\1\s*\)\s*;?/g, 'undefined;');
}

function prepareCjsEvalSource(value, filename, reactionName, traceName, prepareEvalName, nativeEvalName, evalFunction) {
    if (typeof value !== 'string') return value;
    if (evalFunction !== undefined && evalFunction !== wasmRquickjsModuleEval) return value;
    const decodedIdentifierSource = value.replace(/\\u(?:\{([0-9a-fA-F]+)\}|([0-9a-fA-F]{4}))/g, (_, braced, fixed) => {
        const codePoint = numberParseInt(braced === undefined ? fixed : braced, 16);
        return codePoint <= 0x10FFFF ? stringFromCodePoint(codePoint) : '';
    });
    let bridgeName = '__wasm_rquickjs_cjs_eval_bridge';
    let sequence = 0;
    function isInstalledBridge(name) {
        const descriptor = objectGetOwnPropertyDescriptor(wasmRquickjsModuleGlobalThis, name);
        return descriptor !== undefined && descriptor.value === cjsEvalBridge &&
            descriptor.writable === false && descriptor.enumerable === false && descriptor.configurable === false;
    }
    while (value.indexOf(bridgeName) !== -1 || decodedIdentifierSource.indexOf(bridgeName) !== -1 ||
        (objectPrototypeHasOwnProperty(wasmRquickjsModuleGlobalThis, bridgeName) && !isInstalledBridge(bridgeName))) {
        sequence++;
        bridgeName = '__wasm_rquickjs_cjs_eval_bridge_' + sequence;
    }
    if (!isInstalledBridge(bridgeName)) {
        objectDefineProperty(wasmRquickjsModuleGlobalThis, bridgeName, {
            value: cjsEvalBridge,
            writable: false,
            enumerable: false,
            configurable: false,
        });
    }
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_prepare_cjs_source(
        value,
        filename,
        bridgeName + '.reaction',
        bridgeName + '.trace',
        bridgeName + '.prepareEval',
        bridgeName + '.nativeEval',
    ).source;
}

const cjsEvalBridge = {};
Object.defineProperties(cjsEvalBridge, {
    reaction: { value: dynamicImportReaction },
    trace: { value: dynamicImportWithTrace },
    prepareEval: { value: prepareCjsEvalSource },
    nativeEval: { value: wasmRquickjsModuleEval },
});
function rustHasExecArgvFlag(flag) {
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_module_has_exec_argv_flag(flag);
}

function isExperimentalTransformTypesEnabled() {
    return rustHasExecArgvFlag('--experimental-transform-types');
}

function isSourceMapsEnabled() {
    if (rustHasExecArgvFlag('--no-enable-source-maps')) {
        return false;
    }

    return rustHasExecArgvFlag('--enable-source-maps') || isExperimentalTransformTypesEnabled();
}

function getSimpleSourceMapRegistry() {
    let registry = globalThis.__wasm_rquickjs_simple_source_maps;
    if (!registry || typeof registry !== 'object') {
        registry = Object.create(null);
        globalThis.__wasm_rquickjs_simple_source_maps = registry;
    }
    return registry;
}

function getCjsSourceMapOwnerRegistry() {
    let registry = globalThis.__wasm_rquickjs_cjs_source_map_owners;
    if (!registry || typeof registry !== 'object') {
        registry = Object.create(null);
        globalThis.__wasm_rquickjs_cjs_source_map_owners = registry;
    }
    return registry;
}

function getCjsLineOffsetRegistry() {
    let registry = globalThis.__wasm_rquickjs_cjs_line_offsets;
    if (!registry || typeof registry !== 'object') {
        registry = Object.create(null);
        globalThis.__wasm_rquickjs_cjs_line_offsets = registry;
    }
    return registry;
}

const cjsLineOffset = 6;

function derefWeakRef(ref) {
    if (ref === undefined || ref === null) return undefined;
    try {
        if (typeof ref.deref === 'function') return ref.deref();
    } catch (_) {
        return ref;
    }
    try {
        if (typeof WeakRef === 'function' && WeakRef.prototype && typeof WeakRef.prototype.deref === 'function') {
            return WeakRef.prototype.deref.call(ref);
        }
    } catch (_) {
        return ref;
    }
    return ref;
}

function makeWeakRef(value) {
    if (typeof WeakRef !== 'function') return undefined;
    try {
        return new WeakRef(value);
    } catch (err) {
        return undefined;
    }
}

const sourceMapVlqChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const sourceMapVlqMap = Object.create(null);
for (let i = 0; i < sourceMapVlqChars.length; i++) {
    sourceMapVlqMap[sourceMapVlqChars.charAt(i)] = i;
}

function sourceMapInvalidPayloadError(payload) {
    let received;
    if (payload === null) {
        received = ' Received null';
    } else if (typeof payload === 'number') {
        received = ' Received type number (' + payload + ')';
    } else if (typeof payload === 'string') {
        received = " Received type string ('" + payload + "')";
    } else {
        received = ' Received type ' + typeof payload + ' (' + String(payload) + ')';
    }
    const err = new TypeError('The "payload" argument must be of type object.' + received);
    err.code = 'ERR_INVALID_ARG_TYPE';
    return err;
}

function cloneSourceMapPayload(payload) {
    return JSON.parse(JSON.stringify(payload));
}

function decodeSourceMapVlq(text, state) {
    let result = 0;
    let shift = 0;
    let continuation = true;
    while (continuation) {
        if (state.index >= text.length) throw new Error('Unexpected end of source map VLQ');
        const value = sourceMapVlqMap[text.charAt(state.index++)];
        if (value === undefined) throw new Error('Invalid source map VLQ character');
        continuation = (value & 32) !== 0;
        result += (value & 31) * Math.pow(2, shift);
        shift += 5;
    }
    const negative = (result % 2) === 1;
    result = Math.floor(result / 2);
    if (negative && result === 0) return -2147483648;
    return negative ? -result : result;
}

function resolveSourceMapSource(source, sourceRoot, sourceBasePath) {
    source = String(source);
    if (/^[A-Za-z][A-Za-z0-9+.-]*:/.test(source)) return source;
    if (sourceBasePath) {
        const resolved = pathModule.resolve(sourceBasePath, sourceRoot || '', source);
        return nodeUrl.pathToFileURL(resolved).href;
    }
    if (!sourceRoot) return source;
    sourceRoot = String(sourceRoot);
    if (sourceRoot.endsWith('/') || source.startsWith('/')) return sourceRoot + source;
    return sourceRoot + '/' + source;
}

function parseSourceMapMappings(payload, sourceBasePath) {
    const mappings = String(payload.mappings);
    const sources = Array.isArray(payload.sources) ? payload.sources : [];
    const names = Array.isArray(payload.names) ? payload.names : [];
    const sourceRoot = payload.sourceRoot || '';
    const lines = [];
    let generatedLine = 0;
    let previousGeneratedColumn = 0;
    let previousSource = 0;
    let previousOriginalLine = 0;
    let previousOriginalColumn = 0;
    let previousName = 0;
    let i = 0;

    while (i <= mappings.length) {
        if (!lines[generatedLine]) lines[generatedLine] = [];
        if (i === mappings.length) break;
        const ch = mappings.charAt(i);
        if (ch === ';') {
            generatedLine++;
            previousGeneratedColumn = 0;
            i++;
            continue;
        }
        if (ch === ',') {
            i++;
            continue;
        }

        const segmentStart = i;
        while (i < mappings.length && mappings.charAt(i) !== ',' && mappings.charAt(i) !== ';') {
            i++;
        }
        const segmentText = mappings.slice(segmentStart, i);
        if (segmentText.length === 0) continue;
        const state = { index: 0 };
        const generatedColumn = previousGeneratedColumn + decodeSourceMapVlq(segmentText, state);
        previousGeneratedColumn = generatedColumn;
        if (state.index >= segmentText.length) {
            lines[generatedLine].push({ generatedLine, generatedColumn });
            continue;
        }

        const sourceIndex = previousSource + decodeSourceMapVlq(segmentText, state);
        const originalLine = previousOriginalLine + decodeSourceMapVlq(segmentText, state);
        const originalColumn = previousOriginalColumn + decodeSourceMapVlq(segmentText, state);
        previousSource = sourceIndex;
        previousOriginalLine = originalLine;
        previousOriginalColumn = originalColumn;
        let name;
        if (state.index < segmentText.length) {
            const nameIndex = previousName + decodeSourceMapVlq(segmentText, state);
            previousName = nameIndex;
            name = names[nameIndex];
        }

        lines[generatedLine].push({
            generatedLine,
            generatedColumn,
            originalSource: resolveSourceMapSource(sources[sourceIndex], sourceRoot, sourceBasePath),
            originalLine,
            originalColumn,
            name,
        });
    }

    for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
        if (lines[lineIndex]) {
            lines[lineIndex].sort((a, b) => a.generatedColumn - b.generatedColumn);
        }
    }
    return lines;
}

function parseIndexSourceMapMappings(payload, sourceBasePath) {
    const lines = [];
    const sections = Array.isArray(payload.sections) ? payload.sections : [];
    for (let i = 0; i < sections.length; i++) {
        const section = sections[i];
        if (!section || !section.map || !section.offset) continue;
        const offsetLine = Number(section.offset.line) || 0;
        const offsetColumn = Number(section.offset.column) || 0;
        const sectionMap = parseSourceMapMappings(section.map, sourceBasePath);
        for (let line = 0; line < sectionMap.length; line++) {
            const segments = sectionMap[line];
            if (!segments) continue;
            const targetLine = line + offsetLine;
            if (!lines[targetLine]) lines[targetLine] = [];
            for (let j = 0; j < segments.length; j++) {
                const segment = Object.assign({}, segments[j]);
                segment.generatedLine += offsetLine;
                if (line === 0) segment.generatedColumn += offsetColumn;
                lines[targetLine].push(segment);
            }
        }
    }
    for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
        if (lines[lineIndex]) {
            lines[lineIndex].sort((a, b) => a.generatedColumn - b.generatedColumn);
        }
    }
    return lines;
}

function decodeSourceMapPayload(payload, sourceBasePath) {
    try {
        if (Array.isArray(payload.sections)) return parseIndexSourceMapMappings(payload, sourceBasePath);
        return parseSourceMapMappings(payload, sourceBasePath);
    } catch (_) {
        return [];
    }
}

function cloneSourceMapEntry(entry) {
    if (!entry || entry.originalSource === undefined) return {};
    return {
        generatedLine: entry.generatedLine,
        generatedColumn: entry.generatedColumn,
        originalSource: entry.originalSource,
        originalLine: entry.originalLine,
        originalColumn: entry.originalColumn,
        name: entry.name,
    };
}

function findSourceMapMapping(lines, lineOffset, columnOffset) {
    lineOffset = Math.floor(lineOffset);
    columnOffset = Math.floor(columnOffset);
    for (let lineIndex = lineOffset; lineIndex >= 0; lineIndex--) {
        const line = lines[lineIndex];
        if (!line || line.length === 0) continue;
        let match = null;
        if (lineIndex === lineOffset) {
            for (let i = 0; i < line.length; i++) {
                if (line[i].generatedColumn <= columnOffset) match = line[i];
                else break;
            }
            if (match) return match;
        } else {
            return line[line.length - 1];
        }
    }
    return null;
}

class SourceMap {
    constructor(payload, options) {
        if (payload === null || typeof payload !== 'object') {
            throw sourceMapInvalidPayloadError(payload);
        }
        options = options || {};
        this.payload = cloneSourceMapPayload(payload);
        if (options.lineLengths !== undefined) {
            this.lineLengths = Array.prototype.slice.call(options.lineLengths);
        }
        this._decodedMappings = decodeSourceMapPayload(this.payload, options.sourceBasePath);
    }

    findEntry(lineOffset, columnOffset) {
        lineOffset = Number(lineOffset);
        columnOffset = Number(columnOffset);
        if (!Number.isFinite(lineOffset) || !Number.isFinite(columnOffset)) return {};
        return cloneSourceMapEntry(findSourceMapMapping(this._decodedMappings, lineOffset, columnOffset));
    }

    findOrigin(lineNumber, columnNumber) {
        const generatedLine = Number(lineNumber) - 1;
        const generatedColumn = Number(columnNumber) - 1;
        if (!Number.isFinite(generatedLine) || !Number.isFinite(generatedColumn)) return {};
        const match = findSourceMapMapping(this._decodedMappings, generatedLine, generatedColumn);
        if (!match) return {};
        return {
            name: match.name,
            fileName: match.originalSource,
            lineNumber: match.originalLine + 1,
            columnNumber: match.originalColumn + (generatedColumn - match.generatedColumn) + 1,
        };
    }
}

function findSourceMap(path) {
    path = String(path);
    const owners = getCjsSourceMapOwnerRegistry();
    const ownerRef = owners[path];
    if (ownerRef !== undefined && derefWeakRef(ownerRef) === undefined) {
        delete owners[path];
        delete getSimpleSourceMapRegistry()[path];
        return undefined;
    }
    const registry = getSimpleSourceMapRegistry();
    return registry[path];
}

function sourceMapLineLengths(source) {
    return String(source).split(/\r\n|[\n\r\u2028\u2029]/).map(line => line.length);
}

function decodeInlineSourceMap(url) {
    const marker = 'base64,';
    const idx = url.indexOf(marker);
    if (idx === -1) return null;
    try {
        const encoded = url.slice(idx + marker.length);
        const decoded = buffer.Buffer.from(encoded, 'base64').toString('utf8');
        return JSON.parse(decoded);
    } catch (_) {
        return null;
    }
}

function registerSourceMapForCjs(filename, source, moduleObject) {
    const registry = getSimpleSourceMapRegistry();
    const owners = getCjsSourceMapOwnerRegistry();
    if (!isSourceMapsEnabled()) {
        delete registry[filename];
        delete owners[filename];
        return;
    }

    const sourceText = String(source);
    const directiveRe = /\/\/[#@]\s*sourceMappingURL=([^\r\n]+)|\/\*[#@]\s*sourceMappingURL=([\s\S]*?)\*\//g;
    let match;
    let url = null;
    while ((match = directiveRe.exec(sourceText)) !== null) {
        url = (match[1] !== undefined ? match[1] : match[2]).trim();
    }
    if (url === null) {
        delete registry[filename];
        delete owners[filename];
        return;
    }

    let payload = null;
    let sourceBasePath = pathModule.dirname(filename);
    if (url.startsWith('data:')) {
        payload = decodeInlineSourceMap(url);
    } else {
        const mapPath = pathModule.resolve(pathModule.dirname(filename), url);
        sourceBasePath = pathModule.dirname(mapPath);
        const content = tryReadFile(mapPath);
        if (content !== null) {
            try {
                payload = JSON.parse(content);
            } catch (_) {
                payload = null;
            }
        }
    }
    if (payload === null) {
        delete registry[filename];
        delete owners[filename];
        return;
    }
    registry[filename] = new SourceMap(payload, {
        lineLengths: sourceMapLineLengths(source),
        sourceBasePath,
    });
    if (moduleObject) {
        const ownerRef = makeWeakRef(moduleObject);
        if (ownerRef !== undefined) {
            owners[filename] = ownerRef;
        } else {
            delete owners[filename];
        }
    } else {
        delete owners[filename];
    }
}

function isTypeScriptFilename(filename) {
    return filename.endsWith('.ts') || filename.endsWith('.cts') || filename.endsWith('.mts');
}

let recordTypeScriptModuleTransform = () => {};
let recordCommonJsExportAnalysis = () => {};
if (testObservabilityEnabledNative()) {
    let typeScriptModuleTransformCount = 0;
    let commonJsExportAnalysisCount = 0;
    recordTypeScriptModuleTransform = () => { typeScriptModuleTransformCount += 1; };
    recordCommonJsExportAnalysis = () => { commonJsExportAnalysisCount += 1; };
    Object.defineProperties(globalThis, {
        __wasm_rquickjs_record_typescript_module_transform: {
            value: recordTypeScriptModuleTransform,
            writable: false,
            configurable: false,
        },
        __wasm_rquickjs_get_typescript_module_transform_count: {
            value: () => typeScriptModuleTransformCount,
            writable: false,
            configurable: false,
        },
        __wasm_rquickjs_reset_typescript_module_transform_count: {
            value: () => { typeScriptModuleTransformCount = 0; },
            writable: false,
            configurable: false,
        },
        __wasm_rquickjs_record_commonjs_export_analysis: {
            value: recordCommonJsExportAnalysis,
            writable: false,
            configurable: false,
        },
        __wasm_rquickjs_get_commonjs_export_analysis_count: {
            value: () => commonJsExportAnalysisCount,
            writable: false,
            configurable: false,
        },
        __wasm_rquickjs_reset_commonjs_export_analysis_count: {
            value: () => { commonJsExportAnalysisCount = 0; },
            writable: false,
            configurable: false,
        },
    });
}

function transpileTypeScriptModule(filename, source, module = undefined) {
    if (!isTypeScriptFilename(filename)) {
        return source;
    }
    // Rust owns the transform semantics. This adapter only applies CommonJS
    // loader policy; the Rust filesystem loader applies the same service for ESM.
    const output = JSON.parse(transformTypeScriptModuleNative(
        String(source), filename, module
    ));
    recordTypeScriptModuleTransform();
    return output.code;
}

function prepareCommonJsTypeScript(filename, source) {
    return isTypeScriptFilename(filename)
        ? transpileTypeScriptModule(filename, source, false)
        : source;
}

function clearPreparedTypeScriptGraph(graph) {
    // This graph belongs only to the active load transaction. require.cache is
    // the durable per-runtime owner once a module finishes loading.
    if (!graph || typeof graph !== 'object') return;
    for (const filename of Object.keys(graph)) delete graph[filename];
}

export function stripTypeScriptTypes(code, options = undefined) {
    if (typeof code !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
    }
    if (options !== undefined &&
        (options === null || typeof options !== 'object' || Array.isArray(options))) {
        throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
    }
    const normalized = options === undefined ? {} : options;
    const mode = normalized.mode === undefined ? 'strip' : normalized.mode;
    if (mode !== 'strip' && mode !== 'transform') {
        throw new ERR_INVALID_ARG_VALUE('options.mode', mode, "must be 'strip' or 'transform'");
    }
    const sourceMap = normalized.sourceMap === undefined ? false : normalized.sourceMap;
    if (typeof sourceMap !== 'boolean') {
        throw new ERR_INVALID_ARG_TYPE('options.sourceMap', 'boolean', sourceMap);
    }
    const sourceUrl = normalized.sourceUrl;
    if (sourceUrl !== undefined && typeof sourceUrl !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('options.sourceUrl', 'string', sourceUrl);
    }
    if (mode === 'strip' && sourceMap) {
        throw new ERR_INVALID_ARG_VALUE(
            'options.sourceMap', sourceMap, "cannot be used when mode is 'strip'"
        );
    }

    internalUtil.emitExperimentalWarning('stripTypeScriptTypes');
    const transformed = JSON.parse(transformTypeScriptNative(
        code, sourceUrl === undefined ? '' : sourceUrl, mode, sourceMap, undefined
    ));
    let result = transformed.code;
    if (sourceMap) {
        const encoded = buffer.Buffer.from(transformed.sourceMap, 'utf8').toString('base64');
        result += `\n//# sourceMappingURL=data:application/json;base64,${encoded}`;
    }
    if (sourceUrl !== undefined) {
        result += `\n\n//# sourceURL=${sourceUrl}`;
    }
    return result;
}

function getArrowMessagePrivateSymbol() {
    const privateSymbols = globalThis.__wasm_rquickjs_internal_private_symbols;
    if (!privateSymbols || typeof privateSymbols !== 'object') {
        return undefined;
    }

    const arrowMessageSymbol = privateSymbols.arrow_message_private_symbol;
    return typeof arrowMessageSymbol === 'symbol' ? arrowMessageSymbol : undefined;
}

function escapeRegExp(text) {
    return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

function maybeSetArrowMessageOnSyntaxError(err, filename, source) {
    if (!err || err.name !== 'SyntaxError') {
        return;
    }

    const arrowMessageSymbol = getArrowMessagePrivateSymbol();
    if (arrowMessageSymbol === undefined || err[arrowMessageSymbol] !== undefined) {
        return;
    }

    let line = 1;
    let column = 1;

    if (typeof err.lineNumber === 'number' && Number.isFinite(err.lineNumber) && err.lineNumber > 0) {
        line = Math.floor(err.lineNumber);
    }
    if (typeof err.columnNumber === 'number' && Number.isFinite(err.columnNumber) && err.columnNumber > 0) {
        column = Math.floor(err.columnNumber);
    }

    if (typeof err.stack === 'string') {
        const stackMatch = err.stack.match(new RegExp(escapeRegExp(filename) + ':(\\d+)(?::(\\d+))?'));
        if (stackMatch) {
            line = parseInt(stackMatch[1], 10);
            if (stackMatch[2] !== undefined) {
                column = parseInt(stackMatch[2], 10);
            }
        }
    }

    const sourceLines = String(source).split('\n');
    let sourceLine = '';
    if (line >= 1 && line <= sourceLines.length) {
        sourceLine = sourceLines[line - 1].replace(/\r$/, '');
    }

    if (!Number.isFinite(column) || column < 1) {
        column = 1;
    }

    let arrowMessage = filename + ':' + line;
    if (sourceLine.length > 0) {
        arrowMessage += '\n' + sourceLine + '\n' + ' '.repeat(column - 1) + '^';
    }

    err[arrowMessageSymbol] = arrowMessage;
}

function wrapEsmNamespace(ns) {
    if (!ns || typeof ns !== 'object') return ns;
    if (!Object.hasOwn(ns, 'default') || Object.hasOwn(ns, '__esModule')) return ns;
    const wrapped = Object.create(null);
    const namespaceKeys = Object.keys(ns);
    Object.defineProperty(wrapped, '__esModule', {
        value: true,
        writable: true,
        configurable: false,
        enumerable: true,
    });
    for (let i = 0; i < namespaceKeys.length; i++) {
        const k = namespaceKeys[i];
        Object.defineProperty(wrapped, k, {
            value: ns[k],
            writable: true,
            enumerable: true,
            configurable: false,
        });
    }
    Object.defineProperty(wrapped, Symbol.toStringTag, {
        value: 'Module',
        writable: false,
        configurable: false,
        enumerable: false,
    });
    Object.preventExtensions(wrapped);
    function namespaceDescriptor(prop) {
        if (prop === '__esModule') {
            return {
                value: true,
                writable: true,
                enumerable: true,
                configurable: false,
            };
        }
        if (typeof prop === 'string' && Object.hasOwn(ns, prop)) {
            return {
                value: ns[prop],
                writable: true,
                enumerable: true,
                configurable: false,
            };
        }
        return Object.getOwnPropertyDescriptor(wrapped, prop);
    }
    return new Proxy(wrapped, {
        get: function(target, prop, receiver) {
            if (prop === '__esModule') return true;
            if (typeof prop === 'string' && Object.hasOwn(ns, prop)) return ns[prop];
            return Reflect.get(target, prop, receiver);
        },
        getOwnPropertyDescriptor: function(_target, prop) {
            return namespaceDescriptor(prop);
        },
        set: function() {
            return false;
        },
        defineProperty: function() {
            return false;
        },
        deleteProperty: function() {
            return false;
        },
    });
}

// Normalize QuickJS SyntaxError messages for ESM keywords to match Node.js/V8 format.
// QuickJS: "unsupported keyword: export" → Node.js: "Unexpected token 'export'"
function normalizeEsmSyntaxError(err) {
    if (!err || typeof err.message !== 'string') return;
    const m = err.message.match(/^unsupported keyword: (\w+)$/);
    if (m) {
        err.message = "Unexpected token '" + m[1] + "'";
    }
}

function markAsSyntaxError(err) {
    if (!err || err.name === 'SyntaxError') return;
    err.name = 'SyntaxError';
    if (typeof err.stack === 'string') {
        err.stack = err.stack.replace(/^Error:/, 'SyntaxError:');
    }
}

function rustModuleSourceAnalysis(source) {
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_analyze_module_source(source);
}

function isEsmGraphFile(filename, analysis) {
    const packageScope = (filename.endsWith('.js') || filename.endsWith('.ts'))
        ? getPackageScopeInfo(filename)
        : null;
    const explicitPackageType = packageScope ? packageScope.packageType : null;
    const isCommonJsPackage = explicitPackageType === 'commonjs' ||
        (explicitPackageType === null && packageScope !== null && packageScope.isNodeModulesPackage);
    if (filename.endsWith('.mjs') || filename.endsWith('.mts') ||
        ((filename.endsWith('.js') || filename.endsWith('.ts')) && explicitPackageType === 'module')) return true;
    if (filename.endsWith('.cjs') || filename.endsWith('.cts') || isCommonJsPackage) return false;
    return analysis.looksLikeEsm || analysis.hasCjsWrapperLexicalRedeclaration;
}

function readEsmGraphFileInfo(filename, cache) {
    if (Object.prototype.hasOwnProperty.call(cache, filename)) {
        return cache[filename];
    }
    const source = tryReadFile(filename);
    if (source === null) {
        return { source: null, isEsm: false };
    }
    const preparedSource = transpileTypeScriptModule(filename, source, true);
    const analysis = rustModuleSourceAnalysis(preparedSource);
    const info = {
        source: preparedSource,
        analysis,
        isEsm: isEsmGraphFile(filename, analysis),
    };
    cache[filename] = info;
    return info;
}

function esmGraphStaticSpecifiers(fileInfo) {
    return fileInfo.analysis.staticEdges.map((edge) => edge.specifier);
}

function esmGraphRequireSpecifiers(fileInfo) {
    return fileInfo.analysis.requireSpecifiers;
}

function esmGraphCreateRequireSpecifiers(fileInfo) {
    return fileInfo.analysis.createRequireSpecifiers;
}

function fileUrlForPath(filename) {
    return 'file://' + filename;
}

const cjsEsmDefaultSnapshotSymbol = Symbol('wasm-rquickjs.cjs-esm-default-snapshot');
const cjsTypeScriptAnalysisCache = new WeakMap();
const cjsTypeScriptAnalysisCacheQueue = [];
const cjsTypeScriptAnalysisCacheMaxEntries = 32;
const cjsTypeScriptAnalysisCacheMaxBytes = 1024 * 1024;
let cjsTypeScriptAnalysisCacheEntries = 0;
let cjsTypeScriptAnalysisCacheBytes = 0;
let cjsTypeScriptPreparedSourceEntries = 0;
let cjsTypeScriptPreparedSourceBytes = 0;
const cjsEsmDefaultSnapshotToken = {};

function installCjsEsmDefaultSnapshotSlot(mod) {
    if (!mod || (typeof mod !== 'object' && typeof mod !== 'function') || cjsFacadeHasOwnProperty(mod, cjsEsmDefaultSnapshotSymbol)) return;
    const state = { captured: false, value: undefined };
    Object.defineProperty(mod, cjsEsmDefaultSnapshotSymbol, {
        value: function cjsEsmDefaultSnapshotSlot(token, op, value) {
            if (token !== cjsEsmDefaultSnapshotToken) return undefined;
            if (op === 'set') {
                if (!state.captured) {
                    state.captured = true;
                    state.value = value;
                }
                return state.value;
            }
            if (op === 'has') return state.captured;
            if (op === 'get') return state.value;
            return undefined;
        },
        writable: false,
        configurable: false,
        enumerable: false,
    });
}

function cjsEsmDefaultSnapshotSlot(mod) {
    if (!mod || (typeof mod !== 'object' && typeof mod !== 'function')) return undefined;
    const slot = mod[cjsEsmDefaultSnapshotSymbol];
    return typeof slot === 'function' ? slot : undefined;
}

function captureCjsEsmDefaultSnapshot(mod) {
    installCjsEsmDefaultSnapshotSlot(mod);
    const slot = cjsEsmDefaultSnapshotSlot(mod);
    if (!slot || slot(cjsEsmDefaultSnapshotToken, 'has')) return;
    slot(cjsEsmDefaultSnapshotToken, 'set', mod.exports);
}

function hasCjsEsmDefaultSnapshot(cache, filename) {
    if (!cache || typeof cache !== 'object') return false;
    const mod = cache[filename];
    const slot = cjsEsmDefaultSnapshotSlot(mod);
    return !!(slot && slot(cjsEsmDefaultSnapshotToken, 'has'));
}

function getCjsEsmDefaultSnapshot(cache, filename) {
    const mod = cache && cache[filename];
    const slot = cjsEsmDefaultSnapshotSlot(mod);
    return slot ? slot(cjsEsmDefaultSnapshotToken, 'get') : undefined;
}

function discardCjsTypeScriptAnalysisCacheEntry(mod) {
    const entry = mod && cjsTypeScriptAnalysisCache.get(mod);
    if (!entry) return;
    cjsTypeScriptAnalysisCache.delete(mod);
    entry.active = false;
    cjsTypeScriptAnalysisCacheEntries -= 1;
    cjsTypeScriptAnalysisCacheBytes -= entry.bytes;
    if (entry.preparedSource !== undefined) {
        cjsTypeScriptPreparedSourceEntries -= 1;
        cjsTypeScriptPreparedSourceBytes -= entry.preparedBytes;
    }
    entry.mod = undefined;
    entry.originalSource = undefined;
    entry.preparedSource = undefined;
    entry.exportNames = undefined;
    if (cjsTypeScriptAnalysisCacheQueue.length > cjsTypeScriptAnalysisCacheMaxEntries * 2) {
        const active = cjsTypeScriptAnalysisCacheQueue.filter((queued) => queued.active);
        cjsTypeScriptAnalysisCacheQueue.splice(
            0,
            cjsTypeScriptAnalysisCacheQueue.length,
            ...active,
        );
    }
}

function pruneCjsTypeScriptAnalysisCache() {
    while (cjsTypeScriptAnalysisCacheEntries > cjsTypeScriptAnalysisCacheMaxEntries ||
           cjsTypeScriptAnalysisCacheBytes > cjsTypeScriptAnalysisCacheMaxBytes) {
        const entry = cjsTypeScriptAnalysisCacheQueue.shift();
        if (!entry || !entry.active) continue;
        discardCjsTypeScriptAnalysisCacheEntry(entry.mod);
    }
}

function captureCjsTypeScriptAnalysisCacheEntry(mod, originalSource, preparedSource, exportNames) {
    if (!mod || (typeof mod !== 'object' && typeof mod !== 'function')) return;
    discardCjsTypeScriptAnalysisCacheEntry(mod);
    const names = Array.isArray(exportNames) ? exportNames.slice() : undefined;
    const preparedBytes = preparedSource === undefined ? 0 : preparedSource.length * 2;
    const namesBytes = names === undefined
        ? 0
        : names.reduce((total, name) => total + String(name).length * 2, 0);
    const bytes = originalSource.length * 2 + preparedBytes + namesBytes;
    if (bytes > cjsTypeScriptAnalysisCacheMaxBytes) return;
    const entry = {
        mod,
        originalSource,
        preparedSource,
        exportNames: names,
        bytes,
        preparedBytes,
        active: true,
    };
    cjsTypeScriptAnalysisCache.set(mod, entry);
    cjsTypeScriptAnalysisCacheQueue.push(entry);
    cjsTypeScriptAnalysisCacheEntries += 1;
    cjsTypeScriptAnalysisCacheBytes += bytes;
    if (preparedSource !== undefined) {
        cjsTypeScriptPreparedSourceEntries += 1;
        cjsTypeScriptPreparedSourceBytes += preparedBytes;
    }
    pruneCjsTypeScriptAnalysisCache();
}

function captureCjsTypeScriptExportNames(mod, names, originalSource) {
    captureCjsTypeScriptAnalysisCacheEntry(mod, originalSource, undefined, names);
}

function captureCjsTypeScriptPreparedSource(mod, originalSource, preparedSource) {
    captureCjsTypeScriptAnalysisCacheEntry(mod, originalSource, preparedSource, undefined);
}

Object.defineProperty(globalThis, '__wasm_rquickjs_has_cjs_esm_default_snapshot', {
    value: hasCjsEsmDefaultSnapshot,
    writable: false,
    configurable: false,
});

Object.defineProperty(globalThis, '__wasm_rquickjs_get_cjs_esm_default_snapshot', {
    value: getCjsEsmDefaultSnapshot,
    writable: false,
    configurable: false,
});

function getCachedCjsTypeScriptAnalysisEntry(filename, source) {
    const require = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_create_require(filename);
    const resolvedFilename = require.resolve(filename);
    const mod = require.cache[resolvedFilename];
    const entry = mod && cjsTypeScriptAnalysisCache.get(mod);
    if (!entry || entry.originalSource !== source) {
        discardCjsTypeScriptAnalysisCacheEntry(mod);
        return undefined;
    }
    return entry;
}

function getCachedCjsTypeScriptAnalysis(filename, source) {
    const entry = getCachedCjsTypeScriptAnalysisEntry(filename, source);
    return entry === undefined ? undefined : {
        exportNames: entry.exportNames,
        preparedSource: entry.preparedSource,
    };
}

function setCachedCjsTypeScriptExportNames(filename, names, source) {
    const require = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_create_require(filename);
    const resolvedFilename = require.resolve(filename);
    const mod = require.cache[resolvedFilename];
    if (mod) {
        captureCjsTypeScriptExportNames(mod, names, source);
    }
}

Object.defineProperty(globalThis, '__wasm_rquickjs_get_cached_cjs_typescript_analysis', {
    value: getCachedCjsTypeScriptAnalysis,
    writable: false,
    configurable: false,
});
Object.defineProperty(globalThis, '__wasm_rquickjs_set_cached_cjs_typescript_export_names', {
    value: setCachedCjsTypeScriptExportNames,
    writable: false,
    configurable: false,
});
if (testObservabilityEnabledNative()) {
    Object.defineProperty(globalThis, '__wasm_rquickjs_get_cjs_typescript_prepared_source_cache_stats', {
        value: () => ({
            entries: cjsTypeScriptAnalysisCacheEntries,
            bytes: cjsTypeScriptAnalysisCacheBytes,
            preparedEntries: cjsTypeScriptPreparedSourceEntries,
            preparedBytes: cjsTypeScriptPreparedSourceBytes,
            maxEntries: cjsTypeScriptAnalysisCacheMaxEntries,
            maxBytes: cjsTypeScriptAnalysisCacheMaxBytes,
        }),
        writable: false,
        configurable: false,
    });
}

function takePreparedCjsTypeScript(meta) {
    const prepared = meta.__wasm_rquickjs_prepared_cjs_typescript;
    delete meta.__wasm_rquickjs_prepared_cjs_typescript;
    return prepared;
}

Object.defineProperty(globalThis, '__wasm_rquickjs_take_prepared_cjs_typescript', {
    value: takePreparedCjsTypeScript,
    writable: false,
    configurable: false,
});

function loadCjsEsmFacadeDefault(filename, preparedTypeScriptGraph) {
    const require = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_create_require(filename);
    const resolvedFilename = require.resolve(filename);
    return hasCjsEsmDefaultSnapshot(require.cache, resolvedFilename)
        ? getCjsEsmDefaultSnapshot(require.cache, resolvedFilename)
        : preparedTypeScriptGraph
            ? loadPreparedCjsTypeScript(
                resolvedFilename,
                preparedTypeScriptGraph,
                filename,
            )
            : require(filename);
}

function loadPreparedCjsTypeScript(resolvedFilename, graph, traceId) {
    // An ESM facade has no real CommonJS parent; do not invent a self-parent.
    return traceModuleRequire(traceId, null, () => {
        try {
            return loadFilesystemCommonJs(resolvedFilename, null, graph).exports;
        } finally {
            clearPreparedTypeScriptGraph(graph);
        }
    });
}

Object.defineProperty(globalThis, '__wasm_rquickjs_load_cjs_esm_facade_default', {
    value: loadCjsEsmFacadeDefault,
    writable: false,
    configurable: false,
});

function resolveEsmGraphSpecifier(specifier, parentFilename, conditions, mode) {
    mode = mode || 'import';
    if (specifier.startsWith('node:') || specifier.startsWith('data:')) return null;
    const parentDir = pathModule.dirname(parentFilename);
    if (rustClassifiesPathSpecifier(specifier)) {
        try {
            return resolveFilename(specifier, parentDir);
        } catch (_) {
            return null;
        }
    }
    conditions = conditions || (mode === 'cjs-analysis' ? cjsPackageConditions() : esmPackageConditions());
    if (specifier.startsWith('#')) {
        try {
            const resolved = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_require_esm_graph_resolve_package(
                parentFilename,
                specifier,
                conditions,
                mode,
            );
            if (resolved) return { filename: resolved };
        } catch (_) {
            return null;
        }
        return null;
    }
    try {
        const resolved = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_require_esm_graph_resolve_package(
            parentFilename,
            specifier,
            conditions,
            mode,
        );
        return resolved ? { filename: resolved } : null;
    } catch (_) {
        return null;
    }
}

function addRequireEsmGraphMark(filename, marked) {
    const graph = globalThis.__wasm_rquickjs_require_esm_graph_in_progress || Object.create(null);
    const counts = globalThis.__wasm_rquickjs_require_esm_graph_counts || Object.create(null);
    globalThis.__wasm_rquickjs_require_esm_graph_in_progress = graph;
    globalThis.__wasm_rquickjs_require_esm_graph_counts = counts;

    counts[filename] = (counts[filename] || 0) + 1;
    graph[filename] = true;
    marked.push(filename);

    const fileUrl = fileUrlForPath(filename);
    counts[fileUrl] = (counts[fileUrl] || 0) + 1;
    graph[fileUrl] = true;
    marked.push(fileUrl);
}

function stackContains(stack, filename) {
    for (let i = 0; i < stack.length; i++) {
        if (stack[i] === filename) return true;
    }
    return false;
}

function esmGraphReachesAny(filename, stack, seen, fileInfoCache) {
    if (stackContains(stack, filename)) return true;
    seen = seen || Object.create(null);
    if (seen[filename]) return false;
    seen[filename] = true;

    const fileInfo = readEsmGraphFileInfo(filename, fileInfoCache);
    if (fileInfo.source === null) return false;

    const isEsm = fileInfo.isEsm;
    const specifiers = isEsm
        ? esmGraphStaticSpecifiers(fileInfo)
        : esmGraphRequireSpecifiers(fileInfo);
    const conditions = specifiers.length === 0
        ? null
        : (isEsm ? esmPackageConditions() : cjsPackageConditions());
    for (let i = 0; i < specifiers.length; i++) {
        const resolved = resolveEsmGraphSpecifier(specifiers[i], filename, conditions, isEsm ? 'import' : 'cjs-analysis');
        if (resolved && resolved.filename && esmGraphReachesAny(resolved.filename, stack, seen, fileInfoCache)) return true;
    }

    if (isEsm) {
        const bridgeSpecifiers = esmGraphCreateRequireSpecifiers(fileInfo);
        const cjsConditions = bridgeSpecifiers.length === 0 ? null : cjsPackageConditions();
        for (let i = 0; i < bridgeSpecifiers.length; i++) {
            const resolved = resolveEsmGraphSpecifier(bridgeSpecifiers[i], filename, cjsConditions, 'cjs-analysis');
            if (resolved && resolved.filename && esmGraphReachesAny(resolved.filename, stack, seen, fileInfoCache)) return true;
        }
    }

    return false;
}

function scanRequireEsmGraph(filename, marked, seen, stack, fileInfoCache) {
    if (seen[filename]) return;
    seen[filename] = true;

    const fileInfo = readEsmGraphFileInfo(filename, fileInfoCache);
    if (fileInfo.source === null) return;

    const isEsm = fileInfo.isEsm;
    const cjsConditions = isEsm ? null : cjsPackageConditions();
    if (!isEsm) {
        const requireSpecifiers = esmGraphRequireSpecifiers(fileInfo);
        for (let i = 0; i < requireSpecifiers.length; i++) {
            const resolved = resolveEsmGraphSpecifier(requireSpecifiers[i], filename, cjsConditions, 'cjs-analysis');
            if (resolved && resolved.filename) {
                const targetInfo = readEsmGraphFileInfo(resolved.filename, fileInfoCache);
                if (targetInfo.source !== null && targetInfo.isEsm && esmGraphReachesAny(resolved.filename, stack, undefined, fileInfoCache)) {
                    addRequireEsmGraphMark(resolved.filename, marked);
                } else {
                    scanRequireEsmGraph(resolved.filename, marked, seen, stack, fileInfoCache);
                }
            }
        }
        return;
    }

    stack.push(filename);

    const specifiers = esmGraphStaticSpecifiers(fileInfo);
    const esmConditions = specifiers.length === 0 ? null : esmPackageConditions();
    for (let i = 0; i < specifiers.length; i++) {
        const resolved = resolveEsmGraphSpecifier(specifiers[i], filename, esmConditions, 'import');
        if (resolved && resolved.filename) {
            scanRequireEsmGraph(resolved.filename, marked, seen, stack, fileInfoCache);
        }
    }
    const createRequireSpecifiers = esmGraphCreateRequireSpecifiers(fileInfo);
    const createRequireConditions = createRequireSpecifiers.length === 0 ? null : cjsPackageConditions();
    for (let i = 0; i < createRequireSpecifiers.length; i++) {
        const resolved = resolveEsmGraphSpecifier(createRequireSpecifiers[i], filename, createRequireConditions, 'cjs-analysis');
        if (resolved && resolved.filename) {
            const targetInfo = readEsmGraphFileInfo(resolved.filename, fileInfoCache);
            if (targetInfo.source !== null && targetInfo.isEsm && esmGraphReachesAny(resolved.filename, stack, undefined, fileInfoCache)) {
                addRequireEsmGraphMark(resolved.filename, marked);
            } else {
                scanRequireEsmGraph(resolved.filename, marked, seen, stack, fileInfoCache);
            }
        }
    }
    stack.pop();
}

function markRequireEsmGraph(filename) {
    const marked = [];
    scanRequireEsmGraph(filename, marked, Object.create(null), [], Object.create(null));
    return marked;
}

function unmarkRequireEsmGraph(marked) {
    const graph = globalThis.__wasm_rquickjs_require_esm_graph_in_progress;
    const counts = globalThis.__wasm_rquickjs_require_esm_graph_counts;
    if (!graph || !counts) return;
    for (let i = 0; i < marked.length; i++) {
        const key = marked[i];
        counts[key] = (counts[key] || 1) - 1;
        if (counts[key] <= 0) {
            delete counts[key];
            delete graph[key];
        }
    }
}

function throwIfRequireEsmGraphCycle(resolvedFilename) {
    const graph = globalThis.__wasm_rquickjs_require_esm_graph_in_progress;
    if (graph && (graph[resolvedFilename] || graph[fileUrlForPath(resolvedFilename)])) {
        const err = new Error('Cannot require() ES Module ' + resolvedFilename + ' in a cycle.');
        err.code = 'ERR_REQUIRE_CYCLE_MODULE';
        throw err;
    }
}

const wrapper = [
    '(function (exports, require, module, __filename, __dirname) { ',
    '\n});'
];

function wrap(script) {
    const activeWrapper = (typeof moduleExports !== 'undefined' && moduleExports.wrapper) || wrapper;
    return activeWrapper[0] + script + activeWrapper[1];
}

function wrapForCompile(script, dynamicImportBindings) {
    const activeWrapper = (typeof moduleExports !== 'undefined' && moduleExports.wrapper) || wrapper;
    if (dynamicImportBindings) {
        return '(function(' + dynamicImportBindings.reactionName + ',' + dynamicImportBindings.traceName + ',' + dynamicImportBindings.prepareEvalName + ',' + dynamicImportBindings.nativeEvalName + '){return ' +
            activeWrapper[0] + script + activeWrapper[1] +
            '\n})(__wasm_rquickjs_dynamic_import_reaction,__wasm_rquickjs_dynamic_import_with_trace,__wasm_rquickjs_prepare_cjs_eval_source,__wasm_rquickjs_native_eval);';
    }
    return activeWrapper[0] + script + activeWrapper[1];
}

function compileCjs(filename, source, isPreparedTypeScript = false) {
    if (source.length > 0 && source.charCodeAt(0) === 0xFEFF) {
        source = source.slice(1);
    }
    // Strip shebang
    if (source.length > 1 && source.charCodeAt(0) === 0x23 && source.charCodeAt(1) === 0x21) {
        source = '//' + source;
    }

    if (!isPreparedTypeScript) {
        source = transpileTypeScriptModule(filename, source, false);
    }
    source = stripV8OptimizationIntrinsics(source);
    const strippedImportAttributes = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_prepare_cjs_source(
        source,
        filename,
        null,
        null,
        null,
        null,
    );
    source = strippedImportAttributes.source;

    const cjsLineOffsets = getCjsLineOffsetRegistry();
    cjsLineOffsets[filename] = cjsLineOffset;

    const wrappedSource = wrapForCompile(source + '\n//# sourceURL=' + filename + '\n', strippedImportAttributes.dynamicImportBindings);
    return _evalWithFilename(wrappedSource, filename);
}

function callCompiledCjsFunction(mod, compiledFn, source, filename, dirname, childRequire) {
    const previousModuleContext = globalThis.__wasm_rquickjs_current_module;
    globalThis.__wasm_rquickjs_current_module = {
        filename: filename,
        source: source
    };
    const previousCjsImportDir = globalThis.__wasm_rquickjs_cjs_import_dir;
    globalThis.__wasm_rquickjs_cjs_import_dir = dirname;
    try {
        return compiledFn.call(mod.exports, mod.exports, childRequire, mod, filename, dirname);
    } finally {
        globalThis.__wasm_rquickjs_current_module = previousModuleContext;
        if (previousCjsImportDir !== undefined) {
            globalThis.__wasm_rquickjs_cjs_import_dir = previousCjsImportDir;
        } else {
            delete globalThis.__wasm_rquickjs_cjs_import_dir;
        }
    }
}

function compileModuleInto(mod, source, filename, requireOverride) {
    filename = filename === undefined || filename === null ? mod.filename : filename;
    source = String(source);
    registerSourceMapForCjs(filename, source, mod);
    const requireParentFilename = filename === '' && mod && typeof mod.filename === 'string'
        ? mod.filename
        : filename;
    const dirname = pathModule.dirname(filename);
    const requireDirname = pathModule.dirname(requireParentFilename);
    const childRequire = requireOverride || makeRequire(requireDirname, mod, requireParentFilename);
    const compiledFn = compileCjs(filename, source);
    return callCompiledCjsFunction(mod, compiledFn, source, filename, dirname, childRequire);
}

function loaderValueTypeName(value) {
    if (value === null) return 'null';
    const type = typeof value;
    if (type !== 'object') return type;
    if (Array.isArray(value)) return 'Array';
    if (value && value.constructor && typeof value.constructor.name === 'string') return value.constructor.name;
    return 'Object';
}

function makeLoaderInvalidReturnValueError(hookName, value) {
    const err = new TypeError(`Expected an object to be returned from the '${hookName}' hook but got ${loaderValueTypeName(value)}.`);
    err.code = 'ERR_INVALID_RETURN_VALUE';
    return err;
}

function makeLoaderInvalidReturnPropertyValueError(propertyName, hookName, expected, value) {
    const err = new TypeError(`Expected ${expected} for "${propertyName}" from the '${hookName}' hook but got type ${loaderValueTypeName(value)}.`);
    err.code = 'ERR_INVALID_RETURN_PROPERTY_VALUE';
    return err;
}

function makeLoaderUnknownModuleFormatError(format) {
    const err = new RangeError(`Unknown module format: ${String(format)}`);
    err.code = 'ERR_UNKNOWN_MODULE_FORMAT';
    return err;
}

function makeLoaderInvalidUrlError(hookName, loaderUrl, value) {
    const err = new TypeError(`Expected a URL string to be returned for "url" from the '${hookName}' hook in ${String(loaderUrl)} but got ${JSON.stringify(String(value))}.`);
    err.code = 'ERR_INVALID_RETURN_PROPERTY_VALUE';
    return err;
}

function makeLoaderMissingUrlError(hookName, loaderUrl, value) {
    const err = new TypeError(`Expected a URL string to be returned for "url" from the '${hookName}' hook in ${String(loaderUrl)} but got type ${loaderValueTypeName(value)}.`);
    err.code = 'ERR_INVALID_RETURN_PROPERTY_VALUE';
    return err;
}

function makeLoaderChainError(hook) {
    const err = new Error(`${hook} hook did not call the next hook and did not explicitly short circuit`);
    err.code = 'ERR_LOADER_CHAIN_INCOMPLETE';
    return err;
}

function makeEsmModuleNotFoundError(specifier) {
    const err = new Error("Cannot find module '" + specifier + "'");
    err.code = 'ERR_MODULE_NOT_FOUND';
    return err;
}

function makeEsmUnsupportedDirImportError(filename) {
    const err = new Error('Directory import ' + JSON.stringify(filename) + ' is not supported resolving ES modules');
    err.code = 'ERR_UNSUPPORTED_DIR_IMPORT';
    return err;
}

function rustClassifiesPathSpecifier(specifier) {
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_classify_module_specifier(specifier);
}

function defaultLoaderFormatForFilename(filename) {
    if (filename.endsWith('.json')) return 'json';
    if (filename.endsWith('.mjs') || filename.endsWith('.mts')) return 'module';
    if (filename.endsWith('.cjs') || filename.endsWith('.cts')) return 'commonjs';
    return undefined;
}

function loaderFormatOrUndefined(format) {
    return format === undefined || format === null ? undefined : String(format);
}

function registeredLoaderUrlResult(url) {
    return { url };
}

function registeredLoaderUrlFormatResult(url, format) {
    return { url, format };
}

function registeredLoaderUrlFormatSourceResult(url, format, source) {
    const result = registeredLoaderUrlFormatResult(url, format);
    result.source = source;
    return result;
}

function resultForEsmFileUrl(url) {
    const filename = nodeUrl.fileURLToPath(url);
    const stat = _stat(filename);
    if (stat === 1) throw makeEsmUnsupportedDirImportError(filename);
    if (stat !== 0) throw makeEsmModuleNotFoundError(url.href);
    return registeredLoaderUrlFormatResult(url.href, defaultLoaderFormatForFilename(filename));
}

function parentFilenameForLoaderResolve(parentURL, baseUrl) {
    parentURL = String(parentURL || baseUrl);
    if (
        parentURL.startsWith('data:') &&
        typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_lookup_loader_source_url === 'function'
    ) {
        parentURL = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_lookup_loader_source_url(parentURL) || parentURL;
    }
    if (parentURL.startsWith('file://')) {
        return nodeUrl.fileURLToPath(parentURL);
    }
    if (parentURL.startsWith('/')) {
        return parentURL;
    }
    return null;
}

function registeredLoaderBuiltinResolve(specifier, cjsMode) {
    const resolved = builtinResolveSpecifier(specifier);
    if (resolved === undefined) return undefined;
    if (specifier.startsWith('node:')) {
        return cjsMode ? registeredLoaderUrlFormatResult(resolved, 'builtin') : registeredLoaderUrlResult(resolved);
    }
    return registeredLoaderUrlFormatResult(resolved, 'builtin');
}

    function packageConditionArrayForLoaderResolve(context, defaultConditions) {
        if (context && Array.isArray(context.conditions)) {
            const conditions = setFromArray(context.conditions);
            conditions.add('default');
            return Array.from(conditions);
        }
        return defaultConditions;
    }

    function packageResolutionForLoaderResult(resolved) {
        if (!resolved || !resolved.url) return undefined;
        return registeredLoaderUrlFormatResult(String(resolved.url), loaderFormatOrUndefined(resolved.format));
    }

    function cjsLoaderFileFormat(filename, format) {
        return format || (filename.endsWith('.json') ? 'json' : 'commonjs');
    }

    function cjsLoaderFileResult(filename, source, format, url) {
        const resultUrl = url === undefined ? nodeUrl.pathToFileURL(filename).href : String(url);
        const resultFormat = cjsLoaderFileFormat(filename, format);
        return source === null || source === undefined
            ? registeredLoaderUrlFormatResult(resultUrl, resultFormat)
            : registeredLoaderUrlFormatSourceResult(resultUrl, resultFormat, source);
    }

    function cjsLoaderFileUrlResult(url, format, resultUrl) {
        const filename = nodeUrl.fileURLToPath(url);
        if (_stat(filename) !== 0) return undefined;
        return cjsLoaderFileResult(filename, undefined, format, resultUrl);
    }

    function cjsPackageResolutionForLoaderResult(resolved) {
        const packageResolved = packageResolutionForLoaderResult(resolved);
        if (!packageResolved) return undefined;
        if (!packageResolved.url.startsWith('file://')) return packageResolved;
        return cjsLoaderFileUrlResult(packageResolved.url, packageResolved.format, packageResolved.url);
    }

    function resolvePackageDefaultForLoader(specifier, parentURL, context, defaultConditions, mode, mapNotFoundToCjs) {
        try {
            return resolvePackageWithRustBridge(
                parentURL,
                specifier,
                packageConditionArrayForLoaderResolve(context, defaultConditions),
                mode,
                'Internal package resolver provider is not initialized',
            );
        } catch (err) {
            if (mapNotFoundToCjs && err && err.code === 'ERR_MODULE_NOT_FOUND') {
                throw makeModuleNotFoundError(specifier);
            }
            throw err;
        }
    }

    function resolveEsmPackageDefaultForLoader(specifier, parentURL, context) {
        return packageResolutionForLoaderResult(
            resolvePackageDefaultForLoader(specifier, parentURL, context, esmPackageConditions(), 'import', false)
        );
    }

    function resolveCjsPackageDefaultForLoader(specifier, parentURL, context) {
        const resolved = resolvePackageDefaultForLoader(
            specifier,
            parentURL,
            context,
            cjsPackageConditions(),
            'cjs-analysis',
            true,
        );
        return cjsPackageResolutionForLoaderResult(resolved);
    }

    function resolveCjsDefaultForLoader(specifier, parentURL, context) {
        const parentFilename = parentFilenameForLoaderResolve(parentURL, fileUrlForPath('/'));
        const parentDir = parentFilename ? pathModule.dirname(parentFilename) : '/';
        if (specifier.startsWith('file://')) {
            return cjsLoaderFileUrlResult(specifier);
        }
        if (rustClassifiesPathSpecifier(specifier)) {
            const resolved = resolveFilename(specifier, parentDir);
            return cjsLoaderFileResult(resolved.filename, undefined);
        }
        if (specifier.startsWith('#') && parentFilename) {
            return resolveCjsPackageDefaultForLoader(specifier, parentURL, context);
        }
        const packageResolved = resolveCjsPackageDefaultForLoader(specifier, parentURL, context);
        if (packageResolved) return packageResolved;
        return undefined;
    }

    function resultForRelativeOrAbsoluteSpecifier(specifier, parentURL) {
        return resultForEsmFileUrl(new URL(specifier, parentURL));
    }

function isLoaderSourceValue(value) {
    return typeof value === 'string' ||
        value instanceof ArrayBuffer ||
        (typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer) ||
        ArrayBuffer.isView(value);
}

function registeredLoaderHasOwnSource(result) {
    return result && Object.prototype.hasOwnProperty.call(result, 'source');
}

function registeredLoaderHasSource(result) {
    return registeredLoaderHasOwnSource(result) && result.source !== null && result.source !== undefined;
}

function validateRegisteredLoaderResult(result, hookName, context) {
    if (!result || typeof result !== 'object') {
        throw makeLoaderInvalidReturnValueError(hookName, result);
    }
    if (Object.prototype.hasOwnProperty.call(result, 'format')) {
        const format = result.format;
        if (format !== undefined && format !== null && typeof format !== 'string') {
            throw makeLoaderInvalidReturnPropertyValueError('format', hookName, 'a string or nullish value', format);
        }
    }
    if (hookName === 'load' && registeredLoaderHasOwnSource(result)) {
        const source = result.source;
        if (source === null || source === undefined) {
            if (result.format === 'commonjs' || (result.format === undefined && context && context.format === 'commonjs')) return result;
            throw makeLoaderInvalidReturnPropertyValueError('source', hookName, 'a string, ArrayBuffer, or ArrayBufferView', source);
        }
        if (!isLoaderSourceValue(source)) {
            throw makeLoaderInvalidReturnPropertyValueError('source', hookName, 'a string, ArrayBuffer, or ArrayBufferView', source);
        }
    }
    return result;
}

function validateRegisteredLoaderLoadFormat(format) {
    if (format === undefined || format === null) return undefined;
    if (format === 'module' || format === 'commonjs' || format === 'json' || format === 'builtin' || format === 'wasm') {
        return format;
    }
    throw makeLoaderUnknownModuleFormatError(format);
}

function validateRegisteredLoaderResolveUrl(url, loaderUrl) {
    if (typeof url !== 'string') {
        throw makeLoaderMissingUrlError('resolve', loaderUrl, url);
    }
    try {
        new URL(url);
    } catch (_) {
        throw makeLoaderInvalidUrlError('resolve', loaderUrl, url);
    }
}

function loaderSourceToString(source) {
    if (typeof source === 'string') {
        return source;
    }
    if (source instanceof ArrayBuffer) {
        return new TextDecoder().decode(new Uint8Array(source));
    }
    if (typeof SharedArrayBuffer !== 'undefined' && source instanceof SharedArrayBuffer) {
        return new TextDecoder().decode(new Uint8Array(source));
    }
    if (ArrayBuffer.isView(source) && source.buffer instanceof ArrayBuffer) {
        return new TextDecoder().decode(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
    }
    if (
        typeof SharedArrayBuffer !== 'undefined' &&
        ArrayBuffer.isView(source) &&
        source.buffer instanceof SharedArrayBuffer
    ) {
        return new TextDecoder().decode(new Uint8Array(source.buffer, source.byteOffset, source.byteLength));
    }
    throw makeLoaderInvalidReturnPropertyValueError('source', 'load', 'a string, ArrayBuffer, or ArrayBufferView', source);
}

function loaderCommonJsSourceModule(source, url) {
    source = loaderSourceToString(source);
    const filename = loaderCommonJsFilename(url);
    const cacheKey = loaderCommonJsCacheKey(url, filename);
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_build_loader_cjs_facade !== 'function') {
        throw new Error('Internal loader CommonJS facade builder is not initialized');
    }
    const facade = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_build_loader_cjs_facade(
        filename,
        String(url || ''),
        cacheKey,
        source,
    );
    return facade;
}

function loaderFileUrlSource(url) {
    if (!String(url).startsWith('file://')) return null;
    try {
        return tryReadFile(nodeUrl.fileURLToPath(url));
    } catch (_) {
        return null;
    }
}

function registeredLoaderPathOrUrlReturn(url, preserveFileUrlSuffix) {
    url = String(url);
    if (!url.startsWith('file://')) return url;
    const path = nodeUrl.fileURLToPath(url);
    if (!preserveFileUrlSuffix) return path;
    if (/[?#]/.test(path)) return url;
    const suffixStart = url.search(/[?#]/);
    return suffixStart < 0 ? path : path + url.slice(suffixStart);
}

function loaderCommonJsFilename(url) {
    url = String(url || '');
    if (url.startsWith('file://')) {
        return nodeUrl.fileURLToPath(url);
    }
    if (url.startsWith('/')) {
        return url;
    }
    return url || 'anonymous';
}

function loaderCommonJsCacheKey(url, filename) {
    return filename;
}

function validateRequireId(id) {
    if (typeof id !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('id', 'string', id);
    }
    if (id === '') {
        const argErr = new TypeError("The argument 'id' must be a non-empty string. Received ''");
        argErr.code = 'ERR_INVALID_ARG_VALUE';
        throw argErr;
    }
}

function validateRequireRequest(request) {
    if (typeof request !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
    }
}

function markRequireEsmForcedModule(resolvedFilename) {
    let registry = globalThis.__wasm_rquickjs_require_esm_forced_module;
    if (!registry || typeof registry !== 'object') {
        registry = Object.create(null);
        Object.defineProperty(globalThis, '__wasm_rquickjs_require_esm_forced_module', {
            value: registry,
            writable: true,
            configurable: true,
            enumerable: false,
        });
    }
    registry[resolvedFilename] = true;
    registry[nodeUrl.pathToFileURL(resolvedFilename).href] = true;
}

function unmarkRequireEsmForcedModule(resolvedFilename) {
    const registry = globalThis.__wasm_rquickjs_require_esm_forced_module;
    if (!registry || typeof registry !== 'object') return;
    delete registry[resolvedFilename];
    delete registry[nodeUrl.pathToFileURL(resolvedFilename).href];
}

function requireEsmWithCacheGuard(mod, resolvedFilename, forceModule) {
    throwIfRequireEsmGraphCycle(resolvedFilename);
    const markedGraph = markRequireEsmGraph(resolvedFilename);
    Object.defineProperty(mod, '__wasmRequireEsmInProgress', {
        value: true,
        writable: true,
        configurable: true,
        enumerable: false,
    });
    try {
        if (forceModule) markRequireEsmForcedModule(resolvedFilename);
        const namespace = _requireEsm(resolvedFilename);
        if (namespace && typeof namespace === 'object' && Object.hasOwn(namespace, 'module.exports')) {
            return namespace['module.exports'];
        }
        return wrapEsmNamespace(namespace);
    } finally {
        if (forceModule) unmarkRequireEsmForcedModule(resolvedFilename);
        unmarkRequireEsmGraph(markedGraph);
        delete mod.__wasmRequireEsmInProgress;
    }
}

function currentMainScriptFilename() {
    if (!globalThis.process || !globalThis.process.argv || typeof globalThis.process.argv[1] !== 'string') {
        return null;
    }
    const mainScript = globalThis.process.argv[1];
    if (!mainScript) return null;
    try {
        return toCjsCanonicalFilename(mainScript, true);
    } catch (_) {
        const absolute = pathModule.isAbsolute(mainScript) ? mainScript : pathModule.resolve('/', mainScript);
        return absolute;
    }
}

function isMainEntryFilename(resolvedFilename) {
    if (typeof mainModule === 'undefined' || mainModule.filename !== '/') return false;
    const mainScript = currentMainScriptFilename();
    if (!mainScript) return false;
    try {
        return toCjsCanonicalFilename(resolvedFilename, true) === mainScript;
    } catch (_) {
        const absolute = pathModule.isAbsolute(resolvedFilename) ? resolvedFilename : pathModule.resolve('/', resolvedFilename);
        return absolute === mainScript;
    }
}

function unlinkModuleFromParent(parentModule, mod) {
    if (!parentModule || !parentModule.children) return;
    const index = parentModule.children.indexOf(mod);
    if (index !== -1) parentModule.children.splice(index, 1);
}

function discardCjsModuleLoad(cacheKey, parentModule, mod) {
    delete currentModuleCache()[cacheKey];
    unlinkModuleFromParent(parentModule, mod);
}

function defineEnumerableWritable(obj, name, value) {
    Object.defineProperty(obj, name, {
        value,
        writable: true,
        enumerable: true,
        configurable: true,
    });
}

function initializeCjsModuleRecord(mod, id, filename, dirname, parentModule, pathsBase) {
    defineEnumerableWritable(mod, 'id', id);
    defineEnumerableWritable(mod, 'filename', filename);
    defineEnumerableWritable(mod, 'path', dirname);
    defineEnumerableWritable(mod, 'exports', {});
    defineEnumerableWritable(mod, 'loaded', false);
    defineEnumerableWritable(mod, 'parent', parentModule || null);
    defineEnumerableWritable(mod, 'children', []);
    defineEnumerableWritable(mod, 'paths', _nodeModulePaths(pathsBase));
    moduleRequireOverrides.delete(mod);
    installCjsEsmDefaultSnapshotSlot(mod);
    return mod;
}

function loadCommonJsTransaction(descriptor) {
    const filename = descriptor.filename;
    let source = descriptor.source;
    const parentModule = descriptor.parentModule || null;
    const isLoaderSource = descriptor.sourceKind === 'loader';
    const isMainModuleLoad = descriptor.isMainModule === true;
    const canFallbackToEsm = descriptor.allowEsmFallback === true;
    const preparedTypeScriptGraph = descriptor.preparedTypeScriptGraph;
    const preparedTypeScript = preparedTypeScriptGraph && preparedTypeScriptGraph[filename];
    if (preparedTypeScript) delete preparedTypeScriptGraph[filename];
    const cacheKey = descriptor.cacheKey;
    const dirname = pathModule.dirname(filename);
    const pathsBase = isLoaderSource && !pathModule.isAbsolute(filename) ? '/' : dirname;

    // Check cache
    if (currentModuleCache()[cacheKey]) {
        throwIfRequireEsmGraphCycle(cacheKey);
        const cached = currentModuleCache()[cacheKey];
        if (cached.__wasmRequireEsmInProgress) {
            const err = new Error('Cannot require() ES Module ' + filename + ' in a cycle.');
            err.code = 'ERR_REQUIRE_CYCLE_MODULE';
            throw err;
        }
        if (parentModule && parentModule.children && !parentModule.children.includes(cached)) {
            parentModule.children.push(cached);
        }
        return cached;
    }

    let mod;
    if (isMainModuleLoad) {
        mod = mainModule;
        initializeCjsModuleRecord(mod, '.', filename, dirname, null, dirname);
        if (globalThis.process) {
            globalThis.process.mainModule = mod;
        }
    } else {
        mod = initializeCjsModuleRecord(
            Object.create(Module.prototype),
            filename,
            filename,
            dirname,
            parentModule,
            pathsBase,
        );
    }

    // Cache before executing (handles circular dependencies)
    currentModuleCache()[cacheKey] = mod;
    if (parentModule && parentModule.children) {
        parentModule.children.push(mod);
    }

    let cjsEsmDefaultSnapshotEligible = false;

    if (isLoaderSource) {
        try {
            if (descriptor.sourceFormat === 'json') {
                if (source.length > 0 && source.charCodeAt(0) === 0xFEFF) {
                    source = source.slice(1);
                }
                try {
                    mod.exports = JSON.parse(source);
                } catch (error) {
                    const invalidJson = new SyntaxError(filename + ': ' + error.message);
                    invalidJson.code = 'ERR_INVALID_JSON';
                    throw invalidJson;
                }
            } else {
                const loaderRequire = makeLoaderCommonJsRequire(
                    descriptor.sourceUrl || (pathModule.isAbsolute(filename) ? fileUrlForPath(filename) : filename),
                    pathModule.isAbsolute(filename) ? dirname : '/',
                    mod,
                    filename,
                );
                moduleRequireOverrides.set(mod, loaderRequire);
                compileModuleInto(mod, source, filename, loaderRequire);
            }
            cjsEsmDefaultSnapshotEligible = true;
        } catch (err) {
            discardCjsModuleLoad(cacheKey, parentModule, mod);
            throw err;
        }
    } else {
    // Check for custom extension handler
    const ext = findLongestRegisteredExtension(filename);
    const handler = requireExtensions[ext];
    if (handler && !_defaultExtHandlers.has(handler)) {
        try {
            handler(mod, filename);
            cjsEsmDefaultSnapshotEligible = true;
        } catch (err) {
            discardCjsModuleLoad(cacheKey, parentModule, mod);
            throw err;
        }
    } else if (handler === defaultNodeExtensionHandler) {
        discardCjsModuleLoad(cacheKey, parentModule, mod);
        const err = new Error("Native .node modules are not supported in WASM: '" + filename + "'");
        err.code = 'ERR_DLOPEN_FAILED';
        throw err;
    } else if (handler === defaultJsonExtensionHandler) {
        try {
            source = fsModule.readFileSync(filename, 'utf8');
            registerSourceMapForCjs(filename, source, mod);
        } catch (err) {
            discardCjsModuleLoad(cacheKey, parentModule, mod);
            throw err;
        }
        try {
            if (source.length > 0 && source.charCodeAt(0) === 0xFEFF) {
                source = source.slice(1);
            }
            mod.exports = JSON.parse(source);
        } catch (e) {
            discardCjsModuleLoad(cacheKey, parentModule, mod);
            const err = new SyntaxError(filename + ': ' + e.message);
            err.code = 'ERR_INVALID_JSON';
            throw err;
        }
    } else {
        const packageScope = (filename.endsWith('.js') || filename.endsWith('.ts'))
            ? getPackageScopeInfo(filename)
            : null;
        const explicitPackageType = packageScope ? packageScope.packageType : null;
        const isCommonJsPackage = explicitPackageType === 'commonjs' ||
            (explicitPackageType === null && packageScope !== null && packageScope.isNodeModulesPackage);
        const isEsm = filename.endsWith('.mjs') || filename.endsWith('.mts') ||
            ((filename.endsWith('.js') || filename.endsWith('.ts')) && explicitPackageType === 'module');
        if (isEsm && rustHasExecArgvFlag('--no-experimental-require-module')) {
            discardCjsModuleLoad(cacheKey, parentModule, mod);
            const esmErr = new Error(
                "require() of ES Module " + filename + " not supported. " +
                "Instead change the require of " + filename + " to a dynamic " +
                "import() which is available in all CommonJS modules."
            );
            esmErr.code = 'ERR_REQUIRE_ESM';
            throw esmErr;
        }
        if (isEsm) {
            try {
                mod.exports = requireEsmWithCacheGuard(mod, filename);
            } catch (err) {
                discardCjsModuleLoad(cacheKey, parentModule, mod);
                throw err;
            }
        } else {
            try {
                source = preparedTypeScript
                    ? preparedTypeScript.originalSource
                    : fsModule.readFileSync(filename, 'utf8');
                registerSourceMapForCjs(filename, source, mod);
            } catch (err) {
                discardCjsModuleLoad(cacheKey, parentModule, mod);
                throw err;
            }
            const dirname = pathModule.dirname(filename);
            let compiledSource;
            let typeScriptExportNames;
            try {
                compiledSource = preparedTypeScript
                    ? preparedTypeScript.preparedSource
                    : prepareCommonJsTypeScript(filename, source);
                typeScriptExportNames = preparedTypeScript && preparedTypeScript.exportNames;
            } catch (err) {
                discardCjsModuleLoad(cacheKey, parentModule, mod);
                throw err;
            }
            const childRequire = makeRequire(
                dirname,
                mod,
                undefined,
                mainModule,
                preparedTypeScriptGraph,
            );
            let compiledFn;
            let cjsSyntaxError = null;
            const shouldFallbackToEsm = canFallbackToEsm &&
                !filename.endsWith('.cjs') && !filename.endsWith('.cts') && !isCommonJsPackage;
            let cjsWrapperLexicalRedeclaration = false;
            let cjsSourceLooksEsm = false;
            try {
                compiledFn = compileCjs(
                    filename,
                    compiledSource,
                    true,
                );
            } catch (err) {
                // Normalize QuickJS SyntaxError messages for ESM keywords in CJS context
                if (err && err.name === 'SyntaxError') {
                    normalizeEsmSyntaxError(err);
                } else if (err && typeof err.message === 'string' && err.message === 'return not in a function') {
                    markAsSyntaxError(err);
                }
                // For .js files (not .cjs), detect ESM syntax and fall back to ESM loading
                if (shouldFallbackToEsm && err && err.name === 'SyntaxError') {
                    const analysis = rustModuleSourceAnalysis(source);
                    cjsSourceLooksEsm = analysis.looksLikeEsm;
                    cjsWrapperLexicalRedeclaration = analysis.hasCjsWrapperLexicalRedeclaration;
                }
                if (shouldFallbackToEsm && err && err.name === 'SyntaxError' && (cjsSourceLooksEsm || cjsWrapperLexicalRedeclaration)) {
                    cjsSyntaxError = err;
                } else {
                    discardCjsModuleLoad(cacheKey, parentModule, mod);
                    maybeSetArrowMessageOnSyntaxError(err, filename, source);
                    throw err;
                }
            }
            if (cjsSyntaxError || cjsWrapperLexicalRedeclaration) {
                if (rustHasExecArgvFlag('--no-experimental-require-module') && cjsSyntaxError) {
                    discardCjsModuleLoad(cacheKey, parentModule, mod);
                    maybeSetArrowMessageOnSyntaxError(cjsSyntaxError, filename, source);
                    throw cjsSyntaxError;
                }
                // SyntaxError in a .js file — try loading as ESM (entry point detection)
                try {
                    mod.exports = requireEsmWithCacheGuard(mod, filename, true);
                } catch (esmErr) {
                    discardCjsModuleLoad(cacheKey, parentModule, mod);
                    if (cjsSourceLooksEsm || cjsWrapperLexicalRedeclaration) {
                        normalizeEsmSyntaxError(esmErr);
                        throw esmErr;
                    }
                    // ESM loading also failed — throw the original CJS SyntaxError
                    maybeSetArrowMessageOnSyntaxError(cjsSyntaxError, filename, source);
                    throw cjsSyntaxError;
                }
            } else if (compiledFn) {
                try {
                    callCompiledCjsFunction(mod, compiledFn, source, filename, dirname, childRequire);
                } catch (err) {
                    discardCjsModuleLoad(cacheKey, parentModule, mod);
                    maybeSetArrowMessageOnSyntaxError(err, filename, source);
                    throw err;
                }
                if (typeScriptExportNames !== undefined) {
                    captureCjsTypeScriptExportNames(
                        mod,
                        typeScriptExportNames,
                        source,
                    );
                }
                if (isTypeScriptFilename(filename) && typeScriptExportNames === undefined) {
                    captureCjsTypeScriptPreparedSource(
                        mod,
                        source,
                        compiledSource,
                    );
                }
                cjsEsmDefaultSnapshotEligible = true;
            }
        }
    }

    }

    mod.loaded = true;
    if (cjsEsmDefaultSnapshotEligible) {
        captureCjsEsmDefaultSnapshot(mod);
    }
    return mod;
}

function loadFilesystemCommonJs(resolvedFilename, parentModule, preparedTypeScriptGraph = undefined) {
    const isMainModule = isMainEntryFilename(resolvedFilename);
    const filename = toCjsCanonicalFilename(resolvedFilename, isMainModule);
    return loadCommonJsTransaction({
        cacheKey: filename,
        filename,
        parentModule,
        sourceKind: 'filesystem',
        source: undefined,
        sourceUrl: undefined,
        isMainModule,
        allowEsmFallback: true,
        preparedTypeScriptGraph,
    });
}

function makeLoaderCommonJsRequire(parentUrl, parentDir, parentModule, parentFilename) {
    const fallbackRequire = makeRequire(parentDir, parentModule, parentFilename);
    function loaderRequire(id) {
        validateRequireId(id);
        if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders_sync === 'function') {
            const loaded = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders_sync(parentUrl, id);
            if (loaded) {
                if (loaded.format === 'builtin' && loaded.url) {
                    const id = String(loaded.url).startsWith('node:') ? String(loaded.url) : 'node:' + String(loaded.url);
                    const builtin = builtinModuleForSpecifier(id);
                    if (builtin !== undefined) return builtin;
                }
                if (loaded.format === 'commonjs' && registeredLoaderHasSource(loaded)) {
                    const filename = loaderCommonJsFilename(loaded.url);
                    return loadCommonJsLoaderSourceExports(filename, loaded.source, loaded.url, loaderCommonJsCacheKey(loaded.url, filename), parentModule);
                }
                if (loaded.format === 'json' && registeredLoaderHasSource(loaded)) {
                    const filename = loaderCommonJsFilename(loaded.url);
                    return loadJsonLoaderSourceExports(
                        filename,
                        loaded.source,
                        loaded.url,
                        loaderCommonJsCacheKey(loaded.url, filename),
                        parentModule,
                    );
                }
                if (
                    (loaded.format === 'commonjs' || loaded.format === 'json') &&
                    loaded.url &&
                    String(loaded.url).startsWith('file://')
                ) {
                    return loadFilesystemCommonJs(loaderCommonJsFilename(loaded.url), parentModule).exports;
                }
            }
        }
        return fallbackRequire(id);
    }
    loaderRequire.cache = fallbackRequire.cache;
    loaderRequire.extensions = fallbackRequire.extensions;
    loaderRequire.resolve = function resolve(id, options) {
        validateRequireRequest(id);
        if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders_sync === 'function') {
            const loaded = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders_sync(parentUrl, id, true);
            if (loaded && loaded.url) {
                const loadedUrl = String(loaded.url);
                if (loadedUrl.startsWith('node:')) return id.startsWith('node:') ? loadedUrl : loadedUrl.slice(5);
                return registeredLoaderPathOrUrlReturn(loadedUrl);
            }
        }
        return fallbackRequire.resolve(id, options);
    };
    loaderRequire.resolve.paths = fallbackRequire.resolve.paths;
    loaderRequire.main = fallbackRequire.main;
    return loaderRequire;
}

function loadCommonJsLoaderSourceExports(filename, source) {
    const sourceUrl = arguments.length > 2 ? String(arguments[2]) : undefined;
    const cacheKey = arguments.length > 3 ? String(arguments[3]) : undefined;
    const parentModule = arguments.length > 4 ? arguments[4] : null;
    filename = String(filename);
    return loadCommonJsTransaction({
        cacheKey: cacheKey || filename,
        filename,
        parentModule,
        sourceKind: 'loader',
        sourceFormat: 'commonjs',
        source: loaderSourceToString(source),
        sourceUrl,
        isMainModule: false,
        allowEsmFallback: false,
    }).exports;
}

function loadJsonLoaderSourceExports(filename, source) {
    const sourceUrl = arguments.length > 2 ? String(arguments[2]) : undefined;
    const cacheKey = arguments.length > 3 ? String(arguments[3]) : undefined;
    const parentModule = arguments.length > 4 ? arguments[4] : null;
    filename = String(filename);
    return loadCommonJsTransaction({
        cacheKey: cacheKey || filename,
        filename,
        parentModule,
        sourceKind: 'loader',
        sourceFormat: 'json',
        source: loaderSourceToString(source),
        sourceUrl,
        isMainModule: false,
        allowEsmFallback: false,
    }).exports;
}

if (typeof globalThis.__wasm_rquickjs_load_commonjs_loader_source !== 'function') {
    Object.defineProperty(globalThis, '__wasm_rquickjs_load_commonjs_loader_source', {
        value: loadCommonJsLoaderSourceExports,
        writable: false,
        configurable: false,
    });
}

// The root "main" module
const mainModule = Object.assign(Object.create(Module.prototype), {
    id: '.',
    filename: '/',
    path: '/',
    exports: {},
    loaded: true,
    parent: null,
    children: [],
});
installCjsEsmDefaultSnapshotSlot(mainModule);

function rustSplitPackageName(id) {
    return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_split_module_package_name(id);
}

function resolveFromNodeModules(id, parentDir, parentFilename, conditions, lookupPaths, resolution) {
    resolution = resolution || makeCjsResolutionState();
    conditions = conditions || cjsPackageConditions();
    const dirs = Array.isArray(lookupPaths) ? lookupPaths : _nodeModulePaths(parentDir);
    const cacheKey = cjsPathCacheKey(id, dirs);
    const cached = cjsCachedPathResolution(cjsPathCacheValue(cacheKey));
    if (cached !== null) return cached;

    // Split into package name and subpath for packages with subpath specifiers
    const parts = rustSplitPackageName(id);

    const selfResolved = resolvePackageSelfReference(parts, parentDir, conditions, resolution);
    if (selfResolved !== undefined) {
        cjsSetPathCacheResolvedFilename(cacheKey, selfResolved.filename);
        return selfResolved;
    }

    for (let i = 0; i < dirs.length; i++) {
        const pkgDir = pathModule.join(dirs[i], parts.name);
        const exportsResolved = resolvePackageExportsEntry(parts, pkgDir, conditions, resolution);
        if (exportsResolved !== undefined) {
            cjsSetPathCacheResolvedFilename(cacheKey, exportsResolved.filename);
            return exportsResolved;
        }

        const fallbackResolved = resolveCjsPackageFallbacks(parts, pkgDir, id, parentFilename || parentDir);
        if (fallbackResolved !== null) {
            cjsSetPathCacheResolvedFilename(cacheKey, fallbackResolved.filename);
            return fallbackResolved;
        }

    }
    return null;
}

function cjsLookupPathsForResolveOptions(searchPaths) {
    const lookupPaths = [];
    function pushUnique(path) {
        if (!lookupPaths.includes(path)) lookupPaths.push(path);
    }
    for (let pi = 0; pi < searchPaths.length; pi++) {
        if (typeof searchPaths[pi] !== 'string') {
            const argErr = new TypeError("The argument 'paths[" + pi + "]' must be a string. Received " + typeof searchPaths[pi]);
            argErr.code = 'ERR_INVALID_ARG_VALUE';
            throw argErr;
        }
        const nodeModulePaths = _nodeModulePaths(pathModule.resolve(searchPaths[pi]));
        for (let i = 0; i < nodeModulePaths.length; i++) {
            pushUnique(nodeModulePaths[i]);
        }
    }
    for (let i = 0; i < globalPaths.length; i++) {
        pushUnique(globalPaths[i]);
    }
    return lookupPaths;
}

function resolveForRequire(id, options, parentDir, parentFilename, parentLookupPaths) {
    validateRequireRequest(id);
    if (isBuiltin(id)) {
        return id;
    }
    if (id.startsWith('node:')) {
        const err = new Error("Cannot find module '" + id + "'");
        err.code = 'MODULE_NOT_FOUND';
        throw err;
    }
    // If paths option is provided, resolve relative to each path
    if (options && options.paths !== undefined) {
        const searchPaths = options.paths;
        if (!Array.isArray(searchPaths)) {
            const argErr = new TypeError("The argument 'paths' must be an array of strings. Received " + typeof searchPaths);
            argErr.code = 'ERR_INVALID_ARG_VALUE';
            throw argErr;
        }
        const isRelative = rustClassifiesPathSpecifier(id);
        if (!isRelative) {
            const lookupPaths = cjsLookupPathsForResolveOptions(searchPaths);
            const resolution = makeCjsResolutionState();
            const nmResolved = resolveFromNodeModules(id, parentDir, parentFilename, undefined, lookupPaths, resolution);
            if (nmResolved) {
                return nmResolved.__wasmPathCacheHit
                    ? nmResolved.filename
                    : toCjsCanonicalFilename(nmResolved.filename, false);
            }
            const err = new Error("Cannot find module '" + id + "'");
            err.code = 'MODULE_NOT_FOUND';
            throw addRequireStackToModuleNotFound(err, id, parentFilename);
        }
        for (let pi = 0; pi < searchPaths.length; pi++) {
            if (typeof searchPaths[pi] !== 'string') {
                const argErr = new TypeError("The argument 'paths[" + pi + "]' must be a string. Received " + typeof searchPaths[pi]);
                argErr.code = 'ERR_INVALID_ARG_VALUE';
                throw argErr;
            }
            const searchDir = pathModule.resolve(searchPaths[pi]);
            const cacheKey = cjsPathCacheKey(id, pathModule.isAbsolute(id) ? [''] : [searchDir]);
            const cached = cjsCachedPathResolution(cjsPathCacheValue(cacheKey));
            if (cached !== null) return cached.filename;
            try {
                const resolved = resolveFilename(id, searchDir);
                const canonical = toCjsCanonicalFilename(resolved.filename, false);
                cjsSetPathCacheValue(cacheKey, canonical);
                return canonical;
            } catch (e) {
                addRequireStackToModuleNotFound(e, id, parentFilename);
                // Try next path
            }
        }
        const err = new Error("Cannot find module '" + id + "'");
        err.code = 'MODULE_NOT_FOUND';
        throw addRequireStackToModuleNotFound(err, id, parentFilename);
    }
    if (rustClassifiesPathSpecifier(id)) {
        const cacheKey = cjsPathCacheKey(id, pathModule.isAbsolute(id) ? [''] : [parentDir]);
        const cached = cjsCachedPathResolution(cjsPathCacheValue(cacheKey));
        if (cached !== null) return cached.filename;
        try {
            const resolved = resolveFilename(id, parentDir);
            const canonical = toCjsCanonicalFilename(resolved.filename, false);
            cjsSetPathCacheValue(cacheKey, canonical);
            return canonical;
        } catch (err) {
            throw addRequireStackToModuleNotFound(err, id, parentFilename);
        }
    }
    if (id.startsWith('#')) {
        const resolution = makeCjsResolutionState();
        const importsResolved = resolveCjsPackageImportOrNodeModules(id, parentDir, parentFilename, parentLookupPaths, resolution);
        if (importsResolved.builtin) return importsResolved.builtin;
        return toCjsCanonicalFilename(importsResolved.filename, false);
    }
    // node_modules resolution for bare specifiers
    const resolution = makeCjsResolutionState();
    const nmResolved = resolveFromNodeModules(id, parentDir, parentFilename, undefined, parentLookupPaths, resolution);
    if (nmResolved) {
        return nmResolved.__wasmPathCacheHit
            ? nmResolved.filename
            : toCjsCanonicalFilename(nmResolved.filename, false);
    }
    const err = new Error("Cannot find module '" + id + "'");
    err.code = 'MODULE_NOT_FOUND';
    throw err;
}

function currentRequireMain() {
    return mainModule.filename === '/' ? undefined : mainModule;
}

function makeRequire(parentDir, parentModule, parentFilenameOverride, requireMainOverride, preparedTypeScriptGraph) {
    const parentFilename = parentFilenameOverride || (parentModule && parentModule.filename) || null;
    const parentLookupPaths = parentModule && Array.isArray(parentModule.paths)
        ? parentModule.paths.concat(globalPaths)
        : null;
    function localRequire(id) {
        validateRequireId(id);

        return traceModuleRequire(id, parentFilename, () => {
        // Capture buffer.kMaxLength for zlib on first require (matches Node.js CJS capture-at-require semantics)
        if ((id === 'zlib' || id === 'node:zlib') && zlib._captureKMaxLength) {
            zlib._captureKMaxLength();
        }

        // Check module mock registry
        const mockEntry = _resolveRequireMock(id);
        if (mockEntry) {
            if (mockEntry.cache && mockEntry._cachedCjsReady) {
                return mockEntry._cachedCjsResult;
            }
            const mockResult = _materializeCjsMock(mockEntry);
            if (mockEntry.cache) {
                mockEntry._cachedCjsResult = mockResult;
                mockEntry._cachedCjsReady = true;
            }
            return mockResult;
        }

        // node:-prefixed requires always go to builtins, bypassing cache
        if (id.startsWith('node:')) {
            const builtin = requireBuiltinModule(id);
            if (builtin !== undefined) {
                return builtin;
            }
        }

        // Check require.cache before builtins for non-node: specifiers
        // (allows shadowing builtins via require.cache)
        const cached = currentModuleCache()[id];
        if (cached !== undefined) {
            throwIfRequireEsmGraphCycle(id);
            if (cached.__wasmRequireEsmInProgress) {
                const err = new Error('Cannot require() ES Module ' + id + ' in a cycle.');
                err.code = 'ERR_REQUIRE_CYCLE_MODULE';
                throw err;
            }
            return cached.exports;
        }

        // Builtin modules
        const builtin = requireBuiltinModule(id);
        if (builtin !== undefined) {
            return builtin;
        }

        // Relative or absolute file paths
        if (rustClassifiesPathSpecifier(id)) {
            const cacheKey = cjsPathCacheKey(id, pathModule.isAbsolute(id) ? [''] : [parentDir]);
            const cached = cjsCachedPathResolution(cjsPathCacheValue(cacheKey));
            if (cached !== null) {
                const mod = loadFilesystemCommonJs(cached.filename, parentModule || null, preparedTypeScriptGraph);
                return mod.exports;
            }
            let resolved;
            try {
                resolved = resolveFilename(id, parentDir);
            } catch (err) {
                throw addRequireStackToModuleNotFound(err, id, parentFilename);
            }
            cjsSetPathCacheResolvedFilename(cacheKey, resolved.filename);
            const mod = loadFilesystemCommonJs(resolved.filename, parentModule || null, preparedTypeScriptGraph);
            return mod.exports;
        }

        if (id.startsWith('#')) {
            const resolution = makeCjsResolutionState();
            const importsResolved = resolveCjsPackageImportOrNodeModules(id, parentDir, parentFilename, parentLookupPaths, resolution);
            if (importsResolved.builtin) return requireBuiltinModule(importsResolved.builtin);
            const mod = loadFilesystemCommonJs(importsResolved.filename, parentModule || null, preparedTypeScriptGraph);
            return mod.exports;
        }

        // node_modules resolution for bare specifiers
        const resolution = makeCjsResolutionState();
        const nmResolved = resolveFromNodeModules(id, parentDir, parentFilename, undefined, parentLookupPaths, resolution);
        if (nmResolved) {
            const mod = loadFilesystemCommonJs(nmResolved.filename, parentModule || null, preparedTypeScriptGraph);
            return mod.exports;
        }

        const err = new Error("Cannot find module '" + id + "'");
        err.code = 'MODULE_NOT_FOUND';
        throw err;
        });
    }

    localRequire.cache = currentModuleCache();
    localRequire.extensions = requireExtensions;

    localRequire.resolve = function resolve(id, options) {
        return resolveForRequire(id, options, parentDir, parentFilename, parentLookupPaths);
    };

    localRequire.resolve.paths = function paths(request) {
        validateRequireRequest(request);
        if (isBuiltin(request)) {
            return null;
        }
        return _resolveLookupPaths(request, parentModule);
    };

    Object.defineProperty(localRequire, 'main', {
        value: arguments.length >= 4 ? requireMainOverride : mainModule,
        writable: true,
        configurable: true,
        enumerable: true,
    });

    return localRequire;
}

// The global require, rooted at '/'
const globalRequire = makeRequire('/', mainModule);

export let require = globalRequire;

export let createRequire = function createRequire(filename) {
    let filepath;
    const isUrlObj = filename instanceof URL ||
        (filename !== null && typeof filename === 'object' &&
         typeof filename.href === 'string' && typeof filename.protocol === 'string');

    if (isUrlObj || (typeof filename === 'string' && !pathModule.isAbsolute(filename))) {
        try {
            filepath = nodeUrl.fileURLToPath(filename);
        } catch (e) {
            const inspected = typeof filename === 'string' ? "'" + filename + "'" :
                (typeof util.inspect === 'function' ? util.inspect(filename) : String(filename));
            const err = new TypeError(
                "The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received " + inspected
            );
            err.code = 'ERR_INVALID_ARG_VALUE';
            throw err;
        }
    } else if (typeof filename !== 'string') {
        const inspected2 = typeof util.inspect === 'function' ? util.inspect(filename) : String(filename);
        const err2 = new TypeError(
            "The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received " + inspected2
        );
        err2.code = 'ERR_INVALID_ARG_VALUE';
        throw err2;
    } else {
        filepath = filename;
    }
    const dir = pathModule.dirname(filepath);
    const syntheticParent = {
        id: filepath,
        filename: filepath,
        path: dir,
        exports: {},
        loaded: true,
        parent: null,
        children: [],
        paths: _nodeModulePaths(dir),
    };
    return makeRequire(dir, syntheticParent, filepath, currentRequireMain());
};

Object.defineProperty(globalThis, '__wasm_rquickjs_create_require', {
    value: createRequire,
    writable: false,
    configurable: false,
});

function isUrlInstance(value) {
    return value instanceof URL ||
        (value !== null && typeof value === 'object' &&
            typeof value.href === 'string' && typeof value.protocol === 'string');
}

function normalizeFindPackageJsonSpecifier(specifier) {
    if (specifier === undefined) {
        throw new ERR_MISSING_ARGS('specifier');
    }

    if (isUrlInstance(specifier)) {
        const filePath = nodeUrl.fileURLToPath(specifier);
        return {
            kind: 'absolute',
            path: filePath,
            source: filePath,
        };
    }

    if (typeof specifier !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('specifier', ['string', 'URL'], specifier);
    }

    if (specifier.startsWith('file://')) {
        const filePath = nodeUrl.fileURLToPath(specifier);
        return {
            kind: 'absolute',
            path: filePath,
            source: specifier,
        };
    }

    if (pathModule.isAbsolute(specifier)) {
        return {
            kind: 'absolute',
            path: pathModule.normalize(specifier),
            source: specifier,
        };
    }

    if (specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../')) {
        return {
            kind: 'relative',
            value: specifier,
        };
    }

    return {
        kind: 'bare',
        value: specifier,
    };
}

function normalizeFindPackageJsonBase(base, baseRequired) {
    if (base === undefined) {
        if (baseRequired) {
            throw new ERR_INVALID_ARG_TYPE('base', ['string', 'URL'], base);
        }
        return null;
    }

    if (isUrlInstance(base) || (typeof base === 'string' && base.startsWith('file://'))) {
        const filename = nodeUrl.fileURLToPath(base);
        return {
            filename,
            dir: pathModule.dirname(pathModule.resolve(filename)),
        };
    }

    if (typeof base !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('base', ['string', 'URL'], base);
    }

    if (!pathModule.isAbsolute(base)) {
        throw new ERR_INVALID_ARG_TYPE('base', ['string', 'URL'], base);
    }

    const filename = pathModule.resolve(base);
    return {
        filename,
        dir: pathModule.dirname(filename),
    };
}

function findNearestPackageJsonPath(startDir) {
    let dir = pathModule.resolve(startDir || '/');
    while (true) {
        if (pathModule.basename(dir) === 'node_modules') return undefined;
        const pkgJsonPath = pathModule.join(dir, 'package.json');
        if (tryReadFile(pkgJsonPath) !== null) {
            return pathModule.toNamespacedPath(pkgJsonPath);
        }
        const parent = pathModule.dirname(dir);
        if (parent === dir) return undefined;
        dir = parent;
    }
}

function packageSearchStartDir(resolvedPath, sourceSpecifier) {
    if (typeof sourceSpecifier === 'string' &&
        (/\/$/.test(sourceSpecifier) || /(?:^|\/)\.\.?$/.test(sourceSpecifier))) {
        return pathModule.resolve(resolvedPath);
    }

    if (_stat(resolvedPath) === 1) {
        return pathModule.resolve(resolvedPath);
    }

    return pathModule.dirname(pathModule.resolve(resolvedPath));
}

function findBarePackageJson(specifier, parentDir, parentFilename) {
    const resolved = resolveFromNodeModules(
        specifier,
        parentDir,
        parentFilename,
        cjsPackageConditions(),
        undefined,
        makeCjsResolutionState(),
    );
    if (resolved === null) return undefined;

    if (typeof resolved.packageDir === 'string' && resolved.packageDir.length > 0) {
        const pkgJsonPath = pathModule.join(resolved.packageDir, 'package.json');
        if (tryReadFile(pkgJsonPath) !== null) {
            return pathModule.toNamespacedPath(pkgJsonPath);
        }
    }

    return undefined;
}

export let findPackageJSON = function findPackageJSON(specifier, base) {
    const normalizedSpecifier = normalizeFindPackageJsonSpecifier(specifier);
    if (normalizedSpecifier.kind === 'absolute') {
        const startDir = packageSearchStartDir(normalizedSpecifier.path, normalizedSpecifier.source);
        return findNearestPackageJsonPath(startDir);
    }

    const normalizedBase = normalizeFindPackageJsonBase(base, true);
    if (normalizedSpecifier.kind === 'relative') {
        const resolvedPath = pathModule.resolve(normalizedBase.dir, normalizedSpecifier.value);
        const startDir = packageSearchStartDir(resolvedPath, normalizedSpecifier.value);
        return findNearestPackageJsonPath(startDir);
    }

    return findBarePackageJson(normalizedSpecifier.value, normalizedBase.dir, normalizedBase.filename);
};

export let builtinModules = builtinModuleNames;

export let isBuiltinModule = function isBuiltinModule(id) {
    return isBuiltin(id);
};

export let register = function register(specifier, parentURL, options) {
    const url = String(specifier);
    let parent = parentURL;
    let data;
    if (parentURL && typeof parentURL === 'object' && !isUrlInstance(parentURL)) {
        parent = parentURL.parentURL;
        data = parentURL.data;
    } else if (options && typeof options === 'object') {
        data = options.data;
    }
    parent = parent === undefined ? undefined : String(parent);
    const loaders = globalThis.__wasm_rquickjs_registered_loaders ||
        (globalThis.__wasm_rquickjs_registered_loaders = []);
    const realm = globalThis.__wasm_rquickjs_registered_loader_realm_counter =
        (globalThis.__wasm_rquickjs_registered_loader_realm_counter || 0) + 1;
    const loader = { url, parent, data, realm, module: undefined, initialized: false, initializing: undefined };
    loaders.push(loader);
    globalThis.__wasm_rquickjs_registered_loader_generation =
        (globalThis.__wasm_rquickjs_registered_loader_generation || 0) + 1;
    globalThis.__wasm_rquickjs_static_registered_loader_cache = Object.create(null);
    globalThis.__wasm_rquickjs_sync_registered_loader_cache = Object.create(null);
    if (typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_start_registered_loader === 'function') {
        wasmRquickjsModuleGlobalThis.__wasm_rquickjs_start_registered_loader(loader);
    }
};

if (typeof globalThis.__wasm_rquickjs_run_registered_loaders !== 'function') {
    function normalizeLoaderResolvedUrl(url) {
        if (url.startsWith('/')) {
            const query = url.indexOf('?');
            const hash = url.indexOf('#');
            const end = query < 0 ? hash : (hash < 0 ? query : Math.min(query, hash));
            if (end >= 0) {
                url = nodeUrl.pathToFileURL(url.slice(0, end)).href + url.slice(end);
            } else {
                url = nodeUrl.pathToFileURL(url).href;
            }
        }
        return url;
    }

    function resolveRegisteredLoaderUrl(loader) {
        const url = loader.parent !== undefined
            ? normalizeLoaderResolvedUrl(wasmRquickjsModuleGlobalThis.__wasm_rquickjs_import_meta_resolve(loader.parent, loader.url))
            : loader.url;
        return loaderRealmUrl(url, loader.realm);
    }

    function loaderRealmUrl(url, realm) {
        if (!url.startsWith('file://')) return url;
        const hashIndex = url.indexOf('#');
        const beforeHash = hashIndex < 0 ? url : url.slice(0, hashIndex);
        const hash = hashIndex < 0 ? '' : url.slice(hashIndex);
        const separator = beforeHash.includes('?') ? '&' : '?';
        return beforeHash + separator + '__wasm_rquickjs_loader_realm=' + encodeURIComponent(String(realm)) + hash;
    }

    function startRegisteredLoader(loader) {
        if (loader.initialized || loader.initializing) return loader.initializing;
        loader.initializing = (async () => {
            const module = await import(resolveRegisteredLoaderUrl(loader));
            loader.module = module;
            if (typeof module.initialize === 'function') {
                await module.initialize(loader.data);
            }
            loader.initialized = true;
        })();
        loader.initializing.catch(() => {});
        return loader.initializing;
    }
    Object.defineProperty(globalThis, '__wasm_rquickjs_start_registered_loader', {
        value: startRegisteredLoader,
        writable: false,
        configurable: false,
    });

    function normalizeRegisteredLoaderResolvedResult(resolved) {
        if (!resolved || typeof resolved !== 'object' || resolved.url === undefined) return undefined;
        return {
            url: normalizeLoaderResolvedUrl(String(resolved.url)),
            format: loaderFormatOrUndefined(resolved.format),
        };
    }

    function validateRegisteredLoaderResolveResult(hookResult, context, loaderUrl) {
        const result = validateRegisteredLoaderResult(hookResult, 'resolve', context);
        validateRegisteredLoaderResolveUrl(result.url, loaderUrl);
        return result;
    }

    function registeredLoaderLoadContext(baseContext, resolved, resolvedFormat) {
        return {
            conditions: baseContext.conditions,
            importAttributes: resolved.importAttributes && typeof resolved.importAttributes === 'object'
                ? resolved.importAttributes
                : baseContext.importAttributes,
            format: resolvedFormat,
        };
    }

    function registeredLoaderBaseContext(conditions, importAttributes, parentURL) {
        return {
            conditions,
            importAttributes,
            parentURL: String(parentURL),
        };
    }

    function registeredLoaderResolvedState(baseContext, resolved) {
        const normalizedResolved = normalizeRegisteredLoaderResolvedResult(resolved);
        if (!normalizedResolved) return undefined;
        const resolvedFormat = normalizedResolved.format;
        return {
            normalizedResolved,
            resolvedFormat,
            loadContext: registeredLoaderLoadContext(baseContext, resolved, resolvedFormat),
        };
    }

    function registeredLoaderDefaultLoad(_nextUrl, context) {
        return { format: context && context.format };
    }

    function registeredLoaderFinalLoadFormat(loaded, fallbackFormat) {
        return loaded && loaded.format !== undefined && loaded.format !== null
            ? validateRegisteredLoaderLoadFormat(loaded.format)
            : validateRegisteredLoaderLoadFormat(fallbackFormat);
    }

    function validateRegisteredLoaderLoadResultFormat(result) {
        if (result.format !== undefined && result.format !== null && result.format !== '') {
            validateRegisteredLoaderLoadFormat(result.format);
        }
    }

    function validateRegisteredLoaderLoadResult(hookResult, context) {
        const result = validateRegisteredLoaderResult(hookResult, 'load', context);
        validateRegisteredLoaderLoadResultFormat(result);
        return result;
    }

    function registeredLoaderResolveResult(hookResult, context, loaderUrl, nextCalled, allowUndefinedFromNext) {
        if (allowUndefinedFromNext && hookResult === undefined) {
            if (!nextCalled()) throw makeLoaderChainError('resolve');
            return undefined;
        }
        const result = validateRegisteredLoaderResolveResult(hookResult, context, loaderUrl);
        assertRegisteredLoaderChainComplete('resolve', result, nextCalled());
        return result;
    }

    function registeredLoaderLoadResult(hookResult, context, nextCalled) {
        const result = validateRegisteredLoaderLoadResult(hookResult, context);
        assertRegisteredLoaderChainComplete('load', result, nextCalled());
        return result;
    }

    function registeredLoaderNextContext(context, contextForNext) {
        return contextForNext === undefined ? context : Object.assign({}, context, contextForNext);
    }

    function registeredLoaderNextSpecifier(currentSpecifier, specifierForNext) {
        return specifierForNext === undefined ? currentSpecifier : specifierForNext;
    }

    function registeredLoaderNextUrl(currentUrl, urlForNext) {
        return urlForNext === undefined ? currentUrl : String(urlForNext);
    }

    function registeredLoaderResolveInputs(nextSpecifier, context, fallbackParentURL) {
        return {
            specifier: String(nextSpecifier),
            parentURL: context && context.parentURL ? String(context.parentURL) : fallbackParentURL,
        };
    }

    function registeredLoaderHookEntry(loader) {
        return loader.module ? { module: loader.module, url: loader.url } : undefined;
    }

    function assertRegisteredLoaderChainComplete(hookName, result, nextCalled) {
        if (!nextCalled && (!result || result.shortCircuit !== true)) {
            throw makeLoaderChainError(hookName);
        }
    }

    function normalizedRegisteredLoaderResult(resolvedState, resolved, loaded) {
        if (!resolvedState) return undefined;
        const result = {
            url: resolvedState.normalizedResolved.url,
            format: registeredLoaderFinalLoadFormat(loaded, resolvedState.resolvedFormat),
            shortCircuit: !!(
                (resolved && resolved.shortCircuit === true) ||
                (loaded && loaded.shortCircuit === true)
            ),
        };
        if (registeredLoaderHasSource(loaded)) result.source = loaded.source;
        return Object.freeze(result);
    }

    function normalizedRegisteredLoaderCacheResult(result, value, error) {
        const cached = {
            url: result && result.url,
            format: result && result.format,
            shortCircuit: !!(result && result.shortCircuit),
            value,
        };
        if (registeredLoaderHasSource(result)) cached.source = result.source;
        if (error !== undefined) cached.error = error;
        return Object.freeze(cached);
    }

    function registeredLoaderFileSourceFallback(url) {
        url = String(url);
        return url.startsWith('file://') ? loaderFileUrlSource(url) : undefined;
    }

    function registeredLoaderModuleSourceReturn(source, url) {
        return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_loader_source_url(
            'data:text/javascript,' + encodeURIComponent(loaderSourceToString(source)),
            String(url),
        );
    }

    function registeredLoaderJsonSourceReturn(source, url) {
        const registered = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_loader_source_url(
            'data:application/json,' + encodeURIComponent(loaderSourceToString(source)),
            String(url),
        );
        return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_import_attr_rewrite(
            registered,
            'json',
        );
    }

    function registeredLoaderCommonJsReturn(loaded, url, missingSourceReturn) {
        if (registeredLoaderHasSource(loaded)) {
            return loaderCommonJsSourceModule(loaded.source, url);
        }
        return String(url).startsWith('file://')
            ? registeredLoaderPathOrUrlReturn(url, true)
            : missingSourceReturn;
    }

    function resolveEsmDefaultForLoader(specifier, parentURL, context, baseUrl, missingAsUndefined, allowRootedWithoutFileParent) {
        if (specifier.startsWith('data:')) {
            return registeredLoaderUrlResult(specifier);
        }
        const builtin = registeredLoaderBuiltinResolve(specifier, false);
        if (builtin) return builtin;
        if (specifier.startsWith('file://')) {
            return resultForEsmFileUrl(new URL(specifier));
        }
        const parentFilename = parentFilenameForLoaderResolve(parentURL, baseUrl);
        if (specifier.startsWith('/')) {
            if (parentFilename === null && !allowRootedWithoutFileParent) {
                if (missingAsUndefined) return undefined;
                throw makeEsmModuleNotFoundError(specifier);
            }
            return resultForEsmFileUrl(new URL(normalizeLoaderResolvedUrl(specifier)));
        }

        if (parentFilename !== null && rustClassifiesPathSpecifier(specifier)) {
            return resultForRelativeOrAbsoluteSpecifier(specifier, parentURL);
        }

        if (parentFilename !== null) {
            const packageResolved = resolveEsmPackageDefaultForLoader(specifier, parentURL, context);
            if (packageResolved) return packageResolved;
            if (missingAsUndefined) return undefined;
            throw makeEsmModuleNotFoundError(specifier);
        }
        if (missingAsUndefined) return undefined;

        let url = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_import_meta_resolve(parentURL, specifier);
        return registeredLoaderUrlResult(normalizeLoaderResolvedUrl(url));
    }

    async function runRegisteredLoaders(baseUrl, specifier, attrs, mode) {
        const loaders = globalThis.__wasm_rquickjs_registered_loaders;
        if (!loaders || loaders.length === 0) return undefined;

        const entries = [];
        for (let i = 0; i < loaders.length; i++) {
            const loader = loaders[i];
            try {
                await wasmRquickjsModuleGlobalThis.__wasm_rquickjs_start_registered_loader(loader);
            } catch (e) {
                loader.initializing = undefined;
                throw e;
            }
            const entry = registeredLoaderHookEntry(loader);
            if (entry) entries.push(entry);
        }

        const importAttributes = attrs && attrs.typeValue !== undefined
            ? { type: attrs.typeValue }
            : {};

        const baseContext = registeredLoaderBaseContext(loaderHookConditions(), importAttributes, baseUrl);

        const defaultResolve = async (nextSpecifier, context) => {
            const inputs = registeredLoaderResolveInputs(nextSpecifier, context, String(baseUrl));
            return resolveEsmDefaultForLoader(inputs.specifier, inputs.parentURL, context, baseUrl, false, true);
        };

        const runResolve = async (index, nextSpecifier, context) => {
            if (index < 0) return defaultResolve(nextSpecifier, context);
            const entry = entries[index];
            const module = entry.module;
            if (typeof module.resolve === 'function') {
                let nextCalled = false;
                const nextResolve = async (specifierForNext, contextForNext) => {
                    nextCalled = true;
                    return runResolve(
                        index - 1,
                        registeredLoaderNextSpecifier(nextSpecifier, specifierForNext),
                        registeredLoaderNextContext(context, contextForNext),
                    );
                };
                return registeredLoaderResolveResult(await module.resolve(nextSpecifier, context, nextResolve), context, entry.url, () => nextCalled, false);
            }
            return runResolve(index - 1, nextSpecifier, context);
        };

        const resolved = await runResolve(entries.length - 1, specifier, baseContext);
        const resolvedState = registeredLoaderResolvedState(baseContext, resolved);
        if (!resolvedState) return undefined;
        const normalizedResolved = resolvedState.normalizedResolved;

        const runLoad = async (index, nextUrl, context) => {
            if (index < 0) return registeredLoaderDefaultLoad(nextUrl, context);
            const module = entries[index].module;
            if (typeof module.load === 'function') {
                let nextCalled = false;
                const nextLoad = async (urlForNext, contextForNext) => {
                    nextCalled = true;
                    return runLoad(
                        index - 1,
                        registeredLoaderNextUrl(nextUrl, urlForNext),
                        registeredLoaderNextContext(context, contextForNext),
                    );
                };
                return registeredLoaderLoadResult(await module.load(nextUrl, context, nextLoad), context, () => nextCalled);
            }
            return runLoad(index - 1, nextUrl, context);
        };

        const loaded = await runLoad(entries.length - 1, normalizedResolved.url, resolvedState.loadContext);
        const result = normalizedRegisteredLoaderResult(resolvedState, resolved, loaded);
        const loadedHasSource = registeredLoaderHasSource(result);
        const loadedFormat = result.format;
        if (mode === 'static-raw') {
            return result;
        }

        if (loadedHasSource && loadedFormat === 'module') {
            return registeredLoaderModuleSourceReturn(result.source, normalizedResolved.url);
        }
        if (!loadedHasSource && loadedFormat === 'module') {
            if (String(normalizedResolved.url).startsWith('file://')) {
                try {
                    if (nodeUrl.fileURLToPath(normalizedResolved.url).endsWith('.mjs')) return normalizedResolved.url;
                } catch (_) {}
            }
            const fileSource = registeredLoaderFileSourceFallback(normalizedResolved.url);
            if (fileSource !== null && fileSource !== undefined) {
                return registeredLoaderModuleSourceReturn(fileSource, normalizedResolved.url);
            }
        }
        if (loadedFormat === 'commonjs') {
            return registeredLoaderCommonJsReturn(result, normalizedResolved.url, undefined);
        }
        if (loadedHasSource && loadedFormat === 'json') {
            return registeredLoaderJsonSourceReturn(result.source, normalizedResolved.url);
        }
        if (resolvedState.loadContext.importAttributes && resolvedState.loadContext.importAttributes.type === 'json') {
            return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_import_attr_rewrite(normalizedResolved.url, 'json');
        }
        return undefined;
    }
    Object.defineProperty(globalThis, '__wasm_rquickjs_run_registered_loaders', {
        value: runRegisteredLoaders,
        writable: false,
        configurable: false,
    });

    function isLoaderThenable(value) {
        return value && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
    }

    function assertSyncLoaderResult(value, hookName, operation) {
        if (isLoaderThenable(value)) {
            const err = new Error('Async registered loader ' + hookName + ' hooks are not supported from ' + (operation || 'CommonJS require()'));
            err.code = 'ERR_REQUIRE_ASYNC_MODULE';
            throw err;
        }
        return value;
    }

    function runRegisteredLoadersSync(baseUrl, specifier, resolveOnly, mode) {
        const loaders = globalThis.__wasm_rquickjs_registered_loaders;
        if (!loaders || loaders.length === 0) return undefined;
        const isImportMode = mode === 'import';
        const useLoadCache = !resolveOnly && !isImportMode;
        const loadCacheKey = useLoadCache
            ? String(globalThis.__wasm_rquickjs_registered_loader_generation || 0) +
                '\0' + String(baseUrl || fileUrlForPath('/')) + '\0' + String(specifier)
            : undefined;
        const loadCache = useLoadCache
            ? (globalThis.__wasm_rquickjs_sync_registered_loader_cache ||
                (globalThis.__wasm_rquickjs_sync_registered_loader_cache = Object.create(null)))
            : undefined;
        if (loadCache && Object.prototype.hasOwnProperty.call(loadCache, loadCacheKey)) {
            return loadCache[loadCacheKey];
        }
        const entries = [];
        for (let i = 0; i < loaders.length; i++) {
            const loader = loaders[i];
            if (!loader.initialized) {
                if (isImportMode) {
                    continue;
                }
                if (loader.initializing) {
                    const err = new Error('Registered loader initialization has not completed');
                    err.code = 'ERR_REQUIRE_ASYNC_MODULE';
                    throw err;
                }
                continue;
            }
            const entry = registeredLoaderHookEntry(loader);
            if (entry) entries.push(entry);
        }
        if (entries.length === 0) return undefined;

        const baseContext = registeredLoaderBaseContext(
            isImportMode ? loaderHookConditions() : cjsPackageConditions(),
            {},
            baseUrl || fileUrlForPath('/'),
        );

        const defaultResolve = (nextSpecifier, context) => {
            const inputs = registeredLoaderResolveInputs(nextSpecifier, context, baseContext.parentURL);
            const builtin = registeredLoaderBuiltinResolve(inputs.specifier, !isImportMode);
            if (builtin) return builtin;
            if (isImportMode) {
                return resolveEsmDefaultForLoader(inputs.specifier, inputs.parentURL, context, baseContext.parentURL, true, false);
            }
            return resolveCjsDefaultForLoader(inputs.specifier, inputs.parentURL, context);
        };

        const runResolve = (index, nextSpecifier, context) => {
            if (index < 0) return defaultResolve(nextSpecifier, context);
            const entry = entries[index];
            const module = entry.module;
            if (typeof module.resolve === 'function') {
                let nextCalled = false;
                const nextResolve = (specifierForNext, contextForNext) => {
                    nextCalled = true;
                    return runResolve(
                        index - 1,
                        registeredLoaderNextSpecifier(nextSpecifier, specifierForNext),
                        registeredLoaderNextContext(context, contextForNext),
                    );
                };
                const hookResult = assertSyncLoaderResult(module.resolve(nextSpecifier, context, nextResolve), 'resolve', isImportMode ? 'static ES module resolution' : undefined);
                return registeredLoaderResolveResult(hookResult, context, entry.url, () => nextCalled, true);
            }
            return runResolve(index - 1, nextSpecifier, context);
        };

        const initialSpecifier = isImportMode && typeof specifier === 'string'
            ? normalizeLoaderResolvedUrl(specifier)
            : specifier;
        const resolved = runResolve(entries.length - 1, initialSpecifier, baseContext);
        const resolvedState = registeredLoaderResolvedState(baseContext, resolved);
        if (!resolvedState) return undefined;
        const normalizedResolved = resolvedState.normalizedResolved;
        const resolvedFormat = resolvedState.resolvedFormat;
        if (resolveOnly) return registeredLoaderUrlFormatResult(normalizedResolved.url, resolvedFormat);

        const runLoad = (index, nextUrl, context) => {
            if (index < 0) return registeredLoaderDefaultLoad(nextUrl, context);
            const module = entries[index].module;
            if (typeof module.load === 'function') {
                let nextCalled = false;
                const nextLoad = (urlForNext, contextForNext) => {
                    nextCalled = true;
                    return runLoad(
                        index - 1,
                        registeredLoaderNextUrl(nextUrl, urlForNext),
                        registeredLoaderNextContext(context, contextForNext),
                    );
                };
                return registeredLoaderLoadResult(
                    assertSyncLoaderResult(module.load(nextUrl, context, nextLoad), 'load', isImportMode ? 'static ES module resolution' : undefined),
                    context,
                    () => nextCalled,
                );
            }
            return runLoad(index - 1, nextUrl, context);
        };

        const loaded = runLoad(entries.length - 1, normalizedResolved.url, resolvedState.loadContext);
        const result = normalizedRegisteredLoaderResult(resolvedState, resolved, loaded);
        if (loadCache) loadCache[loadCacheKey] = result;
        return result;
    }
    Object.defineProperty(globalThis, '__wasm_rquickjs_run_registered_loaders_sync', {
        value: runRegisteredLoadersSync,
        writable: false,
        configurable: false,
    });

    function staticRegisteredLoaderCacheParts(specifier, attrs) {
        let value = String(specifier);
        let typeValue = attrs && attrs.typeValue !== undefined ? String(attrs.typeValue) : '';
        if (typeValue === '') {
            let match = /^data:([^,]*);__wasm_rquickjs_import_type=([^;,]+)(,.*)$/.exec(value);
            if (match) {
                value = 'data:' + match[1] + match[3];
                typeValue = match[2].split('-')[0];
            } else {
                match = /([?#&])__wasm_rquickjs_import_type=([^&#]+)(&?)/.exec(value);
                if (match) {
                    const tokenStart = match.index;
                    const tokenEnd = tokenStart + match[0].length;
                    const prefix = value.slice(0, tokenStart);
                    const suffix = value.slice(tokenEnd);
                    const separator = match[1];
                    if (separator === '&') {
                        value = prefix + (suffix ? '&' + suffix : '');
                    } else if (match[3] === '&') {
                        value = prefix + separator + suffix;
                    } else {
                        value = prefix + suffix;
                    }
                    if (value.endsWith('?') || value.endsWith('#')) value = value.slice(0, -1);
                    typeValue = match[2].split('-')[0];
                }
            }
        }
        return { specifier: value, typeValue };
    }

    function staticRegisteredLoaderCacheKey(baseUrl, specifier, attrs) {
        const parts = staticRegisteredLoaderCacheParts(specifier, attrs);
        const generation = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_registered_loader_generation || 0;
        const importAttributes = parts.typeValue === ''
            ? '{}'
            : JSON.stringify({ type: parts.typeValue });
        return String(generation) + '\0' + String(baseUrl) + '\0' + parts.specifier + '\0' + importAttributes + '\0';
    }

    function staticRegisteredLoaderPathReturn(url) {
        return registeredLoaderPathOrUrlReturn(url, true);
    }

    function staticRegisteredLoaderReturn(loaded) {
        if (!loaded || !loaded.url) return undefined;
        const url = String(loaded.url);
        const format = loaderFormatOrUndefined(loaded.format);
        const hasSource = registeredLoaderHasSource(loaded);
        if (hasSource && (format === undefined || format === 'module')) {
            return registeredLoaderModuleSourceReturn(loaded.source, url);
        }
        if (!hasSource && format === 'module') {
            return staticRegisteredLoaderPathReturn(url);
        }
        if (format === 'commonjs') {
            return registeredLoaderCommonJsReturn(loaded, url, staticRegisteredLoaderPathReturn(url));
        }
        if (hasSource && format === 'json') {
            return registeredLoaderJsonSourceReturn(loaded.source, url);
        }
        return staticRegisteredLoaderPathReturn(url);
    }

    function staticRegisteredLoaderReturnForEdge(loaded, attrs) {
        if (
            attrs &&
            attrs.typeValue === 'json' &&
            loaded &&
            loaded.format === 'json' &&
            !registeredLoaderHasOwnSource(loaded) &&
            loaded.url &&
            typeof wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_import_attr_rewrite === 'function'
        ) {
            const url = String(loaded.url);
            const target = staticRegisteredLoaderPathReturn(url);
            return wasmRquickjsModuleGlobalThis.__wasm_rquickjs_register_import_attr_rewrite(target, 'json');
        }
        return staticRegisteredLoaderReturn(loaded);
    }

    function staticRegisteredLoaderSourceForUrl(url) {
        url = String(url);
        if (url.startsWith('file://')) {
            return loaderFileUrlSource(url);
        }
        if (url.startsWith('/')) {
            return tryReadFile(url);
        }
        if (url.startsWith('data:')) {
            const comma = url.indexOf(',');
            if (comma < 0) return null;
            const meta = url.slice(5, comma).toLowerCase();
            if (meta.indexOf('text/javascript') < 0 && meta.indexOf('application/javascript') < 0) {
                return null;
            }
            const body = url.slice(comma + 1);
            try {
                return meta.indexOf(';base64') >= 0 ? atob(body) : decodeURIComponent(body);
            } catch (_) {
                return null;
            }
        }
        return null;
    }

    function staticRegisteredLoaderChildUrl(loaded, fallback) {
        fallback = String(fallback);
        if (fallback.startsWith('data:')) return fallback;
        if (loaded && loaded.url) return String(loaded.url);
        return normalizeLoaderResolvedUrl(fallback);
    }

    function staticRegisteredLoaderParentAliases(parentUrl) {
        const aliases = [parentUrl];
        const virtualPrefix = 'file:///__wasm_rquickjs_virtual__/';
        if (parentUrl.startsWith(virtualPrefix) && parentUrl.endsWith('.mjs')) {
            aliases.push('file:///' + parentUrl.slice(virtualPrefix.length, -4));
        }
        return aliases;
    }

    function staticRegisteredLoaderCacheEntry(parentUrl, specifier, attrs, edgeReturn) {
        const key = staticRegisteredLoaderCacheKey(parentUrl, specifier, attrs);
        const cache = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_static_registered_loader_cache;
        if (Object.prototype.hasOwnProperty.call(cache, key)) {
            return { cached: cache[key], created: false };
        }
        return (async () => {
            try {
                const loaded = await wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders(parentUrl, specifier, attrs, 'static-raw');
                const value = edgeReturn
                    ? staticRegisteredLoaderReturnForEdge(loaded, attrs)
                    : staticRegisteredLoaderReturn(loaded);
                cache[key] = normalizedRegisteredLoaderCacheResult(loaded, value, undefined);
            } catch (error) {
                cache[key] = normalizedRegisteredLoaderCacheResult(undefined, undefined, error);
            }
            return { cached: cache[key], created: true };
        })();
    }

    async function prepareStaticRegisteredLoaderGraph(parentUrl, seen) {
        parentUrl = normalizeLoaderResolvedUrl(String(parentUrl));
        seen = seen || Object.create(null);
        if (seen[parentUrl]) return;
        seen[parentUrl] = true;

        const source = staticRegisteredLoaderSourceForUrl(parentUrl);
        if (source === null) return;
        const edges = rustModuleSourceAnalysis(source).staticEdges;
        for (let i = 0; i < edges.length; i++) {
            const specifier = edges[i].specifier;
            const attrs = edges[i].attrs;
            let cacheEntry = staticRegisteredLoaderCacheEntry(parentUrl, specifier, attrs, true);
            if (isLoaderThenable(cacheEntry)) cacheEntry = await cacheEntry;
            const { cached } = cacheEntry;
            if (cached && cached.error) continue;
            if (cached && !cached.error && cached.value !== undefined) {
                await prepareStaticRegisteredLoaderGraph(
                    staticRegisteredLoaderChildUrl(cached, cached.value),
                    seen,
                );
            }
        }
    }

    async function prepareStaticRegisteredLoaderEntry(entryUrl, entrySpecifier, entryParentUrl, entryAttrs) {
        if (!wasmRquickjsModuleGlobalThis.__wasm_rquickjs_static_registered_loader_cache) {
            wasmRquickjsModuleGlobalThis.__wasm_rquickjs_static_registered_loader_cache = Object.create(null);
        }
        if (entrySpecifier !== undefined && entryParentUrl !== undefined) {
            const parentUrl = normalizeLoaderResolvedUrl(String(entryParentUrl));
            const specifier = String(entrySpecifier);
            let cacheEntry = staticRegisteredLoaderCacheEntry(parentUrl, specifier, entryAttrs, false);
            if (isLoaderThenable(cacheEntry)) cacheEntry = await cacheEntry;
            const { cached, created } = cacheEntry;
            if (created && cached && cached.error) return;
            const aliases = staticRegisteredLoaderParentAliases(parentUrl);
            for (let i = 1; i < aliases.length; i++) {
                wasmRquickjsModuleGlobalThis.__wasm_rquickjs_static_registered_loader_cache[
                    staticRegisteredLoaderCacheKey(aliases[i], specifier, entryAttrs)
                ] = cached;
            }
        }
        await prepareStaticRegisteredLoaderGraph(entryUrl, Object.create(null));
    }
    Object.defineProperty(globalThis, '__wasm_rquickjs_prepare_static_registered_loader_graph', {
        value: prepareStaticRegisteredLoaderEntry,
        writable: false,
        configurable: false,
    });

    function resolveStaticRegisteredLoader(baseUrl, specifier) {
        const cache = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_static_registered_loader_cache;
        const key = staticRegisteredLoaderCacheKey(baseUrl, specifier);
        if (cache && Object.prototype.hasOwnProperty.call(cache, key)) {
            const cached = cache[key];
            if (cached.error) throw cached.error;
            return cached.value;
        }
        const loaded = wasmRquickjsModuleGlobalThis.__wasm_rquickjs_run_registered_loaders_sync(baseUrl, specifier, false, 'import');
        return staticRegisteredLoaderReturn(loaded);
    }
    Object.defineProperty(globalThis, '__wasm_rquickjs_resolve_static_registered_loader', {
        value: resolveStaticRegisteredLoader,
        writable: false,
        configurable: false,
    });
}

// "node_modules" reversed as char codes: s-e-l-u-d-o-m-_-e-d-o-n
const nmChars = [115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110];
const nmLen = nmChars.length;

function _nodeModulePaths(from) {
    from = pathModule.resolve(from);

    if (from === '/') {
        return ['/node_modules'];
    }

    const paths = [];
    for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
        const code = from.charCodeAt(i);
        if (code === 47) { // '/'
            if (p !== nmLen) {
                paths.push(from.slice(0, last) + '/node_modules');
            }
            last = i;
            p = 0;
        } else if (p !== -1) {
            if (nmChars[p] === code) {
                ++p;
            } else {
                p = -1;
            }
        }
    }

    paths.push('/node_modules');

    return paths;
}

function _resolveLookupPaths(request, parent) {
    if (isBuiltinModule(request)) {
        return null;
    }

    // Check if request is a relative path (starts with ./ or ../)
    // On non-Windows, .\ is NOT a relative path separator
    let isRelative = false;
    if (request.length > 0 && request.charAt(0) === '.') {
        if (request.length === 1) {
            isRelative = true;
        } else {
            const second = request.charAt(1);
            if (second === '/' || second === '.') {
                isRelative = true;
            }
        }
    }

    if (!isRelative) {
        let paths;
        if (parent && parent.paths && parent.paths.length) {
            paths = parent.paths.concat(globalPaths);
        } else {
            paths = globalPaths.slice();
        }
        return paths.length > 0 ? paths : null;
    }

    // Relative path with no parent
    if (!parent || !parent.id || !parent.filename) {
        return ['.'];
    }

    return [pathModule.dirname(parent.filename)];
}

function setSourceMapsSupport(enabled, options) {
    if (typeof enabled !== 'boolean') {
        throw new ERR_INVALID_ARG_TYPE('enabled', 'boolean', enabled);
    }
    if (options === undefined) {
        options = {};
    }
    if (options === null || typeof options !== 'object' || Array.isArray(options)) {
        throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
    }
    const { nodeModules, generatedCode } = options;
    if (nodeModules !== undefined && typeof nodeModules !== 'boolean') {
        throw new ERR_INVALID_ARG_TYPE('options.nodeModules', 'boolean', nodeModules);
    }
    if (generatedCode !== undefined && typeof generatedCode !== 'boolean') {
        throw new ERR_INVALID_ARG_TYPE('options.generatedCode', 'boolean', generatedCode);
    }
}

const globalPaths = [];

function _initPaths() {
    const nodePath = globalThis.process && globalThis.process.env && globalThis.process.env.NODE_PATH;
    const paths = [];
    if (nodePath) {
        const parts = nodePath.split(':');
        for (let i = 0; i < parts.length; i++) {
            const p = parts[i].trim();
            if (p.length > 0) {
                paths.push(pathModule.resolve(p));
            }
        }
    }

    const homeDir = (globalThis.process && globalThis.process.env && globalThis.process.env.HOME) || '/root';
    paths.push(pathModule.resolve(homeDir, '.node_modules'));
    paths.push(pathModule.resolve(homeDir, '.node_libraries'));
    paths.push('/usr/local/lib/node');

    globalPaths.length = 0;
    for (let j = 0; j < paths.length; j++) {
        globalPaths.push(paths[j]);
    }
}

_initPaths();

function _stat(filename) {
    try {
        const st = fsModule.statSync(filename);
        if (st.isDirectory()) return 1;
        if (st.isFile()) return 0;
        return -2;
    } catch (e) {
        return -2;
    }
}

function runMain() {
    const mainScript = process.argv[1];
    if (mainScript) {
        globalRequire(mainScript);
    }
}

export let syncBuiltinESMExports = function() {
    const registry = globalThis.__wasm_rquickjs_sync_builtin_esm_exports;
    if (!registry) return;
    if (typeof registry.fs === 'function') registry.fs();
    if (typeof registry.events === 'function') registry.events();
    require = moduleExports.require;
    createRequire = moduleExports.createRequire;
    findPackageJSON = moduleExports.findPackageJSON;
    builtinModules = moduleExports.builtinModules;
    isBuiltinModule = moduleExports.isBuiltin;
    register = moduleExports.register;
    syncBuiltinESMExports = moduleExports.syncBuiltinESMExports;
};

function Module(id, parent) {
    defineEnumerableWritable(this, 'id', id === undefined ? '' : id);
    defineEnumerableWritable(this, 'path', pathModule.dirname(this.id));
    defineEnumerableWritable(this, 'exports', {});
    defineEnumerableWritable(this, 'filename', null);
    defineEnumerableWritable(this, 'loaded', false);
    defineEnumerableWritable(this, 'children', []);
    defineEnumerableWritable(this, 'parent', parent || null);
    if (parent && parent.children) {
        Array.prototype.push.call(parent.children, this);
    }
    installCjsEsmDefaultSnapshotSlot(this);
}

const moduleRequireOverrides = new WeakMap();

Module.prototype.require = function require(id) {
    const override = moduleRequireOverrides.get(this);
    if (override) {
        return override(id);
    }
    const baseDir = this && typeof this.filename === 'string'
        ? pathModule.dirname(this.filename)
        : '.';
    return makeRequire(baseDir, this || null)(id);
};

Module.prototype._compile = function _compile(content, filename) {
    if (!(this instanceof Module)) {
        throw new ERR_INVALID_ARG_TYPE('mod', 'Module', this);
    }
    return compileModuleInto(this, content, arguments.length > 1 ? filename : this.filename);
};

function moduleLoad(request, parent, isMain) {
    void isMain;
    if (parent && typeof parent.filename === 'string') {
        return makeRequire(pathModule.dirname(parent.filename), parent)(request);
    }
    return makeRequire('.', parent || null)(request);
}

function moduleResolveFilename(request, parent, isMain, options) {
    void isMain;
    const baseDir = parent && typeof parent.filename === 'string'
        ? pathModule.dirname(parent.filename)
        : '.';
    const parentFilename = parent && typeof parent.filename === 'string'
        ? parent.filename
        : null;
    const parentLookupPaths = parent && Array.isArray(parent.paths)
        ? parent.paths.concat(globalPaths)
        : null;
    return resolveForRequire(request, options, baseDir, parentFilename, parentLookupPaths);
}

const moduleExports = Object.assign(Module, {
    require: globalRequire,
    createRequire,
    findPackageJSON,
    findSourceMap,
    SourceMap,
    builtinModules: builtinModuleNames,
    syncBuiltinESMExports,
    isBuiltin: isBuiltinModule,
    register,
    wrap: wrap,
    wrapper: wrapper,
    runMain: runMain,
    _nodeModulePaths: _nodeModulePaths,
    _resolveLookupPaths: _resolveLookupPaths,
    _resolveFilename: moduleResolveFilename,
    _load: moduleLoad,
    _initPaths: _initPaths,
    _pathCache: Object.create(null),
    _cache: moduleCache,
    _extensions: requireExtensions,
    _stat: _stat,
    globalPaths: globalPaths,
    setSourceMapsSupport,
    stripTypeScriptTypes,
});
moduleExportsInitialized = true;
Object.defineProperties(moduleExports, {
    __wasm_rquickjs_cjs_facade_has_own: {
        value: cjsFacadeHasOwnProperty,
        writable: false,
        configurable: false,
    },
    __wasm_rquickjs_dynamic_import_with_trace: {
        value: dynamicImportWithTrace,
        writable: false,
        configurable: false,
    },
    __wasm_rquickjs_load_cjs_esm_facade_default: {
        value: loadCjsEsmFacadeDefault,
        writable: false,
        configurable: false,
    },
    __wasm_rquickjs_load_commonjs_loader_source: {
        value: loadCommonJsLoaderSourceExports,
        writable: false,
        configurable: false,
    },
});
moduleExports.Module = Module;

// Add self-reference so require('module') works
builtinModuleMap['module'] = moduleExports;
builtinModuleMap['node:module'] = moduleExports;
publicBuiltinIdSet.add('module');
if (!builtinModuleNames.includes('module')) {
    builtinModuleNames.push('module');
}

export default moduleExports;