redjs 0.4.0

Full-control Rust embedding layer over Bun's JavaScriptCore prebuilt. C ABI designed for zero-alloc C# (NativeAOT) codegen bindings.
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
// redjs C++ glue: the parts of JSC that are only reachable through the
// *internal* C++ API (fast JSFunction host bindings, CallFrame/JSValue ABI
// export, direct typed-array/promise/heap access, Options).
//
// Everything here is `extern "C"` with the `rjs_` prefix; the Rust crate
// re-exports it (plus pure C-API calls) as the stable `redjs_` ABI.
//
// Compiled with clang-cl /std:c++23preview against the Bun WebKit prebuilt's
// internal headers; cmakeconfig.h is force-included by build.rs so this TU
// matches the prebuilt's ABI exactly (see build.rs).
//
// Threading: every entry takes the VM's API lock (recursive, cheap when
// already held). Host callbacks run with the lock already held by JSC, so
// nested rjs_* calls from inside a callback are fine.
//
// Value lifetime contract (important for the C# side): an EncodedJSValue held
// in native stack/registers is protected by JSC's conservative stack scan
// while this thread is inside the VM. Storing a value into heap memory (C#
// object fields, Rust collections) requires rjs_protect / a strong handle.

#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/APICast.h>
#include <JavaScriptCore/CallData.h>
#include <JavaScriptCore/DeferredWorkTimer.h>
#include <JavaScriptCore/Completion.h>
#include <JavaScriptCore/CachedBytecode.h>
#include <JavaScriptCore/CachedTypes.h>
#include <JavaScriptCore/CodeCache.h>
#include <JavaScriptCore/JSCBytecodeCacheVersion.h>
#include <JavaScriptCore/ParserError.h>
#include <JavaScriptCore/SourceCodeKey.h>
#include <JavaScriptCore/Strong.h>
#include <JavaScriptCore/UnlinkedProgramCodeBlock.h>
#include <JavaScriptCore/Error.h>
#include <JavaScriptCore/ExceptionHelpers.h>
#include <JavaScriptCore/ExceptionScope.h>
#include <JavaScriptCore/JSCast.h>
#include <JavaScriptCore/Heap.h>
#include <JavaScriptCore/Identifier.h>
#include <JavaScriptCore/InitializeThreading.h>
#include <JavaScriptCore/JSArray.h>
#include <JavaScriptCore/JSArrayBuffer.h>
#include <JavaScriptCore/JSArrayBufferView.h>
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSCJSValue.h>
#include <JavaScriptCore/JSFunction.h>
#include <JavaScriptCore/JSFunctionInlines.h>
#include <JavaScriptCore/FunctionExecutable.h>
#include <JavaScriptCore/NativeExecutable.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSLock.h>
#include <JavaScriptCore/JSPromise.h>
#include <JavaScriptCore/MicrotaskQueue.h>
#include <JavaScriptCore/JSString.h>
#include <JavaScriptCore/ObjectConstructor.h>
#include <JavaScriptCore/Options.h>
#include <JavaScriptCore/SourceCode.h>
#include <JavaScriptCore/SourceOrigin.h>
#include <JavaScriptCore/ThrowScope.h>
#include <JavaScriptCore/TypedArrayType.h>
#include <JavaScriptCore/VM.h>
#include <wtf/NakedPtr.h>
#include <wtf/text/WTFString.h>
#include <wtf/text/StringBuilder.h>
#include <cstdlib>
#include <cstring>
#include <unicode/udata.h>

// JSC registers unwind info for its JIT pool, LLInt and vmEntry with
// `RtlAddGrowableFunctionTable` (oven-sh/WebKit #315/#442), which lives in
// ntdll.lib. build.rs puts ntdll on the list for every cargo link; this
// directive puts it in the objects, so a consumer that links the THIN
// redjs.lib by hand (a C# host's NativeAOT csproj) gets it without having to
// learn that it changed.
#if defined(_WIN32)
#pragma comment(lib, "ntdll")
#endif

using namespace JSC;

namespace {

constexpr int32_t RJS_OK = 0;
constexpr int32_t RJS_EXCEPTION = 1;
constexpr int32_t RJS_BAD_ARG = 2;

inline JSGlobalObject* G(void* g) { return static_cast<JSGlobalObject*>(g); }

// JSLockHolder, skipped when this thread already holds the VM's lock — every call made from a host
// function. A nested JSLockHolder only bumps the lock count, behind a VM ref and unref.
class RjsLock final {
public:
    explicit RjsLock(VM& vm)
    {
        if (!vm.apiLock().currentThreadIsHoldingLock())
            m_holder.emplace(vm);
    }

private:
    std::optional<JSLockHolder> m_holder;
};

// Bun's WebKit fork removed CatchScope.h; VM::exception()/clearException()
// are private. ExceptionScope (protected ctor, public exception() +
// tryClearException()) gives us the sanctioned access path — this is the
// release-mode CatchScope in all but name.
class RjsCatchScope final : public ExceptionScope {
public:
    ALWAYS_INLINE RjsCatchScope(VM& vm)
        : ExceptionScope(vm)
    {
    }
};

// Returns true and stores the exception value into *out if the VM holds a
// pending exception (clearing it unless it's a termination request). `out`
// may be null (exception swallowed).
inline bool takeException(VM& vm, uint64_t* out)
{
    RjsCatchScope scope(vm);
    if (Exception* e = scope.exception()) {
        if (out)
            *out = JSValue::encode(e->value());
        (void)scope.tryClearException();
        return true;
    }
    return false;
}

// Our stable typed-array kind ids (mirrored in ffi.rs / C#).
enum RjsTaKind : uint32_t {
    RJS_TA_NONE = 0,
    RJS_TA_I8 = 1,
    RJS_TA_U8 = 2,
    RJS_TA_U8C = 3,
    RJS_TA_I16 = 4,
    RJS_TA_U16 = 5,
    RJS_TA_I32 = 6,
    RJS_TA_U32 = 7,
    RJS_TA_F32 = 8,
    RJS_TA_F64 = 9,
    RJS_TA_BIGI64 = 10,
    RJS_TA_BIGU64 = 11,
    RJS_TA_DATAVIEW = 12,
    RJS_TA_F16 = 13,
};

uint32_t mapTypedArrayType(TypedArrayType t)
{
    switch (t) {
    case TypeInt8: return RJS_TA_I8;
    case TypeUint8: return RJS_TA_U8;
    case TypeUint8Clamped: return RJS_TA_U8C;
    case TypeInt16: return RJS_TA_I16;
    case TypeUint16: return RJS_TA_U16;
    case TypeInt32: return RJS_TA_I32;
    case TypeUint32: return RJS_TA_U32;
    case TypeFloat32: return RJS_TA_F32;
    case TypeFloat64: return RJS_TA_F64;
    case TypeBigInt64: return RJS_TA_BIGI64;
    case TypeBigUint64: return RJS_TA_BIGU64;
    case TypeDataView: return RJS_TA_DATAVIEW;
    case TypeFloat16: return RJS_TA_F16;
    default: return RJS_TA_NONE;
    }
}

// Our stable property-attribute bits -> JSC's.
unsigned mapAttributes(uint32_t a)
{
    unsigned r = 0;
    if (a & 1) r |= PropertyAttribute::ReadOnly;
    if (a & 2) r |= PropertyAttribute::DontEnum;
    if (a & 4) r |= PropertyAttribute::DontDelete;
    return r;
}

} // namespace

// ---------------------------------------------------------------------------
// ABI layout export: everything the C# side needs to decode JSValues and read
// host-call CallFrames with zero FFI calls. Filled from the real headers this
// TU is compiled against, so it can never drift from the prebuilt.
// ---------------------------------------------------------------------------

struct RjsLayout {
    uint32_t abiVersion;
    // Byte offsets from the CallFrame pointer passed to host functions.
    uint32_t cfArgcOffset;   // u32 argumentCountIncludingThis (payload half)
    uint32_t cfThisOffset;   // EncodedJSValue `this`
    uint32_t cfArgsOffset;   // EncodedJSValue args[0] (contiguous upward)
    uint32_t cfCalleeOffset; // JSObject* callee
    uint32_t reserved0;
    // JSVALUE64 NaN-boxing constants.
    uint64_t tagNumber;          // JSValue::NumberTag
    uint64_t doubleEncodeOffset; // JSValue::DoubleEncodeOffset
    uint64_t notCellMask;        // JSValue::NotCellMask
    uint64_t valueUndefined;
    uint64_t valueNull;
    uint64_t valueTrue;
    uint64_t valueFalse;
};

extern "C" void rjs_layout(RjsLayout* out)
{
    out->abiVersion = 1;
    out->cfArgcOffset = static_cast<uint32_t>(CallFrameSlot::argumentCountIncludingThis) * sizeof(Register);
    out->cfThisOffset = static_cast<uint32_t>(CallFrameSlot::thisArgument) * sizeof(Register);
    out->cfArgsOffset = static_cast<uint32_t>(CallFrameSlot::firstArgument) * sizeof(Register);
    out->cfCalleeOffset = static_cast<uint32_t>(CallFrameSlot::callee) * sizeof(Register);
    out->reserved0 = 0;
    out->tagNumber = static_cast<uint64_t>(JSValue::NumberTag);
    out->doubleEncodeOffset = static_cast<uint64_t>(JSValue::DoubleEncodeOffset);
    out->notCellMask = static_cast<uint64_t>(JSValue::NotCellMask);
    out->valueUndefined = JSValue::encode(jsUndefined());
    out->valueNull = JSValue::encode(jsNull());
    out->valueTrue = JSValue::encode(jsBoolean(true));
    out->valueFalse = JSValue::encode(jsBoolean(false));
}

// ---------------------------------------------------------------------------
// Init / lifecycle
// ---------------------------------------------------------------------------

// The bun-webkit static ICU expects the common data to be handed over
// explicitly (u_init otherwise fails with U_FILE_ACCESS_ERROR and every
// Intl/toLocale* API breaks). Same wiring Bun does at startup.
//
// Version-agnostic on purpose: ICU renames every export by its major version
// (`udata_setCommonData` is `udata_setCommonData_78` at link time) and names
// the data blob the same way (`icudt78_dat`). Both names come from the
// prebuilt's own headers — <unicode/udata.h> carries the renaming and
// U_ICUDATA_ENTRY_POINT expands to the blob's symbol — so an ICU bump in the
// prebuilt is a recompile, not an edit.
#if !defined(__APPLE__)
extern "C" const char U_ICUDATA_ENTRY_POINT[];

// A package handed over by the embedder (`icu::set_common_data`). ICU searches its common data in
// the order it was set, so this one is found before the linked one.
static const void* rjsIcuCommonData;

extern "C" void rjs_icu_set_common_data(const void* data)
{
    rjsIcuCommonData = data;
}

