summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/loader/mozJSModuleLoader.cpp
blob: cdf4df1970b4a247f6add070b0f86587ee5768bb (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ScriptLoadRequest.h"
#include "mozilla/Assertions.h"  // MOZ_ASSERT, MOZ_ASSERT_IF
#include "mozilla/Attributes.h"
#include "mozilla/ArrayUtils.h"  // mozilla::ArrayLength
#include "mozilla/RefPtr.h"      // RefPtr, mozilla::StaticRefPtr
#include "mozilla/Utf8.h"        // mozilla::Utf8Unit

#include <cstdarg>

#include "mozilla/Logging.h"
#include "mozilla/dom/RequestBinding.h"
#ifdef ANDROID
#  include <android/log.h>
#endif
#ifdef XP_WIN
#  include <windows.h>
#endif

#include "jsapi.h"
#include "js/Array.h"  // JS::GetArrayLength, JS::IsArrayObject
#include "js/CharacterEncoding.h"
#include "js/CompilationAndEvaluation.h"
#include "js/CompileOptions.h"         // JS::CompileOptions
#include "js/ErrorReport.h"            // JS_ReportErrorUTF8, JSErrorReport
#include "js/Exception.h"              // JS_ErrorFromException
#include "js/friend/JSMEnvironment.h"  // JS::ExecuteInJSMEnvironment, JS::GetJSMEnvironmentOfScriptedCaller, JS::NewJSMEnvironment
#include "js/friend/ErrorMessages.h"   // JSMSG_*
#include "js/loader/ModuleLoadRequest.h"
#include "js/Object.h"  // JS::GetCompartment
#include "js/Printf.h"
#include "js/PropertyAndElement.h"  // JS_DefineFunctions, JS_DefineProperty, JS_Enumerate, JS_GetElement, JS_GetProperty, JS_GetPropertyById, JS_HasOwnProperty, JS_HasOwnPropertyById, JS_SetProperty, JS_SetPropertyById
#include "js/PropertySpec.h"
#include "js/SourceText.h"  // JS::SourceText
#include "nsCOMPtr.h"
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsIFile.h"
#include "mozJSModuleLoader.h"
#include "mozJSLoaderUtils.h"
#include "nsIFileURL.h"
#include "nsIJARURI.h"
#include "nsIChannel.h"
#include "nsIStreamListener.h"
#include "nsNetUtil.h"
#include "nsJSUtils.h"
#include "xpcprivate.h"
#include "xpcpublic.h"
#include "nsContentUtils.h"
#include "nsXULAppAPI.h"
#include "WrapperFactory.h"
#include "JSMEnvironmentProxy.h"
#include "ModuleEnvironmentProxy.h"
#include "JSServices.h"

#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/MacroForEach.h"
#include "mozilla/Preferences.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/ScriptPreloader.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Try.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/ReferrerPolicyBinding.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/WorkerCommon.h"  // dom::GetWorkerPrivateFromContext
#include "mozilla/dom/WorkerPrivate.h"  // dom::WorkerPrivate, dom::AutoSyncLoopHolder
#include "mozilla/dom/WorkerRunnable.h"  // dom::MainThreadStopSyncLoopRunnable
#include "mozilla/Unused.h"

using namespace mozilla;
using namespace mozilla::scache;
using namespace mozilla::loader;
using namespace xpc;
using namespace JS;

#define JS_CACHE_PREFIX(aScopeType, aCompilationTarget) \
  "jsloader/" aScopeType "/" aCompilationTarget

/**
 * Buffer sizes for serialization and deserialization of scripts.
 * FIXME: bug #411579 (tune this macro!) Last updated: Jan 2008
 */
#define XPC_SERIALIZATION_BUFFER_SIZE (64 * 1024)
#define XPC_DESERIALIZATION_BUFFER_SIZE (12 * 8192)

// MOZ_LOG=JSModuleLoader:5
static LazyLogModule gJSCLLog("JSModuleLoader");

#define LOG(args) MOZ_LOG(gJSCLLog, mozilla::LogLevel::Debug, args)

// Components.utils.import error messages
#define ERROR_SCOPE_OBJ "%s - Second argument must be an object."
#define ERROR_NO_TARGET_OBJECT "%s - Couldn't find target object for import."
#define ERROR_NOT_PRESENT "%s - EXPORTED_SYMBOLS is not present."
#define ERROR_NOT_AN_ARRAY "%s - EXPORTED_SYMBOLS is not an array."
#define ERROR_GETTING_ARRAY_LENGTH \
  "%s - Error getting array length of EXPORTED_SYMBOLS."
#define ERROR_ARRAY_ELEMENT "%s - EXPORTED_SYMBOLS[%d] is not a string."
#define ERROR_GETTING_SYMBOL "%s - Could not get symbol '%s'."
#define ERROR_SETTING_SYMBOL "%s - Could not set symbol '%s' on target object."
#define ERROR_UNINITIALIZED_SYMBOL \
  "%s - Symbol '%s' accessed before initialization. Cyclic import?"

static constexpr char JSM_Suffix[] = ".jsm";
static constexpr size_t JSM_SuffixLength = mozilla::ArrayLength(JSM_Suffix) - 1;
static constexpr char JSM_JS_Suffix[] = ".jsm.js";
static constexpr size_t JSM_JS_SuffixLength =
    mozilla::ArrayLength(JSM_JS_Suffix) - 1;
static constexpr char JS_Suffix[] = ".js";
static constexpr size_t JS_SuffixLength = mozilla::ArrayLength(JS_Suffix) - 1;
static constexpr char MJS_Suffix[] = ".sys.mjs";
static constexpr size_t MJS_SuffixLength = mozilla::ArrayLength(MJS_Suffix) - 1;

static bool IsJSM(const nsACString& aLocation) {
  if (aLocation.Length() < JSM_SuffixLength) {
    return false;
  }
  const auto ext = Substring(aLocation, aLocation.Length() - JSM_SuffixLength);
  return ext == JSM_Suffix;
}

static bool IsJS(const nsACString& aLocation) {
  if (aLocation.Length() < JS_SuffixLength) {
    return false;
  }
  const auto ext = Substring(aLocation, aLocation.Length() - JS_SuffixLength);
  return ext == JS_Suffix;
}

static bool IsJSM_JS(const nsACString& aLocation) {
  if (aLocation.Length() < JSM_JS_SuffixLength) {
    return false;
  }
  const auto ext =
      Substring(aLocation, aLocation.Length() - JSM_JS_SuffixLength);
  return ext == JSM_JS_Suffix;
}

static bool IsMJS(const nsACString& aLocation) {
  if (aLocation.Length() < MJS_SuffixLength) {
    return false;
  }
  const auto ext = Substring(aLocation, aLocation.Length() - MJS_SuffixLength);
  return ext == MJS_Suffix;
}

static void MJSToJSM(const nsACString& aLocation, nsAutoCString& aOut) {
  MOZ_ASSERT(IsMJS(aLocation));
  aOut = Substring(aLocation, 0, aLocation.Length() - MJS_SuffixLength);
  aOut += JSM_Suffix;
}

static bool TryToMJS(const nsACString& aLocation, nsAutoCString& aOut) {
  if (IsJSM(aLocation)) {
    aOut = Substring(aLocation, 0, aLocation.Length() - JSM_SuffixLength);
    aOut += MJS_Suffix;
    return true;
  }

  if (IsJSM_JS(aLocation)) {
    aOut = Substring(aLocation, 0, aLocation.Length() - JSM_JS_SuffixLength);
    aOut += MJS_Suffix;
    return true;
  }

  if (IsJS(aLocation)) {
    aOut = Substring(aLocation, 0, aLocation.Length() - JS_SuffixLength);
    aOut += MJS_Suffix;
    return true;
  }

  return false;
}

static bool Dump(JSContext* cx, unsigned argc, Value* vp) {
  if (!nsJSUtils::DumpEnabled()) {
    return true;
  }

  CallArgs args = CallArgsFromVp(argc, vp);

  if (args.length() == 0) {
    return true;
  }

  RootedString str(cx, JS::ToString(cx, args[0]));
  if (!str) {
    return false;
  }

  JS::UniqueChars utf8str = JS_EncodeStringToUTF8(cx, str);
  if (!utf8str) {
    return false;
  }

  MOZ_LOG(nsContentUtils::DOMDumpLog(), mozilla::LogLevel::Debug,
          ("[Backstage.Dump] %s", utf8str.get()));
#ifdef ANDROID
  __android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", utf8str.get());
#endif
#ifdef XP_WIN
  if (IsDebuggerPresent()) {
    nsAutoJSString wstr;
    if (!wstr.init(cx, str)) {
      return false;
    }
    OutputDebugStringW(wstr.get());
  }
#endif
  fputs(utf8str.get(), stdout);
  fflush(stdout);
  return true;
}

static bool Debug(JSContext* cx, unsigned argc, Value* vp) {
#ifdef DEBUG
  return Dump(cx, argc, vp);
#else
  return true;
#endif
}

static const JSFunctionSpec gGlobalFun[] = {
    JS_FN("dump", Dump, 1, 0), JS_FN("debug", Debug, 1, 0),
    JS_FN("atob", Atob, 1, 0), JS_FN("btoa", Btoa, 1, 0), JS_FS_END};

class MOZ_STACK_CLASS JSCLContextHelper {
 public:
  explicit JSCLContextHelper(JSContext* aCx);
  ~JSCLContextHelper();

  void reportErrorAfterPop(UniqueChars&& buf);

 private:
  JSContext* mContext;
  UniqueChars mBuf;

  // prevent copying and assignment
  JSCLContextHelper(const JSCLContextHelper&) = delete;
  const JSCLContextHelper& operator=(const JSCLContextHelper&) = delete;
};

static nsresult MOZ_FORMAT_PRINTF(2, 3)
    ReportOnCallerUTF8(JSContext* callerContext, const char* format, ...) {
  if (!callerContext) {
    return NS_ERROR_FAILURE;
  }

  va_list ap;
  va_start(ap, format);

  UniqueChars buf = JS_vsmprintf(format, ap);
  if (!buf) {
    va_end(ap);
    return NS_ERROR_OUT_OF_MEMORY;
  }

  JS_ReportErrorUTF8(callerContext, "%s", buf.get());

  va_end(ap);
  return NS_OK;
}

NS_IMPL_ISUPPORTS(mozJSModuleLoader, nsIMemoryReporter)

mozJSModuleLoader::mozJSModuleLoader()
    : mImports(16),
      mInProgressImports(16),
      mFallbackImports(16),
#ifdef STARTUP_RECORDER_ENABLED
      mImportStacks(16),
#endif
      mLocations(16),
      mInitialized(false),
      mLoaderGlobal(dom::RootingCx()),
      mServicesObj(dom::RootingCx()) {
}

#define ENSURE_DEP(name)          \
  {                               \
    nsresult rv = Ensure##name(); \
    NS_ENSURE_SUCCESS(rv, rv);    \
  }
#define ENSURE_DEPS(...) MOZ_FOR_EACH(ENSURE_DEP, (), (__VA_ARGS__));
#define BEGIN_ENSURE(self, ...) \
  {                             \
    if (m##self) return NS_OK;  \
    ENSURE_DEPS(__VA_ARGS__);   \
  }

class MOZ_STACK_CLASS ModuleLoaderInfo {
 public:
  explicit ModuleLoaderInfo(const nsACString& aLocation,
                            SkipCheckForBrokenURLOrZeroSized aSkipCheck =
                                SkipCheckForBrokenURLOrZeroSized::No)
      : mLocation(&aLocation), mIsModule(false), mSkipCheck(aSkipCheck) {}
  explicit ModuleLoaderInfo(JS::loader::ModuleLoadRequest* aRequest)
      : mLocation(nullptr),
        mURI(aRequest->mURI),
        mIsModule(true),
        mSkipCheck(aRequest->GetSyncLoadContext()->mSkipCheck) {}

  SkipCheckForBrokenURLOrZeroSized getSkipCheckForBrokenURLOrZeroSized() const {
    return mSkipCheck;
  }

  void resetChannelWithCheckForBrokenURLOrZeroSized() {
    MOZ_ASSERT(mSkipCheck == SkipCheckForBrokenURLOrZeroSized::Yes);
    mSkipCheck = SkipCheckForBrokenURLOrZeroSized::No;
    mScriptChannel = nullptr;
  }

  nsIIOService* IOService() {
    MOZ_ASSERT(mIOService);
    return mIOService;
  }
  nsresult EnsureIOService() {
    if (mIOService) {
      return NS_OK;
    }
    nsresult rv;
    mIOService = do_GetIOService(&rv);
    return rv;
  }

  nsIURI* URI() {
    MOZ_ASSERT(mURI);
    return mURI;
  }
  nsresult EnsureURI() {
    BEGIN_ENSURE(URI, IOService);
    MOZ_ASSERT(mLocation);
    return mIOService->NewURI(*mLocation, nullptr, nullptr,
                              getter_AddRefs(mURI));
  }

  nsIChannel* ScriptChannel() {
    MOZ_ASSERT(mScriptChannel);
    return mScriptChannel;
  }
  nsresult EnsureScriptChannel() {
    BEGIN_ENSURE(ScriptChannel, IOService, URI);

    // Skip check for missing URL when handling JSM-to-ESM fallback.
    return NS_NewChannel(
        getter_AddRefs(mScriptChannel), mURI,
        nsContentUtils::GetSystemPrincipal(),
        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
        nsIContentPolicy::TYPE_SCRIPT,
        /* aCookieJarSettings = */ nullptr,
        /* aPerformanceStorage = */ nullptr,
        /* aLoadGroup = */ nullptr, /* aCallbacks = */ nullptr,
        nsIRequest::LOAD_NORMAL, mIOService, /* aSandboxFlags = */ 0,
        mSkipCheck == SkipCheckForBrokenURLOrZeroSized::Yes);
  }

  nsIURI* ResolvedURI() {
    MOZ_ASSERT(mResolvedURI);
    return mResolvedURI;
  }
  nsresult EnsureResolvedURI() {
    BEGIN_ENSURE(ResolvedURI, URI);
    return ResolveURI(mURI, getter_AddRefs(mResolvedURI));
  }

  const nsACString& Key() {
    MOZ_ASSERT(mLocation);
    return *mLocation;
  }

  [[nodiscard]] nsresult GetLocation(nsCString& aLocation) {
    nsresult rv = EnsureURI();
    NS_ENSURE_SUCCESS(rv, rv);
    return mURI->GetSpec(aLocation);
  }

  bool IsModule() const { return mIsModule; }

 private:
  const nsACString* mLocation;
  nsCOMPtr<nsIIOService> mIOService;
  nsCOMPtr<nsIURI> mURI;
  nsCOMPtr<nsIChannel> mScriptChannel;
  nsCOMPtr<nsIURI> mResolvedURI;
  const bool mIsModule;
  SkipCheckForBrokenURLOrZeroSized mSkipCheck;
};

template <typename... Args>
static nsresult ReportOnCallerUTF8(JSCLContextHelper& helper,
                                   const char* format, ModuleLoaderInfo& info,
                                   Args... args) {
  nsCString location;
  MOZ_TRY(info.GetLocation(location));

  UniqueChars buf = JS_smprintf(format, location.get(), args...);
  if (!buf) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  helper.reportErrorAfterPop(std::move(buf));
  return NS_ERROR_FAILURE;
}

#undef BEGIN_ENSURE
#undef ENSURE_DEPS
#undef ENSURE_DEP

mozJSModuleLoader::~mozJSModuleLoader() {
  MOZ_ASSERT(!mInitialized,
             "UnloadModules() was not explicitly called before cleaning up "
             "mozJSModuleLoader");

  if (mInitialized) {
    UnloadModules();
  }
}

StaticRefPtr<mozJSModuleLoader> mozJSModuleLoader::sSelf;
StaticRefPtr<mozJSModuleLoader> mozJSModuleLoader::sDevToolsLoader;

void mozJSModuleLoader::FindTargetObject(JSContext* aCx,
                                         MutableHandleObject aTargetObject) {
  aTargetObject.set(JS::GetJSMEnvironmentOfScriptedCaller(aCx));

  // The above could fail if the scripted caller is not a JSM (it could be a DOM
  // scope, for instance).
  //
  // If the target object was not in the JSM shared global, return the global
  // instead. This is needed when calling the subscript loader within a frame
  // script, since it the FrameScript NSVO will have been found.
  if (!aTargetObject ||
      !IsLoaderGlobal(JS::GetNonCCWObjectGlobal(aTargetObject))) {
    aTargetObject.set(JS::GetScriptedCallerGlobal(aCx));

    // Return nullptr if the scripted caller is in a different compartment.
    if (JS::GetCompartment(aTargetObject) != js::GetContextCompartment(aCx)) {
      aTargetObject.set(nullptr);
    }
  }
}

void mozJSModuleLoader::InitStatics() {
  MOZ_ASSERT(!sSelf);
  sSelf = new mozJSModuleLoader();
  RegisterWeakMemoryReporter(sSelf);

  dom::AutoJSAPI jsapi;
  jsapi.Init();
  JSContext* cx = jsapi.cx();
  sSelf->InitSharedGlobal(cx);

  NonSharedGlobalSyncModuleLoaderScope::InitStatics();
}

void mozJSModuleLoader::UnloadLoaders() {
  if (sSelf) {
    sSelf->Unload();
  }
  if (sDevToolsLoader) {
    sDevToolsLoader->Unload();
  }
}

void mozJSModuleLoader::Unload() {
  UnloadModules();

  if (mModuleLoader) {
    mModuleLoader->Shutdown();
    mModuleLoader = nullptr;
  }
}

void mozJSModuleLoader::ShutdownLoaders() {
  MOZ_ASSERT(sSelf);
  UnregisterWeakMemoryReporter(sSelf);
  sSelf = nullptr;

  if (sDevToolsLoader) {
    UnregisterWeakMemoryReporter(sDevToolsLoader);
    sDevToolsLoader = nullptr;
  }
}

mozJSModuleLoader* mozJSModuleLoader::GetOrCreateDevToolsLoader(
    JSContext* aCx) {
  if (sDevToolsLoader) {
    return sDevToolsLoader;
  }
  sDevToolsLoader = new mozJSModuleLoader();
  RegisterWeakMemoryReporter(sDevToolsLoader);

  sDevToolsLoader->InitSharedGlobal(aCx);

  return sDevToolsLoader;
}

void mozJSModuleLoader::InitSyncModuleLoaderForGlobal(
    nsIGlobalObject* aGlobal) {
  MOZ_ASSERT(!mLoaderGlobal);
  MOZ_ASSERT(!mModuleLoader);

  RefPtr<SyncScriptLoader> scriptLoader = new SyncScriptLoader;
  mModuleLoader = new SyncModuleLoader(scriptLoader, aGlobal);
  mLoaderGlobal = aGlobal->GetGlobalJSObject();
}

void mozJSModuleLoader::DisconnectSyncModuleLoaderFromGlobal() {
  MOZ_ASSERT(mLoaderGlobal);
  MOZ_ASSERT(mModuleLoader);

  mLoaderGlobal = nullptr;
  Unload();
}

/* static */
bool mozJSModuleLoader::IsSharedSystemGlobal(nsIGlobalObject* aGlobal) {
  return sSelf->IsLoaderGlobal(aGlobal->GetGlobalJSObject());
}

/* static */
bool mozJSModuleLoader::IsDevToolsLoaderGlobal(nsIGlobalObject* aGlobal) {
  return sDevToolsLoader &&
         sDevToolsLoader->IsLoaderGlobal(aGlobal->GetGlobalJSObject());
}

// This requires that the keys be strings and the values be pointers.
template <class Key, class Data, class UserData, class Converter>
static size_t SizeOfTableExcludingThis(
    const nsBaseHashtable<Key, Data, UserData, Converter>& aTable,
    MallocSizeOf aMallocSizeOf) {
  size_t n = aTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
  for (const auto& entry : aTable) {
    n += entry.GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
    n += entry.GetData()->SizeOfIncludingThis(aMallocSizeOf);
  }
  return n;
}

#ifdef STARTUP_RECORDER_ENABLED
template <class Key, class Data, class UserData, class Converter>
static size_t SizeOfStringTableExcludingThis(
    const nsBaseHashtable<Key, Data, UserData, Converter>& aTable,
    MallocSizeOf aMallocSizeOf) {
  size_t n = aTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
  for (const auto& entry : aTable) {
    n += entry.GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
    n += entry.GetData().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  }
  return n;
}
#endif

size_t mozJSModuleLoader::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
  size_t n = aMallocSizeOf(this);
  n += SizeOfTableExcludingThis(mImports, aMallocSizeOf);
  n += mLocations.ShallowSizeOfExcludingThis(aMallocSizeOf);
  n += SizeOfTableExcludingThis(mInProgressImports, aMallocSizeOf);
  n += SizeOfTableExcludingThis(mFallbackImports, aMallocSizeOf);
#ifdef STARTUP_RECORDER_ENABLED
  n += SizeOfStringTableExcludingThis(mImportStacks, aMallocSizeOf);
#endif
  return n;
}

// Memory report paths are split on '/', with each module displayed as a
// separate layer of a visual tree. Any slashes which are meant to belong to a
// particular path module, rather than be used to build a hierarchy, therefore
// need to be replaced with backslashes, which are displayed as slashes in the
// UI.
//
// If `aAnonymize` is true, this function also attempts to translate any file:
// URLs to replace the path of the GRE directory with a placeholder containing
// no private information, and strips all other file: URIs of everything upto
// their last `/`.
static nsAutoCString MangleURL(const char* aURL, bool aAnonymize) {
  nsAutoCString url(aURL);

  if (aAnonymize) {
    static nsCString greDirURI;
    if (greDirURI.IsEmpty()) {
      nsCOMPtr<nsIFile> file;
      Unused << NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(file));
      if (file) {
        nsCOMPtr<nsIURI> uri;
        NS_NewFileURI(getter_AddRefs(uri), file);
        if (uri) {
          uri->GetSpec(greDirURI);
          RunOnShutdown([&]() { greDirURI.Truncate(0); });
        }
      }
    }

    url.ReplaceSubstring(greDirURI, "<GREDir>/"_ns);

    if (FindInReadable("file:"_ns, url)) {
      if (StringBeginsWith(url, "jar:file:"_ns)) {
        int32_t idx = url.RFindChar('!');
        url = "jar:file://<anonymized>!"_ns + Substring(url, idx);
      } else {
        int32_t idx = url.RFindChar('/');
        url = "file://<anonymized>/"_ns + Substring(url, idx);
      }
    }
  }

  url.ReplaceChar('/', '\\');
  return url;
}

