redjs 0.3.1

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
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
//! The structured serializer: HTML §2.7 `StructuredSerializeInternal` /
//! `StructuredDeserialize` (+ the `WithTransfer` pair), writing BYTES.
//!
//! [`serialize`] turns a JS value into a [`Serialized`] — a `Vec<u8>` plus an
//! attachment table — that holds no JSC value and no pointer into any heap. It
//! is `Send`: bytes made on one thread decode in a context on another (the
//! Worker seam), and [`deserialize_storage`] reads bytes that came back from
//! disk (the IndexedDB seam). Nothing here keeps process-global JS state; the
//! per-context intrinsics live in a registry keyed by context and dropped by
//! [`forget`].
//!
//! `structuredClone` and `BroadcastChannel.postMessage` are both built on it.
//!
//! # Brands, not `instanceof`
//!
//! HTML branches on internal slots ([[DateValue]], [[MapData]], [[ErrorData]],
//! [[ArrayBufferData]], …). Those are read from the cell's own type
//! (`rjs_brand`), so a forged `Symbol.toStringTag` or a swapped prototype
//! changes nothing, and nothing user-visible runs except what the spec itself
//! runs: property getters, `name` on an Error, `ToString` of a message, and —
//! for `structuredClone`'s `transfer` — the iterator protocol. Map/Set
//! contents are read through `Map.prototype.entries` / `Set.prototype.values`
//! and the iterator `next` CAPTURED AT INSTALL, so overriding them later does
//! not reach in either.
//!
//! # The wire format (version 1, frozen)
//!
//! ```text
//! header   "RJS"  u8 version (=1)  u8 flags (bit 0 = written for storage)
//! body     exactly one value; trailing bytes = corrupt
//! varint   unsigned LEB128 (≤ 10 bytes)        f64  8 bytes little-endian
//! string   0x08 varint n, n Latin-1 bytes  |  0x09 varint n, n UTF-16LE units
//! ```
//!
//! | tag  | value                    | payload                                              |
//! |------|--------------------------|------------------------------------------------------|
//! | 0x01 | undefined                |                                                      |
//! | 0x02 | null                     |                                                      |
//! | 0x03 | false                    |                                                      |
//! | 0x04 | true                     |                                                      |
//! | 0x05 | int32                    | varint zigzag                                        |
//! | 0x06 | double                   | f64 (NaN is canonicalised on read)                   |
//! | 0x07 | BigInt                   | varint n, n bytes of decimal ASCII (`-?[0-9]+`)      |
//! | 0x08 | string, Latin-1          | see above                                            |
//! | 0x09 | string, UTF-16           | see above (lone surrogates survive)                  |
//! | 0x0A | key: array index         | varint (key position only; ≤ 2^32 − 2)               |
//! | 0x0F | end of a list            |                                                      |
//! | 0x10 | reference                | varint object id                                     |
//! | 0x11 | Object                   | (key value)* 0x0F                                    |
//! | 0x12 | Array                    | varint length, (key value)* 0x0F                     |
//! | 0x13 | —                        | reserved                                             |
//! | 0x14 | Date                     | f64 [[DateValue]]                                    |
//! | 0x15 | RegExp                   | string source, string flags                          |
//! | 0x16 | Map                      | (key value)* 0x0F                                    |
//! | 0x17 | Set                      | value* 0x0F                                          |
//! | 0x18 | Boolean object           | u8                                                   |
//! | 0x19 | Number object            | f64                                                  |
//! | 0x1A | String object            | string                                               |
//! | 0x1B | BigInt object            | as 0x07 without the tag                              |
//! | 0x1C | Error                    | u8 name, u8 mask (1 message, 2 stack, 4 cause), [string], [string], [value] |
//! | 0x20 | ArrayBuffer              | varint n, n bytes                                    |
//! | 0x21 | resizable ArrayBuffer    | varint n, varint max, n bytes                        |
//! | 0x22 | view                     | u8 kind, varint byteOffset, varint length+1 (0 = length-tracking), buffer value |
//! | 0x30 | Blob (inline)            | string type, varint n, n bytes                       |
//! | 0x31 | File (inline)            | string type, varint n, n bytes, string name, f64 lastModified, string webkitRelativePath |
//! | 0x32 | FileList                 | varint n, n File values (0x31 or 0x45)               |
//! | 0x33 | ImageData                | varint w, varint h, u8 colorSpace (0 srgb), data value |
//! | 0x34 | DOMPoint(ReadOnly)       | u8 read-only, 4 × f64 (x y z w)                      |
//! | 0x35 | DOMRect(ReadOnly)        | reserved                                             |
//! | 0x36 | DOMQuad                  | reserved                                             |
//! | 0x37 | DOMMatrix(ReadOnly)      | u8 read-only, u8 is2D, 6 × f64 (a b c d e f) or 16 × f64 (m11…m44) |
//! | 0x38 | DOMException             | u8 kind (0, 1 = QuotaExceededError), string name, string message, u8 mask (1 stack, 2 quota, 4 requested), [string], [f64], [f64] |
//! | 0x39 | host platform object     | string interface, varint n, n bytes (the host's own encoding; see [`HostCodec`]) |
//! | 0x3A–0x3F | —                   | reserved (platform objects)                          |
//! | 0x40 | transferred ArrayBuffer  | varint attachment                                    |
//! | 0x41 | MessagePort              | reserved — varint attachment                         |
//! | 0x42 | ImageBitmap              | varint attachment                                    |
//! | 0x43 | Blob by reference        | varint attachment                                    |
//! | 0x44 | SharedArrayBuffer        | reserved — varint attachment                         |
//! | 0x45 | File by reference        | varint attachment                                    |
//! | 0x46–0x4F | —                   | reserved (attachments)                               |
//!
//! Error names: 0 Error, 1 EvalError, 2 RangeError, 3 ReferenceError,
//! 4 SyntaxError, 5 TypeError, 6 URIError (HTML's list; anything else is
//! "Error"). View kinds are `RjsTaKind`: 1 Int8 … 11 BigUint64, 12 DataView,
//! 13 Float16.
//!
//! **Identity.** Every object tag (0x11–0x39) takes the next object id AT THE
//! TAG, before its children, in the writer and the reader alike, so a later
//! 0x10 can point at an object still being written — cycles. Attachment tags
//! (0x4x) take no id: the same attachment index IS the same object (the
//! reader caches by index).
//!
//! **Attachments.** Indices run over the transfer list first (in its order),
//! then the extras: Blobs and Files by reference (the bytes are an `Arc` or a
//! path, never copied or read) and ImageBitmap copies. Storage bytes have no
//! attachment table, so [`deserialize_storage`] rejects every 0x4x tag; a Blob
//! or File written for storage goes inline (0x30 / 0x31), an ImageBitmap is a
//! DataCloneError (HTML: `forStorage`).
//!
//! **Path-backed Blobs for storage.** Inline means the bytes: a File the host
//! made from disk is READ when it is serialized for storage — the snapshot a
//! browser's IndexedDB takes too — under the same snapshot rule as any other
//! read, so a file that changed since it was picked fails the serialization
//! with `NotReadableError` (IndexedDB's `put()` throws it). The stored copy
//! is then independent of the file. The read is synchronous (A-F2).
//!
//! # Transfer
//!
//! HTML's order exactly: validate the list (transferable, not shared, no
//! duplicates) → serialize, with each listed object standing for its own
//! attachment → then per item: still not detached, take the data, detach. A
//! throw anywhere before that last phase leaves every listed buffer intact.
//! P0 copies a transferred buffer's bytes on the way out and again on the way
//! in; moving the JSC `ArrayBufferContents` instead is the Workers zero-copy
//! follow-up and changes no byte of the format.
//!
//! # Depth
//!
//! The walk is native recursion, bounded by the VM's own soft stack limit on
//! the current thread: a graph too deep to walk is the RangeError a browser
//! throws, never a native overflow. A worker thread that clones deep graphs
//! needs the stack JS itself would (≥ 4 MiB).

use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};

use super::util::*;
use crate::glue::{self, brand, RjsBrandInfo};

pub const MAGIC: [u8; 3] = *b"RJS";
pub const VERSION: u8 = 1;
/// Header flag: written with `forStorage` (no attachment table).
pub const FLAG_STORAGE: u8 = 1;

/// Wire tags. Reserved ones are listed so nothing else ever takes them.
pub mod tag {
    pub const UNDEFINED: u8 = 0x01;
    pub const NULL: u8 = 0x02;
    pub const FALSE: u8 = 0x03;
    pub const TRUE: u8 = 0x04;
    pub const INT32: u8 = 0x05;
    pub const DOUBLE: u8 = 0x06;
    pub const BIGINT: u8 = 0x07;
    pub const STRING_LATIN1: u8 = 0x08;
    pub const STRING_UTF16: u8 = 0x09;
    pub const KEY_INDEX: u8 = 0x0A;
    pub const END: u8 = 0x0F;

    pub const OBJECT_REF: u8 = 0x10;
    pub const OBJECT: u8 = 0x11;
    pub const ARRAY: u8 = 0x12;
    pub const RESERVED_13: u8 = 0x13;
    pub const DATE: u8 = 0x14;
    pub const REGEXP: u8 = 0x15;
    pub const MAP: u8 = 0x16;
    pub const SET: u8 = 0x17;
    pub const BOOLEAN_OBJECT: u8 = 0x18;
    pub const NUMBER_OBJECT: u8 = 0x19;
    pub const STRING_OBJECT: u8 = 0x1A;
    pub const BIGINT_OBJECT: u8 = 0x1B;
    pub const ERROR: u8 = 0x1C;

    pub const ARRAY_BUFFER: u8 = 0x20;
    pub const RESIZABLE_ARRAY_BUFFER: u8 = 0x21;
    pub const VIEW: u8 = 0x22;

    pub const BLOB: u8 = 0x30;
    pub const FILE: u8 = 0x31;
    pub const FILE_LIST: u8 = 0x32;
    pub const IMAGE_DATA: u8 = 0x33;
    pub const DOMPOINT: u8 = 0x34;
    pub const DOMRECT: u8 = 0x35;
    pub const DOMQUAD: u8 = 0x36;
    pub const DOMMATRIX: u8 = 0x37;
    pub const DOMEXCEPTION: u8 = 0x38;
    pub const HOST_OBJECT: u8 = 0x39;

    pub const TRANSFERRED_BUFFER: u8 = 0x40;
    pub const MESSAGE_PORT: u8 = 0x41;
    pub const IMAGE_BITMAP: u8 = 0x42;
    pub const BLOB_REF: u8 = 0x43;
    pub const SHARED_BUFFER: u8 = 0x44;
    pub const FILE_REF: u8 = 0x45;
}

/// HTML's Error names, in wire order.
pub const ERROR_NAMES: [&str; 7] = ["Error", "EvalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"];

/// View constructors by `RjsTaKind` (index 0 unused).
const VIEW_CTORS: [&str; 14] = [
    "",
    "Int8Array",
    "Uint8Array",
    "Uint8ClampedArray",
    "Int16Array",
    "Uint16Array",
    "Int32Array",
    "Uint32Array",
    "Float32Array",
    "Float64Array",
    "BigInt64Array",
    "BigUint64Array",
    "DataView",
    "Float16Array",
];
const ELEM_SIZE: [u64; 14] = [0, 1, 1, 1, 2, 2, 4, 4, 4, 8, 8, 8, 1, 2];

// ---------------------------------------------------------------------------
// Public surface
// ---------------------------------------------------------------------------

pub struct Options<'a> {
    /// HTML `forStorage`: no attachments, Blobs inline, ImageBitmap refused.
    pub for_storage: bool,
    /// Objects to transfer (ArrayBuffer, ImageBitmap). The caller keeps them
    /// alive for the duration of the call.
    pub transfer: &'a [u64],
}

/// One entry of the attachment table — plain Rust data.
#[derive(Debug, Clone)]
pub enum Attachment {
    /// A transferred ArrayBuffer's bytes; `max_byte_length` for a resizable one.
    TransferredBuffer { bytes: Vec<u8>, max_byte_length: Option<u64> },
    /// An ImageBitmap's pixels (moved when transferred, copied when not).
    Bitmap { width: u32, height: u32, rgba: Vec<u8> },
    /// A Blob, by reference: the same immutable source (bytes or a path),
    /// shared — nothing is read.
    BlobRef { range: super::blob::Range, mime: String },
    /// A File, by reference: as `BlobRef`, plus what makes it a File.
    FileRef { range: super::blob::Range, mime: String, file: super::blob::FileMeta },
}

/// The output of [`serialize`]: no JSC value anywhere, hence `Send`.
#[derive(Debug)]
pub struct Serialized {
    pub bytes: Vec<u8>,
    pub attachments: Vec<Attachment>,
}