extern "C" const void* rjs_icu_linked_data()
{
    return U_ICUDATA_ENTRY_POINT;
}
#endif

extern "C" int32_t rjs_init(const char* options)
{
#if !defined(__APPLE__)
    static bool icuReady = false;
    if (!icuReady) {
        icuReady = true;
        UErrorCode err = U_ZERO_ERROR;
        if (rjsIcuCommonData) {
            udata_setCommonData(rjsIcuCommonData, &err);
            if (U_FAILURE(err))
                fprintf(stderr, "[redjs] udata_setCommonData (embedder data) failed: %s (%d)\n", u_errorName(err), static_cast<int>(err));
            err = U_ZERO_ERROR;
        }
        udata_setCommonData(U_ICUDATA_ENTRY_POINT, &err);
        if (U_FAILURE(err))
            fprintf(stderr, "[redjs] udata_setCommonData failed: %s (%d)\n", u_errorName(err), static_cast<int>(err));
    }
#endif
    JSC::initialize();
    if (options && *options) {
        if (!Options::setOptions(options))
            return RJS_BAD_ARG;
    }
    return RJS_OK;
}

extern "C" int32_t rjs_set_options(const char* options)
{
    if (!options)
        return RJS_BAD_ARG;
    return Options::setOptions(options) ? RJS_OK : RJS_BAD_ARG;
}

extern "C" uint64_t rjs_global(void* g)
{
    return JSValue::encode(JSValue(G(g)));
}

extern "C" void rjs_lock(void* g)
{
    G(g)->vm().apiLock().lock();
}

extern "C" void rjs_unlock(void* g)
{
    G(g)->vm().apiLock().unlock();
}

extern "C" void rjs_drain_microtasks(void* g)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    // Async engine work (WebAssembly.instantiate compilation, Atomics.waitAsync
    // …) resolves its promises through the VM's DeferredWorkTimer, which is
    // normally serviced by a host run loop we don't run — pump it here so
    // those promises settle on the frame tick, then drain the microtasks it
    // queued.
    vm.deferredWorkTimer->doWork(vm);
    vm.drainMicrotasks();
}

// 1 if JS is on this VM's stack right now (any VMEntryScope is live) — the
// "JS execution context stack is empty" test HTML's microtask checkpoint
// hinges on. `web::checkpoint` runs its hooks only when this is 0.
extern "C" int32_t rjs_in_js(void* g)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    return vm.isEntered() ? 1 : 0;
}

extern "C" void rjs_gc(void* g, int32_t mode)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    switch (mode) {
    case 1: vm.heap.collectNow(Sync, CollectionScope::Eden); break;
    case 2: vm.heap.collectAsync(CollectionScope::Full); break;
    default: vm.heap.collectNow(Sync, CollectionScope::Full); break;
    }
}

struct RjsHeapStats {
    uint64_t heapSize;
    uint64_t heapCapacity;
    uint64_t extraMemory;
    uint64_t objectCount;
    uint64_t protectedObjectCount;
    uint64_t globalObjectCount;
};

extern "C" void rjs_heap_stats(void* g, RjsHeapStats* out)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    out->heapSize = vm.heap.size();
    out->heapCapacity = vm.heap.capacity();
    out->extraMemory = vm.heap.extraMemorySize();
    out->objectCount = vm.heap.objectCount();
    out->protectedObjectCount = vm.heap.protectedObjectCount();
    out->globalObjectCount = vm.heap.globalObjectCount();
}

extern "C" void rjs_report_extra_memory(void* g, uint64_t bytes)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    vm.heap.deprecatedReportExtraMemory(static_cast<size_t>(bytes));
}

// ---------------------------------------------------------------------------
// Eval
// ---------------------------------------------------------------------------

static int32_t evalCommon(JSGlobalObject* g, const WTF::String& code, const WTF::String& file, uint64_t* out)
{
    VM& vm = g->vm();
    RjsLock lock(vm);
    auto source = makeSource(code, SourceOrigin(), SourceTaintedOrigin::Untainted, file);
    NakedPtr<Exception> exc;
    JSValue result = evaluate(g, source, jsUndefined(), exc);
    if (exc) {
        if (out)
            *out = JSValue::encode(exc->value());
        return RJS_EXCEPTION;
    }
    if (out)
        *out = JSValue::encode(result);
    return RJS_OK;
}

extern "C" int32_t rjs_eval16(void* g, const char16_t* src, size_t len, const char16_t* url, size_t urlLen, uint64_t* out)
{
    WTF::String code(std::span<const char16_t> { src, len });
    WTF::String file = urlLen ? WTF::String(std::span<const char16_t> { url, urlLen }) : WTF::String();
    return evalCommon(G(g), code, file, out);
}

extern "C" int32_t rjs_eval8(void* g, const char* src, size_t len, const char* url, size_t urlLen, uint64_t* out)
{
    WTF::String code = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(src), len });
    WTF::String file = urlLen
        ? WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(url), urlLen })
        : WTF::String();
    return evalCommon(G(g), code, file, out);
}

// A cache is an optimisation of this classic-script source, never another
// program. The Rust layer admits only caches generated here or explicitly
// imported from a trusted local artifact after identity/integrity checks.
class RjsCachedSourceProvider final : public StringSourceProvider {
public:
    static Ref<RjsCachedSourceProvider> create(const WTF::String& code, WTF::String file, Ref<CachedBytecode>&& bytecode)
    {
        return adoptRef(*new RjsCachedSourceProvider(code, WTF::move(file), WTF::move(bytecode)));
    }

    RefPtr<CachedBytecode> cachedBytecode() const final
    {
        m_wasRead = true;
        return m_bytecode.ptr();
    }
    void didGenerateUnlinkedCodeBlock(VM&, const SourceCodeKey&, UnlinkedCodeBlock*) const final
    {
        m_wasGenerated = true;
    }
    bool wasUsed() const { return m_wasRead && !m_wasGenerated && Options::useCodeCache(); }

private:
    RjsCachedSourceProvider(const WTF::String& code, WTF::String file, Ref<CachedBytecode>&& bytecode)
        : StringSourceProvider(code, SourceOrigin(), SourceTaintedOrigin::Untainted, WTF::move(file), TextPosition(), SourceProviderSourceType::Program)
        , m_bytecode(WTF::move(bytecode))
    {
    }

    Ref<CachedBytecode> m_bytecode;
    mutable bool m_wasRead { false };
    mutable bool m_wasGenerated { false };
};

extern "C" uint32_t rjs_bytecode_cache_version()
{
    return computeJSCBytecodeCacheVersion();
}

// Snapshot all effective options instead of guessing which JSC flag a future
// engine revision will add to its bytecode format. The caller hashes the text.
extern "C" size_t rjs_bytecode_cache_options(void* g, uint8_t* destination, size_t capacity)
{
    RjsLock lock(G(g)->vm());
    WTF::StringBuilder builder;
    Options::dumpAllOptionsInALine(builder);
    auto options = builder.toString().utf8();
    if (destination && capacity >= options.length())
        std::memcpy(destination, options.data(), options.length());
    return options.length();
}

// The handle owns the encoded payload until rjs_bytecode_free. It contains no
// borrowed Rust memory; the bytes can be copied before releasing the handle.
extern "C" int32_t rjs_bytecode_generate(void* global, const char* src, size_t len, const char* url, size_t urlLen,
    uint32_t functionDepth, void** handle, const uint8_t** bytes, size_t* byteLength, uint64_t* out)
{
    if (!global || !handle || !bytes || !byteLength || (!src && len) || (!url && urlLen))
        return RJS_BAD_ARG;
    *handle = nullptr;
    *bytes = nullptr;
    *byteLength = 0;
    auto* g = G(global);
    VM& vm = g->vm();
    RjsLock lock(vm);
    auto code = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(src), len });
    auto file = urlLen ? WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(url), urlLen }) : WTF::String();
    auto source = makeSource(code, SourceOrigin(), SourceTaintedOrigin::Untainted, file);
    ParserError parserError;
    auto* block = recursivelyGenerateUnlinkedCodeBlockForProgram(vm, source, NoLexicallyScopedFeatures,
        JSParserScriptMode::Classic, { }, parserError, EvalContextType::None, functionDepth, OptimizeBytecode::No);
    if (parserError.isValid()) {
        if (out)
            *out = JSValue::encode(parserError.toErrorObject(g, source));
        return RJS_EXCEPTION;
    }
    if (!block)
        return RJS_BAD_ARG;
    Strong<UnlinkedProgramCodeBlock> rootedBlock(vm, block);
    auto key = sourceCodeKeyForSerializedProgram(vm, source);
    auto bytecode = encodeCodeBlock(vm, key, block, nullptr, BytecodeCacheUpdatable::No);
    if (!bytecode)
        return RJS_BAD_ARG;
    *bytes = bytecode->span().data();
    *byteLength = bytecode->size();
    *handle = bytecode.leakRef();
    return RJS_OK;
}

extern "C" void rjs_bytecode_free(void* handle)
{
    if (handle)
        static_cast<CachedBytecode*>(handle)->deref();
}

extern "C" int32_t rjs_eval8_cached(void* global, const char* src, size_t len, const char* url, size_t urlLen,
    const uint8_t* bytes, size_t byteLength, uint32_t* used, uint64_t* out)
{
    if (!global || !used || (!src && len) || (!url && urlLen) || (!bytes && byteLength))
        return RJS_BAD_ARG;
    *used = 0;
    auto* g = G(global);
    VM& vm = g->vm();
    RjsLock lock(vm);
    auto code = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(src), len });
    auto file = urlLen ? WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(url), urlLen }) : WTF::String();
    if (!byteLength)
        return evalCommon(g, code, file, out);

    // JSC may decode nested functions after eval returns, or keep pointers into
    // its decoder. Give CachedBytecode ownership, never promise persistence of
    // a caller's slice. Its destructor follows the last decoder's lifetime.
    auto* copy = static_cast<uint8_t*>(std::malloc(byteLength));
    if (!copy)
        return evalCommon(g, code, file, out);
    std::memcpy(copy, bytes, byteLength);
    auto bytecode = CachedBytecode::create(std::span<uint8_t> { copy, byteLength },
        [] (const void* address) { std::free(const_cast<void*>(address)); }, { });
    // Bun omits source text from its cache. Its provider-less
    // isCachedBytecodeStillValid() intentionally rejects such payloads. The
    // ordinary CodeCache decoder receives this runtime provider, validates the
    // format/source key, and falls back to parsing before any execution.
    auto provider = RjsCachedSourceProvider::create(code, WTF::move(file), WTF::move(bytecode));
    SourceCode cachedSource(provider.copyRef());
    NakedPtr<Exception> exception;
    auto result = evaluate(g, cachedSource, jsUndefined(), exception);
    *used = provider->wasUsed() ? 1 : 0;
    // An exception is an executed program's result. Never fall back here: it
    // would repeat any effects performed before the throw.
    if (out)
        *out = JSValue::encode(exception ? exception->value() : result);
    return exception ? RJS_EXCEPTION : RJS_OK;
}