NS_IMETHODIMP
mozJSModuleLoader::CollectReports(nsIHandleReportCallback* aHandleReport,
                                  nsISupports* aData, bool aAnonymize) {
  for (const auto& entry : mImports.Values()) {
    nsAutoCString path("js-module-loader/modules/");
    path.Append(MangleURL(entry->location, aAnonymize));

    aHandleReport->Callback(""_ns, path, KIND_NONHEAP, UNITS_COUNT, 1,
                            "Loaded JS modules"_ns, aData);
  }

  return NS_OK;
}

void mozJSModuleLoader::CreateLoaderGlobal(JSContext* aCx,
                                           const nsACString& aLocation,
                                           MutableHandleObject aGlobal) {
  auto backstagePass = MakeRefPtr<BackstagePass>();
  RealmOptions options;
  auto& creationOptions = options.creationOptions();

  creationOptions.setFreezeBuiltins(true).setNewCompartmentInSystemZone();
  if (IsDevToolsLoader()) {
    creationOptions.setInvisibleToDebugger(true);
  }
  xpc::SetPrefableRealmOptions(options);

  // Defer firing OnNewGlobalObject until after the __URI__ property has
  // been defined so the JS debugger can tell what module the global is
  // for
  RootedObject global(aCx);

#ifdef DEBUG
  // See mozJSModuleLoader::DefineJSServices.
  mIsInitializingLoaderGlobal = true;
#endif
  nsresult rv = xpc::InitClassesWithNewWrappedGlobal(
      aCx, static_cast<nsIGlobalObject*>(backstagePass),
      nsContentUtils::GetSystemPrincipal(), xpc::DONT_FIRE_ONNEWGLOBALHOOK,
      options, &global);
#ifdef DEBUG
  mIsInitializingLoaderGlobal = false;
#endif
  NS_ENSURE_SUCCESS_VOID(rv);

  NS_ENSURE_TRUE_VOID(global);

  backstagePass->SetGlobalObject(global);

  JSAutoRealm ar(aCx, global);
  if (!JS_DefineFunctions(aCx, global, gGlobalFun)) {
    return;
  }

  if (!CreateJSServices(aCx)) {
    return;
  }

  if (!DefineJSServices(aCx, global)) {
    return;
  }

  // Set the location information for the new global, so that tools like
  // about:memory may use that information
  xpc::SetLocationForGlobal(global, aLocation);

  MOZ_ASSERT(!mModuleLoader);
  RefPtr<SyncScriptLoader> scriptLoader = new SyncScriptLoader;
  mModuleLoader = new SyncModuleLoader(scriptLoader, backstagePass);
  backstagePass->InitModuleLoader(mModuleLoader);

  aGlobal.set(global);
}

