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
|
# Copyright (C) 2008 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
# Copyright (C) 2008 Martin Soto <soto@freedesktop.org>
# Copyright (C) 2008 Alp Toker <alp@atoker.com>
# Copyright (C) 2009 Adam Dingle <adam@yorba.org>
# Copyright (C) 2009 Jim Nelson <jim@yorba.org>
# Copyright (C) 2009, 2010 Igalia S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this library; see the file COPYING.LIB. If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
package CodeGeneratorGObject;
use constant FileNamePrefix => "WebKitDOM";
use File::Basename;
use FindBin;
# Global Variables
my %implIncludes = ();
my %hdrIncludes = ();
my @stableSymbols = ();
my $defineTypeMacro = "G_DEFINE_TYPE";
my $defineTypeInterfaceImplementation = ")";
my @txtEventListeners = ();
my @txtInstallProps = ();
my @txtSetProps = ();
my @txtGetProps = ();
my $className = "";
# FIXME: this should be replaced with a function that recurses up the tree
# to find the actual base type.
my %baseTypeHash = ("Object" => 1, "Node" => 1, "NodeList" => 1, "NamedNodeMap" => 1, "DOMImplementation" => 1,
"Event" => 1, "CSSRule" => 1, "CSSValue" => 1, "StyleSheet" => 1, "MediaList" => 1,
"Counter" => 1, "Rect" => 1, "RGBColor" => 1, "XPathExpression" => 1, "XPathResult" => 1,
"NodeIterator" => 1, "TreeWalker" => 1, "AbstractView" => 1, "Blob" => 1, "DOMTokenList" => 1,
"HTMLCollection" => 1, "TextTrackCue" => 1);
# Only objects derived from Node are released by the DOM object cache and can be
# transfer none. Ideally we could use GetBaseClass with the parent type to check
# whether it's Node, but unfortunately we only have the name of the return type,
# and we can't know its parent base class. Since there are fewer classes in the
# API that are not derived from Node, we will list them here to decide the
# transfer type.
my %transferFullTypeHash = ("AudioTrack" => 1, "AudioTrackList" => 1, "BarProp" => 1, "BatteryManager" => 1,
"CSSRuleList" => 1, "CSSStyleDeclaration" => 1, "CSSStyleSheet" => 1,
"DOMApplicationCache" => 1, "DOMMimeType" => 1, "DOMMimeTypeArray" => 1, "DOMNamedFlowCollection" => 1,
"DOMPlugin" => 1, "DOMPluginArray" => 1,
"DOMSelection" => 1, "DOMSettableTokenList" => 1, "DOMStringList" => 1,
"DOMWindow" => 1, "DOMWindowCSS" => 1, "EventTarget" => 1,
"File" => 1, "FileList" => 1, "Gamepad" => 1, "GamepadList" => 1,
"Geolocation" => 1, "HTMLOptionsCollection" => 1, "History" => 1,
"KeyboardEvent" => 1, "MediaError" => 1, "MediaController" => 1,
"MouseEvent" => 1, "MediaQueryList" => 1, "Navigator" => 1, "NodeFilter" => 1,
"Performance" => 1, "PerformanceEntry" => 1, "PerformanceEntryList" => 1, "PerformanceNavigation" => 1, "PerformanceTiming" => 1,
"Range" => 1, "Screen" => 1, "SpeechSynthesis" => 1, "SpeechSynthesisVoice" => 1,
"Storage" => 1, "StyleMedia" => 1, "TextTrack" => 1, "TextTrackCueList" => 1,
"TimeRanges" => 1, "Touch" => 1, "UIEvent" => 1, "UserMessageHandler" => 1, "UserMessageHandlersNamespace" => 1,
"ValidityState" => 1, "VideoTrack" => 1, "WebKitNamedFlow" => 1,
"WebKitNamespace" => 1, "WebKitPoint" => 1, "WheelEvent" => 1, "XPathNSResolver" => 1);
# List of function parameters that are allowed to be NULL
my $canBeNullParams = {
'webkit_dom_document_create_attribute_ns' => ['namespaceURI'],
'webkit_dom_document_create_element_ns' => ['namespaceURI'],
'webkit_dom_document_create_entity_reference' => ['name'],
'webkit_dom_document_create_node_iterator' => ['filter'],
'webkit_dom_document_create_tree_walker' => ['filter'],
'webkit_dom_document_evaluate' => ['inResult', 'resolver'],
'webkit_dom_document_get_override_style' => ['pseudoElement'],
'webkit_dom_dom_implementation_create_document' => ['namespaceURI', 'doctype'],
'webkit_dom_dom_window_get_computed_style' => ['pseudoElement'],
'webkit_dom_element_set_attribute_ns' => ['namespaceURI'],
'webkit_dom_node_insert_before' => ['refChild'],
};
# Default constructor
sub new {
my $object = shift;
my $reference = { };
$codeGenerator = shift;
bless($reference, $object);
}
my $licenceTemplate = << "EOF";
/*
* This file is part of the WebKit open source project.
* This file has been generated by generate-bindings.pl. DO NOT MODIFY!
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
EOF
sub ShouldBeExposedAsInterface {
my $interface = shift;
return $interface eq "EventTarget";
}
sub GetParentClassName {
my $interface = shift;
my $parent = $interface->parent;
return "WebKitDOMObject" unless $parent and !ShouldBeExposedAsInterface($parent);
return "WebKitDOM" . $parent;
}
sub GetParentImplClassName {
my $interface = shift;
my $parent = $interface->parent;
return "Object" unless $parent and !ShouldBeExposedAsInterface($parent);
return $parent;
}
sub IsBaseType
{
my $type = shift;
return 1 if $baseTypeHash{$type};
return 0;
}
sub GetBaseClass
{
$parent = shift;
$interface = shift;
return $parent if $parent eq "Object" or IsBaseType($parent);
return "Object" if ShouldBeExposedAsInterface($parent);
return "Event" if $codeGenerator->InheritsInterface($interface, "Event");
return "CSSValue" if $parent eq "SVGColor" or $parent eq "CSSValueList";
return "Node";
}
# From String::CamelCase 0.01
sub camelize
{
my $s = shift;
join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
}
sub decamelize
{
my $s = shift;
$s =~ s{([^a-zA-Z]?)([A-Z]*)([A-Z])([a-z]?)}{
my $fc = pos($s)==0;
my ($p0,$p1,$p2,$p3) = ($1,lc$2,lc$3,$4);
my $t = $p0 || $fc ? $p0 : '_';
$t .= $p3 ? $p1 ? "${p1}_$p2$p3" : "$p2$p3" : "$p1$p2";
$t;
}ge;
# Some strings are not correctly decamelized, apply fix ups
for ($s) {
s/domcss/dom_css/;
s/domhtml/dom_html/;
s/domdom/dom_dom/;
s/domcdata/dom_cdata/;
s/domui/dom_ui/;
s/x_path/xpath/;
s/web_kit/webkit/;
s/htmli_frame/html_iframe/;
s/htmlbr/html_br/;
s/htmlli/html_li/;
s/htmlhr/html_hr/;
s/htmld/html_d/;
s/htmlo/html_o/;
s/htmlu/html_u/;
}
return $s;
}
sub HumanReadableConditional {
my @conditional = split('_', shift);
my @upperCaseExceptions = ("SQL", "API");
my @humanReadable;
for $part (@conditional) {
if (!grep {$_ eq $part} @upperCaseExceptions) {
$part = camelize(lc($part));
}
push(@humanReadable, $part);
}
return join(' ', @humanReadable);
}
sub GetParentGObjType {
my $interface = shift;
my $parent = $interface->parent;
return "WEBKIT_DOM_TYPE_OBJECT" unless $parent and !ShouldBeExposedAsInterface($parent);
return "WEBKIT_DOM_TYPE_" . uc(decamelize(($parent)));
}
sub GetClassName {
my $name = shift;
return "WebKitDOM$name";
}
sub SkipAttribute {
my $attribute = shift;
if ($attribute->signature->extendedAttributes->{"Custom"}
|| $attribute->signature->extendedAttributes->{"CustomGetter"}) {
return 1;
}
my $propType = $attribute->signature->type;
if ($propType =~ /Constructor$/) {
return 1;
}
return 1 if $attribute->isStatic;
return 1 if $codeGenerator->IsTypedArrayType($propType);
$codeGenerator->AssertNotSequenceType($propType);
if ($codeGenerator->GetArrayType($propType)) {
return 1;
}
if ($codeGenerator->IsEnumType($propType)) {
return 1;
}
# This is for DOMWindow.idl location attribute
if ($attribute->signature->name eq "location") {
return 1;
}
# This is for HTMLInput.idl valueAsDate
if ($attribute->signature->name eq "valueAsDate") {
return 1;
}
# This is for DOMWindow.idl Crypto attribute
if ($attribute->signature->type eq "Crypto") {
return 1;
}
return 1 if $attribute->signature->type eq "EventHandler";
return 1 if $attribute->signature->type eq "Symbol";
if ($attribute->signature->type eq "MediaQueryListListener") {
return 1;
}
# Skip indexed database attributes for now, they aren't yet supported for the GObject generator.
if ($attribute->signature->name =~ /^(?:webkit)?[Ii]ndexedDB/ or $attribute->signature->name =~ /^(?:webkit)?IDB/) {
return 1;
}
if ($attribute->signature->extendedAttributes->{"JSBuiltin"}) {
return 1;
}
return 0;
}
sub SkipFunction {
my $object = shift;
my $function = shift;
my $parentNode = shift;
my $decamelize = shift;
my $prefix = shift;
my $functionName = "webkit_dom_" . $decamelize . "_" . $prefix . decamelize($function->signature->name);
my $functionReturnType = $prefix eq "set_" ? "void" : $function->signature->type;
my $isCustomFunction = $function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"CustomBinding"};
my $callWith = $function->signature->extendedAttributes->{"CallWith"};
my $isUnsupportedCallWith = $codeGenerator->ExtendedAttributeContains($callWith, "ScriptArguments") || $codeGenerator->ExtendedAttributeContains($callWith, "CallStack") || $codeGenerator->ExtendedAttributeContains($callWith, "FirstWindow") || $codeGenerator->ExtendedAttributeContains($callWith, "ActiveWindow");
# Static methods are unsupported
return 1 if $function->isStatic;
if (($isCustomFunction || $isUnsupportedCallWith) &&
$functionName ne "webkit_dom_node_replace_child" &&
$functionName ne "webkit_dom_node_insert_before" &&
$functionName ne "webkit_dom_node_remove_child" &&
$functionName ne "webkit_dom_node_append_child" &&
$functionName ne "webkit_dom_html_collection_item" &&
$functionName ne "webkit_dom_html_collection_named_item") {
return 1;
}
# Skip functions that have callback parameters, because this code generator doesn't know
# how to auto-generate callbacks. Skip functions that have "MediaQueryListListener" or
# sequence<T> parameters, because this code generator doesn't know how to auto-generate
# MediaQueryListListener or sequence<T>. Skip EventListeners because they are handled elsewhere.
foreach my $param (@{$function->parameters}) {
if ($codeGenerator->IsFunctionOnlyCallbackInterface($param->type) ||
$param->extendedAttributes->{"Clamp"} ||
$param->type eq "MediaQueryListListener" ||
$param->type eq "EventListener" ||
$codeGenerator->GetSequenceType($param->type)) {
return 1;
}
}
# This is for DataTransferItemList.idl add(File) method
if ($functionName eq "webkit_dom_data_transfer_item_list_add" && @{$function->parameters} == 1) {
return 1;
}
# Skip Console::profile() and Console::profileEnd() as they're not correctly generated for the moment.
if ($functionName eq "webkit_dom_console_profile" || $functionName eq "webkit_dom_console_profile_end") {
return 1;
}
if ($codeGenerator->IsTypedArrayType($function->signature->type) || $codeGenerator->GetArrayType($function->signature->type)) {
return 1;
}
if ($function->signature->name eq "set" and $parentNode->extendedAttributes->{"TypedArray"}) {
return 1;
}
if ($object eq "MediaQueryListListener") {
return 1;
}
if ($function->signature->name eq "getSVGDocument") {
return 1;
}
if ($function->signature->name eq "getCSSCanvasContext") {
return 1;
}
if ($function->signature->name eq "setRangeText" && @{$function->parameters} == 1) {
return 1;
}
if ($function->signature->name eq "timeEnd") {
return 1;
}
if ($codeGenerator->GetSequenceType($functionReturnType)) {
return 1;
}
if ($function->signature->name eq "supports" && @{$function->parameters} == 1) {
return 1;
}
return 1 if $function->signature->type eq "Promise";
return 1 if $function->signature->type eq "Symbol";
return 1 if $function->signature->type eq "Date";
return 1 if $function->signature->extendedAttributes->{"JSBuiltin"};
return 1 if $function->signature->extendedAttributes->{"Private"};
return 0;
}
# Name type used in the g_value_{set,get}_* functions
sub GetGValueTypeName {
my $type = shift;
my %types = ("DOMString", "string",
"DOMTimeStamp", "uint",
"float", "float",
"unrestricted float", "float",
"double", "double",
"unrestricted double", "double",
"boolean", "boolean",
"char", "char",
"long", "long",
"long long", "int64",
"byte", "int8",
"octet", "uint8",
"short", "int",
"uchar", "uchar",
"unsigned", "uint",
"int", "int",
"unsigned int", "uint",
"unsigned long long", "uint64",
"unsigned long", "ulong",
"unsigned short", "uint");
return $types{$type} ? $types{$type} : "object";
}
# Name type used in C declarations
sub GetGlibTypeName {
my $type = shift;
my $name = GetClassName($type);
my %types = ("DOMString", "gchar*",
"DOMTimeStamp", "guint32",
"SerializedScriptValue", "gchar*",
"float", "gfloat",
"unrestricted float", "gfloat",
"double", "gdouble",
"unrestricted double", "gdouble",
"boolean", "gboolean",
"char", "gchar",
"long", "glong",
"long long", "gint64",
"byte", "gint8",
"octet", "guint8",
"short", "gshort",
"uchar", "guchar",
"unsigned", "guint",
"int", "gint",
"unsigned int", "guint",
"unsigned long", "gulong",
"unsigned long long", "guint64",
"unsigned short", "gushort",
"void", "void");
return $types{$type} ? $types{$type} : "$name*";
}
sub IsGDOMClassType {
my $type = shift;
return 0 if $codeGenerator->IsNonPointerType($type) || $codeGenerator->IsStringType($type) || $type eq "SerializedScriptValue";
return 1;
}
sub IsPropertyReadable {
my $property = shift;
return !SkipAttribute($property);
}
sub IsPropertyWriteable {
my $property = shift;
if (!IsPropertyReadable($property)) {
return 0;
}
if ($property->isReadOnly) {
return 0;
}
my $gtype = GetGValueTypeName($property->signature->type);
my $hasGtypeSignature = $gtype eq "boolean" || $gtype eq "float" || $gtype eq "double" ||
$gtype eq "int64" || $gtype eq "uint64" ||
$gtype eq "long" || $gtype eq "ulong" ||
$gtype eq "int" || $gtype eq "uint" ||
$gtype eq "short" || $gtype eq "ushort" ||
$gtype eq "int8" || $gtype eq "uint8" ||
$gtype eq "char" || $gtype eq "uchar" ||
$gtype eq "string";
if (!$hasGtypeSignature) {
return 0;
}
# FIXME: We are not generating setters for 'Replaceable' attributes now, but we should somehow.
if ($property->signature->extendedAttributes->{"Replaceable"}) {
return 0;
}
if ($property->signature->extendedAttributes->{"CustomSetter"}) {
return 0;
}
return 0 if $property->signature->extendedAttributes->{"CallWith"} || $property->signature->extendedAttributes->{"SetterCallWith"};
return 1;
}
sub GenerateConditionalWarning
{
my $node = shift;
my $indentSize = shift;
if (!$indentSize) {
$indentSize = 4;
}
my $conditional = $node->extendedAttributes->{"Conditional"};
my @warn;
if ($conditional) {
if ($conditional =~ /&/) {
my @splitConditionals = split(/&/, $conditional);
foreach $condition (@splitConditionals) {
push(@warn, "#if !ENABLE($condition)\n");
push(@warn, ' ' x $indentSize . "WEBKIT_WARN_FEATURE_NOT_PRESENT(\"" . HumanReadableConditional($condition) . "\")\n");
push(@warn, "#endif\n");
}
} elsif ($conditional =~ /\|/) {
foreach $condition (split(/\|/, $conditional)) {
push(@warn, ' ' x $indentSize . "WEBKIT_WARN_FEATURE_NOT_PRESENT(\"" . HumanReadableConditional($condition) . "\")\n");
}
} else {
push(@warn, ' ' x $indentSize . "WEBKIT_WARN_FEATURE_NOT_PRESENT(\"" . HumanReadableConditional($conditional) . "\")\n");
}
}
return @warn;
}
sub GenerateProperty {
my $attribute = shift;
my $interfaceName = shift;
my @writeableProperties = @{shift @_};
my $parentNode = shift;
my $hasGetterException = $attribute->signature->extendedAttributes->{"GetterRaisesException"};
my $hasSetterException = $attribute->signature->extendedAttributes->{"SetterRaisesException"};
my $decamelizeInterfaceName = decamelize($interfaceName);
my $propName = decamelize($attribute->signature->name);
my $propFunctionName = GetFunctionSignatureName($interfaceName, $attribute);
my $propNameCaps = uc($propName);
my ${propEnum} = "PROP_${propNameCaps}";
push(@cBodyProperties, " ${propEnum},\n");
my $propType = $attribute->signature->type;
my ${propGType} = decamelize($propType);
my ${ucPropGType} = uc($propGType);
my $gtype = GetGValueTypeName($propType);
my $gparamflag = "WEBKIT_PARAM_READABLE";
my $writeable = IsPropertyWriteable($attribute);
my $mutableString = "read-only";
my $hasCustomSetter = $attribute->signature->extendedAttributes->{"CustomSetter"};
if ($writeable && $hasCustomSetter) {
$mutableString = "read-only (due to custom functions needed in webkitdom)";
} elsif ($writeable) {
$gparamflag = "WEBKIT_PARAM_READWRITE";
$mutableString = "read-write";
}
my $getterFunctionName = "webkit_dom_${decamelizeInterfaceName}_get_" . $propFunctionName;
my @getterArguments = ();
push(@getterArguments, "self");
push(@getterArguments, "nullptr") if $hasGetterException || FunctionUsedToRaiseException($getterFunctionName);
if (grep {$_ eq $attribute} @writeableProperties) {
my $setterFunctionName = "webkit_dom_${decamelizeInterfaceName}_set_" . $propFunctionName;
my @setterArguments = ();
push(@setterArguments, "self, g_value_get_$gtype(value)");
push(@setterArguments, "nullptr") if $hasSetterException || FunctionUsedToRaiseException($setterFunctionName);
push(@txtSetProps, " case ${propEnum}:\n");
push(@txtSetProps, " " . $setterFunctionName . "(" . join(", ", @setterArguments) . ");\n");
push(@txtSetProps, " break;\n");
}
push(@txtGetProps, " case ${propEnum}:\n");
my $postConvertFunction = "";
if ($gtype eq "string") {
push(@txtGetProps, " g_value_take_string(value, " . $getterFunctionName . "(" . join(", ", @getterArguments) . "));\n");
} else {
push(@txtGetProps, " g_value_set_$gtype(value, " . $getterFunctionName . "(" . join(", ", @getterArguments) . "));\n");
}
push(@txtGetProps, " break;\n");
my %parameterSpecOptions = ("int" => [ "G_MININT", "G_MAXINT", "0" ],
"int8" => [ "G_MININT8", "G_MAXINT8", "0" ],
"boolean" => [ "FALSE" ],
"float" => [ "-G_MAXFLOAT", "G_MAXFLOAT", "0" ],
"double" => [ "-G_MAXDOUBLE", "G_MAXDOUBLE", "0" ],
"uint64" => [ "0", "G_MAXUINT64", "0" ],
"long" => [ "G_MINLONG", "G_MAXLONG", "0" ],
"int64" => [ "G_MININT64", "G_MAXINT64", "0" ],
"ulong" => [ "0", "G_MAXULONG", "0" ],
"uint" => [ "0", "G_MAXUINT", "0" ],
"uint8" => [ "0", "G_MAXUINT8", "0" ],
"ushort" => [ "0", "G_MAXUINT16", "0" ],
"uchar" => [ "G_MININT8", "G_MAXINT8", "0" ],
"char" => [ "0", "G_MAXUINT8", "0" ],
"string" => [ '""', ],
"object" => [ "WEBKIT_DOM_TYPE_${ucPropGType}" ]);
my $extraParameters = join(", ", @{$parameterSpecOptions{$gtype}});
my $glibTypeName = GetGlibTypeName($propType);
$propName =~ s/_/-/g;
my $txtInstallProp = << "EOF";
g_object_class_install_property(
gobjectClass,
$propEnum,
g_param_spec_$gtype(
"$propName",
"$interfaceName:$propName",
"$mutableString $glibTypeName $interfaceName:$propName",
$extraParameters,
$gparamflag));
EOF
push(@txtInstallProps, $txtInstallProp);
}
sub GenerateProperties {
my ($object, $interfaceName, $interface) = @_;
my $decamelize = decamelize($interfaceName);
my $clsCaps = uc($decamelize);
my $lowerCaseIfaceName = "webkit_dom_$decamelize";
my $parentImplClassName = GetParentImplClassName($interface);
my $conditionGuardStart = "";
my $conditionGuardEnd = "";
my $conditionalString = $codeGenerator->GenerateConditionalString($interface);
if ($conditionalString) {
$conditionGuardStart = "#if ${conditionalString}";
$conditionGuardEnd = "#endif // ${conditionalString}";
}
# Properties
my $implContent = "";
my @readableProperties = grep { IsPropertyReadable($_) } @{$interface->attributes};
my @writeableProperties = grep { IsPropertyWriteable($_) } @{$interface->attributes};
my $numProperties = scalar @readableProperties;
# Properties
if ($numProperties > 0) {
$implContent = << "EOF";
enum {
PROP_0,
EOF
push(@cBodyProperties, $implContent);
push(@txtGetProps, "static void ${lowerCaseIfaceName}_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)\n");
push(@txtGetProps, "{\n");
push(@txtGetProps, " ${className}* self = WEBKIT_DOM_${clsCaps}(object);\n");
push(@txtGetProps, "\n");
push(@txtGetProps, " switch (propertyId) {\n");
if (scalar @writeableProperties > 0) {
push(@txtSetProps, "static void ${lowerCaseIfaceName}_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)\n");
push(@txtSetProps, "{\n");
push(@txtSetProps, " ${className}* self = WEBKIT_DOM_${clsCaps}(object);\n");
push(@txtSetProps, "\n");
push(@txtSetProps, " switch (propertyId) {\n");
}
foreach my $attribute (@readableProperties) {
GenerateProperty($attribute, $interfaceName, \@writeableProperties, $interface);
}
push(@cBodyProperties, "};\n\n");
$txtGetProp = << "EOF";
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
break;
}
}
EOF
push(@txtGetProps, $txtGetProp);
if (scalar @writeableProperties > 0) {
$txtSetProps = << "EOF";
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
break;
}
}
EOF
push(@txtSetProps, $txtSetProps);
}
}
# Do not insert extra spaces when interpolating array variables
$" = "";
if ($parentImplClassName eq "Object") {
$implContent = << "EOF";
static void ${lowerCaseIfaceName}_finalize(GObject* object)
{
${className}Private* priv = WEBKIT_DOM_${clsCaps}_GET_PRIVATE(object);
$conditionGuardStart
WebKit::DOMObjectCache::forget(priv->coreObject.get());
$conditionGuardEnd
priv->~${className}Private();
G_OBJECT_CLASS(${lowerCaseIfaceName}_parent_class)->finalize(object);
}
EOF
push(@cBodyProperties, $implContent);
}
if ($numProperties > 0) {
if (scalar @writeableProperties > 0) {
push(@cBodyProperties, @txtSetProps);
push(@cBodyProperties, "\n");
}
push(@cBodyProperties, @txtGetProps);
push(@cBodyProperties, "\n");
}
# Add a constructor implementation only for direct subclasses of Object to make sure
# that the WebCore wrapped object is added only once to the DOM cache. The DOM garbage
# collector works because Node is a direct subclass of Object and the version of
# DOMObjectCache::put() that receives a Node (which is the one setting the frame) is
# always called for DOM objects derived from Node.
if ($parentImplClassName eq "Object") {
$implContent = << "EOF";
static GObject* ${lowerCaseIfaceName}_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties)
{
GObject* object = G_OBJECT_CLASS(${lowerCaseIfaceName}_parent_class)->constructor(type, constructPropertiesCount, constructProperties);
$conditionGuardStart
${className}Private* priv = WEBKIT_DOM_${clsCaps}_GET_PRIVATE(object);
priv->coreObject = static_cast<WebCore::${interfaceName}*>(WEBKIT_DOM_OBJECT(object)->coreObject);
WebKit::DOMObjectCache::put(priv->coreObject.get(), object);
$conditionGuardEnd
return object;
}
EOF
push(@cBodyProperties, $implContent);
}
$implContent = << "EOF";
static void ${lowerCaseIfaceName}_class_init(${className}Class* requestClass)
{
EOF
push(@cBodyProperties, $implContent);
if ($parentImplClassName eq "Object" || $numProperties > 0) {
push(@cBodyProperties, " GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass);\n");
if ($parentImplClassName eq "Object") {
push(@cBodyProperties, " g_type_class_add_private(gobjectClass, sizeof(${className}Private));\n");
push(@cBodyProperties, " gobjectClass->constructor = ${lowerCaseIfaceName}_constructor;\n");
push(@cBodyProperties, " gobjectClass->finalize = ${lowerCaseIfaceName}_finalize;\n");
}
if ($numProperties > 0) {
if (scalar @writeableProperties > 0) {
push(@cBodyProperties, " gobjectClass->set_property = ${lowerCaseIfaceName}_set_property;\n");
}
push(@cBodyProperties, " gobjectClass->get_property = ${lowerCaseIfaceName}_get_property;\n");
push(@cBodyProperties, "\n");
push(@cBodyProperties, @txtInstallProps);
}
} else {
push(@cBodyProperties, " UNUSED_PARAM(requestClass);\n");
}
$implContent = << "EOF";
}
static void ${lowerCaseIfaceName}_init(${className}* request)
{
EOF
push(@cBodyProperties, $implContent);
if ($parentImplClassName eq "Object") {
$implContent = << "EOF";
${className}Private* priv = WEBKIT_DOM_${clsCaps}_GET_PRIVATE(request);
new (priv) ${className}Private();
EOF
push(@cBodyProperties, $implContent);
} else {
push(@cBodyProperties, " UNUSED_PARAM(request);\n");
}
$implContent = << "EOF";
}
EOF
push(@cBodyProperties, $implContent);
}
sub GenerateConstants {
my ($interface, $prefix) = @_;
my $isStableClass = scalar(@stableSymbols);
if (@{$interface->constants}) {
my @constants = @{$interface->constants};
foreach my $constant (@constants) {
my $conditionalString = $codeGenerator->GenerateConditionalString($constant);
my $constantName = $prefix . $constant->name;
my $constantValue = $constant->value;
my $stableSymbol = grep {$_ =~ /^\Q$constantName/} @stableSymbols;
my $stableSymbolVersion;
if ($stableSymbol) {
($dummy, $stableSymbolVersion) = split('@', $stableSymbol, 2);
push(@symbols, "$constantName\n");
}
my @constantHeader = ();
push(@constantHeader, "#if ${conditionalString}") if $conditionalString;
push(@constantHeader, "/**");
push(@constantHeader, " * ${constantName}:");
if ($stableSymbolVersion) {
push(@constantHeader, " * Since: ${stableSymbolVersion}");
}
push(@constantHeader, " */");
push(@constantHeader, "#define $constantName $constantValue");
push(@constantHeader, "#endif /* ${conditionalString} */") if $conditionalString;
push(@constantHeader, "\n");
if ($stableSymbol or !$isStableClass) {
push(@hBody, join("\n", @constantHeader));
} else {
push(@hBodyUnstable, join("\n", @constantHeader));
}
}
}
}
sub GenerateHeader {
my ($object, $interfaceName, $parentClassName, $interface) = @_;
my $implContent = "";
# Add the default header template
@hPrefix = split("\r", $licenceTemplate);
push(@hPrefix, "\n");
my $isStableClass = scalar(@stableSymbols);
if ($isStableClass) {
# Force single header include.
my $headerCheck = << "EOF";
#if !defined(__WEBKITDOM_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error "Only <webkitdom/webkitdom.h> can be included directly."
#endif
EOF
push(@hPrefix, $headerCheck);
}
# Header guard
my $guard = $className . "_h";
@hPrefixGuard = << "EOF";
#ifndef $guard
#define $guard
EOF
if (!$isStableClass) {
push(@hPrefixGuard, "#ifdef WEBKIT_DOM_USE_UNSTABLE_API\n\n");
}
$implContent = << "EOF";
G_BEGIN_DECLS
EOF
push(@hBodyPre, $implContent);
my $decamelize = decamelize($interfaceName);
my $clsCaps = uc($decamelize);
my $lowerCaseIfaceName = "webkit_dom_$decamelize";
$implContent = << "EOF";
#define WEBKIT_DOM_TYPE_${clsCaps} (${lowerCaseIfaceName}_get_type())
#define WEBKIT_DOM_${clsCaps}(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), WEBKIT_DOM_TYPE_${clsCaps}, ${className}))
#define WEBKIT_DOM_${clsCaps}_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), WEBKIT_DOM_TYPE_${clsCaps}, ${className}Class)
#define WEBKIT_DOM_IS_${clsCaps}(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), WEBKIT_DOM_TYPE_${clsCaps}))
#define WEBKIT_DOM_IS_${clsCaps}_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), WEBKIT_DOM_TYPE_${clsCaps}))
#define WEBKIT_DOM_${clsCaps}_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), WEBKIT_DOM_TYPE_${clsCaps}, ${className}Class))
EOF
push(@hBody, $implContent);
if ($isStableClass) {
push(@symbols, "GType ${lowerCaseIfaceName}_get_type(void)\n");
}
GenerateConstants($interface, "WEBKIT_DOM_${clsCaps}_");
$implContent = << "EOF";
struct _${className} {
${parentClassName} parent_instance;
};
struct _${className}Class {
${parentClassName}Class parent_class;
};
EOF
push(@hBody, $implContent);
push(@hBody, "WEBKIT_API GType\n${lowerCaseIfaceName}_get_type(void);\n");
push(@hBody, "\n");
}
sub GetGReturnMacro {
my ($paramName, $paramIDLType, $returnType, $functionName) = @_;
my $condition;
if ($paramIDLType eq "GError") {
$condition = "!$paramName || !*$paramName";
} elsif (IsGDOMClassType($paramIDLType)) {
my $paramTypeCaps = uc(decamelize($paramIDLType));
$condition = "WEBKIT_DOM_IS_${paramTypeCaps}($paramName)";
if (ParamCanBeNull($functionName, $paramName)) {
$condition = "!$paramName || $condition";
}
} else {
if (ParamCanBeNull($functionName, $paramName)) {
return "";
}
$condition = "$paramName";
}
my $macro;
if ($returnType ne "void") {
$defaultReturn = $returnType eq "gboolean" ? "FALSE" : 0;
$macro = " g_return_val_if_fail($condition, $defaultReturn);\n";
} else {
$macro = " g_return_if_fail($condition);\n";
}
return $macro;
}
sub ParamCanBeNull {
my($functionName, $paramName) = @_;
if (defined($functionName)) {
return scalar(grep {$_ eq $paramName} @{$canBeNullParams->{$functionName}});
}
return 0;
}
sub GetFunctionSignatureName {
my ($interfaceName, $function) = @_;
my $signatureName = decamelize($function->signature->name);
return $signatureName if $signatureName ne "type";
# For HTML type attribute use type_attr.
# Example: webkit_dom_html_link_element_get_type_attr()
my $contentAttributeName = $codeGenerator->ContentAttributeName(\%implIncludes, $interfaceName, $function);
if ($contentAttributeName) {
return "type_attr" if $contentAttributeName eq "WebCore::HTMLNames::typeAttr";
}
# For methods returning a MIME type use content_type.
# Examples: webkit_dom_style_sheet_get_content_type(), webkit_dom_dom_mime_type_get_content_type()
if ($interfaceName eq "StyleSheet" || $interfaceName eq "DOMMimeType") {
return "content_type";
}
# For HTMLFieldSet use field_set_type.
# Example: webkit_dom_html_field_set_element_get_field_set_type()
if ($interfaceName eq "HTMLFieldSet") {
return "field_set_type";
}
# For any other cases use the last word of the interface name.
# Examples: webkit_dom_blob_get_blob_type(), webkit_dom_event_get_event_type()
my @nameTokens = split('_', decamelize($interfaceName));
my $name = $nameTokens[-1];
# If the last word is element and there are more words, use the previous one.
# Example: webkit_dom_html_button_element_get_button_type()
if (scalar(@nameTokens) > 1 && $name eq "element") {
$name = $nameTokens[-2];
}
return "${name}_type";
}
sub GetTransferTypeForReturnType {
my $returnType = shift;
# Node is always transfer none.
return "none" if $returnType eq "Node";
# Any base class but Node is transfer full.
return "full" if IsBaseType($returnType);
# Any other class not derived from Node is transfer full.
return "full" if $transferFullTypeHash{$returnType};
return "none";
}
sub GetEffectiveFunctionName {
my $functionName = shift;
# Rename webkit_dom_[document|element]_get_elements_by_tag_name* and webkit_dom_[document|element]_get_elements_by_class_name
# functions since they were changed to return a WebKitDOMHTMLCollection instead of a WebKitDOMNodeList in
# r188809 and r188735. The old methods are now manually added as deprecated.
if ($functionName eq "webkit_dom_document_get_elements_by_tag_name"
|| $functionName eq "webkit_dom_document_get_elements_by_tag_name_ns"
|| $functionName eq "webkit_dom_document_get_elements_by_class_name"
|| $functionName eq "webkit_dom_element_get_elements_by_tag_name"
|| $functionName eq "webkit_dom_element_get_elements_by_tag_name_ns"
|| $functionName eq "webkit_dom_element_get_elements_by_class_name") {
return $functionName . "_as_html_collection";
}
return $functionName;
}
sub FunctionUsedToRaiseException {
my $functionName = shift;
return $functionName eq "webkit_dom_character_data_append_data"
|| $functionName eq "webkit_dom_character_data_set_data"
|| $functionName eq "webkit_dom_document_create_node_iterator"
|| $functionName eq "webkit_dom_document_create_tree_walker"
|| $functionName eq "webkit_dom_node_iterator_next_node"
|| $functionName eq "webkit_dom_node_iterator_previous_node"
|| $functionName eq "webkit_dom_range_clone_range"
|| $functionName eq "webkit_dom_range_collapse"
|| $functionName eq "webkit_dom_range_detach"
|| $functionName eq "webkit_dom_range_get_common_ancestor_container"
|| $functionName eq "webkit_dom_range_get_end_container"
|| $functionName eq "webkit_dom_range_get_start_container"
|| $functionName eq "webkit_dom_range_get_collapsed"
|| $functionName eq "webkit_dom_range_get_end_offset"
|| $functionName eq "webkit_dom_range_get_start_offset"
|| $functionName eq "webkit_dom_range_to_string";
}
sub GenerateFunction {
my ($object, $interfaceName, $function, $prefix, $parentNode) = @_;
my $decamelize = decamelize($interfaceName);
if (SkipFunction($object, $function, $parentNode, $decamelize, $prefix)) {
return;
}
my $functionSigType = $prefix eq "set_" ? "void" : $function->signature->type;
my $functionSigName = GetFunctionSignatureName($interfaceName, $function);
my $functionName = GetEffectiveFunctionName("webkit_dom_" . $decamelize . "_" . $prefix . $functionSigName);
my $returnType = GetGlibTypeName($functionSigType);
my $returnValueIsGDOMType = IsGDOMClassType($functionSigType);
my $raisesException = $function->signature->extendedAttributes->{"RaisesException"};
# If a method used to raise an exception, but was changed to not raise it anymore, the
# API changes because we use a explicit GError parameter to handle the exceptions.
# In this case, it's better to keep the GError parameter even if it's unused to keep
# the API compatibility.
my $usedToRaiseException = FunctionUsedToRaiseException($functionName);
my $conditionalString = $codeGenerator->GenerateConditionalString($function->signature);
my $parentConditionalString = $codeGenerator->GenerateConditionalString($parentNode);
my @conditionalWarn = GenerateConditionalWarning($function->signature);
my @parentConditionalWarn = GenerateConditionalWarning($parentNode);
my $functionSig = "${className}* self";
my $symbolSig = "${className}*";
my @callImplParams;
foreach my $param (@{$function->parameters}) {
my $paramIDLType = $param->type;
my $arrayOrSequenceType = $codeGenerator->GetArrayOrSequenceType($paramIDLType);
$paramIDLType = $arrayOrSequenceType if $arrayOrSequenceType ne "";
my $paramType = GetGlibTypeName($paramIDLType);
my $const = $paramType eq "gchar*" ? "const " : "";
my $paramName = $param->name;
$functionSig .= ", ${const}$paramType $paramName";
$symbolSig .= ", ${const}$paramType";
my $paramIsGDOMType = IsGDOMClassType($paramIDLType);
if ($paramIsGDOMType) {
if ($paramIDLType ne "any") {
$implIncludes{"WebKitDOM${paramIDLType}Private.h"} = 1;
}
}
if ($paramIsGDOMType || ($paramIDLType eq "DOMString")) {
$paramName = "converted" . $codeGenerator->WK_ucfirst($paramName);
}
if ($paramIDLType eq "NodeFilter" || $paramIDLType eq "XPathNSResolver") {
$paramName = "WTF::getPtr(" . $paramName . ")";
}
if ($paramIDLType eq "SerializedScriptValue") {
$implIncludes{"SerializedScriptValue.h"} = 1;
$paramName = "WebCore::SerializedScriptValue::create(WTF::String::fromUTF8(" . $paramName . "))";
}
push(@callImplParams, $paramName);
}
if ($returnType ne "void" && $returnValueIsGDOMType && $functionSigType ne "any") {
$implIncludes{"WebKitDOM${functionSigType}Private.h"} = 1;
}
$functionSig .= ", GError** error" if $raisesException || $usedToRaiseException;
$symbolSig .= ", GError**" if $raisesException || $usedToRaiseException;
my $symbol = "$returnType $functionName($symbolSig)";
my $isStableClass = scalar(@stableSymbols);
my ($stableSymbol) = grep {$_ =~ /^\Q$symbol/} @stableSymbols;
my $stableSymbolVersion;
if ($stableSymbol and $isStableClass) {
($dummy, $stableSymbolVersion) = split('@', $stableSymbol, 2);
push(@symbols, "$symbol\n");
}
my @functionHeader = ();
# Insert introspection annotations
push(@functionHeader, "/**");
push(@functionHeader, " * ${functionName}:");
push(@functionHeader, " * \@self: A #${className}");
foreach my $param (@{$function->parameters}) {
my $paramIDLType = $param->type;
my $arrayOrSequenceType = $codeGenerator->GetArrayOrSequenceType($paramIDLType);
$paramIDLType = $arrayOrSequenceType if $arrayOrSequenceType ne "";
my $paramType = GetGlibTypeName($paramIDLType);
# $paramType can have a trailing * in some cases
$paramType =~ s/\*$//;
my $paramName = $param->name;
my $paramAnnotations = "";
if (ParamCanBeNull($functionName, $paramName)) {
$paramAnnotations = " (allow-none):";
}
push(@functionHeader, " * \@${paramName}:${paramAnnotations} A #${paramType}");
}
push(@functionHeader, " * \@error: #GError") if $raisesException || $usedToRaiseException;
push(@functionHeader, " *");
my $returnTypeName = $returnType;
my $hasReturnTag = 0;
$returnTypeName =~ s/\*$//;
if ($returnValueIsGDOMType) {
my $transferType = GetTransferTypeForReturnType($functionSigType);
push(@functionHeader, " * Returns: (transfer $transferType): A #${returnTypeName}");
$hasReturnTag = 1;
} elsif ($returnType ne "void") {
push(@functionHeader, " * Returns: A #${returnTypeName}");
$hasReturnTag = 1;
}
if (!$stableSymbol) {
if ($hasReturnTag) {
push(@functionHeader, " *");
}
push(@functionHeader, " * Stability: Unstable");
} elsif ($stableSymbolVersion) {
if ($hasReturnTag) {
push(@functionHeader, " *");
}
push(@functionHeader, " * Since: ${stableSymbolVersion}");
}
push(@functionHeader, "**/");
push(@functionHeader, "WEBKIT_API $returnType\n$functionName($functionSig);");
push(@functionHeader, "\n");
if ($stableSymbol or !$isStableClass) {
push(@hBody, join("\n", @functionHeader));
} else {
push(@hBodyUnstable, join("\n", @functionHeader));
}
push(@cBody, "$returnType $functionName($functionSig)\n{\n");
push(@cBody, "#if ${parentConditionalString}\n") if $parentConditionalString;
push(@cBody, "#if ${conditionalString}\n") if $conditionalString;
push(@cBody, " WebCore::JSMainThreadNullState state;\n");
# g_return macros to check parameters of public methods.
$gReturnMacro = GetGReturnMacro("self", $interfaceName, $returnType);
push(@cBody, $gReturnMacro);
foreach my $param (@{$function->parameters}) {
my $paramName = $param->name;
my $paramIDLType = $param->type;
my $paramTypeIsPointer = !$codeGenerator->IsNonPointerType($paramIDLType);
if ($paramTypeIsPointer) {
$gReturnMacro = GetGReturnMacro($paramName, $paramIDLType, $returnType, $functionName);
push(@cBody, $gReturnMacro);
}
}
if ($raisesException) {
$gReturnMacro = GetGReturnMacro("error", "GError", $returnType);
push(@cBody, $gReturnMacro);
} elsif ($usedToRaiseException) {
push(@cBody, " UNUSED_PARAM(error);\n");
}
# The WebKit::core implementations check for null already; no need to duplicate effort.
push(@cBody, " WebCore::${interfaceName}* item = WebKit::core(self);\n");
$returnParamName = "";
foreach my $param (@{$function->parameters}) {
my $paramIDLType = $param->type;
my $paramName = $param->name;
my $paramIsGDOMType = IsGDOMClassType($paramIDLType);
$convertedParamName = "converted" . $codeGenerator->WK_ucfirst($paramName);
if ($paramIDLType eq "DOMString") {
push(@cBody, " WTF::String ${convertedParamName} = WTF::String::fromUTF8($paramName);\n");
} elsif ($paramIDLType eq "NodeFilter" || $paramIDLType eq "XPathNSResolver") {
push(@cBody, " RefPtr<WebCore::$paramIDLType> ${convertedParamName} = WebKit::core($paramName);\n");
} elsif ($paramIsGDOMType) {
push(@cBody, " WebCore::${paramIDLType}* ${convertedParamName} = WebKit::core($paramName);\n");
}
$returnParamName = $convertedParamName if $param->extendedAttributes->{"CustomReturn"};
}
my $assign = "";
my $assignPre = "";
my $assignPost = "";
# We need to special-case these Node methods because their C++
# signature is different from what we'd expect given their IDL
# description; see Node.h.
my $functionHasCustomReturn = $functionName eq "webkit_dom_node_append_child" ||
$functionName eq "webkit_dom_node_insert_before" ||
$functionName eq "webkit_dom_node_replace_child" ||
$functionName eq "webkit_dom_node_remove_child";
if ($returnType ne "void" && !$functionHasCustomReturn) {
if ($returnValueIsGDOMType) {
$assign = "RefPtr<WebCore::${functionSigType}> gobjectResult = ";
$assignPre = "WTF::getPtr(";
$assignPost = ")";
} else {
$assign = "${returnType} result = ";
if ($function->signature->isNullable) {
# FIXME: Returning 0 is probably not right for all nullable attribute values.
# We may want to handle this the way we do in the Objective-C bindings: not
# handle it at all, and not expose any nullables.
$assignPost = ".valueOr(0)";
}
}
if ($functionSigType eq "SerializedScriptValue") {
$assignPre = "convertToUTF8String(";
$assignPost = "->toString())";
}
}
if ($raisesException) {
push(@cBody, " WebCore::ExceptionCode ec = 0;\n");
push(@callImplParams, "ec");
}
my $functionImplementationName = $function->signature->extendedAttributes->{"ImplementedAs"} || $function->signature->name;
if ($functionHasCustomReturn) {
push(@cBody, " bool ok = item->${functionImplementationName}(" . join(", ", @callImplParams) . ");\n");
my $customNodeAppendChild = << "EOF";
if (ok)
return WebKit::kit($returnParamName);
EOF
push(@cBody, $customNodeAppendChild);
if($raisesException) {
my $exceptionHandling = << "EOF";
WebCore::ExceptionCodeDescription ecdesc(ec);
g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
EOF
push(@cBody, $exceptionHandling);
}
push(@cBody, " return 0;\n");
push(@cBody, "}\n\n");
return;
} elsif ($functionSigType eq "DOMString") {
my $getterContentHead;
if ($prefix) {
my ($functionName, @arguments) = $codeGenerator->GetterExpression(\%implIncludes, $interfaceName, $function);
push(@arguments, @callImplParams);
if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
$implIncludes{"${implementedBy}.h"} = 1;
unshift(@arguments, "item");
$functionName = "WebCore::${implementedBy}::${functionName}";
} else {
$functionName = "item->${functionName}";
}
$getterContentHead = "${assign}convertToUTF8String(${functionName}(" . join(", ", @arguments) . "));\n";
} else {
my @arguments = @callImplParams;
if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
$implIncludes{"${implementedBy}.h"} = 1;
unshift(@arguments, "item");
$getterContentHead = "${assign}convertToUTF8String(WebCore::${implementedBy}::${functionImplementationName}(" . join(", ", @arguments) . "));\n";
} else {
$getterContentHead = "${assign}convertToUTF8String(item->${functionImplementationName}(" . join(", ", @arguments) . "));\n";
}
}
push(@cBody, " ${getterContentHead}");
} else {
my $contentHead;
if ($prefix eq "get_") {
my ($functionName, @arguments) = $codeGenerator->GetterExpression(\%implIncludes, $interfaceName, $function);
push(@arguments, @callImplParams);
if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
$implIncludes{"${implementedBy}.h"} = 1;
unshift(@arguments, "*item");
$functionName = "WebCore::${implementedBy}::${functionName}";
} else {
$functionName = "item->${functionName}";
}
$contentHead = "${assign}${assignPre}${functionName}(" . join(", ", @arguments) . ")${assignPost};\n";
} elsif ($prefix eq "set_") {
my ($functionName, @arguments) = $codeGenerator->SetterExpression(\%implIncludes, $interfaceName, $function);
push(@arguments, @callImplParams);
if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
$implIncludes{"${implementedBy}.h"} = 1;
unshift(@arguments, "*item");
$functionName = "WebCore::${implementedBy}::${functionName}";
$contentHead = "${assign}${assignPre}${functionName}(" . join(", ", @arguments) . ")${assignPost};\n";
} else {
$functionName = "item->${functionName}";
$contentHead = "${assign}${assignPre}${functionName}(" . join(", ", @arguments) . ")${assignPost};\n";
}
} else {
my @arguments = @callImplParams;
if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
$implIncludes{"${implementedBy}.h"} = 1;
unshift(@arguments, "*item");
$contentHead = "${assign}${assignPre}WebCore::${implementedBy}::${functionImplementationName}(" . join(", ", @arguments) . ")${assignPost};\n";
} else {
$contentHead = "${assign}${assignPre}item->${functionImplementationName}(" . join(", ", @arguments) . ")${assignPost};\n";
}
}
push(@cBody, " ${contentHead}");
if($raisesException) {
my $exceptionHandling = << "EOF";
if (ec) {
WebCore::ExceptionCodeDescription ecdesc(ec);
g_set_error_literal(error, g_quark_from_string("WEBKIT_DOM"), ecdesc.code, ecdesc.name);
}
EOF
push(@cBody, $exceptionHandling);
}
}
if ($returnType ne "void" && !$functionHasCustomReturn) {
if ($functionSigType ne "any") {
if ($returnValueIsGDOMType) {
push(@cBody, " return WebKit::kit(gobjectResult.get());\n");
} else {
push(@cBody, " return result;\n");
}
} else {
push(@cBody, " return 0; // TODO: return canvas object\n");
}
}
if ($conditionalString) {
push(@cBody, "#else\n");
push(@cBody, " UNUSED_PARAM(self);\n");
foreach my $param (@{$function->parameters}) {
push(@cBody, " UNUSED_PARAM(" . $param->name . ");\n");
}
push(@cBody, " UNUSED_PARAM(error);\n") if $raisesException;
push(@cBody, @conditionalWarn) if scalar(@conditionalWarn);
if ($returnType ne "void") {
if ($codeGenerator->IsNonPointerType($functionSigType)) {
push(@cBody, " return static_cast<${returnType}>(0);\n");
} else {
push(@cBody, " return 0;\n");
}
}
push(@cBody, "#endif /* ${conditionalString} */\n");
}
if ($parentConditionalString) {
push(@cBody, "#else\n");
push(@cBody, " UNUSED_PARAM(self);\n");
foreach my $param (@{$function->parameters}) {
push(@cBody, " UNUSED_PARAM(" . $param->name . ");\n");
}
push(@cBody, " UNUSED_PARAM(error);\n") if $raisesException;
push(@cBody, @parentConditionalWarn) if scalar(@parentConditionalWarn);
if ($returnType ne "void") {
if ($codeGenerator->IsNonPointerType($functionSigType)) {
push(@cBody, " return static_cast<${returnType}>(0);\n");
} else {
push(@cBody, " return 0;\n");
}
}
push(@cBody, "#endif /* ${parentConditionalString} */\n");
}
push(@cBody, "}\n\n");
}
sub ClassHasFunction {
my ($class, $name) = @_;
foreach my $function (@{$class->functions}) {
if ($function->signature->name eq $name) {
return 1;
}
}
return 0;
}
sub GenerateFunctions {
my ($object, $interfaceName, $interface) = @_;
foreach my $function (@{$interface->functions}) {
$object->GenerateFunction($interfaceName, $function, "", $interface);
}
TOP:
foreach my $attribute (@{$interface->attributes}) {
if (SkipAttribute($attribute)) {
next TOP;
}
my $attrNameUpper = $codeGenerator->WK_ucfirst($attribute->signature->name);
my $getname = "get${attrNameUpper}";
my $setname = "set${attrNameUpper}";
if (ClassHasFunction($interface, $getname) || ClassHasFunction($interface, $setname)) {
# Very occasionally an IDL file defines getter/setter functions for one of its
# attributes; in this case we don't need to autogenerate the getter/setter.
next TOP;
}
# Generate an attribute getter. For an attribute "foo", this is a function named
# "get_foo" which calls a DOM class method named foo().
my $function = new domFunction();
$function->signature($attribute->signature);
$function->signature->extendedAttributes({%{$attribute->signature->extendedAttributes}});
if ($attribute->signature->extendedAttributes->{"GetterRaisesException"}) {
$function->signature->extendedAttributes->{"RaisesException"} = "VALUE_IS_MISSING";
}
$object->GenerateFunction($interfaceName, $function, "get_", $interface);
# FIXME: We are not generating setters for 'Replaceable'
# attributes now, but we should somehow.
my $custom = $attribute->signature->extendedAttributes->{"CustomSetter"};
if ($attribute->isReadOnly || $attribute->signature->extendedAttributes->{"Replaceable"}
|| $attribute->signature->extendedAttributes->{"CallWith"}
|| $attribute->signature->extendedAttributes->{"SetterCallWith"} || $custom) {
next TOP;
}
# Generate an attribute setter. For an attribute, "foo", this is a function named
# "set_foo" which calls a DOM class method named setFoo().
$function = new domFunction();
$function->signature(new domSignature());
$function->signature->name($attribute->signature->name);
$function->signature->type($attribute->signature->type);
$function->signature->extendedAttributes({%{$attribute->signature->extendedAttributes}});
my $param = new domSignature();
$param->name("value");
$param->type($attribute->signature->type);
my %attributes = ();
$param->extendedAttributes(\%attributes);
my $arrayRef = $function->parameters;
push(@$arrayRef, $param);
if ($attribute->signature->extendedAttributes->{"SetterRaisesException"}) {
$function->signature->extendedAttributes->{"RaisesException"} = "VALUE_IS_MISSING";
} else {
delete $function->signature->extendedAttributes->{"RaisesException"};
}
$object->GenerateFunction($interfaceName, $function, "set_", $interface);
}
}
sub ImplementsInterface {
my $interface = shift;
my $implementInterface = shift;
return $codeGenerator->InheritsInterface($interface, $implementInterface);
}
sub GenerateCFile {
my ($object, $interfaceName, $parentClassName, $parentGObjType, $interface) = @_;
if (ImplementsInterface($interface, "EventTarget")) {
$object->GenerateEventTargetIface($interface);
}
my $implContent = "";
my $decamelize = decamelize($interfaceName);
my $clsCaps = uc($decamelize);
my $lowerCaseIfaceName = "webkit_dom_$decamelize";
my $parentImplClassName = GetParentImplClassName($interface);
my $baseClassName = GetBaseClass($parentImplClassName, $interface);
# Add a private struct only for direct subclasses of Object so that we can use RefPtr
# for the WebCore wrapped object and make sure we only increment the reference counter once.
if ($parentImplClassName eq "Object") {
my $conditionalString = $codeGenerator->GenerateConditionalString($interface);
push(@cStructPriv, "#define WEBKIT_DOM_${clsCaps}_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_${clsCaps}, ${className}Private)\n\n");
push(@cStructPriv, "typedef struct _${className}Private {\n");
push(@cStructPriv, "#if ${conditionalString}\n") if $conditionalString;
push(@cStructPriv, " RefPtr<WebCore::${interfaceName}> coreObject;\n");
push(@cStructPriv, "#endif // ${conditionalString}\n") if $conditionalString;
push(@cStructPriv, "} ${className}Private;\n\n");
}
$implContent = << "EOF";
${defineTypeMacro}(${className}, ${lowerCaseIfaceName}, ${parentGObjType}${defineTypeInterfaceImplementation}
EOF
push(@cBodyProperties, $implContent);
if ($parentImplClassName eq "Object") {
push(@cBodyPriv, "${className}* kit(WebCore::$interfaceName* obj)\n");
push(@cBodyPriv, "{\n");
push(@cBodyPriv, " if (!obj)\n");
push(@cBodyPriv, " return 0;\n\n");
push(@cBodyPriv, " if (gpointer ret = DOMObjectCache::get(obj))\n");
push(@cBodyPriv, " return WEBKIT_DOM_${clsCaps}(ret);\n\n");
if (IsPolymorphic($interfaceName)) {
push(@cBodyPriv, " return wrap(obj);\n");
} else {
push(@cBodyPriv, " return wrap${interfaceName}(obj);\n");
}
push(@cBodyPriv, "}\n\n");
} else {
push(@cBodyPriv, "${className}* kit(WebCore::$interfaceName* obj)\n");
push(@cBodyPriv, "{\n");
if (!IsPolymorphic($baseClassName)) {
push(@cBodyPriv, " if (!obj)\n");
push(@cBodyPriv, " return 0;\n\n");
push(@cBodyPriv, " if (gpointer ret = DOMObjectCache::get(obj))\n");
push(@cBodyPriv, " return WEBKIT_DOM_${clsCaps}(ret);\n\n");
push(@cBodyPriv, " return wrap${interfaceName}(obj);\n");
} else {
push(@cBodyPriv, " return WEBKIT_DOM_${clsCaps}(kit(static_cast<WebCore::$baseClassName*>(obj)));\n");
}
push(@cBodyPriv, "}\n\n");
}
$implContent = << "EOF";
WebCore::${interfaceName}* core(${className}* request)
{
return request ? static_cast<WebCore::${interfaceName}*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0;
}
${className}* wrap${interfaceName}(WebCore::${interfaceName}* coreObject)
{
ASSERT(coreObject);
return WEBKIT_DOM_${clsCaps}(g_object_new(WEBKIT_DOM_TYPE_${clsCaps}, "core-object", coreObject, nullptr));
}
EOF
push(@cBodyPriv, $implContent);
$object->GenerateProperties($interfaceName, $interface);
$object->GenerateFunctions($interfaceName, $interface);
}
sub GenerateEndHeader {
my ($object) = @_;
my $isStableClass = scalar(@stableSymbols);
if (!$isStableClass) {
push(@hPrefixGuardEnd, "#endif /* WEBKIT_DOM_USE_UNSTABLE_API */\n");
}
#Header guard
my $guard = $className . "_h";
push(@hBody, "G_END_DECLS\n\n");
push(@hPrefixGuardEnd, "#endif /* $guard */\n");
}
sub IsPolymorphic {
my $type = shift;
return scalar(grep {$_ eq $type} qw(Blob Event HTMLCollection Node StyleSheet TextTrackCue));
}
sub GenerateEventTargetIface {
my $object = shift;
my $interface = shift;
my $interfaceName = $interface->name;
my $decamelize = decamelize($interfaceName);
my $conditionalString = $codeGenerator->GenerateConditionalString($interface);
my @conditionalWarn = GenerateConditionalWarning($interface);
$implIncludes{"GObjectEventListener.h"} = 1;
$implIncludes{"WebKitDOMEventTarget.h"} = 1;
$implIncludes{"WebKitDOMEventPrivate.h"} = 1;
push(@cBodyProperties, "static gboolean webkit_dom_${decamelize}_dispatch_event(WebKitDOMEventTarget* target, WebKitDOMEvent* event, GError** error)\n{\n");
push(@cBodyProperties, "#if ${conditionalString}\n") if $conditionalString;
push(@cBodyProperties, " WebCore::Event* coreEvent = WebKit::core(event);\n");
push(@cBodyProperties, " WebCore::${interfaceName}* coreTarget = static_cast<WebCore::${interfaceName}*>(WEBKIT_DOM_OBJECT(target)->coreObject);\n\n");
push(@cBodyProperties, " WebCore::ExceptionCode ec = 0;\n");
push(@cBodyProperties, " gboolean result = coreTarget->dispatchEventForBindings(coreEvent, ec);\n");
push(@cBodyProperties, " if (ec) {\n WebCore::ExceptionCodeDescription description(ec);\n");
push(@cBodyProperties, " g_set_error_literal(error, g_quark_from_string(\"WEBKIT_DOM\"), description.code, description.name);\n }\n");
push(@cBodyProperties, " return result;\n");
if ($conditionalString) {
push(@cBodyProperties, "#else\n");
push(@cBodyProperties, " UNUSED_PARAM(target);\n");
push(@cBodyProperties, " UNUSED_PARAM(event);\n");
push(@cBodyProperties, " UNUSED_PARAM(error);\n");
push(@cBodyProperties, @conditionalWarn) if scalar(@conditionalWarn);
push(@cBodyProperties, " return false;\n#endif // ${conditionalString}\n");
}
push(@cBodyProperties, "}\n\n");
push(@cBodyProperties, "static gboolean webkit_dom_${decamelize}_add_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)\n{\n");
push(@cBodyProperties, "#if ${conditionalString}\n") if $conditionalString;
push(@cBodyProperties, " WebCore::${interfaceName}* coreTarget = static_cast<WebCore::${interfaceName}*>(WEBKIT_DOM_OBJECT(target)->coreObject);\n");
push(@cBodyProperties, " return WebCore::GObjectEventListener::addEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);\n");
if ($conditionalString) {
push(@cBodyProperties, "#else\n");
push(@cBodyProperties, " UNUSED_PARAM(target);\n");
push(@cBodyProperties, " UNUSED_PARAM(eventName);\n");
push(@cBodyProperties, " UNUSED_PARAM(handler);\n");
push(@cBodyProperties, " UNUSED_PARAM(useCapture);\n");
push(@cBodyProperties, @conditionalWarn) if scalar(@conditionalWarn);
push(@cBodyProperties, " return false;\n#endif // ${conditionalString}\n");
}
push(@cBodyProperties, "}\n\n");
push(@cBodyProperties, "static gboolean webkit_dom_${decamelize}_remove_event_listener(WebKitDOMEventTarget* target, const char* eventName, GClosure* handler, gboolean useCapture)\n{\n");
push(@cBodyProperties, "#if ${conditionalString}\n") if $conditionalString;
push(@cBodyProperties, " WebCore::${interfaceName}* coreTarget = static_cast<WebCore::${interfaceName}*>(WEBKIT_DOM_OBJECT(target)->coreObject);\n");
push(@cBodyProperties, " return WebCore::GObjectEventListener::removeEventListener(G_OBJECT(target), coreTarget, eventName, handler, useCapture);\n");
if ($conditionalString) {
push(@cBodyProperties, "#else\n");
push(@cBodyProperties, " UNUSED_PARAM(target);\n");
push(@cBodyProperties, " UNUSED_PARAM(eventName);\n");
push(@cBodyProperties, " UNUSED_PARAM(handler);\n");
push(@cBodyProperties, " UNUSED_PARAM(useCapture);\n");
push(@cBodyProperties, @conditionalWarn) if scalar(@conditionalWarn);
push(@cBodyProperties, " return false;\n#endif // ${conditionalString}\n");
}
push(@cBodyProperties, "}\n\n");
push(@cBodyProperties, "static void webkit_dom_event_target_init(WebKitDOMEventTargetIface* iface)\n{\n");
push(@cBodyProperties, " iface->dispatch_event = webkit_dom_${decamelize}_dispatch_event;\n");
push(@cBodyProperties, " iface->add_event_listener = webkit_dom_${decamelize}_add_event_listener;\n");
push(@cBodyProperties, " iface->remove_event_listener = webkit_dom_${decamelize}_remove_event_listener;\n}\n\n");
$defineTypeMacro = "G_DEFINE_TYPE_WITH_CODE";
$defineTypeInterfaceImplementation = ", G_IMPLEMENT_INTERFACE(WEBKIT_DOM_TYPE_EVENT_TARGET, webkit_dom_event_target_init))";
}
sub Generate {
my ($object, $interface) = @_;
my $parentClassName = GetParentClassName($interface);
my $parentGObjType = GetParentGObjType($interface);
my $interfaceName = $interface->name;
my $parentImplClassName = GetParentImplClassName($interface);
my $baseClassName = GetBaseClass($parentImplClassName, $interface);
# Add the default impl header template
@cPrefix = split("\r", $licenceTemplate);
push(@cPrefix, "\n");
$implIncludes{"DOMObjectCache.h"} = 1;
$implIncludes{"WebKitDOMPrivate.h"} = 1;
$implIncludes{"gobject/ConvertToUTF8String.h"} = 1;
$implIncludes{"${className}Private.h"} = 1;
$implIncludes{"Document.h"} = 1;
$implIncludes{"JSMainThreadExecState.h"} = 1;
$implIncludes{"ExceptionCode.h"} = 1;
$implIncludes{"ExceptionCodeDescription.h"} = 1;
$implIncludes{"CSSImportRule.h"} = 1;
if ($parentImplClassName ne "Object" and IsPolymorphic($baseClassName)) {
$implIncludes{"WebKitDOM${baseClassName}Private.h"} = 1;
}
$hdrIncludes{"webkitdom/${parentClassName}.h"} = 1;
$object->GenerateHeader($interfaceName, $parentClassName, $interface);
$object->GenerateCFile($interfaceName, $parentClassName, $parentGObjType, $interface);
$object->GenerateEndHeader();
}
sub HasUnstableCustomAPI {
my $domClassName = shift;
return scalar(grep {$_ eq $domClassName} qw(WebKitDOMDOMWindow WebKitDOMUserMessageHandlersNamespace WebKitDOMHTMLLinkElement));
}
sub WriteData {
my $object = shift;
my $interface = shift;
my $outputDir = shift;
mkdir $outputDir;
my $isStableClass = scalar(@stableSymbols);
# Write a private header.
my $interfaceName = $interface->name;
my $filename = "$outputDir/" . $className . "Private.h";
my $guard = "${className}Private_h";
# Add the guard if the 'Conditional' extended attribute exists
my $conditionalString = $codeGenerator->GenerateConditionalString($interface);
open(PRIVHEADER, ">$filename") or die "Couldn't open file $filename for writing";
print PRIVHEADER split("\r", $licenceTemplate);
print PRIVHEADER "\n";
my $text = << "EOF";
#ifndef $guard
#define $guard
#include "${interfaceName}.h"
#include <webkitdom/${className}.h>
EOF
print PRIVHEADER $text;
print PRIVHEADER "#if ${conditionalString}\n" if $conditionalString;
print PRIVHEADER "\n";
$text = << "EOF";
namespace WebKit {
${className}* wrap${interfaceName}(WebCore::${interfaceName}*);
${className}* kit(WebCore::${interfaceName}*);
WebCore::${interfaceName}* core(${className}*);
EOF
print PRIVHEADER $text;
$text = << "EOF";
} // namespace WebKit
EOF
print PRIVHEADER $text;
print PRIVHEADER "#endif /* ${conditionalString} */\n\n" if $conditionalString;
print PRIVHEADER "#endif /* ${guard} */\n";
close(PRIVHEADER);
my $basename = FileNamePrefix . $interfaceName;
$basename =~ s/_//g;
# Write public header.
my $fullHeaderFilename = "$outputDir/" . $basename . ".h";
my $installedHeaderFilename = "${basename}.h";
open(HEADER, ">$fullHeaderFilename") or die "Couldn't open file $fullHeaderFilename";
print HEADER @hPrefix;
print HEADER @hPrefixGuard;
print HEADER "#include <glib-object.h>\n";
print HEADER map { "#include <$_>\n" } sort keys(%hdrIncludes);
if ($isStableClass) {
print HEADER "#include <webkitdom/webkitdomdefines.h>\n\n";
} else {
if (HasUnstableCustomAPI($className)) {
print HEADER "#include <webkitdom/WebKitDOMCustomUnstable.h>\n";
}
print HEADER "#include <webkitdom/webkitdomdefines-unstable.h>\n\n";
}
print HEADER @hBodyPre;
print HEADER @hBody;
print HEADER @hPrefixGuardEnd;
close(HEADER);
# Write the unstable header if needed.
if ($isStableClass and scalar(@hBodyUnstable)) {
my $fullUnstableHeaderFilename = "$outputDir/" . $className . "Unstable.h";
open(UNSTABLE, ">$fullUnstableHeaderFilename") or die "Couldn't open file $fullUnstableHeaderFilename";
print UNSTABLE split("\r", $licenceTemplate);
print UNSTABLE "\n";
$guard = "${className}Unstable_h";
$text = << "EOF";
#ifndef $guard
#define $guard
#ifdef WEBKIT_DOM_USE_UNSTABLE_API
EOF
print UNSTABLE $text;
if (HasUnstableCustomAPI($className)) {
print UNSTABLE "#include <webkitdom/WebKitDOMCustomUnstable.h>\n";
}
print UNSTABLE "#include <webkitdom/webkitdomdefines-unstable.h>\n\n";
print UNSTABLE "#if ${conditionalString}\n\n" if $conditionalString;
print UNSTABLE "G_BEGIN_DECLS\n";
print UNSTABLE "\n";
print UNSTABLE @hBodyUnstable;
print UNSTABLE "\n";
print UNSTABLE "G_END_DECLS\n";
print UNSTABLE "\n";
print UNSTABLE "#endif /* ${conditionalString} */\n\n" if $conditionalString;
print UNSTABLE "#endif /* WEBKIT_DOM_USE_UNSTABLE_API */\n";
print UNSTABLE "#endif /* ${guard} */\n";
close(UNSTABLE);
}
# Write the implementation sources
my $implFileName = "$outputDir/" . $basename . ".cpp";
open(IMPL, ">$implFileName") or die "Couldn't open file $implFileName";
print IMPL @cPrefix;
print IMPL "#include \"config.h\"\n";
print IMPL "#include \"$installedHeaderFilename\"\n\n";
# Remove the implementation header from the list of included files.
%includesCopy = %implIncludes;
print IMPL map { "#include \"$_\"\n" } sort keys(%includesCopy);
if ($isStableClass and scalar(@hBodyUnstable)) {
print IMPL "#include \"${className}Unstable.h\"\n";
}
print IMPL "#include <wtf/GetPtr.h>\n";
print IMPL "#include <wtf/RefPtr.h>\n\n";
print IMPL @cStructPriv;
print IMPL "#if ${conditionalString}\n\n" if $conditionalString;
print IMPL "namespace WebKit {\n\n";
print IMPL @cBodyPriv;
print IMPL "} // namespace WebKit\n\n";
print IMPL "#endif // ${conditionalString}\n\n" if $conditionalString;
print IMPL @cBodyProperties;
print IMPL @cBody;
close(IMPL);
# Write a symbols file.
if ($isStableClass) {
my $symbolsFileName = "$outputDir/" . $basename . ".symbols";
open(SYM, ">$symbolsFileName") or die "Couldn't open file $symbolsFileName";
print SYM @symbols;
close(SYM);
}
%implIncludes = ();
%hdrIncludes = ();
@hPrefix = ();
@hBody = ();
@hBodyUnstable = ();
@cPrefix = ();
@cBody = ();
@cBodyPriv = ();
@cBodyProperties = ();
@cStructPriv = ();
@symbols = ();
@stableSymbols = ();
}
sub IsInterfaceSymbol {
my ($line, $lowerCaseIfaceName) = @_;
# Function.
return 1 if $line =~ /^[a-zA-Z0-9\*]+\s${lowerCaseIfaceName}_.+$/;
# Constant.
my $prefix = uc($lowerCaseIfaceName);
return 1 if $line =~ /^${prefix}_[A-Z_]+$/;
return 0;
}
sub ReadStableSymbols {
my $interfaceName = shift;
@stableSymbols = ();
my $bindingsDir = dirname($FindBin::Bin);
my $fileName = "$bindingsDir/gobject/webkitdom.symbols";
open FILE, "<", $fileName or die "Could not open $fileName";
my @lines = <FILE>;
close FILE;
my $decamelize = decamelize($interfaceName);
my $lowerCaseIfaceName = "webkit_dom_$decamelize";
foreach $line (@lines) {
$line =~ s/\n$//;
my ($symbol) = split('@', $line, 2);
if ($symbol eq "GType ${lowerCaseIfaceName}_get_type(void)") {
push(@stableSymbols, $line);
next;
}
if (scalar(@stableSymbols) and IsInterfaceSymbol($symbol, $lowerCaseIfaceName) and $symbol !~ /^GType/) {
push(@stableSymbols, $line);
next;
}
if (scalar(@stableSymbols) and $symbol !~ /^GType/) {
warn "Symbol %line found, but a get_type was expected";
}
last if scalar(@stableSymbols);
}
}
sub GenerateInterface {
my ($object, $interface, $defines) = @_;
# Set up some global variables
$className = GetClassName($interface->name);
ReadStableSymbols($interface->name);
$object->Generate($interface);
}
1;
|