// ---------------------------------------------------------------------------
// Values
// ---------------------------------------------------------------------------

// Mirrored in ffi.rs / C#.
enum RjsKind : uint32_t {
    RJS_KIND_UNDEFINED = 0,
    RJS_KIND_NULL = 1,
    RJS_KIND_BOOL = 2,
    RJS_KIND_INT32 = 3,
    RJS_KIND_DOUBLE = 4,
    RJS_KIND_STRING = 5,
    RJS_KIND_SYMBOL = 6,
    RJS_KIND_BIGINT = 7,
    RJS_KIND_OBJECT = 8,
    RJS_KIND_FUNCTION = 9,
    RJS_KIND_ARRAY = 10,
    RJS_KIND_TYPEDARRAY = 11,
    RJS_KIND_ARRAYBUFFER = 12,
    RJS_KIND_PROMISE = 13,
};

extern "C" uint32_t rjs_kind(void* g, uint64_t v)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    JSValue val = JSValue::decode(v);
    if (val.isUndefined() || !v) return RJS_KIND_UNDEFINED;
    if (val.isNull()) return RJS_KIND_NULL;
    if (val.isBoolean()) return RJS_KIND_BOOL;
    if (val.isInt32()) return RJS_KIND_INT32;
    if (val.isNumber()) return RJS_KIND_DOUBLE;
    if (val.isString()) return RJS_KIND_STRING;
    if (val.isSymbol()) return RJS_KIND_SYMBOL;
    if (val.isBigInt()) return RJS_KIND_BIGINT;
    if (!val.isObject()) return RJS_KIND_OBJECT; // other cells: treat as object
    if (dynamicDowncast<JSArrayBufferView>(val)) return RJS_KIND_TYPEDARRAY;
    if (dynamicDowncast<JSArrayBuffer>(val)) return RJS_KIND_ARRAYBUFFER;
    if (dynamicDowncast<JSPromise>(val)) return RJS_KIND_PROMISE;
    if (isJSArray(val)) return RJS_KIND_ARRAY;
    if (val.isCallable()) return RJS_KIND_FUNCTION;
    return RJS_KIND_OBJECT;
}

extern "C" int32_t rjs_to_number(void* g, uint64_t v, double* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    double d = JSValue::decode(v).toNumber(go);
    uint64_t exc = 0;
    if (takeException(vm, &exc))
        return RJS_EXCEPTION;
    *out = d;
    return RJS_OK;
}

// ToNumber that hands back what it threw (rjs_to_number swallows it), for the
// WebIDL conversions whose exceptions must reach the caller unchanged.
extern "C" int32_t rjs_to_number_x(void* g, uint64_t v, double* out, uint64_t* exc)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    double d = JSValue::decode(v).toNumber(go);
    if (takeException(vm, exc))
        return RJS_EXCEPTION;
    *out = d;
    return RJS_OK;
}

extern "C" int32_t rjs_to_string(void* g, uint64_t v, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSString* s = JSValue::decode(v).toString(go);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(JSValue(s));
    return RJS_OK;
}

extern "C" int32_t rjs_to_boolean(void* g, uint64_t v)
{
    JSGlobalObject* go = G(g);
    RjsLock lock(go->vm());
    return JSValue::decode(v).toBoolean(go) ? 1 : 0;
}

extern "C" int32_t rjs_strict_equals(void* g, uint64_t a, uint64_t b)
{
    JSGlobalObject* go = G(g);
    RjsLock lock(go->vm());
    return JSValue::strictEqual(go, JSValue::decode(a), JSValue::decode(b)) ? 1 : 0;
}

// ---------------------------------------------------------------------------
// Strings
// ---------------------------------------------------------------------------

extern "C" int32_t rjs_string16(void* g, const char16_t* p, size_t len, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    if (!len) {
        *out = JSValue::encode(jsEmptyString(vm));
        return RJS_OK;
    }
    *out = JSValue::encode(jsString(vm, WTF::String(std::span<const char16_t> { p, len })));
    return RJS_OK;
}

extern "C" int32_t rjs_string8(void* g, const char* p, size_t len, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    if (!len) {
        *out = JSValue::encode(jsEmptyString(vm));
        return RJS_OK;
    }
    auto s = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(p), len });
    if (s.isNull())
        return RJS_BAD_ARG; // invalid UTF-8
    *out = JSValue::encode(jsString(vm, s));
    return RJS_OK;
}

struct RjsStr {
    const void* ptr;
    uint32_t len;
    uint32_t is8bit; // 1 => Latin-1 bytes, 0 => UTF-16 code units
};

extern "C" int32_t rjs_string_info(void* g, uint64_t v, RjsStr* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue val = JSValue::decode(v);
    if (!val.isString())
        return RJS_BAD_ARG;
    auto held = asString(val)->value(go); // resolves ropes; result owned by the JSString cell
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    const WTF::String& w = held;
    auto* impl = w.impl();
    if (!impl || !impl->length()) {
        out->ptr = nullptr;
        out->len = 0;
        out->is8bit = 1;
        return RJS_OK;
    }
    out->len = impl->length();
    if (impl->is8Bit()) {
        out->is8bit = 1;
        out->ptr = impl->span8().data();
    } else {
        out->is8bit = 0;
        out->ptr = impl->span16().data();
    }
    return RJS_OK;
}

// ---------------------------------------------------------------------------
// Atoms (interned property names)
// ---------------------------------------------------------------------------

extern "C" void* rjs_atom16(void* g, const char16_t* p, size_t len)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    return new Identifier(Identifier::fromString(vm, WTF::String(std::span<const char16_t> { p, len })));
}

extern "C" void* rjs_atom8(void* g, const char* p, size_t len)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    auto s = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(p), len });
    if (s.isNull())
        return nullptr;
    return new Identifier(Identifier::fromString(vm, s));
}

extern "C" void rjs_atom_free(void* g, void* atom)
{
    // Identifier destruction derefs an ATOM StringImpl, which must be removed
    // from the owning VM's atom table — and WTF resolves that table through a
    // thread-local that JSLock swaps to the VM's on acquire. Freeing without
    // the lock hits the wrong table and fastfails in AtomStringImpl::remove.
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    delete static_cast<Identifier*>(atom);
}

// ---------------------------------------------------------------------------
// Properties
// ---------------------------------------------------------------------------

extern "C" int32_t rjs_get(void* g, uint64_t base, void* atom, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue r = JSValue::decode(base).get(go, *static_cast<Identifier*>(atom));
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(r);
    return RJS_OK;
}

// A read that throws comes back as undefined, as rjs_get callers that ignore the status see it.
extern "C" int32_t rjs_get_many(void* g, uint64_t base, void* const* atoms, size_t n, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue b = JSValue::decode(base);
    if (!b.isObject())
        return RJS_BAD_ARG;
    for (size_t i = 0; i < n; ++i) {
        JSValue r = b.get(go, *static_cast<Identifier*>(atoms[i]));
        out[i] = takeException(vm, nullptr) ? JSValue::encode(jsUndefined()) : JSValue::encode(r);
    }
    return RJS_OK;
}

// base[atoms[0]][atoms[1]]…; RJS_BAD_ARG when a step's base is not an object.
extern "C" int32_t rjs_get_path(void* g, uint64_t base, void* const* atoms, size_t n, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue v = JSValue::decode(base);
    for (size_t i = 0; i < n; ++i) {
        if (!v.isObject())
            return RJS_BAD_ARG;
        v = v.get(go, *static_cast<Identifier*>(atoms[i]));
        if (takeException(vm, out))
            return RJS_EXCEPTION;
    }
    *out = JSValue::encode(v);
    return RJS_OK;
}

extern "C" int32_t rjs_define_many(void* g, uint64_t obj, void* const* atoms, const uint64_t* vals, size_t n, uint32_t attrs)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    JSObject* o = asObject(base);
    unsigned a = mapAttributes(attrs);
    for (size_t i = 0; i < n; ++i)
        o->putDirect(vm, *static_cast<Identifier*>(atoms[i]), JSValue::decode(vals[i]), a);
    return RJS_OK;
}

extern "C" int32_t rjs_set(void* g, uint64_t obj, void* atom, uint64_t val)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    JSObject* o = asObject(base);
    PutPropertySlot slot(base, false);
    o->methodTable()->put(o, go, *static_cast<Identifier*>(atom), JSValue::decode(val), slot);
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    return RJS_OK;
}

extern "C" int32_t rjs_define(void* g, uint64_t obj, void* atom, uint64_t val, uint32_t attrs)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    asObject(base)->putDirect(vm, *static_cast<Identifier*>(atom), JSValue::decode(val), mapAttributes(attrs));
    return RJS_OK;
}

extern "C" int32_t rjs_get_index(void* g, uint64_t base, uint32_t index, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue r = JSValue::decode(base).get(go, index);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(r);
    return RJS_OK;
}

extern "C" int32_t rjs_set_index(void* g, uint64_t obj, uint32_t index, uint64_t val)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    JSObject* o = asObject(base);
    o->methodTable()->putByIndex(o, go, index, JSValue::decode(val), false);
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    return RJS_OK;
}

extern "C" int32_t rjs_has(void* g, uint64_t obj, void* atom)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return -2;
    bool r = asObject(base)->hasProperty(go, *static_cast<Identifier*>(atom));
    if (takeException(vm, nullptr))
        return -1;
    return r ? 1 : 0;
}

extern "C" int32_t rjs_delete(void* g, uint64_t obj, void* atom)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return -2;
    bool r = asObject(base)->deleteProperty(go, *static_cast<Identifier*>(atom));
    if (takeException(vm, nullptr))
        return -1;
    return r ? 1 : 0;
}

// ---------------------------------------------------------------------------
// Objects / arrays / functions / calls
// ---------------------------------------------------------------------------

extern "C" int32_t rjs_object_create(void* g, uint64_t proto, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue protoVal = JSValue::decode(proto);
    JSObject* o;
    if (protoVal.isObject())
        o = constructEmptyObject(go, asObject(protoVal));
    else if (protoVal.isNull())
        // Object.create(null). The (globalObject, prototype) overload asks the
        // structure cache for a prototype it then dereferences — a null one
        // fastfails; the realm keeps a structure for exactly this.
        o = constructEmptyObject(vm, go->nullPrototypeObjectStructure());
    else
        o = constructEmptyObject(go);
    *out = JSValue::encode(JSValue(o));
    return RJS_OK;
}

extern "C" int32_t rjs_array_create(void* g, uint32_t len, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSArray* a = constructEmptyArray(go, nullptr, len);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(JSValue(a));
    return RJS_OK;
}

