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
//! Native `UIEvent` / `MouseEvent` / `KeyboardEvent` / `WheelEvent` /
//! `FocusEvent` / `InputEvent` / `PointerEvent` / `DragEvent` and the
//! `DataTransfer` a drag event carries (UI Events, Pointer Events, Input
//! Events, HTML §Drag and drop).
//!
//! These are data carriers. Dispatch belongs to `events.rs` and is untouched:
//! each of these is a real subclass of the existing native `Event`, so
//! `dispatchEvent(new MouseEvent('click'))` goes through the one dispatch path
//! that already exists, `instanceof Event` is true, and the listener sees the
//! same object it would in a browser.
//!
//! Field storage follows `events.rs` exactly — the private `[[s_event]]` slot
//! object — rather than a second Rust-side store. Dispatch already reads that
//! slot, and two stores for one event is how a `type` read and a `type` write
//! end up disagreeing.
//!
//! ## Deliberate narrowings, each because there is no layout or hit-testing
//!
//! - `offsetX` / `offsetY` are defined relative to the target's padding edge
//!   (CSSOM View §"offsetX"). That needs layout. They read back exactly what
//!   the init dictionary supplied, defaulting to 0 — never a guess derived from
//!   `clientX`, which would be a plausible wrong number.
//! - `pageX` / `pageY` are `clientX + scrollX` (CSSOM View §"pageX"). There is
//!   no scrolling viewport here, so `scrollX` is 0 and they equal `clientX` /
//!   `clientY`. That is the correct value for this environment, not a stub.
//! - `getCoalescedEvents()` / `getPredictedEvents()` return an empty list.
//!   For a script-constructed event that is what the specification requires
//!   (Pointer Events §"getCoalescedEvents" — the list is the UA's, and a
//!   synthesized event has none). A host that starts delivering *trusted*
//!   pointer events with coalescing would have to fill these.
//! - `DataTransfer` is the HTML drag data store: string and file items,
//!   `items` (a `DataTransferItemList` of `DataTransferItem`s), `files` (a
//!   `FileList`), and the four modes — read/write for a script-made one,
//!   protected for `dragenter` / `dragover` / `dragleave` (types visible,
//!   data and files not), read-only for `drop`, and disabled after. The HOST
//!   makes the one an OS drop carries ([`create_drag_data`]) and moves it
//!   between modes ([`set_drag_data_mode`]). `webkitGetAsEntry` is absent
//!   (there is no FileSystemEntry); `getAsFileSystemHandle` exists only while
//!   the host has installed it ([`set_file_system_handle_hook`]).
//! - `KeyboardEvent` carries no legacy `keyCode` / `charCode`. Those are a
//!   layout-dependent mapping from a physical key to a Windows virtual-key
//!   code, and there is no layout table here — a made-up number would be
//!   plausible and wrong in exactly the way that breaks a shortcut handler.
//!   `key` (the character) and `code` (the physical key) are the members the
//!   host fills, and they are what current specifications define.
//! - `KeyboardEvent.getModifierState` answers the same four modifiers as
//!   `MouseEvent`'s, for the same reason: those are the only key states this
//!   runtime tracks.
//!
//! ## One deviation from the file's "every number goes through `num`" rule
//!
//! `KeyboardEvent.location` and `WheelEvent.deltaMode` are WebIDL
//! `unsigned long` *enumerations* — their whole use is `=== DOM_KEY_LOCATION_*`
//! / `=== DOM_DELTA_*`. A fractional or negative value there would silently
//! match no branch, so those two get a real ToUint32 (`uint32`).
//! `button` / `buttons` / `detail` stay on `num`: they are opaque numbers,
//! never compared against a constant table.

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

use super::events;
use super::util::*;
use crate::capi;
use crate::glue;

// ---------------------------------------------------------------------------
// DataTransfer (the drag data store)
// ---------------------------------------------------------------------------

/// HTML §"drag data store mode", plus the state after the event: the object
/// "no longer associated with a drag data store".
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DragDataMode {
    /// `dragstart`, and every `new DataTransfer()`: readable and writable.
    ReadWrite,
    /// `drop`: everything readable, nothing writable.
    ReadOnly,
    /// `dragenter` / `dragover` / `dragleave`: the kinds and types of the
    /// items are visible; their data and files are not.
    Protected,
    /// The event is over: no items, no data, nothing writable.
    Disabled,
}

impl DragDataMode {
    fn readable(self) -> bool {
        matches!(self, DragDataMode::ReadWrite | DragDataMode::ReadOnly)
    }
}

/// One item of the drag data store's item list.
#[derive(Clone, Debug, PartialEq)]
pub enum ItemKind {
    /// A string item; the `String` is its data.
    Text(String),
    /// A file item. The `File` itself lives in the DataTransfer's JS-side
    /// slot under the item's id; `path` is set for a host drop (what
    /// `getAsFileSystemHandle` is handed), `None` for `items.add(file)`.
    File { path: Option<std::path::PathBuf>, is_dir: bool },
}

#[derive(Clone, Debug, PartialEq)]
pub struct Item {
    /// Never reused within a DataTransfer: a `DataTransferItem` whose item
    /// was removed must not come back to life as a later one.
    pub id: u64,
    pub kind: ItemKind,
    /// The item's type string (a format for text, the File's type for a file).
    pub mime: String,
}

pub struct DataTransferData {
    /// The item list, in order (`types` and `items` are order-sensitive).
    pub items: Vec<Item>,
    pub drop_effect: String,
    pub effect_allowed: String,
    pub mode: DragDataMode,
    next_id: u64,
}

impl Default for DataTransferData {
    /// What `new DataTransfer()` makes (HTML §"DataTransfer()").
    fn default() -> Self {
        DataTransferData { items: Vec::new(), drop_effect: "none".into(), effect_allowed: "none".into(), mode: DragDataMode::ReadWrite, next_id: 1 }
    }
}

/// HTML §"the DataTransfer interface": `"text"` is an alias for
/// `"text/plain"` and `"url"` for `"text/uri-list"`; otherwise the format is
/// ASCII-lowercased.
pub fn normalize_format(format: &str) -> String {
    let f = format.to_ascii_lowercase();
    match f.as_str() {
        "text" => "text/plain".to_string(),
        "url" => "text/uri-list".to_string(),
        _ => f,
    }
}

impl DataTransferData {
    fn text_index(&self, f: &str) -> Option<usize> {
        self.items.iter().position(|i| matches!(i.kind, ItemKind::Text(_)) && i.mime == f)
    }

    /// `getData` over the item list (the mode is the caller's business).
    /// `"url"` reads `text/uri-list` and answers its first URL: the first
    /// line that is not a `#` comment.
    pub fn get(&self, format: &str) -> String {
        let f = normalize_format(format);
        let Some(i) = self.text_index(&f) else { return String::new() };
        let ItemKind::Text(data) = &self.items[i].kind else { return String::new() };
        if format.eq_ignore_ascii_case("url") {
            return data.split(['\r', '\n']).find(|l| !l.is_empty() && !l.starts_with('#')).unwrap_or("").to_string();
        }
        data.clone()
    }

    /// `setData`: the old item of that format goes and the new one is
    /// appended — so it moves to the end of `types`, as the spec says.
    pub fn set(&mut self, format: &str, data: &str) {
        let f = normalize_format(format);
        if let Some(i) = self.text_index(&f) {
            self.items.remove(i);
        }
        self.push(ItemKind::Text(data.to_string()), f);
    }

    /// `clearData`: string items only — "does not affect whether any files
    /// were included in the drag".
    pub fn clear(&mut self, format: Option<&str>) {
        match format {
            Some(f) => {
                let f = normalize_format(f);
                if let Some(i) = self.text_index(&f) {
                    self.items.remove(i);
                }
            }
            None => self.items.retain(|i| !matches!(i.kind, ItemKind::Text(_))),
        }
    }

    /// `types`: the string items' formats, then `"Files"` if there is a file.
    pub fn types(&self) -> Vec<String> {
        if self.mode == DragDataMode::Disabled {
            return Vec::new();
        }
        let mut t: Vec<String> = self.items.iter().filter(|i| matches!(i.kind, ItemKind::Text(_))).map(|i| i.mime.clone()).collect();
        if self.items.iter().any(|i| matches!(i.kind, ItemKind::File { .. })) {
            t.push("Files".into());
        }
        t
    }

    fn push(&mut self, kind: ItemKind, mime: String) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        self.items.push(Item { id, kind, mime });
        id
    }

    /// The items as `items` shows them: none once the event is over.
    fn visible(&self) -> &[Item] {
        if self.mode == DragDataMode::Disabled { &[] } else { &self.items }
    }
}

// ---------------------------------------------------------------------------
// Per-context classes
// ---------------------------------------------------------------------------

struct UiClasses {
    ui_event: Class,
    mouse: Class,
    keyboard: Class,
    wheel: Class,
    focus: Class,
    input: Class,
    pointer: Class,
    drag: Class,
    dt_cls: capi::JSClassRef,
    dt: Class,
    list_cls: capi::JSClassRef,
    list: Class,
    item_cls: capi::JSClassRef,
    item: Class,
    /// DataTransfer → its slot bag: `list` (the one DataTransferItemList),
    /// `files` and `objs` (id → File / DataTransferItem, null-prototype).
    s_dt: *mut c_void,
    /// DataTransferItemList / DataTransferItem → their DataTransfer.
    s_owner: *mut c_void,
    /// `Object.defineProperty` / `Object.freeze`, as installed.
    define_property: u64,
    freeze: u64,
}
unsafe impl Send for UiClasses {}
unsafe impl Sync for UiClasses {}

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

fn classes(g: *mut c_void) -> &'static UiClasses {
    registry().lock().unwrap().get(&(g as usize)).copied().expect("webapi::uievents not installed")
}

/// The host's `getAsFileSystemHandle` (File System Access): given a dropped
/// item's path and whether it is a directory, a `FileSystemHandle` value
/// (`Ok`) or the exception the promise rejects with (`Err`). Called on the JS
/// thread with the item's drag data store still readable.
pub type HandleHook = unsafe fn(g: *mut c_void, path: &std::path::Path, is_dir: bool) -> Result<u64, u64>;

fn handle_hooks() -> &'static Mutex<HashMap<usize, HandleHook>> {
    static H: OnceLock<Mutex<HashMap<usize, HandleHook>>> = OnceLock::new();
    H.get_or_init(|| Mutex::new(HashMap::new()))
}

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

/// The `[[s_event]]` slot object — the same one `events.rs` fills and dispatch
/// reads.
unsafe fn slots(g: *mut c_void, this: u64) -> Option<u64> {
    let s = get_a(g, this, state(g).s_event);
    if is_cell(s) { Some(s) } else { None }
}

/// A number member read from the init dictionary, or its default when absent.
unsafe fn num(g: *mut c_void, v: u64, default: f64) -> u64 {
    if v == UNDEF { enc_f64(default) } else { enc_f64(read_number(g, v)) }
}