void mozJSModuleLoader::InitSharedGlobal(JSContext* aCx) {
  JS::RootedObject globalObj(aCx);

  CreateLoaderGlobal(
      aCx, IsDevToolsLoader() ? "DevTools global"_ns : "shared JSM global"_ns,
      &globalObj);

  // If we fail to create a module global this early, we're not going to
  // get very far, so just bail out now.
  MOZ_RELEASE_ASSERT(globalObj);
  mLoaderGlobal = globalObj;

  // AutoEntryScript required to invoke debugger hook, which is a
  // Gecko-specific concept at present.
  dom::AutoEntryScript aes(globalObj, "module loader report global");
  JS_FireOnNewGlobalObject(aes.cx(), globalObj);
}

// Read script file on the main thread and pass it back to worker.
class ScriptReaderRunnable final : public nsIRunnable,
                                   public nsINamed,
                                   public nsIStreamListener {
 public:
  ScriptReaderRunnable(dom::WorkerPrivate* aWorkerPrivate,
                       nsIEventTarget* aSyncLoopTarget,
                       const nsCString& aLocation)
      : mLocation(aLocation),
        mRv(NS_ERROR_FAILURE),
        mWorkerPrivate(aWorkerPrivate),
        mSyncLoopTarget(aSyncLoopTarget) {}

  NS_DECL_THREADSAFE_ISUPPORTS

  nsCString& Data() { return mData; }

  nsresult Result() const { return mRv; }

  // nsIRunnable

  NS_IMETHOD
  Run() override {
    MOZ_ASSERT(NS_IsMainThread());

    nsresult rv = StartReadScriptFromLocation();
    if (NS_FAILED(rv)) {
      OnComplete(rv);
    }

    return NS_OK;
  }

  // nsINamed

  NS_IMETHOD
  GetName(nsACString& aName) override {
    aName.AssignLiteral("ScriptReaderRunnable");
    return NS_OK;
  }

  // nsIStreamListener

  NS_IMETHOD OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
                             uint64_t aOffset, uint32_t aCount) override {
    uint32_t read = 0;
    return aInputStream->ReadSegments(AppendSegmentToData, this, aCount, &read);
  }

  // nsIRequestObserver

  NS_IMETHOD OnStartRequest(nsIRequest* aRequest) override { return NS_OK; }

  NS_IMETHOD OnStopRequest(nsIRequest* aRequest,
                           nsresult aStatusCode) override {
    OnComplete(aStatusCode);
    return NS_OK;
  }

 private:
  ~ScriptReaderRunnable() = default;

  static nsresult AppendSegmentToData(nsIInputStream* aInputStream,
                                      void* aClosure, const char* aRawSegment,
                                      uint32_t aToOffset, uint32_t aCount,
                                      uint32_t* outWrittenCount) {
    auto* self = static_cast<ScriptReaderRunnable*>(aClosure);
    self->mData.Append(aRawSegment, aCount);
    *outWrittenCount = aCount;
    return NS_OK;
  }

  void OnComplete(nsresult aRv) {
    MOZ_ASSERT(NS_IsMainThread());

    mRv = aRv;

    RefPtr<dom::MainThreadStopSyncLoopRunnable> runnable =
        new dom::MainThreadStopSyncLoopRunnable(
            mWorkerPrivate, std::move(mSyncLoopTarget), mRv);
    MOZ_ALWAYS_TRUE(runnable->Dispatch());

    mWorkerPrivate = nullptr;
    mSyncLoopTarget = nullptr;
  }

  nsresult StartReadScriptFromLocation() {
    MOZ_ASSERT(NS_IsMainThread());

    ModuleLoaderInfo info(mLocation);
    nsresult rv = info.EnsureScriptChannel();
    NS_ENSURE_SUCCESS(rv, rv);

    return info.ScriptChannel()->AsyncOpen(this);
  }

 private:
  nsAutoCString mLocation;
  nsCString mData;
  nsresult mRv;

  // This pointer is guaranteed to be alive until OnComplete, given
  // the worker thread is synchronously waiting with AutoSyncLoopHolder::Run
  // until the corresponding WorkerPrivate::StopSyncLoop is called by
  // MainThreadStopSyncLoopRunnable, which is dispatched from OnComplete.
  dom::WorkerPrivate* mWorkerPrivate;

  nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
};

NS_IMPL_ISUPPORTS(ScriptReaderRunnable, nsIRunnable, nsINamed,
                  nsIStreamListener)