extern "C" int32_t rjs_array_length(void* g, uint64_t v, uint32_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue val = JSValue::decode(v);
    if (isJSArray(val)) {
        *out = downcast<JSArray>(val)->length();
        return RJS_OK;
    }
    if (!val.isObject())
        return RJS_BAD_ARG;
    JSValue lenVal = val.get(go, vm.propertyNames->length);
    uint32_t len = lenVal.toUInt32(go);
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    *out = len;
    return RJS_OK;
}

// `sysvFn` must follow the JSC host-call convention for this build:
// EncodedJSValue (SysV x64) fn(JSGlobalObject*, CallFrame*).
// Rust/Zig can produce these directly; C# goes through the trampoline pool.
extern "C" int32_t rjs_function_create(void* g, const char16_t* name, size_t nameLen, uint32_t arity, void* sysvFn, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    using RawHostFn = EncodedJSValue(JSC_HOST_CALL_ATTRIBUTES*)(JSGlobalObject*, CallFrame*);
    WTF::String n = nameLen ? WTF::String(std::span<const char16_t> { name, nameLen }) : WTF::String();
    JSFunction* f = JSFunction::create(vm, go, arity, n, reinterpret_cast<RawHostFn>(sysvFn), ImplementationVisibility::Public);
    *out = JSValue::encode(JSValue(f));
    return RJS_OK;
}

struct RjsProfileFunctionInfo {
    uint32_t line;
    uint32_t column;
    uint32_t nameLen;
    uint32_t urlLen;
    char name[192];
    char url[768];
};

// Executable metadata avoids observable function.name/displayName getters.
extern "C" int32_t rjs_profile_function_info(void* g, uint64_t value, RjsProfileFunctionInfo* out)
{
    if (!g || !out)
        return 0;
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSFunction* function = dynamicDowncast<JSFunction>(JSValue::decode(value));
    if (!function)
        return 0;
    std::memset(out, 0, sizeof(*out));
    auto copy = [](const String& value, char* destination, size_t capacity) -> uint32_t {
        auto bytes = value.utf8();
        size_t length = bytes.length() < capacity ? bytes.length() : capacity;
        if (length)
            std::memcpy(destination, bytes.data(), length);
        return static_cast<uint32_t>(length);
    };
    if (function->isHostFunction()) {
        auto* executable = static_cast<NativeExecutable*>(function->executable());
        out->nameLen = copy(executable->name(), out->name, sizeof(out->name));
    } else {
        FunctionExecutable* executable = function->jsExecutable();
        out->nameLen = copy(executable->nameWithoutGC(), out->name, sizeof(out->name));
        if (!executable->source().isNull() && executable->source().provider()) {
            out->urlLen = copy(executable->sourceURL(), out->url, sizeof(out->url));
            out->line = executable->firstLine();
            out->column = executable->startColumn();
        }
    }
    return 1;
}

extern "C" int32_t rjs_call(void* g, uint64_t fnVal, uint64_t thisVal, const uint64_t* args, uint32_t argc, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue f = JSValue::decode(fnVal);
    auto callData = JSC::getCallData(f);
    if (callData.type == CallData::Type::None)
        return RJS_BAD_ARG;
    MarkedArgumentBuffer buf;
    buf.ensureCapacity(argc);
    for (uint32_t i = 0; i < argc; i++)
        buf.append(JSValue::decode(args[i]));
    if (buf.hasOverflowed())
        return RJS_BAD_ARG;
    NakedPtr<Exception> exc;
    JSValue r = JSC::call(go, f, callData, JSValue::decode(thisVal), buf, exc);
    if (exc) {
        if (out)
            *out = JSValue::encode(exc->value());
        return RJS_EXCEPTION;
    }
    if (out)
        *out = JSValue::encode(r);
    return RJS_OK;
}

extern "C" int32_t rjs_construct(void* g, uint64_t ctorVal, const uint64_t* args, uint32_t argc, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue c = JSValue::decode(ctorVal);
    auto constructData = JSC::getConstructData(c);
    if (constructData.type == CallData::Type::None)
        return RJS_BAD_ARG;
    MarkedArgumentBuffer buf;
    buf.ensureCapacity(argc);
    for (uint32_t i = 0; i < argc; i++)
        buf.append(JSValue::decode(args[i]));
    if (buf.hasOverflowed())
        return RJS_BAD_ARG;
    JSObject* r = JSC::construct(go, c, constructData, buf);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(JSValue(r));
    return RJS_OK;
}

// ---------------------------------------------------------------------------
// Errors / exceptions
// ---------------------------------------------------------------------------

extern "C" void rjs_throw(void* g, uint64_t err)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto scope = DECLARE_THROW_SCOPE(vm);
    throwException(go, scope, JSValue::decode(err));
}

extern "C" int32_t rjs_error_create(void* g, uint32_t kind, const char16_t* msg, size_t len, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    WTF::String m = len ? WTF::String(std::span<const char16_t> { msg, len }) : WTF::String();
    JSObject* e;
    switch (kind) {
    case 1: e = createTypeError(go, m); break;
    case 2: e = createRangeError(go, m); break;
    case 3: e = createSyntaxError(go, m); break;
    default: e = createError(go, m); break;
    }
    *out = JSValue::encode(JSValue(e));
    return RJS_OK;
}

extern "C" int32_t rjs_error_info(void* g, uint64_t err, uint64_t* msgOut, uint64_t* stackOut)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue e = JSValue::decode(err);

    JSValue msg = e.isObject() ? e.get(go, vm.propertyNames->message) : e;
    if (takeException(vm, nullptr)) { msg = e; }
    JSString* msgStr = msg.toString(go);
    if (takeException(vm, nullptr)) { msgStr = jsEmptyString(vm); }
    *msgOut = JSValue::encode(JSValue(msgStr));

    JSValue stack = e.isObject() ? e.get(go, vm.propertyNames->stack) : jsUndefined();
    if (takeException(vm, nullptr)) { stack = jsUndefined(); }
    if (stack.isUndefined()) {
        *stackOut = JSValue::encode(jsEmptyString(vm));
    } else {
        JSString* stackStr = stack.toString(go);
        if (takeException(vm, nullptr)) { stackStr = jsEmptyString(vm); }
        *stackOut = JSValue::encode(JSValue(stackStr));
    }
    return RJS_OK;
}

extern "C" int32_t rjs_exception_pending(void* g, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    return takeException(vm, out) ? 1 : 0;
}

// ---------------------------------------------------------------------------
// Typed arrays / ArrayBuffers (introspection; creation goes through the C API)
// ---------------------------------------------------------------------------

struct RjsTaInfo {
    void* data;
    uint32_t length;     // element count
    uint32_t byteLength;
    uint32_t byteOffset;
    uint32_t kind;       // RjsTaKind
    uint32_t detached;
    uint32_t shared;
};

extern "C" int32_t rjs_typedarray_info(void* g, uint64_t v, RjsTaInfo* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto* view = dynamicDowncast<JSArrayBufferView>(JSValue::decode(v));
    if (!view)
        return RJS_BAD_ARG;
    out->kind = mapTypedArrayType(typedArrayType(view->type()));
    out->detached = view->isDetached() ? 1 : 0;
    out->shared = view->isShared() ? 1 : 0;
    if (out->detached) {
        out->data = nullptr;
        out->length = 0;
        out->byteLength = 0;
        out->byteOffset = 0;
        return RJS_OK;
    }
    out->data = view->vector();
    out->length = static_cast<uint32_t>(view->length());
    out->byteLength = static_cast<uint32_t>(view->byteLength());
    out->byteOffset = static_cast<uint32_t>(view->byteOffset());
    return RJS_OK;
}

struct RjsAbInfo {
    void* data;
    uint64_t byteLength;
    uint32_t detached;
    uint32_t shared;
};

extern "C" int32_t rjs_arraybuffer_info(void* g, uint64_t v, RjsAbInfo* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto* jsBuf = dynamicDowncast<JSArrayBuffer>(JSValue::decode(v));
    if (!jsBuf)
        return RJS_BAD_ARG;
    ArrayBuffer* impl = jsBuf->impl();
    if (!impl) {
        out->data = nullptr;
        out->byteLength = 0;
        out->detached = 1;
        out->shared = 0;
        return RJS_OK;
    }
    out->data = impl->data();
    out->byteLength = impl->byteLength();
    out->detached = out->data ? 0 : 1;
    out->shared = impl->isShared() ? 1 : 0;
    return RJS_OK;
}

extern "C" int32_t rjs_arraybuffer_detach(void* g, uint64_t v)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto* jsBuf = dynamicDowncast<JSArrayBuffer>(JSValue::decode(v));
    // A locked buffer (a WebAssembly.Memory's) is not detachable: ECMA-262
    // DetachArrayBuffer throws for it, and the caller says so.
    if (!jsBuf || !jsBuf->impl() || !jsBuf->impl()->isDetachable())
        return RJS_BAD_ARG;
    jsBuf->impl()->detach(vm);
    return RJS_OK;
}

// ---------------------------------------------------------------------------
// Promises
// ---------------------------------------------------------------------------

extern "C" int32_t rjs_promise_info(void* g, uint64_t v, uint32_t* statusOut, uint64_t* resultOut)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto* p = dynamicDowncast<JSPromise>(JSValue::decode(v));
    if (!p)
        return RJS_BAD_ARG;
    *statusOut = static_cast<uint32_t>(p->status()); // 0 pending, 1 fulfilled, 2 rejected
    *resultOut = JSValue::encode(p->result());
    return RJS_OK;
}

// ---------------------------------------------------------------------------
// Native classes: constructors, accessors, private-name slots
// ---------------------------------------------------------------------------

#include <JavaScriptCore/GetterSetter.h>
#include <JavaScriptCore/PrivateName.h>

// Like rjs_function_create but constructable: `ctorFn` runs for `new F()` /
// Reflect.construct. Inside the construct callback the CallFrame's THIS slot
// holds new.target (JSC convention for C++ constructors).
extern "C" int32_t rjs_function_create_full(void* g, const char16_t* name, size_t nameLen, uint32_t arity, void* callFn, void* ctorFn, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    using RawHostFn = EncodedJSValue(JSC_HOST_CALL_ATTRIBUTES*)(JSGlobalObject*, CallFrame*);
    WTF::String n = nameLen ? WTF::String(std::span<const char16_t> { name, nameLen }) : WTF::String();
    JSFunction* f = JSFunction::create(vm, go, arity, n,
        reinterpret_cast<RawHostFn>(callFn), ImplementationVisibility::Public, NoIntrinsic,
        reinterpret_cast<RawHostFn>(ctorFn));
    *out = JSValue::encode(JSValue(f));
    return RJS_OK;
}