unsafe fn flag(g: *mut c_void, v: u64) -> u64 {
    enc_bool(to_bool(g, v))
}

fn obj_or_null(v: u64) -> u64 {
    if v == UNDEF { NULL } else { v }
}

unsafe fn string_or(g: *mut c_void, v: u64, default: &str) -> u64 {
    if v == UNDEF {
        js_string(g, default)
    } else if glue::rjs_kind(g, v) == 5 {
        v
    } else {
        js_string(g, &read_string(g, v).unwrap_or_default())
    }
}

/// A `DOMString?` member whose default is null (`InputEvent.data`): an explicit
/// `null` stays null rather than becoming the string `"null"`.
unsafe fn nullable_string(g: *mut c_void, v: u64) -> u64 {
    if v == UNDEF || v == NULL { NULL } else { string_or(g, v, "") }
}

/// ECMA-262 ToUint32, for the enumerated `unsigned long` members — see the
/// module header for why these two do not just use `num`.
fn to_uint32(d: f64) -> u32 {
    if !d.is_finite() || d == 0.0 {
        return 0;
    }
    d.trunc().rem_euclid(4_294_967_296.0) as u32
}

unsafe fn uint32(g: *mut c_void, v: u64) -> u64 {
    if v == UNDEF { enc_f64(0.0) } else { enc_f64(to_uint32(read_number(g, v)) as f64) }
}

// ---------------------------------------------------------------------------
// Members. Each level reads its init members and defines its slots with one
// engine call each; the names are the attributes the getters read.
// ---------------------------------------------------------------------------

/// WebIDL constants — on the interface object *and* the prototype.
const KEY_LOCATIONS: &[(&str, i32)] = &[
    ("DOM_KEY_LOCATION_STANDARD", 0),
    ("DOM_KEY_LOCATION_LEFT", 1),
    ("DOM_KEY_LOCATION_RIGHT", 2),
    ("DOM_KEY_LOCATION_NUMPAD", 3),
];

const DELTA_MODES: &[(&str, i32)] =
    &[("DOM_DELTA_PIXEL", 0), ("DOM_DELTA_LINE", 1), ("DOM_DELTA_PAGE", 2)];

const UI: &[&str] = &["view", "detail", "which"];

pub(crate) unsafe fn init_ui_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    ui_slots(g, obj, ty, init);
}

unsafe fn ui_slots(g: *mut c_void, obj: u64, ty: u64, init: u64) -> u64 {
    let s = events::init_event_slots(g, obj, ty, init, false);
    let mut m = [UNDEF; 3];
    get_many(g, init, UI, &mut m);
    // Legacy `which`: 0 unless the init dict says otherwise (UI Events, legacy
    // section). Mouse events override it.
    define_many(g, s, UI, &[obj_or_null(m[0]), num(g, m[1], 0.0), num(g, m[2], 0.0)], 0);
    s
}

const MOUSE: &[&str] = &[
    "screenX", "screenY", "clientX", "clientY", "offsetX", "offsetY", "movementX", "movementY",
    "button", "buttons", "ctrlKey", "shiftKey", "altKey", "metaKey", "relatedTarget", "which",
];

unsafe fn init_mouse_event(g: *mut c_void, obj: u64, ty: u64, init: u64) -> u64 {
    let s = ui_slots(g, obj, ty, init);
    let mut m = [UNDEF; 15];
    get_many(g, init, &MOUSE[..15], &mut m);
    let mut v = [UNDEF; 16];
    for i in 0..10 {
        v[i] = num(g, m[i], 0.0);
    }
    for i in 10..14 {
        v[i] = flag(g, m[i]);
    }
    v[14] = obj_or_null(m[14]);
    // `which` for a mouse event is button + 1 (legacy).
    v[15] = enc_f64(dec_f64(v[8]).unwrap_or(0.0) + 1.0);
    define_many(g, s, MOUSE, &v, 0);
    s
}

unsafe fn init_mouse(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    init_mouse_event(g, obj, ty, init);
}

const KEYBOARD: &[&str] = &["key", "code", "location", "ctrlKey", "shiftKey", "altKey", "metaKey", "repeat", "isComposing"];

unsafe fn init_keyboard_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    let s = ui_slots(g, obj, ty, init);
    let mut m = [UNDEF; 9];
    get_many(g, init, KEYBOARD, &mut m);
    let mut v = [UNDEF; 9];
    v[0] = string_or(g, m[0], "");
    v[1] = string_or(g, m[1], "");
    v[2] = uint32(g, m[2]);
    for i in 3..9 {
        v[i] = flag(g, m[i]);
    }
    define_many(g, s, KEYBOARD, &v, 0);
}

const WHEEL: &[&str] = &["deltaX", "deltaY", "deltaZ", "deltaMode"];

unsafe fn init_wheel_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    let s = init_mouse_event(g, obj, ty, init);
    let mut m = [UNDEF; 4];
    get_many(g, init, WHEEL, &mut m);
    define_many(g, s, WHEEL, &[num(g, m[0], 0.0), num(g, m[1], 0.0), num(g, m[2], 0.0), uint32(g, m[3])], 0);
}

const FOCUS: &[&str] = &["relatedTarget"];

unsafe fn init_focus_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    let s = ui_slots(g, obj, ty, init);
    let mut m = [UNDEF; 1];
    get_many(g, init, FOCUS, &mut m);
    define_many(g, s, FOCUS, &[obj_or_null(m[0])], 0);
}

const INPUT: &[&str] = &["data", "inputType", "isComposing"];

unsafe fn init_input_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    let s = ui_slots(g, obj, ty, init);
    let mut m = [UNDEF; 3];
    get_many(g, init, INPUT, &mut m);
    define_many(g, s, INPUT, &[nullable_string(g, m[0]), string_or(g, m[1], ""), flag(g, m[2])], 0);
}

const POINTER: &[&str] = &[
    "pointerId", "width", "height", "pressure", "tangentialPressure", "tiltX", "tiltY", "twist",
    "altitudeAngle", "azimuthAngle", "pointerType", "isPrimary",
];
/// Spec defaults: altitude π/2 (perpendicular), azimuth 0.
const POINTER_DEFAULTS: [f64; 10] = [0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, std::f64::consts::FRAC_PI_2, 0.0];

unsafe fn init_pointer_event(g: *mut c_void, obj: u64, ty: u64, init: u64) {
    let s = init_mouse_event(g, obj, ty, init);
    let mut m = [UNDEF; 12];
    get_many(g, init, POINTER, &mut m);
    let mut v = [UNDEF; 12];
    for i in 0..10 {
        v[i] = num(g, m[i], POINTER_DEFAULTS[i]);
    }
    v[10] = string_or(g, m[10], "");
    v[11] = flag(g, m[11]);
    define_many(g, s, POINTER, &v, 0);
}

const DRAG: &[&str] = &["dataTransfer"];

// ---------------------------------------------------------------------------
// Getters
// ---------------------------------------------------------------------------

unsafe fn slot_get(g: *mut c_void, cf: *mut c_void, key: &'static str, label: &str) -> u64 {
    match get_in(g, cf_this(cf), state(g).s_event, key) {
        Some(v) if v == UNDEF => NULL,
        Some(v) => v,
        None => throw_type_error(g, &format!("{label} called on a non-Event")),
    }
}

macro_rules! slot_getters {
    ($($fn_name:ident, $key:literal, $label:literal;)*) => {
        $(
            #[crate::host_fn]
            unsafe fn $fn_name(g: *mut c_void, cf: *mut c_void) -> u64 {
                slot_get(g, cf, $key, $label)
            }
        )*
    };
}

slot_getters! {
    ui_view, "view", "UIEvent.view";
    ui_detail, "detail", "UIEvent.detail";
    ui_which, "which", "UIEvent.which";
    m_screen_x, "screenX", "MouseEvent.screenX";
    m_screen_y, "screenY", "MouseEvent.screenY";
    m_client_x, "clientX", "MouseEvent.clientX";
    m_client_y, "clientY", "MouseEvent.clientY";
    m_offset_x, "offsetX", "MouseEvent.offsetX";
    m_offset_y, "offsetY", "MouseEvent.offsetY";
    m_movement_x, "movementX", "MouseEvent.movementX";
    m_movement_y, "movementY", "MouseEvent.movementY";
    m_button, "button", "MouseEvent.button";
    m_buttons, "buttons", "MouseEvent.buttons";
    m_ctrl, "ctrlKey", "MouseEvent.ctrlKey";
    m_shift, "shiftKey", "MouseEvent.shiftKey";
    m_alt, "altKey", "MouseEvent.altKey";
    m_meta, "metaKey", "MouseEvent.metaKey";
    m_related, "relatedTarget", "MouseEvent.relatedTarget";
    k_key, "key", "KeyboardEvent.key";
    k_code, "code", "KeyboardEvent.code";
    k_location, "location", "KeyboardEvent.location";
    k_repeat, "repeat", "KeyboardEvent.repeat";
    k_composing, "isComposing", "KeyboardEvent.isComposing";
    k_ctrl, "ctrlKey", "KeyboardEvent.ctrlKey";
    k_shift, "shiftKey", "KeyboardEvent.shiftKey";
    k_alt, "altKey", "KeyboardEvent.altKey";
    k_meta, "metaKey", "KeyboardEvent.metaKey";
    w_delta_x, "deltaX", "WheelEvent.deltaX";
    w_delta_y, "deltaY", "WheelEvent.deltaY";
    w_delta_z, "deltaZ", "WheelEvent.deltaZ";
    w_delta_mode, "deltaMode", "WheelEvent.deltaMode";
    f_related, "relatedTarget", "FocusEvent.relatedTarget";
    i_data, "data", "InputEvent.data";
    i_input_type, "inputType", "InputEvent.inputType";
    i_composing, "isComposing", "InputEvent.isComposing";
    p_id, "pointerId", "PointerEvent.pointerId";
    p_width, "width", "PointerEvent.width";
    p_height, "height", "PointerEvent.height";
    p_pressure, "pressure", "PointerEvent.pressure";
    p_tangential, "tangentialPressure", "PointerEvent.tangentialPressure";
    p_tilt_x, "tiltX", "PointerEvent.tiltX";
    p_tilt_y, "tiltY", "PointerEvent.tiltY";
    p_twist, "twist", "PointerEvent.twist";
    p_altitude, "altitudeAngle", "PointerEvent.altitudeAngle";
    p_azimuth, "azimuthAngle", "PointerEvent.azimuthAngle";
    p_type, "pointerType", "PointerEvent.pointerType";
    p_primary, "isPrimary", "PointerEvent.isPrimary";
    d_data_transfer, "dataTransfer", "DragEvent.dataTransfer";
}