/* static */
nsresult mozJSModuleLoader::ReadScriptOnMainThread(JSContext* aCx,
                                                   const nsCString& aLocation,
                                                   nsCString& aData) {
  dom::WorkerPrivate* workerPrivate = dom::GetWorkerPrivateFromContext(aCx);
  MOZ_ASSERT(workerPrivate);

  dom::AutoSyncLoopHolder syncLoop(workerPrivate, dom::WorkerStatus::Canceling);
  nsCOMPtr<nsISerialEventTarget> syncLoopTarget =
      syncLoop.GetSerialEventTarget();
  if (!syncLoopTarget) {
    return NS_ERROR_DOM_INVALID_STATE_ERR;
  }

  RefPtr<ScriptReaderRunnable> runnable =
      new ScriptReaderRunnable(workerPrivate, syncLoopTarget, aLocation);

  if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
    return NS_ERROR_FAILURE;
  }

  syncLoop.Run();

  if (NS_FAILED(runnable->Result())) {
    return runnable->Result();
  }

  aData = std::move(runnable->Data());

  return NS_OK;
}

/* static */
nsresult mozJSModuleLoader::LoadSingleModuleScriptOnWorker(
    SyncModuleLoader* aModuleLoader, JSContext* aCx,
    JS::loader::ModuleLoadRequest* aRequest, MutableHandleScript aScriptOut) {
  nsAutoCString location;
  nsresult rv = aRequest->mURI->GetSpec(location);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCString data;
  rv = ReadScriptOnMainThread(aCx, location, data);
  NS_ENSURE_SUCCESS(rv, rv);

  CompileOptions options(aCx);
  ScriptPreloader::FillCompileOptionsForCachedStencil(options);
  options.setFileAndLine(location.BeginReading(), 1);
  SetModuleOptions(options);

  JS::SourceText<mozilla::Utf8Unit> srcBuf;
  if (!srcBuf.init(aCx, data.get(), data.Length(),
                   JS::SourceOwnership::Borrowed)) {
    return NS_ERROR_FAILURE;
  }

  RefPtr<JS::Stencil> stencil =
      CompileStencil(aCx, options, srcBuf, /* aIsModule = */ true);
  if (!stencil) {
    return NS_ERROR_FAILURE;
  }

  aScriptOut.set(InstantiateStencil(aCx, stencil, /* aIsModule = */ true));

  return NS_OK;
}

/* static */
nsresult mozJSModuleLoader::LoadSingleModuleScript(
    SyncModuleLoader* aModuleLoader, JSContext* aCx,
    JS::loader::ModuleLoadRequest* aRequest, MutableHandleScript aScriptOut) {
  AUTO_PROFILER_MARKER_TEXT(
      "ChromeUtils.importESModule static import", JS,
      MarkerOptions(MarkerStack::Capture(),
                    MarkerInnerWindowIdFromJSContext(aCx)),
      nsContentUtils::TruncatedURLForDisplay(aRequest->mURI));

  if (!NS_IsMainThread()) {
    return LoadSingleModuleScriptOnWorker(aModuleLoader, aCx, aRequest,
                                          aScriptOut);
  }

  ModuleLoaderInfo info(aRequest);
  nsresult rv = info.EnsureResolvedURI();
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIFile> sourceFile;
  rv = GetSourceFile(info.ResolvedURI(), getter_AddRefs(sourceFile));
  NS_ENSURE_SUCCESS(rv, rv);

  bool realFile = LocationIsRealFile(aRequest->mURI);

  RootedScript script(aCx);
  rv = GetScriptForLocation(aCx, info, sourceFile, realFile, aScriptOut);
  NS_ENSURE_SUCCESS(rv, rv);

#ifdef STARTUP_RECORDER_ENABLED
  if (aModuleLoader == sSelf->mModuleLoader) {
    sSelf->RecordImportStack(aCx, aRequest);
  } else if (sDevToolsLoader &&
             aModuleLoader == sDevToolsLoader->mModuleLoader) {
    sDevToolsLoader->RecordImportStack(aCx, aRequest);
  } else {
    // NOTE: Do not record import stack for non-shared globals, given the
    //       loader is associated with the global only while importing.
  }
#endif

  return NS_OK;
}