// Defines a spec accessor property. `getter`/`setter` are function values (or
// undefined for write-only/read-only).
extern "C" int32_t rjs_define_accessor(void* g, uint64_t obj, void* atom, uint64_t getter, uint64_t setter, uint32_t attrs)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    GetterSetter* gs = GetterSetter::create(vm, go, JSValue::decode(getter), JSValue::decode(setter));
    asObject(base)->putDirectAccessor(go, *static_cast<Identifier*>(atom), gs, mapAttributes(attrs) | PropertyAttribute::Accessor);
    return RJS_OK;
}

// A PRIVATE-name atom: properties keyed by it are invisible to all of JS
// (not even Object.getOwnPropertySymbols) — internal slots for native classes.
extern "C" void* rjs_atom_private(void* g, const char* p, size_t len)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    auto s = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(p), len });
    PrivateName pn(PrivateName::PrivateSymbol, s);
    return new Identifier(Identifier::fromUid(pn));
}

// The well-known Symbol.iterator as an atom, so native classes can implement
// the iteration protocol (for..of, spread, Array.from) without evaluating JS.
extern "C" void* rjs_atom_iterator(void* g)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    return new Identifier(vm.propertyNames->iteratorSymbol);
}

// Enqueues `fn` on the VM's real microtask queue (the HTML `queueMicrotask`).
// InvokeFunctionJob is JSC's "call this function" microtask; the prebuilt
// exports VM::queueMicrotask(QueuedTask&&) (the JSGlobalObject convenience
// overloads aren't in the import lib), and QueuedTask is header-constructible.
extern "C" int32_t rjs_queue_microtask(void* g, uint64_t fn)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue f = JSValue::decode(fn);
    if (!f.isCallable())
        return RJS_BAD_ARG;
    vm.queueMicrotask(QueuedTask(nullptr, InternalMicrotask::InvokeFunctionJob, 0, go, f));
    return RJS_OK;
}

// Own enumerable string-keyed property names of an object, as a JS array of
// strings (the structured serializer / object walking).
extern "C" int32_t rjs_own_keys(void* g, uint64_t obj, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    JSObject* o = asObject(base);
    // The Bun fork replaced PropertyNameArray construction with a builder.
    PropertyNameArrayBuilder names(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
    o->methodTable()->getOwnPropertyNames(o, go, names, DontEnumPropertiesMode::Exclude);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    JSArray* arr = constructEmptyArray(go, nullptr, 0);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    unsigned i = 0;
    for (const auto& id : names) {
        arr->putDirectIndex(go, i++, jsString(vm, id.string()));
    }
    *out = JSValue::encode(JSValue(arr));
    return RJS_OK;
}

// ---------------------------------------------------------------------------
// Structured serialization support (webapi/serialize.rs, webapi/domexception.rs)
//
// HTML's StructuredSerializeInternal branches on INTERNAL SLOTS ([[DateValue]],
// [[RegExpMatcher]], [[MapData]], [[ErrorData]], [[ArrayBufferData]]…), which
// no script can observe without side effects — `instanceof` and
// `Object.prototype.toString` are both forgeable. These read the cell's own
// type instead, so a brand is exactly what the engine says it is.
// ---------------------------------------------------------------------------

#include <JavaScriptCore/BigIntObject.h>
#include <JavaScriptCore/BooleanObject.h>
#include <JavaScriptCore/DateInstance.h>
#include <JavaScriptCore/NumberObject.h>
#include <JavaScriptCore/RegExpObject.h>
#include <JavaScriptCore/StringObject.h>
#include <JavaScriptCore/SymbolObject.h>
#include <JavaScriptCore/YarrFlags.h>

// Well-known symbols as atoms: 0 iterator, 1 toStringTag, 2 asyncIterator.
extern "C" void* rjs_atom_wellknown(void* g, uint32_t which)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    switch (which) {
    case 0: return new Identifier(vm.propertyNames->iteratorSymbol);
    case 1: return new Identifier(vm.propertyNames->toStringTagSymbol);
    case 2: return new Identifier(vm.propertyNames->asyncIteratorSymbol);
    default: return nullptr;
    }
}

// Stable brand ids (mirrored in glue.rs).
enum RjsBrand : uint32_t {
    RJS_BRAND_NONE = 0,       // not an object
    RJS_BRAND_PLAIN = 1,      // ordinary JSFinalObject
    RJS_BRAND_ARRAY = 2,
    RJS_BRAND_FUNCTION = 3,
    RJS_BRAND_PROXY = 4,
    RJS_BRAND_DATE = 5,
    RJS_BRAND_REGEXP = 6,
    RJS_BRAND_MAP = 7,
    RJS_BRAND_SET = 8,
    RJS_BRAND_ERROR = 9,
    RJS_BRAND_BOOLEAN_OBJECT = 10,
    RJS_BRAND_NUMBER_OBJECT = 11,
    RJS_BRAND_STRING_OBJECT = 12,
    RJS_BRAND_BIGINT_OBJECT = 13,
    RJS_BRAND_SYMBOL_OBJECT = 14,
    RJS_BRAND_ARRAY_BUFFER = 15,
    RJS_BRAND_SHARED_ARRAY_BUFFER = 16,
    RJS_BRAND_VIEW = 17,      // typed array or DataView (taKind says which)
    RJS_BRAND_PROMISE = 18,
    RJS_BRAND_GLOBAL = 19,
    RJS_BRAND_OTHER = 20,     // any other object with internal slots (WeakMap, generator, arguments, C-API object…)
};

enum : uint32_t {
    RJS_BF_DETACHED = 1,
    RJS_BF_RESIZABLE = 2,        // AB: resizable/growable; view: backed by one
    RJS_BF_LENGTH_TRACKING = 4,  // view: length follows its buffer
    RJS_BF_REDJS_SLOT = 8,       // carries a "redjs#…" private-name slot (a redjs platform object)
};

struct RjsBrandInfo {
    uint32_t brand;
    uint32_t flags;
    uint32_t taKind;
    uint32_t reserved;
    uint64_t byteLength;    // ArrayBuffer
    uint64_t byteOffset;    // view: [[ByteOffset]]
    uint64_t length;        // view: [[ArrayLength]] (DataView: [[ByteLength]])
    uint64_t maxByteLength; // resizable ArrayBuffer
    double number;          // Date: [[DateValue]]
    void* data;             // ArrayBuffer bytes (copy before any allocation)
};

namespace {

// A redjs native class keeps its internal slots under private names minted by
// rjs_atom_private as "redjs#…". Class private fields (`#x`) are private names
// too, but their description never starts with "redjs#", and an ordinary
// object that only has those is still an ordinary object to HTML.
bool hasRedjsSlot(JSGlobalObject* go, JSObject* o)
{
    VM& vm = go->vm();
    PropertyNameArrayBuilder names(vm, PropertyNameMode::Symbols, PrivateSymbolMode::Include);
    o->methodTable()->getOwnPropertyNames(o, go, names, DontEnumPropertiesMode::Include);
    if (takeException(vm, nullptr))
        return false;
    for (const auto& id : names) {
        auto* uid = id.impl();
        if (!uid || !uid->isSymbol() || !static_cast<SymbolImpl*>(uid)->isPrivate())
            continue;
        if (WTF::String(uid).startsWith("redjs#"_s))
            return true;
    }
    return false;
}

} // namespace

extern "C" uint32_t rjs_brand(void* g, uint64_t v, RjsBrandInfo* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    *out = RjsBrandInfo {};
    JSValue val = JSValue::decode(v);
    if (!v || !val.isObject())
        return out->brand = RJS_BRAND_NONE;
    JSObject* o = asObject(val);
    uint32_t b = RJS_BRAND_OTHER;
    switch (o->type()) {
    case ProxyObjectType: b = RJS_BRAND_PROXY; break;
    case GlobalObjectType:
    case GlobalProxyType: b = RJS_BRAND_GLOBAL; break;
    case ArrayType:
    case DerivedArrayType: b = RJS_BRAND_ARRAY; break;
    case JSDateType:
        b = RJS_BRAND_DATE;
        out->number = downcast<DateInstance>(o)->internalNumber();
        break;
    case RegExpObjectType: b = RJS_BRAND_REGEXP; break;
    case JSMapType: b = RJS_BRAND_MAP; break;
    case JSSetType: b = RJS_BRAND_SET; break;
    case ErrorInstanceType: b = RJS_BRAND_ERROR; break;
    case BooleanObjectType: b = RJS_BRAND_BOOLEAN_OBJECT; break;
    case NumberObjectType: b = RJS_BRAND_NUMBER_OBJECT; break;
    case StringObjectType:
    case DerivedStringObjectType: b = RJS_BRAND_STRING_OBJECT; break;
    case JSPromiseType: b = RJS_BRAND_PROMISE; break;
    case ArrayBufferType: {
        ArrayBuffer* impl = downcast<JSArrayBuffer>(o)->impl();
        b = (impl && impl->isShared()) ? RJS_BRAND_SHARED_ARRAY_BUFFER : RJS_BRAND_ARRAY_BUFFER;
        if (!impl || impl->isDetached()) {
            out->flags |= RJS_BF_DETACHED;
            break;
        }
        out->data = impl->data();
        out->byteLength = impl->byteLength();
        if (impl->isResizableOrGrowableShared()) {
            out->flags |= RJS_BF_RESIZABLE;
            out->maxByteLength = impl->maxByteLength().value_or(impl->byteLength());
        }
        break;
    }
    case Int8ArrayType:
    case Uint8ArrayType:
    case Uint8ClampedArrayType:
    case Int16ArrayType:
    case Uint16ArrayType:
    case Int32ArrayType:
    case Uint32ArrayType:
    case Float16ArrayType:
    case Float32ArrayType:
    case Float64ArrayType:
    case BigInt64ArrayType:
    case BigUint64ArrayType:
    case DataViewType: {
        auto* view = downcast<JSArrayBufferView>(o);
        b = RJS_BRAND_VIEW;
        out->taKind = mapTypedArrayType(typedArrayType(view->type()));
        if (view->isDetached())
            out->flags |= RJS_BF_DETACHED;
        if (view->isResizableOrGrowableShared())
            out->flags |= RJS_BF_RESIZABLE;
        if (view->isAutoLength())
            out->flags |= RJS_BF_LENGTH_TRACKING;
        out->byteOffset = view->byteOffsetRaw();
        out->length = view->lengthRaw();
        break;
    }
    default:
        if (dynamicDowncast<BigIntObject>(o))
            b = RJS_BRAND_BIGINT_OBJECT;
        else if (dynamicDowncast<SymbolObject>(o))
            b = RJS_BRAND_SYMBOL_OBJECT;
        else if (o->isCallable())
            b = RJS_BRAND_FUNCTION;
        else if (o->type() == FinalObjectType || o->type() == CompiledObjectType)
            b = RJS_BRAND_PLAIN;
        break;
    }
    if ((b == RJS_BRAND_PLAIN || b == RJS_BRAND_ERROR) && hasRedjsSlot(go, o))
        out->flags |= RJS_BF_REDJS_SLOT;
    out->brand = b;
    return b;
}