/// CSSOM View §"pageX": `clientX + scrollX`, and there is no scrolling
/// viewport here, so the scroll offset is 0.
#[crate::host_fn]
unsafe fn m_page_x(g: *mut c_void, cf: *mut c_void) -> u64 {
    slot_get(g, cf, "clientX", "MouseEvent.pageX")
}
#[crate::host_fn]
unsafe fn m_page_y(g: *mut c_void, cf: *mut c_void) -> u64 {
    slot_get(g, cf, "clientY", "MouseEvent.pageY")
}
/// `x`/`y` are defined as aliases of `clientX`/`clientY`.
#[crate::host_fn]
unsafe fn m_x(g: *mut c_void, cf: *mut c_void) -> u64 {
    slot_get(g, cf, "clientX", "MouseEvent.x")
}
#[crate::host_fn]
unsafe fn m_y(g: *mut c_void, cf: *mut c_void) -> u64 {
    slot_get(g, cf, "clientY", "MouseEvent.y")
}

/// `getModifierState` is the same operation on every event that mixes in
/// `KeyboardEventInit`'s modifiers — one body, one label per interface.
unsafe fn modifier_state(g: *mut c_void, cf: *mut c_void, label: &str) -> u64 {
    let key = read_string(g, cf_arg(cf, 0)).unwrap_or_default();
    let Some(s) = slots(g, cf_this(cf)) else {
        return throw_type_error(g, &format!("{label} called on a non-Event"));
    };
    // UI Events §"modifier keys": only the four this runtime tracks can be
    // true. Every other named modifier is legitimately false — there is no key
    // state source for CapsLock or AltGraph here.
    let slot = match key.as_str() {
        "Control" => "ctrlKey",
        "Shift" => "shiftKey",
        "Alt" => "altKey",
        "Meta" => "metaKey",
        _ => return FALSE,
    };
    enc_bool(truthy_flag(get(g, s, slot)))
}

#[crate::host_fn]
unsafe fn m_get_modifier_state(g: *mut c_void, cf: *mut c_void) -> u64 {
    modifier_state(g, cf, "MouseEvent.getModifierState")
}

#[crate::host_fn]
unsafe fn k_get_modifier_state(g: *mut c_void, cf: *mut c_void) -> u64 {
    modifier_state(g, cf, "KeyboardEvent.getModifierState")
}

#[crate::host_fn]
unsafe fn p_empty_list(g: *mut c_void, _cf: *mut c_void) -> u64 {
    new_array(g)
}

// ---------------------------------------------------------------------------
// Constructors
// ---------------------------------------------------------------------------

macro_rules! event_ctor {
    ($name:ident, $field:ident, $init:path, $label:literal) => {
        #[crate::host_fn]
        unsafe fn $name(g: *mut c_void, cf: *mut c_void) -> u64 {
            if cf_argc(cf) < 1 {
                return throw_type_error(g, concat!($label, " constructor requires a type argument"));
            }
            let c = classes(g);
            let obj = ordinary_create(g, cf_new_target(cf), c.$field.proto);
            $init(g, obj, cf_arg(cf, 0), cf_arg(cf, 1));
            obj
        }
    };
}

event_ctor!(ui_construct, ui_event, init_ui_event, "UIEvent");
event_ctor!(mouse_construct, mouse, init_mouse, "MouseEvent");
event_ctor!(keyboard_construct, keyboard, init_keyboard_event, "KeyboardEvent");
event_ctor!(wheel_construct, wheel, init_wheel_event, "WheelEvent");
event_ctor!(focus_construct, focus, init_focus_event, "FocusEvent");
event_ctor!(input_construct, input, init_input_event, "InputEvent");
event_ctor!(pointer_construct, pointer, init_pointer_event, "PointerEvent");

#[crate::host_fn]
unsafe fn drag_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "DragEvent constructor requires a type argument");
    }
    let c = classes(g);
    let obj = ordinary_create(g, cf_new_target(cf), c.drag.proto);
    let s = init_mouse_event(g, obj, cf_arg(cf, 0), cf_arg(cf, 1));
    let mut m = [UNDEF; 1];
    get_many(g, cf_arg(cf, 1), DRAG, &mut m);
    define_many(g, s, DRAG, &[obj_or_null(m[0])], 0);
    obj
}

// ---------------------------------------------------------------------------
// DataTransfer
// ---------------------------------------------------------------------------

unsafe fn dt_of<'a>(g: *mut c_void, v: u64) -> Option<&'a mut DataTransferData> {
    host_data::<DataTransferData>(g, classes(g).dt_cls, v)
}

unsafe fn this_dt<'a>(g: *mut c_void, cf: *mut c_void) -> Option<&'a mut DataTransferData> {
    dt_of(g, cf_this(cf))
}

/// The DataTransfer's slot bag (see `UiClasses::s_dt`).
unsafe fn bag(g: *mut c_void, dt: u64) -> u64 {
    get_a(g, dt, classes(g).s_dt)
}

/// A DataTransfer with its bag and its one DataTransferItemList.
unsafe fn make_dt(g: *mut c_void, proto: u64, data: DataTransferData) -> u64 {
    let c = classes(g);
    let dt = host_create(g, c.dt_cls, proto, data);
    let b = new_object(g, NULL);
    define(g, b, "files", new_object(g, NULL), 0);
    define(g, b, "objs", new_object(g, NULL), 0);
    let list = host_create(g, c.list_cls, c.list.proto, DtListData { shown: 0 });
    glue::rjs_define(g, list, c.s_owner, dt, 1 | 2 | 4);
    define(g, b, "list", list, 0);
    glue::rjs_define(g, dt, c.s_dt, b, 1 | 2 | 4);
    dt
}

#[crate::host_fn]
unsafe fn dt_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    let c = classes(g);
    let proto = ordinary_create_proto(g, cf_new_target(cf), c.dt.proto);
    make_dt(g, proto, DataTransferData::default())
}

unsafe fn ordinary_create_proto(g: *mut c_void, new_target: u64, fallback: u64) -> u64 {
    let p = if is_cell(new_target) { get(g, new_target, "prototype") } else { UNDEF };
    if is_cell(p) { p } else { fallback }
}

/// The cached `DataTransferItem` for item `id` (the same object every time).
unsafe fn item_obj(g: *mut c_void, dt: u64, id: u64) -> u64 {
    let c = classes(g);
    let objs = get(g, bag(g, dt), "objs");
    let key = id.to_string();
    let v = get(g, objs, &key);
    if is_cell(v) {
        return v;
    }
    let o = host_create(g, c.item_cls, c.item.proto, DtItemData { id });
    glue::rjs_define(g, o, c.s_owner, dt, 1 | 2 | 4);
    define(g, objs, &key, o, 0);
    o
}

/// Makes the list's own index properties match the item list again, and
/// lets go of the Files and item objects of items that are gone. Runs after
/// every change to the item list or the mode.
unsafe fn resync(g: *mut c_void, dt: u64) {
    let c = classes(g);
    let Some(d) = dt_of(g, dt) else { return };
    let ids: Vec<u64> = d.visible().iter().map(|i| i.id).collect();
    let all: Vec<u64> = d.items.iter().map(|i| i.id).collect();
    let b = bag(g, dt);
    let list = get(g, b, "list");
    let Some(ld) = host_data::<DtListData>(g, c.list_cls, list) else { return };
    let shown = ld.shown;
    ld.shown = ids.len() as u32;
    for i in ids.len() as u32..shown {
        let a = atom(g, &i.to_string());
        glue::rjs_delete(g, list, a);
        glue::rjs_atom_free(g, a);
    }
    for (i, id) in ids.iter().enumerate() {
        let o = item_obj(g, dt, *id);
        // A legacy platform object's index: read-only, enumerable,
        // configurable (WebIDL) — configurable is also what lets the next
        // resync take it away.
        let desc = new_object(g, NULL);
        define(g, desc, "value", o, 0);
        define(g, desc, "enumerable", TRUE, 0);
        define(g, desc, "configurable", TRUE, 0);
        let _ = call(g, c.define_property, UNDEF, &[list, enc_i32(i as i32), desc]);
    }
    for map in ["files", "objs"] {
        let m = get(g, b, map);
        let mut keys = UNDEF;
        if glue::rjs_own_keys(g, m, &mut keys) != 0 {
            continue;
        }
        for k in 0..array_len(g, keys) {
            let Some(name) = read_string(g, get_i(g, keys, k)) else { continue };
            if name.parse::<u64>().is_ok_and(|id| !all.contains(&id)) {
                let a = atom(g, &name);
                glue::rjs_delete(g, m, a);
                glue::rjs_atom_free(g, a);
            }
        }
    }
}

/// `DOMString` argument `i` of a method that requires `n`, converted with
/// ToString (rethrowing); `Err` = the exception is pending.
unsafe fn string_arg(g: *mut c_void, cf: *mut c_void, i: u32, n: u32, who: &str) -> Result<String, ()> {
    let argc = cf_argc(cf);
    if argc < n {
        throw_type_error(g, &format!("Failed to execute {who}: {n} argument{} required, but only {argc} present.", if n == 1 { "" } else { "s" }));
        return Err(());
    }
    let s = to_js_string(g, cf_arg(cf, i))?;
    Ok(read_string(g, s).unwrap_or_default())
}

#[crate::host_fn]
unsafe fn dt_get_data(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.getData called on a non-DataTransfer");
    }
    let Ok(format) = string_arg(g, cf, 0, 1, "'getData' on 'DataTransfer'") else { return UNDEF };
    let Some(d) = this_dt(g, cf) else { return UNDEF };
    // Protected (dragenter/over/leave): types are visible, data is not.
    let v = if d.mode.readable() { d.get(&format) } else { String::new() };
    js_string(g, &v)
}

#[crate::host_fn]
unsafe fn dt_set_data(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.setData called on a non-DataTransfer");
    }
    let Ok(format) = string_arg(g, cf, 0, 2, "'setData' on 'DataTransfer'") else { return UNDEF };
    let Ok(data) = string_arg(g, cf, 1, 2, "'setData' on 'DataTransfer'") else { return UNDEF };
    let Some(d) = this_dt(g, cf) else { return UNDEF };
    if d.mode == DragDataMode::ReadWrite {
        d.set(&format, &data);
        resync(g, cf_this(cf));
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn dt_clear_data(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.clearData called on a non-DataTransfer");
    }
    let format = if cf_arg(cf, 0) == UNDEF {
        None
    } else {
        let Ok(f) = string_arg(g, cf, 0, 1, "'clearData' on 'DataTransfer'") else { return UNDEF };
        Some(f)
    };
    let Some(d) = this_dt(g, cf) else { return UNDEF };
    if d.mode == DragDataMode::ReadWrite {
        d.clear(format.as_deref());
        resync(g, cf_this(cf));
    }
    UNDEF
}

/// `types`: a frozen array (a new one per read).
#[crate::host_fn]
unsafe fn dt_types(g: *mut c_void, cf: *mut c_void) -> u64 {
    let Some(d) = this_dt(g, cf) else {
        return throw_type_error(g, "DataTransfer.types called on a non-DataTransfer");
    };
    let formats = d.types();
    let arr = new_array(g);
    for (i, f) in formats.iter().enumerate() {
        let v = js_string(g, f);
        set_i(g, arr, i as u32, v);
    }
    let _ = call(g, classes(g).freeze, UNDEF, &[arr]);
    arr
}

