package js_of_ocaml-compiler
Compiler from OCaml bytecode to JavaScript
Install
Dune Dependency
Authors
Maintainers
Sources
js_of_ocaml-6.0.1.tbz
sha256=813dbee2b62e1541049ea23a20e405cf244e27ebfa9859785cfa53e286d2c614
sha512=194ae5d1122171fa8253b6a41438a2fc330caf4ab6dd008fcce1253fd51fbe4b1149813da6075c5deb52ea136143def57c83c3f4e32421803d7699648fdc563b
doc/src/js_of_ocaml-compiler/js_traverse.ml.html
Source file js_traverse.ml
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
(* Js_of_ocaml compiler * http://www.ocsigen.org/js_of_ocaml/ * Copyright (C) 2013 Hugo Heuzard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, with linking exception; * either version 2.1 of the License, or (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) open! Stdlib open Javascript class type mapper = object method loc : Javascript.location -> Javascript.location method parse_info : Parse_info.t -> Parse_info.t method expression : Javascript.expression -> Javascript.expression method expression_o : Javascript.expression option -> Javascript.expression option method switch_case : Javascript.expression -> Javascript.expression method block : Javascript.statement_list -> Javascript.statement_list method fun_decl : Javascript.function_declaration -> Javascript.function_declaration method class_decl : Javascript.class_declaration -> Javascript.class_declaration method class_element : Javascript.class_element -> Javascript.class_element method initialiser : Javascript.expression * Javascript.location -> Javascript.expression * Javascript.location method initialiser_o : (Javascript.expression * Javascript.location) option -> (Javascript.expression * Javascript.location) option method for_binding : Javascript.variable_declaration_kind -> Javascript.for_binding -> Javascript.for_binding method binding_property : Javascript.binding_property -> Javascript.binding_property method variable_declaration : Javascript.variable_declaration_kind -> Javascript.variable_declaration -> Javascript.variable_declaration method statement : Javascript.statement -> Javascript.statement method statement_o : (Javascript.statement * Javascript.location) option -> (Javascript.statement * Javascript.location) option method statements : Javascript.statement_list -> Javascript.statement_list method formal_parameter_list : Javascript.formal_parameter_list -> Javascript.formal_parameter_list method ident : Javascript.ident -> Javascript.ident method program : Javascript.program -> Javascript.program method function_body : statement_list -> statement_list method import : import -> import method export : export -> export end (* generic js ast walk/map *) class map : mapper = object (m) method loc = function | N -> N | U -> U | Pi x -> Pi (m#parse_info x) method parse_info i = i method ident i = match i with | V v -> V v | S { name; var; loc } -> S { name; var; loc = m#loc loc } method private early_error { reason; loc } = { reason; loc = m#parse_info loc } method statements l = List.map l ~f:(fun (s, pc) -> m#statement s, m#loc pc) method variable_declaration _ x = match x with | DeclIdent (id, eo) -> DeclIdent (m#ident id, m#initialiser_o eo) | DeclPattern (p, i) -> DeclPattern (m#binding_pattern p, m#initialiser i) method for_binding _ x = m#binding x method formal_parameter_list { list; rest } = { list = List.map list ~f:m#param; rest = Option.map rest ~f:m#binding } method private property_name x = match x with | (PNI _ | PNS _ | PNN _) as x -> x | PComputed e -> PComputed (m#expression e) method fun_decl (k, params, body, nid) = k, m#formal_parameter_list params, m#function_body body, m#loc nid method class_decl x = { extends = Option.map x.extends ~f:m#expression ; body = List.map x.body ~f:m#class_element } method class_element x = match x with | CEMethod (s, n, meth) -> CEMethod (s, m#class_element_name n, m#method_ meth) | CEField (s, n, i) -> CEField (s, m#class_element_name n, m#initialiser_o i) | CEStaticBLock b -> CEStaticBLock (m#block b) method private class_element_name x = match x with | PropName n -> PropName (m#property_name n) | PrivName x -> PrivName x method block l = m#statements l method statement s = match s with | Block b -> Block (m#block b) | Variable_statement (k, l) -> Variable_statement (k, List.map l ~f:(m#variable_declaration k)) | Function_declaration (id, fun_decl) -> Function_declaration (m#ident id, m#fun_decl fun_decl) | Class_declaration (id, cl_decl) -> Class_declaration (m#ident id, m#class_decl cl_decl) | Empty_statement -> Empty_statement | Debugger_statement -> Debugger_statement | Expression_statement e -> Expression_statement (m#expression e) | If_statement (e, (s, loc), sopt) -> If_statement (m#expression e, (m#statement s, m#loc loc), m#statement_o sopt) | Do_while_statement ((s, loc), e) -> Do_while_statement ((m#statement s, m#loc loc), m#expression e) | While_statement (e, (s, loc)) -> While_statement (m#expression e, (m#statement s, m#loc loc)) | For_statement (e1, e2, e3, (s, loc)) -> let e1 = match e1 with | Left o -> Left (m#expression_o o) | Right (k, l) -> Right (k, List.map l ~f:(fun d -> m#variable_declaration k d)) in For_statement (e1, m#expression_o e2, m#expression_o e3, (m#statement s, m#loc loc)) | ForIn_statement (e1, e2, (s, loc)) -> let e1 = match e1 with | Left e -> Left (m#expression e) | Right (k, d) -> Right (k, m#for_binding k d) in ForIn_statement (e1, m#expression e2, (m#statement s, m#loc loc)) | ForOf_statement (e1, e2, (s, loc)) -> let e1 = match e1 with | Left e -> Left (m#expression e) | Right (k, d) -> Right (k, m#for_binding k d) in ForOf_statement (e1, m#expression e2, (m#statement s, m#loc loc)) | ForAwaitOf_statement (e1, e2, (s, loc)) -> let e1 = match e1 with | Left e -> Left (m#expression e) | Right (k, d) -> Right (k, m#for_binding k d) in ForAwaitOf_statement (e1, m#expression e2, (m#statement s, m#loc loc)) | Continue_statement s -> Continue_statement s | Break_statement s -> Break_statement s | Return_statement (e, loc) -> Return_statement (m#expression_o e, m#loc loc) | Labelled_statement (l, (s, loc)) -> Labelled_statement (l, (m#statement s, m#loc loc)) | Throw_statement e -> Throw_statement (m#expression e) | Switch_statement (e, l, def, l') -> Switch_statement ( m#expression e , List.map l ~f:(fun (e, s) -> m#switch_case e, m#statements s) , (match def with | None -> None | Some l -> Some (m#statements l)) , List.map l' ~f:(fun (e, s) -> m#switch_case e, m#statements s) ) | Try_statement (b, catch, final) -> Try_statement ( m#block b , (match catch with | None -> None | Some (id, b) -> Some (Option.map ~f:m#param id, m#block b)) , match final with | None -> None | Some s -> Some (m#block s) ) | With_statement (e, (s, loc)) -> With_statement (m#expression e, (m#statement s, m#loc loc)) | Import (import, loc) -> Import (m#import import, m#parse_info loc) | Export (export, loc) -> Export (m#export export, m#parse_info loc) method import { from; kind } = let kind = match kind with | Namespace (iopt, i) -> Namespace (Option.map ~f:m#ident iopt, m#ident i) | Named (iopt, l) -> Named (Option.map ~f:m#ident iopt, List.map ~f:(fun (s, id) -> s, m#ident id) l) | Default import_default -> Default (m#ident import_default) | SideEffect -> SideEffect in { from; kind } method export e = match e with | ExportVar (k, l) -> ( match m#statement (Variable_statement (k, l)) with | Variable_statement (k, l) -> ExportVar (k, l) | _ -> assert false) | ExportFun (id, f) -> ( match m#statement (Function_declaration (id, f)) with | Function_declaration (id, f) -> ExportFun (id, f) | _ -> assert false) | ExportClass (id, f) -> ( match m#statement (Class_declaration (id, f)) with | Class_declaration (id, f) -> ExportClass (id, f) | _ -> assert false) | ExportNames l -> ExportNames (List.map ~f:(fun (id, s) -> m#ident id, s) l) | ExportDefaultFun (Some id, decl) -> ( match m#statement (Function_declaration (id, decl)) with | Function_declaration (id, decl) -> ExportDefaultFun (Some id, decl) | _ -> assert false) | ExportDefaultFun (None, decl) -> ( match m#expression (EFun (None, decl)) with | EFun (None, decl) -> ExportDefaultFun (None, decl) | _ -> assert false) | ExportDefaultClass (Some id, decl) -> ( match m#statement (Class_declaration (id, decl)) with | Class_declaration (id, decl) -> ExportDefaultClass (Some id, decl) | _ -> assert false) | ExportDefaultClass (None, decl) -> ( match m#expression (EClass (None, decl)) with | EClass (None, decl) -> ExportDefaultClass (None, decl) | _ -> assert false) | ExportDefaultExpression e -> ExportDefaultExpression (m#expression e) | ExportFrom l -> ExportFrom l | CoverExportFrom e -> CoverExportFrom (m#early_error e) method statement_o x = match x with | None -> None | Some (s, loc) -> Some (m#statement s, m#loc loc) method switch_case e = m#expression e method private argument a = match a with | Arg e -> Arg (m#expression e) | ArgSpread e -> ArgSpread (m#expression e) method private template l = List.map l ~f:(function | TStr s -> TStr s | TExp e -> TExp (m#expression e)) method expression x = match x with | ESeq (e1, e2) -> ESeq (m#expression e1, m#expression e2) | ECond (e1, e2, e3) -> ECond (m#expression e1, m#expression e2, m#expression e3) | EBin (b, e1, e2) -> EBin (b, m#expression e1, m#expression e2) | EAssignTarget x -> ( match x with | ArrayTarget l -> EAssignTarget (ArrayTarget (List.map l ~f:(function | TargetElementHole -> TargetElementHole | TargetElementId (i, e) -> TargetElementId (m#ident i, m#initialiser_o e) | TargetElement e -> TargetElement (m#expression e) | TargetElementSpread e -> TargetElementSpread (m#expression e)))) | ObjectTarget l -> EAssignTarget (ObjectTarget (List.map l ~f:(function | TargetPropertyId (Prop_and_ident i, e) -> TargetPropertyId (Prop_and_ident (m#ident i), m#initialiser_o e) | TargetProperty (n, e, i) -> TargetProperty (m#property_name n, m#expression e, m#initialiser_o i) | TargetPropertyMethod (n, x) -> TargetPropertyMethod (m#property_name n, m#method_ x) | TargetPropertySpread e -> TargetPropertySpread (m#expression e))))) | EUn (b, e1) -> EUn (b, m#expression e1) | ECallTemplate (e1, t, loc) -> ECallTemplate (m#expression e1, m#template t, m#loc loc) | ECall (e1, ak, e2, loc) -> ECall (m#expression e1, ak, List.map e2 ~f:m#argument, m#loc loc) | EAccess (e1, ak, e2) -> EAccess (m#expression e1, ak, m#expression e2) | EDot (e1, ak, id) -> EDot (m#expression e1, ak, id) | EDotPrivate (e1, ak, id) -> EDotPrivate (m#expression e1, ak, id) | ENew (e1, args, loc) -> ENew (m#expression e1, Option.map ~f:(List.map ~f:m#argument) args, m#loc loc) | EVar v -> EVar (m#ident v) | EFun (idopt, fun_decl) -> let idopt = Option.map ~f:m#ident idopt in EFun (idopt, m#fun_decl fun_decl) | EClass (id, cl_decl) -> EClass (Option.map ~f:m#ident id, m#class_decl cl_decl) | EArrow (fun_decl, consise, x) -> EArrow (m#fun_decl fun_decl, consise, x) | EArr l -> EArr (List.map l ~f:(function | ElementHole -> ElementHole | Element e -> Element (m#expression e) | ElementSpread e -> ElementSpread (m#expression e))) | EObj l -> EObj (List.map l ~f:(fun p -> match p with | Property (i, e) -> Property (m#property_name i, m#expression e) | PropertyMethod (n, x) -> PropertyMethod (m#property_name n, m#method_ x) | PropertySpread e -> PropertySpread (m#expression e) | CoverInitializedName (e, a, b) -> CoverInitializedName (m#early_error e, a, b))) | (EStr _ as x) | (EBool _ as x) | (ENum _ as x) | (ERegexp _ as x) -> x | ETemplate t -> ETemplate (m#template t) | EYield { delegate; expr } -> EYield { delegate; expr = m#expression_o expr } | EPrivName i -> EPrivName i | CoverParenthesizedExpressionAndArrowParameterList e -> CoverParenthesizedExpressionAndArrowParameterList (m#early_error e) | CoverCallExpressionAndAsyncArrowHead e -> CoverCallExpressionAndAsyncArrowHead (m#early_error e) method private method_ x = match x with | MethodSet fun_decl -> MethodSet (m#fun_decl fun_decl) | MethodGet fun_decl -> MethodGet (m#fun_decl fun_decl) | Method fun_decl -> Method (m#fun_decl fun_decl) method private param p = m#binding_element p method private binding_element (b, e) = m#binding b, m#initialiser_o e method private binding x = match x with | BindingIdent x -> BindingIdent (m#ident x) | BindingPattern x -> BindingPattern (m#binding_pattern x) method private binding_pattern x = match x with | ObjectBinding { list; rest } -> ObjectBinding { list = List.map list ~f:m#binding_property ; rest = Option.map rest ~f:m#ident } | ArrayBinding { list; rest } -> ArrayBinding { list = List.map list ~f:m#binding_array_elt ; rest = Option.map rest ~f:m#binding } method private binding_array_elt x = match x with | None -> None | Some (b, e) -> Some (m#binding b, m#initialiser_o e) method binding_property x = match x with | Prop_binding (i, e) -> Prop_binding (m#property_name i, m#binding_element e) | Prop_ident (Prop_and_ident i, e) -> Prop_ident (Prop_and_ident (m#ident i), m#initialiser_o e) method expression_o x = match x with | None -> None | Some s -> Some (m#expression s) method initialiser (e, loc) = m#expression e, m#loc loc method initialiser_o x = match x with | None -> None | Some i -> Some (m#initialiser i) method program x = m#statements x method function_body x = m#statements x end class type iterator = object method fun_decl : Javascript.function_declaration -> unit method class_decl : Javascript.class_declaration -> unit method early_error : Javascript.early_error -> unit method expression : Javascript.expression -> unit method expression_o : Javascript.expression option -> unit method switch_case : Javascript.expression -> unit method block : Javascript.statement_list -> unit method initialiser : Javascript.expression * Javascript.location -> unit method initialiser_o : (Javascript.expression * Javascript.location) option -> unit method for_binding : Javascript.variable_declaration_kind -> Javascript.for_binding -> unit method variable_declaration : Javascript.variable_declaration_kind -> Javascript.variable_declaration -> unit method statement : Javascript.statement -> unit method statement_o : (Javascript.statement * Javascript.location) option -> unit method statements : Javascript.statement_list -> unit method ident : Javascript.ident -> unit method program : Javascript.program -> unit method function_body : Javascript.statement_list -> unit method import : import -> unit method export : export -> unit end (* generic js ast iterator *) class iter : iterator = object (m) method ident _ = () method early_error _ = () method block l = m#statements l method statements l = List.iter l ~f:(fun (s, _) -> m#statement s) method variable_declaration _ x = match x with | DeclIdent (id, eo) -> m#ident id; m#initialiser_o eo | DeclPattern (p, (e, (_ : location))) -> m#binding_pattern p; m#expression e method for_binding _ x = m#binding x method private formal_parameter_list { list; rest } = List.iter list ~f:m#param; Option.iter rest ~f:m#binding method private property_name x = match x with | PNI _ | PNS _ | PNN _ -> () | PComputed e -> m#expression e method fun_decl (_k, params, body, _loc) = m#formal_parameter_list params; m#function_body body method class_decl x = Option.iter x.extends ~f:m#expression; List.iter x.body ~f:m#class_element method private class_element x = match x with | CEMethod (_static, name, x) -> m#class_element_name name; m#method_ x | CEField (_static, n, i) -> m#class_element_name n; m#initialiser_o i | CEStaticBLock b -> m#block b method private class_element_name x = match x with | PropName n -> m#property_name n | PrivName (Utf8 _) -> () method statement s = match s with | Block b -> m#block b | Variable_statement (k, l) -> List.iter l ~f:(m#variable_declaration k) | Function_declaration (id, fun_decl) -> m#ident id; m#fun_decl fun_decl | Class_declaration (id, cl_decl) -> m#ident id; m#class_decl cl_decl | Empty_statement -> () | Debugger_statement -> () | Expression_statement e -> m#expression e | If_statement (e, (s, _), sopt) -> m#expression e; m#statement s; m#statement_o sopt | Do_while_statement ((s, _), e) -> m#statement s; m#expression e | While_statement (e, (s, _)) -> m#expression e; m#statement s | For_statement (e1, e2, e3, (s, _)) -> (match e1 with | Left o -> m#expression_o o | Right (k, l) -> List.iter l ~f:(fun d -> m#variable_declaration k d)); m#expression_o e2; m#expression_o e3; m#statement s | ForIn_statement (e1, e2, (s, _)) -> (match e1 with | Left e -> m#expression e | Right (k, d) -> m#for_binding k d); m#expression e2; m#statement s | ForOf_statement (e1, e2, (s, _)) -> (match e1 with | Left e -> m#expression e | Right (k, d) -> m#for_binding k d); m#expression e2; m#statement s | ForAwaitOf_statement (e1, e2, (s, _)) -> (match e1 with | Left e -> m#expression e | Right (k, d) -> m#for_binding k d); m#expression e2; m#statement s | Continue_statement _ -> () | Break_statement _ -> () | Return_statement (e, _) -> m#expression_o e | Labelled_statement (_, (s, _)) -> m#statement s | Throw_statement e -> m#expression e | Switch_statement (e, l, def, l') -> m#expression e; List.iter l ~f:(fun (e, s) -> m#switch_case e; m#statements s); (match def with | None -> () | Some l -> m#statements l); List.iter l' ~f:(fun (e, s) -> m#switch_case e; m#statements s) | Try_statement (b, catch, final) -> ( m#block b; (match catch with | None -> () | Some (id, b) -> Option.iter ~f:m#param id; m#block b); match final with | None -> () | Some s -> m#block s) | With_statement (e, (s, _)) -> m#expression e; m#statement s | Import (x, _loc) -> m#import x | Export (x, _loc) -> m#export x method import { from = _; kind } = match kind with | Namespace (iopt, i) -> Option.iter ~f:m#ident iopt; m#ident i | Named (iopt, l) -> Option.iter ~f:m#ident iopt; List.iter ~f:(fun (_, id) -> m#ident id) l | Default import_default -> m#ident import_default | SideEffect -> () method export e = match e with | ExportVar (k, l) -> m#statement (Variable_statement (k, l)) | ExportFun (id, f) -> m#statement (Function_declaration (id, f)) | ExportClass (id, f) -> m#statement (Class_declaration (id, f)) | ExportNames l -> List.iter ~f:(fun (id, _) -> m#ident id) l | ExportDefaultFun (Some id, decl) -> m#statement (Function_declaration (id, decl)) | ExportDefaultFun (None, decl) -> m#expression (EFun (None, decl)) | ExportDefaultClass (Some id, decl) -> m#statement (Class_declaration (id, decl)) | ExportDefaultClass (None, decl) -> m#expression (EClass (None, decl)) | ExportDefaultExpression e -> m#expression e | ExportFrom { from = _; kind = _ } -> () | CoverExportFrom e -> m#early_error e method statement_o x = match x with | None -> () | Some (s, _) -> m#statement s method switch_case e = m#expression e method private argument a = match a with | Arg e -> m#expression e | ArgSpread e -> m#expression e method private template l = List.iter l ~f:(function | TStr _ -> () | TExp e -> m#expression e) method expression x = match x with | ESeq (e1, e2) -> m#expression e1; m#expression e2 | ECond (e1, e2, e3) -> m#expression e1; m#expression e2; m#expression e3 | EBin (_, e1, e2) -> m#expression e1; m#expression e2 | EAssignTarget x -> ( match x with | ArrayTarget l -> List.iter l ~f:(function | TargetElementHole -> () | TargetElementId (i, e) -> m#ident i; m#initialiser_o e | TargetElement e -> m#expression e | TargetElementSpread e -> m#expression e) | ObjectTarget l -> List.iter l ~f:(function | TargetPropertyId (Prop_and_ident i, e) -> m#ident i; m#initialiser_o e | TargetProperty (n, e, i) -> m#property_name n; m#expression e; m#initialiser_o i | TargetPropertyMethod (n, x) -> m#property_name n; m#method_ x | TargetPropertySpread e -> m#expression e)) | EUn (_, e1) -> m#expression e1 | ECall (e1, _ak, e2, _) -> m#expression e1; List.iter e2 ~f:m#argument | ECallTemplate (e1, a, _) -> m#expression e1; m#template a | EAccess (e1, _ak, e2) -> m#expression e1; m#expression e2 | EDot (e1, _ak, _) -> m#expression e1 | EDotPrivate (e1, _, _) -> m#expression e1 | ENew (e1, Some args, _) -> m#expression e1; List.iter args ~f:m#argument | ENew (e1, None, _) -> m#expression e1 | EVar v -> m#ident v | EFun (idopt, fun_decl) -> (match idopt with | None -> () | Some i -> m#ident i); m#fun_decl fun_decl | EClass (i, cl_decl) -> Option.iter ~f:m#ident i; m#class_decl cl_decl | EArrow (fun_decl, _, _) -> m#fun_decl fun_decl | EArr l -> List.iter l ~f:(function | ElementHole -> () | Element e -> m#expression e | ElementSpread e -> m#expression e) | EObj l -> List.iter l ~f:(fun p -> match p with | Property (i, e) -> m#property_name i; m#expression e | PropertyMethod (n, x) -> m#property_name n; m#method_ x | PropertySpread e -> m#expression e | CoverInitializedName (e, _, _) -> m#early_error e) | EStr _ | EBool _ | ENum _ | ERegexp _ -> () | ETemplate l -> m#template l | EYield { delegate = _; expr } -> m#expression_o expr | EPrivName (Utf8 _) -> () | CoverParenthesizedExpressionAndArrowParameterList e -> m#early_error e | CoverCallExpressionAndAsyncArrowHead e -> m#early_error e method private method_ x = match x with | MethodSet fun_decl -> m#fun_decl fun_decl | MethodGet fun_decl -> m#fun_decl fun_decl | Method fun_decl -> m#fun_decl fun_decl method private param p = m#binding_element p method private binding_element (b, e) = m#binding b; m#initialiser_o e method private binding x = match x with | BindingIdent x -> m#ident x | BindingPattern x -> m#binding_pattern x method private binding_pattern x = match x with | ObjectBinding { list; rest } -> List.iter list ~f:m#binding_property; Option.iter rest ~f:m#ident | ArrayBinding { list; rest } -> List.iter list ~f:m#binding_array_elt; Option.iter rest ~f:m#binding method private binding_array_elt x = match x with | None -> () | Some (b, e) -> m#binding b; m#initialiser_o e method private binding_property x = match x with | Prop_binding ((_ : property_name), e) -> m#binding_element e | Prop_ident (Prop_and_ident i, e) -> m#ident i; m#initialiser_o e method expression_o x = match x with | None -> () | Some s -> m#expression s method initialiser (e, _) = m#expression e method initialiser_o x = match x with | None -> () | Some i -> m#initialiser i method program x = m#statements x method function_body x = m#statements x end (* var substitution *) class subst sub = object inherit map method ident x = sub x end class = object (m) inherit map as super method expression e = match e with (* JavaScript engines recognize the pattern 'typeof x==="number"'; if the string is shared, less efficient code is generated. *) | EBin (op, EUn (Typeof, e1), (EStr _ as e2)) -> EBin (op, EUn (Typeof, super#expression e1), e2) | EBin (op, (EStr _ as e1), EUn (Typeof, e2)) -> EBin (op, e1, EUn (Typeof, super#expression e2)) (* Some js bundler get confused when the argument of 'require' is not a literal *) | ECall ( EVar (S { var = None; name = Utf8 "requires"; _ }) , (ANormal | ANullish) , [ Arg (EStr _) ] , _ ) -> e | _ -> super#expression e (* do not replace constant in switch case *) method switch_case e = match e with | ENum _ | EStr _ -> e | _ -> m#expression e method statements l = match l with | [] -> [] | ((Expression_statement (EStr _), _) as prolog) :: rest -> prolog :: List.map rest ~f:(fun (x, loc) -> m#statement x, loc) | rest -> List.map rest ~f:(fun (x, loc) -> m#statement x, loc) end class replace_expr f = object inherit map_for_share_constant as super method expression e = try EVar (f e) with Not_found -> super#expression e end (* this optimisation should be done at the lowest common scope *) class = object inherit map_for_share_constant as super val count = Hashtbl.create 17 method expression e = let e = match e with | EStr _ | ENum _ -> let n = try Hashtbl.find count e with Not_found -> 0 in Hashtbl.replace count e (n + 1); e | _ -> e in super#expression e method program p = let p = super#program p in let all = Hashtbl.create 17 in Hashtbl.iter (fun x n -> let = match x with | EStr (Utf8 s) when n > 1 -> if String.length s < 20 then Some ("str_" ^ s) else Some ("str_" ^ String.sub s ~pos:0 ~len:16 ^ "_abr") | ENum s when n > 1 -> let s = Javascript.Num.to_string s in let l = String.length s in if l > 2 then Some ("num_" ^ s) else None | _ -> None in match shareit with | Some name -> let v = Code.Var.fresh_n name in Hashtbl.add all x (V v) | _ -> ()) count; if Hashtbl.length all = 0 then p else let f = Hashtbl.find all in let p = (new replace_expr f)#program p in let all = Hashtbl.fold (fun e v acc -> DeclIdent (v, Some (e, N)) :: acc) all [] in (Variable_statement (Var, all), N) :: p end type t = { use : IdentSet.t ; def_var : IdentSet.t ; def_local : IdentSet.t } let empty = { use = IdentSet.empty; def_var = IdentSet.empty; def_local = IdentSet.empty } (* def/used/free variable *) type block = | Catch of formal_parameter | Params of formal_parameter_list | Normal class type freevar = object ('a) inherit mapper method merge_info : 'a -> unit method merge_block_info : 'a -> unit method record_block : block -> unit method state : t method def_var : Javascript.ident -> unit method def_local : Javascript.ident -> unit method use_var : Javascript.ident -> unit method get_count : int Javascript.IdentMap.t method get_free : IdentSet.t method get_def : IdentSet.t method get_use : IdentSet.t end class free = object (m : 'test) inherit map as super val level : int = 0 val mutable state_ : t = empty val count = ref Javascript.IdentMap.empty method state = state_ method get_count = !count method get_free = IdentSet.diff m#state.use (IdentSet.union m#state.def_var m#state.def_local) method get_def = IdentSet.union m#state.def_var m#state.def_local method get_use = m#state.use method merge_info from = let free = from#get_free in state_ <- { state_ with use = IdentSet.union state_.use free } method merge_block_info from = let use = let state = from#state in IdentSet.diff state.use state.def_local in let def_var = from#state.def_var in state_ <- { use = IdentSet.union state_.use use ; def_var = IdentSet.union state_.def_var def_var ; def_local = state_.def_local } method use_var x = count := IdentMap.update x (function | None -> Some 1 | Some n -> Some (succ n)) !count; state_ <- { state_ with use = IdentSet.add x state_.use } method def_var x = count := IdentMap.update x (function | None -> Some 1 | Some n -> Some (succ n)) !count; state_ <- { state_ with def_var = IdentSet.add x state_.def_var } method def_local x = count := IdentMap.update x (function | None -> Some 1 | Some n -> Some (succ n)) !count; state_ <- { state_ with def_local = IdentSet.add x state_.def_local } method fun_decl (k, params, body, nid) = let tbody = ({<state_ = empty; level = succ level>} :> 'test) in let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in let params = tbody#formal_parameter_list params in tbody#record_block (Params params); m#merge_info tbody; k, params, body, nid method expression x = match x with | EVar v -> m#use_var v; x | EFun (ident, (k, params, body, nid)) -> let tbody = ({<state_ = empty; level = succ level>} :> 'test) in let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in let params = tbody#formal_parameter_list params in let ident = match ident with | Some i -> if IdentSet.mem i tbody#state.use then ( tbody#def_var i; ident) else None | None -> None in tbody#record_block (Params params); m#merge_info tbody; EFun (ident, (k, params, body, nid)) | EClass (ident_o, cl_decl) -> let same_level = level in let cbody = {<state_ = empty; level = same_level>} in let ident_o = Option.map ~f:(fun id -> cbody#def_var id; id) ident_o in let cl_decl = cbody#class_decl cl_decl in cbody#record_block Normal; m#merge_block_info cbody; EClass (ident_o, cl_decl) | _ -> super#expression x method record_block _ = () method variable_declaration k x = let ids = bound_idents_of_variable_declaration x in (match k with | Let | Const -> List.iter ids ~f:m#def_local | Var -> List.iter ids ~f:m#def_var); super#variable_declaration k x method block b = let same_level = level in let tbody = {<state_ = empty; level = same_level>} in let b = tbody#statements b in tbody#record_block Normal; m#merge_block_info tbody; b method class_element x = match x with | CEStaticBLock l -> let tbody = {<state_ = empty; level = level + 1>} in let l = tbody#statements l in tbody#record_block Normal; m#merge_info tbody; CEStaticBLock l | _ -> super#class_element x method statement x = match x with | Function_declaration (id, (k, params, body, nid)) -> let tbody = {<state_ = empty; level = succ level>} in let ids = bound_idents_of_params params in List.iter ids ~f:tbody#def_var; let body = tbody#function_body body in let params = tbody#formal_parameter_list params in tbody#record_block (Params params); m#def_local id; m#merge_info tbody; Function_declaration (id, (k, params, body, nid)) | Class_declaration (id, cl_decl) -> let same_level = level in let cbody = {<state_ = empty; level = same_level>} in let cl_decl = cbody#class_decl cl_decl in cbody#record_block Normal; m#merge_block_info cbody; m#def_local id; Class_declaration (id, cl_decl) | Block b -> Block (m#block b) | For_statement (Right (((Const | Let) as k), l), e1, e2, (st, loc)) -> let same_level = level in let m' = {<state_ = empty; level = same_level>} in let l = List.map ~f:(m'#variable_declaration k) l in let e1 = Option.map ~f:m'#expression e1 in let e2 = Option.map ~f:m'#expression e2 in let st = m'#statement st in m'#record_block Normal; m#merge_block_info m'; For_statement (Right (k, l), e1, e2, (st, m#loc loc)) | ForIn_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let same_level = level in let m' = {<state_ = empty; level = same_level>} in let l = m'#for_binding k l in let e2 = m'#expression e2 in let st = m'#statement st in m'#record_block Normal; m#merge_block_info m'; ForIn_statement (Right (k, l), e2, (st, m#loc loc)) | ForOf_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let same_level = level in let m' = {<state_ = empty; level = same_level>} in let l = m'#for_binding k l in let e2 = m'#expression e2 in let st = m'#statement st in m'#record_block Normal; m#merge_block_info m'; ForOf_statement (Right (k, l), e2, (st, m#loc loc)) | ForAwaitOf_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let same_level = level in let m' = {<state_ = empty; level = same_level>} in let l = m'#for_binding k l in let e2 = m'#expression e2 in let st = m'#statement st in m'#record_block Normal; m#merge_block_info m'; ForAwaitOf_statement (Right (k, l), e2, (st, m#loc loc)) | Switch_statement (e, l, def, l') -> let same_level = level in let m' = {<state_ = empty; level = same_level>} in let l = List.map l ~f:(fun (e, s) -> m'#switch_case e, m'#statements s) in let l' = List.map l' ~f:(fun (e, s) -> m'#switch_case e, m'#statements s) in let def = match def with | None -> None | Some l -> Some (m'#statements l) in let e = m#expression e in m'#record_block Normal; m#merge_block_info m'; Switch_statement (e, l, def, l') | Try_statement (b, w, f) -> let same_level = level in let b = m#block b in let w = match w with | None -> None | Some (None, b) -> Some (None, m#block b) | Some (Some id, block) -> let tw = {<state_ = empty; level = same_level>} in let block = tw#statements block in tw#record_block (Catch id); (* special merge here *) (* we need to propagate both def and use .. *) (* .. except the use of 'id' since its scope is limited to 'block' *) let ids = bound_idents_of_binding (fst id) in let clean set = List.fold_left ids ~init:set ~f:(fun set id -> IdentSet.remove id set) in let def_var = tw#state.def_var in let use = clean (IdentSet.diff tw#state.use tw#state.def_local) in state_ <- { use = IdentSet.union state_.use use ; def_var = IdentSet.union state_.def_var def_var ; def_local = state_.def_local }; Some (Some id, block) in let f = match f with | None -> None | Some f -> Some (m#block f) in Try_statement (b, w, f) | Import ({ from = _; kind }, _) -> (match kind with | Namespace (iopt, i) -> Option.iter ~f:m#def_local iopt; m#def_local i | Named (iopt, l) -> Option.iter ~f:m#def_local iopt; List.iter ~f:(fun (_, id) -> m#def_local id) l | Default import_default -> m#def_local import_default | SideEffect -> ()); super#statement x | _ -> super#statement x method for_binding k x = (match x with | BindingIdent x -> ( match k with | Let | Const -> m#def_local x | Var -> m#def_var x) | BindingPattern x -> ( let ids = bound_idents_of_pattern x in match k with | Let | Const -> List.iter ids ~f:m#def_local | Var -> List.iter ids ~f:m#def_var)); super#for_binding k x end type scope = | Module | Lexical_block | Fun_block of ident option class rename_variable ~esm = let declared scope params body = let declared_names = ref StringSet.empty in let decl_var x = match x with | S { name = Utf8 name; _ } -> declared_names := StringSet.add name !declared_names | _ -> () in (match scope with | Module -> () | Lexical_block -> () | Fun_block None -> () | Fun_block (Some x) -> decl_var x); List.iter params ~f:(fun x -> decl_var x); (object (self) val depth = 0 inherit iter as super method expression _ = () method fun_decl _ = () method class_decl _ = () method statement x = match scope, x with | (Fun_block _ | Module), Function_declaration (id, fd) -> decl_var id; self#fun_decl fd | Lexical_block, Function_declaration (_, fd) -> self#fun_decl fd | (Fun_block _ | Module), Class_declaration (id, cl_decl) -> decl_var id; self#class_decl cl_decl | Lexical_block, Class_declaration (_, cl_decl) -> self#class_decl cl_decl | _, For_statement (Right (((Const | Let) as k), l), _e1, _e2, (st, _loc)) -> let m = {<depth = depth + 1>} in List.iter ~f:(m#variable_declaration k) l; m#statement st | _, ForOf_statement (Right (((Const | Let) as k), l), _e2, (st, _loc)) -> let m = {<depth = depth + 1>} in m#for_binding k l; m#statement st | _, ForAwaitOf_statement (Right (((Const | Let) as k), l), _e2, (st, _loc)) -> let m = {<depth = depth + 1>} in m#for_binding k l; m#statement st | _, ForIn_statement (Right (((Const | Let) as k), l), _e2, (st, _loc)) -> let m = {<depth = depth + 1>} in m#for_binding k l; m#statement st | _, Switch_statement (_, l, def, l') -> let m = {<depth = depth + 1>} in List.iter l ~f:(fun (_, s) -> m#statements s); Option.iter def ~f:(fun l -> m#statements l); List.iter l' ~f:(fun (_, s) -> m#statements s) | _, Import ({ kind; from = _ }, _loc) -> ( match kind with | Namespace (iopt, i) -> Option.iter ~f:decl_var iopt; decl_var i | Named (iopt, l) -> Option.iter ~f:decl_var iopt; List.iter ~f:(fun (_, id) -> decl_var id) l | Default import_default -> decl_var import_default | SideEffect -> ()) | (Fun_block _ | Lexical_block | Module), _ -> super#statement x method export e = match e with | ExportVar (_k, _l) -> () | ExportFun (_id, _f) -> () | ExportClass (_id, _f) -> () | ExportNames l -> List.iter ~f:(fun (id, _) -> self#ident id) l | ExportDefaultFun (Some id, decl) -> self#statement (Function_declaration (id, decl)) | ExportDefaultClass (Some id, decl) -> self#statement (Class_declaration (id, decl)) | ExportDefaultFun (None, decl) -> self#fun_decl decl | ExportDefaultClass (None, decl) -> self#class_decl decl | ExportDefaultExpression e -> self#expression e | ExportFrom { from = _; kind = _ } -> () | CoverExportFrom _ -> () method variable_declaration k l = if match scope, k with | (Lexical_block | Fun_block _ | Module), (Let | Const) -> depth = 0 | Lexical_block, Var -> false | (Fun_block _ | Module), Var -> true then let ids = bound_idents_of_variable_declaration l in List.iter ids ~f:decl_var method block l = let m = {<depth = depth + 1>} in m#statements l method for_binding k p = if match scope, k with | (Lexical_block | Fun_block _ | Module), (Let | Const) -> depth = 0 | Lexical_block, Var -> false | (Fun_block _ | Module), Var -> true then match p with | BindingIdent i -> decl_var i | BindingPattern p -> let ids = bound_idents_of_pattern p in List.iter ids ~f:decl_var end) #statements body; !declared_names in object (m) inherit map as super val subst = StringMap.empty val decl = StringSet.empty val labels = StringMap.empty method update_state scope params iter_body = let declared_names = declared scope params iter_body in {<subst = StringSet.fold (fun name subst -> StringMap.add name (Code.Var.fresh_n name) subst) declared_names subst ; decl = declared_names>} method ident x = match x with | V _ -> x | S { name = Utf8 name; _ } -> ( try V (StringMap.find name subst) with Not_found -> x) method class_element x = match x with | CEStaticBLock l -> let m' = m#update_state (Fun_block None) [] l in CEStaticBLock (m'#statements l) | _ -> super#class_element x method fun_decl (k, params, body, nid) = let ids = bound_idents_of_params params in let m' = m#update_state (Fun_block None) ids body in k, m'#formal_parameter_list params, m'#function_body body, m#loc nid method program p = if esm then let m' = m#update_state Module [] p in m'#statements p else let m' = m#update_state Lexical_block [] p in m'#statements p method binding_property x = match x with | Prop_ident (Prop_and_ident (S { name = Utf8 name' as name; _ } as ident), e) when StringMap.mem name' subst -> let x = Prop_binding (PNI name, (BindingIdent ident, e)) in super#binding_property x | x -> super#binding_property x method expression e = match e with | EFun (ident, (k, params, body, nid)) -> let ids = bound_idents_of_params params in let m' = m#update_state (Fun_block ident) ids body in EFun ( Option.map ident ~f:m'#ident , (k, m'#formal_parameter_list params, m'#function_body body, m#loc nid) ) | EClass (Some id, cl_decl) -> let m' = m#update_state Lexical_block [ id ] [] in EClass (Some (m'#ident id), m'#class_decl cl_decl) | EAssignTarget (ObjectTarget l) -> let l = List.map l ~f:(function | TargetPropertyId (Prop_and_ident (S { name = Utf8 name' as name; _ } as ident), rhs) when StringMap.mem name' subst -> TargetProperty (PNI name, EVar ident, rhs) | b -> b) in super#expression (EAssignTarget (ObjectTarget l)) | _ -> super#expression e method statement s = match s with | Labelled_statement (l, (s, loc)) -> let l, m = match l with | L _ -> l, m | S (Utf8 u) -> let l = Label.fresh () in let m = {<labels = StringMap.add u l labels>} in l, m in Labelled_statement (l, (m#statement s, loc)) | Break_statement (Some l) -> ( match l with | L _ -> s | S (Utf8 l) -> ( match StringMap.find_opt l labels with | None -> s | Some l -> Break_statement (Some l))) | Continue_statement (Some l) -> ( match l with | L _ -> s | S (Utf8 l) -> ( match StringMap.find_opt l labels with | None -> s | Some l -> Continue_statement (Some l))) | Function_declaration (id, (k, params, body, nid)) -> let ids = bound_idents_of_params params in let m' = m#update_state (Fun_block None) ids body in Function_declaration ( m#ident id , (k, m'#formal_parameter_list params, m'#function_body body, m#loc nid) ) | For_statement (Right (((Const | Let) as k), l), e1, e2, (st, loc)) -> let ids = List.concat_map ~f:bound_idents_of_variable_declaration l in let m' = m#update_state Lexical_block ids [] in For_statement ( Right (k, List.map ~f:(m'#variable_declaration k) l) , Option.map ~f:m'#expression e1 , Option.map ~f:m'#expression e2 , (m'#statement st, m'#loc loc) ) | ForOf_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let ids = bound_idents_of_binding l in let m' = m#update_state Lexical_block ids [] in ForOf_statement ( Right (k, m'#for_binding k l) , m'#expression e2 , (m'#statement st, m'#loc loc) ) | ForAwaitOf_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let ids = bound_idents_of_binding l in let m' = m#update_state Lexical_block ids [] in ForAwaitOf_statement ( Right (k, m'#for_binding k l) , m'#expression e2 , (m'#statement st, m'#loc loc) ) | ForIn_statement (Right (((Const | Let) as k), l), e2, (st, loc)) -> let ids = bound_idents_of_binding l in let m' = m#update_state Lexical_block ids [] in ForIn_statement ( Right (k, m'#for_binding k l) , m'#expression e2 , (m'#statement st, m'#loc loc) ) | Block l -> let m' = m#update_state Lexical_block [] l in Block (m'#statements l) | Try_statement (block, catch, final) -> let block = let m' = m#update_state Lexical_block [] block in m'#statements block in let final = match final with | None -> None | Some final -> let m' = m#update_state Lexical_block [] final in Some (m'#statements final) in let catch = match catch with | None -> None | Some (i, catch) -> let i, l = match i with | None -> None, [] | Some ((pat, _) as p) -> let ids = bound_idents_of_binding pat in let l = List.filter ids ~f:(function | S { name = Utf8 name; _ } -> not (StringSet.mem name decl) | V _ -> false) in Some p, l in let m' = m#update_state Lexical_block l catch in let i = match i with | None -> None | Some i -> ( match m'#formal_parameter_list (list [ i ]) with | { list = [ i ]; rest = None } -> Some i | _ -> assert false) in Some (i, m'#statements catch) in Try_statement (block, catch, final) | Switch_statement (e, l, def, l') -> let all = let r = ref [] in Option.iter def ~f:(fun l -> r := List.rev_append l !r); List.iter l ~f:(fun (_, s) -> r := List.rev_append s !r); List.iter l' ~f:(fun (_, s) -> r := List.rev_append s !r); !r in let m' = m#update_state Lexical_block [] all in Switch_statement ( m#expression e , List.map l ~f:(fun (e, s) -> m'#switch_case e, m'#statements s) , (match def with | None -> None | Some l -> Some (m'#statements l)) , List.map l' ~f:(fun (e, s) -> m'#switch_case e, m'#statements s) ) | _ -> super#statement s end class compact_vardecl = let expr_eq id e = EBin (Eq, EVar id, e) in object (m) inherit map as super val mutable insert_ = IdentSet.empty method private var x = insert_ <- IdentSet.add x insert_ method fun_decl (k, params, body, nid) = let m' = {<insert_ = IdentSet.empty>} in let params = m'#formal_parameter_list params in let body = m'#function_body body in let body = m'#pack body in k, params, body, m#loc nid method private split x = let rec loop = function | ESeq (e1, e2) -> loop e1 @ loop e2 | e -> [ e ] in loop x method pack rest = let all = insert_ in let may_flush rem vars s instr = if List.is_empty vars then rem, [], s :: instr else rem, [], s :: (Variable_statement (Var, List.rev vars), N) :: instr in let rem, vars, instr = List.fold_left rest ~init:(all, [], []) ~f:(fun (rem, vars, instr) (s, loc) -> match s with | Expression_statement e -> let l = m#split e in List.fold_left l ~init:(rem, vars, instr) ~f:(fun (rem, vars, instr) e -> match e with | EBin (Eq, EVar id, exp) when IdentSet.mem id rem -> ( IdentSet.remove id rem , DeclIdent (id, Some (exp, N)) :: vars , instr ) | x -> may_flush rem vars (Expression_statement x, N) instr) | Function_declaration _ as x -> rem, vars, (x, loc) :: instr | _ as s -> may_flush rem vars (s, loc) instr) in let instr = match vars with | [] -> List.rev instr | d -> let d = Variable_statement (Var, List.rev d) in List.rev ((d, N) :: instr) in let l = IdentSet.fold (fun x acc -> DeclIdent (x, None) :: acc) rem [] in match l, instr with | [], _ -> instr | l, (Variable_statement (Var, l'), loc) :: rest -> (Variable_statement (Var, List.rev_append l l'), loc) :: rest | l, _ -> (Variable_statement (Var, l), N) :: instr method statements s = let s = super#statements s in List.concat_map s ~f:(fun (s, loc) -> match s with | Variable_statement (Var, l) -> List.filter_map l ~f:(function | DeclIdent (x, Some (init, loc)) -> m#var x; Some (Expression_statement (expr_eq x init), loc) | DeclIdent (x, None) -> m#var x; None | DeclPattern _ as x -> Some (Variable_statement (Var, [ x ]), loc)) | s -> [ s, loc ]) method program p = let p = super#program p in let body = m#pack p in body end (* - Group variable_statement together *) (* - Remove unnecessary block *) class clean = object (_m) inherit map as super method statements l = let l = super#statements l in List.filter l ~f:(function | (Empty_statement | Expression_statement (EVar _)), _ -> false | _ -> true) |> List.group ~f:(fun (x, _) (prev, _) -> match prev, x with | Variable_statement (k1, _), Variable_statement (k2, _) when Poly.(k1 = k2) -> true | _, _ -> false) |> List.map ~f:(function | (Variable_statement (k1, _), _) :: _ as l -> let loc = List.find_map l ~f:(fun (_, loc) -> match loc with | N | U -> None | Pi _ -> Some loc) |> function | None -> N | Some x -> x in ( Variable_statement ( k1 , List.concat_map l ~f:(function | Variable_statement (_, l), _ -> l | _ -> assert false) ) , loc ) | [ x ] -> x | [] | _ :: _ :: _ -> assert false) method statement s = let s = super#statement s in let b = function | Block [], loc -> Empty_statement, loc | Block [ x ], _ -> x | b -> b in let bopt = function | Some (Block [], _) -> None | Some (Block [ x ], _) -> Some x | Some b -> Some b | None -> None in match s with | If_statement (if', then', else') -> If_statement (if', b then', bopt else') | Do_while_statement (do', while') -> Do_while_statement (b do', while') | While_statement (cond, st) -> While_statement (cond, b st) | For_statement (p1, p2, p3, st) -> For_statement (p1, p2, p3, b st) | ForIn_statement (param, e, st) -> ForIn_statement (param, e, b st) | ForOf_statement (param, e, st) -> ForOf_statement (param, e, b st) | ForAwaitOf_statement (param, e, st) -> ForAwaitOf_statement (param, e, b st) | Switch_statement (e, l, Some [], []) -> Switch_statement (e, l, None, []) | s -> s end let opt_cons b l = match b with | Some b -> b :: l | None -> l let use_fun_context l = let exception True in try (object inherit iter as super method fun_decl _ = () method ident x = match x with (* An ArrowFunction does not define local bindings for arguments, super, this, or new.target. *) | S { name = Utf8 ("this" | "arguments" | "super" | "new" (* new.target *)); _ } -> raise True | _ -> () method expression x = match x with | EArrow (_, _, ANo_fun_context) -> () | EArrow (_, _, AUse_parent_fun_context) -> raise True | EArrow (fun_decl, _, AUnknown) -> super#fun_decl fun_decl | _ -> super#expression x end) #statements l; false with True -> true (* - Split variable_statement *) (* - rewrite assign_op *) (* - rewrite function_expression into function_declaration *) (* - if simplification *) (* - arithmetic simplification *) class simpl = object (m) inherit map as super method expression e = let e = super#expression e in let is_zero x = match Num.to_string x with | "0" | "0." -> true | _ -> false in match e with | EBin (Plus, e1, e2) -> ( match e2, e1 with | ENum n, _ when Num.is_neg n -> EBin (Minus, e1, ENum (Num.neg n)) | _, ENum n when Num.is_neg n -> EBin (Minus, e2, ENum (Num.neg n)) | ENum zero, (ENum _ as x) when is_zero zero -> x | (ENum _ as x), ENum zero when is_zero zero -> x | _ -> e) | EBin (Minus, e1, e2) -> ( match e2, e1 with | ENum n, _ when Num.is_neg n -> EBin (Plus, e1, ENum (Num.neg n)) | (ENum _ as x), ENum zero when is_zero zero -> x | _ -> e) | EFun (None, (({ generator = false; async = true | false }, _, body, _) as fun_decl)) when Config.Flag.es6 () && not (use_fun_context body) -> let consise = match body with | [ (Return_statement _, _) ] -> true | _ -> false in EArrow (fun_decl, consise, ANo_fun_context) | EArrow (((_, _, body, _) as fun_decl), consise, AUnknown) -> if use_fun_context body then EArrow (fun_decl, consise, AUse_parent_fun_context) else EArrow (fun_decl, consise, ANo_fun_context) | e -> e method statement s = let s = super#statement s in match s with | Block [ x ] -> fst x | _ -> s method program p = m#statements_top (m#statements p) method function_body b = m#statements_top (m#statements b) method private statements_top l = (* In strict mode, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode. *) List.map l ~f:(function s, loc -> (match s with | Variable_statement ((Var | Let | Const), [ DeclIdent (addr, Some (EFun (None, decl), loc)) ]) -> Function_declaration (addr, decl), loc | Variable_statement ((Var | Let | Const), ([] | _ :: _ :: _)) -> assert false | s -> s, loc)) method statements s = let s = super#statements s in List.fold_right s ~init:[] ~f:(fun (st, loc) rem -> match st with (* if (1) e1 ... --> e1 *) | If_statement (ENum n, iftrue, _) when Num.is_one n -> iftrue :: rem (* if (0) e1 else e2 --> e2 *) | If_statement (ENum n, _, iffalse) when Num.is_zero n -> opt_cons iffalse rem (* if (e1) return e2 else return e3 --> return e1 ? e2 : e3 *) | If_statement ( cond , (Return_statement (Some e1, _), _) , Some (Return_statement (Some e2, _), _) ) -> ( Return_statement ( Some (ECond (cond, e1, e2)) , U (*TODO: it would be better to use the location of the end of the function, but we can't easily get it. *) ) , loc ) :: rem (* if (e1) v1 = e2 else v1 = e3 --> v1 = e1 ? e2 : e3 *) | If_statement ( cond , (Expression_statement (EBin (Eq, v1, e1)), _) , Some (Expression_statement (EBin (Eq, v2, e2)), _) ) when Poly.(v1 = v2) -> (Expression_statement (EBin (Eq, v1, ECond (cond, e1, e2))), loc) :: rem (* The following optimizations cause the generated JS to compress less. (* if (e1) e2 else e3 --> e1 ? e2 : e3 *) | If_statement (e1, (Expression_statement e2, _), Some (Expression_statement e3, _)) -> (Expression_statement (ECond (e1, e2, e3)), loc) :: rem (* if (!e1) e2 --> e1 || e2 *) | If_statement (EUn (Not, e1), (Expression_statement e2, _), None) -> (Expression_statement (EBin (Or, e1, e2)), loc) :: rem (* if (e1) e2 --> e1 && e2 *) | If_statement (e1, (Expression_statement e2, _), None) -> (Expression_statement (EBin (And, e1, e2)), loc) :: rem *) | Variable_statement (((Var | Let | Const) as k), l1) -> let x = List.map l1 ~f:(fun d -> Variable_statement (k, [ d ]), loc) in x @ rem | _ -> (st, loc) :: rem) end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>