// The primitive inside a Boolean/Number/String/BigInt/Symbol wrapper object.
extern "C" int32_t rjs_wrapper_value(void* g, uint64_t v, uint64_t* out)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    JSValue w = JSValue::decode(v);
    JSWrapperObject* wo = nullptr;
    if (auto* x = dynamicDowncast<BooleanObject>(w))
        wo = x;
    else if (auto* x = dynamicDowncast<NumberObject>(w))
        wo = x;
    else if (auto* x = dynamicDowncast<StringObject>(w))
        wo = x;
    else if (auto* x = dynamicDowncast<BigIntObject>(w))
        wo = x;
    else if (auto* x = dynamicDowncast<SymbolObject>(w))
        wo = x;
    if (!wo)
        return RJS_BAD_ARG;
    *out = JSValue::encode(wo->internalValue());
    return RJS_OK;
}

// [[OriginalSource]] and [[OriginalFlags]] of a RegExp, as JS strings.
extern "C" int32_t rjs_regexp_parts(void* g, uint64_t v, uint64_t* pattern, uint64_t* flags)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    auto* r = dynamicDowncast<RegExpObject>(JSValue::decode(v));
    if (!r)
        return RJS_BAD_ARG;
    RegExp* re = r->regExp();
    *pattern = JSValue::encode(jsString(vm, re->pattern()));
    auto fs = Yarr::flagsString(re->flags());
    *flags = JSValue::encode(jsString(vm, WTF::String(fs.span())));
    return RJS_OK;
}

// [[Get]] with a VALUE key (ToPropertyKey) — getters run, exceptions come back.
extern "C" int32_t rjs_get_v(void* g, uint64_t obj, uint64_t key, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    Identifier pk = JSValue::decode(key).toPropertyKey(go);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    JSValue r = JSValue::decode(obj).get(go, pk);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    *out = JSValue::encode(r);
    return RJS_OK;
}

// HasOwnProperty with a VALUE key: 1 / 0, -1 exception (in *exc), -2 not an object.
extern "C" int32_t rjs_has_own_v(void* g, uint64_t obj, uint64_t key, uint64_t* exc)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return -2;
    Identifier pk = JSValue::decode(key).toPropertyKey(go);
    if (takeException(vm, exc))
        return -1;
    bool r = asObject(base)->hasOwnProperty(go, pk);
    if (takeException(vm, exc))
        return -1;
    return r ? 1 : 0;
}

// CreateDataProperty with a VALUE key (index keys land as indices).
extern "C" int32_t rjs_define_v(void* g, uint64_t obj, uint64_t key, uint64_t val)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    Identifier pk = JSValue::decode(key).toPropertyKey(go);
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    asObject(base)->putDirectMayBeIndex(go, pk, JSValue::decode(val));
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    return RJS_OK;
}

// An OWN DATA property: 1 (+ value), 0 absent or an accessor, -1 exception.
// HTML's Error serialization reads `message` this way ("if IsDataDescriptor").
extern "C" int32_t rjs_get_own_data(void* g, uint64_t obj, void* atom, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return 0;
    JSObject* o = asObject(base);
    const Identifier& id = *static_cast<Identifier*>(atom);
    PropertySlot slot(o, PropertySlot::InternalMethodType::GetOwnProperty);
    bool has = o->methodTable()->getOwnPropertySlot(o, go, id, slot);
    if (takeException(vm, out))
        return -1;
    if (!has || slot.isAccessor())
        return 0;
    JSValue r = slot.getValue(go, id);
    if (takeException(vm, out))
        return -1;
    *out = JSValue::encode(r);
    return 1;
}

// A JS string from Latin-1 bytes (not UTF-8: every byte is one code unit).
extern "C" int32_t rjs_string_latin1(void* g, const uint8_t* p, size_t len, uint64_t* out)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    if (!len) {
        *out = JSValue::encode(jsEmptyString(vm));
        return RJS_OK;
    }
    *out = JSValue::encode(jsString(vm, WTF::String(std::span<const Latin1Character> { reinterpret_cast<const Latin1Character*>(p), len })));
    return RJS_OK;
}

// A view's [[ViewedArrayBuffer]] (materialised for a fast typed array — the
// serializer needs the buffer's identity, two views on one buffer stay one).
extern "C" int32_t rjs_view_buffer(void* g, uint64_t v, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    auto* view = dynamicDowncast<JSArrayBufferView>(JSValue::decode(v));
    if (!view)
        return RJS_BAD_ARG;
    JSArrayBuffer* b = view->possiblySharedJSBuffer(go);
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    if (!b)
        return RJS_BAD_ARG;
    *out = JSValue::encode(JSValue(b));
    return RJS_OK;
}

// 1 (+ length) when `v` is a JSArray whose own enumerable properties are
// exactly its indices 0..length-1 — no holes, no accessors, no extra named
// properties — so it can go over the wire as a bare value list.
extern "C" int32_t rjs_array_dense(void* g, uint64_t v, uint32_t* lenOut)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue val = JSValue::decode(v);
    if (!isJSArray(val))
        return 0;
    JSArray* a = downcast<JSArray>(val);
    uint32_t len = a->length();
    for (uint32_t i = 0; i < len; i++) {
        if (!a->canGetIndexQuickly(i))
            return 0;
    }
    PropertyNameArrayBuilder names(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
    a->getOwnNonIndexPropertyNames(go, names, DontEnumPropertiesMode::Exclude);
    if (takeException(vm, nullptr))
        return 0;
    for (const auto& id : names) {
        (void)id;
        return 0;
    }
    *lenOut = len;
    return 1;
}

// HTML's property step — "if ! HasOwnProperty(value, key) is true, then let
// inputValue be ? value.[[Get]](key, value)" — as ONE own lookup. Only for an
// ordinary object or an Array (proxies never get here), where the two are the
// same thing: 1 present (*out = the value; a getter has run), 0 absent,
// -1 exception (in *out).
extern "C" int32_t rjs_own_get_v(void* g, uint64_t obj, uint64_t key, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return 0;
    JSObject* o = asObject(base);
    Identifier pk = JSValue::decode(key).toPropertyKey(go);
    if (takeException(vm, out))
        return -1;
    PropertySlot slot(o, PropertySlot::InternalMethodType::Get);
    bool has = o->methodTable()->getOwnPropertySlot(o, go, pk, slot);
    if (takeException(vm, out))
        return -1;
    if (!has)
        return 0;
    JSValue r = slot.getValue(go, pk);
    if (takeException(vm, out))
        return -1;
    *out = JSValue::encode(r);
    return 1;
}

// rjs_own_get_v for an array-index key, without making a string of it; an
// Array's plain element is read straight out of its butterfly.
extern "C" int32_t rjs_own_get_index(void* g, uint64_t obj, uint32_t index, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return 0;
    JSObject* o = asObject(base);
    if (auto* a = dynamicDowncast<JSArray>(o)) {
        if (a->canGetIndexQuickly(index)) {
            *out = JSValue::encode(a->getIndexQuickly(index));
            return 1;
        }
    }
    PropertySlot slot(o, PropertySlot::InternalMethodType::Get);
    bool has = o->methodTable()->getOwnPropertySlotByIndex(o, go, index, slot);
    if (takeException(vm, out))
        return -1;
    if (!has)
        return 0;
    JSValue r = slot.getValue(go, static_cast<uint64_t>(index));
    if (takeException(vm, out))
        return -1;
    *out = JSValue::encode(r);
    return 1;
}

// CreateDataProperty(obj, index, val) — never a setter on the prototype chain.
extern "C" int32_t rjs_define_index(void* g, uint64_t obj, uint32_t index, uint64_t val)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    asObject(base)->putDirectIndex(go, index, JSValue::decode(val));
    if (takeException(vm, nullptr))
        return RJS_EXCEPTION;
    return RJS_OK;
}

// The VM's soft stack limit on this thread (0 when it has none yet). The
// serializer recurses natively and stops where JS would: a graph too deep to
// walk is a RangeError, as in a browser, never a native stack overflow.
extern "C" uintptr_t rjs_stack_soft_limit(void* g)
{
    VM& vm = G(g)->vm();
    RjsLock lock(vm);
    return reinterpret_cast<uintptr_t>(vm.softStackLimit());
}

// ---------------------------------------------------------------------------
// Memory release (level transitions / background): full GC + return free
// malloc pages to the OS (bmalloc scavenge).
// ---------------------------------------------------------------------------

#include <wtf/FastMalloc.h>

// ---------------------------------------------------------------------------
// JSC's own timers.
//
// The incremental sweeper, the eden/full GC activity callbacks and the
// stop-if-necessary timer are JSRunLoopTimers: RunLoop timers on the thread
// that created the VM. This prebuilt's RunLoop (USE_BUN_EVENT_LOOP, with
// Bun's hooks left at their defaults) is the GENERIC one, whose timers fire
// only when something runs that RunLoop — and nothing did. So no timer-driven
// collection ever ran: the heap grew by allocation-triggered eden collections,
// was never swept in the background, and never shrank. The Argon client sat
// at a 280 MB GC heap capacity for 85 MB of live objects after two idle
// minutes; one full collection took it to 110 MB and 175 MB of RAM went back
// (skathi research/reports/m26/perf-mem-2.md).
//
// One non-blocking turn (RunMode::Iterate: the due timers and the dispatched
// functions, then return) from the pump, between waves, is how a host that
// owns the loop services them; the timers then decide WHEN to collect and
// sweep, exactly as in Safari or Bun.
// ---------------------------------------------------------------------------

#include <wtf/RunLoop.h>

extern "C" void rjs_run_loop_cycle()
{
    WTF::RunLoop::cycle();
}

#include <JavaScriptCore/GCActivityCallback.h>
#include <JavaScriptCore/IncrementalSweeper.h>

// Seconds until the first of `g`'s GC timers fires, -1 when none is armed: an
// embedder that stops pumping while idle must still pump for them.
extern "C" double rjs_gc_timers_due_s(void* g)
{
    Heap& heap = G(g)->vm().heap;
    JSRunLoopTimer* timers[] = { heap.fullActivityCallback(), heap.edenActivityCallback(), &heap.sweeper() };
    double due = -1;
    for (JSRunLoopTimer* timer : timers) {
        if (!timer)
            continue;
        if (auto left = timer->timeUntilFire(); left && (due < 0 || left->seconds() < due))
            due = std::max(left->seconds(), 0.0);
    }
    return due;
}