/// `files`: a new FileList per read over the SAME File objects, empty unless
/// the data is readable (HTML: protected mode shows no files).
#[crate::host_fn]
unsafe fn dt_files(g: *mut c_void, cf: *mut c_void) -> u64 {
    let this = cf_this(cf);
    let Some(d) = this_dt(g, cf) else {
        return throw_type_error(g, "DataTransfer.files called on a non-DataTransfer");
    };
    let ids: Vec<u64> = if d.mode.readable() {
        d.items.iter().filter(|i| matches!(i.kind, ItemKind::File { .. })).map(|i| i.id).collect()
    } else {
        Vec::new()
    };
    let files_map = get(g, bag(g, this), "files");
    let files: Vec<u64> = ids.iter().map(|id| get(g, files_map, &id.to_string())).filter(|v| is_cell(*v)).collect();
    super::file::create_file_list(g, &files)
}

#[crate::host_fn]
unsafe fn dt_items(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.items called on a non-DataTransfer");
    }
    get(g, bag(g, cf_this(cf)), "list")
}

#[crate::host_fn]
unsafe fn dt_get_drop(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_dt(g, cf) {
        Some(d) => {
            let v = d.drop_effect.clone();
            js_string(g, &v)
        }
        None => throw_type_error(g, "DataTransfer.dropEffect called on a non-DataTransfer"),
    }
}

/// `dropEffect` takes one of its four values in any mode; anything else is
/// ignored (HTML).
#[crate::host_fn]
unsafe fn dt_set_drop(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.dropEffect called on a non-DataTransfer");
    }
    let Ok(s) = to_js_string(g, cf_arg(cf, 0)) else { return UNDEF };
    let v = read_string(g, s).unwrap_or_default();
    if let Some(d) = this_dt(g, cf) {
        if matches!(v.as_str(), "none" | "copy" | "link" | "move") {
            d.drop_effect = v;
        }
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn dt_get_effect(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_dt(g, cf) {
        Some(d) => {
            let v = d.effect_allowed.clone();
            js_string(g, &v)
        }
        None => throw_type_error(g, "DataTransfer.effectAllowed called on a non-DataTransfer"),
    }
}

/// `effectAllowed` changes only in read/write mode (dragstart), and only to
/// one of its nine values (HTML).
#[crate::host_fn]
unsafe fn dt_set_effect(g: *mut c_void, cf: *mut c_void) -> u64 {
    if this_dt(g, cf).is_none() {
        return throw_type_error(g, "DataTransfer.effectAllowed called on a non-DataTransfer");
    }
    let Ok(s) = to_js_string(g, cf_arg(cf, 0)) else { return UNDEF };
    let v = read_string(g, s).unwrap_or_default();
    if let Some(d) = this_dt(g, cf) {
        let known = matches!(v.as_str(), "none" | "copy" | "copyLink" | "copyMove" | "link" | "linkMove" | "move" | "all" | "uninitialized");
        if known && d.mode == DragDataMode::ReadWrite {
            d.effect_allowed = v;
        }
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn dt_noop(_g: *mut c_void, _cf: *mut c_void) -> u64 {
    UNDEF
}

// ---- DataTransferItemList ---------------------------------------------------

/// What a DataTransferItemList carries natively: how many index properties
/// it shows (what the next resync has to take away). Its items are its
/// DataTransfer's.
struct DtListData {
    shown: u32,
}

/// The list (or item) `v`'s DataTransfer and its data.
unsafe fn owner<'a>(g: *mut c_void, v: u64) -> Option<(u64, &'a mut DataTransferData)> {
    let dt = get_a(g, v, classes(g).s_owner);
    dt_of(g, dt).map(|d| (dt, d))
}

unsafe fn this_list<'a>(g: *mut c_void, cf: *mut c_void, what: &str) -> Result<(u64, &'a mut DataTransferData), u64> {
    let c = classes(g);
    let this = cf_this(cf);
    if host_data::<DtListData>(g, c.list_cls, this).is_none() {
        return Err(throw_type_error(g, &format!("DataTransferItemList.{what} called on a non-DataTransferItemList")));
    }
    owner(g, this).ok_or_else(|| throw_type_error(g, "DataTransferItemList: lost its DataTransfer"))
}

#[crate::host_fn]
unsafe fn list_length(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_list(g, cf, "length") {
        Ok((_, d)) => enc_f64(d.visible().len() as f64),
        Err(e) => e,
    }
}

/// `add(data, type)` / `add(file)`. `null` unless read/write; a second
/// string item of a type is a NotSupportedError.
#[crate::host_fn]
unsafe fn list_add(g: *mut c_void, cf: *mut c_void) -> u64 {
    if let Err(e) = this_list(g, cf, "add") {
        return e;
    }
    let argc = cf_argc(cf);
    // Overload resolution on the argument count: two arguments are
    // (DOMString, DOMString), one must be a File.
    let (kind, mime, file) = if argc >= 2 {
        let Ok(data) = string_arg(g, cf, 0, 2, "'add' on 'DataTransferItemList'") else { return UNDEF };
        let Ok(ty) = string_arg(g, cf, 1, 2, "'add' on 'DataTransferItemList'") else { return UNDEF };
        (ItemKind::Text(data), ty.to_ascii_lowercase(), UNDEF)
    } else if argc == 1 {
        let f = cf_arg(cf, 0);
        if !super::file::is_file(g, f) {
            return throw_type_error(g, "Failed to execute 'add' on 'DataTransferItemList': parameter 1 is not of type 'File'.");
        }
        let mime = super::blob::data_of(g, f).map(|b| b.mime.to_ascii_lowercase()).unwrap_or_default();
        (ItemKind::File { path: None, is_dir: false }, mime, f)
    } else {
        return throw_type_error(g, "Failed to execute 'add' on 'DataTransferItemList': 1 argument required, but only 0 present.");
    };
    let Ok((dt, d)) = this_list(g, cf, "add") else { return UNDEF };
    if d.mode != DragDataMode::ReadWrite {
        return NULL;
    }
    if matches!(kind, ItemKind::Text(_)) && d.text_index(&mime).is_some() {
        return super::domexception::throw(
            g,
            "NotSupportedError",
            &format!("Failed to execute 'add' on 'DataTransferItemList': An item already exists for type '{mime}'."),
        );
    }
    let id = d.push(kind, mime);
    if is_cell(file) {
        define(g, get(g, bag(g, dt), "files"), &id.to_string(), file, 0);
    }
    resync(g, dt);
    item_obj(g, dt, id)
}

#[crate::host_fn]
unsafe fn list_remove(g: *mut c_void, cf: *mut c_void) -> u64 {
    if let Err(e) = this_list(g, cf, "remove") {
        return e;
    }
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to execute 'remove' on 'DataTransferItemList': 1 argument required, but only 0 present.");
    }
    let Ok(n) = to_number(g, cf_arg(cf, 0)) else { return UNDEF };
    let Ok((dt, d)) = this_list(g, cf, "remove") else { return UNDEF };
    // HTML: "not in the read/write mode" throws, the disabled mode included
    // (Chromium: `!CanWriteData()`).
    match d.mode {
        DragDataMode::ReadWrite => {}
        _ => {
            return super::domexception::throw(
                g,
                "InvalidStateError",
                "Failed to execute 'remove' on 'DataTransferItemList': The list is not writable.",
            )
        }
    }
    // `unsigned long`: ToNumber, then modulo 2^32.
    let i = if n.is_finite() { n.trunc().rem_euclid(4_294_967_296.0) as usize } else { 0 };
    if i < d.items.len() {
        d.items.remove(i);
        resync(g, dt);
    }
    UNDEF
}

#[crate::host_fn]
unsafe fn list_clear(g: *mut c_void, cf: *mut c_void) -> u64 {
    let (dt, d) = match this_list(g, cf, "clear") {
        Ok(v) => v,
        Err(e) => return e,
    };
    if d.mode == DragDataMode::ReadWrite {
        d.items.clear();
        resync(g, dt);
    }
    UNDEF
}

// ---- DataTransferItem ---------------------------------------------------------

struct DtItemData {
    id: u64,
}

/// The item behind a DataTransferItem: `None` once it is disabled (removed,
/// or its event is over) — every member then answers as for no item.
unsafe fn this_item(g: *mut c_void, cf: *mut c_void, what: &str) -> Result<Option<(u64, DragDataMode, Item)>, u64> {
    let c = classes(g);
    let this = cf_this(cf);
    let Some(id) = host_data::<DtItemData>(g, c.item_cls, this).map(|d| d.id) else {
        return Err(throw_type_error(g, &format!("DataTransferItem.{what} called on a non-DataTransferItem")));
    };
    let Some((dt, d)) = owner(g, this) else { return Ok(None) };
    Ok(d.visible().iter().find(|i| i.id == id).cloned().map(|i| (dt, d.mode, i)))
}

#[crate::host_fn]
unsafe fn item_kind(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_item(g, cf, "kind") {
        Ok(Some((_, _, i))) => js_string(g, if matches!(i.kind, ItemKind::Text(_)) { "string" } else { "file" }),
        Ok(None) => js_string(g, ""),
        Err(e) => e,
    }
}

#[crate::host_fn]
unsafe fn item_type(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_item(g, cf, "type") {
        Ok(Some((_, _, i))) => js_string(g, &i.mime),
        Ok(None) => js_string(g, ""),
        Err(e) => e,
    }
}

/// The callback and the string, carried to the task that delivers them.
struct AsString {
    callback: u64,
    data: String,
}
unsafe impl Send for AsString {}

/// `getAsString(callback)`: the data is delivered from a queued task, never
/// synchronously, and only for a readable string item.
#[crate::host_fn]
unsafe fn item_get_as_string(g: *mut c_void, cf: *mut c_void) -> u64 {
    let found = match this_item(g, cf, "getAsString") {
        Ok(v) => v,
        Err(e) => return e,
    };
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to execute 'getAsString' on 'DataTransferItem': 1 argument required, but only 0 present.");
    }
    // `FunctionStringCallback?`: undefined converts to null, like null.
    let cb = cf_arg(cf, 0);
    if cb == NULL || cb == UNDEF {
        return UNDEF;
    }
    if glue::rjs_kind(g, cb) != 9 {
        return throw_type_error(g, "Failed to execute 'getAsString' on 'DataTransferItem': The callback provided as parameter 1 is not a function.");
    }
    let Some((_, mode, Item { kind: ItemKind::Text(data), .. })) = found else { return UNDEF };
    if !mode.readable() {
        return UNDEF;
    }
    crate::ffi::redjs_protect(g, cb);
    let task = AsString { callback: cb, data };
    let id = crate::context::id_of(g);
    // A task of its own, delivered within the pump's budget (blob's inbox).
    super::blob::queue_task(
        g,
        Box::new(move |g| unsafe {
            let task = task;
            if crate::context::id_of(g) != id {
                return; // a different context at this address: the value is not its
            }
            let s = js_string(g, &task.data);
            if let Err(exc) = call(g, task.callback, UNDEF, &[s]) {
                crate::errors::report(g, exc, crate::errors::ErrKind::Listener);
            }
            crate::ffi::redjs_unprotect(g, task.callback);
        }),
    );
    UNDEF
}