/* static */
nsresult mozJSModuleLoader::GetSourceFile(nsIURI* aResolvedURI,
                                          nsIFile** aSourceFileOut) {
  // Get the JAR if there is one.
  nsCOMPtr<nsIJARURI> jarURI;
  nsresult rv = NS_OK;
  jarURI = do_QueryInterface(aResolvedURI, &rv);
  nsCOMPtr<nsIFileURL> baseFileURL;
  if (NS_SUCCEEDED(rv)) {
    nsCOMPtr<nsIURI> baseURI;
    while (jarURI) {
      jarURI->GetJARFile(getter_AddRefs(baseURI));
      jarURI = do_QueryInterface(baseURI, &rv);
    }
    baseFileURL = do_QueryInterface(baseURI, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
  } else {
    baseFileURL = do_QueryInterface(aResolvedURI, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return baseFileURL->GetFile(aSourceFileOut);
}

/* static */
bool mozJSModuleLoader::LocationIsRealFile(nsIURI* aURI) {
  // We need to be extra careful checking for URIs pointing to files.
  // EnsureFile may not always get called, especially on resource URIs so we
  // need to call GetFile to make sure this is a valid file.
  nsresult rv = NS_OK;
  nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
  nsCOMPtr<nsIFile> testFile;
  if (NS_SUCCEEDED(rv)) {
    fileURL->GetFile(getter_AddRefs(testFile));
  }

  return bool(testFile);
}

JSObject* mozJSModuleLoader::PrepareObjectForLocation(JSContext* aCx,
                                                      nsIFile* aModuleFile,
                                                      nsIURI* aURI,
                                                      bool aRealFile) {
  RootedObject globalObj(aCx, GetSharedGlobal());
  MOZ_ASSERT(globalObj);
  JSAutoRealm ar(aCx, globalObj);

  // |thisObj| is the object we set properties on for a particular .jsm.
  RootedObject thisObj(aCx, JS::NewJSMEnvironment(aCx));
  NS_ENSURE_TRUE(thisObj, nullptr);

  if (aRealFile) {
    if (XRE_IsParentProcess()) {
      RootedObject locationObj(aCx);

      nsresult rv = nsXPConnect::XPConnect()->WrapNative(
          aCx, thisObj, aModuleFile, NS_GET_IID(nsIFile),
          locationObj.address());
      NS_ENSURE_SUCCESS(rv, nullptr);
      NS_ENSURE_TRUE(locationObj, nullptr);

      if (!JS_DefineProperty(aCx, thisObj, "__LOCATION__", locationObj, 0)) {
        return nullptr;
      }
    }
  }

  // Expose the URI from which the script was imported through a special
  // variable that we insert into the JSM.
  nsAutoCString nativePath;
  NS_ENSURE_SUCCESS(aURI->GetSpec(nativePath), nullptr);

  RootedString exposedUri(
      aCx, JS_NewStringCopyN(aCx, nativePath.get(), nativePath.Length()));
  NS_ENSURE_TRUE(exposedUri, nullptr);

  if (!JS_DefineProperty(aCx, thisObj, "__URI__", exposedUri, 0)) {
    return nullptr;
  }

  return thisObj;
}

static mozilla::Result<nsCString, nsresult> ReadScript(
    ModuleLoaderInfo& aInfo) {
  MOZ_TRY(aInfo.EnsureScriptChannel());

  nsCOMPtr<nsIInputStream> scriptStream;
  MOZ_TRY(aInfo.ScriptChannel()->Open(getter_AddRefs(scriptStream)));

  uint64_t len64;
  uint32_t bytesRead;

  MOZ_TRY(scriptStream->Available(&len64));
  NS_ENSURE_TRUE(len64 < UINT32_MAX, Err(NS_ERROR_FILE_TOO_BIG));
  NS_ENSURE_TRUE(len64, Err(NS_ERROR_FAILURE));
  uint32_t len = (uint32_t)len64;

  /* malloc an internal buf the size of the file */
  nsCString str;
  if (!str.SetLength(len, fallible)) {
    return Err(NS_ERROR_OUT_OF_MEMORY);
  }

  /* read the file in one swoop */
  MOZ_TRY(scriptStream->Read(str.BeginWriting(), len, &bytesRead));
  if (bytesRead != len) {
    return Err(NS_BASE_STREAM_OSERROR);
  }

  return std::move(str);
}

nsresult mozJSModuleLoader::ObjectForLocation(
    ModuleLoaderInfo& aInfo, nsIFile* aModuleFile, MutableHandleObject aObject,
    MutableHandleScript aTableScript, char** aLocation,
    bool aPropagateExceptions, MutableHandleValue aException) {
  MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");

  dom::AutoJSAPI jsapi;
  jsapi.Init();
  JSContext* cx = jsapi.cx();

  nsresult rv = aInfo.EnsureURI();
  NS_ENSURE_SUCCESS(rv, rv);

  bool realFile = LocationIsRealFile(aInfo.URI());

  RootedObject obj(
      cx, PrepareObjectForLocation(cx, aModuleFile, aInfo.URI(), realFile));
  NS_ENSURE_TRUE(obj, NS_ERROR_FAILURE);
  MOZ_ASSERT(!JS_IsGlobalObject(obj));

  JSAutoRealm ar(cx, obj);

  RootedScript script(cx);
  rv = GetScriptForLocation(cx, aInfo, aModuleFile, realFile, &script,
                            aLocation);
  if (NS_FAILED(rv)) {
    // Propagate the exception, if one exists. Also, don't leave the stale
    // exception on this context.
    if (aPropagateExceptions && jsapi.HasException()) {
      if (!jsapi.StealException(aException)) {
        return NS_ERROR_OUT_OF_MEMORY;
      }
    }

    return rv;
  }

  // Assign aObject here so that it's available to recursive imports.
  // See bug 384168.
  aObject.set(obj);

  aTableScript.set(script);

  {  // Scope for AutoEntryScript
    AutoAllowLegacyScriptExecution exemption;

    // We're going to run script via JS_ExecuteScript, so we need an
    // AutoEntryScript. This is Gecko-specific and not in any spec.
    dom::AutoEntryScript aes(CurrentGlobalOrNull(cx),
                             "module loader load module");
    JSContext* aescx = aes.cx();

    bool executeOk = false;
    if (JS_IsGlobalObject(obj)) {
      JS::RootedValue rval(cx);
      executeOk = JS_ExecuteScript(aescx, script, &rval);
    } else {
      executeOk = JS::ExecuteInJSMEnvironment(aescx, script, obj);
    }

    if (!executeOk) {
      if (aPropagateExceptions && aes.HasException()) {
        // Ignore return value because we're returning an error code
        // anyway.
        Unused << aes.StealException(aException);
      }
      aObject.set(nullptr);
      aTableScript.set(nullptr);
      return NS_ERROR_FAILURE;
    }
  }

  return rv;
}

/* static */
void mozJSModuleLoader::SetModuleOptions(CompileOptions& aOptions) {
  aOptions.setModule();

  // Top level await is not supported in synchronously loaded modules.
  aOptions.topLevelAwait = false;

  // Make all top-level `vars` available in `ModuleEnvironmentObject`.
  aOptions.deoptimizeModuleGlobalVars = true;
}

/* static */
nsresult mozJSModuleLoader::GetScriptForLocation(
    JSContext* aCx, ModuleLoaderInfo& aInfo, nsIFile* aModuleFile,
    bool aUseMemMap, MutableHandleScript aScriptOut, char** aLocationOut) {
  // JS compilation errors are returned via an exception on the context.
  MOZ_ASSERT(!JS_IsExceptionPending(aCx));

  aScriptOut.set(nullptr);

  nsAutoCString nativePath;
  nsresult rv = aInfo.URI()->GetSpec(nativePath);
  NS_ENSURE_SUCCESS(rv, rv);

  // Before compiling the script, first check to see if we have it in
  // the preloader cache or the startupcache.  Note: as a rule, preloader cache
  // errors and startupcache errors are not fatal to loading the script, since
  // we can always slow-load.

  bool storeIntoStartupCache = false;
  StartupCache* cache = StartupCache::GetSingleton();

  aInfo.EnsureResolvedURI();

  nsAutoCString cachePath;
  if (aInfo.IsModule()) {
    rv = PathifyURI(JS_CACHE_PREFIX("non-syntactic", "module"),
                    aInfo.ResolvedURI(), cachePath);
  } else {
    rv = PathifyURI(JS_CACHE_PREFIX("non-syntactic", "script"),
                    aInfo.ResolvedURI(), cachePath);
  }
  NS_ENSURE_SUCCESS(rv, rv);

  JS::DecodeOptions decodeOptions;
  ScriptPreloader::FillDecodeOptionsForCachedStencil(decodeOptions);

  RefPtr<JS::Stencil> stencil =
      ScriptPreloader::GetSingleton().GetCachedStencil(aCx, decodeOptions,
                                                       cachePath);

  if (!stencil && cache) {
    ReadCachedStencil(cache, cachePath, aCx, decodeOptions,
                      getter_AddRefs(stencil));
    if (!stencil) {
      JS_ClearPendingException(aCx);

      storeIntoStartupCache = true;
    }
  }

  if (stencil) {
    LOG(("Successfully loaded %s from cache\n", nativePath.get()));
  } else {
    // The script wasn't in the cache , so compile it now.
    LOG(("Slow loading %s\n", nativePath.get()));

    CompileOptions options(aCx);
    ScriptPreloader::FillCompileOptionsForCachedStencil(options);
    options.setFileAndLine(nativePath.get(), 1);
    if (aInfo.IsModule()) {
      SetModuleOptions(options);
    } else {
      options.setForceStrictMode();
      options.setNonSyntacticScope(true);
    }

    // If we can no longer write to caches, we should stop using lazy sources
    // and instead let normal syntax parsing occur. This can occur in content
    // processes after the ScriptPreloader is flushed where we can read but no
    // longer write.
    if (!storeIntoStartupCache && !ScriptPreloader::GetSingleton().Active()) {
      options.setSourceIsLazy(false);
    }

    if (aUseMemMap) {
      AutoMemMap map;
      MOZ_TRY(map.init(aModuleFile));

      // Note: exceptions will get handled further down;
      // don't early return for them here.
      auto buf = map.get<char>();

      JS::SourceText<mozilla::Utf8Unit> srcBuf;
      if (srcBuf.init(aCx, buf.get(), map.size(),
                      JS::SourceOwnership::Borrowed)) {
        stencil = CompileStencil(aCx, options, srcBuf, aInfo.IsModule());
      }
    } else {
      nsCString str;
      MOZ_TRY_VAR(str, ReadScript(aInfo));

      JS::SourceText<mozilla::Utf8Unit> srcBuf;
      if (srcBuf.init(aCx, str.get(), str.Length(),
                      JS::SourceOwnership::Borrowed)) {
        stencil = CompileStencil(aCx, options, srcBuf, aInfo.IsModule());
      }
    }

#ifdef DEBUG
    // The above shouldn't touch any options for instantiation.
    JS::InstantiateOptions instantiateOptions(options);
    instantiateOptions.assertDefault();
#endif

    if (!stencil) {
      return NS_ERROR_FAILURE;
    }
  }

  aScriptOut.set(InstantiateStencil(aCx, stencil, aInfo.IsModule()));
  if (!aScriptOut) {
    return NS_ERROR_FAILURE;
  }

  // ScriptPreloader::NoteScript needs to be called unconditionally, to
  // reflect the usage into the next session's cache.
  ScriptPreloader::GetSingleton().NoteStencil(nativePath, cachePath, stencil);

  // Write to startup cache only when we didn't have any cache for the script
  // and compiled it.
  if (storeIntoStartupCache) {
    MOZ_ASSERT(stencil);

    // We successfully compiled the script, so cache it.
    rv = WriteCachedStencil(cache, cachePath, aCx, stencil);

    // Don't treat failure to write as fatal, since we might be working
    // with a read-only cache.
    if (NS_SUCCEEDED(rv)) {
      LOG(("Successfully wrote to cache\n"));
    } else {
      LOG(("Failed to write to cache\n"));
    }
  }

  /* Owned by ModuleEntry. Freed when we remove from the table. */
  if (aLocationOut) {
    *aLocationOut = ToNewCString(nativePath, mozilla::fallible);
    if (!*aLocationOut) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  return NS_OK;
}

void mozJSModuleLoader::UnloadModules() {
  MOZ_ASSERT(!mIsUnloaded);

  mInitialized = false;
  mIsUnloaded = true;

  if (mLoaderGlobal) {
    MOZ_ASSERT(JS_HasExtensibleLexicalEnvironment(mLoaderGlobal));
    JS::RootedObject lexicalEnv(dom::RootingCx(),
                                JS_ExtensibleLexicalEnvironment(mLoaderGlobal));
    JS_SetAllNonReservedSlotsToUndefined(lexicalEnv);
    JS_SetAllNonReservedSlotsToUndefined(mLoaderGlobal);
    mLoaderGlobal = nullptr;
  }
  mServicesObj = nullptr;

#ifdef STARTUP_RECORDER_ENABLED
  mImportStacks.Clear();
#endif
  mFallbackImports.Clear();
  mInProgressImports.Clear();
  mImports.Clear();
  mLocations.Clear();
}

/* static */
already_AddRefed<Stencil> mozJSModuleLoader::CompileStencil(
    JSContext* aCx, const JS::CompileOptions& aOptions,
    JS::SourceText<mozilla::Utf8Unit>& aSource, bool aIsModule) {
  if (aIsModule) {
    return CompileModuleScriptToStencil(aCx, aOptions, aSource);
  }

  return CompileGlobalScriptToStencil(aCx, aOptions, aSource);
}

/* static */
JSScript* mozJSModuleLoader::InstantiateStencil(JSContext* aCx,
                                                JS::Stencil* aStencil,
                                                bool aIsModule) {
  JS::InstantiateOptions instantiateOptions;

  if (aIsModule) {
    RootedObject module(aCx);
    module = JS::InstantiateModuleStencil(aCx, instantiateOptions, aStencil);
    if (!module) {
      return nullptr;
    }

    return JS::GetModuleScript(module);
  }

  return JS::InstantiateGlobalStencil(aCx, instantiateOptions, aStencil);
}

nsresult mozJSModuleLoader::ImportInto(const nsACString& registryLocation,
                                       HandleValue targetValArg, JSContext* cx,
                                       uint8_t optionalArgc,
                                       MutableHandleValue retval) {
  MOZ_ASSERT(nsContentUtils::IsCallerChrome());

  RootedValue targetVal(cx, targetValArg);
  RootedObject targetObject(cx, nullptr);

  if (optionalArgc) {
    // The caller passed in the optional second argument. Get it.
    if (targetVal.isObject()) {
      // If we're passing in something like a content DOM window, chances
      // are the caller expects the properties to end up on the object
      // proper and not on the Xray holder. This is dubious, but can be used
      // during testing. Given that dumb callers can already leak JSMs into
      // content by passing a raw content JS object (where Xrays aren't
      // possible), we aim for consistency here. Waive xray.
      if (WrapperFactory::IsXrayWrapper(&targetVal.toObject()) &&
          !WrapperFactory::WaiveXrayAndWrap(cx, &targetVal)) {
        return NS_ERROR_FAILURE;
      }
      targetObject = &targetVal.toObject();
    } else if (!targetVal.isNull()) {
      // If targetVal isNull(), we actually want to leave targetObject null.
      // Not doing so breaks |make package|.
      return ReportOnCallerUTF8(cx, ERROR_SCOPE_OBJ,
                                PromiseFlatCString(registryLocation).get());
    }
  } else {
    FindTargetObject(cx, &targetObject);
    if (!targetObject) {
      return ReportOnCallerUTF8(cx, ERROR_NO_TARGET_OBJECT,
                                PromiseFlatCString(registryLocation).get());
    }
  }

  js::AssertSameCompartment(cx, targetObject);

  RootedObject global(cx);
  nsresult rv = ImportInto(registryLocation, targetObject, cx, &global);

  if (global) {
    if (!JS_WrapObject(cx, &global)) {
      NS_ERROR("can't wrap return value");
      return NS_ERROR_FAILURE;
    }

    retval.setObject(*global);
  }
  return rv;
}

nsresult mozJSModuleLoader::IsModuleLoaded(const nsACString& aLocation,
                                           bool* retval) {
  MOZ_ASSERT(nsContentUtils::IsCallerChrome());

  if (mIsUnloaded) {
    *retval = false;
    return NS_OK;
  }

  mInitialized = true;
  ModuleLoaderInfo info(aLocation);
  if (mImports.Get(info.Key())) {
    *retval = true;
    return NS_OK;
  }

  if (mModuleLoader) {
    nsAutoCString mjsLocation;
    if (!TryToMJS(aLocation, mjsLocation)) {
      *retval = false;
      return NS_OK;
    }

    ModuleLoaderInfo mjsInfo(mjsLocation);

    nsresult rv = mjsInfo.EnsureURI();
    NS_ENSURE_SUCCESS(rv, rv);

    if (mModuleLoader->IsModuleFetched(mjsInfo.URI())) {
      *retval = true;
      return NS_OK;
    }
  }

  *retval = false;
  return NS_OK;
}

nsresult mozJSModuleLoader::IsJSModuleLoaded(const nsACString& aLocation,
                                             bool* retval) {
  MOZ_ASSERT(nsContentUtils::IsCallerChrome());

  if (mIsUnloaded) {
    *retval = false;
    return NS_OK;
  }

  mInitialized = true;
  ModuleLoaderInfo info(aLocation);
  if (mImports.Get(info.Key())) {
    *retval = true;
    return NS_OK;
  }

  *retval = false;
  return NS_OK;
}

nsresult mozJSModuleLoader::IsESModuleLoaded(const nsACString& aLocation,
                                             bool* retval) {
  MOZ_ASSERT(nsContentUtils::IsCallerChrome());

  if (mIsUnloaded) {
    *retval = false;
    return NS_OK;
  }

  mInitialized = true;
  ModuleLoaderInfo info(aLocation);

  nsresult rv = info.EnsureURI();
  NS_ENSURE_SUCCESS(rv, rv);

  if (mModuleLoader->IsModuleFetched(info.URI())) {
    *retval = true;
    return NS_OK;
  }

  *retval = false;
  return NS_OK;
}

void mozJSModuleLoader::GetLoadedModules(nsTArray<nsCString>& aLoadedModules) {
  aLoadedModules.SetCapacity(mImports.Count());
  for (const auto& data : mImports.Values()) {
    aLoadedModules.AppendElement(data->location);
  }
}

nsresult mozJSModuleLoader::GetLoadedESModules(
    nsTArray<nsCString>& aLoadedModules) {
  return mModuleLoader->GetFetchedModuleURLs(aLoadedModules);
}

nsresult mozJSModuleLoader::GetLoadedJSAndESModules(
    nsTArray<nsCString>& aLoadedModules) {
  GetLoadedModules(aLoadedModules);

  nsTArray<nsCString> modules;
  nsresult rv = GetLoadedESModules(modules);
  NS_ENSURE_SUCCESS(rv, rv);

  for (const auto& location : modules) {
    if (IsMJS(location)) {
      nsAutoCString jsmLocation;
      // NOTE: Unconditionally convert to *.jsm.  This doesn't cover *.js case
      //       but given `Cu.loadedModules` is rarely used for system modules,
      //       this won't cause much compat issue.
      MJSToJSM(location, jsmLocation);
      aLoadedModules.AppendElement(jsmLocation);
    }
  }

  return NS_OK;
}

#ifdef STARTUP_RECORDER_ENABLED
void mozJSModuleLoader::RecordImportStack(JSContext* aCx,
                                          const nsACString& aLocation) {
  if (!Preferences::GetBool("browser.startup.record", false)) {
    return;
  }

  mImportStacks.InsertOrUpdate(
      aLocation, xpc_PrintJSStack(aCx, false, false, false).get());
}

void mozJSModuleLoader::RecordImportStack(
    JSContext* aCx, JS::loader::ModuleLoadRequest* aRequest) {
  if (!Preferences::GetBool("browser.startup.record", false)) {
    return;
  }

  nsAutoCString location;
  nsresult rv = aRequest->mURI->GetSpec(location);
  if (NS_FAILED(rv)) {
    return;
  }

  auto recordJSStackOnly = [&]() {
    mImportStacks.InsertOrUpdate(
        location, xpc_PrintJSStack(aCx, false, false, false).get());
  };

  if (aRequest->IsTopLevel()) {
    recordJSStackOnly();
    return;
  }

  nsAutoCString importerSpec;
  rv = aRequest->mReferrer->GetSpec(importerSpec);
  if (NS_FAILED(rv)) {
    recordJSStackOnly();
    return;
  }

  ModuleLoaderInfo importerInfo(importerSpec);
  auto importerStack = mImportStacks.Lookup(importerInfo.Key());
  if (!importerStack) {
    // The importer's stack is not collected, possibly due to OOM.
    recordJSStackOnly();
    return;
  }

  nsAutoCString stack;

  stack += "* import [\"";
  stack += importerSpec;
  stack += "\"]\n";
  stack += *importerStack;

  mImportStacks.InsertOrUpdate(location, stack);
}
#endif

nsresult mozJSModuleLoader::GetModuleImportStack(const nsACString& aLocation,
                                                 nsACString& retval) {
#ifdef STARTUP_RECORDER_ENABLED
  MOZ_ASSERT(nsContentUtils::IsCallerChrome());

  // When querying the DevTools loader, it may not be initialized yet
  if (!mInitialized) {
    return NS_ERROR_FAILURE;
  }

  ModuleLoaderInfo info(aLocation);
  auto str = mImportStacks.Lookup(info.Key());
  if (!str) {
    return NS_ERROR_FAILURE;
  }

  retval = *str;
  return NS_OK;
#else
  return NS_ERROR_NOT_IMPLEMENTED;
#endif
}

nsresult mozJSModuleLoader::ImportInto(const nsACString& aLocation,
                                       HandleObject targetObj, JSContext* cx,
                                       MutableHandleObject vp) {
  vp.set(nullptr);

  JS::RootedObject exports(cx);
  MOZ_TRY(Import(cx, aLocation, vp, &exports, !targetObj));

  if (targetObj) {
    JS::Rooted<JS::IdVector> ids(cx, JS::IdVector(cx));
    if (!JS_Enumerate(cx, exports, &ids)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    JS::RootedValue value(cx);
    JS::RootedId id(cx);
    for (jsid idVal : ids) {
      id = idVal;
      if (!JS_GetPropertyById(cx, exports, id, &value) ||
          !JS_SetPropertyById(cx, targetObj, id, value)) {
        return NS_ERROR_FAILURE;
      }
    }
  }

  return NS_OK;
}

nsresult mozJSModuleLoader::ExtractExports(JSContext* aCx,
                                           ModuleLoaderInfo& aInfo,
                                           ModuleEntry* aMod,
                                           JS::MutableHandleObject aExports) {
  // cxhelper must be created before jsapi, so that jsapi is destroyed and
  // pops any context it has pushed before we report to the caller context.
  JSCLContextHelper cxhelper(aCx);

  // Even though we are calling JS_SetPropertyById on targetObj, we want
  // to ensure that we never run script here, so we use an AutoJSAPI and
  // not an AutoEntryScript.
  dom::AutoJSAPI jsapi;
  jsapi.Init();
  JSContext* cx = jsapi.cx();
  JSAutoRealm ar(cx, aMod->obj);

  RootedValue symbols(cx);
  {
    RootedObject obj(
        cx, ResolveModuleObjectProperty(cx, aMod->obj, "EXPORTED_SYMBOLS"));
    if (!obj || !JS_GetProperty(cx, obj, "EXPORTED_SYMBOLS", &symbols)) {
      return ReportOnCallerUTF8(cxhelper, ERROR_NOT_PRESENT, aInfo);
    }
  }

  bool isArray;
  if (!JS::IsArrayObject(cx, symbols, &isArray)) {
    return NS_ERROR_FAILURE;
  }
  if (!isArray) {
    return ReportOnCallerUTF8(cxhelper, ERROR_NOT_AN_ARRAY, aInfo);
  }

  RootedObject symbolsObj(cx, &symbols.toObject());

  // Iterate over symbols array, installing symbols on targetObj:

  uint32_t symbolCount = 0;
  if (!JS::GetArrayLength(cx, symbolsObj, &symbolCount)) {
    return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_ARRAY_LENGTH, aInfo);
  }

#ifdef DEBUG
  nsAutoCString logBuffer;
#endif

  aExports.set(JS_NewPlainObject(cx));
  if (!aExports) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  bool missing = false;

  RootedValue value(cx);
  RootedId symbolId(cx);
  RootedObject symbolHolder(cx);
  for (uint32_t i = 0; i < symbolCount; ++i) {
    if (!JS_GetElement(cx, symbolsObj, i, &value) || !value.isString() ||
        !JS_ValueToId(cx, value, &symbolId)) {
      return ReportOnCallerUTF8(cxhelper, ERROR_ARRAY_ELEMENT, aInfo, i);
    }

    symbolHolder = ResolveModuleObjectPropertyById(cx, aMod->obj, symbolId);
    if (!symbolHolder ||
        !JS_GetPropertyById(cx, symbolHolder, symbolId, &value)) {
      RootedString symbolStr(cx, symbolId.toString());
      JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, symbolStr);
      if (!bytes) {
        return NS_ERROR_FAILURE;
      }
      return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_SYMBOL, aInfo,
                                bytes.get());
    }

    // It's possible |value| is the uninitialized lexical MagicValue when
    // there's a cyclic import: const obj = ChromeUtils.import("parent.jsm").
    if (value.isMagic(JS_UNINITIALIZED_LEXICAL)) {
      RootedString symbolStr(cx, symbolId.toString());
      JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, symbolStr);
      if (!bytes) {
        return NS_ERROR_FAILURE;
      }
      return ReportOnCallerUTF8(cxhelper, ERROR_UNINITIALIZED_SYMBOL, aInfo,
                                bytes.get());
    }

    if (value.isUndefined()) {
      missing = true;
    }

    if (!JS_SetPropertyById(cx, aExports, symbolId, value)) {
      RootedString symbolStr(cx, symbolId.toString());
      JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, symbolStr);
      if (!bytes) {
        return NS_ERROR_FAILURE;
      }
      return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_SYMBOL, aInfo,
                                bytes.get());
    }
#ifdef DEBUG
    if (i == 0) {
      logBuffer.AssignLiteral("Installing symbols [ ");
    }
    JS::UniqueChars bytes = JS_EncodeStringToLatin1(cx, symbolId.toString());
    if (!!bytes) {
      logBuffer.Append(bytes.get());
    }
    logBuffer.Append(' ');
    if (i == symbolCount - 1) {
      nsCString location;
      MOZ_TRY(aInfo.GetLocation(location));
      LOG(("%s] from %s\n", logBuffer.get(), location.get()));
    }
#endif
  }

  // Don't cache the exports object if any of its exported symbols are
  // missing. If the module hasn't finished loading yet, they may be
  // defined the next time we try to import it.
  if (!missing) {
    aMod->exports = aExports;
  }
  return NS_OK;
}