// rjs_release_memory, after throwing away all compiled code (bytecode, JIT)
// that is not running — what JSC's VM::shrinkFootprintWhenIdle does. Every
// function compiles again from its source the next time it is called. A
// measuring tool: what is left after it is the program's DATA, and the
// difference is what its CODE held.
extern "C" void rjs_release_memory_and_code(void* g)
{
    VM& vm = G(g)->vm();
    {
        RjsLock lock(vm);
        vm.deleteAllCode(DeleteAllCodeIfNotCollecting);
        vm.heap.collectNow(Sync, CollectionScope::Full);
        vm.heap.collectNow(Sync, CollectionScope::Full);
    }
    WTF::releaseFastMallocFreeMemory();
}

extern "C" void rjs_release_memory(void* g)
{
    VM& vm = G(g)->vm();
    {
        RjsLock lock(vm);
        vm.heap.collectNow(Sync, CollectionScope::Full);
        vm.heap.collectNow(Sync, CollectionScope::Full); // finalize weaks/finalizers freed by pass 1
    }
    WTF::releaseFastMallocFreeMemory();
}

// ---------------------------------------------------------------------------
// JSON-shaped values, for embedders that exchange values with the meaning a
// JSON round trip gives them (JSON.stringify on the way in, JSON.parse on the
// way out) without the text: one value as JSON.stringify sees it, an object's
// fields by atom, and a whole value built from a flat encoding.
// ---------------------------------------------------------------------------

#include <JavaScriptCore/ArrayConstructor.h>
#include <JavaScriptCore/JSONObject.h>
#include <JavaScriptCore/ProxyObject.h>
#include <wtf/text/ASCIIFastPath.h>
#include <unordered_map>

enum RjsJsonKind : uint32_t {
    RJS_JSON_ABSENT = 0,
    RJS_JSON_NULL = 1,
    RJS_JSON_FALSE = 2,
    RJS_JSON_TRUE = 3,
    RJS_JSON_NUMBER = 4,
    RJS_JSON_STRING8 = 5,
    RJS_JSON_STRING16 = 6,
    RJS_JSON_ARRAY = 7,
    RJS_JSON_OBJECT = 8,
    RJS_JSON_PROXY = 9,
    RJS_JSON_BIGINT = 10,
    RJS_JSON_UNREAD = 11,
};

struct RjsJsonNode {
    uint64_t value;
    double number;
    const void* ptr;
    uint32_t len;
    uint32_t kind;
};

namespace {

void jsonString(JSGlobalObject* go, JSString* s, RjsJsonNode* out)
{
    out->value = JSValue::encode(s);
    out->kind = RJS_JSON_STRING8;
    auto held = s->value(go);
    const WTF::String& w = held;
    auto* impl = w.impl();
    if (!impl || !impl->length())
        return;
    out->len = impl->length();
    if (impl->is8Bit())
        out->ptr = impl->span8().data();
    else {
        out->kind = RJS_JSON_STRING16;
        out->ptr = impl->span16().data();
    }
}

void jsonNumber(double d, RjsJsonNode* out)
{
    if (std::isfinite(d)) {
        out->kind = RJS_JSON_NUMBER;
        out->number = d;
    } else
        out->kind = RJS_JSON_NULL;
}

// SerializeJSONProperty after toJSON: what the value is written as.
int32_t jsonClassify(JSGlobalObject* go, VM& vm, JSValue val, RjsJsonNode* out)
{
    out->value = JSValue::encode(val);
    if (!val || val.isUndefined() || val.isSymbol())
        return out->kind = RJS_JSON_ABSENT, RJS_OK;
    if (val.isNull())
        return out->kind = RJS_JSON_NULL, RJS_OK;
    if (val.isBoolean())
        return out->kind = val.asBoolean() ? RJS_JSON_TRUE : RJS_JSON_FALSE, RJS_OK;
    if (val.isNumber())
        return jsonNumber(val.asNumber(), out), RJS_OK;
    if (val.isString()) {
        jsonString(go, asString(val), out);
        return takeException(vm, &out->value) ? RJS_EXCEPTION : RJS_OK;
    }
    if (val.isBigInt())
        return out->kind = RJS_JSON_BIGINT, RJS_OK;
    if (!val.isObject())
        return out->kind = RJS_JSON_OBJECT, RJS_OK;
    JSObject* o = asObject(val);
    if (o->isCallable())
        return out->kind = RJS_JSON_ABSENT, RJS_OK;
    switch (o->type()) {
    case NumberObjectType: {
        double d = val.toNumber(go);
        if (takeException(vm, &out->value))
            return RJS_EXCEPTION;
        return jsonNumber(d, out), RJS_OK;
    }
    case StringObjectType:
    case DerivedStringObjectType: {
        JSString* s = val.toString(go);
        if (takeException(vm, &out->value))
            return RJS_EXCEPTION;
        jsonString(go, s, out);
        return takeException(vm, &out->value) ? RJS_EXCEPTION : RJS_OK;
    }
    case BooleanObjectType:
        return out->kind = uncheckedDowncast<BooleanObject>(o)->internalValue().asBoolean() ? RJS_JSON_TRUE : RJS_JSON_FALSE, RJS_OK;
    case ProxyObjectType: {
        bool array = isArray(go, val);
        if (takeException(vm, &out->value))
            return RJS_EXCEPTION;
        return out->kind = array ? RJS_JSON_ARRAY : RJS_JSON_PROXY, RJS_OK;
    }
    default:
        break;
    }
    if (auto* big = dynamicDowncast<BigIntObject>(o)) {
        out->value = JSValue::encode(big->internalValue());
        return out->kind = RJS_JSON_BIGINT, RJS_OK;
    }
    out->kind = isJSArray(val) ? RJS_JSON_ARRAY : RJS_JSON_OBJECT;
    return RJS_OK;
}

// `val.toJSON(key)` in place of `val` when it has one; `key()` makes the key.
template<typename Key>
int32_t jsonToJSON(JSGlobalObject* go, VM& vm, JSValue& val, void* toJSON, const Key& key, RjsJsonNode* out)
{
    JSValue fn = val.get(go, *static_cast<Identifier*>(toJSON));
    if (takeException(vm, &out->value))
        return RJS_EXCEPTION;
    if (!fn.isCallable())
        return RJS_OK;
    auto callData = JSC::getCallData(fn);
    MarkedArgumentBuffer args;
    args.append(key());
    NakedPtr<Exception> exc;
    val = JSC::call(go, fn, callData, val, args, exc);
    if (exc) {
        out->value = JSValue::encode(exc->value());
        return RJS_EXCEPTION;
    }
    return RJS_OK;
}

} // namespace

// `v` as JSON.stringify writes it: toJSON called with `key` (a string value),
// else `keyAtom`'s name, else the string of `index`; then unboxed and classified.
extern "C" int32_t rjs_json_node(void* g, uint64_t v, void* toJSON, uint64_t key, void* keyAtom, uint32_t index, RjsJsonNode* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    *out = RjsJsonNode {};
    JSValue val = JSValue::decode(v);
    if (val.isObject() || val.isBigInt()) {
        int32_t r = jsonToJSON(go, vm, val, toJSON, [&]() -> JSValue {
            if (key)
                return JSValue::decode(key);
            if (keyAtom)
                return jsString(vm, static_cast<Identifier*>(keyAtom)->string());
            return jsString(vm, String::number(index));
        }, out);
        if (r != RJS_OK)
            return r;
    }
    return jsonClassify(go, vm, val, out);
}

// Elements `start`.. of an array whose length was `len`, each as rjs_json_node
// makes it with its index. Stops at `len`, after `cap` elements, or after the
// first element that may have run JS (an object or a BigInt, or any element of
// an array that is not plain), so nothing is read ahead of what JS could change.
// `count` is how many were read; on an exception, out[0].value is what was thrown.
extern "C" int32_t rjs_json_items(void* g, uint64_t array, uint32_t start, uint32_t len, void* toJSON, RjsJsonNode* out, uint32_t cap, uint32_t* count)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    *count = 0;
    JSValue base = JSValue::decode(array);
    if (!base.isObject() || !cap)
        return RJS_BAD_ARG;
    JSObject* a = asObject(base);
    bool plain = isJSArray(a) && go->isOriginalArrayStructure(a->structure()) && go->arrayPrototypeChainIsSane();
    for (uint32_t i = start; i < len && *count < cap; ++i) {
        RjsJsonNode* node = &out[*count];
        *node = RjsJsonNode {};
        JSValue val = a->getIndex(go, i);
        if (takeException(vm, &out[0].value))
            return RJS_EXCEPTION;
        ++*count;
        bool ran = !plain;
        if (val.isObject() || val.isBigInt()) {
            ran = true;
            int32_t r = jsonToJSON(go, vm, val, toJSON, [&]() -> JSValue { return jsString(vm, String::number(i)); }, node);
            if (r == RJS_OK)
                r = jsonClassify(go, vm, val, node);
            if (r != RJS_OK) {
                out[0].value = node->value;
                return r;
            }
        } else if (int32_t r = jsonClassify(go, vm, val, node); r != RJS_OK) {
            out[0].value = node->value;
            return r;
        }
        if (ran)
            break;
    }
    return RJS_OK;
}

// An object's own enumerable properties by atom, getters run, in atom order:
// out[i] is 0 for one it does not have. A string value is also made into
// nodes[i]; anything else leaves nodes[i].kind RJS_JSON_UNREAD. RJS_BAD_ARG for
// a Proxy or a non-object, whose keys only a full walk reads right.
extern "C" int32_t rjs_json_fields(void* g, uint64_t obj, void* const* atoms, size_t n, uint64_t* out, RjsJsonNode* nodes)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JSValue base = JSValue::decode(obj);
    if (!base.isObject())
        return RJS_BAD_ARG;
    JSObject* o = asObject(base);
    if (o->type() == ProxyObjectType)
        return RJS_BAD_ARG;
    for (size_t i = 0; i < n; ++i) {
        const Identifier& id = *static_cast<Identifier*>(atoms[i]);
        PropertySlot slot(o, PropertySlot::InternalMethodType::GetOwnProperty);
        bool has = o->methodTable()->getOwnPropertySlot(o, go, id, slot);
        if (takeException(vm, out))
            return RJS_EXCEPTION;
        if (!has || (slot.attributes() & PropertyAttribute::DontEnum)) {
            out[i] = 0;
            continue;
        }
        JSValue r = slot.getValue(go, id);
        if (takeException(vm, out))
            return RJS_EXCEPTION;
        out[i] = JSValue::encode(r);
        nodes[i] = RjsJsonNode {};
        nodes[i].kind = RJS_JSON_UNREAD;
        if (r.isString()) {
            jsonString(go, asString(r), &nodes[i]);
            if (takeException(vm, out))
                return RJS_EXCEPTION;
        }
    }
    return RJS_OK;
}