/// `getAsFile()`: the File of a readable file item, else `null`.
#[crate::host_fn]
unsafe fn item_get_as_file(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_item(g, cf, "getAsFile") {
        Ok(Some((dt, mode, i))) if mode.readable() && matches!(i.kind, ItemKind::File { .. }) => {
            let f = get(g, get(g, bag(g, dt), "files"), &i.id.to_string());
            if is_cell(f) { f } else { NULL }
        }
        Ok(_) => NULL,
        Err(e) => e,
    }
}

/// `getAsFileSystemHandle()` (File System Access): defined only while the host
/// has a hook installed, so `'getAsFileSystemHandle' in item` tells the truth.
/// `null` for a string item, a script-added File, or outside drop/dragstart.
#[crate::host_fn]
unsafe fn item_get_as_handle(g: *mut c_void, cf: *mut c_void) -> u64 {
    // A promise-returning operation REJECTS on a bad `this` (WebIDL), so the
    // brand check makes the TypeError without throwing it.
    if host_data::<DtItemData>(g, classes(g).item_cls, cf_this(cf)).is_none() {
        let e = super::fetch::make_error(
            g,
            super::fetch::TYPE_ERR,
            "",
            "DataTransferItem.getAsFileSystemHandle called on a non-DataTransferItem",
        );
        return super::domexception::rejected_with(g, e);
    }
    let found = match this_item(g, cf, "getAsFileSystemHandle") {
        Ok(v) => v,
        Err(e) => return super::domexception::rejected_with(g, e),
    };
    let hook = handle_hooks().lock().unwrap().get(&(g as usize)).copied();
    let (Some(hook), Some((_, mode, Item { kind: ItemKind::File { path: Some(path), is_dir }, .. }))) = (hook, found) else {
        return resolved_promise(g, NULL);
    };
    if !mode.readable() {
        return resolved_promise(g, NULL);
    }
    match hook(g, &path, is_dir) {
        Ok(h) => resolved_promise(g, h),
        Err(e) => super::domexception::rejected_with(g, e),
    }
}

// ---- the host's side ------------------------------------------------------------

/// One dropped path, as the host's drop handler has it.
#[derive(Clone, Debug)]
pub struct DropEntry {
    pub path: std::path::PathBuf,
    pub is_dir: bool,
    /// `(size, mtime)` if the host stat'ed it already; `None` stats it here.
    pub stat: Option<(u64, std::time::SystemTime)>,
}

/// A DataTransfer for an OS drop: one path-backed `File` per entry (nothing
/// is read), `effectAllowed: "all"`, `dropEffect: "copy"`, in `mode`. The
/// host dispatches `dragenter` / `dragover` / `dragleave` with it in
/// [`DragDataMode::Protected`], switches it to `ReadOnly` for `drop`
/// ([`set_drag_data_mode`]), and to `Disabled` when the event is over.
///
/// A directory becomes a 0-byte File named after it, as in Chromium; reading
/// it fails. Its contents are reachable through `getAsFileSystemHandle` when
/// the host installs that hook (advisory A-F3 says so, once).
///
/// # Safety
/// `g` must be a live context with the web API installed, on its thread.
pub unsafe fn create_drag_data(g: *mut c_void, entries: &[DropEntry], mode: DragDataMode) -> u64 {
    let c = classes(g);
    let data = DataTransferData { effect_allowed: "all".into(), drop_effect: "copy".into(), mode, ..DataTransferData::default() };
    let dt = make_dt(g, c.dt.proto, data);
    let files_map = get(g, bag(g, dt), "files");
    for e in entries {
        let name = e.path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
        let (size, mtime) = match (e.is_dir, e.stat) {
            (false, Some((s, m))) => (s, Some(m)),
            (false, None) => super::file::snapshot(&e.path).unwrap_or((0, None)),
            (true, _) => (0, e.stat.map(|s| s.1).or_else(|| std::fs::metadata(&e.path).and_then(|m| m.modified()).ok())),
        };
        if e.is_dir {
            super::blob::advise(
                "A-F3",
                "File (A-F3): a directory was dropped. It appears in dataTransfer.files as a 0-byte File that cannot be \
                 read, as in Chromium; walk its contents with items[i].getAsFileSystemHandle() where the host provides it."
                    .to_string(),
            );
        }
        let f = super::file::create_file(g, &e.path, &name, if e.is_dir { Some("") } else { None }, size, mtime, "");
        let mime = super::blob::data_of(g, f).map(|b| b.mime.clone()).unwrap_or_default();
        let Some(d) = dt_of(g, dt) else { break };
        let id = d.push(ItemKind::File { path: Some(e.path.clone()), is_dir: e.is_dir }, mime);
        define(g, files_map, &id.to_string(), f, 0);
    }
    resync(g, dt);
    dt
}

/// Moves a DataTransfer to another mode (the host, between drag events).
/// `false` when `dt` is not a DataTransfer.
///
/// # Safety
/// As [`create_drag_data`].
pub unsafe fn set_drag_data_mode(g: *mut c_void, dt: u64, mode: DragDataMode) -> bool {
    let Some(d) = dt_of(g, dt) else { return false };
    d.mode = mode;
    resync(g, dt);
    true
}

/// Installs (or, with `None`, removes) the host's `getAsFileSystemHandle`.
/// The method appears on `DataTransferItem.prototype` only while a hook is
/// installed — feature detection stays honest.
///
/// # Safety
/// As [`create_drag_data`].
pub unsafe fn set_file_system_handle_hook(g: *mut c_void, hook: Option<HandleHook>) {
    match hook {
        Some(h) => {
            handle_hooks().lock().unwrap().insert(g as usize, h);
        }
        None => {
            handle_hooks().lock().unwrap().remove(&(g as usize));
        }
    }
    let Some(c) = registry().lock().unwrap().get(&(g as usize)).copied() else { return };
    define_handle_method(g, c, hook.is_some());
}

unsafe fn define_handle_method(g: *mut c_void, c: &UiClasses, present: bool) {
    let a = atom(g, "getAsFileSystemHandle");
    if present {
        glue::rjs_define(g, c.item.proto, a, make_fn(g, "getAsFileSystemHandle", 0, item_get_as_handle), 2);
    } else {
        glue::rjs_delete(g, c.item.proto, a);
    }
    glue::rjs_atom_free(g, a);
}

// ---------------------------------------------------------------------------
// Install
// ---------------------------------------------------------------------------

/// WebIDL §"Constants": a constant is defined on both the interface object and
/// the interface prototype object, read-only and non-configurable — but
/// enumerable, unlike every method and accessor above.
unsafe fn define_constants(g: *mut c_void, class: &Class, consts: &[(&'static str, i32)]) {
    for (name, value) in consts {
        define(g, class.ctor, name, enc_i32(*value), 1 | 4);
        define(g, class.proto, name, enc_i32(*value), 1 | 4);
    }
}

pub unsafe fn install(g: *mut c_void, event_proto: u64) {
    let ui_event = define_class(g, &ClassSpec {
        name: "UIEvent",
        arity: 1,
        construct: Some(ui_construct),
        parent_proto: event_proto,
        methods: &[],
        getters: &[("view", ui_view), ("detail", ui_detail), ("which", ui_which)],
        accessors: &[],
    });

    let mouse = define_class(g, &ClassSpec {
        name: "MouseEvent",
        arity: 1,
        construct: Some(mouse_construct),
        parent_proto: ui_event.proto,
        methods: &[("getModifierState", 1, m_get_modifier_state)],
        getters: &[
            ("screenX", m_screen_x), ("screenY", m_screen_y),
            ("clientX", m_client_x), ("clientY", m_client_y),
            ("x", m_x), ("y", m_y),
            ("pageX", m_page_x), ("pageY", m_page_y),
            ("offsetX", m_offset_x), ("offsetY", m_offset_y),
            ("movementX", m_movement_x), ("movementY", m_movement_y),
            ("button", m_button), ("buttons", m_buttons),
            ("ctrlKey", m_ctrl), ("shiftKey", m_shift), ("altKey", m_alt), ("metaKey", m_meta),
            ("relatedTarget", m_related),
        ],
        accessors: &[],
    });

    let keyboard = define_class(g, &ClassSpec {
        name: "KeyboardEvent",
        arity: 1,
        construct: Some(keyboard_construct),
        parent_proto: ui_event.proto,
        methods: &[("getModifierState", 1, k_get_modifier_state)],
        getters: &[
            ("key", k_key), ("code", k_code), ("location", k_location),
            ("repeat", k_repeat), ("isComposing", k_composing),
            ("ctrlKey", k_ctrl), ("shiftKey", k_shift), ("altKey", k_alt), ("metaKey", k_meta),
        ],
        accessors: &[],
    });
    define_constants(g, &keyboard, KEY_LOCATIONS);

    let wheel = define_class(g, &ClassSpec {
        name: "WheelEvent",
        arity: 1,
        construct: Some(wheel_construct),
        parent_proto: mouse.proto,
        methods: &[],
        getters: &[
            ("deltaX", w_delta_x), ("deltaY", w_delta_y), ("deltaZ", w_delta_z),
            ("deltaMode", w_delta_mode),
        ],
        accessors: &[],
    });
    define_constants(g, &wheel, DELTA_MODES);

    let focus = define_class(g, &ClassSpec {
        name: "FocusEvent",
        arity: 1,
        construct: Some(focus_construct),
        parent_proto: ui_event.proto,
        methods: &[],
        getters: &[("relatedTarget", f_related)],
        accessors: &[],
    });

    let input = define_class(g, &ClassSpec {
        name: "InputEvent",
        arity: 1,
        construct: Some(input_construct),
        parent_proto: ui_event.proto,
        methods: &[],
        getters: &[
            ("data", i_data), ("inputType", i_input_type), ("isComposing", i_composing),
        ],
        accessors: &[],
    });

    let pointer = define_class(g, &ClassSpec {
        name: "PointerEvent",
        arity: 1,
        construct: Some(pointer_construct),
        parent_proto: mouse.proto,
        methods: &[
            ("getCoalescedEvents", 0, p_empty_list),
            ("getPredictedEvents", 0, p_empty_list),
        ],
        getters: &[
            ("pointerId", p_id), ("width", p_width), ("height", p_height),
            ("pressure", p_pressure), ("tangentialPressure", p_tangential),
            ("tiltX", p_tilt_x), ("tiltY", p_tilt_y), ("twist", p_twist),
            ("altitudeAngle", p_altitude), ("azimuthAngle", p_azimuth),
            ("pointerType", p_type), ("isPrimary", p_primary),
        ],
        accessors: &[],
    });

    let dt_cls = make_host_class::<DataTransferData>("DataTransfer");
    let dt = define_class(g, &ClassSpec {
        name: "DataTransfer",
        arity: 0,
        construct: Some(dt_construct),
        parent_proto: 0,
        methods: &[
            ("getData", 1, dt_get_data),
            ("setData", 2, dt_set_data),
            ("clearData", 1, dt_clear_data),
            // Spec: sets the drag feedback image. There is no drag feedback
            // surface here, and the call is advisory — a no-op is the whole
            // behaviour, not a placeholder for one.
            ("setDragImage", 3, dt_noop),
        ],
        getters: &[("types", dt_types), ("files", dt_files), ("items", dt_items)],
        accessors: &[
            ("dropEffect", dt_get_drop, dt_set_drop),
            ("effectAllowed", dt_get_effect, dt_set_effect),
        ],
    });

    let list_cls = make_host_class::<DtListData>("DataTransferItemList");
    let list = define_class(g, &ClassSpec {
        name: "DataTransferItemList",
        arity: 0,
        construct: None,
        parent_proto: 0,
        methods: &[("add", 1, list_add), ("remove", 1, list_remove), ("clear", 0, list_clear)],
        getters: &[("length", list_length)],
        accessors: &[],
    });
    // An indexed getter and `length`: it iterates as an array does (WebIDL).
    let global = glue::rjs_global(g);
    let values = get(g, get(g, get(g, global, "Array"), "prototype"), "values");
    let it = glue::rjs_atom_iterator(g);
    glue::rjs_define(g, list.proto, it, values, 2);
    glue::rjs_atom_free(g, it);

    let item_cls = make_host_class::<DtItemData>("DataTransferItem");
    let item = define_class(g, &ClassSpec {
        name: "DataTransferItem",
        arity: 0,
        construct: None,
        parent_proto: 0,
        methods: &[("getAsString", 1, item_get_as_string), ("getAsFile", 0, item_get_as_file)],
        getters: &[("kind", item_kind), ("type", item_type)],
        accessors: &[],
    });

    let object = get(g, global, "Object");
    let define_property = get(g, object, "defineProperty");
    let freeze = get(g, object, "freeze");
    crate::ffi::redjs_protect(g, define_property);
    crate::ffi::redjs_protect(g, freeze);

    let drag = define_class(g, &ClassSpec {
        name: "DragEvent",
        arity: 1,
        construct: Some(drag_construct),
        parent_proto: mouse.proto,
        methods: &[],
        getters: &[("dataTransfer", d_data_transfer)],
        accessors: &[],
    });

    let boxed: &'static UiClasses = Box::leak(Box::new(UiClasses {
        ui_event,
        mouse,
        keyboard,
        wheel,
        focus,
        input,
        pointer,
        drag,
        dt_cls,
        dt,
        list_cls,
        list,
        item_cls,
        item,
        s_dt: atom_priv(g, "redjs#dataTransfer"),
        s_owner: atom_priv(g, "redjs#dataTransferOwner"),
        define_property,
        freeze,
    }));
    registry().lock().unwrap().insert(g as usize, boxed);
    // A hook the host installed before this context had the classes.
    if handle_hooks().lock().unwrap().contains_key(&(g as usize)) {
        define_handle_method(g, boxed, true);
    }
}


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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_aliases_normalise_the_way_the_spec_says() {
        assert_eq!(normalize_format("Text"), "text/plain");
        assert_eq!(normalize_format("URL"), "text/uri-list");
        assert_eq!(normalize_format("Text/HTML"), "text/html");
    }

    #[test]
    fn setting_the_same_format_twice_replaces_rather_than_appends() {
        let mut d = DataTransferData::default();
        d.set("text", "a");
        d.set("text/plain", "b");
        assert_eq!(d.items.len(), 1, "the alias created a second entry: {:?}", d.items);
        assert_eq!(d.get("Text"), "b");
    }

    #[test]
    fn a_missing_format_reads_back_as_empty_string_not_a_panic() {
        assert_eq!(DataTransferData::default().get("text/plain"), "");
    }

    #[test]
    fn clear_data_with_a_format_removes_only_that_one() {
        let mut d = DataTransferData::default();
        d.set("text", "a");
        d.set("text/html", "b");
        d.clear(Some("text"));
        assert_eq!(d.get("text/html"), "b");
        assert_eq!(d.get("text"), "");
        d.clear(None);
        assert!(d.items.is_empty());
    }
}