/// Serializes `v`. `Err` carries the exception VALUE (a DataCloneError
/// DOMException, or whatever a getter threw) — not yet thrown.
///
/// # Safety
/// `g` must be a live context with the web API installed, on its thread.
pub unsafe fn serialize(g: *mut c_void, v: u64, opts: &Options) -> Result<Serialized, u64> {
    let _lock = Locked::new(g);
    let Some(it) = intrinsics(g) else {
        return Err(error_value(g, 1, "structured serialization is not installed in this context"));
    };
    if opts.for_storage && !opts.transfer.is_empty() {
        return Err(dce(g, "A value serialized for storage cannot transfer anything."));
    }
    let mut e = Enc {
        g,
        it,
        out: Vec::with_capacity(64),
        ids: HashMap::new(),
        next_id: 0,
        attach_of: HashMap::new(),
        attachments: Vec::new(),
        for_storage: opts.for_storage,
        keep: KeepAlive::new(g),
        stack: StackGuard::new(g),
    };
    e.out.extend_from_slice(&MAGIC);
    e.out.push(VERSION);
    e.out.push(if opts.for_storage { FLAG_STORAGE } else { 0 });
    e.validate_transfer(opts.transfer)?;
    e.value(v, 0)?;
    e.transfer(opts.transfer)?;
    let bytes = core::mem::take(&mut e.out);
    let attachments = core::mem::take(&mut e.attachments).into_iter().map(|a| a.expect("every attachment slot is filled")).collect();
    Ok(Serialized { bytes, attachments })
}

/// Deserializes, consuming the attachments (transferred data moves in).
///
/// # Safety
/// As [`serialize`].
pub unsafe fn deserialize(g: *mut c_void, s: Serialized) -> Result<u64, u64> {
    let Serialized { bytes, attachments } = s;
    decode(g, &bytes, Source::Owned(attachments.into_iter().map(Some).collect()), false)
}

/// Deserializes without consuming — once per receiver (BroadcastChannel).
///
/// # Safety
/// As [`serialize`].
pub unsafe fn deserialize_ref(g: *mut c_void, s: &Serialized) -> Result<u64, u64> {
    decode(g, &s.bytes, Source::Borrowed(&s.attachments), false)
}

/// Deserializes bytes written with `for_storage` (from disk): any attachment
/// tag is corruption.
///
/// # Safety
/// As [`serialize`].
pub unsafe fn deserialize_storage(g: *mut c_void, bytes: &[u8]) -> Result<u64, u64> {
    decode(g, bytes, Source::None, true)
}

// ---------------------------------------------------------------------------
// Per-context intrinsics (captured at install, before any user script)
// ---------------------------------------------------------------------------

struct Intrinsics {
    date: u64,
    regexp: u64,
    map: u64,
    map_set: u64,
    map_entries: u64,
    map_next: u64,
    set: u64,
    set_add: u64,
    set_values: u64,
    set_next: u64,
    errors: [u64; 7],
    array_buffer: u64,
    views: [u64; 14],
    bigint: u64,
    object: u64,
    /// ReadableStream / WritableStream / TransformStream prototypes.
    streams: [u64; 3],
    a_name: *mut c_void,
    a_message: *mut c_void,
    a_stack: *mut c_void,
    a_cause: *mut c_void,
    a_done: *mut c_void,
    a_value: *mut c_void,
    a_next: *mut c_void,
    a_transfer: *mut c_void,
    a_iterator: *mut c_void,
}

unsafe impl Send for Intrinsics {}
unsafe impl Sync for Intrinsics {}

fn registry() -> &'static Mutex<HashMap<usize, &'static Intrinsics>> {
    static R: OnceLock<Mutex<HashMap<usize, &'static Intrinsics>>> = OnceLock::new();
    R.get_or_init(|| Mutex::new(HashMap::new()))
}

fn intrinsics(g: *mut c_void) -> Option<&'static Intrinsics> {
    registry().lock().unwrap().get(&(g as usize)).copied()
}

pub(crate) fn forget(gone: crate::context::Gone) {
    registry().lock().unwrap().remove(&gone.key);
    refused().lock().unwrap().remove(&gone.key);
}

// ---------------------------------------------------------------------------
// Platform objects a host implements in script, which are not [Serializable]
// ---------------------------------------------------------------------------
//
// A host that implements a platform interface as a script class (skathi's
// File System Access handles and its FileReader) makes ORDINARY objects, and
// the algorithm would clone them as plain objects: `{}` from a
// FileSystemFileHandle, with no error. HTML says a platform object that is not
// [Serializable] is a DataCloneError. So the host names the prototype, and
// the error names the interface, with Blink's words ("FileReader object could
// not be cloned.").
//
// A script class for an interface that IS [Serializable] (FileSystemHandle)
// names its prototype with a [`HostCodec`] instead: the instance travels as
// tag 0x39 — the interface and the host's own bytes, which no script sees —
// and the receiving context's host makes a fresh instance from them.

/// How a host serializes the instances of one script-implemented platform
/// interface. Both run on the context's thread, inside the walk; a `String`
/// error becomes the DataCloneError's message.
#[derive(Clone, Copy)]
pub struct HostCodec {
    /// The instance's bytes, opaque to this module.
    pub encode: unsafe fn(g: *mut c_void, v: u64) -> Result<Vec<u8>, String>,
    /// A new instance from bytes `encode` wrote (perhaps in another context,
    /// perhaps read back from disk).
    pub decode: unsafe fn(g: *mut c_void, bytes: &[u8]) -> Result<u64, String>,
}

/// One marked prototype: refused, or serialized by its host.
#[derive(Clone)]
struct Marked {
    proto: u64,
    interface: String,
    codec: Option<HostCodec>,
}

/// Per context, in registration order (the first one on a chain wins); the
/// prototypes protected.
fn refused() -> &'static Mutex<HashMap<usize, Vec<Marked>>> {
    static R: OnceLock<Mutex<HashMap<usize, Vec<Marked>>>> = OnceLock::new();
    R.get_or_init(|| Mutex::new(HashMap::new()))
}

/// Has any context marked a prototype? Every plain object pays one relaxed
/// load for this check until one has.
static ANY_REFUSED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Marks `proto` as the prototype of a platform interface this runtime cannot
/// serialize. Serializing a value whose prototype chain reaches it fails with
/// DataCloneError "`<interface>` object could not be cloned.", for
/// `structuredClone`, `postMessage` and IndexedDB alike.
///
/// # Safety
/// `g` must be a live context with the web API installed, on its thread.
pub unsafe fn refuse_platform_prototype(g: *mut c_void, proto: u64, interface: &str) {
    mark(g, proto, interface, None);
}

/// Marks `proto` as the prototype of a [Serializable] platform interface the
/// host implements in script: a value whose prototype chain reaches it is
/// written as tag 0x39 with `codec.encode`'s bytes, for `structuredClone`,
/// `postMessage` and IndexedDB alike, and read back with the `codec.decode`
/// the receiving context registered under the same `interface` — or a
/// DataCloneError where none did.
///
/// # Safety
/// As [`refuse_platform_prototype`].
pub unsafe fn serialize_platform_prototype(g: *mut c_void, proto: u64, interface: &str, codec: HostCodec) {
    mark(g, proto, interface, Some(codec));
}

unsafe fn mark(g: *mut c_void, proto: u64, interface: &str, codec: Option<HostCodec>) {
    if !is_cell(proto) {
        return;
    }
    crate::ffi::redjs_protect(g, proto);
    let m = Marked { proto, interface: interface.to_string(), codec };
    refused().lock().unwrap().entry(g as usize).or_default().push(m);
    ANY_REFUSED.store(true, std::sync::atomic::Ordering::Release);
}

/// The marked interface `v` is an instance of, if any.
unsafe fn marked_interface(g: *mut c_void, v: u64) -> Option<Marked> {
    if !ANY_REFUSED.load(std::sync::atomic::Ordering::Acquire) {
        return None;
    }
    // Copied out, so the walk below runs with no lock held.
    let list = refused().lock().unwrap().get(&(g as usize))?.clone();
    list.into_iter().find(|m| proto_chain_has(g, v, m.proto))
}

/// This context's codec for `interface`, if its host registered one.
fn codec_for(g: *mut c_void, interface: &str) -> Option<HostCodec> {
    let list = refused().lock().unwrap();
    list.get(&(g as usize))?.iter().find_map(|m| if m.interface == interface { m.codec } else { None })
}

pub unsafe fn install(g: *mut c_void) {
    let global = glue::rjs_global(g);
    let keep = |v: u64| -> u64 {
        if is_cell(v) {
            crate::ffi::redjs_protect(g, v);
        }
        v
    };
    let ctor = |name: &str| keep(get(g, global, name));
    let proto_fn = |c: u64, name: &str| keep(get(g, get(g, c, "prototype"), name));
    // %MapIteratorPrototype%.next and %SetIteratorPrototype%.next: reachable
    // only through an iterator instance.
    let iter_next = |c: u64, make_iter: u64| -> u64 {
        let Ok(inst) = construct(g, c, &[]) else { return UNDEF };
        let Ok(iter) = call(g, make_iter, inst, &[]) else { return UNDEF };
        let p = crate::capi::JSObjectGetPrototype(g, iter as usize as crate::capi::JSObjectRef) as usize as u64;
        keep(get(g, p, "next"))
    };

    let map = ctor("Map");
    let set = ctor("Set");
    let map_entries = proto_fn(map, "entries");
    let set_values = proto_fn(set, "values");
    let streams = ["ReadableStream", "WritableStream", "TransformStream"].map(|n| keep(get(g, get(g, global, n), "prototype")));
    let it = Intrinsics {
        date: ctor("Date"),
        regexp: ctor("RegExp"),
        map,
        map_set: proto_fn(map, "set"),
        map_entries,
        map_next: iter_next(map, map_entries),
        set,
        set_add: proto_fn(set, "add"),
        set_values,
        set_next: iter_next(set, set_values),
        errors: ERROR_NAMES.map(|n| ctor(n)),
        array_buffer: ctor("ArrayBuffer"),
        views: core::array::from_fn(|k| if k == 0 { UNDEF } else { ctor(VIEW_CTORS[k]) }),
        bigint: ctor("BigInt"),
        object: ctor("Object"),
        streams,
        a_name: atom(g, "name"),
        a_message: atom(g, "message"),
        a_stack: atom(g, "stack"),
        a_cause: atom(g, "cause"),
        a_done: atom(g, "done"),
        a_value: atom(g, "value"),
        a_next: atom(g, "next"),
        a_transfer: atom(g, "transfer"),
        a_iterator: glue::rjs_atom_wellknown(g, 0),
    };
    registry().lock().unwrap().insert(g as usize, Box::leak(Box::new(it)));

    let n16: Vec<u16> = "structuredClone".encode_utf16().collect();
    let mut f = UNDEF;
    glue::rjs_function_create(g, n16.as_ptr(), n16.len(), 1, structured_clone as *const c_void, &mut f);
    // A WebIDL operation on the global: writable, enumerable, configurable.
    define(g, global, "structuredClone", f, 0);
}

// ---------------------------------------------------------------------------
// structuredClone(value, { transfer })
// ---------------------------------------------------------------------------

#[crate::host_fn]
unsafe fn structured_clone(g: *mut c_void, cf: *mut c_void) -> u64 {
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to execute 'structuredClone': 1 argument required, but only 0 present.");
    }
    let Some(it) = intrinsics(g) else {
        return throw_type_error(g, "structuredClone is not installed in this context");
    };
    let mut keep = KeepAlive::new(g);
    let transfer = match transfer_option(g, it, cf_arg(cf, 1), &mut keep) {
        Ok(t) => t,
        Err(e) => {
            glue::rjs_throw(g, e);
            return UNDEF;
        }
    };
    let r = serialize(g, cf_arg(cf, 0), &Options { for_storage: false, transfer: &transfer }).and_then(|s| deserialize(g, s));
    drop(keep);
    match r {
        Ok(v) => v,
        Err(e) => {
            glue::rjs_throw(g, e);
            UNDEF
        }
    }
}