/* static */
bool mozJSModuleLoader::IsTrustedScheme(nsIURI* aURI) {
  return aURI->SchemeIs("resource") || aURI->SchemeIs("chrome");
}

nsresult mozJSModuleLoader::Import(JSContext* aCx, const nsACString& aLocation,
                                   JS::MutableHandleObject aModuleGlobal,
                                   JS::MutableHandleObject aModuleExports,
                                   bool aIgnoreExports) {
  if (mIsUnloaded) {
    JS_ReportErrorASCII(aCx, "Module loaded is already unloaded");
    return NS_ERROR_FAILURE;
  }

  mInitialized = true;

  AUTO_PROFILER_MARKER_TEXT(
      "ChromeUtils.import", JS,
      MarkerOptions(MarkerStack::Capture(),
                    MarkerInnerWindowIdFromJSContext(aCx)),
      Substring(aLocation, 0, std::min(size_t(128), aLocation.Length())));

  // The JSM may already be ESM-ified, and in that case the load is expected
  // to fail.  Suppress the error message, the crash, and also the telemetry
  // event for the failure.
  //
  // If this load fails, it will be redirected to `.sys.mjs` URL
  // in TryFallbackToImportESModule, and if the redirect also fails,
  // the load is performed again below, with the check enabled.
  ModuleLoaderInfo info(aLocation, SkipCheckForBrokenURLOrZeroSized::Yes);

  nsresult rv;
  ModuleEntry* mod;
  UniquePtr<ModuleEntry> newEntry;
  if (!mImports.Get(info.Key(), &mod) &&
      !mInProgressImports.Get(info.Key(), &mod)) {
    // We're trying to import a new JSM, but we're late in shutdown and this
    // will likely not succeed and might even crash, so fail here.
    if (PastShutdownPhase(ShutdownPhase::XPCOMShutdownFinal)) {
      return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
    }

    // If we've hit file-not-found and fallback was successful,
    // return the cached data.
    bool aFound;
    rv = TryCachedFallbackToImportESModule(
        aCx, aLocation, aModuleGlobal, aModuleExports, aIgnoreExports, &aFound);
    NS_ENSURE_SUCCESS(rv, rv);
    if (aFound) {
      return NS_OK;
    }

    newEntry = MakeUnique<ModuleEntry>(RootingContext::get(aCx));

    // Note: This implies EnsureURI().
    MOZ_TRY(info.EnsureResolvedURI());

    // Reject imports from untrusted sources.
    if (!IsTrustedScheme(info.URI())) {
      return NS_ERROR_DOM_SECURITY_ERR;
    }

    nsCOMPtr<nsIFile> sourceFile;
    rv = GetSourceFile(info.ResolvedURI(), getter_AddRefs(sourceFile));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = info.ResolvedURI()->GetSpec(newEntry->resolvedURL);
    NS_ENSURE_SUCCESS(rv, rv);

    nsCString* existingPath;
    if (mLocations.Get(newEntry->resolvedURL, &existingPath) &&
        *existingPath != info.Key()) {
      return NS_ERROR_UNEXPECTED;
    }

    mLocations.InsertOrUpdate(newEntry->resolvedURL,
                              MakeUnique<nsCString>(info.Key()));

    RootedValue exception(aCx);
    {
      mInProgressImports.InsertOrUpdate(info.Key(), newEntry.get());
      auto cleanup =
          MakeScopeExit([&]() { mInProgressImports.Remove(info.Key()); });

      rv = ObjectForLocation(info, sourceFile, &newEntry->obj,
                             &newEntry->thisObjectKey, &newEntry->location,
                             true, &exception);
    }

    if (NS_FAILED(rv)) {
      mLocations.Remove(newEntry->resolvedURL);
      if (!exception.isUndefined()) {
        // An exception was thrown during compilation. Propagate it
        // out to our caller so they can report it.
        bool isModuleSyntaxError = false;

        if (exception.isObject()) {
          JS::Rooted<JSObject*> exceptionObj(aCx, &exception.toObject());
          JSAutoRealm ar(aCx, exceptionObj);
          JSErrorReport* report = JS_ErrorFromException(aCx, exceptionObj);
          if (report) {
            switch (report->errorNumber) {
              case JSMSG_IMPORT_DECL_AT_TOP_LEVEL:
              case JSMSG_EXPORT_DECL_AT_TOP_LEVEL:
                // If the exception is related to module syntax, it's most
                // likely because of misuse of API.
                // Provide better error message.
                isModuleSyntaxError = true;

                JS_ReportErrorUTF8(aCx,
                                   "ChromeUtils.import is called against "
                                   "an ES module script (%s).  Please use "
                                   "ChromeUtils.importESModule instead "
                                   "(SyntaxError: %s)",
                                   aLocation.BeginReading(),
                                   report->message().c_str());
                break;
              default:
                break;
            }
          }
        }

        if (!isModuleSyntaxError) {
          if (!JS_WrapValue(aCx, &exception)) {
            return NS_ERROR_OUT_OF_MEMORY;
          }
          JS_SetPendingException(aCx, exception);
        }

        return NS_ERROR_FAILURE;
      }

      if (rv == NS_ERROR_FILE_NOT_FOUND || rv == NS_ERROR_FILE_ACCESS_DENIED) {
        // NS_ERROR_FILE_ACCESS_DENIED happens if the access is blocked by
        // sandbox.
        rv = TryFallbackToImportESModule(aCx, aLocation, aModuleGlobal,
                                         aModuleExports, aIgnoreExports);

        if (rv == NS_ERROR_FILE_NOT_FOUND ||
            rv == NS_ERROR_FILE_ACCESS_DENIED) {
          // Both JSM and ESM are not found, with the check inside necko
          // skipped (See EnsureScriptChannel and mSkipCheck).
          //
          // Perform the load again with the check enabled, so that
          // logging, crash-on-autonation, and telemetry event happen.
          if (NS_SUCCEEDED(info.EnsureURI()) &&
              !LocationIsRealFile(info.URI())) {
            info.resetChannelWithCheckForBrokenURLOrZeroSized();
            (void)ReadScript(info);
          }
        }

        return rv;
      }

      // Something failed, but we don't know what it is, guess.
      return NS_ERROR_FILE_NOT_FOUND;
    }

#ifdef STARTUP_RECORDER_ENABLED
    RecordImportStack(aCx, aLocation);
#endif

    mod = newEntry.get();
  }

  MOZ_ASSERT(mod->obj, "Import table contains entry with no object");
  JS::RootedObject globalProxy(aCx);
  {
    JSAutoRealm ar(aCx, mod->obj);

    globalProxy = CreateJSMEnvironmentProxy(aCx, mod->obj);
    if (!globalProxy) {
      return NS_ERROR_FAILURE;
    }
  }
  if (!JS_WrapObject(aCx, &globalProxy)) {
    return NS_ERROR_FAILURE;
  }
  aModuleGlobal.set(globalProxy);

  JS::RootedObject exports(aCx, mod->exports);
  if (!exports && !aIgnoreExports) {
    MOZ_TRY(ExtractExports(aCx, info, mod, &exports));
  }

  if (exports && !JS_WrapObject(aCx, &exports)) {
    mLocations.Remove(newEntry->resolvedURL);
    return NS_ERROR_FAILURE;
  }
  aModuleExports.set(exports);

  // Cache this module for later
  if (newEntry) {
    mImports.InsertOrUpdate(info.Key(), std::move(newEntry));
  }

  return NS_OK;
}