#[cfg(test)]
mod jsc_tests {
    use crate::webapi::testing::*;

    #[test]
    fn the_inheritance_chain_reaches_event() {
        let c = ctx();
        ok(&c, "new MouseEvent('click') instanceof MouseEvent");
        ok(&c, "new MouseEvent('click') instanceof UIEvent");
        ok(&c, "new MouseEvent('click') instanceof Event");
        ok(&c, "new PointerEvent('pointerdown') instanceof MouseEvent");
        ok(&c, "new DragEvent('drop') instanceof MouseEvent");
        ok(&c, "!(new MouseEvent('click') instanceof PointerEvent)");
    }

    #[test]
    fn init_dictionary_members_read_back() {
        let c = ctx();
        ok(&c, "(e => e.clientX === 10 && e.clientY === 20 && e.button === 2 && e.ctrlKey === true)\
                (new MouseEvent('click', { clientX: 10, clientY: 20, button: 2, ctrlKey: true }))");
        ok(&c, "(e => e.x === 10 && e.pageX === 10)(new MouseEvent('click', { clientX: 10 }))");
        ok(&c, "(e => e.pointerType === 'pen' && e.pressure === 0.5 && e.isPrimary === true)\
                (new PointerEvent('pointerdown', { pointerType: 'pen', pressure: 0.5, isPrimary: true }))");
    }

    #[test]
    fn init_members_convert_by_their_webidl_types() {
        let c = ctx();
        ok(&c, "(e => e.ctrlKey === true && e.shiftKey === false && e.altKey === false && e.metaKey === true && e.which === 4)\
                (new MouseEvent('x', { ctrlKey: 'y', shiftKey: 0, altKey: NaN, metaKey: {}, button: '3' }))");
        ok(&c, "(e => e.pointerType === '5' && e.isPrimary === true && e.bubbles === true && e.type === '7')\
                (new PointerEvent(7, { pointerType: 5, isPrimary: 1, bubbles: 'yes' }))");
        ok(&c, "(e => e.location === 4294967295 && e.key === 'null')(new KeyboardEvent('k', { location: -1, key: null }))");
        ok(&c, "(e => e.data === null && e.inputType === '')(new InputEvent('i', { data: null }))");
        ok(&c, "(e => e.deltaY === 2 && e.deltaMode === 1 && e.clientX === 3)(new WheelEvent('w', { deltaY: 2, deltaMode: 1.7, clientX: 3 }))");
        ok(&c, "new MouseEvent('x', { get clientX() { throw 1 } }).clientX === 0");
    }

    #[test]
    fn unset_members_take_their_specified_defaults() {
        let c = ctx();
        ok(&c, "(e => e.clientX === 0 && e.button === 0 && e.buttons === 0 && e.ctrlKey === false && e.relatedTarget === null)(new MouseEvent('click'))");
        ok(&c, "(e => e.width === 1 && e.height === 1 && e.pointerType === '' && e.altitudeAngle === Math.PI / 2)(new PointerEvent('p'))");
    }

    #[test]
    fn offset_coordinates_come_from_the_init_dict_and_do_not_guess() {
        // The narrowing that matters: offsetX has no layout to be relative to,
        // so it must NOT silently mirror clientX — that is a plausible wrong
        // number, and a hit test built on it lands in the wrong place.
        let c = ctx();
        ok(&c, "new MouseEvent('click', { clientX: 100 }).offsetX === 0");
        ok(&c, "new MouseEvent('click', { offsetX: 7 }).offsetX === 7");
    }

    #[test]
    fn get_modifier_state_answers_the_four_tracked_keys() {
        let c = ctx();
        ok(&c, "new MouseEvent('c', { shiftKey: true }).getModifierState('Shift') === true");
        ok(&c, "new MouseEvent('c', { shiftKey: true }).getModifierState('Control') === false");
        ok(&c, "new MouseEvent('c').getModifierState('CapsLock') === false");
    }

    #[test]
    fn a_mouse_event_dispatches_through_the_existing_event_target() {
        // The reason these are subclasses and not a parallel system: the one
        // dispatch path in events.rs carries them unchanged.
        let c = ctx();
        s(
            &c,
            "globalThis.seen = null; const t = new EventTarget(); \
             t.addEventListener('click', e => { globalThis.seen = [e.type, e.clientX, e instanceof MouseEvent, e.target === t]; }); \
             t.dispatchEvent(new MouseEvent('click', { clientX: 42 })); 'ok'",
        );
        ok(&c, "globalThis.seen !== null");
        ok(&c, "globalThis.seen.join() === 'click,42,true,true'");
    }

    #[test]
    fn prevent_default_still_works_through_the_subclass() {
        let c = ctx();
        ok(&c, "(() => { const t = new EventTarget(); t.addEventListener('x', e => e.preventDefault()); \
                 return t.dispatchEvent(new MouseEvent('x', { cancelable: true })) === false; })()");
    }

    #[test]
    fn a_drag_event_carries_its_data_transfer() {
        let c = ctx();
        ok(
            &c,
            "(() => { const dt = new DataTransfer(); dt.setData('text', 'hello'); \
              const e = new DragEvent('drop', { dataTransfer: dt }); \
              return e.dataTransfer === dt && e.dataTransfer.getData('text/plain') === 'hello' \
                  && e.dataTransfer.types.join() === 'text/plain'; })()",
        );
        ok(&c, "new DragEvent('drop').dataTransfer === null");
    }