namespace {

enum : uint8_t {
    BUILD_NULL = 0,
    BUILD_FALSE = 1,
    BUILD_TRUE = 2,
    BUILD_NUMBER = 3,
    BUILD_STRING = 4,
    BUILD_JSON = 5,
    BUILD_BYTES = 6,
    BUILD_ARRAY = 7,
    BUILD_OBJECT = 8,
    BUILD_FIELD = 9,
    BUILD_ENTRY = 10,
};

struct FieldAtom {
    Identifier id;
    std::optional<uint32_t> index;
};

struct FieldKeyHash {
    size_t operator()(const std::pair<uintptr_t, size_t>& k) const { return k.first * 31 + k.second; }
};

// Field names are `&'static str`s of the program: their atoms are made once
// per VM and kept for good (an Identifier must not outlive its VM's lock).
struct FieldAtoms {
    VM* vm { nullptr };
    std::unordered_map<std::pair<uintptr_t, size_t>, FieldAtom*, FieldKeyHash> map;
};

const FieldAtom& fieldAtom(VM& vm, const char* p, size_t n)
{
    static thread_local FieldAtoms* atoms = nullptr;
    if (!atoms || atoms->vm != &vm) {
        atoms = new FieldAtoms;
        atoms->vm = &vm;
    }
    auto key = std::make_pair(reinterpret_cast<uintptr_t>(p), n);
    auto it = atoms->map.find(key);
    if (it != atoms->map.end())
        return *it->second;
    auto s = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(p), n });
    auto* atom = new FieldAtom { Identifier::fromString(vm, s), std::nullopt };
    atom->index = parseIndex(atom->id);
    atoms->map.emplace(key, atom);
    return *atom;
}

struct JsonBuilder {
    JSGlobalObject* go;
    VM& vm;
    const uint8_t* p;
    const uint8_t* end;
    JSValue blobs;
    // Short strings met in this build, reused: one cell for every "ok".
    struct Recent {
        const uint8_t* bytes;
        uint32_t len;
        JSString* cell;
    } recent[64] {};
    // Empty object structures by inline capacity, looked up once per build.
    Structure* empty[JSFinalObject::maxInlineCapacity + 1] {};
    // Field atoms met in this build, by the key's address.
    struct Field {
        uintptr_t ptr;
        size_t len;
        const FieldAtom* atom;
    } fields[64] {};
    // The structure an object of these field atoms, in this order, ended with: the next one is
    // made with it and filled by offset. Kept for one build, alive on this stack.
    static constexpr unsigned shapeMax = 16;
    struct Shape {
        uint32_t count;
        uintptr_t keys[shapeMax];
        Structure* structure;
        bool resolved;
        PropertyOffset offsets[shapeMax];
    } shapes[8] {};
    bool failed { false };

    const FieldAtom& field(uintptr_t ptr, size_t len)
    {
        Field& f = fields[(ptr >> 3) & 63];
        if (f.atom && f.ptr == ptr && f.len == len)
            return *f.atom;
        const FieldAtom& atom = fieldAtom(vm, reinterpret_cast<const char*>(ptr), len);
        f = Field { ptr, len, &atom };
        return atom;
    }

    Shape& shapeSlot(const uintptr_t* keys, uint32_t count)
    {
        uintptr_t h = count;
        for (uint32_t i = 0; i < count; ++i)
            h = h * 31 + (keys[i] >> 3);
        return shapes[h & 7];
    }

    JSObject* emptyObject(uint32_t count)
    {
        unsigned capacity = std::min<unsigned>(std::max<unsigned>(count, 1), JSFinalObject::maxInlineCapacity);
        if (!empty[capacity])
            empty[capacity] = go->structureCache().emptyObjectStructureForPrototype(go, go->objectPrototype(), capacity);
        return constructEmptyObject(vm, empty[capacity]);
    }

    // An object of `count` FIELD keys that are not indices, values read first; null when the
    // encoding has anything else, with `p` back where it was.
    JSObject* shaped(uint32_t count)
    {
        const uint8_t* start = p;
        uintptr_t keys[shapeMax];
        const FieldAtom* atoms[shapeMax];
        JSValue values[shapeMax];
        for (uint32_t i = 0; i < count; ++i) {
            if (p >= end || *p != BUILD_FIELD) {
                p = start;
                return nullptr;
            }
            ++p;
            auto ptr = take<uintptr_t>();
            auto len = take<size_t>();
            if (failed)
                return nullptr;
            const FieldAtom& atom = field(ptr, len);
            if (atom.index) {
                p = start;
                return nullptr;
            }
            keys[i] = reinterpret_cast<uintptr_t>(&atom);
            atoms[i] = &atom;
            values[i] = value();
            if (failed)
                return nullptr;
        }
        Shape& shape = shapeSlot(keys, count);
        if (shape.structure && shape.count == count && std::equal(keys, keys + count, shape.keys)) {
            // The offsets are looked up when the shape is met a second time.
            for (uint32_t i = 0; i < count && !shape.resolved; ++i) {
                shape.offsets[i] = shape.structure->get(vm, atoms[i]->id);
                if (!isValidOffset(shape.offsets[i]))
                    shape.structure = nullptr;
            }
            shape.resolved = true;
            if (shape.structure) {
                JSObject* o = constructEmptyObject(vm, shape.structure);
                for (uint32_t i = 0; i < count; ++i)
                    o->putDirectOffset(vm, shape.offsets[i], values[i]);
                return o;
            }
        }
        JSObject* o = emptyObject(count);
        for (uint32_t i = 0; i < count; ++i)
            o->putDirect(vm, atoms[i]->id, values[i]);
        Structure* structure = o->structure();
        if (structure->isDictionary() || structure->outOfLineCapacity())
            return o;
        shape.count = count;
        shape.structure = structure;
        shape.resolved = false;
        std::copy(keys, keys + count, shape.keys);
        return o;
    }

    template<typename T> T take()
    {
        T v;
        if (end - p < static_cast<ptrdiff_t>(sizeof(T))) {
            failed = true;
            return T {};
        }
        std::memcpy(&v, p, sizeof(T));
        p += sizeof(T);
        return v;
    }

    std::span<const uint8_t> bytes()
    {
        uint32_t n = take<uint32_t>();
        if (failed || end - p < static_cast<ptrdiff_t>(n)) {
            failed = true;
            return {};
        }
        std::span<const uint8_t> s { p, n };
        p += n;
        return s;
    }

    JSString* string(std::span<const uint8_t> s)
    {
        if (s.empty())
            return jsEmptyString(vm);
        if (s.size() == 1 && s[0] < 0x80)
            return vm.smallStrings.singleCharacterString(s[0]);
        Recent* slot = nullptr;
        if (s.size() <= 24) {
            uint32_t h = 2166136261u;
            for (uint8_t c : s)
                h = (h ^ c) * 16777619u;
            slot = &recent[h & 63];
            if (slot->cell && slot->len == s.size() && !std::memcmp(slot->bytes, s.data(), s.size()))
                return slot->cell;
        }
        auto latin1 = std::span<const Latin1Character> { reinterpret_cast<const Latin1Character*>(s.data()), s.size() };
        JSString* cell = charactersAreAllASCII(latin1)
            ? jsString(vm, WTF::String(latin1))
            : jsString(vm, WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(s.data()), s.size() }));
        if (slot)
            *slot = Recent { s.data(), static_cast<uint32_t>(s.size()), cell };
        return cell;
    }

    JSValue value()
    {
        if (failed || p >= end) {
            failed = true;
            return {};
        }
        switch (*p++) {
        case BUILD_NULL:
            return jsNull();
        case BUILD_FALSE:
            return jsBoolean(false);
        case BUILD_TRUE:
            return jsBoolean(true);
        case BUILD_NUMBER:
            return jsNumber(take<double>());
        case BUILD_STRING:
            return string(bytes());
        case BUILD_JSON: {
            auto s = bytes();
            if (failed)
                return {};
            JSValue v = JSONParseWithException(go, WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(s.data()), s.size() }));
            if (!v)
                failed = true;
            return v;
        }
        case BUILD_BYTES: {
            uint32_t i = take<uint32_t>();
            if (failed || !isJSArray(blobs)) {
                failed = true;
                return {};
            }
            return blobs.get(go, i);
        }
        case BUILD_ARRAY: {
            uint32_t count = take<uint32_t>();
            if (failed)
                return {};
            if (!count)
                return constructEmptyArray(go, nullptr);
            MarkedArgumentBuffer items;
            for (uint32_t i = 0; i < count; ++i) {
                JSValue v = value();
                if (failed)
                    return {};
                items.append(v);
            }
            JSArray* a = items.hasOverflowed() ? nullptr : constructArray(go, go->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), items);
            if (!a)
                failed = true;
            return a;
        }
        case BUILD_OBJECT: {
            uint32_t count = take<uint32_t>();
            if (failed)
                return {};
            if (count && count <= shapeMax) {
                if (JSObject* o = shaped(count))
                    return o;
                if (failed)
                    return {};
            }
            JSObject* o = emptyObject(count);
            for (uint32_t i = 0; i < count && !failed; ++i) {
                if (p >= end) {
                    failed = true;
                    break;
                }
                uint8_t tag = *p++;
                if (tag == BUILD_FIELD) {
                    auto ptr = take<uintptr_t>();
                    auto len = take<size_t>();
                    if (failed)
                        break;
                    const FieldAtom& atom = field(ptr, len);
                    JSValue v = value();
                    if (failed)
                        break;
                    if (atom.index)
                        o->putDirectIndex(go, *atom.index, v);
                    else
                        o->putDirect(vm, atom.id, v);
                } else if (tag == BUILD_ENTRY) {
                    auto s = bytes();
                    if (failed)
                        break;
                    auto name = WTF::String::fromUTF8(std::span<const char8_t> { reinterpret_cast<const char8_t*>(s.data()), s.size() });
                    JSValue v = value();
                    if (failed)
                        break;
                    o->putDirectMayBeIndex(go, Identifier::fromString(vm, name), v);
                } else
                    failed = true;
            }
            return failed ? JSValue() : JSValue(o);
        }
        default:
            failed = true;
            return {};
        }
    }
};

} // namespace

// A value from the flat encoding the Rust side writes (tags above; numbers,
// lengths and counts little-endian; a field key the address and length of a
// string that lives as long as the process). `blobs`: an array whose element
// i is what BYTES i stands for.
extern "C" int32_t rjs_json_build(void* g, const uint8_t* code, size_t len, uint64_t blobs, uint64_t* out)
{
    JSGlobalObject* go = G(g);
    VM& vm = go->vm();
    RjsLock lock(vm);
    JsonBuilder b { go, vm, code, code + len, JSValue::decode(blobs) };
    JSValue v = b.value();
    if (takeException(vm, out))
        return RJS_EXCEPTION;
    if (b.failed || !v)
        return RJS_BAD_ARG;
    *out = JSValue::encode(v);
    return RJS_OK;
}