nsresult mozJSModuleLoader::TryFallbackToImportESModule(
    JSContext* aCx, const nsACString& aLocation,
    JS::MutableHandleObject aModuleGlobal,
    JS::MutableHandleObject aModuleExports, bool aIgnoreExports) {
  nsAutoCString mjsLocation;
  if (!TryToMJS(aLocation, mjsLocation)) {
    return NS_ERROR_FILE_NOT_FOUND;
  }

  JS::RootedObject moduleNamespace(aCx);
  // The fallback can fail if the URL was not for ESMified JSM.  Suppress the
  // error message, the crash, and also the telemetry event for the failure.
  nsresult rv = ImportESModule(aCx, mjsLocation, &moduleNamespace,
                               SkipCheckForBrokenURLOrZeroSized::Yes);
  if (rv == NS_ERROR_FILE_NOT_FOUND || rv == NS_ERROR_FILE_ACCESS_DENIED) {
    // The error for ESModule shouldn't be exposed if the file does not exist,
    // or the access is blocked by sandbox.
    if (JS_IsExceptionPending(aCx)) {
      JS_ClearPendingException(aCx);
    }
  }
  NS_ENSURE_SUCCESS(rv, rv);

  JS::RootedObject globalProxy(aCx);
  {
    JSAutoRealm ar(aCx, moduleNamespace);

    JS::RootedObject moduleObject(
        aCx, JS::GetModuleForNamespace(aCx, moduleNamespace));
    if (!moduleObject) {
      return NS_ERROR_FAILURE;
    }

    globalProxy = CreateModuleEnvironmentProxy(aCx, moduleObject);
    if (!globalProxy) {
      return NS_ERROR_FAILURE;
    }

    // Cache the redirect to use in subsequent imports.
    ModuleLoaderInfo info(aLocation);
    auto newEntry = MakeUnique<FallbackModuleEntry>(RootingContext::get(aCx));
    newEntry->globalProxy = globalProxy;
    newEntry->moduleNamespace = moduleNamespace;
    mFallbackImports.InsertOrUpdate(info.Key(), std::move(newEntry));
  }

  if (!JS_WrapObject(aCx, &globalProxy)) {
    return NS_ERROR_FAILURE;
  }
  aModuleGlobal.set(globalProxy);

  if (!aIgnoreExports) {
    JS::RootedObject exports(aCx, moduleNamespace);
    if (!JS_WrapObject(aCx, &exports)) {
      return NS_ERROR_FAILURE;
    }
    aModuleExports.set(exports);
  }

  return NS_OK;
}