    #[test]
    fn a_script_made_data_transfer_is_read_write_with_items() {
        let c = ctx();
        ok(&c, "(() => { const dt = new DataTransfer(); return dt.items === dt.items && dt.items instanceof DataTransferItemList \
                 && dt.items.length === 0 && dt.files instanceof FileList && dt.files.length === 0 \
                 && dt.dropEffect === 'none' && dt.effectAllowed === 'none'; })()");
        ok(&c, "(() => { const dt = new DataTransfer(); dt.setData('text', 'a'); dt.setData('text/html', '<b>'); dt.setData('Text/Plain', 'b'); \
                 const f = new File(['x'], 'x.txt', { type: 'Text/Plain' }); const it = dt.items.add(f); \
                 return dt.types.join() === 'text/html,text/plain,Files' && Object.isFrozen(dt.types) && dt.getData('text') === 'b' \
                   && dt.items.length === 3 && it === dt.items[2] && it.kind === 'file' && it.type === 'text/plain' && it.getAsFile() === f \
                   && dt.files[0] === f && dt.files !== dt.files && [...dt.items].map(i => i.kind).join() === 'string,string,file'; })()");
        ok(&c, "(() => { const dt = new DataTransfer(); dt.setData('url', '#c\\r\\nhttps://a.example/\\r\\nhttps://b.example/'); \
                 return dt.getData('URL') === 'https://a.example/' && dt.getData('text/uri-list').startsWith('#c'); })()");
        // clearData() leaves files alone; items.clear() does not.
        ok(&c, "(() => { const dt = new DataTransfer(); dt.setData('a', '1'); dt.items.add(new File([], 'f')); dt.clearData(); \
                 const kept = dt.types.join() === 'Files' && dt.items.length === 1; dt.items.clear(); return kept && dt.items.length === 0 && dt.items[0] === undefined; })()");
        assert!(throws(&c, "(() => { const dt = new DataTransfer(); dt.items.add('x', 'a/b'); dt.items.add('y', 'A/B'); })()")
            .contains("already exists"));
        assert!(throws(&c, "new DataTransfer().items.add('only one')").contains("not of type 'File'"));
        ok(&c, "(() => { const dt = new DataTransfer(); const it = dt.items.add('s', 'a/b'); dt.items.remove(0); \
                 return it.kind === '' && it.type === '' && dt.items.length === 0; })()");
        ok(&c, "(() => { const dt = new DataTransfer(); dt.dropEffect = 'bogus'; dt.effectAllowed = 'bogus'; const a = dt.dropEffect + dt.effectAllowed; \
                 dt.dropEffect = 'move'; dt.effectAllowed = 'copyLink'; return a === 'nonenone' && dt.dropEffect === 'move' && dt.effectAllowed === 'copyLink'; })()");
        assert_eq!(
            throws(&c, "new DataTransfer().getData()"),
            "Failed to execute 'getData' on 'DataTransfer': 1 argument required, but only 0 present."
        );
        ok(&c, "'getAsFileSystemHandle' in DataTransferItem.prototype === false && !('webkitGetAsEntry' in DataTransferItem.prototype)");
        assert!(throws(&c, "new DataTransferItemList()").contains("Illegal constructor"));
        assert!(throws(&c, "new DataTransferItem()").contains("Illegal constructor"));
    }

    #[test]
    fn get_as_string_delivers_from_a_task() {
        let c = crate::webapi::file::testing::file_ctx();
        let r = crate::webapi::file::testing::run(
            &c,
            "const dt = new DataTransfer(); dt.setData('text', 'hi'); let sync = 'none'; \
             const got = new Promise(r => dt.items[0].getAsString(s => { r(s); })); dt.items[0].getAsString(s => { sync = s; }); \
             const before = sync; dt.items[0].getAsString(null); dt.items[0].getAsString(undefined); \
             let threw = false; try { dt.items[0].getAsString(5); } catch (e) { threw = e instanceof TypeError; } \
             return [before, await got, threw].join();",
        );
        assert_eq!(r, "none,hi,true");
    }