/// WebIDL: `StructuredSerializeOptions` → `sequence<object> transfer`.
unsafe fn transfer_option(g: *mut c_void, it: &Intrinsics, options: u64, keep: &mut KeepAlive) -> Result<Vec<u64>, u64> {
    const WHAT: &str = "Failed to execute 'structuredClone'";
    if options == UNDEF || options == NULL {
        return Ok(Vec::new());
    }
    if !is_object(g, options) {
        return Err(error_value(g, 1, &format!("{WHAT}: The provided value is not of type 'StructuredSerializeOptions'.")));
    }
    let t = get_x(g, options, it.a_transfer)?;
    if t == UNDEF {
        return Ok(Vec::new());
    }
    let not_seq = || error_value(g, 1, &format!("{WHAT}: Failed to read the 'transfer' property from 'StructuredSerializeOptions': The provided value cannot be converted to a sequence."));
    if !is_object(g, t) {
        return Err(not_seq());
    }
    let method = get_x(g, t, it.a_iterator)?;
    if method == UNDEF || method == NULL || glue::rjs_kind(g, method) != 9 {
        return Err(not_seq());
    }
    let iter = call(g, method, t, &[])?;
    if !is_object(g, iter) {
        return Err(error_value(g, 1, "Result of the Symbol.iterator method is not an object"));
    }
    let next = get_x(g, iter, it.a_next)?;
    if glue::rjs_kind(g, next) != 9 {
        return Err(error_value(g, 1, "The iterator's 'next' method is not callable"));
    }
    let mut out = Vec::new();
    loop {
        let r = call(g, next, iter, &[])?;
        if !is_object(g, r) {
            return Err(error_value(g, 1, "Iterator result is not an object"));
        }
        let done = get_x(g, r, it.a_done)?;
        if glue::rjs_to_boolean(g, done) != 0 {
            break;
        }
        let v = get_x(g, r, it.a_value)?;
        if !is_object(g, v) {
            return Err(error_value(g, 1, &format!("{WHAT}: Failed to read the 'transfer' property from 'StructuredSerializeOptions': Iterator value is not an object.")));
        }
        keep.hold(v);
        out.push(v);
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Shared plumbing
// ---------------------------------------------------------------------------

type R<T = ()> = Result<T, u64>;

/// The VM lock for the whole walk: without an outer holder, every glue call
/// would be an outermost release — and JSC drains microtasks there, so user
/// JS could run between two reads of the same graph.
struct Locked(*mut c_void);

impl Locked {
    unsafe fn new(g: *mut c_void) -> Locked {
        glue::rjs_lock(g);
        Locked(g)
    }
}

impl Drop for Locked {
    fn drop(&mut self) {
        unsafe { glue::rjs_unlock(self.0) }
    }
}

/// Values only Rust heap memory points at (memo tables, Map entry lists),
/// rooted in one protected array for as long as the walk runs.
struct KeepAlive {
    g: *mut c_void,
    arr: u64,
    n: u32,
}

impl KeepAlive {
    unsafe fn new(g: *mut c_void) -> KeepAlive {
        let mut arr = UNDEF;
        glue::rjs_array_create(g, 0, &mut arr);
        crate::ffi::redjs_protect(g, arr);
        KeepAlive { g, arr, n: 0 }
    }

    unsafe fn hold(&mut self, v: u64) {
        if is_cell(v) {
            glue::rjs_define_index(self.g, self.arr, self.n, v);
            self.n += 1;
        }
    }
}

impl Drop for KeepAlive {
    fn drop(&mut self) {
        unsafe { crate::ffi::redjs_unprotect(self.g, self.arr) }
    }
}

/// Native-recursion bound: the VM's soft stack limit plus a margin for the
/// frames between two checks and for building the RangeError itself.
#[derive(Clone, Copy)]
struct StackGuard {
    limit: usize,
}

const STACK_MARGIN: usize = 64 * 1024;
/// Only when the VM reports no limit (never entered on this thread).
const DEPTH_FALLBACK: u32 = 2048;

impl StackGuard {
    unsafe fn new(g: *mut c_void) -> StackGuard {
        StackGuard { limit: glue::rjs_stack_soft_limit(g) }
    }

    #[inline(never)]
    fn ok(&self, depth: u32) -> bool {
        let marker = 0u8;
        let sp = core::hint::black_box(&marker) as *const u8 as usize;
        if self.limit != 0 {
            sp > self.limit.saturating_add(STACK_MARGIN)
        } else {
            depth < DEPTH_FALLBACK
        }
    }
}

unsafe fn error_value(g: *mut c_void, kind: u32, msg: &str) -> u64 {
    let m: Vec<u16> = msg.encode_utf16().collect();
    let mut e = UNDEF;
    glue::rjs_error_create(g, kind, m.as_ptr(), m.len(), &mut e);
    e
}

unsafe fn dce(g: *mut c_void, msg: &str) -> u64 {
    super::domexception::create(g, "DataCloneError", msg)
}

unsafe fn stack_overflow(g: *mut c_void) -> u64 {
    error_value(g, 2, "Maximum call stack size exceeded.")
}

unsafe fn is_object(g: *mut c_void, v: u64) -> bool {
    is_cell(v) && glue::rjs_kind(g, v) >= 8
}

/// `? Get(obj, key)` with the exception as a value.
unsafe fn get_x(g: *mut c_void, obj: u64, a: *mut c_void) -> R<u64> {
    let mut out = UNDEF;
    if glue::rjs_get(g, obj, a, &mut out) != 0 {
        return Err(out);
    }
    Ok(out)
}

unsafe fn construct(g: *mut c_void, ctor: u64, args: &[u64]) -> R<u64> {
    let mut out = UNDEF;
    match glue::rjs_construct(g, ctor, args.as_ptr(), args.len() as u32, &mut out) {
        0 => Ok(out),
        1 => Err(out),
        _ => Err(error_value(g, 1, "not a constructor")),
    }
}

/// An own DATA property's value (1), else `None`; `Err` = it threw.
unsafe fn own_data(g: *mut c_void, obj: u64, a: *mut c_void) -> R<Option<u64>> {
    let mut out = UNDEF;
    match glue::rjs_get_own_data(g, obj, a, &mut out) {
        1 => Ok(Some(out)),
        0 => Ok(None),
        _ => Err(out),
    }
}

/// A canonical array index (`"0"`, `"17"`, never `"01"`), ≤ 2^32 − 2.
fn canonical_index<T: Copy + Into<u32>>(units: &[T]) -> Option<u32> {
    let n = units.len();
    if n == 0 || n > 10 {
        return None;
    }
    let digit = |c: T| -> Option<u64> {
        let c: u32 = c.into();
        (0x30..=0x39).contains(&c).then(|| (c - 0x30) as u64)
    };
    if n > 1 && digit(units[0]) == Some(0) {
        return None;
    }
    let mut v: u64 = 0;
    for &c in units {
        v = v * 10 + digit(c)?;
    }
    (v <= 0xFFFF_FFFE).then_some(v as u32)
}

enum JsStr<'a> {
    Latin1(&'a [u8]),
    Utf16(&'a [u16]),
}

/// A JS string's storage, borrowed (valid until the next allocation).
unsafe fn js_str<'a>(g: *mut c_void, s: u64) -> Option<JsStr<'a>> {
    let mut info = crate::ffi::RjsStr::default();
    if glue::rjs_string_info(g, s, &mut info) != 0 {
        return None;
    }
    let n = info.len as usize;
    if n == 0 {
        return Some(JsStr::Latin1(&[]));
    }
    Some(if info.is8bit != 0 {
        JsStr::Latin1(core::slice::from_raw_parts(info.ptr as *const u8, n))
    } else {
        JsStr::Utf16(core::slice::from_raw_parts(info.ptr as *const u16, n))
    })
}

// ---------------------------------------------------------------------------
// Encoder
// ---------------------------------------------------------------------------

struct Enc {
    g: *mut c_void,
    it: &'static Intrinsics,
    out: Vec<u8>,
    /// HTML's memory: object → id.
    ids: HashMap<u64, u32>,
    next_id: u32,
    /// Transfer-list objects and by-reference extras → (attachment, tag).
    attach_of: HashMap<u64, (u32, u8)>,
    /// `None` = a transfer slot, filled after the walk.
    attachments: Vec<Option<Attachment>>,
    for_storage: bool,
    keep: KeepAlive,
    stack: StackGuard,
}

/// V8's text for the same failure (`DataCloneErrorOutOfMemory`).
const OUT_OF_MEMORY: &str = "Data cannot be cloned, out of memory.";

impl Enc {
    fn put(&mut self, b: u8) {
        self.out.push(b);
    }

    /// Room for `n` more bytes, or a DataCloneError: a value too large to
    /// serialize (a multi-GB ArrayBuffer, a picked video put into IndexedDB)
    /// is an exception, never an out-of-memory abort of the process.
    unsafe fn reserve(&mut self, n: u64) -> R {
        match usize::try_from(n) {
            Ok(n) if self.out.try_reserve(n).is_ok() => Ok(()),
            _ => Err(dce(self.g, OUT_OF_MEMORY)),
        }
    }

    fn varint(&mut self, mut v: u64) {
        loop {
            let b = (v & 0x7f) as u8;
            v >>= 7;
            if v == 0 {
                self.out.push(b);
                return;
            }
            self.out.push(b | 0x80);
        }
    }

    fn f64(&mut self, d: f64) {
        self.out.extend_from_slice(&d.to_le_bytes());
    }

    unsafe fn memo(&mut self, v: u64) {
        self.ids.insert(v, self.next_id);
        self.next_id += 1;
        self.keep.hold(v);
    }

    unsafe fn string(&mut self, s: u64) -> R {
        let Some(js) = js_str(self.g, s) else {
            return Err(dce(self.g, "A string could not be read."));
        };
        match js {
            JsStr::Latin1(b) => {
                self.out.push(tag::STRING_LATIN1);
                self.varint(b.len() as u64);
                self.out.extend_from_slice(b);
            }
            JsStr::Utf16(u) => {
                self.out.push(tag::STRING_UTF16);
                self.varint(u.len() as u64);
                self.out.reserve(u.len() * 2);
                for &c in u {
                    self.out.extend_from_slice(&c.to_le_bytes());
                }
            }
        }
        Ok(())
    }

    /// A Rust string (a Blob's type), Latin-1 when it fits.
    fn rust_string(&mut self, s: &str) {
        if s.chars().all(|c| (c as u32) <= 0xFF) {
            self.put(tag::STRING_LATIN1);
            self.varint(s.chars().count() as u64);
            for c in s.chars() {
                self.out.push(c as u32 as u8);
            }
        } else {
            let u: Vec<u16> = s.encode_utf16().collect();
            self.put(tag::STRING_UTF16);
            self.varint(u.len() as u64);
            for c in u {
                self.out.extend_from_slice(&c.to_le_bytes());
            }
        }
    }

    /// Decimal digits of a BigInt, untagged.
    unsafe fn bigint_digits(&mut self, b: u64) -> R {
        let mut s = UNDEF;
        if glue::rjs_to_string(self.g, b, &mut s) != 0 {
            return Err(s);
        }
        let digits: Vec<u8> = match js_str(self.g, s) {
            Some(JsStr::Latin1(d)) => d.to_vec(),
            Some(JsStr::Utf16(u)) => u.iter().map(|&c| c as u8).collect(),
            None => return Err(dce(self.g, "A BigInt could not be read.")),
        };
        self.varint(digits.len() as u64);
        self.out.extend_from_slice(&digits);
        Ok(())
    }

    unsafe fn validate_transfer(&mut self, transfer: &[u64]) -> R {
        let g = self.g;
        for (i, &t) in transfer.iter().enumerate() {
            if !is_object(g, t) {
                return Err(dce(g, &format!("Value at index {i} of the transfer list is not an object.")));
            }
            let mut info = RjsBrandInfo::default();
            let b = glue::rjs_brand(g, t, &mut info);
            let tg = if b == brand::ARRAY_BUFFER {
                tag::TRANSFERRED_BUFFER
            } else if b == brand::SHARED_ARRAY_BUFFER {
                return Err(dce(g, &format!("SharedArrayBuffer at index {i} is not transferable.")));
            } else if super::image::bitmap_of(g, t).is_some() {
                tag::IMAGE_BITMAP
            } else if self.it.streams.iter().any(|&p| proto_chain_has(g, t, p)) {
                advise_stream_transfer();
                return Err(dce(g, &format!("Value at index {i} is a stream; transferring streams is not supported in this runtime.")));
            } else {
                return Err(dce(g, &format!("Value at index {i} does not have a transferable type.")));
            };
            if self.attach_of.contains_key(&t) {
                return Err(dce(g, &format!("Value at index {i} is a duplicate of an earlier entry in the transfer list.")));
            }
            self.attach_of.insert(t, (i as u32, tg));
            self.attachments.push(None);
        }
        Ok(())
    }

    /// HTML StructuredSerializeWithTransfer step 5: per listed object, in
    /// order — still attached, take its data, detach it.
    unsafe fn transfer(&mut self, transfer: &[u64]) -> R {
        let g = self.g;
        for (i, &t) in transfer.iter().enumerate() {
            let (_, tg) = self.attach_of[&t];
            if tg == tag::TRANSFERRED_BUFFER {
                let mut info = RjsBrandInfo::default();
                glue::rjs_brand(g, t, &mut info);
                if info.flags & brand::F_DETACHED != 0 {
                    return Err(dce(g, &format!("ArrayBuffer at index {i} is already detached.")));
                }
                let n = info.byte_length as usize;
                let bytes = if n == 0 || info.data.is_null() {
                    Vec::new()
                } else {
                    core::slice::from_raw_parts(info.data as *const u8, n).to_vec()
                };
                let max_byte_length = (info.flags & brand::F_RESIZABLE != 0).then_some(info.max_byte_length);
                if glue::rjs_arraybuffer_detach(g, t) != 0 {
                    return Err(error_value(g, 1, &format!("ArrayBuffer at index {i} cannot be detached.")));
                }
                self.attachments[i] = Some(Attachment::TransferredBuffer { bytes, max_byte_length });
            } else {
                let Some(b) = super::image::bitmap_of(g, t) else {
                    return Err(dce(g, &format!("Value at index {i} does not have a transferable type.")));
                };
                if b.width == 0 || b.height == 0 {
                    return Err(dce(g, &format!("ImageBitmap at index {i} is already detached.")));
                }
                let rgba = core::mem::take(&mut b.rgba);
                let (width, height) = (b.width, b.height);
                b.width = 0;
                b.height = 0;
                self.attachments[i] = Some(Attachment::Bitmap { width, height, rgba });
            }
        }
        Ok(())
    }

    unsafe fn extra(&mut self, v: u64, tg: u8, a: Attachment) {
        let idx = self.attachments.len() as u32;
        self.attachments.push(Some(a));
        self.attach_of.insert(v, (idx, tg));
        self.keep.hold(v);
        self.put(tg);
        self.varint(idx as u64);
    }

    unsafe fn value(&mut self, v: u64, depth: u32) -> R {
        match v {
            UNDEF => self.put(tag::UNDEFINED),
            NULL => self.put(tag::NULL),
            FALSE => self.put(tag::FALSE),
            TRUE => self.put(tag::TRUE),
            _ if (v & TAG_NUMBER) == TAG_NUMBER => {
                let i = v as u32 as i32;
                self.put(tag::INT32);
                self.varint(((i << 1) ^ (i >> 31)) as u32 as u64);
            }
            _ if (v & TAG_NUMBER) != 0 => {
                self.put(tag::DOUBLE);
                self.f64(dec_f64(v).unwrap_or(f64::NAN));
            }
            _ => match glue::rjs_kind(self.g, v) {
                5 => return self.string(v),
                6 => return Err(dce(self.g, "A Symbol could not be cloned.")),
                7 => {
                    self.put(tag::BIGINT);
                    return self.bigint_digits(v);
                }
                _ if is_cell(v) => return self.object(v, depth),
                _ => return Err(dce(self.g, "This value could not be cloned.")),
            },
        }
        Ok(())
    }

    unsafe fn object(&mut self, v: u64, depth: u32) -> R {
        let g = self.g;
        if let Some(&(idx, tg)) = self.attach_of.get(&v) {
            self.put(tg);
            self.varint(idx as u64);
            return Ok(());
        }
        if let Some(&id) = self.ids.get(&v) {
            self.put(tag::OBJECT_REF);
            self.varint(id as u64);
            return Ok(());
        }
        if !self.stack.ok(depth) {
            return Err(stack_overflow(g));
        }
        let mut info = RjsBrandInfo::default();
        match glue::rjs_brand(g, v, &mut info) {
            brand::BOOLEAN_OBJECT | brand::NUMBER_OBJECT | brand::STRING_OBJECT | brand::BIGINT_OBJECT => {
                let b = info.brand;
                let mut prim = UNDEF;
                glue::rjs_wrapper_value(g, v, &mut prim);
                self.memo(v);
                match b {
                    brand::BOOLEAN_OBJECT => {
                        self.put(tag::BOOLEAN_OBJECT);
                        self.put((prim == TRUE) as u8);
                    }
                    brand::NUMBER_OBJECT => {
                        self.put(tag::NUMBER_OBJECT);
                        self.f64(dec_f64(prim).unwrap_or(f64::NAN));
                    }
                    brand::STRING_OBJECT => {
                        self.put(tag::STRING_OBJECT);
                        self.string(prim)?;
                    }
                    _ => {
                        self.put(tag::BIGINT_OBJECT);
                        self.bigint_digits(prim)?;
                    }
                }
                Ok(())
            }
            brand::DATE => {
                self.memo(v);
                self.put(tag::DATE);
                self.f64(info.number);
                Ok(())
            }
            brand::REGEXP => {
                let (mut p, mut f) = (UNDEF, UNDEF);
                glue::rjs_regexp_parts(g, v, &mut p, &mut f);
                self.memo(v);
                self.put(tag::REGEXP);
                self.string(p)?;
                self.string(f)
            }
            brand::ARRAY_BUFFER => {
                if info.flags & brand::F_DETACHED != 0 {
                    return Err(dce(g, "An ArrayBuffer is detached and could not be cloned."));
                }
                self.memo(v);
                let n = info.byte_length;
                if info.flags & brand::F_RESIZABLE != 0 {
                    self.put(tag::RESIZABLE_ARRAY_BUFFER);
                    self.varint(n);
                    self.varint(info.max_byte_length);
                } else {
                    self.put(tag::ARRAY_BUFFER);
                    self.varint(n);
                }
                if n > 0 && !info.data.is_null() {
                    self.reserve(n)?;
                    self.out.extend_from_slice(core::slice::from_raw_parts(info.data as *const u8, n as usize));
                }
                Ok(())
            }
            brand::SHARED_ARRAY_BUFFER => Err(dce(g, "A SharedArrayBuffer could not be cloned: this context is not cross-origin isolated.")),
            brand::VIEW => self.view(v, &info, depth),
            brand::MAP => self.map(v, depth),
            brand::SET => self.set(v, depth),
            brand::ERROR if info.flags & brand::F_REDJS_SLOT != 0 => self.dom_exception(v),
            brand::ERROR => self.error(v, depth),
            brand::ARRAY => {
                let len = array_len(g, v);
                self.memo(v);
                self.put(tag::ARRAY);
                self.varint(len as u64);
                let mut dense = 0u32;
                if glue::rjs_array_dense(g, v, &mut dense) == 1 {
                    for i in 0..dense {
                        self.index_prop(v, i, depth)?;
                    }
                } else {
                    self.props(v, depth)?;
                }
                self.put(tag::END);
                Ok(())
            }
            brand::PLAIN if info.flags & brand::F_REDJS_SLOT != 0 => match super::platform::image_data_parts(g, v) {
                Some((data, w, h)) => {
                    self.memo(v);
                    self.put(tag::IMAGE_DATA);
                    self.varint(w as u64);
                    self.varint(h as u64);
                    self.put(0); // "srgb"
                    self.value(data, depth + 1)
                }
                None => Err(dce(g, "This platform object could not be cloned.")),
            },
            brand::PLAIN => {
                if let Some(m) = marked_interface(g, v) {
                    return self.marked(v, m);
                }
                self.memo(v);
                self.put(tag::OBJECT);
                self.props(v, depth)?;
                self.put(tag::END);
                Ok(())
            }
            brand::OTHER => self.host_object(v, depth),
            brand::FUNCTION => Err(dce(g, "A function could not be cloned.")),
            brand::PROXY => Err(dce(g, "A Proxy could not be cloned.")),
            brand::PROMISE => Err(dce(g, "A Promise could not be cloned.")),
            brand::GLOBAL => Err(dce(g, "The global object could not be cloned.")),
            brand::SYMBOL_OBJECT => Err(dce(g, "A Symbol object could not be cloned.")),
            _ => Err(dce(g, "This object could not be cloned.")),
        }
    }

    /// HTML's property loop over a snapshot of the enumerable own keys:
    /// each one still own → `? Get` → serialize.
    unsafe fn props(&mut self, v: u64, depth: u32) -> R {
        let g = self.g;
        let mut keys = UNDEF;
        if glue::rjs_own_keys(g, v, &mut keys) != 0 {
            return Err(keys);
        }
        let n = array_len(g, keys);
        for i in 0..n {
            let key = get_i(g, keys, i);
            let index = match js_str(g, key) {
                Some(JsStr::Latin1(b)) => canonical_index(b),
                Some(JsStr::Utf16(u)) => canonical_index(u),
                None => None,
            };
            if let Some(ix) = index {
                self.index_prop(v, ix, depth)?;
                continue;
            }
            let mut val = UNDEF;
            match glue::rjs_own_get_v(g, v, key, &mut val) {
                1 => {}
                0 => continue,
                _ => return Err(val),
            }
            self.string(key)?;
            self.value(val, depth + 1)?;
        }
        Ok(())
    }

    unsafe fn index_prop(&mut self, v: u64, i: u32, depth: u32) -> R {
        let mut val = UNDEF;
        match glue::rjs_own_get_index(self.g, v, i, &mut val) {
            1 => {}
            0 => return Ok(()),
            _ => return Err(val),
        }
        self.put(tag::KEY_INDEX);
        self.varint(i as u64);
        self.value(val, depth + 1)
    }

    unsafe fn view(&mut self, v: u64, info: &RjsBrandInfo, depth: u32) -> R {
        let g = self.g;
        let kind = info.ta_kind as usize;
        if !(1..=13).contains(&kind) {
            return Err(dce(g, "This view could not be cloned."));
        }
        let mut buf = UNDEF;
        match glue::rjs_view_buffer(g, v, &mut buf) {
            0 => {}
            1 => return Err(buf),
            _ => return Err(dce(g, "This view could not be cloned.")),
        }
        let mut binfo = RjsBrandInfo::default();
        glue::rjs_brand(g, buf, &mut binfo);
        if info.flags & brand::F_DETACHED != 0 || binfo.flags & brand::F_DETACHED != 0 {
            return Err(dce(g, "An ArrayBuffer is detached and could not be cloned."));
        }
        // IsArrayBufferViewOutOfBounds (a resizable buffer that shrank).
        let tracking = info.flags & brand::F_LENGTH_TRACKING != 0;
        let blen = binfo.byte_length;
        let end = info.byte_offset.saturating_add(if tracking { 0 } else { info.length.saturating_mul(ELEM_SIZE[kind]) });
        if binfo.brand == brand::ARRAY_BUFFER && end > blen {
            return Err(dce(g, "A view that is out of bounds could not be cloned."));
        }
        self.memo(v);
        self.put(tag::VIEW);
        self.put(kind as u8);
        self.varint(info.byte_offset);
        self.varint(if tracking { 0 } else { info.length + 1 });
        self.object(buf, depth + 1)
    }

    unsafe fn map(&mut self, v: u64, depth: u32) -> R {
        let (g, it) = (self.g, self.it);
        self.memo(v);
        self.put(tag::MAP);
        // copiedList first: what the walk below does to the map is not seen.
        let iter = call(g, it.map_entries, v, &[])?;
        let mut pairs = Vec::new();
        loop {
            let r = call(g, it.map_next, iter, &[])?;
            if get_a(g, r, it.a_done) == TRUE {
                break;
            }
            let pair = get_a(g, r, it.a_value);
            let (k, x) = (get_i(g, pair, 0), get_i(g, pair, 1));
            self.keep.hold(k);
            self.keep.hold(x);
            pairs.push((k, x));
        }
        for (k, x) in pairs {
            self.value(k, depth + 1)?;
            self.value(x, depth + 1)?;
        }
        self.put(tag::END);
        Ok(())
    }

    unsafe fn set(&mut self, v: u64, depth: u32) -> R {
        let (g, it) = (self.g, self.it);
        self.memo(v);
        self.put(tag::SET);
        let iter = call(g, it.set_values, v, &[])?;
        let mut items = Vec::new();
        loop {
            let r = call(g, it.set_next, iter, &[])?;
            if get_a(g, r, it.a_done) == TRUE {
                break;
            }
            let x = get_a(g, r, it.a_value);
            self.keep.hold(x);
            items.push(x);
        }
        for x in items {
            self.value(x, depth + 1)?;
        }
        self.put(tag::END);
        Ok(())
    }

    unsafe fn error(&mut self, v: u64, depth: u32) -> R {
        let (g, it) = (self.g, self.it);
        // name = ? Get(value, "name"), outside HTML's list → "Error".
        let name = get_x(g, v, it.a_name)?;
        let name_ix = match js_str(g, name) {
            Some(JsStr::Latin1(b)) => ERROR_NAMES.iter().position(|n| n.as_bytes() == b),
            Some(JsStr::Utf16(u)) => ERROR_NAMES.iter().position(|n| n.encode_utf16().eq(u.iter().copied())),
            None => None,
        }
        .unwrap_or(0);
        // message only from an own DATA property, then ? ToString.
        let message = match own_data(g, v, it.a_message)? {
            Some(m) => {
                let mut s = UNDEF;
                if glue::rjs_kind(g, m) == 5 {
                    s = m;
                } else if glue::rjs_to_string(g, m, &mut s) != 0 {
                    return Err(s);
                }
                Some(s)
            }
            None => None,
        };
        let stack = own_data(g, v, it.a_stack)?.filter(|&s| glue::rjs_kind(g, s) == 5);
        let cause = own_data(g, v, it.a_cause)?;
        self.memo(v);
        self.put(tag::ERROR);
        self.put(name_ix as u8);
        self.put((message.is_some() as u8) | ((stack.is_some() as u8) << 1) | ((cause.is_some() as u8) << 2));
        if let Some(m) = message {
            self.string(m)?;
        }
        if let Some(s) = stack {
            self.string(s)?;
        }
        if let Some(c) = cause {
            self.value(c, depth + 1)?;
        }
        Ok(())
    }

    unsafe fn dom_exception(&mut self, v: u64) -> R {
        let g = self.g;
        let Some((name, message, quota)) = super::domexception::raw_parts(g, v) else {
            return Err(dce(g, "This platform object could not be cloned."));
        };
        let stack = own_data(g, v, self.it.a_stack)?.filter(|&s| glue::rjs_kind(g, s) == 5);
        let (q, r) = quota.map(|(q, r)| (dec_f64(q), dec_f64(r))).unwrap_or((None, None));
        self.memo(v);
        self.put(tag::DOMEXCEPTION);
        self.put(quota.is_some() as u8);
        self.string(name)?;
        self.string(message)?;
        self.put((stack.is_some() as u8) | ((q.is_some() as u8) << 1) | ((r.is_some() as u8) << 2));
        if let Some(s) = stack {
            self.string(s)?;
        }
        if let Some(q) = q {
            self.f64(q);
        }
        if let Some(r) = r {
            self.f64(r);
        }
        Ok(())
    }

    unsafe fn marked(&mut self, v: u64, m: Marked) -> R {
        let g = self.g;
        let Some(codec) = m.codec else {
            return Err(dce(g, &format!("{} object could not be cloned.", m.interface)));
        };
        let bytes = (codec.encode)(g, v).map_err(|msg| dce(g, &msg))?;
        self.memo(v);
        self.put(tag::HOST_OBJECT);
        self.rust_string(&m.interface);
        self.varint(bytes.len() as u64);
        self.reserve(bytes.len() as u64)?;
        self.out.extend_from_slice(&bytes);
        Ok(())
    }

    /// C-API host objects: the serializable platform classes, or refused.
    unsafe fn host_object(&mut self, v: u64, depth: u32) -> R {
        let g = self.g;
        if let Some(b) = super::blob::data_of(g, v) {
            let (range, mime, file) = (b.range(), b.mime.clone(), b.file.clone());
            if self.for_storage {
                // Inline: the bytes now, read straight into the output, under
                // the snapshot rule if the Blob is path-backed (see the module
                // docs). Room for them first: too large is a DataCloneError.
                if matches!(range.source, super::blob::Source::Disk(_)) && range.len > super::blob::SYNC_READ_ADVISORY_BYTES {
                    super::blob::advise_sync_read("IndexedDB (a value written for storage)", range.len);
                }
                self.memo(v);
                self.put(if file.is_some() { tag::FILE } else { tag::BLOB });
                self.rust_string(&mime);
                self.varint(range.len);
                self.reserve(range.len)?;
                match range.read_into(&mut self.out) {
                    Ok(()) => {}
                    Err(super::blob::ReadError::OutOfMemory) => return Err(dce(g, OUT_OF_MEMORY)),
                    Err(e) => return Err(e.to_js(g)),
                }
                if let Some(f) = file {
                    self.rust_string(&f.name);
                    self.f64(f.last_modified);
                    self.rust_string(&f.relative_path);
                }
            } else {
                match file {
                    Some(file) => self.extra(v, tag::FILE_REF, Attachment::FileRef { range, mime, file }),
                    None => self.extra(v, tag::BLOB_REF, Attachment::BlobRef { range, mime }),
                }
            }
            return Ok(());
        }
        if let Some(files) = super::file::file_list_items(g, v) {
            self.memo(v);
            self.put(tag::FILE_LIST);
            self.varint(files.len() as u64);
            for f in files {
                self.value(f, depth + 1)?;
            }
            return Ok(());
        }
        if let Some(b) = super::image::bitmap_of(g, v) {
            if b.width == 0 || b.height == 0 {
                return Err(dce(g, "An ImageBitmap is detached and could not be cloned."));
            }
            if self.for_storage {
                return Err(dce(g, "An ImageBitmap could not be serialized for storage."));
            }
            let a = Attachment::Bitmap { width: b.width, height: b.height, rgba: b.rgba.clone() };
            self.extra(v, tag::IMAGE_BITMAP, a);
            return Ok(());
        }
        if let Some((p, read_only)) = super::geometry::point_parts(g, v) {
            self.memo(v);
            self.put(tag::DOMPOINT);
            self.put(read_only as u8);
            for c in p {
                self.f64(c);
            }
            return Ok(());
        }
        if let Some((r, read_only)) = super::geometry::rect_parts(g, v) {
            self.memo(v);
            self.put(tag::DOMRECT);
            self.put(read_only as u8);
            for c in r {
                self.f64(c);
            }
            return Ok(());
        }
        if let Some((m, read_only)) = super::geometry::matrix_parts(g, v) {
            self.memo(v);
            self.put(tag::DOMMATRIX);
            self.put(read_only as u8);
            self.put(m.is2d as u8);
            if m.is2d {
                for i in [0, 1, 4, 5, 12, 13] {
                    self.f64(m.a[i]);
                }
            } else {
                for c in m.a {
                    self.f64(c);
                }
            }
            return Ok(());
        }
        if let Some(m) = marked_interface(g, v) {
            return self.marked(v, m);
        }
        Err(dce(g, "This platform object could not be cloned."))
    }
}

fn advise_stream_transfer() {
    static ONCE: OnceLock<()> = OnceLock::new();
    ONCE.get_or_init(|| {
        eprintln!(
            "[redjs] structuredClone/postMessage: transferring a ReadableStream / WritableStream / \
             TransformStream is not implemented in this runtime (a browser would move the stream) — \
             it is a DataCloneError here. Read the chunks out and send those instead."
        );
    });
}

// ---------------------------------------------------------------------------
// Decoder
// ---------------------------------------------------------------------------

enum Source<'a> {
    Owned(Vec<Option<Attachment>>),
    Borrowed(&'a [Attachment]),
    /// Storage bytes: no attachment table at all.
    None,
}

/// A reserved id whose object is still being built (view, ImageData).
const PLACEHOLDER: u64 = 0;

struct Dec<'a> {
    g: *mut c_void,
    it: &'static Intrinsics,
    buf: &'a [u8],
    pos: usize,
    ids: Vec<u64>,
    keep: KeepAlive,
    src: Source<'a>,
    cache: HashMap<u32, u64>,
    stack: StackGuard,
}

unsafe fn decode(g: *mut c_void, bytes: &[u8], src: Source, storage: bool) -> Result<u64, u64> {
    let _lock = Locked::new(g);
    let Some(it) = intrinsics(g) else {
        return Err(error_value(g, 1, "structured serialization is not installed in this context"));
    };
    if bytes.len() < 5 || bytes[..3] != MAGIC || bytes[3] != VERSION || bytes[4] & !FLAG_STORAGE != 0 {
        return Err(corrupt(g));
    }
    let src = if storage { Source::None } else { src };
    let mut d = Dec { g, it, buf: bytes, pos: 5, ids: Vec::new(), keep: KeepAlive::new(g), src, cache: HashMap::new(), stack: StackGuard::new(g) };
    let v = d.value(0)?;
    if d.pos != bytes.len() {
        return Err(corrupt(g));
    }
    Ok(v)
}

unsafe fn corrupt(g: *mut c_void) -> u64 {
    dce(g, "The serialized data is corrupt and could not be deserialized.")
}

impl<'a> Dec<'a> {
    unsafe fn bad<T>(&self) -> R<T> {
        Err(corrupt(self.g))
    }

    unsafe fn byte(&mut self) -> R<u8> {
        match self.buf.get(self.pos) {
            Some(&b) => {
                self.pos += 1;
                Ok(b)
            }
            None => self.bad(),
        }
    }

    unsafe fn varint(&mut self) -> R<u64> {
        let mut v: u64 = 0;
        for shift in (0..70).step_by(7) {
            let b = self.byte()?;
            if shift == 63 && b > 1 {
                return self.bad();
            }
            v |= ((b & 0x7f) as u64) << shift;
            if b & 0x80 == 0 {
                return Ok(v);
            }
        }
        self.bad()
    }

    unsafe fn u32v(&mut self) -> R<u32> {
        let v = self.varint()?;
        if v > u32::MAX as u64 {
            return self.bad();
        }
        Ok(v as u32)
    }

    unsafe fn f64(&mut self) -> R<f64> {
        let b = self.bytes(8)?;
        let d = f64::from_le_bytes(b.try_into().unwrap());
        // A NaN with a payload is not a valid JSValue double; one NaN.
        Ok(if d.is_nan() { f64::NAN } else { d })
    }

    unsafe fn bytes(&mut self, n: u64) -> R<&'a [u8]> {
        let buf: &'a [u8] = self.buf;
        if n > (buf.len() - self.pos) as u64 {
            return self.bad();
        }
        let s = &buf[self.pos..self.pos + n as usize];
        self.pos += n as usize;
        Ok(s)
    }

    unsafe fn memo(&mut self, v: u64) {
        self.ids.push(v);
        self.keep.hold(v);
    }

    fn reserve(&mut self) -> usize {
        self.ids.push(PLACEHOLDER);
        self.ids.len() - 1
    }

    unsafe fn fill(&mut self, slot: usize, v: u64) {
        self.ids[slot] = v;
        self.keep.hold(v);
    }

    unsafe fn string_body(&mut self, t: u8) -> R<u64> {
        let n = self.varint()?;
        let mut out = UNDEF;
        if t == tag::STRING_LATIN1 {
            let b = self.bytes(n)?;
            glue::rjs_string_latin1(self.g, b.as_ptr(), b.len(), &mut out);
        } else {
            let b = self.bytes(n.checked_mul(2).unwrap_or(u64::MAX))?;
            let units: Vec<u16> = b.chunks_exact(2).map(|c| u16::from_le_bytes([c[0], c[1]])).collect();
            glue::rjs_string16(self.g, units.as_ptr(), units.len(), &mut out);
        }
        Ok(out)
    }

    /// A position that must hold a string.
    unsafe fn string(&mut self) -> R<u64> {
        let t = self.byte()?;
        if t != tag::STRING_LATIN1 && t != tag::STRING_UTF16 {
            return self.bad();
        }
        self.string_body(t)
    }

    unsafe fn rust_string(&mut self) -> R<String> {
        let s = self.string()?;
        Ok(read_string(self.g, s).unwrap_or_default())
    }

    unsafe fn bigint(&mut self) -> R<u64> {
        let n = self.varint()?;
        let d = self.bytes(n)?;
        let body = d.strip_prefix(b"-").unwrap_or(d);
        if body.is_empty() || !body.iter().all(u8::is_ascii_digit) {
            return self.bad();
        }
        let mut s = UNDEF;
        glue::rjs_string_latin1(self.g, d.as_ptr(), d.len(), &mut s);
        call(self.g, self.it.bigint, UNDEF, &[s]).or_else(|_| self.bad())
    }

    unsafe fn value(&mut self, depth: u32) -> R<u64> {
        let t = self.byte()?;
        match t {
            tag::UNDEFINED => Ok(UNDEF),
            tag::NULL => Ok(NULL),
            tag::FALSE => Ok(FALSE),
            tag::TRUE => Ok(TRUE),
            tag::INT32 => {
                let z = self.u32v()?;
                Ok(enc_i32(((z >> 1) as i32) ^ -((z & 1) as i32)))
            }
            tag::DOUBLE => Ok(enc_f64(self.f64()?)),
            tag::BIGINT => self.bigint(),
            tag::STRING_LATIN1 | tag::STRING_UTF16 => self.string_body(t),
            tag::OBJECT_REF => {
                let id = self.varint()?;
                match self.ids.get(id as usize) {
                    Some(&v) if v != PLACEHOLDER => Ok(v),
                    _ => self.bad(),
                }
            }
            tag::TRANSFERRED_BUFFER | tag::IMAGE_BITMAP | tag::BLOB_REF | tag::FILE_REF => self.attachment(t),
            _ => {
                if !self.stack.ok(depth) {
                    return Err(stack_overflow(self.g));
                }
                self.object(t, depth)
            }
        }
    }

    unsafe fn props(&mut self, o: u64, depth: u32) -> R {
        let g = self.g;
        loop {
            let t = self.byte()?;
            match t {
                tag::END => return Ok(()),
                tag::KEY_INDEX => {
                    let i = self.u32v()?;
                    if i == u32::MAX {
                        return self.bad();
                    }
                    let v = self.value(depth + 1)?;
                    if glue::rjs_define_index(g, o, i, v) != 0 {
                        return self.bad();
                    }
                }
                tag::STRING_LATIN1 | tag::STRING_UTF16 => {
                    let k = self.string_body(t)?;
                    let v = self.value(depth + 1)?;
                    if glue::rjs_define_v(g, o, k, v) != 0 {
                        return self.bad();
                    }
                }
                _ => return self.bad(),
            }
        }
    }

    /// The message/stack/cause tail shared by Error and DOMException: the
    /// engine's own capture of `stack` is materialised first, so the carried
    /// one (or its absence) is what stays.
    unsafe fn set_stack(&mut self, e: u64, stack: Option<u64>) {
        let a = self.it.a_stack;
        let _ = get_a(self.g, e, a);
        match stack {
            Some(s) => {
                glue::rjs_define(self.g, e, a, s, 2);
            }
            None => {
                glue::rjs_delete(self.g, e, a);
            }
        }
    }

    unsafe fn object(&mut self, t: u8, depth: u32) -> R<u64> {
        let (g, it) = (self.g, self.it);
        match t {
            tag::OBJECT => {
                let o = new_object(g, UNDEF);
                self.memo(o);
                self.props(o, depth)?;
                Ok(o)
            }
            tag::ARRAY => {
                let len = self.u32v()?;
                let mut a = UNDEF;
                if glue::rjs_array_create(g, len, &mut a) != 0 {
                    return Err(a);
                }
                self.memo(a);
                self.props(a, depth)?;
                Ok(a)
            }
            tag::DATE => {
                let t = self.f64()?;
                let d = construct(g, it.date, &[enc_f64(t)]).or_else(|_| self.bad())?;
                self.memo(d);
                Ok(d)
            }
            tag::REGEXP => {
                let p = self.string()?;
                let f = self.string()?;
                let r = construct(g, it.regexp, &[p, f]).or_else(|_| self.bad())?;
                self.memo(r);
                Ok(r)
            }
            tag::MAP => {
                let m = construct(g, it.map, &[]).or_else(|_| self.bad())?;
                self.memo(m);
                loop {
                    if self.buf.get(self.pos) == Some(&tag::END) {
                        self.pos += 1;
                        return Ok(m);
                    }
                    let k = self.value(depth + 1)?;
                    let v = self.value(depth + 1)?;
                    call(g, it.map_set, m, &[k, v])?;
                }
            }
            tag::SET => {
                let s = construct(g, it.set, &[]).or_else(|_| self.bad())?;
                self.memo(s);
                loop {
                    if self.buf.get(self.pos) == Some(&tag::END) {
                        self.pos += 1;
                        return Ok(s);
                    }
                    let v = self.value(depth + 1)?;
                    call(g, it.set_add, s, &[v])?;
                }
            }
            tag::BOOLEAN_OBJECT | tag::NUMBER_OBJECT | tag::STRING_OBJECT | tag::BIGINT_OBJECT => {
                let prim = match t {
                    tag::BOOLEAN_OBJECT => match self.byte()? {
                        0 => FALSE,
                        1 => TRUE,
                        _ => return self.bad(),
                    },
                    tag::NUMBER_OBJECT => enc_f64(self.f64()?),
                    tag::STRING_OBJECT => self.string()?,
                    _ => self.bigint()?,
                };
                let w = call(g, it.object, UNDEF, &[prim]).or_else(|_| self.bad())?;
                self.memo(w);
                Ok(w)
            }
            tag::ERROR => {
                let name = self.byte()? as usize;
                let mask = self.byte()?;
                if name >= ERROR_NAMES.len() || mask & !7 != 0 {
                    return self.bad();
                }
                let e = construct(g, it.errors[name], &[]).or_else(|_| self.bad())?;
                self.memo(e);
                if mask & 1 != 0 {
                    let m = self.string()?;
                    glue::rjs_define(g, e, it.a_message, m, 2);
                }
                let stack = if mask & 2 != 0 { Some(self.string()?) } else { None };
                self.set_stack(e, stack);
                if mask & 4 != 0 {
                    let c = self.value(depth + 1)?;
                    glue::rjs_define(g, e, it.a_cause, c, 2);
                }
                Ok(e)
            }
            tag::ARRAY_BUFFER | tag::RESIZABLE_ARRAY_BUFFER => {
                let n = self.varint()?;
                let max = if t == tag::RESIZABLE_ARRAY_BUFFER { Some(self.varint()?) } else { None };
                if max.is_some_and(|m| m < n) {
                    return self.bad();
                }
                let bytes = self.bytes(n)?;
                let ab = new_buffer(g, it, bytes, max).or_else(|_| self.bad())?;
                self.memo(ab);
                Ok(ab)
            }
            tag::VIEW => {
                let kind = self.byte()? as usize;
                let offset = self.varint()?;
                let len_p1 = self.varint()?;
                if !(1..=13).contains(&kind) || !is_cell(it.views[kind]) {
                    return self.bad();
                }
                let slot = self.reserve();
                let buffer = self.value(depth + 1)?;
                let mut info = RjsBrandInfo::default();
                if glue::rjs_brand(g, buffer, &mut info) != brand::ARRAY_BUFFER {
                    return self.bad();
                }
                let mut args = vec![buffer, enc_f64(offset as f64)];
                if len_p1 != 0 {
                    args.push(enc_f64((len_p1 - 1) as f64));
                }
                let v = construct(g, it.views[kind], &args).or_else(|_| self.bad())?;
                self.fill(slot, v);
                Ok(v)
            }
            tag::BLOB => {
                let mime = self.rust_string()?;
                let n = self.varint()?;
                let bytes = self.bytes(n)?.to_vec();
                let b = super::blob::create(g, UNDEF, bytes, mime);
                self.memo(b);
                Ok(b)
            }
            tag::FILE => {
                let slot = self.reserve();
                let mime = self.rust_string()?;
                let n = self.varint()?;
                let bytes = self.bytes(n)?.to_vec();
                let name = self.rust_string()?;
                let last_modified = self.f64()?;
                let relative_path = self.rust_string()?;
                // `long long` milliseconds: anything else was never written.
                if !last_modified.is_finite() || last_modified.trunc() != last_modified {
                    return self.bad();
                }
                let len = bytes.len() as u64;
                let data = super::blob::BlobData {
                    source: super::blob::Source::Memory(Arc::new(bytes)),
                    offset: 0,
                    len,
                    mime,
                    file: Some(super::blob::FileMeta { name, last_modified, relative_path, backed: false }),
                };
                let f = super::file::from_data(g, data);
                if !is_cell(f) {
                    return self.bad();
                }
                self.fill(slot, f);
                Ok(f)
            }
            tag::FILE_LIST => {
                let slot = self.reserve();
                let n = self.varint()?;
                let mut files = Vec::with_capacity(n.min((self.buf.len() - self.pos) as u64) as usize);
                for _ in 0..n {
                    let f = self.value(depth + 1)?;
                    if !super::file::is_file(g, f) {
                        return self.bad();
                    }
                    files.push(f);
                }
                let list = super::file::create_file_list(g, &files);
                if !is_cell(list) {
                    return self.bad();
                }
                self.fill(slot, list);
                Ok(list)
            }
            tag::IMAGE_DATA => {
                let w = self.u32v()?;
                let h = self.u32v()?;
                if self.byte()? != 0 {
                    return self.bad();
                }
                let slot = self.reserve();
                let data = self.value(depth + 1)?;
                let Some(v) = super::platform::create_image_data(g, data, w, h) else { return self.bad() };
                self.fill(slot, v);
                Ok(v)
            }
            tag::DOMPOINT => {
                let read_only = match self.byte()? {
                    0 => false,
                    1 => true,
                    _ => return self.bad(),
                };
                let mut p = [0.0; 4];
                for c in &mut p {
                    *c = self.f64()?;
                }
                let Some(v) = super::geometry::create_point(g, p, read_only) else { return self.bad() };
                self.memo(v);
                Ok(v)
            }
            tag::DOMRECT => {
                let read_only = match self.byte()? {
                    0 => false,
                    1 => true,
                    _ => return self.bad(),
                };
                let mut r = [0.0; 4];
                for c in &mut r {
                    *c = self.f64()?;
                }
                let Some(v) = super::geometry::create_rect(g, r, read_only) else { return self.bad() };
                self.memo(v);
                Ok(v)
            }
            tag::DOMMATRIX => {
                let read_only = self.byte()?;
                let is2d = self.byte()?;
                if read_only > 1 || is2d > 1 {
                    return self.bad();
                }
                let m = if is2d == 1 {
                    let mut v = [0.0; 6];
                    for c in &mut v {
                        *c = self.f64()?;
                    }
                    super::geometry::Mat::from_2d(v)
                } else {
                    let mut v = [0.0; 16];
                    for c in &mut v {
                        *c = self.f64()?;
                    }
                    super::geometry::Mat::from_16(v)
                };
                let Some(v) = super::geometry::create_matrix(g, m, read_only == 1) else { return self.bad() };
                self.memo(v);
                Ok(v)
            }
            tag::DOMEXCEPTION => {
                let kind = self.byte()?;
                let name = self.string()?;
                let message = self.string()?;
                let mask = self.byte()?;
                if kind > 1 || mask & !7 != 0 || (kind == 0 && mask & 6 != 0) {
                    return self.bad();
                }
                let stack = if mask & 1 != 0 { Some(self.string()?) } else { None };
                let q = if mask & 2 != 0 { enc_f64(self.f64()?) } else { NULL };
                let r = if mask & 4 != 0 { enc_f64(self.f64()?) } else { NULL };
                let quota = (kind == 1).then_some((q, r));
                let Some(e) = super::domexception::from_raw(g, name, message, quota) else { return self.bad() };
                self.memo(e);
                self.set_stack(e, stack);
                Ok(e)
            }
            tag::HOST_OBJECT => {
                let slot = self.reserve();
                let interface = self.rust_string()?;
                let n = self.varint()?;
                let bytes = self.bytes(n)?;
                // Blink's words where a receiver cannot make the interface.
                let Some(codec) = codec_for(g, &interface) else { return Err(dce(g, "Unable to deserialize cloned data.")) };
                let v = (codec.decode)(g, bytes).map_err(|msg| dce(g, &msg))?;
                if !is_object(g, v) {
                    return self.bad();
                }
                self.fill(slot, v);
                Ok(v)
            }
            _ => self.bad(),
        }
    }

    unsafe fn attachment(&mut self, t: u8) -> R<u64> {
        let g = self.g;
        let i = self.u32v()?;
        if let Some(&v) = self.cache.get(&i) {
            return Ok(v);
        }
        let ix = i as usize;
        // Owned: the entry moves out (transferred data moves in). Borrowed:
        // a copy per receiver — a Blob's is just another `Arc`.
        let a: Option<Attachment> = match &mut self.src {
            Source::None => None,
            Source::Owned(list) => list.get_mut(ix).and_then(Option::take),
            Source::Borrowed(list) => list.get(ix).cloned(),
        };
        let v = match (t, a) {
            (tag::TRANSFERRED_BUFFER, Some(Attachment::TransferredBuffer { bytes, max_byte_length })) => {
                new_buffer(g, self.it, &bytes, max_byte_length).or_else(|_| self.bad())?
            }
            (tag::IMAGE_BITMAP, Some(Attachment::Bitmap { width, height, rgba })) => {
                if width == 0 || height == 0 || rgba.len() as u64 != width as u64 * height as u64 * 4 {
                    return self.bad();
                }
                super::image::create_bitmap(g, super::image::BitmapData::new(width, height, rgba))
            }
            (tag::BLOB_REF, Some(Attachment::BlobRef { range, mime })) => {
                if !range.fits() {
                    return self.bad();
                }
                let data = super::blob::BlobData { source: range.source, offset: range.offset, len: range.len, mime, file: None };
                super::blob::from_data(g, data)
            }
            (tag::FILE_REF, Some(Attachment::FileRef { range, mime, file })) => {
                if !range.fits() {
                    return self.bad();
                }
                let data = super::blob::BlobData { source: range.source, offset: range.offset, len: range.len, mime, file: Some(file) };
                let f = super::file::from_data(g, data);
                if !is_cell(f) {
                    return self.bad();
                }
                f
            }
            _ => return self.bad(),
        };
        self.cache.insert(i, v);
        self.keep.hold(v);
        Ok(v)
    }
}

/// A fresh ArrayBuffer in this realm holding `bytes` (resizable to `max`).
unsafe fn new_buffer(g: *mut c_void, it: &Intrinsics, bytes: &[u8], max: Option<u64>) -> R<u64> {
    let n = enc_f64(bytes.len() as f64);
    let ab = match max {
        Some(m) => {
            let opts = new_object(g, NULL);
            define(g, opts, "maxByteLength", enc_f64(m as f64), 0);
            construct(g, it.array_buffer, &[n, opts])?
        }
        None => construct(g, it.array_buffer, &[n])?,
    };
    if !bytes.is_empty() {
        let mut info = RjsBrandInfo::default();
        glue::rjs_brand(g, ab, &mut info);
        if info.data.is_null() || (info.byte_length as usize) < bytes.len() {
            return Err(error_value(g, 2, "Array buffer allocation failed"));
        }
        core::ptr::copy_nonoverlapping(bytes.as_ptr(), info.data as *mut u8, bytes.len());
    }
    Ok(ab)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::webapi::testing::{ctx, ok, s, throws};

    /// `name/code` of what `src` throws, through JS so the DOMException is real.
    fn thrown(c: &crate::api::Context, src: &str) -> String {
        s(c, &format!("(() => {{ try {{ {src}; return 'no throw'; }} catch (e) {{ return (e instanceof DOMException ? 'DOMException:' : '') + e.name + '/' + (e.code ?? '-'); }} }})()"))
    }

    /// A host's script-defined platform class (skathi's FileReader, its
    /// file handles) is refused by name, a subclass of it too, and every
    /// other plain object still clones.
    #[test]
    fn a_refused_platform_prototype_is_a_data_clone_error() {
        let c = ctx();
        s(&c, "globalThis.Reader = class Reader {}; globalThis.Sub = class extends Reader {}; 0");
        unsafe {
            let g = c.raw();
            let reader = get(g, glue::rjs_global(g), "Reader");
            super::refuse_platform_prototype(g, get(g, reader, "prototype"), "FileReader");
        }
        assert_eq!(thrown(&c, "structuredClone(new Reader())"), "DOMException:DataCloneError/25");
        assert_eq!(thrown(&c, "structuredClone({ inner: [new Sub()] })"), "DOMException:DataCloneError/25");
        assert_eq!(
            s(&c, "(() => { try { structuredClone(new Reader()); } catch (e) { return e.message; } })()"),
            "FileReader object could not be cloned."
        );
        ok(&c, "(() => { const r = structuredClone({ a: 1, b: { c: [2] } }); return r.a === 1 && r.b.c[0] === 2; })()");
        ok(&c, "(() => { class Other {} return Object.getPrototypeOf(structuredClone(new Other())) === Object.prototype; })()");
    }

    /// The test host's codec: a `Tok` travels as its `tok` string, and comes
    /// back through `makeTok` — a fresh instance, never the original.
    unsafe fn tok_encode(g: *mut c_void, v: u64) -> Result<Vec<u8>, String> {
        match read_string(g, get(g, v, "tok")) {
            Some(t) if !t.is_empty() => Ok(t.into_bytes()),
            _ => Err("Tok object could not be cloned.".into()),
        }
    }

    unsafe fn tok_decode(g: *mut c_void, bytes: &[u8]) -> Result<u64, String> {
        let t = std::str::from_utf8(bytes).map_err(|_| "Unable to deserialize cloned data.".to_string())?;
        let make = get(g, glue::rjs_global(g), "makeTok");
        call(g, make, UNDEF, &[js_string(g, t)]).map_err(|_| "Unable to deserialize cloned data.".to_string())
    }

    /// A host's script class for a [Serializable] interface (skathi's
    /// FileSystemHandle) round-trips through its codec, identity and storage
    /// included; a context with no codec for it refuses to read it.
    #[test]
    fn a_host_serialized_platform_prototype_round_trips_through_its_codec() {
        let c = ctx();
        s(&c, "globalThis.Tok = class Tok { constructor(t) { this.tok = t; } }; globalThis.SubTok = class extends Tok {}; \
               globalThis.makeTok = t => { const k = new SubTok(t); k.made = true; return k; }; 0");
        let codec = HostCodec { encode: tok_encode, decode: tok_decode };
        unsafe {
            let g = c.raw();
            let tok = get(g, glue::rjs_global(g), "Tok");
            super::serialize_platform_prototype(g, get(g, tok, "prototype"), "Tok", codec);
        }
        ok(&c, "(() => { const k = new Tok('a'); const r = structuredClone({ x: k, y: [k] }); \
                 return r.x instanceof Tok && r.x !== k && r.x.made && r.x.tok === 'a' && r.y[0] === r.x; })()");
        // Own properties beside the host's state are the host's business: not copied.
        ok(&c, "(() => { const k = new Tok('b'); k.extra = 1; return !('extra' in structuredClone(k)); })()");
        // The codec's refusal is a DataCloneError with its message.
        assert_eq!(thrown(&c, "structuredClone(new Tok(''))"), "DOMException:DataCloneError/25");
        assert_eq!(s(&c, "(() => { try { structuredClone(new Tok('')); } catch (e) { return e.message; } })()"), "Tok object could not be cloned.");
        unsafe {
            let g = c.raw();
            // For storage too (IndexedDB keeps File System Access handles).
            let st = ser(&c, "({ k: new Tok('disk'), n: 1 })", true).unwrap();
            assert!(st.bytes.contains(&tag::HOST_OBJECT));
            let v = deserialize_storage(g, &st.bytes).unwrap();
            define(g, glue::rjs_global(g), "fromDisk", v, 0);
            ok(&c, "fromDisk.k instanceof Tok && fromDisk.k.tok === 'disk' && fromDisk.n === 1");
            #[cfg(feature = "indexeddb")]
            {
                use crate::webapi::indexeddb::{key::Key, keypath::KeyPath, svalue::Parsed};
                let p = Parsed::parse(&st.bytes).expect("key paths parse around it");
                assert_eq!(p.extract(&KeyPath::String("n".encode_utf16().collect())), Ok(Some(Key::Number(1.0))));
            }

            // A context whose host registered no codec for the interface.
            let other = ctx();
            let e = deserialize_storage(other.raw(), &st.bytes).expect_err("decoded with no codec");
            define(other.raw(), glue::rjs_global(other.raw()), "noCodec", e, 0);
            assert_eq!(s(&other, "noCodec.name + ': ' + noCodec.message"), "DataCloneError: Unable to deserialize cloned data.");
        }
    }

    #[test]
    fn primitives_round_trip_exactly() {
        let c = ctx();
        ok(&c, "(() => { const v = [undefined, null, true, false, 0, -0, 1.5, NaN, Infinity, -Infinity, -2147483648, 2147483647, 2**31, 2**53, -1e-300, \
                 10n, -(2n ** 80n), 0n, 'abc', '\\u00e9t\\u00e9', '\\u{1F600}', '\\uD800x', '']; \
                 const r = structuredClone(v); \
                 return r.length === v.length && r.every((x, i) => Object.is(x, v[i])); })()");
    }

    #[test]
    fn objects_arrays_keys_and_holes() {
        let c = ctx();
        ok(&c, "(() => { const a = [1, , 3]; a.extra = 'x'; a[10] = 'ten'; \
                 const r = structuredClone(a); \
                 return Array.isArray(r) && r.length === 11 && !(1 in r) && r[2] === 3 && r.extra === 'x' && r[10] === 'ten'; })()");
        ok(&c, "(() => { const o = { b: 1, a: 2, 2: 'two', 1: 'one' }; Object.defineProperty(o, 'hidden', { value: 1, enumerable: false }); \
                 o.__proto__x = 0; const r = structuredClone(o); \
                 return Object.keys(r).join() === '1,2,b,a,__proto__x' && !('hidden' in r) && Object.getPrototypeOf(r) === Object.prototype; })()");
        // An own "__proto__" key is a key, not a prototype.
        ok(&c, "(() => { const o = JSON.parse('{\"__proto__\": {\"x\": 1}}'); const r = structuredClone(o); \
                 return Object.getPrototypeOf(r) === Object.prototype && Object.hasOwn(r, '__proto__') && r.__proto__.x === 1; })()");
        // Null-prototype objects come back with Object.prototype (HTML).
        ok(&c, "(() => { const o = Object.create(null); o.k = 1; const r = structuredClone(o); return Object.getPrototypeOf(r) === Object.prototype && r.k === 1; })()");
        // Class instances lose their class, keep their own data.
        ok(&c, "(() => { class P { #secret = 1; constructor() { this.x = 2; } get y() { return 3; } } const r = structuredClone(new P()); \
                 return Object.getPrototypeOf(r) === Object.prototype && r.x === 2 && !('y' in r); })()");
    }

    #[test]
    fn getters_run_once_and_deleted_keys_are_skipped() {
        let c = ctx();
        ok(&c, "(() => { let n = 0; const o = { get a() { n++; delete this.b; return 'A'; }, b: 'B', c: 'C' }; \
                 const r = structuredClone(o); \
                 return n === 1 && r.a === 'A' && !('b' in r) && r.c === 'C' && Object.getOwnPropertyDescriptor(r, 'a').value === 'A'; })()");
        // A getter that throws propagates as itself.
        assert_eq!(
            s(&c, "(() => { try { structuredClone({ get a() { throw new RangeError('boom'); } }); } catch (e) { return e.name + ':' + e.message; } })()"),
            "RangeError:boom"
        );
        // Array: a getter shrinking the array mid-walk.
        ok(&c, "(() => { const a = [0, 1, 2, 3]; Object.defineProperty(a, 1, { get() { a.length = 2; return 'one'; }, enumerable: true, configurable: true }); \
                 const r = structuredClone(a); return r.length === 4 && r[1] === 'one' && !(2 in r) && !(3 in r); })()");
    }

    #[test]
    fn identity_and_cycles() {
        let c = ctx();
        ok(&c, "(() => { const shared = { s: 1 }; const o = { a: shared, b: shared, list: [shared] }; o.self = o; o.list.push(o.list); \
                 const r = structuredClone(o); \
                 return r.a === r.b && r.list[0] === r.a && r.self === r && r.list[1] === r.list && r.a !== shared; })()");
        ok(&c, "(() => { const k = {}; const m = new Map([[k, k], ['x', 1]]); m.set('m', m); const st = new Set([k, m]); \
                 const r = structuredClone({ m, st }); const rk = [...r.m.keys()][0]; \
                 return r.m.get(rk) === rk && r.m.get('m') === r.m && r.st.has(rk) && r.st.has(r.m) && r.m.get('x') === 1; })()");
        // Two views over one buffer stay over one buffer.
        ok(&c, "(() => { const b = new ArrayBuffer(8); const u = new Uint8Array(b); const d = new DataView(b, 2, 4); u[3] = 9; \
                 const r = structuredClone({ u, d, b }); \
                 return r.u.buffer === r.b && r.d.buffer === r.b && r.d.byteOffset === 2 && r.d.byteLength === 4 && r.d.getUint8(1) === 9; })()");
        // An Error whose cause is itself.
        ok(&c, "(() => { const e = new Error('loop'); e.cause = e; const r = structuredClone(e); return r.cause === r && r.message === 'loop'; })()");
    }

    #[test]
    fn builtins_round_trip() {
        let c = ctx();
        ok(&c, "(() => { const d = new Date(12345); const bad = new Date(NaN); const re = /a+b/giu; re.lastIndex = 3; \
                 const r = structuredClone([d, bad, re, new Boolean(false), new Number(-0), new String('s\\uD800'), Object(5n)]); \
                 return r[0] instanceof Date && r[0].getTime() === 12345 && Number.isNaN(r[1].getTime()) \
                   && r[2] instanceof RegExp && r[2].source === 'a+b' && r[2].flags === 'giu' && r[2].lastIndex === 0 \
                   && typeof r[3] === 'object' && r[3].valueOf() === false && Object.is(r[4].valueOf(), -0) \
                   && r[5].valueOf() === 's\\uD800' && typeof r[6] === 'object' && r[6].valueOf() === 5n; })()");
        // Map/Set keep insertion order; a Map subclass comes back a Map.
        ok(&c, "(() => { class M extends Map {} const m = new M([[3, 'c'], [1, 'a']]); const r = structuredClone(m); \
                 return Object.getPrototypeOf(r) === Map.prototype && [...r.keys()].join() === '3,1'; })()");
        // Overriding the iteration surface after install changes nothing.
        ok(&c, "(() => { const src = new Map([['k', 1]]); const P = Map.prototype, ent = P.entries, it = P[Symbol.iterator], set = P.set; \
                 P.entries = P[Symbol.iterator] = function* () { yield ['evil', 1]; }; \
                 P.set = function () { throw new Error('set called'); }; \
                 try { const r = structuredClone(src); return r.size === 1 && ent.call(r).next().value[0] === 'k'; } \
                 finally { P.entries = ent; P[Symbol.iterator] = it; P.set = set; } })()");
    }

    #[test]
    fn errors_follow_html() {
        let c = ctx();
        ok(&c, "(() => { const r = structuredClone([new TypeError('t'), new RangeError('r'), new SyntaxError('s'), new URIError('u'), \
                 new EvalError('e'), new ReferenceError('f'), new AggregateError([], 'agg')]); \
                 return r.map(e => e.constructor.name + ':' + e.message).join() === \
                   'TypeError:t,RangeError:r,SyntaxError:s,URIError:u,EvalError:e,ReferenceError:f,Error:agg'; })()");
        // Name from Get, normalized; message only from an own data property.
        ok(&c, "(() => { const e = new Error('m'); e.name = 'TypeError'; const f = new Error('m2'); f.name = 'Custom'; \
                 const g = new Error(); Object.defineProperty(g, 'message', { get() { return 'getter'; } }); \
                 const [re, rf, rg] = structuredClone([e, f, g]); \
                 return re instanceof TypeError && re.message === 'm' && rf.constructor === Error && rf.name === 'Error' \
                   && !Object.hasOwn(rg, 'message') && rg.message === ''; })()");
        // Stack and cause come along; message is non-enumerable as from the constructor.
        ok(&c, "(() => { const e = new Error('x', { cause: { why: 1 } }); const r = structuredClone(e); \
                 return r.stack === e.stack && r.cause.why === 1 && r.cause !== e.cause \
                   && !Object.getOwnPropertyDescriptor(r, 'message').enumerable; })()");
        // A message whose ToString throws: the throw propagates.
        assert_eq!(
            s(&c, "(() => { const e = new Error(); e.message = { toString() { throw new Error('ts'); } }; try { structuredClone(e); } catch (x) { return x.message; } })()"),
            "ts"
        );
    }

    #[test]
    fn dom_exceptions_round_trip() {
        let c = ctx();
        ok(&c, "(() => { const e = new DOMException('gone', 'NotFoundError'); const r = structuredClone(e); \
                 return r instanceof DOMException && r !== e && r.name === 'NotFoundError' && r.message === 'gone' && r.code === 8 && r.stack === e.stack; })()");
        ok(&c, "(() => { const q = new QuotaExceededError('full', { quota: 10, requested: 12 }); const r = structuredClone(q); \
                 const q2 = structuredClone(new QuotaExceededError('x')); \
                 return r instanceof QuotaExceededError && r.quota === 10 && r.requested === 12 && r.code === 22 \
                   && q2.quota === null && q2.requested === null; })()");
    }

    #[test]
    fn buffers_and_views() {
        let c = ctx();
        ok(&c, "(() => { const kinds = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, \
                   Float32Array, Float64Array, BigInt64Array, BigUint64Array]; \
                 return kinds.every(K => { const b = new ArrayBuffer(64); const v = new K(b, 8, 3); v[1] = K.name.startsWith('Big') ? 7n : 7; \
                   const r = structuredClone(v); return r instanceof K && r.byteOffset === 8 && r.length === 3 && r[1] == 7 && r.buffer.byteLength === 64; }); })()");
        ok(&c, "(() => { const rab = new ArrayBuffer(4, { maxByteLength: 16 }); const tracking = new Uint8Array(rab); const fixed = new Uint8Array(rab, 1, 2); \
                 const r = structuredClone({ rab, tracking, fixed }); r.rab.resize(10); \
                 return r.rab.resizable && r.rab.maxByteLength === 16 && r.tracking.length === 10 && r.fixed.length === 2; })()");
        assert_eq!(thrown(&c, "const rab = new ArrayBuffer(8, { maxByteLength: 8 }); const v = new Uint8Array(rab, 4, 4); rab.resize(2); structuredClone(v)"), "DOMException:DataCloneError/25");
        assert_eq!(thrown(&c, "const b = new ArrayBuffer(4); structuredClone(0, { transfer: [b] }); structuredClone(b)"), "DOMException:DataCloneError/25");
        assert_eq!(thrown(&c, "const b = new ArrayBuffer(4); const v = new Uint8Array(b); structuredClone(0, { transfer: [b] }); structuredClone(v)"), "DOMException:DataCloneError/25");
    }

    #[test]
    fn transfer_follows_html() {
        let c = ctx();
        ok(&c, "(() => { const b = new ArrayBuffer(4); new Uint8Array(b)[0] = 42; const v = new Uint8Array(b); \
                 const r = structuredClone({ v, b }, { transfer: [b] }); \
                 return b.byteLength === 0 && v.length === 0 && r.b.byteLength === 4 && r.v.buffer === r.b && r.v[0] === 42; })()");
        // Listed but unreferenced: still detached.
        ok(&c, "(() => { const b = new ArrayBuffer(4); structuredClone(1, { transfer: [b] }); return b.byteLength === 0; })()");
        // From any iterable.
        ok(&c, "(() => { const b = new ArrayBuffer(2); const r = structuredClone(b, { transfer: (function* () { yield b; })() }); return b.byteLength === 0 && r.byteLength === 2; })()");
        // Resizable transfers stay resizable.
        ok(&c, "(() => { const b = new ArrayBuffer(2, { maxByteLength: 8 }); const r = structuredClone(b, { transfer: [b] }); return b.byteLength === 0 && r.resizable && r.maxByteLength === 8 && r.byteLength === 2; })()");
        // Refusals, and a failed serialize detaches nothing.
        assert_eq!(thrown(&c, "const b = new ArrayBuffer(1); structuredClone(0, { transfer: [b, b] })"), "DOMException:DataCloneError/25");
        assert_eq!(thrown(&c, "structuredClone(0, { transfer: [{}] })"), "DOMException:DataCloneError/25");
        assert_eq!(thrown(&c, "structuredClone(0, { transfer: [new Uint8Array(1)] })"), "DOMException:DataCloneError/25");
        ok(&c, "(() => { const b = new ArrayBuffer(4); try { structuredClone({ b, f() {} }, { transfer: [b] }); } catch (e) {} return b.byteLength === 4; })()");
        // Detached mid-walk by a getter → DataCloneError at the detach step.
        assert_eq!(
            thrown(&c, "const b = new ArrayBuffer(4); structuredClone({ get x() { structuredClone(0, { transfer: [b] }); return 1; } }, { transfer: [b] })"),
            "DOMException:DataCloneError/25"
        );
        // WebIDL conversion of the options.
        assert_eq!(thrown(&c, "structuredClone()"), "TypeError/-");
        assert_eq!(thrown(&c, "structuredClone(1, 5)"), "TypeError/-");
        assert_eq!(thrown(&c, "structuredClone(1, { transfer: 5 })"), "TypeError/-");
        assert_eq!(thrown(&c, "structuredClone(1, { transfer: [1] })"), "TypeError/-");
        assert_eq!(s(&c, "structuredClone(7, null) + structuredClone(1, { transfer: undefined })"), "8");
        // A stream in the list: refused, with the one-shot advisory.
        assert_eq!(thrown(&c, "structuredClone(0, { transfer: [new ReadableStream()] })"), "DOMException:DataCloneError/25");
    }

    #[test]
    fn refusals_are_data_clone_errors() {
        let c = ctx();
        for src in [
            "structuredClone(() => 1)",
            "structuredClone(Symbol('s'))",
            "structuredClone({ s: Symbol.iterator })",
            "structuredClone(new Proxy({}, {}))",
            "structuredClone(Promise.resolve())",
            "structuredClone(new WeakMap())",
            "structuredClone(Object(Symbol()))",
            "structuredClone(globalThis)",
            "structuredClone(new Event('x'))",
            "structuredClone(new EventTarget())",
            "structuredClone(new (class extends EventTarget {})())",
            "structuredClone(new AbortController().signal)",
            "structuredClone(new Headers())",
        ] {
            assert_eq!(thrown(&c, src), "DOMException:DataCloneError/25", "{src}");
        }
        assert!(throws(&c, "structuredClone({ f() {} })").contains("function"));
    }

    #[test]
    fn platform_objects_round_trip() {
        let c = ctx();
        ok(&c, "(() => { const b = new Blob(['hello'], { type: 'text/plain' }); const s = b.slice(1, 4); \
                 const r = structuredClone({ b, s, again: b }); \
                 return r.b instanceof Blob && r.b !== b && r.b.size === 5 && r.b.type === 'text/plain' && r.s.size === 3 && r.again === r.b; })()");
        ok(&c, "(() => { const d = new Uint8ClampedArray(8); d[5] = 200; const img = new ImageData(d, 2, 1); \
                 const r = structuredClone({ img, d }); \
                 return r.img instanceof ImageData && r.img.width === 2 && r.img.height === 1 && r.img.data === r.d && r.d[5] === 200; })()");
        ok(&c, "(() => { const p = new DOMPoint(1, 2, 3, 4); const q = new DOMPointReadOnly(5); \
                 const m = new DOMMatrix([1, 2, 3, 4, 5, 6]); const m3 = new DOMMatrixReadOnly([1, 0, 0, 0, 0, 1, 0.5, 0, 0, 0, 1, 0, 0, 0, 0, 1]); \
                 const [rp, rq, rm, rm3] = structuredClone([p, q, m, m3]); \
                 return rp instanceof DOMPoint && rp.w === 4 && !(rq instanceof DOMPoint) && rq instanceof DOMPointReadOnly && rq.x === 5 \
                   && rm instanceof DOMMatrix && rm.is2D && rm.f === 6 && !(rm3 instanceof DOMMatrix) && rm3 instanceof DOMMatrixReadOnly \
                   && !rm3.is2D && rm3.m23 === 0.5; })()");
    }

    #[test]
    fn image_bitmaps_copy_and_transfer() {
        let c = ctx();
        unsafe {
            let g = c.raw();
            let bm = super::super::image::create_bitmap(g, super::super::image::BitmapData::new(1, 1, vec![1, 2, 3, 4]));
            define(g, glue::rjs_global(g), "bm", bm, 0);
        }
        ok(&c, "(() => { const r = structuredClone(bm); return r instanceof ImageBitmap && r !== bm && r.width === 1 && bm.width === 1; })()");
        ok(&c, "(() => { const r = structuredClone({ a: bm, b: bm }); return r.a === r.b; })()");
        ok(&c, "(() => { const r = structuredClone(bm, { transfer: [bm] }); return r.width === 1 && bm.width === 0; })()");
        assert_eq!(thrown(&c, "structuredClone(bm)"), "DOMException:DataCloneError/25");
    }

    unsafe fn ser(c: &crate::api::Context, src: &str, for_storage: bool) -> Result<Serialized, String> {
        let v = c.eval(src).unwrap();
        serialize(c.raw(), v.0, &Options { for_storage, transfer: &[] }).map_err(|e| c.error_info(crate::api::Value(e)).0)
    }

    #[test]
    fn wire_format_is_frozen() {
        let c = ctx();
        unsafe {
            let got = ser(&c, "({ a: 1, b: [true] })", false).unwrap();
            assert_eq!(
                got.bytes,
                [b'R', b'J', b'S', 1, 0, 0x11, 0x08, 1, b'a', 0x05, 2, 0x08, 1, b'b', 0x12, 1, 0x0A, 0, 0x04, 0x0F, 0x0F]
            );
            assert!(got.attachments.is_empty());
            let got = ser(&c, "(() => { const o = {}; return [o, o, -1, 'é\\u0100', 2n, new Date(0)]; })()", true).unwrap();
            let mut want = vec![b'R', b'J', b'S', 1, FLAG_STORAGE, 0x12, 6];
            want.extend([0x0A, 0, 0x11, 0x0F]); // [0] = {} (id 1; the array is id 0)
            want.extend([0x0A, 1, 0x10, 1]); // [1] = ref 1
            want.extend([0x0A, 2, 0x05, 1]); // -1 zigzag
            want.extend([0x0A, 3, 0x09, 2, 0xE9, 0, 0x00, 0x01]); // UTF-16LE
            want.extend([0x0A, 4, 0x07, 1, b'2']);
            want.extend([0x0A, 5, 0x14]);
            want.extend(0f64.to_le_bytes());
            want.push(0x0F);
            assert_eq!(got.bytes, want);
        }
    }

    #[test]
    fn storage_mode() {
        let c = ctx();
        unsafe {
            let g = c.raw();
            let s = ser(&c, "({ b: new Blob(['xyz'], { type: 'a/b' }), m: new Map([[1, new Set([2])]]) })", true).unwrap();
            assert!(s.attachments.is_empty());
            let v = deserialize_storage(g, &s.bytes).unwrap();
            define(g, glue::rjs_global(g), "fromDisk", v, 0);
            ok(&c, "fromDisk.b instanceof Blob && fromDisk.b.type === 'a/b' && fromDisk.b.size === 3 && fromDisk.m.get(1).has(2)");

            let bm = super::super::image::create_bitmap(g, super::super::image::BitmapData::new(1, 1, vec![0; 4]));
            define(g, glue::rjs_global(g), "bm2", bm, 0);
            let e = ser(&c, "({ bm2 })", true).unwrap_err();
            assert!(e.contains("storage"), "{e}");

            // Attachment tags never come out of storage.
            let not_storage = ser(&c, "new Blob(['q'])", false).unwrap();
            assert_eq!(not_storage.bytes[5], tag::BLOB_REF);
            assert!(deserialize_storage(g, &not_storage.bytes).is_err());
        }
    }

    #[test]
    fn corrupt_bytes_are_data_clone_errors() {
        let c = ctx();
        unsafe {
            let g = c.raw();
            let good = ser(&c, "({ k: [1, 2, 3], s: 'str' })", true).unwrap().bytes;
            assert!(deserialize_storage(g, &good).is_ok());
            let mut cases: Vec<Vec<u8>> = Vec::new();
            for n in 0..good.len() {
                cases.push(good[..n].to_vec()); // every truncation
            }
            let mut trailing = good.clone();
            trailing.push(0x01);
            cases.push(trailing);
            cases.push(b"XYZ\x01\x00\x01".to_vec()); // magic
            cases.push(b"RJS\x02\x00\x01".to_vec()); // version
            cases.push(b"RJS\x01\x80\x01".to_vec()); // unknown flag
            cases.push(b"RJS\x01\x00\x10\x00".to_vec()); // ref to nothing
            cases.push(b"RJS\x01\x00\x13".to_vec()); // reserved tag
            cases.push(b"RJS\x01\x00\x22\x02\x00\x00\x10\x00".to_vec()); // view whose buffer is itself
            cases.push(b"RJS\x01\x00\x07\x02\x31\x2e".to_vec()); // "1." as a BigInt
            cases.push(b"RJS\x01\x00\x08\xff\xff\xff\xff\x0f".to_vec()); // string longer than the data
            cases.push(b"RJS\x01\x00\x1C\x07\x00".to_vec()); // error name out of range
            cases.push(b"RJS\x01\x00\x05\xff\xff\xff\xff\xff\xff\xff\xff\xff\x7f".to_vec()); // varint overflow
            for bad in &cases {
                let e = deserialize_storage(g, bad).expect_err("corrupt bytes decoded");
                define(g, glue::rjs_global(g), "corruptErr", e, 0);
                ok(&c, "corruptErr instanceof DOMException && corruptErr.name === 'DataCloneError'");
            }
            // A NaN with a payload reads as the one NaN.
            let mut nan = b"RJS\x01\x00\x06".to_vec();
            nan.extend(0xFFF8_0000_DEAD_BEEFu64.to_le_bytes());
            let v = deserialize_storage(g, &nan).unwrap();
            define(g, glue::rjs_global(g), "nanV", v, 0);
            ok(&c, "Number.isNaN(nanV)");
        }
    }

    /// Bytes made in one context decode in another context on another thread
    /// (the Worker seam): nothing in a `Serialized` points into a heap.
    #[test]
    fn bytes_cross_threads() {
        let c = ctx();
        let s = unsafe {
            ser(&c, "(() => { const o = { n: 1.25, s: 'x\\u{1F600}', big: 2n ** 70n, d: new Date(5), u: new Uint16Array([1, 65535]), \
                       e: new TypeError('t'), b: new Blob(['blob']) }; o.me = o; return o; })()", false).unwrap()
        };
        drop(c);
        let out = std::thread::spawn(move || {
            let c2 = ctx();
            unsafe {
                let v = deserialize(c2.raw(), s).unwrap();
                define(c2.raw(), glue::rjs_global(c2.raw()), "got", v, 0);
            }
            crate::webapi::testing::s(&c2, "[got.me === got, got.n, got.s === 'x\\u{1F600}', got.big === 2n ** 70n, got.d.getTime(), \
                 got.u[1], got.e instanceof TypeError, got.b instanceof Blob, got.b.size].join()")
        })
        .join()
        .unwrap();
        assert_eq!(out, "true,1.25,true,true,5,65535,true,true,4");
    }

    #[test]
    fn deep_graphs_throw_range_error_not_overflow() {
        // JSC and native recursion share the host thread's stack. Match the
        // production UI's explicit 8 MiB instead of libtest's 2 MiB default.
        std::thread::Builder::new()
            .name("serialize-production-stack".into())
            .stack_size(8 * 1024 * 1024)
            .spawn(|| {
                let c = ctx();
                ok(&c, "(() => { let o = {}; for (let i = 0; i < 500; i++) o = { o }; let r = structuredClone(o); let n = 0; while (r.o) { r = r.o; n++; } return n === 500; })()");
                assert_deep_graph_refusal(&c);
            })
            .expect("spawn production-stack serializer test")
            .join()
            .unwrap();
    }

    #[test]
    fn deep_graphs_on_small_stack_refuse_safely() {
        // A smaller embedder stack must still produce a catchable RangeError
        // and leave its context usable, rather than overflowing native frames.
        std::thread::Builder::new()
            .name("serialize-small-stack".into())
            .stack_size(2 * 1024 * 1024)
            .spawn(|| {
                let c = ctx();
                assert_deep_graph_refusal(&c);
            })
            .expect("spawn small-stack serializer test")
            .join()
            .unwrap();
    }

    fn assert_deep_graph_refusal(c: &crate::api::Context) {
        assert_eq!(
            s(c, "(() => { let a = []; for (let i = 0; i < 100000; i++) a = [a]; try { structuredClone(a); return 'cloned'; } catch (e) { return e.constructor.name; } })()"),
            "RangeError"
        );
        ok(c, "structuredClone([1])[0] === 1");
    }
}