nsresult mozJSModuleLoader::TryCachedFallbackToImportESModule(
    JSContext* aCx, const nsACString& aLocation,
    JS::MutableHandleObject aModuleGlobal,
    JS::MutableHandleObject aModuleExports, bool aIgnoreExports, bool* aFound) {
  ModuleLoaderInfo info(aLocation);
  FallbackModuleEntry* fallbackMod;
  if (!mFallbackImports.Get(info.Key(), &fallbackMod)) {
    *aFound = false;
    return NS_OK;
  }

  JS::RootedObject globalProxy(aCx, fallbackMod->globalProxy);
  if (!JS_WrapObject(aCx, &globalProxy)) {
    return NS_ERROR_FAILURE;
  }
  aModuleGlobal.set(globalProxy);

  if (!aIgnoreExports) {
    JS::RootedObject exports(aCx, fallbackMod->moduleNamespace);
    if (!JS_WrapObject(aCx, &exports)) {
      return NS_ERROR_FAILURE;
    }
    aModuleExports.set(exports);
  }

  *aFound = true;
  return NS_OK;
}

nsresult mozJSModuleLoader::ImportESModule(
    JSContext* aCx, const nsACString& aLocation,
    JS::MutableHandleObject aModuleNamespace,
    SkipCheckForBrokenURLOrZeroSized
        aSkipCheck /* = SkipCheckForBrokenURLOrZeroSized::No */) {
  using namespace JS::loader;

  if (mIsUnloaded) {
    JS_ReportErrorASCII(aCx, "Module loaded is already unloaded");
    return NS_ERROR_FAILURE;
  }

  mInitialized = true;

  // Called from ChromeUtils::ImportESModule.
  nsCString str(aLocation);

  AUTO_PROFILER_MARKER_TEXT(
      "ChromeUtils.importESModule", JS,
      MarkerOptions(MarkerStack::Capture(),
                    MarkerInnerWindowIdFromJSContext(aCx)),
      Substring(aLocation, 0, std::min(size_t(128), aLocation.Length())));

  RootedObject globalObj(aCx, GetSharedGlobal());
  MOZ_ASSERT(globalObj);
  MOZ_ASSERT_IF(NS_IsMainThread(),
                xpc::Scriptability::Get(globalObj).Allowed());

  // The module loader should be instantiated when fetching the shared global
  MOZ_ASSERT(mModuleLoader);

  JSAutoRealm ar(aCx, globalObj);

  nsCOMPtr<nsIURI> uri;
  nsresult rv = NS_NewURI(getter_AddRefs(uri), aLocation);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIPrincipal> principal =
      mModuleLoader->GetGlobalObject()->PrincipalOrNull();
  MOZ_ASSERT(principal);

  RefPtr<ScriptFetchOptions> options = new ScriptFetchOptions(
      CORS_NONE, /* aNonce = */ u""_ns, dom::RequestPriority::Auto,
      ParserMetadata::NotParserInserted, principal);

  RefPtr<SyncLoadContext> context = new SyncLoadContext();
  context->mSkipCheck = aSkipCheck;

  RefPtr<VisitedURLSet> visitedSet =
      ModuleLoadRequest::NewVisitedSetForTopLevelImport(uri);

  RefPtr<ModuleLoadRequest> request = new ModuleLoadRequest(
      uri, dom::ReferrerPolicy::No_referrer, options, dom::SRIMetadata(),
      /* aReferrer = */ nullptr, context,
      /* aIsTopLevel = */ true,
      /* aIsDynamicImport = */ false, mModuleLoader, visitedSet, nullptr);

  request->NoCacheEntryFound();

  rv = request->StartModuleLoad();
  if (NS_FAILED(rv)) {
    mModuleLoader->MaybeReportLoadError(aCx);
    return rv;
  }

  rv = mModuleLoader->ProcessRequests();
  if (NS_FAILED(rv)) {
    mModuleLoader->MaybeReportLoadError(aCx);
    return rv;
  }

  MOZ_ASSERT(request->IsFinished());
  if (!request->mModuleScript) {
    mModuleLoader->MaybeReportLoadError(aCx);
    return NS_ERROR_FAILURE;
  }

  // All modules are loaded. MaybeReportLoadError isn't necessary from here.

  if (!request->InstantiateModuleGraph()) {
    return NS_ERROR_FAILURE;
  }

  rv = mModuleLoader->EvaluateModuleInContext(aCx, request,
                                              JS::ThrowModuleErrorsSync);
  NS_ENSURE_SUCCESS(rv, rv);
  if (JS_IsExceptionPending(aCx)) {
    return NS_ERROR_FAILURE;
  }

  RefPtr<ModuleScript> moduleScript = request->mModuleScript;
  JS::Rooted<JSObject*> module(aCx, moduleScript->ModuleRecord());
  aModuleNamespace.set(JS::GetModuleNamespace(aCx, module));

  return NS_OK;
}

nsresult mozJSModuleLoader::Unload(const nsACString& aLocation) {
  if (!mInitialized) {
    return NS_OK;
  }

  ModuleLoaderInfo info(aLocation);

  ModuleEntry* mod;
  if (mImports.Get(info.Key(), &mod)) {
    mLocations.Remove(mod->resolvedURL);
    mImports.Remove(info.Key());
  }

  // If this is the last module to be unloaded, we will leak mLoaderGlobal
  // until UnloadModules is called. So be it.

  return NS_OK;
}

bool mozJSModuleLoader::CreateJSServices(JSContext* aCx) {
  JSObject* services = NewJSServices(aCx);
  if (!services) {
    return false;
  }

  mServicesObj = services;
  return true;
}

bool mozJSModuleLoader::DefineJSServices(JSContext* aCx,
                                         JS::Handle<JSObject*> aGlobal) {
  if (!mServicesObj) {
    // This function is called whenever creating a new global that needs
    // `Services`, including the loader's shared global.
    //
    // This function is no-op if it's called during creating the loader's
    // shared global.
    //
    // See also  CreateAndDefineJSServices.
    MOZ_ASSERT(!mLoaderGlobal);
    MOZ_ASSERT(mIsInitializingLoaderGlobal);
    return true;
  }

  JS::Rooted<JS::Value> services(aCx, ObjectValue(*mServicesObj));
  if (!JS_WrapValue(aCx, &services)) {
    return false;
  }

  JS::Rooted<JS::PropertyKey> servicesId(
      aCx, XPCJSContext::Get()->GetStringID(XPCJSContext::IDX_SERVICES));
  return JS_DefinePropertyById(aCx, aGlobal, servicesId, services, 0);
}

size_t mozJSModuleLoader::ModuleEntry::SizeOfIncludingThis(
    MallocSizeOf aMallocSizeOf) const {
  size_t n = aMallocSizeOf(this);
  n += aMallocSizeOf(location);

  return n;
}

//----------------------------------------------------------------------

/* static */
MOZ_THREAD_LOCAL(mozJSModuleLoader*)
NonSharedGlobalSyncModuleLoaderScope::sTlsActiveLoader;

void NonSharedGlobalSyncModuleLoaderScope::InitStatics() {
  sTlsActiveLoader.infallibleInit();
}

NonSharedGlobalSyncModuleLoaderScope::NonSharedGlobalSyncModuleLoaderScope(
    JSContext* aCx, nsIGlobalObject* aGlobal) {
  MOZ_ASSERT_IF(NS_IsMainThread(),
                !mozJSModuleLoader::IsSharedSystemGlobal(aGlobal));
  MOZ_ASSERT_IF(NS_IsMainThread(),
                !mozJSModuleLoader::IsDevToolsLoaderGlobal(aGlobal));

  mAsyncModuleLoader = aGlobal->GetModuleLoader(aCx);
  MOZ_ASSERT(mAsyncModuleLoader,
             "The consumer should guarantee the global returns non-null module "
             "loader");

  mLoader = new mozJSModuleLoader();
  RegisterWeakMemoryReporter(mLoader);
  mLoader->InitSyncModuleLoaderForGlobal(aGlobal);

  mAsyncModuleLoader->CopyModulesTo(mLoader->mModuleLoader);

  mMaybeOverride.emplace(mAsyncModuleLoader, mLoader->mModuleLoader);

  MOZ_ASSERT(!sTlsActiveLoader.get());
  sTlsActiveLoader.set(mLoader);
}

NonSharedGlobalSyncModuleLoaderScope::~NonSharedGlobalSyncModuleLoaderScope() {
  MOZ_ASSERT(sTlsActiveLoader.get() == mLoader);
  sTlsActiveLoader.set(nullptr);

  mLoader->DisconnectSyncModuleLoaderFromGlobal();
  UnregisterWeakMemoryReporter(mLoader);
}

void NonSharedGlobalSyncModuleLoaderScope::Finish() {
  mLoader->mModuleLoader->MoveModulesTo(mAsyncModuleLoader);
}

/* static */
bool NonSharedGlobalSyncModuleLoaderScope::IsActive() {
  return !!sTlsActiveLoader.get();
}

/*static */
mozJSModuleLoader* NonSharedGlobalSyncModuleLoaderScope::ActiveLoader() {
  return sTlsActiveLoader.get();
}

//----------------------------------------------------------------------

JSCLContextHelper::JSCLContextHelper(JSContext* aCx)
    : mContext(aCx), mBuf(nullptr) {}

JSCLContextHelper::~JSCLContextHelper() {
  if (mBuf) {
    JS_ReportErrorUTF8(mContext, "%s", mBuf.get());
  }
}

void JSCLContextHelper::reportErrorAfterPop(UniqueChars&& buf) {
  MOZ_ASSERT(!mBuf, "Already called reportErrorAfterPop");
  mBuf = std::move(buf);
}