    #[test]
    fn an_os_drop_is_protected_then_read_only_then_disabled() {
        use super::{create_drag_data, set_drag_data_mode, DragDataMode, DropEntry};
        use crate::webapi::file::testing::{file_ctx, run, TempFile};
        let c = file_ctx();
        let a = TempFile::new("photo.png", b"PNGDATA");
        let dir = std::env::temp_dir().join(format!("redjs-drop-dir-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let g = c.raw();
        unsafe {
            let entries =
                [DropEntry { path: a.0.clone(), is_dir: false, stat: None }, DropEntry { path: dir.clone(), is_dir: true, stat: None }];
            let dt = create_drag_data(g, &entries, DragDataMode::Protected);
            crate::webapi::util::define(g, crate::glue::rjs_global(g), "dt", dt, 0);
            // dragenter / dragover: kinds and types, no files, no data.
            ok(&c, "dt.types.join() === 'Files' && dt.items.length === 2 && dt.items[0].kind === 'file' && dt.items[0].type === 'image/png' \
                    && dt.files.length === 0 && dt.items[0].getAsFile() === null && dt.effectAllowed === 'all' && dt.dropEffect === 'copy'");
            ok(&c, "(() => { dt.setData('x', 'y'); dt.items.add('a', 'b'); return dt.types.join() === 'Files'; })()");
            assert!(throws(&c, "dt.items.remove(0)").contains("not writable"));
            // drop: everything readable, nothing writable.
            assert!(set_drag_data_mode(g, dt, DragDataMode::ReadOnly));
            ok(&c, "dt.files.length === 2 && dt.files[0].name === 'photo.png' && dt.items[0].getAsFile() === dt.files[0] \
                    && dt.files[1].size === 0 && dt.files[1].type === '' && dt.items.add('a', 'b') === null");
            assert_eq!(run(&c, "return await dt.files[0].text();"), "PNGDATA");
            assert!(run(&c, "return await dt.files[1].text();").starts_with("ERROR: NotReadableError"), "a directory cannot be read");
            ok(&c, "(() => { globalThis.kept = dt.items[0]; return true; })()");
            // After the event: nothing is left in it.
            set_drag_data_mode(g, dt, DragDataMode::Disabled);
            ok(&c, "dt.types.length === 0 && dt.items.length === 0 && dt.items[0] === undefined && dt.files.length === 0 \
                    && kept.kind === '' && kept.getAsFile() === null && dt.getData('text') === ''");
            // Disabled is not read/write either: remove() throws (it returned silently).
            assert!(throws(&c, "dt.items.remove(0)").contains("not writable"));
        }
        assert!(crate::webapi::blob::advisories().iter().any(|m| m.contains("A-F3")));
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn the_file_system_handle_hook_is_called_for_a_dropped_path() {
        use super::{create_drag_data, set_drag_data_mode, set_file_system_handle_hook, DragDataMode, DropEntry};
        use crate::webapi::file::testing::{file_ctx, run, TempFile};
        unsafe fn hook(g: *mut core::ffi::c_void, path: &std::path::Path, is_dir: bool) -> Result<u64, u64> {
            let o = crate::webapi::util::new_object(g, crate::webapi::util::UNDEF);
            let name = path.file_name().unwrap().to_string_lossy().into_owned();
            crate::webapi::util::define(g, o, "name", crate::webapi::util::js_string(g, &name), 0);
            crate::webapi::util::define(g, o, "kind", crate::webapi::util::js_string(g, if is_dir { "directory" } else { "file" }), 0);
            Ok(o)
        }
        let c = file_ctx();
        let t = TempFile::new("h.txt", b"h");
        let g = c.raw();
        unsafe {
            set_file_system_handle_hook(g, Some(hook));
            ok(&c, "typeof DataTransferItem.prototype.getAsFileSystemHandle === 'function'");
            // A bad `this` is a rejection, not a synchronous throw plus a stray promise.
            ok(&c, "(() => { const p = DataTransferItem.prototype.getAsFileSystemHandle.call({}); globalThis.bad = p; \n                     p.catch(() => {}); return p instanceof Promise; })()");
            assert_eq!(run(&c, "try { await bad; return 'resolved'; } catch (e) { return String(e instanceof TypeError); }"), "true");
            let dt = create_drag_data(g, &[DropEntry { path: t.0.clone(), is_dir: false, stat: None }], DragDataMode::Protected);
            crate::webapi::util::define(g, crate::glue::rjs_global(g), "dt", dt, 0);
            assert_eq!(run(&c, "return String(await dt.items[0].getAsFileSystemHandle());"), "null", "protected: null");
            set_drag_data_mode(g, dt, DragDataMode::ReadOnly);
            assert_eq!(run(&c, "const h = await dt.items[0].getAsFileSystemHandle(); return h.kind + ':' + h.name;"), "file:h.txt");
            assert_eq!(
                run(&c, "const d = new DataTransfer(); d.items.add(new File([], 'f')); d.items.add('s', 't'); \
                         return [await d.items[0].getAsFileSystemHandle(), await d.items[1].getAsFileSystemHandle()].join();"),
                ",",
                "a script-added File and a string item have no handle"
            );
            set_file_system_handle_hook(g, None);
        }
        ok(&c, "!('getAsFileSystemHandle' in DataTransferItem.prototype)");
    }

    #[test]
    fn a_getter_on_a_foreign_object_is_a_type_error() {
        let c = ctx();
        let msg = throws(&c, "Object.getOwnPropertyDescriptor(MouseEvent.prototype,'clientX').get.call({})");
        assert!(msg.contains("non-Event"), "{msg}");
    }

    // -- KeyboardEvent ------------------------------------------------------

    #[test]
    fn a_keyboard_event_is_a_ui_event_and_an_event() {
        let c = ctx();
        ok(&c, "new KeyboardEvent('keydown') instanceof KeyboardEvent");
        ok(&c, "new KeyboardEvent('keydown') instanceof UIEvent");
        ok(&c, "new KeyboardEvent('keydown') instanceof Event");
        ok(&c, "!(new KeyboardEvent('keydown') instanceof MouseEvent)");
        ok(&c, "Object.getPrototypeOf(KeyboardEvent.prototype) === UIEvent.prototype");
        ok(&c, "new KeyboardEvent('keydown').type === 'keydown'");
    }

    #[test]
    fn keyboard_event_members_default_the_way_the_spec_says() {
        let c = ctx();
        ok(
            &c,
            "(e => e.key === '' && e.code === '' && e.location === 0 && e.repeat === false \
              && e.isComposing === false && e.ctrlKey === false && e.shiftKey === false \
              && e.altKey === false && e.metaKey === false)(new KeyboardEvent('keydown'))",
        );
        // …and the same with an empty dictionary, not just a missing one.
        ok(&c, "new KeyboardEvent('keydown', {}).key === ''");
    }

    #[test]
    fn keyboard_event_reads_every_member_back_from_its_init_dictionary() {
        let c = ctx();
        ok(
            &c,
            "(e => e.key === 'a' && e.code === 'KeyA' && e.location === 3 && e.repeat === true \
              && e.isComposing === true && e.ctrlKey === true && e.shiftKey === true \
              && e.altKey === true && e.metaKey === true)\
             (new KeyboardEvent('keydown', { key: 'a', code: 'KeyA', \
               location: KeyboardEvent.DOM_KEY_LOCATION_NUMPAD, repeat: true, isComposing: true, \
               ctrlKey: true, shiftKey: true, altKey: true, metaKey: true }))",
        );
        // Inherited UIEvent/Event members come along.
        ok(
            &c,
            "(e => e.bubbles === true && e.cancelable === true && e.detail === 7 && e.view === null)\
             (new KeyboardEvent('keydown', { bubbles: true, cancelable: true, detail: 7 }))",
        );
    }

    #[test]
    fn keyboard_event_get_modifier_state_answers_the_four_tracked_keys() {
        let c = ctx();
        ok(&c, "new KeyboardEvent('k', { ctrlKey: true }).getModifierState('Control') === true");
        ok(&c, "new KeyboardEvent('k', { shiftKey: true }).getModifierState('Shift') === true");
        ok(&c, "new KeyboardEvent('k', { altKey: true }).getModifierState('Alt') === true");
        ok(&c, "new KeyboardEvent('k', { metaKey: true }).getModifierState('Meta') === true");
        ok(&c, "new KeyboardEvent('k').getModifierState('Control') === false");
        ok(&c, "new KeyboardEvent('k').getModifierState('CapsLock') === false");
    }

    #[test]
    fn the_key_location_constants_sit_on_the_constructor_and_the_prototype() {
        let c = ctx();
        for owner in ["KeyboardEvent", "KeyboardEvent.prototype"] {
            ok(&c, &format!("{owner}.DOM_KEY_LOCATION_STANDARD === 0"));
            ok(&c, &format!("{owner}.DOM_KEY_LOCATION_LEFT === 1"));
            ok(&c, &format!("{owner}.DOM_KEY_LOCATION_RIGHT === 2"));
            ok(&c, &format!("{owner}.DOM_KEY_LOCATION_NUMPAD === 3"));
        }
        ok(&c, "new KeyboardEvent('k').DOM_KEY_LOCATION_NUMPAD === 3");
        // WebIDL constants are read-only and non-configurable.
        ok(
            &c,
            "(d => d.writable === false && d.configurable === false)\
             (Object.getOwnPropertyDescriptor(KeyboardEvent, 'DOM_KEY_LOCATION_LEFT'))",
        );
    }

    #[test]
    fn a_cancelable_keyboard_event_can_be_prevented() {
        let c = ctx();
        ok(&c, "new KeyboardEvent('k').cancelable === false");
        ok(
            &c,
            "(() => { const e = new KeyboardEvent('k', { cancelable: true }); \
              e.preventDefault(); return e.defaultPrevented === true; })()",
        );
        // Not cancelable → preventDefault is a no-op, per the spec.
        ok(
            &c,
            "(() => { const e = new KeyboardEvent('k'); e.preventDefault(); \
              return e.defaultPrevented === false; })()",
        );
        ok(
            &c,
            "(() => { const t = new EventTarget(); t.addEventListener('keydown', e => e.preventDefault()); \
              return t.dispatchEvent(new KeyboardEvent('keydown', { cancelable: true })) === false; })()",
        );
    }

    // -- WheelEvent ---------------------------------------------------------

    #[test]
    fn a_wheel_event_is_a_mouse_event_all_the_way_down() {
        let c = ctx();
        ok(&c, "new WheelEvent('wheel') instanceof WheelEvent");
        ok(&c, "new WheelEvent('wheel') instanceof MouseEvent");
        ok(&c, "new WheelEvent('wheel') instanceof UIEvent");
        ok(&c, "new WheelEvent('wheel') instanceof Event");
        ok(&c, "Object.getPrototypeOf(WheelEvent.prototype) === MouseEvent.prototype");
    }

    #[test]
    fn wheel_deltas_default_to_zero_and_read_back_from_the_init_dictionary() {
        let c = ctx();
        ok(
            &c,
            "(e => e.deltaX === 0 && e.deltaY === 0 && e.deltaZ === 0 && e.deltaMode === 0)\
             (new WheelEvent('wheel'))",
        );
        ok(
            &c,
            "(e => e.deltaX === -1.5 && e.deltaY === 120 && e.deltaZ === 3 && e.deltaMode === 1)\
             (new WheelEvent('wheel', { deltaX: -1.5, deltaY: 120, deltaZ: 3, \
               deltaMode: WheelEvent.DOM_DELTA_LINE }))",
        );
        // The MouseEvent members it inherits are real, not shadowed away.
        ok(
            &c,
            "(e => e.clientX === 4 && e.ctrlKey === true && e.buttons === 0 \
              && e.getModifierState('Control') === true)\
             (new WheelEvent('wheel', { clientX: 4, ctrlKey: true }))",
        );
    }

    #[test]
    fn delta_mode_is_an_enumeration_so_it_is_coerced_to_an_unsigned_long() {
        // The reason deltaMode does not go through the plain numeric path:
        // every use of it is `=== WheelEvent.DOM_DELTA_*`, and 1.5 matches none
        // of them while looking like a valid value.
        let c = ctx();
        ok(&c, "new WheelEvent('wheel', { deltaMode: 1.9 }).deltaMode === 1");
        ok(&c, "new WheelEvent('wheel', { deltaMode: NaN }).deltaMode === 0");
        // …while a plain double member keeps its fraction.
        ok(&c, "new WheelEvent('wheel', { deltaY: 1.9 }).deltaY === 1.9");
    }

    #[test]
    fn the_delta_mode_constants_sit_on_the_constructor_and_the_prototype() {
        let c = ctx();
        for owner in ["WheelEvent", "WheelEvent.prototype"] {
            ok(&c, &format!("{owner}.DOM_DELTA_PIXEL === 0"));
            ok(&c, &format!("{owner}.DOM_DELTA_LINE === 1"));
            ok(&c, &format!("{owner}.DOM_DELTA_PAGE === 2"));
        }
        ok(&c, "new WheelEvent('wheel').DOM_DELTA_PAGE === 2");
    }

    #[test]
    fn a_cancelable_wheel_event_can_be_prevented() {
        let c = ctx();
        ok(
            &c,
            "(() => { const t = new EventTarget(); t.addEventListener('wheel', e => e.preventDefault()); \
              return t.dispatchEvent(new WheelEvent('wheel', { cancelable: true, deltaY: 10 })) === false; })()",
        );
    }

    // -- FocusEvent ---------------------------------------------------------

    #[test]
    fn a_focus_event_is_a_ui_event_and_carries_its_related_target() {
        let c = ctx();
        ok(&c, "new FocusEvent('focus') instanceof FocusEvent");
        ok(&c, "new FocusEvent('focus') instanceof UIEvent");
        ok(&c, "new FocusEvent('focus') instanceof Event");
        ok(&c, "!(new FocusEvent('focus') instanceof MouseEvent)");
        ok(&c, "new FocusEvent('blur').relatedTarget === null");
        ok(&c, "new FocusEvent('blur', {}).relatedTarget === null");
        ok(
            &c,
            "(() => { const t = new EventTarget(); \
              return new FocusEvent('blur', { relatedTarget: t }).relatedTarget === t; })()",
        );
        ok(&c, "new FocusEvent('focus', { bubbles: true, detail: 2 }).detail === 2");
    }

    #[test]
    fn a_cancelable_focus_event_can_be_prevented() {
        let c = ctx();
        ok(
            &c,
            "(() => { const e = new FocusEvent('focusout', { cancelable: true }); \
              e.preventDefault(); return e.defaultPrevented === true; })()",
        );
    }

    // -- InputEvent ---------------------------------------------------------

    #[test]
    fn an_input_event_is_a_ui_event_with_null_data_by_default() {
        let c = ctx();
        ok(&c, "new InputEvent('input') instanceof InputEvent");
        ok(&c, "new InputEvent('input') instanceof UIEvent");
        ok(&c, "new InputEvent('input') instanceof Event");
        ok(
            &c,
            "(e => e.data === null && e.inputType === '' && e.isComposing === false)\
             (new InputEvent('input'))",
        );
    }

    #[test]
    fn input_event_members_read_back_from_the_init_dictionary() {
        let c = ctx();
        ok(
            &c,
            "(e => e.data === 'x' && e.inputType === 'insertText' && e.isComposing === true)\
             (new InputEvent('input', { data: 'x', inputType: 'insertText', isComposing: true }))",
        );
        // `data` is DOMString? — an explicit null stays null, and the empty
        // string stays the empty string rather than collapsing to null.
        ok(&c, "new InputEvent('input', { data: null }).data === null");
        ok(&c, "new InputEvent('input', { data: '' }).data === ''");
        ok(&c, "new InputEvent('beforeinput', { inputType: 'deleteContentBackward' }).data === null");
    }

    #[test]
    fn a_cancelable_input_event_can_be_prevented() {
        let c = ctx();
        ok(
            &c,
            "(() => { const t = new EventTarget(); t.addEventListener('beforeinput', e => e.preventDefault()); \
              return t.dispatchEvent(new InputEvent('beforeinput', { cancelable: true, data: 'q' })) === false; })()",
        );
    }

    // -- shared constructor behaviour ---------------------------------------

    #[test]
    fn the_new_classes_require_a_type_and_reject_a_bare_call() {
        let c = ctx();
        for name in ["KeyboardEvent", "WheelEvent", "FocusEvent", "InputEvent"] {
            let msg = throws(&c, &format!("new {name}()"));
            assert!(msg.contains("type argument"), "{name}: {msg}");
            let msg = throws(&c, &format!("{name}('x')"));
            assert!(msg.contains("new"), "{name}: {msg}");
        }
    }

    #[test]
    fn the_new_classes_subclass_through_new_target() {
        // ordinary_create takes the prototype from new.target, so a JS subclass
        // gets its own prototype and still reaches Event.
        let c = ctx();
        for (name, ty) in [
            ("KeyboardEvent", "keydown"),
            ("WheelEvent", "wheel"),
            ("FocusEvent", "focus"),
            ("InputEvent", "input"),
        ] {
            ok(
                &c,
                &format!(
                    "(() => {{ class Sub extends {name} {{ get tag() {{ return 'sub'; }} }} \
                      const e = new Sub('{ty}', {{ cancelable: true }}); \
                      return e instanceof Sub && e instanceof {name} && e instanceof Event \
                        && e.tag === 'sub' && e.type === '{ty}' && e.cancelable === true; }})()"
                ),
            );
        }
    }

    #[test]
    fn the_new_classes_dispatch_through_the_existing_event_target() {
        let c = ctx();
        s(
            &c,
            "globalThis.log = []; const t = new EventTarget(); \
             t.addEventListener('keydown', e => globalThis.log.push([e.key, e instanceof KeyboardEvent, e.target === t])); \
             t.addEventListener('wheel', e => globalThis.log.push([e.deltaY, e instanceof WheelEvent, e.target === t])); \
             t.addEventListener('input', e => globalThis.log.push([e.inputType, e instanceof InputEvent, e.target === t])); \
             t.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' })); \
             t.dispatchEvent(new WheelEvent('wheel', { deltaY: 3 })); \
             t.dispatchEvent(new InputEvent('input', { inputType: 'insertText' })); 'ok'",
        );
        ok(&c, "globalThis.log.length === 3");
        ok(&c, "globalThis.log.map(r => r.join(':')).join('|') === 'Enter:true:true|3:true:true|insertText:true:true'");
    }

    #[test]
    fn the_new_getters_refuse_a_foreign_receiver() {
        let c = ctx();
        for (proto, name) in [
            ("KeyboardEvent", "key"),
            ("WheelEvent", "deltaY"),
            ("FocusEvent", "relatedTarget"),
            ("InputEvent", "inputType"),
        ] {
            let msg = throws(
                &c,
                &format!("Object.getOwnPropertyDescriptor({proto}.prototype,'{name}').get.call({{}})"),
            );
            assert!(msg.contains("non-Event"), "{proto}.{name}: {msg}");
            assert!(msg.contains(proto), "{proto}.{name}: {msg}");
        }
        let msg = throws(&c, "KeyboardEvent.prototype.getModifierState.call({}, 'Shift')");
        assert!(msg.contains("KeyboardEvent.getModifierState"), "{msg}");
    }
}