1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153
|
/*
* QuickJS C library
*
* Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#if defined(_WIN32)
#include <windows.h>
#include <conio.h>
#include <utime.h>
#else
#include <dlfcn.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#if defined(__FreeBSD__)
extern char **environ;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
typedef sig_t sighandler_t;
#endif
#if defined(__APPLE__)
#if !defined(environ)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#endif
#endif /* __APPLE__ */
#endif
/* enable the os.Worker API. It relies on POSIX threads */
#define USE_WORKER
#ifdef USE_WORKER
#include <pthread.h>
#include <stdatomic.h>
#endif
#include "cutils.h"
#include "list.h"
#include "quickjs-libc.h"
#if !defined(PATH_MAX)
#define PATH_MAX 4096
#endif
/* TODO:
- add socket calls
*/
typedef struct {
struct list_head link;
int fd;
JSValue rw_func[2];
} JSOSRWHandler;
typedef struct {
struct list_head link;
int sig_num;
JSValue func;
} JSOSSignalHandler;
typedef struct {
struct list_head link;
int timer_id;
int64_t timeout;
JSValue func;
} JSOSTimer;
typedef struct {
struct list_head link;
uint8_t *data;
size_t data_len;
/* list of SharedArrayBuffers, necessary to free the message */
uint8_t **sab_tab;
size_t sab_tab_len;
} JSWorkerMessage;
typedef struct JSWaker {
#ifdef _WIN32
HANDLE handle;
#else
int read_fd;
int write_fd;
#endif
} JSWaker;
typedef struct {
int ref_count;
#ifdef USE_WORKER
pthread_mutex_t mutex;
#endif
struct list_head msg_queue; /* list of JSWorkerMessage.link */
JSWaker waker;
} JSWorkerMessagePipe;
typedef struct {
struct list_head link;
JSWorkerMessagePipe *recv_pipe;
JSValue on_message_func;
} JSWorkerMessageHandler;
typedef struct JSThreadState {
struct list_head os_rw_handlers; /* list of JSOSRWHandler.link */
struct list_head os_signal_handlers; /* list JSOSSignalHandler.link */
struct list_head os_timers; /* list of JSOSTimer.link */
struct list_head port_list; /* list of JSWorkerMessageHandler.link */
int eval_script_recurse; /* only used in the main thread */
int next_timer_id; /* for setTimeout() */
/* not used in the main thread */
JSWorkerMessagePipe *recv_pipe, *send_pipe;
} JSThreadState;
static uint64_t os_pending_signals;
static int (*os_poll_func)(JSContext *ctx);
static void js_std_dbuf_init(JSContext *ctx, DynBuf *s)
{
dbuf_init2(s, JS_GetRuntime(ctx), (DynBufReallocFunc *)js_realloc_rt);
}
static BOOL my_isdigit(int c)
{
return (c >= '0' && c <= '9');
}
static JSValue js_printf_internal(JSContext *ctx,
int argc, JSValueConst *argv, FILE *fp)
{
char fmtbuf[32];
uint8_t cbuf[UTF8_CHAR_LEN_MAX+1];
JSValue res;
DynBuf dbuf;
const char *fmt_str = NULL;
const uint8_t *fmt, *fmt_end;
const uint8_t *p;
char *q;
int i, c, len, mod;
size_t fmt_len;
int32_t int32_arg;
int64_t int64_arg;
double double_arg;
const char *string_arg;
/* Use indirect call to dbuf_printf to prevent gcc warning */
int (*dbuf_printf_fun)(DynBuf *s, const char *fmt, ...) = (void*)dbuf_printf;
js_std_dbuf_init(ctx, &dbuf);
if (argc > 0) {
fmt_str = JS_ToCStringLen(ctx, &fmt_len, argv[0]);
if (!fmt_str)
goto fail;
i = 1;
fmt = (const uint8_t *)fmt_str;
fmt_end = fmt + fmt_len;
while (fmt < fmt_end) {
for (p = fmt; fmt < fmt_end && *fmt != '%'; fmt++)
continue;
dbuf_put(&dbuf, p, fmt - p);
if (fmt >= fmt_end)
break;
q = fmtbuf;
*q++ = *fmt++; /* copy '%' */
/* flags */
for(;;) {
c = *fmt;
if (c == '0' || c == '#' || c == '+' || c == '-' || c == ' ' ||
c == '\'') {
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = c;
fmt++;
} else {
break;
}
}
/* width */
if (*fmt == '*') {
if (i >= argc)
goto missing;
if (JS_ToInt32(ctx, &int32_arg, argv[i++]))
goto fail;
q += snprintf(q, fmtbuf + sizeof(fmtbuf) - q, "%d", int32_arg);
fmt++;
} else {
while (my_isdigit(*fmt)) {
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = *fmt++;
}
}
if (*fmt == '.') {
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = *fmt++;
if (*fmt == '*') {
if (i >= argc)
goto missing;
if (JS_ToInt32(ctx, &int32_arg, argv[i++]))
goto fail;
q += snprintf(q, fmtbuf + sizeof(fmtbuf) - q, "%d", int32_arg);
fmt++;
} else {
while (my_isdigit(*fmt)) {
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = *fmt++;
}
}
}
/* we only support the "l" modifier for 64 bit numbers */
mod = ' ';
if (*fmt == 'l') {
mod = *fmt++;
}
/* type */
c = *fmt++;
if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid;
*q++ = c;
*q = '\0';
switch (c) {
case 'c':
if (i >= argc)
goto missing;
if (JS_IsString(argv[i])) {
string_arg = JS_ToCString(ctx, argv[i++]);
if (!string_arg)
goto fail;
int32_arg = qjs_unicode_from_utf8((const uint8_t *)string_arg, UTF8_CHAR_LEN_MAX, &p);
JS_FreeCString(ctx, string_arg);
} else {
if (JS_ToInt32(ctx, &int32_arg, argv[i++]))
goto fail;
}
/* handle utf-8 encoding explicitly */
if ((unsigned)int32_arg > 0x10FFFF)
int32_arg = 0xFFFD;
/* ignore conversion flags, width and precision */
len = qjs_unicode_to_utf8(cbuf, int32_arg);
dbuf_put(&dbuf, cbuf, len);
break;
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
if (i >= argc)
goto missing;
if (JS_ToInt64Ext(ctx, &int64_arg, argv[i++]))
goto fail;
if (mod == 'l') {
/* 64 bit number */
#if defined(_WIN32)
if (q >= fmtbuf + sizeof(fmtbuf) - 3)
goto invalid;
q[2] = q[-1];
q[-1] = 'I';
q[0] = '6';
q[1] = '4';
q[3] = '\0';
dbuf_printf_fun(&dbuf, fmtbuf, (int64_t)int64_arg);
#else
if (q >= fmtbuf + sizeof(fmtbuf) - 2)
goto invalid;
q[1] = q[-1];
q[-1] = q[0] = 'l';
q[2] = '\0';
dbuf_printf_fun(&dbuf, fmtbuf, (long long)int64_arg);
#endif
} else {
dbuf_printf_fun(&dbuf, fmtbuf, (int)int64_arg);
}
break;
case 's':
if (i >= argc)
goto missing;
/* XXX: handle strings containing null characters */
string_arg = JS_ToCString(ctx, argv[i++]);
if (!string_arg)
goto fail;
dbuf_printf_fun(&dbuf, fmtbuf, string_arg);
JS_FreeCString(ctx, string_arg);
break;
case 'e':
case 'f':
case 'g':
case 'a':
case 'E':
case 'F':
case 'G':
case 'A':
if (i >= argc)
goto missing;
if (JS_ToFloat64(ctx, &double_arg, argv[i++]))
goto fail;
dbuf_printf_fun(&dbuf, fmtbuf, double_arg);
break;
case '%':
dbuf_putc(&dbuf, '%');
break;
default:
/* XXX: should support an extension mechanism */
invalid:
JS_ThrowTypeError(ctx, "invalid conversion specifier in format string");
goto fail;
missing:
JS_ThrowReferenceError(ctx, "missing argument for conversion specifier");
goto fail;
}
}
JS_FreeCString(ctx, fmt_str);
}
if (dbuf.error) {
res = JS_ThrowOutOfMemory(ctx);
} else {
if (fp) {
len = fwrite(dbuf.buf, 1, dbuf.size, fp);
res = JS_NewInt32(ctx, len);
} else {
res = JS_NewStringLen(ctx, (char *)dbuf.buf, dbuf.size);
}
}
dbuf_free(&dbuf);
return res;
fail:
JS_FreeCString(ctx, fmt_str);
dbuf_free(&dbuf);
return JS_EXCEPTION;
}
uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
{
FILE *f;
uint8_t *buf;
size_t buf_len;
long lret;
f = fopen(filename, "rb");
if (!f)
return NULL;
if (fseek(f, 0, SEEK_END) < 0)
goto fail;
lret = ftell(f);
if (lret < 0)
goto fail;
/* XXX: on Linux, ftell() return LONG_MAX for directories */
if (lret == LONG_MAX) {
errno = EISDIR;
goto fail;
}
buf_len = lret;
if (fseek(f, 0, SEEK_SET) < 0)
goto fail;
if (ctx)
buf = js_malloc(ctx, buf_len + 1);
else
buf = malloc(buf_len + 1);
if (!buf)
goto fail;
if (fread(buf, 1, buf_len, f) != buf_len) {
errno = EIO;
if (ctx)
js_free(ctx, buf);
else
free(buf);
fail:
fclose(f);
return NULL;
}
buf[buf_len] = '\0';
fclose(f);
*pbuf_len = buf_len;
return buf;
}
/* load and evaluate a file */
static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
uint8_t *buf;
const char *filename;
JSValue ret;
size_t buf_len;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
buf = js_load_file(ctx, &buf_len, filename);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load '%s'", filename);
JS_FreeCString(ctx, filename);
return JS_EXCEPTION;
}
ret = JS_Eval(ctx, (char *)buf, buf_len, filename,
JS_EVAL_TYPE_GLOBAL);
js_free(ctx, buf);
JS_FreeCString(ctx, filename);
return ret;
}
/* load a file as a UTF-8 encoded string */
static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
uint8_t *buf;
const char *filename;
JSValue ret;
size_t buf_len;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
buf = js_load_file(ctx, &buf_len, filename);
JS_FreeCString(ctx, filename);
if (!buf)
return JS_NULL;
ret = JS_NewStringLen(ctx, (char *)buf, buf_len);
js_free(ctx, buf);
return ret;
}
typedef JSModuleDef *(JSInitModuleFunc)(JSContext *ctx,
const char *module_name);
#if defined(_WIN32)
static JSModuleDef *js_module_loader_so(JSContext *ctx,
const char *module_name)
{
JS_ThrowReferenceError(ctx, "shared library modules are not supported yet");
return NULL;
}
#else
static JSModuleDef *js_module_loader_so(JSContext *ctx,
const char *module_name)
{
JSModuleDef *m;
void *hd;
JSInitModuleFunc *init;
char *filename;
if (!strchr(module_name, '/')) {
/* must add a '/' so that the DLL is not searched in the
system library paths */
filename = js_malloc(ctx, strlen(module_name) + 2 + 1);
if (!filename)
return NULL;
strcpy(filename, "./");
strcpy(filename + 2, module_name);
} else {
filename = (char *)module_name;
}
/* C module */
hd = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
if (filename != module_name)
js_free(ctx, filename);
if (!hd) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s' as shared library",
module_name);
goto fail;
}
init = dlsym(hd, "js_init_module");
if (!init) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s': js_init_module not found",
module_name);
goto fail;
}
m = init(ctx, module_name);
if (!m) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s': initialization error",
module_name);
fail:
if (hd)
dlclose(hd);
return NULL;
}
return m;
}
#endif /* !_WIN32 */
int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
JS_BOOL use_realpath, JS_BOOL is_main)
{
JSModuleDef *m;
char buf[PATH_MAX + 16];
JSValue meta_obj;
JSAtom module_name_atom;
const char *module_name;
assert(JS_VALUE_GET_TAG(func_val) == JS_TAG_MODULE);
m = JS_VALUE_GET_PTR(func_val);
module_name_atom = JS_GetModuleName(ctx, m);
module_name = JS_AtomToCString(ctx, module_name_atom);
JS_FreeAtom(ctx, module_name_atom);
if (!module_name)
return -1;
if (!strchr(module_name, ':')) {
strcpy(buf, "file://");
#if !defined(_WIN32)
/* realpath() cannot be used with modules compiled with qjsc
because the corresponding module source code is not
necessarily present */
if (use_realpath) {
char *res = realpath(module_name, buf + strlen(buf));
if (!res) {
JS_ThrowTypeError(ctx, "realpath failure");
JS_FreeCString(ctx, module_name);
return -1;
}
} else
#endif
{
pstrcat(buf, sizeof(buf), module_name);
}
} else {
pstrcpy(buf, sizeof(buf), module_name);
}
JS_FreeCString(ctx, module_name);
meta_obj = JS_GetImportMeta(ctx, m);
if (JS_IsException(meta_obj))
return -1;
JS_DefinePropertyValueStr(ctx, meta_obj, "url",
JS_NewString(ctx, buf),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, meta_obj, "main",
JS_NewBool(ctx, is_main),
JS_PROP_C_W_E);
JS_FreeValue(ctx, meta_obj);
return 0;
}
JSModuleDef *js_module_loader(JSContext *ctx,
const char *module_name, void *opaque)
{
JSModuleDef *m;
if (has_suffix(module_name, ".so")) {
m = js_module_loader_so(ctx, module_name);
} else {
size_t buf_len;
uint8_t *buf;
JSValue func_val;
buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
module_name);
return NULL;
}
/* compile the module */
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
js_free(ctx, buf);
if (JS_IsException(func_val))
return NULL;
/* XXX: could propagate the exception */
js_module_set_import_meta(ctx, func_val, TRUE, FALSE);
/* the module is already referenced, so we must free it */
m = JS_VALUE_GET_PTR(func_val);
JS_FreeValue(ctx, func_val);
}
return m;
}
static JSValue js_std_exit(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int status;
if (JS_ToInt32(ctx, &status, argv[0]))
status = -1;
exit(status);
return JS_UNDEFINED;
}
static JSValue js_std_getenv(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *name, *str;
name = JS_ToCString(ctx, argv[0]);
if (!name)
return JS_EXCEPTION;
str = getenv(name);
JS_FreeCString(ctx, name);
if (!str)
return JS_UNDEFINED;
else
return JS_NewString(ctx, str);
}
#if defined(_WIN32)
static void setenv(const char *name, const char *value, int overwrite)
{
char *str;
size_t name_len, value_len;
name_len = strlen(name);
value_len = strlen(value);
str = malloc(name_len + 1 + value_len + 1);
memcpy(str, name, name_len);
str[name_len] = '=';
memcpy(str + name_len + 1, value, value_len);
str[name_len + 1 + value_len] = '\0';
_putenv(str);
free(str);
}
static void unsetenv(const char *name)
{
setenv(name, "", TRUE);
}
#endif /* _WIN32 */
static JSValue js_std_setenv(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *name, *value;
name = JS_ToCString(ctx, argv[0]);
if (!name)
return JS_EXCEPTION;
value = JS_ToCString(ctx, argv[1]);
if (!value) {
JS_FreeCString(ctx, name);
return JS_EXCEPTION;
}
setenv(name, value, TRUE);
JS_FreeCString(ctx, name);
JS_FreeCString(ctx, value);
return JS_UNDEFINED;
}
static JSValue js_std_unsetenv(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *name;
name = JS_ToCString(ctx, argv[0]);
if (!name)
return JS_EXCEPTION;
unsetenv(name);
JS_FreeCString(ctx, name);
return JS_UNDEFINED;
}
/* return an object containing the list of the available environment
variables. */
static JSValue js_std_getenviron(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
char **envp;
const char *name, *p, *value;
JSValue obj;
uint32_t idx;
size_t name_len;
JSAtom atom;
int ret;
obj = JS_NewObject(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
envp = environ;
for(idx = 0; envp[idx] != NULL; idx++) {
name = envp[idx];
p = strchr(name, '=');
name_len = p - name;
if (!p)
continue;
value = p + 1;
atom = JS_NewAtomLen(ctx, name, name_len);
if (atom == JS_ATOM_NULL)
goto fail;
ret = JS_DefinePropertyValue(ctx, obj, atom, JS_NewString(ctx, value),
JS_PROP_C_W_E);
JS_FreeAtom(ctx, atom);
if (ret < 0)
goto fail;
}
return obj;
fail:
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue js_std_gc(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JS_RunGC(JS_GetRuntime(ctx));
return JS_UNDEFINED;
}
static int interrupt_handler(JSRuntime *rt, void *opaque)
{
return (os_pending_signals >> SIGINT) & 1;
}
static int get_bool_option(JSContext *ctx, BOOL *pbool,
JSValueConst obj,
const char *option)
{
JSValue val;
val = JS_GetPropertyStr(ctx, obj, option);
if (JS_IsException(val))
return -1;
if (!JS_IsUndefined(val)) {
*pbool = JS_ToBool(ctx, val);
}
JS_FreeValue(ctx, val);
return 0;
}
static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
const char *str;
size_t len;
JSValue ret;
JSValueConst options_obj;
BOOL backtrace_barrier = FALSE;
BOOL is_async = FALSE;
int flags;
if (argc >= 2) {
options_obj = argv[1];
if (get_bool_option(ctx, &backtrace_barrier, options_obj,
"backtrace_barrier"))
return JS_EXCEPTION;
if (get_bool_option(ctx, &is_async, options_obj,
"async"))
return JS_EXCEPTION;
}
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str)
return JS_EXCEPTION;
if (!ts->recv_pipe && ++ts->eval_script_recurse == 1) {
/* install the interrupt handler */
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
}
flags = JS_EVAL_TYPE_GLOBAL;
if (backtrace_barrier)
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
if (is_async)
flags |= JS_EVAL_FLAG_ASYNC;
ret = JS_Eval(ctx, str, len, "<evalScript>", flags);
JS_FreeCString(ctx, str);
if (!ts->recv_pipe && --ts->eval_script_recurse == 0) {
/* remove the interrupt handler */
JS_SetInterruptHandler(JS_GetRuntime(ctx), NULL, NULL);
os_pending_signals &= ~((uint64_t)1 << SIGINT);
/* convert the uncatchable "interrupted" error into a normal error
so that it can be caught by the REPL */
if (JS_IsException(ret))
JS_ResetUncatchableError(ctx);
}
return ret;
}
static JSClassID js_std_file_class_id;
typedef struct {
FILE *f;
BOOL close_in_finalizer;
BOOL is_popen;
} JSSTDFile;
static void js_std_file_finalizer(JSRuntime *rt, JSValue val)
{
JSSTDFile *s = JS_GetOpaque(val, js_std_file_class_id);
if (s) {
if (s->f && s->close_in_finalizer) {
if (s->is_popen)
pclose(s->f);
else
fclose(s->f);
}
js_free_rt(rt, s);
}
}
static ssize_t js_get_errno(ssize_t ret)
{
if (ret == -1)
ret = -errno;
return ret;
}
static JSValue js_std_strerror(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int err;
if (JS_ToInt32(ctx, &err, argv[0]))
return JS_EXCEPTION;
return JS_NewString(ctx, strerror(err));
}
static JSValue js_std_parseExtJSON(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue obj;
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str)
return JS_EXCEPTION;
obj = JS_ParseJSON2(ctx, str, len, "<input>", JS_PARSE_JSON_EXT);
JS_FreeCString(ctx, str);
return obj;
}
static JSValue js_new_std_file(JSContext *ctx, FILE *f,
BOOL close_in_finalizer,
BOOL is_popen)
{
JSSTDFile *s;
JSValue obj;
obj = JS_NewObjectClass(ctx, js_std_file_class_id);
if (JS_IsException(obj))
return obj;
s = js_mallocz(ctx, sizeof(*s));
if (!s) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
s->close_in_finalizer = close_in_finalizer;
s->is_popen = is_popen;
s->f = f;
JS_SetOpaque(obj, s);
return obj;
}
static void js_set_error_object(JSContext *ctx, JSValue obj, int err)
{
if (!JS_IsUndefined(obj)) {
JS_SetPropertyStr(ctx, obj, "errno", JS_NewInt32(ctx, err));
}
}
static JSValue js_std_open(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *filename, *mode = NULL;
FILE *f;
int err;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
goto fail;
mode = JS_ToCString(ctx, argv[1]);
if (!mode)
goto fail;
if (mode[strspn(mode, "rwa+b")] != '\0') {
JS_ThrowTypeError(ctx, "invalid file mode");
goto fail;
}
f = fopen(filename, mode);
if (!f)
err = errno;
else
err = 0;
if (argc >= 3)
js_set_error_object(ctx, argv[2], err);
JS_FreeCString(ctx, filename);
JS_FreeCString(ctx, mode);
if (!f)
return JS_NULL;
return js_new_std_file(ctx, f, TRUE, FALSE);
fail:
JS_FreeCString(ctx, filename);
JS_FreeCString(ctx, mode);
return JS_EXCEPTION;
}
static JSValue js_std_popen(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *filename, *mode = NULL;
FILE *f;
int err;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
goto fail;
mode = JS_ToCString(ctx, argv[1]);
if (!mode)
goto fail;
if (mode[strspn(mode, "rw")] != '\0') {
JS_ThrowTypeError(ctx, "invalid file mode");
goto fail;
}
f = popen(filename, mode);
if (!f)
err = errno;
else
err = 0;
if (argc >= 3)
js_set_error_object(ctx, argv[2], err);
JS_FreeCString(ctx, filename);
JS_FreeCString(ctx, mode);
if (!f)
return JS_NULL;
return js_new_std_file(ctx, f, TRUE, TRUE);
fail:
JS_FreeCString(ctx, filename);
JS_FreeCString(ctx, mode);
return JS_EXCEPTION;
}
static JSValue js_std_fdopen(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *mode;
FILE *f;
int fd, err;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
mode = JS_ToCString(ctx, argv[1]);
if (!mode)
goto fail;
if (mode[strspn(mode, "rwa+")] != '\0') {
JS_ThrowTypeError(ctx, "invalid file mode");
goto fail;
}
f = fdopen(fd, mode);
if (!f)
err = errno;
else
err = 0;
if (argc >= 3)
js_set_error_object(ctx, argv[2], err);
JS_FreeCString(ctx, mode);
if (!f)
return JS_NULL;
return js_new_std_file(ctx, f, TRUE, FALSE);
fail:
JS_FreeCString(ctx, mode);
return JS_EXCEPTION;
}
static JSValue js_std_tmpfile(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f;
f = tmpfile();
if (argc >= 1)
js_set_error_object(ctx, argv[0], f ? 0 : errno);
if (!f)
return JS_NULL;
return js_new_std_file(ctx, f, TRUE, FALSE);
}
static JSValue js_std_sprintf(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return js_printf_internal(ctx, argc, argv, NULL);
}
static JSValue js_std_printf(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return js_printf_internal(ctx, argc, argv, stdout);
}
static FILE *js_std_file_get(JSContext *ctx, JSValueConst obj)
{
JSSTDFile *s = JS_GetOpaque2(ctx, obj, js_std_file_class_id);
if (!s)
return NULL;
if (!s->f) {
JS_ThrowTypeError(ctx, "invalid file handle");
return NULL;
}
return s->f;
}
static JSValue js_std_file_puts(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
FILE *f;
int i;
const char *str;
size_t len;
if (magic == 0) {
f = stdout;
} else {
f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
}
for(i = 0; i < argc; i++) {
str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str)
return JS_EXCEPTION;
fwrite(str, 1, len, f);
JS_FreeCString(ctx, str);
}
return JS_UNDEFINED;
}
static JSValue js_std_file_close(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSSTDFile *s = JS_GetOpaque2(ctx, this_val, js_std_file_class_id);
int err;
if (!s)
return JS_EXCEPTION;
if (!s->f)
return JS_ThrowTypeError(ctx, "invalid file handle");
if (s->is_popen)
err = js_get_errno(pclose(s->f));
else
err = js_get_errno(fclose(s->f));
s->f = NULL;
return JS_NewInt32(ctx, err);
}
static JSValue js_std_file_printf(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
return js_printf_internal(ctx, argc, argv, f);
}
static JSValue js_std_file_flush(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
fflush(f);
return JS_UNDEFINED;
}
static JSValue js_std_file_tell(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int is_bigint)
{
FILE *f = js_std_file_get(ctx, this_val);
int64_t pos;
if (!f)
return JS_EXCEPTION;
#if defined(__linux__) || defined(__GLIBC__)
pos = ftello(f);
#else
pos = ftell(f);
#endif
if (is_bigint)
return JS_NewBigInt64(ctx, pos);
else
return JS_NewInt64(ctx, pos);
}
static JSValue js_std_file_seek(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
int64_t pos;
int whence, ret;
if (!f)
return JS_EXCEPTION;
if (JS_ToInt64Ext(ctx, &pos, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &whence, argv[1]))
return JS_EXCEPTION;
#if defined(__linux__) || defined(__GLIBC__)
ret = fseeko(f, pos, whence);
#else
ret = fseek(f, pos, whence);
#endif
if (ret < 0)
ret = -errno;
return JS_NewInt32(ctx, ret);
}
static JSValue js_std_file_eof(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
return JS_NewBool(ctx, feof(f));
}
static JSValue js_std_file_error(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
return JS_NewBool(ctx, ferror(f));
}
static JSValue js_std_file_clearerr(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
clearerr(f);
return JS_UNDEFINED;
}
static JSValue js_std_file_fileno(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
return JS_NewInt32(ctx, fileno(f));
}
static JSValue js_std_file_read_write(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
FILE *f = js_std_file_get(ctx, this_val);
uint64_t pos, len;
size_t size, ret;
uint8_t *buf;
if (!f)
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[1]))
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &len, argv[2]))
return JS_EXCEPTION;
buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
if (!buf)
return JS_EXCEPTION;
if (pos + len > size)
return JS_ThrowRangeError(ctx, "read/write array buffer overflow");
if (magic)
ret = fwrite(buf + pos, 1, len, f);
else
ret = fread(buf + pos, 1, len, f);
return JS_NewInt64(ctx, ret);
}
/* XXX: could use less memory and go faster */
static JSValue js_std_file_getline(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
int c;
DynBuf dbuf;
JSValue obj;
if (!f)
return JS_EXCEPTION;
js_std_dbuf_init(ctx, &dbuf);
for(;;) {
c = fgetc(f);
if (c == EOF) {
if (dbuf.size == 0) {
/* EOF */
dbuf_free(&dbuf);
return JS_NULL;
} else {
break;
}
}
if (c == '\n')
break;
if (dbuf_putc(&dbuf, c)) {
dbuf_free(&dbuf);
return JS_ThrowOutOfMemory(ctx);
}
}
obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size);
dbuf_free(&dbuf);
return obj;
}
/* XXX: could use less memory and go faster */
static JSValue js_std_file_readAsString(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
int c;
DynBuf dbuf;
JSValue obj;
uint64_t max_size64;
size_t max_size;
JSValueConst max_size_val;
if (!f)
return JS_EXCEPTION;
if (argc >= 1)
max_size_val = argv[0];
else
max_size_val = JS_UNDEFINED;
max_size = (size_t)-1;
if (!JS_IsUndefined(max_size_val)) {
if (JS_ToIndex(ctx, &max_size64, max_size_val))
return JS_EXCEPTION;
if (max_size64 < max_size)
max_size = max_size64;
}
js_std_dbuf_init(ctx, &dbuf);
while (max_size != 0) {
c = fgetc(f);
if (c == EOF)
break;
if (dbuf_putc(&dbuf, c)) {
dbuf_free(&dbuf);
return JS_EXCEPTION;
}
max_size--;
}
obj = JS_NewStringLen(ctx, (const char *)dbuf.buf, dbuf.size);
dbuf_free(&dbuf);
return obj;
}
static JSValue js_std_file_getByte(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
if (!f)
return JS_EXCEPTION;
return JS_NewInt32(ctx, fgetc(f));
}
static JSValue js_std_file_putByte(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
FILE *f = js_std_file_get(ctx, this_val);
int c;
if (!f)
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &c, argv[0]))
return JS_EXCEPTION;
c = fputc(c, f);
return JS_NewInt32(ctx, c);
}
/* urlGet */
#define URL_GET_PROGRAM "curl -s -i --"
#define URL_GET_BUF_SIZE 4096
static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
DynBuf *dbuf)
{
int c;
char *p;
p = buf;
for(;;) {
c = fgetc(f);
if (c < 0)
return -1;
if ((p - buf) < buf_size - 1)
*p++ = c;
if (dbuf)
dbuf_putc(dbuf, c);
if (c == '\n')
break;
}
*p = '\0';
return 0;
}
static int http_get_status(const char *buf)
{
const char *p = buf;
while (*p != ' ' && *p != '\0')
p++;
if (*p != ' ')
return 0;
while (*p == ' ')
p++;
return atoi(p);
}
static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *url;
DynBuf cmd_buf;
DynBuf data_buf_s, *data_buf = &data_buf_s;
DynBuf header_buf_s, *header_buf = &header_buf_s;
char *buf;
size_t i, len;
int status;
JSValue response = JS_UNDEFINED, ret_obj;
JSValueConst options_obj;
FILE *f;
BOOL binary_flag, full_flag;
url = JS_ToCString(ctx, argv[0]);
if (!url)
return JS_EXCEPTION;
binary_flag = FALSE;
full_flag = FALSE;
if (argc >= 2) {
options_obj = argv[1];
if (get_bool_option(ctx, &binary_flag, options_obj, "binary"))
goto fail_obj;
if (get_bool_option(ctx, &full_flag, options_obj, "full")) {
fail_obj:
JS_FreeCString(ctx, url);
return JS_EXCEPTION;
}
}
js_std_dbuf_init(ctx, &cmd_buf);
dbuf_printf(&cmd_buf, "%s '", URL_GET_PROGRAM);
for(i = 0; url[i] != '\0'; i++) {
unsigned char c = url[i];
switch (c) {
case '\'':
/* shell single quoted string does not support \' */
dbuf_putstr(&cmd_buf, "'\\''");
break;
case '[': case ']': case '{': case '}': case '\\':
/* prevent interpretation by curl as range or set specification */
dbuf_putc(&cmd_buf, '\\');
/* FALLTHROUGH */
default:
dbuf_putc(&cmd_buf, c);
break;
}
}
JS_FreeCString(ctx, url);
dbuf_putstr(&cmd_buf, "'");
dbuf_putc(&cmd_buf, '\0');
if (dbuf_error(&cmd_buf)) {
dbuf_free(&cmd_buf);
return JS_EXCEPTION;
}
// printf("%s\n", (char *)cmd_buf.buf);
f = popen((char *)cmd_buf.buf, "r");
dbuf_free(&cmd_buf);
if (!f) {
return JS_ThrowTypeError(ctx, "could not start curl");
}
js_std_dbuf_init(ctx, data_buf);
js_std_dbuf_init(ctx, header_buf);
buf = js_malloc(ctx, URL_GET_BUF_SIZE);
if (!buf)
goto fail;
/* get the HTTP status */
if (http_get_header_line(f, buf, URL_GET_BUF_SIZE, NULL) < 0) {
status = 0;
goto bad_header;
}
status = http_get_status(buf);
if (!full_flag && !(status >= 200 && status <= 299)) {
goto bad_header;
}
/* wait until there is an empty line */
for(;;) {
if (http_get_header_line(f, buf, URL_GET_BUF_SIZE, header_buf) < 0) {
bad_header:
response = JS_NULL;
goto done;
}
if (!strcmp(buf, "\r\n"))
break;
}
if (dbuf_error(header_buf))
goto fail;
header_buf->size -= 2; /* remove the trailing CRLF */
/* download the data */
for(;;) {
len = fread(buf, 1, URL_GET_BUF_SIZE, f);
if (len == 0)
break;
dbuf_put(data_buf, (uint8_t *)buf, len);
}
if (dbuf_error(data_buf))
goto fail;
if (binary_flag) {
response = JS_NewArrayBufferCopy(ctx,
data_buf->buf, data_buf->size);
} else {
response = JS_NewStringLen(ctx, (char *)data_buf->buf, data_buf->size);
}
if (JS_IsException(response))
goto fail;
done:
js_free(ctx, buf);
buf = NULL;
pclose(f);
f = NULL;
dbuf_free(data_buf);
data_buf = NULL;
if (full_flag) {
ret_obj = JS_NewObject(ctx);
if (JS_IsException(ret_obj))
goto fail;
JS_DefinePropertyValueStr(ctx, ret_obj, "response",
response,
JS_PROP_C_W_E);
if (!JS_IsNull(response)) {
JS_DefinePropertyValueStr(ctx, ret_obj, "responseHeaders",
JS_NewStringLen(ctx, (char *)header_buf->buf,
header_buf->size),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, ret_obj, "status",
JS_NewInt32(ctx, status),
JS_PROP_C_W_E);
}
} else {
ret_obj = response;
}
dbuf_free(header_buf);
return ret_obj;
fail:
if (f)
pclose(f);
js_free(ctx, buf);
if (data_buf)
dbuf_free(data_buf);
if (header_buf)
dbuf_free(header_buf);
JS_FreeValue(ctx, response);
return JS_EXCEPTION;
}
static JSClassDef js_std_file_class = {
"FILE",
.finalizer = js_std_file_finalizer,
};
static const JSCFunctionListEntry js_std_error_props[] = {
/* various errno values */
#define DEF(x) JS_PROP_INT32_DEF(#x, x, JS_PROP_CONFIGURABLE )
DEF(EINVAL),
DEF(EIO),
DEF(EACCES),
DEF(EEXIST),
DEF(ENOSPC),
DEF(ENOSYS),
DEF(EBUSY),
DEF(ENOENT),
DEF(EPERM),
DEF(EPIPE),
DEF(EBADF),
#undef DEF
};
static const JSCFunctionListEntry js_std_funcs[] = {
JS_CFUNC_DEF("exit", 1, js_std_exit ),
JS_CFUNC_DEF("gc", 0, js_std_gc ),
JS_CFUNC_DEF("evalScript", 1, js_evalScript ),
JS_CFUNC_DEF("loadScript", 1, js_loadScript ),
JS_CFUNC_DEF("getenv", 1, js_std_getenv ),
JS_CFUNC_DEF("setenv", 1, js_std_setenv ),
JS_CFUNC_DEF("unsetenv", 1, js_std_unsetenv ),
JS_CFUNC_DEF("getenviron", 1, js_std_getenviron ),
JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ),
JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
JS_CFUNC_DEF("parseExtJSON", 1, js_std_parseExtJSON ),
/* FILE I/O */
JS_CFUNC_DEF("open", 2, js_std_open ),
JS_CFUNC_DEF("popen", 2, js_std_popen ),
JS_CFUNC_DEF("fdopen", 2, js_std_fdopen ),
JS_CFUNC_DEF("tmpfile", 0, js_std_tmpfile ),
JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 0 ),
JS_CFUNC_DEF("printf", 1, js_std_printf ),
JS_CFUNC_DEF("sprintf", 1, js_std_sprintf ),
JS_PROP_INT32_DEF("SEEK_SET", SEEK_SET, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("SEEK_CUR", SEEK_CUR, JS_PROP_CONFIGURABLE ),
JS_PROP_INT32_DEF("SEEK_END", SEEK_END, JS_PROP_CONFIGURABLE ),
JS_OBJECT_DEF("Error", js_std_error_props, countof(js_std_error_props), JS_PROP_CONFIGURABLE),
};
static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
JS_CFUNC_DEF("close", 0, js_std_file_close ),
JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 1 ),
JS_CFUNC_DEF("printf", 1, js_std_file_printf ),
JS_CFUNC_DEF("flush", 0, js_std_file_flush ),
JS_CFUNC_MAGIC_DEF("tell", 0, js_std_file_tell, 0 ),
JS_CFUNC_MAGIC_DEF("tello", 0, js_std_file_tell, 1 ),
JS_CFUNC_DEF("seek", 2, js_std_file_seek ),
JS_CFUNC_DEF("eof", 0, js_std_file_eof ),
JS_CFUNC_DEF("fileno", 0, js_std_file_fileno ),
JS_CFUNC_DEF("error", 0, js_std_file_error ),
JS_CFUNC_DEF("clearerr", 0, js_std_file_clearerr ),
JS_CFUNC_MAGIC_DEF("read", 3, js_std_file_read_write, 0 ),
JS_CFUNC_MAGIC_DEF("write", 3, js_std_file_read_write, 1 ),
JS_CFUNC_DEF("getline", 0, js_std_file_getline ),
JS_CFUNC_DEF("readAsString", 0, js_std_file_readAsString ),
JS_CFUNC_DEF("getByte", 0, js_std_file_getByte ),
JS_CFUNC_DEF("putByte", 1, js_std_file_putByte ),
/* setvbuf, ... */
};
static int js_std_init(JSContext *ctx, JSModuleDef *m)
{
JSValue proto;
/* FILE class */
/* the class ID is created once */
JS_NewClassID(&js_std_file_class_id);
/* the class is created once per runtime */
JS_NewClass(JS_GetRuntime(ctx), js_std_file_class_id, &js_std_file_class);
proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_std_file_proto_funcs,
countof(js_std_file_proto_funcs));
JS_SetClassProto(ctx, js_std_file_class_id, proto);
JS_SetModuleExportList(ctx, m, js_std_funcs,
countof(js_std_funcs));
JS_SetModuleExport(ctx, m, "in", js_new_std_file(ctx, stdin, FALSE, FALSE));
JS_SetModuleExport(ctx, m, "out", js_new_std_file(ctx, stdout, FALSE, FALSE));
JS_SetModuleExport(ctx, m, "err", js_new_std_file(ctx, stderr, FALSE, FALSE));
return 0;
}
JSModuleDef *js_init_module_std(JSContext *ctx, const char *module_name)
{
JSModuleDef *m;
m = JS_NewCModule(ctx, module_name, js_std_init);
if (!m)
return NULL;
JS_AddModuleExportList(ctx, m, js_std_funcs, countof(js_std_funcs));
JS_AddModuleExport(ctx, m, "in");
JS_AddModuleExport(ctx, m, "out");
JS_AddModuleExport(ctx, m, "err");
return m;
}
/**********************************************************/
/* 'os' object */
static JSValue js_os_open(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *filename;
int flags, mode, ret;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &flags, argv[1]))
goto fail;
if (argc >= 3 && !JS_IsUndefined(argv[2])) {
if (JS_ToInt32(ctx, &mode, argv[2])) {
fail:
JS_FreeCString(ctx, filename);
return JS_EXCEPTION;
}
} else {
mode = 0666;
}
#if defined(_WIN32)
/* force binary mode by default */
if (!(flags & O_TEXT))
flags |= O_BINARY;
#endif
ret = js_get_errno(open(filename, flags, mode));
JS_FreeCString(ctx, filename);
return JS_NewInt32(ctx, ret);
}
static JSValue js_os_close(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, ret;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
ret = js_get_errno(close(fd));
return JS_NewInt32(ctx, ret);
}
static JSValue js_os_seek(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, whence;
int64_t pos, ret;
BOOL is_bigint;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
is_bigint = JS_IsBigInt(ctx, argv[1]);
if (JS_ToInt64Ext(ctx, &pos, argv[1]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &whence, argv[2]))
return JS_EXCEPTION;
ret = lseek(fd, pos, whence);
if (ret == -1)
ret = -errno;
if (is_bigint)
return JS_NewBigInt64(ctx, ret);
else
return JS_NewInt64(ctx, ret);
}
static JSValue js_os_read_write(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
int fd;
uint64_t pos, len;
size_t size;
ssize_t ret;
uint8_t *buf;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[2]))
return JS_EXCEPTION;
if (JS_ToIndex(ctx, &len, argv[3]))
return JS_EXCEPTION;
buf = JS_GetArrayBuffer(ctx, &size, argv[1]);
if (!buf)
return JS_EXCEPTION;
if (pos + len > size)
return JS_ThrowRangeError(ctx, "read/write array buffer overflow");
if (magic)
ret = js_get_errno(write(fd, buf + pos, len));
else
ret = js_get_errno(read(fd, buf + pos, len));
return JS_NewInt64(ctx, ret);
}
static JSValue js_os_isatty(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
return JS_NewBool(ctx, isatty(fd));
}
#if defined(_WIN32)
static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd;
HANDLE handle;
CONSOLE_SCREEN_BUFFER_INFO info;
JSValue obj;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
handle = (HANDLE)_get_osfhandle(fd);
if (!GetConsoleScreenBufferInfo(handle, &info))
return JS_NULL;
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return obj;
JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, info.dwSize.X), JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, info.dwSize.Y), JS_PROP_C_W_E);
return obj;
}
/* Windows 10 built-in VT100 emulation */
#define __ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#define __ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
static JSValue js_os_ttySetRaw(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd;
HANDLE handle;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
handle = (HANDLE)_get_osfhandle(fd);
SetConsoleMode(handle, ENABLE_WINDOW_INPUT | __ENABLE_VIRTUAL_TERMINAL_INPUT);
_setmode(fd, _O_BINARY);
if (fd == 0) {
handle = (HANDLE)_get_osfhandle(1); /* corresponding output */
SetConsoleMode(handle, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | __ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
return JS_UNDEFINED;
}
#else
static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd;
struct winsize ws;
JSValue obj;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (ioctl(fd, TIOCGWINSZ, &ws) == 0 &&
ws.ws_col >= 4 && ws.ws_row >= 4) {
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return obj;
JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, ws.ws_col), JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, ws.ws_row), JS_PROP_C_W_E);
return obj;
} else {
return JS_NULL;
}
}
static struct termios oldtty;
static void term_exit(void)
{
tcsetattr(0, TCSANOW, &oldtty);
}
/* XXX: should add a way to go back to normal mode */
static JSValue js_os_ttySetRaw(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
struct termios tty;
int fd;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
memset(&tty, 0, sizeof(tty));
tcgetattr(fd, &tty);
oldtty = tty;
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag |= OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &tty);
atexit(term_exit);
return JS_UNDEFINED;
}
#endif /* !_WIN32 */
static JSValue js_os_remove(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *filename;
int ret;
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
return JS_EXCEPTION;
#if defined(_WIN32)
{
struct stat st;
if (stat(filename, &st) == 0 && S_ISDIR(st.st_mode)) {
ret = rmdir(filename);
} else {
ret = unlink(filename);
}
}
#else
ret = remove(filename);
#endif
ret = js_get_errno(ret);
JS_FreeCString(ctx, filename);
return JS_NewInt32(ctx, ret);
}
static JSValue js_os_rename(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *oldpath, *newpath;
int ret;
oldpath = JS_ToCString(ctx, argv[0]);
if (!oldpath)
return JS_EXCEPTION;
newpath = JS_ToCString(ctx, argv[1]);
if (!newpath) {
JS_FreeCString(ctx, oldpath);
return JS_EXCEPTION;
}
ret = js_get_errno(rename(oldpath, newpath));
JS_FreeCString(ctx, oldpath);
JS_FreeCString(ctx, newpath);
return JS_NewInt32(ctx, ret);
}
static BOOL is_main_thread(JSRuntime *rt)
{
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
return !ts->recv_pipe;
}
static JSOSRWHandler *find_rh(JSThreadState *ts, int fd)
{
JSOSRWHandler *rh;
struct list_head *el;
list_for_each(el, &ts->os_rw_handlers) {
rh = list_entry(el, JSOSRWHandler, link);
if (rh->fd == fd)
return rh;
}
return NULL;
}
static void free_rw_handler(JSRuntime *rt, JSOSRWHandler *rh)
{
int i;
list_del(&rh->link);
for(i = 0; i < 2; i++) {
JS_FreeValueRT(rt, rh->rw_func[i]);
}
js_free_rt(rt, rh);
}
static JSValue js_os_setReadHandler(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSOSRWHandler *rh;
int fd;
JSValueConst func;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
func = argv[1];
if (JS_IsNull(func)) {
rh = find_rh(ts, fd);
if (rh) {
JS_FreeValue(ctx, rh->rw_func[magic]);
rh->rw_func[magic] = JS_NULL;
if (JS_IsNull(rh->rw_func[0]) &&
JS_IsNull(rh->rw_func[1])) {
/* remove the entry */
free_rw_handler(JS_GetRuntime(ctx), rh);
}
}
} else {
if (!JS_IsFunction(ctx, func))
return JS_ThrowTypeError(ctx, "not a function");
rh = find_rh(ts, fd);
if (!rh) {
rh = js_mallocz(ctx, sizeof(*rh));
if (!rh)
return JS_EXCEPTION;
rh->fd = fd;
rh->rw_func[0] = JS_NULL;
rh->rw_func[1] = JS_NULL;
list_add_tail(&rh->link, &ts->os_rw_handlers);
}
JS_FreeValue(ctx, rh->rw_func[magic]);
rh->rw_func[magic] = JS_DupValue(ctx, func);
}
return JS_UNDEFINED;
}
static JSOSSignalHandler *find_sh(JSThreadState *ts, int sig_num)
{
JSOSSignalHandler *sh;
struct list_head *el;
list_for_each(el, &ts->os_signal_handlers) {
sh = list_entry(el, JSOSSignalHandler, link);
if (sh->sig_num == sig_num)
return sh;
}
return NULL;
}
static void free_sh(JSRuntime *rt, JSOSSignalHandler *sh)
{
list_del(&sh->link);
JS_FreeValueRT(rt, sh->func);
js_free_rt(rt, sh);
}
static void os_signal_handler(int sig_num)
{
os_pending_signals |= ((uint64_t)1 << sig_num);
}
#if defined(_WIN32)
typedef void (*sighandler_t)(int sig_num);
#endif
static JSValue js_os_signal(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSOSSignalHandler *sh;
uint32_t sig_num;
JSValueConst func;
sighandler_t handler;
if (!is_main_thread(rt))
return JS_ThrowTypeError(ctx, "signal handler can only be set in the main thread");
if (JS_ToUint32(ctx, &sig_num, argv[0]))
return JS_EXCEPTION;
if (sig_num >= 64)
return JS_ThrowRangeError(ctx, "invalid signal number");
func = argv[1];
/* func = null: SIG_DFL, func = undefined, SIG_IGN */
if (JS_IsNull(func) || JS_IsUndefined(func)) {
sh = find_sh(ts, sig_num);
if (sh) {
free_sh(JS_GetRuntime(ctx), sh);
}
if (JS_IsNull(func))
handler = SIG_DFL;
else
handler = SIG_IGN;
signal(sig_num, handler);
} else {
if (!JS_IsFunction(ctx, func))
return JS_ThrowTypeError(ctx, "not a function");
sh = find_sh(ts, sig_num);
if (!sh) {
sh = js_mallocz(ctx, sizeof(*sh));
if (!sh)
return JS_EXCEPTION;
sh->sig_num = sig_num;
list_add_tail(&sh->link, &ts->os_signal_handlers);
}
JS_FreeValue(ctx, sh->func);
sh->func = JS_DupValue(ctx, func);
signal(sig_num, os_signal_handler);
}
return JS_UNDEFINED;
}
#if defined(__linux__) || defined(__APPLE__)
static int64_t get_time_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
}
static int64_t get_time_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}
#else
/* more portable, but does not work if the date is updated */
static int64_t get_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
}
static int64_t get_time_ns(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000000000 + (tv.tv_usec * 1000);
}
#endif
static JSValue js_os_now(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv)
{
return JS_NewFloat64(ctx, (double)get_time_ns() / 1e6);
}
static void free_timer(JSRuntime *rt, JSOSTimer *th)
{
list_del(&th->link);
JS_FreeValueRT(rt, th->func);
js_free_rt(rt, th);
}
static JSValue js_os_setTimeout(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
int64_t delay;
JSValueConst func;
JSOSTimer *th;
func = argv[0];
if (!JS_IsFunction(ctx, func))
return JS_ThrowTypeError(ctx, "not a function");
if (JS_ToInt64(ctx, &delay, argv[1]))
return JS_EXCEPTION;
th = js_mallocz(ctx, sizeof(*th));
if (!th)
return JS_EXCEPTION;
th->timer_id = ts->next_timer_id;
if (ts->next_timer_id == INT32_MAX)
ts->next_timer_id = 1;
else
ts->next_timer_id++;
th->timeout = get_time_ms() + delay;
th->func = JS_DupValue(ctx, func);
list_add_tail(&th->link, &ts->os_timers);
return JS_NewInt32(ctx, th->timer_id);
}
static JSOSTimer *find_timer_by_id(JSThreadState *ts, int timer_id)
{
struct list_head *el;
if (timer_id <= 0)
return NULL;
list_for_each(el, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link);
if (th->timer_id == timer_id)
return th;
}
return NULL;
}
static JSValue js_os_clearTimeout(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSOSTimer *th;
int timer_id;
if (JS_ToInt32(ctx, &timer_id, argv[0]))
return JS_EXCEPTION;
th = find_timer_by_id(ts, timer_id);
if (!th)
return JS_UNDEFINED;
free_timer(rt, th);
return JS_UNDEFINED;
}
/* return a promise */
static JSValue js_os_sleepAsync(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
int64_t delay;
JSOSTimer *th;
JSValue promise, resolving_funcs[2];
if (JS_ToInt64(ctx, &delay, argv[0]))
return JS_EXCEPTION;
promise = JS_NewPromiseCapability(ctx, resolving_funcs);
if (JS_IsException(promise))
return JS_EXCEPTION;
th = js_mallocz(ctx, sizeof(*th));
if (!th) {
JS_FreeValue(ctx, promise);
JS_FreeValue(ctx, resolving_funcs[0]);
JS_FreeValue(ctx, resolving_funcs[1]);
return JS_EXCEPTION;
}
th->timer_id = -1;
th->timeout = get_time_ms() + delay;
th->func = JS_DupValue(ctx, resolving_funcs[0]);
list_add_tail(&th->link, &ts->os_timers);
JS_FreeValue(ctx, resolving_funcs[0]);
JS_FreeValue(ctx, resolving_funcs[1]);
return promise;
}
static void call_handler(JSContext *ctx, JSValueConst func)
{
JSValue ret, func1;
/* 'func' might be destroyed when calling itself (if it frees the
handler), so must take extra care */
func1 = JS_DupValue(ctx, func);
ret = JS_Call(ctx, func1, JS_UNDEFINED, 0, NULL);
JS_FreeValue(ctx, func1);
if (JS_IsException(ret))
js_std_dump_error(ctx);
JS_FreeValue(ctx, ret);
}
#ifdef USE_WORKER
#ifdef _WIN32
static int js_waker_init(JSWaker *w)
{
w->handle = CreateEvent(NULL, TRUE, FALSE, NULL);
return w->handle ? 0 : -1;
}
static void js_waker_signal(JSWaker *w)
{
SetEvent(w->handle);
}
static void js_waker_clear(JSWaker *w)
{
ResetEvent(w->handle);
}
static void js_waker_close(JSWaker *w)
{
CloseHandle(w->handle);
w->handle = INVALID_HANDLE_VALUE;
}
#else // !_WIN32
static int js_waker_init(JSWaker *w)
{
int fds[2];
if (pipe(fds) < 0)
return -1;
w->read_fd = fds[0];
w->write_fd = fds[1];
return 0;
}
static void js_waker_signal(JSWaker *w)
{
int ret;
for(;;) {
ret = write(w->write_fd, "", 1);
if (ret == 1)
break;
if (ret < 0 && (errno != EAGAIN || errno != EINTR))
break;
}
}
static void js_waker_clear(JSWaker *w)
{
uint8_t buf[16];
int ret;
for(;;) {
ret = read(w->read_fd, buf, sizeof(buf));
if (ret >= 0)
break;
if (errno != EAGAIN && errno != EINTR)
break;
}
}
static void js_waker_close(JSWaker *w)
{
close(w->read_fd);
close(w->write_fd);
w->read_fd = -1;
w->write_fd = -1;
}
#endif // _WIN32
static void js_free_message(JSWorkerMessage *msg);
/* return 1 if a message was handled, 0 if no message */
static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
JSWorkerMessageHandler *port)
{
JSWorkerMessagePipe *ps = port->recv_pipe;
int ret;
struct list_head *el;
JSWorkerMessage *msg;
JSValue obj, data_obj, func, retval;
pthread_mutex_lock(&ps->mutex);
if (!list_empty(&ps->msg_queue)) {
el = ps->msg_queue.next;
msg = list_entry(el, JSWorkerMessage, link);
/* remove the message from the queue */
list_del(&msg->link);
if (list_empty(&ps->msg_queue))
js_waker_clear(&ps->waker);
pthread_mutex_unlock(&ps->mutex);
data_obj = JS_ReadObject(ctx, msg->data, msg->data_len,
JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE);
js_free_message(msg);
if (JS_IsException(data_obj))
goto fail;
obj = JS_NewObject(ctx);
if (JS_IsException(obj)) {
JS_FreeValue(ctx, data_obj);
goto fail;
}
JS_DefinePropertyValueStr(ctx, obj, "data", data_obj, JS_PROP_C_W_E);
/* 'func' might be destroyed when calling itself (if it frees the
handler), so must take extra care */
func = JS_DupValue(ctx, port->on_message_func);
retval = JS_Call(ctx, func, JS_UNDEFINED, 1, (JSValueConst *)&obj);
JS_FreeValue(ctx, obj);
JS_FreeValue(ctx, func);
if (JS_IsException(retval)) {
fail:
js_std_dump_error(ctx);
} else {
JS_FreeValue(ctx, retval);
}
ret = 1;
} else {
pthread_mutex_unlock(&ps->mutex);
ret = 0;
}
return ret;
}
#else
static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
JSWorkerMessageHandler *port)
{
return 0;
}
#endif /* !USE_WORKER */
#if defined(_WIN32)
static int js_os_poll(JSContext *ctx)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
int min_delay, count;
int64_t cur_time, delay;
JSOSRWHandler *rh;
struct list_head *el;
HANDLE handles[MAXIMUM_WAIT_OBJECTS]; // 64
/* XXX: handle signals if useful */
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers) &&
list_empty(&ts->port_list)) {
return -1; /* no more events */
}
if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms();
min_delay = 10000;
list_for_each(el, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link);
delay = th->timeout - cur_time;
if (delay <= 0) {
JSValue func;
/* the timer expired */
func = th->func;
th->func = JS_UNDEFINED;
free_timer(rt, th);
call_handler(ctx, func);
JS_FreeValue(ctx, func);
return 0;
} else if (delay < min_delay) {
min_delay = delay;
}
}
} else {
min_delay = -1;
}
count = 0;
list_for_each(el, &ts->os_rw_handlers) {
rh = list_entry(el, JSOSRWHandler, link);
if (rh->fd == 0 && !JS_IsNull(rh->rw_func[0])) {
handles[count++] = (HANDLE)_get_osfhandle(rh->fd); // stdin
if (count == (int)countof(handles))
break;
}
}
list_for_each(el, &ts->port_list) {
JSWorkerMessageHandler *port = list_entry(el, JSWorkerMessageHandler, link);
if (JS_IsNull(port->on_message_func))
continue;
handles[count++] = port->recv_pipe->waker.handle;
if (count == (int)countof(handles))
break;
}
if (count > 0) {
DWORD ret, timeout = INFINITE;
if (min_delay != -1)
timeout = min_delay;
ret = WaitForMultipleObjects(count, handles, FALSE, timeout);
if (ret < count) {
list_for_each(el, &ts->os_rw_handlers) {
rh = list_entry(el, JSOSRWHandler, link);
if (rh->fd == 0 && !JS_IsNull(rh->rw_func[0])) {
call_handler(ctx, rh->rw_func[0]);
/* must stop because the list may have been modified */
goto done;
}
}
list_for_each(el, &ts->port_list) {
JSWorkerMessageHandler *port = list_entry(el, JSWorkerMessageHandler, link);
if (!JS_IsNull(port->on_message_func)) {
JSWorkerMessagePipe *ps = port->recv_pipe;
if (ps->waker.handle == handles[ret]) {
if (handle_posted_message(rt, ctx, port))
goto done;
}
}
}
}
} else {
Sleep(min_delay);
}
done:
return 0;
}
#else
static int js_os_poll(JSContext *ctx)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
int ret, fd_max, min_delay;
int64_t cur_time, delay;
fd_set rfds, wfds;
JSOSRWHandler *rh;
struct list_head *el;
struct timeval tv, *tvp;
/* only check signals in the main thread */
if (!ts->recv_pipe &&
unlikely(os_pending_signals != 0)) {
JSOSSignalHandler *sh;
uint64_t mask;
list_for_each(el, &ts->os_signal_handlers) {
sh = list_entry(el, JSOSSignalHandler, link);
mask = (uint64_t)1 << sh->sig_num;
if (os_pending_signals & mask) {
os_pending_signals &= ~mask;
call_handler(ctx, sh->func);
return 0;
}
}
}
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers) &&
list_empty(&ts->port_list))
return -1; /* no more events */
if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms();
min_delay = 10000;
list_for_each(el, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link);
delay = th->timeout - cur_time;
if (delay <= 0) {
JSValue func;
/* the timer expired */
func = th->func;
th->func = JS_UNDEFINED;
free_timer(rt, th);
call_handler(ctx, func);
JS_FreeValue(ctx, func);
return 0;
} else if (delay < min_delay) {
min_delay = delay;
}
}
tv.tv_sec = min_delay / 1000;
tv.tv_usec = (min_delay % 1000) * 1000;
tvp = &tv;
} else {
tvp = NULL;
}
FD_ZERO(&rfds);
FD_ZERO(&wfds);
fd_max = -1;
list_for_each(el, &ts->os_rw_handlers) {
rh = list_entry(el, JSOSRWHandler, link);
fd_max = max_int(fd_max, rh->fd);
if (!JS_IsNull(rh->rw_func[0]))
FD_SET(rh->fd, &rfds);
if (!JS_IsNull(rh->rw_func[1]))
FD_SET(rh->fd, &wfds);
}
list_for_each(el, &ts->port_list) {
JSWorkerMessageHandler *port = list_entry(el, JSWorkerMessageHandler, link);
if (!JS_IsNull(port->on_message_func)) {
JSWorkerMessagePipe *ps = port->recv_pipe;
fd_max = max_int(fd_max, ps->waker.read_fd);
FD_SET(ps->waker.read_fd, &rfds);
}
}
ret = select(fd_max + 1, &rfds, &wfds, NULL, tvp);
if (ret > 0) {
list_for_each(el, &ts->os_rw_handlers) {
rh = list_entry(el, JSOSRWHandler, link);
if (!JS_IsNull(rh->rw_func[0]) &&
FD_ISSET(rh->fd, &rfds)) {
call_handler(ctx, rh->rw_func[0]);
/* must stop because the list may have been modified */
goto done;
}
if (!JS_IsNull(rh->rw_func[1]) &&
FD_ISSET(rh->fd, &wfds)) {
call_handler(ctx, rh->rw_func[1]);
/* must stop because the list may have been modified */
goto done;
}
}
list_for_each(el, &ts->port_list) {
JSWorkerMessageHandler *port = list_entry(el, JSWorkerMessageHandler, link);
if (!JS_IsNull(port->on_message_func)) {
JSWorkerMessagePipe *ps = port->recv_pipe;
if (FD_ISSET(ps->waker.read_fd, &rfds)) {
if (handle_posted_message(rt, ctx, port))
goto done;
}
}
}
}
done:
return 0;
}
#endif /* !_WIN32 */
static JSValue make_obj_error(JSContext *ctx,
JSValue obj,
int err)
{
JSValue arr;
if (JS_IsException(obj))
return obj;
arr = JS_NewArray(ctx);
if (JS_IsException(arr))
return JS_EXCEPTION;
JS_DefinePropertyValueUint32(ctx, arr, 0, obj,
JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, arr, 1, JS_NewInt32(ctx, err),
JS_PROP_C_W_E);
return arr;
}
static JSValue make_string_error(JSContext *ctx,
const char *buf,
int err)
{
return make_obj_error(ctx, JS_NewString(ctx, buf), err);
}
/* return [cwd, errorcode] */
static JSValue js_os_getcwd(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
char buf[PATH_MAX];
int err;
if (!getcwd(buf, sizeof(buf))) {
buf[0] = '\0';
err = errno;
} else {
err = 0;
}
return make_string_error(ctx, buf, err);
}
static JSValue js_os_chdir(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *target;
int err;
target = JS_ToCString(ctx, argv[0]);
if (!target)
return JS_EXCEPTION;
err = js_get_errno(chdir(target));
JS_FreeCString(ctx, target);
return JS_NewInt32(ctx, err);
}
static JSValue js_os_mkdir(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int mode, ret;
const char *path;
if (argc >= 2) {
if (JS_ToInt32(ctx, &mode, argv[1]))
return JS_EXCEPTION;
} else {
mode = 0777;
}
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
#if defined(_WIN32)
(void)mode;
ret = js_get_errno(mkdir(path));
#else
ret = js_get_errno(mkdir(path, mode));
#endif
JS_FreeCString(ctx, path);
return JS_NewInt32(ctx, ret);
}
/* return [array, errorcode] */
static JSValue js_os_readdir(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *path;
DIR *f;
struct dirent *d;
JSValue obj;
int err;
uint32_t len;
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
obj = JS_NewArray(ctx);
if (JS_IsException(obj)) {
JS_FreeCString(ctx, path);
return JS_EXCEPTION;
}
f = opendir(path);
if (!f)
err = errno;
else
err = 0;
JS_FreeCString(ctx, path);
if (!f)
goto done;
len = 0;
for(;;) {
errno = 0;
d = readdir(f);
if (!d) {
err = errno;
break;
}
JS_DefinePropertyValueUint32(ctx, obj, len++,
JS_NewString(ctx, d->d_name),
JS_PROP_C_W_E);
}
closedir(f);
done:
return make_obj_error(ctx, obj, err);
}
#if !defined(_WIN32)
static int64_t timespec_to_ms(const struct timespec *tv)
{
return (int64_t)tv->tv_sec * 1000 + (tv->tv_nsec / 1000000);
}
#endif
/* return [obj, errcode] */
static JSValue js_os_stat(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int is_lstat)
{
const char *path;
int err, res;
struct stat st;
JSValue obj;
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
#if defined(_WIN32)
res = stat(path, &st);
#else
if (is_lstat)
res = lstat(path, &st);
else
res = stat(path, &st);
#endif
if (res < 0)
err = errno;
else
err = 0;
JS_FreeCString(ctx, path);
if (res < 0) {
obj = JS_NULL;
} else {
obj = JS_NewObject(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
JS_DefinePropertyValueStr(ctx, obj, "dev",
JS_NewInt64(ctx, st.st_dev),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ino",
JS_NewInt64(ctx, st.st_ino),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "mode",
JS_NewInt32(ctx, st.st_mode),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "nlink",
JS_NewInt64(ctx, st.st_nlink),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "uid",
JS_NewInt64(ctx, st.st_uid),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "gid",
JS_NewInt64(ctx, st.st_gid),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "rdev",
JS_NewInt64(ctx, st.st_rdev),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "size",
JS_NewInt64(ctx, st.st_size),
JS_PROP_C_W_E);
#if !defined(_WIN32)
JS_DefinePropertyValueStr(ctx, obj, "blocks",
JS_NewInt64(ctx, st.st_blocks),
JS_PROP_C_W_E);
#endif
#if defined(_WIN32)
JS_DefinePropertyValueStr(ctx, obj, "atime",
JS_NewInt64(ctx, (int64_t)st.st_atime * 1000),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "mtime",
JS_NewInt64(ctx, (int64_t)st.st_mtime * 1000),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ctime",
JS_NewInt64(ctx, (int64_t)st.st_ctime * 1000),
JS_PROP_C_W_E);
#elif defined(__APPLE__)
JS_DefinePropertyValueStr(ctx, obj, "atime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_atimespec)),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "mtime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_mtimespec)),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ctime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_ctimespec)),
JS_PROP_C_W_E);
#else
JS_DefinePropertyValueStr(ctx, obj, "atime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_atim)),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "mtime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_mtim)),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ctime",
JS_NewInt64(ctx, timespec_to_ms(&st.st_ctim)),
JS_PROP_C_W_E);
#endif
}
return make_obj_error(ctx, obj, err);
}
#if !defined(_WIN32)
static void ms_to_timeval(struct timeval *tv, uint64_t v)
{
tv->tv_sec = v / 1000;
tv->tv_usec = (v % 1000) * 1000;
}
#endif
static JSValue js_os_utimes(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *path;
int64_t atime, mtime;
int ret;
if (JS_ToInt64(ctx, &atime, argv[1]))
return JS_EXCEPTION;
if (JS_ToInt64(ctx, &mtime, argv[2]))
return JS_EXCEPTION;
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
#if defined(_WIN32)
{
struct _utimbuf times;
times.actime = atime / 1000;
times.modtime = mtime / 1000;
ret = js_get_errno(_utime(path, ×));
}
#else
{
struct timeval times[2];
ms_to_timeval(×[0], atime);
ms_to_timeval(×[1], mtime);
ret = js_get_errno(utimes(path, times));
}
#endif
JS_FreeCString(ctx, path);
return JS_NewInt32(ctx, ret);
}
/* sleep(delay_ms) */
static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int64_t delay;
int ret;
if (JS_ToInt64(ctx, &delay, argv[0]))
return JS_EXCEPTION;
if (delay < 0)
delay = 0;
#if defined(_WIN32)
{
if (delay > INT32_MAX)
delay = INT32_MAX;
Sleep(delay);
ret = 0;
}
#else
{
struct timespec ts;
ts.tv_sec = delay / 1000;
ts.tv_nsec = (delay % 1000) * 1000000;
ret = js_get_errno(nanosleep(&ts, NULL));
}
#endif
return JS_NewInt32(ctx, ret);
}
#if defined(_WIN32)
static char *realpath(const char *path, char *buf)
{
if (!_fullpath(buf, path, PATH_MAX)) {
errno = ENOENT;
return NULL;
} else {
return buf;
}
}
#endif
/* return [path, errorcode] */
static JSValue js_os_realpath(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *path;
char buf[PATH_MAX], *res;
int err;
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
res = realpath(path, buf);
JS_FreeCString(ctx, path);
if (!res) {
buf[0] = '\0';
err = errno;
} else {
err = 0;
}
return make_string_error(ctx, buf, err);
}
#if !defined(_WIN32)
static JSValue js_os_symlink(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *target, *linkpath;
int err;
target = JS_ToCString(ctx, argv[0]);
if (!target)
return JS_EXCEPTION;
linkpath = JS_ToCString(ctx, argv[1]);
if (!linkpath) {
JS_FreeCString(ctx, target);
return JS_EXCEPTION;
}
err = js_get_errno(symlink(target, linkpath));
JS_FreeCString(ctx, target);
JS_FreeCString(ctx, linkpath);
return JS_NewInt32(ctx, err);
}
/* return [path, errorcode] */
static JSValue js_os_readlink(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *path;
char buf[PATH_MAX];
int err;
ssize_t res;
path = JS_ToCString(ctx, argv[0]);
if (!path)
return JS_EXCEPTION;
res = readlink(path, buf, sizeof(buf) - 1);
if (res < 0) {
buf[0] = '\0';
err = errno;
} else {
buf[res] = '\0';
err = 0;
}
JS_FreeCString(ctx, path);
return make_string_error(ctx, buf, err);
}
static char **build_envp(JSContext *ctx, JSValueConst obj)
{
uint32_t len, i;
JSPropertyEnum *tab;
char **envp, *pair;
const char *key, *str;
JSValue val;
size_t key_len, str_len;
if (JS_GetOwnPropertyNames(ctx, &tab, &len, obj,
JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0)
return NULL;
envp = js_mallocz(ctx, sizeof(envp[0]) * ((size_t)len + 1));
if (!envp)
goto fail;
for(i = 0; i < len; i++) {
val = JS_GetProperty(ctx, obj, tab[i].atom);
if (JS_IsException(val))
goto fail;
str = JS_ToCString(ctx, val);
JS_FreeValue(ctx, val);
if (!str)
goto fail;
key = JS_AtomToCString(ctx, tab[i].atom);
if (!key) {
JS_FreeCString(ctx, str);
goto fail;
}
key_len = strlen(key);
str_len = strlen(str);
pair = js_malloc(ctx, key_len + str_len + 2);
if (!pair) {
JS_FreeCString(ctx, key);
JS_FreeCString(ctx, str);
goto fail;
}
memcpy(pair, key, key_len);
pair[key_len] = '=';
memcpy(pair + key_len + 1, str, str_len);
pair[key_len + 1 + str_len] = '\0';
envp[i] = pair;
JS_FreeCString(ctx, key);
JS_FreeCString(ctx, str);
}
done:
for(i = 0; i < len; i++)
JS_FreeAtom(ctx, tab[i].atom);
js_free(ctx, tab);
return envp;
fail:
if (envp) {
for(i = 0; i < len; i++)
js_free(ctx, envp[i]);
js_free(ctx, envp);
envp = NULL;
}
goto done;
}
/* execvpe is not available on non GNU systems */
static int my_execvpe(const char *filename, char **argv, char **envp)
{
char *path, *p, *p_next, *p1;
char buf[PATH_MAX];
size_t filename_len, path_len;
BOOL eacces_error;
filename_len = strlen(filename);
if (filename_len == 0) {
errno = ENOENT;
return -1;
}
if (strchr(filename, '/'))
return execve(filename, argv, envp);
path = getenv("PATH");
if (!path)
path = (char *)"/bin:/usr/bin";
eacces_error = FALSE;
p = path;
for(p = path; p != NULL; p = p_next) {
p1 = strchr(p, ':');
if (!p1) {
p_next = NULL;
path_len = strlen(p);
} else {
p_next = p1 + 1;
path_len = p1 - p;
}
/* path too long */
if ((path_len + 1 + filename_len + 1) > PATH_MAX)
continue;
memcpy(buf, p, path_len);
buf[path_len] = '/';
memcpy(buf + path_len + 1, filename, filename_len);
buf[path_len + 1 + filename_len] = '\0';
execve(buf, argv, envp);
switch(errno) {
case EACCES:
eacces_error = TRUE;
break;
case ENOENT:
case ENOTDIR:
break;
default:
return -1;
}
}
if (eacces_error)
errno = EACCES;
return -1;
}
/* exec(args[, options]) -> exitcode */
static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValueConst options, args = argv[0];
JSValue val, ret_val;
const char **exec_argv, *file = NULL, *str, *cwd = NULL;
char **envp = environ;
uint32_t exec_argc, i;
int ret, pid, status;
BOOL block_flag = TRUE, use_path = TRUE;
static const char *std_name[3] = { "stdin", "stdout", "stderr" };
int std_fds[3];
uint32_t uid = -1, gid = -1;
val = JS_GetPropertyStr(ctx, args, "length");
if (JS_IsException(val))
return JS_EXCEPTION;
ret = JS_ToUint32(ctx, &exec_argc, val);
JS_FreeValue(ctx, val);
if (ret)
return JS_EXCEPTION;
/* arbitrary limit to avoid overflow */
if (exec_argc < 1 || exec_argc > 65535) {
return JS_ThrowTypeError(ctx, "invalid number of arguments");
}
exec_argv = js_mallocz(ctx, sizeof(exec_argv[0]) * (exec_argc + 1));
if (!exec_argv)
return JS_EXCEPTION;
for(i = 0; i < exec_argc; i++) {
val = JS_GetPropertyUint32(ctx, args, i);
if (JS_IsException(val))
goto exception;
str = JS_ToCString(ctx, val);
JS_FreeValue(ctx, val);
if (!str)
goto exception;
exec_argv[i] = str;
}
exec_argv[exec_argc] = NULL;
for(i = 0; i < 3; i++)
std_fds[i] = i;
/* get the options, if any */
if (argc >= 2) {
options = argv[1];
if (get_bool_option(ctx, &block_flag, options, "block"))
goto exception;
if (get_bool_option(ctx, &use_path, options, "usePath"))
goto exception;
val = JS_GetPropertyStr(ctx, options, "file");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
file = JS_ToCString(ctx, val);
JS_FreeValue(ctx, val);
if (!file)
goto exception;
}
val = JS_GetPropertyStr(ctx, options, "cwd");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
cwd = JS_ToCString(ctx, val);
JS_FreeValue(ctx, val);
if (!cwd)
goto exception;
}
/* stdin/stdout/stderr handles */
for(i = 0; i < 3; i++) {
val = JS_GetPropertyStr(ctx, options, std_name[i]);
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
int fd;
ret = JS_ToInt32(ctx, &fd, val);
JS_FreeValue(ctx, val);
if (ret)
goto exception;
std_fds[i] = fd;
}
}
val = JS_GetPropertyStr(ctx, options, "env");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
envp = build_envp(ctx, val);
JS_FreeValue(ctx, val);
if (!envp)
goto exception;
}
val = JS_GetPropertyStr(ctx, options, "uid");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
ret = JS_ToUint32(ctx, &uid, val);
JS_FreeValue(ctx, val);
if (ret)
goto exception;
}
val = JS_GetPropertyStr(ctx, options, "gid");
if (JS_IsException(val))
goto exception;
if (!JS_IsUndefined(val)) {
ret = JS_ToUint32(ctx, &gid, val);
JS_FreeValue(ctx, val);
if (ret)
goto exception;
}
}
pid = fork();
if (pid < 0) {
JS_ThrowTypeError(ctx, "fork error");
goto exception;
}
if (pid == 0) {
/* child */
/* remap the stdin/stdout/stderr handles if necessary */
for(i = 0; i < 3; i++) {
if (std_fds[i] != i) {
if (dup2(std_fds[i], i) < 0)
_exit(127);
}
}
#if defined(HAVE_CLOSEFROM)
/* closefrom() is available on many recent unix systems:
Linux with glibc 2.34+, Solaris 9+, FreeBSD 7.3+,
NetBSD 3.0+, OpenBSD 3.5+.
Linux with the musl libc and macOS don't have it.
*/
closefrom(3);
#else
{
/* Close the file handles manually, limit to 1024 to avoid
costly loop on linux Alpine where sysconf(_SC_OPEN_MAX)
returns a huge value 1048576.
Patch inspired by nicolas-duteil-nova. See also:
https://stackoverflow.com/questions/73229353/
https://stackoverflow.com/questions/899038/#918469
*/
int fd_max = min_int(sysconf(_SC_OPEN_MAX), 1024);
for(i = 3; i < fd_max; i++)
close(i);
}
#endif
if (cwd) {
if (chdir(cwd) < 0)
_exit(127);
}
if (uid != -1) {
if (setuid(uid) < 0)
_exit(127);
}
if (gid != -1) {
if (setgid(gid) < 0)
_exit(127);
}
if (!file)
file = exec_argv[0];
if (use_path)
ret = my_execvpe(file, (char **)exec_argv, envp);
else
ret = execve(file, (char **)exec_argv, envp);
_exit(127);
}
/* parent */
if (block_flag) {
for(;;) {
ret = waitpid(pid, &status, 0);
if (ret == pid) {
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
break;
} else if (WIFSIGNALED(status)) {
ret = -WTERMSIG(status);
break;
}
}
}
} else {
ret = pid;
}
ret_val = JS_NewInt32(ctx, ret);
done:
JS_FreeCString(ctx, file);
JS_FreeCString(ctx, cwd);
for(i = 0; i < exec_argc; i++)
JS_FreeCString(ctx, exec_argv[i]);
js_free(ctx, exec_argv);
if (envp != environ) {
char **p;
p = envp;
while (*p != NULL) {
js_free(ctx, *p);
p++;
}
js_free(ctx, envp);
}
return ret_val;
exception:
ret_val = JS_EXCEPTION;
goto done;
}
/* getpid() -> pid */
static JSValue js_os_getpid(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
return JS_NewInt32(ctx, getpid());
}
/* waitpid(pid, block) -> [pid, status] */
static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int pid, status, options, ret;
JSValue obj;
if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &options, argv[1]))
return JS_EXCEPTION;
ret = waitpid(pid, &status, options);
if (ret < 0) {
ret = -errno;
status = 0;
}
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return obj;
JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, ret),
JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, status),
JS_PROP_C_W_E);
return obj;
}
/* pipe() -> [read_fd, write_fd] or null if error */
static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int pipe_fds[2], ret;
JSValue obj;
ret = pipe(pipe_fds);
if (ret < 0)
return JS_NULL;
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return obj;
JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewInt32(ctx, pipe_fds[0]),
JS_PROP_C_W_E);
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, pipe_fds[1]),
JS_PROP_C_W_E);
return obj;
}
/* kill(pid, sig) */
static JSValue js_os_kill(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int pid, sig, ret;
if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &sig, argv[1]))
return JS_EXCEPTION;
ret = js_get_errno(kill(pid, sig));
return JS_NewInt32(ctx, ret);
}
/* dup(fd) */
static JSValue js_os_dup(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, ret;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
ret = js_get_errno(dup(fd));
return JS_NewInt32(ctx, ret);
}
/* dup2(fd) */
static JSValue js_os_dup2(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int fd, fd2, ret;
if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION;
if (JS_ToInt32(ctx, &fd2, argv[1]))
return JS_EXCEPTION;
ret = js_get_errno(dup2(fd, fd2));
return JS_NewInt32(ctx, ret);
}
#endif /* !_WIN32 */
#ifdef USE_WORKER
/* Worker */
typedef struct {
JSWorkerMessagePipe *recv_pipe;
JSWorkerMessagePipe *send_pipe;
JSWorkerMessageHandler *msg_handler;
} JSWorkerData;
typedef struct {
char *filename; /* module filename */
char *basename; /* module base name */
JSWorkerMessagePipe *recv_pipe, *send_pipe;
int strip_flags;
} WorkerFuncArgs;
typedef struct {
int ref_count;
uint64_t buf[0];
} JSSABHeader;
static JSClassID js_worker_class_id;
static JSContext *(*js_worker_new_context_func)(JSRuntime *rt);
static int atomic_add_int(int *ptr, int v)
{
return atomic_fetch_add((_Atomic(uint32_t) *)ptr, v) + v;
}
/* shared array buffer allocator */
static void *js_sab_alloc(void *opaque, size_t size)
{
JSSABHeader *sab;
sab = malloc(sizeof(JSSABHeader) + size);
if (!sab)
return NULL;
sab->ref_count = 1;
return sab->buf;
}
static void js_sab_free(void *opaque, void *ptr)
{
JSSABHeader *sab;
int ref_count;
sab = (JSSABHeader *)((uint8_t *)ptr - sizeof(JSSABHeader));
ref_count = atomic_add_int(&sab->ref_count, -1);
assert(ref_count >= 0);
if (ref_count == 0) {
free(sab);
}
}
static void js_sab_dup(void *opaque, void *ptr)
{
JSSABHeader *sab;
sab = (JSSABHeader *)((uint8_t *)ptr - sizeof(JSSABHeader));
atomic_add_int(&sab->ref_count, 1);
}
static JSWorkerMessagePipe *js_new_message_pipe(void)
{
JSWorkerMessagePipe *ps;
ps = malloc(sizeof(*ps));
if (!ps)
return NULL;
if (js_waker_init(&ps->waker)) {
free(ps);
return NULL;
}
ps->ref_count = 1;
init_list_head(&ps->msg_queue);
pthread_mutex_init(&ps->mutex, NULL);
return ps;
}
static JSWorkerMessagePipe *js_dup_message_pipe(JSWorkerMessagePipe *ps)
{
atomic_add_int(&ps->ref_count, 1);
return ps;
}
static void js_free_message(JSWorkerMessage *msg)
{
size_t i;
/* free the SAB */
for(i = 0; i < msg->sab_tab_len; i++) {
js_sab_free(NULL, msg->sab_tab[i]);
}
free(msg->sab_tab);
free(msg->data);
free(msg);
}
static void js_free_message_pipe(JSWorkerMessagePipe *ps)
{
struct list_head *el, *el1;
JSWorkerMessage *msg;
int ref_count;
if (!ps)
return;
ref_count = atomic_add_int(&ps->ref_count, -1);
assert(ref_count >= 0);
if (ref_count == 0) {
list_for_each_safe(el, el1, &ps->msg_queue) {
msg = list_entry(el, JSWorkerMessage, link);
js_free_message(msg);
}
pthread_mutex_destroy(&ps->mutex);
js_waker_close(&ps->waker);
free(ps);
}
}
static void js_free_port(JSRuntime *rt, JSWorkerMessageHandler *port)
{
if (port) {
js_free_message_pipe(port->recv_pipe);
JS_FreeValueRT(rt, port->on_message_func);
list_del(&port->link);
js_free_rt(rt, port);
}
}
static void js_worker_finalizer(JSRuntime *rt, JSValue val)
{
JSWorkerData *worker = JS_GetOpaque(val, js_worker_class_id);
if (worker) {
js_free_message_pipe(worker->recv_pipe);
js_free_message_pipe(worker->send_pipe);
js_free_port(rt, worker->msg_handler);
js_free_rt(rt, worker);
}
}
static JSClassDef js_worker_class = {
"Worker",
.finalizer = js_worker_finalizer,
};
static void *worker_func(void *opaque)
{
WorkerFuncArgs *args = opaque;
JSRuntime *rt;
JSThreadState *ts;
JSContext *ctx;
JSValue val;
rt = JS_NewRuntime();
if (rt == NULL) {
fprintf(stderr, "JS_NewRuntime failure");
exit(1);
}
JS_SetStripInfo(rt, args->strip_flags);
js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
/* set the pipe to communicate with the parent */
ts = JS_GetRuntimeOpaque(rt);
ts->recv_pipe = args->recv_pipe;
ts->send_pipe = args->send_pipe;
/* function pointer to avoid linking the whole JS_NewContext() if
not needed */
ctx = js_worker_new_context_func(rt);
if (ctx == NULL) {
fprintf(stderr, "JS_NewContext failure");
}
JS_SetCanBlock(rt, TRUE);
js_std_add_helpers(ctx, -1, NULL);
val = JS_LoadModule(ctx, args->basename, args->filename);
free(args->filename);
free(args->basename);
free(args);
val = js_std_await(ctx, val);
if (JS_IsException(val))
js_std_dump_error(ctx);
JS_FreeValue(ctx, val);
js_std_loop(ctx);
JS_FreeContext(ctx);
js_std_free_handlers(rt);
JS_FreeRuntime(rt);
return NULL;
}
static JSValue js_worker_ctor_internal(JSContext *ctx, JSValueConst new_target,
JSWorkerMessagePipe *recv_pipe,
JSWorkerMessagePipe *send_pipe)
{
JSValue obj = JS_UNDEFINED, proto;
JSWorkerData *s;
/* create the object */
if (JS_IsUndefined(new_target)) {
proto = JS_GetClassProto(ctx, js_worker_class_id);
} else {
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if (JS_IsException(proto))
goto fail;
}
obj = JS_NewObjectProtoClass(ctx, proto, js_worker_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(obj))
goto fail;
s = js_mallocz(ctx, sizeof(*s));
if (!s)
goto fail;
s->recv_pipe = js_dup_message_pipe(recv_pipe);
s->send_pipe = js_dup_message_pipe(send_pipe);
JS_SetOpaque(obj, s);
return obj;
fail:
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
int argc, JSValueConst *argv)
{
JSRuntime *rt = JS_GetRuntime(ctx);
WorkerFuncArgs *args = NULL;
pthread_t tid;
pthread_attr_t attr;
JSValue obj = JS_UNDEFINED;
int ret;
const char *filename = NULL, *basename;
JSAtom basename_atom;
/* XXX: in order to avoid problems with resource liberation, we
don't support creating workers inside workers */
if (!is_main_thread(rt))
return JS_ThrowTypeError(ctx, "cannot create a worker inside a worker");
/* base name, assuming the calling function is a normal JS
function */
basename_atom = JS_GetScriptOrModuleName(ctx, 1);
if (basename_atom == JS_ATOM_NULL) {
return JS_ThrowTypeError(ctx, "could not determine calling script or module name");
}
basename = JS_AtomToCString(ctx, basename_atom);
JS_FreeAtom(ctx, basename_atom);
if (!basename)
goto fail;
/* module name */
filename = JS_ToCString(ctx, argv[0]);
if (!filename)
goto fail;
args = malloc(sizeof(*args));
if (!args)
goto oom_fail;
memset(args, 0, sizeof(*args));
args->filename = strdup(filename);
args->basename = strdup(basename);
/* ports */
args->recv_pipe = js_new_message_pipe();
if (!args->recv_pipe)
goto oom_fail;
args->send_pipe = js_new_message_pipe();
if (!args->send_pipe)
goto oom_fail;
args->strip_flags = JS_GetStripInfo(rt);
obj = js_worker_ctor_internal(ctx, new_target,
args->send_pipe, args->recv_pipe);
if (JS_IsException(obj))
goto fail;
pthread_attr_init(&attr);
/* no join at the end */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&tid, &attr, worker_func, args);
pthread_attr_destroy(&attr);
if (ret != 0) {
JS_ThrowTypeError(ctx, "could not create worker");
goto fail;
}
JS_FreeCString(ctx, basename);
JS_FreeCString(ctx, filename);
return obj;
oom_fail:
JS_ThrowOutOfMemory(ctx);
fail:
JS_FreeCString(ctx, basename);
JS_FreeCString(ctx, filename);
if (args) {
free(args->filename);
free(args->basename);
js_free_message_pipe(args->recv_pipe);
js_free_message_pipe(args->send_pipe);
free(args);
}
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessagePipe *ps;
size_t data_len, sab_tab_len, i;
uint8_t *data;
JSWorkerMessage *msg;
uint8_t **sab_tab;
if (!worker)
return JS_EXCEPTION;
data = JS_WriteObject2(ctx, &data_len, argv[0],
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
&sab_tab, &sab_tab_len);
if (!data)
return JS_EXCEPTION;
msg = malloc(sizeof(*msg));
if (!msg)
goto fail;
msg->data = NULL;
msg->sab_tab = NULL;
/* must reallocate because the allocator may be different */
msg->data = malloc(data_len);
if (!msg->data)
goto fail;
memcpy(msg->data, data, data_len);
msg->data_len = data_len;
if (sab_tab_len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (!msg->sab_tab)
goto fail;
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
}
msg->sab_tab_len = sab_tab_len;
js_free(ctx, data);
js_free(ctx, sab_tab);
/* increment the SAB reference counts */
for(i = 0; i < msg->sab_tab_len; i++) {
js_sab_dup(NULL, msg->sab_tab[i]);
}
ps = worker->send_pipe;
pthread_mutex_lock(&ps->mutex);
/* indicate that data is present */
if (list_empty(&ps->msg_queue))
js_waker_signal(&ps->waker);
list_add_tail(&msg->link, &ps->msg_queue);
pthread_mutex_unlock(&ps->mutex);
return JS_UNDEFINED;
fail:
if (msg) {
free(msg->data);
free(msg->sab_tab);
free(msg);
}
js_free(ctx, data);
js_free(ctx, sab_tab);
return JS_EXCEPTION;
}
static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val,
JSValueConst func)
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessageHandler *port;
if (!worker)
return JS_EXCEPTION;
port = worker->msg_handler;
if (JS_IsNull(func)) {
if (port) {
js_free_port(rt, port);
worker->msg_handler = NULL;
}
} else {
if (!JS_IsFunction(ctx, func))
return JS_ThrowTypeError(ctx, "not a function");
if (!port) {
port = js_mallocz(ctx, sizeof(*port));
if (!port)
return JS_EXCEPTION;
port->recv_pipe = js_dup_message_pipe(worker->recv_pipe);
port->on_message_func = JS_NULL;
list_add_tail(&port->link, &ts->port_list);
worker->msg_handler = port;
}
JS_FreeValue(ctx, port->on_message_func);
port->on_message_func = JS_DupValue(ctx, func);
}
return JS_UNDEFINED;
}
static JSValue js_worker_get_onmessage(JSContext *ctx, JSValueConst this_val)
{
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessageHandler *port;
if (!worker)
return JS_EXCEPTION;
port = worker->msg_handler;
if (port) {
return JS_DupValue(ctx, port->on_message_func);
} else {
return JS_NULL;
}
}
static const JSCFunctionListEntry js_worker_proto_funcs[] = {
JS_CFUNC_DEF("postMessage", 1, js_worker_postMessage ),
JS_CGETSET_DEF("onmessage", js_worker_get_onmessage, js_worker_set_onmessage ),
};
#endif /* USE_WORKER */
void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt))
{
#ifdef USE_WORKER
js_worker_new_context_func = func;
#endif
}
#if defined(_WIN32)
#define OS_PLATFORM "win32"
#elif defined(__APPLE__)
#define OS_PLATFORM "darwin"
#elif defined(EMSCRIPTEN)
#define OS_PLATFORM "js"
#else
#define OS_PLATFORM "linux"
#endif
#define OS_FLAG(x) JS_PROP_INT32_DEF(#x, x, JS_PROP_CONFIGURABLE )
static const JSCFunctionListEntry js_os_funcs[] = {
JS_CFUNC_DEF("open", 2, js_os_open ),
OS_FLAG(O_RDONLY),
OS_FLAG(O_WRONLY),
OS_FLAG(O_RDWR),
OS_FLAG(O_APPEND),
OS_FLAG(O_CREAT),
OS_FLAG(O_EXCL),
OS_FLAG(O_TRUNC),
#if defined(_WIN32)
OS_FLAG(O_BINARY),
OS_FLAG(O_TEXT),
#endif
JS_CFUNC_DEF("close", 1, js_os_close ),
JS_CFUNC_DEF("seek", 3, js_os_seek ),
JS_CFUNC_MAGIC_DEF("read", 4, js_os_read_write, 0 ),
JS_CFUNC_MAGIC_DEF("write", 4, js_os_read_write, 1 ),
JS_CFUNC_DEF("isatty", 1, js_os_isatty ),
JS_CFUNC_DEF("ttyGetWinSize", 1, js_os_ttyGetWinSize ),
JS_CFUNC_DEF("ttySetRaw", 1, js_os_ttySetRaw ),
JS_CFUNC_DEF("remove", 1, js_os_remove ),
JS_CFUNC_DEF("rename", 2, js_os_rename ),
JS_CFUNC_MAGIC_DEF("setReadHandler", 2, js_os_setReadHandler, 0 ),
JS_CFUNC_MAGIC_DEF("setWriteHandler", 2, js_os_setReadHandler, 1 ),
JS_CFUNC_DEF("signal", 2, js_os_signal ),
OS_FLAG(SIGINT),
OS_FLAG(SIGABRT),
OS_FLAG(SIGFPE),
OS_FLAG(SIGILL),
OS_FLAG(SIGSEGV),
OS_FLAG(SIGTERM),
#if !defined(_WIN32)
OS_FLAG(SIGQUIT),
OS_FLAG(SIGPIPE),
OS_FLAG(SIGALRM),
OS_FLAG(SIGUSR1),
OS_FLAG(SIGUSR2),
OS_FLAG(SIGCHLD),
OS_FLAG(SIGCONT),
OS_FLAG(SIGSTOP),
OS_FLAG(SIGTSTP),
OS_FLAG(SIGTTIN),
OS_FLAG(SIGTTOU),
#endif
JS_CFUNC_DEF("now", 0, js_os_now ),
JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ),
JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ),
JS_CFUNC_DEF("sleepAsync", 1, js_os_sleepAsync ),
JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ),
JS_CFUNC_DEF("getcwd", 0, js_os_getcwd ),
JS_CFUNC_DEF("chdir", 0, js_os_chdir ),
JS_CFUNC_DEF("mkdir", 1, js_os_mkdir ),
JS_CFUNC_DEF("readdir", 1, js_os_readdir ),
/* st_mode constants */
OS_FLAG(S_IFMT),
OS_FLAG(S_IFIFO),
OS_FLAG(S_IFCHR),
OS_FLAG(S_IFDIR),
OS_FLAG(S_IFBLK),
OS_FLAG(S_IFREG),
#if !defined(_WIN32)
OS_FLAG(S_IFSOCK),
OS_FLAG(S_IFLNK),
OS_FLAG(S_ISGID),
OS_FLAG(S_ISUID),
#endif
JS_CFUNC_MAGIC_DEF("stat", 1, js_os_stat, 0 ),
JS_CFUNC_DEF("utimes", 3, js_os_utimes ),
JS_CFUNC_DEF("sleep", 1, js_os_sleep ),
JS_CFUNC_DEF("realpath", 1, js_os_realpath ),
#if !defined(_WIN32)
JS_CFUNC_MAGIC_DEF("lstat", 1, js_os_stat, 1 ),
JS_CFUNC_DEF("symlink", 2, js_os_symlink ),
JS_CFUNC_DEF("readlink", 1, js_os_readlink ),
JS_CFUNC_DEF("exec", 1, js_os_exec ),
JS_CFUNC_DEF("getpid", 0, js_os_getpid ),
JS_CFUNC_DEF("waitpid", 2, js_os_waitpid ),
OS_FLAG(WNOHANG),
JS_CFUNC_DEF("pipe", 0, js_os_pipe ),
JS_CFUNC_DEF("kill", 2, js_os_kill ),
JS_CFUNC_DEF("dup", 1, js_os_dup ),
JS_CFUNC_DEF("dup2", 2, js_os_dup2 ),
#endif
};
static int js_os_init(JSContext *ctx, JSModuleDef *m)
{
os_poll_func = js_os_poll;
#ifdef USE_WORKER
{
JSRuntime *rt = JS_GetRuntime(ctx);
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSValue proto, obj;
/* Worker class */
JS_NewClassID(&js_worker_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_worker_class_id, &js_worker_class);
proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_worker_proto_funcs, countof(js_worker_proto_funcs));
obj = JS_NewCFunction2(ctx, js_worker_ctor, "Worker", 1,
JS_CFUNC_constructor, 0);
JS_SetConstructor(ctx, obj, proto);
JS_SetClassProto(ctx, js_worker_class_id, proto);
/* set 'Worker.parent' if necessary */
if (ts->recv_pipe && ts->send_pipe) {
JS_DefinePropertyValueStr(ctx, obj, "parent",
js_worker_ctor_internal(ctx, JS_UNDEFINED, ts->recv_pipe, ts->send_pipe),
JS_PROP_C_W_E);
}
JS_SetModuleExport(ctx, m, "Worker", obj);
}
#endif /* USE_WORKER */
return JS_SetModuleExportList(ctx, m, js_os_funcs,
countof(js_os_funcs));
}
JSModuleDef *js_init_module_os(JSContext *ctx, const char *module_name)
{
JSModuleDef *m;
m = JS_NewCModule(ctx, module_name, js_os_init);
if (!m)
return NULL;
JS_AddModuleExportList(ctx, m, js_os_funcs, countof(js_os_funcs));
#ifdef USE_WORKER
JS_AddModuleExport(ctx, m, "Worker");
#endif
return m;
}
/**********************************************************/
static JSValue js_print(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
int i;
const char *str;
size_t len;
for(i = 0; i < argc; i++) {
if (i != 0)
putchar(' ');
str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str)
return JS_EXCEPTION;
fwrite(str, 1, len, stdout);
JS_FreeCString(ctx, str);
}
putchar('\n');
return JS_UNDEFINED;
}
static JSValue js_console_log(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue ret;
ret = js_print(ctx, this_val, argc, argv);
fflush(stdout);
return ret;
}
void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
{
JSValue global_obj, console, args, performance;
int i;
/* XXX: should these global definitions be enumerable? */
global_obj = JS_GetGlobalObject(ctx);
console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, console, "log",
JS_NewCFunction(ctx, js_console_log, "log", 1));
JS_SetPropertyStr(ctx, global_obj, "console", console);
performance = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, performance, "now",
JS_NewCFunction(ctx, js_os_now, "now", 0));
JS_SetPropertyStr(ctx, global_obj, "performance", performance);
/* same methods as the mozilla JS shell */
if (argc >= 0) {
args = JS_NewArray(ctx);
for(i = 0; i < argc; i++) {
JS_SetPropertyUint32(ctx, args, i, JS_NewString(ctx, argv[i]));
}
JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args);
}
JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__loadScript",
JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1));
JS_FreeValue(ctx, global_obj);
}
void js_std_init_handlers(JSRuntime *rt)
{
JSThreadState *ts;
ts = malloc(sizeof(*ts));
if (!ts) {
fprintf(stderr, "Could not allocate memory for the worker");
exit(1);
}
memset(ts, 0, sizeof(*ts));
init_list_head(&ts->os_rw_handlers);
init_list_head(&ts->os_signal_handlers);
init_list_head(&ts->os_timers);
init_list_head(&ts->port_list);
ts->next_timer_id = 1;
JS_SetRuntimeOpaque(rt, ts);
#ifdef USE_WORKER
/* set the SharedArrayBuffer memory handlers */
{
JSSharedArrayBufferFunctions sf;
memset(&sf, 0, sizeof(sf));
sf.sab_alloc = js_sab_alloc;
sf.sab_free = js_sab_free;
sf.sab_dup = js_sab_dup;
JS_SetSharedArrayBufferFunctions(rt, &sf);
}
#endif
}
void js_std_free_handlers(JSRuntime *rt)
{
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
struct list_head *el, *el1;
list_for_each_safe(el, el1, &ts->os_rw_handlers) {
JSOSRWHandler *rh = list_entry(el, JSOSRWHandler, link);
free_rw_handler(rt, rh);
}
list_for_each_safe(el, el1, &ts->os_signal_handlers) {
JSOSSignalHandler *sh = list_entry(el, JSOSSignalHandler, link);
free_sh(rt, sh);
}
list_for_each_safe(el, el1, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link);
free_timer(rt, th);
}
#ifdef USE_WORKER
/* XXX: free port_list ? */
js_free_message_pipe(ts->recv_pipe);
js_free_message_pipe(ts->send_pipe);
#endif
free(ts);
JS_SetRuntimeOpaque(rt, NULL); /* fail safe */
}
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val)
{
const char *str;
str = JS_ToCString(ctx, val);
if (str) {
fprintf(f, "%s\n", str);
JS_FreeCString(ctx, str);
} else {
fprintf(f, "[exception]\n");
}
}
static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
{
JSValue val;
BOOL is_error;
is_error = JS_IsError(ctx, exception_val);
js_dump_obj(ctx, stderr, exception_val);
if (is_error) {
val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(val)) {
js_dump_obj(ctx, stderr, val);
}
JS_FreeValue(ctx, val);
}
}
void js_std_dump_error(JSContext *ctx)
{
JSValue exception_val;
exception_val = JS_GetException(ctx);
js_std_dump_error1(ctx, exception_val);
JS_FreeValue(ctx, exception_val);
}
void js_std_promise_rejection_tracker(JSContext *ctx, JSValueConst promise,
JSValueConst reason,
BOOL is_handled, void *opaque)
{
if (!is_handled) {
fprintf(stderr, "Possibly unhandled promise rejection: ");
js_std_dump_error1(ctx, reason);
exit(1);
}
}
/* main loop which calls the user JS callbacks */
void js_std_loop(JSContext *ctx)
{
JSContext *ctx1;
int err;
for(;;) {
/* execute the pending jobs */
for(;;) {
err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
if (err <= 0) {
if (err < 0) {
js_std_dump_error(ctx1);
}
break;
}
}
if (!os_poll_func || os_poll_func(ctx))
break;
}
}
/* Wait for a promise and execute pending jobs while waiting for
it. Return the promise result or JS_EXCEPTION in case of promise
rejection. */
JSValue js_std_await(JSContext *ctx, JSValue obj)
{
JSValue ret;
int state;
for(;;) {
state = JS_PromiseState(ctx, obj);
if (state == JS_PROMISE_FULFILLED) {
ret = JS_PromiseResult(ctx, obj);
JS_FreeValue(ctx, obj);
break;
} else if (state == JS_PROMISE_REJECTED) {
ret = JS_Throw(ctx, JS_PromiseResult(ctx, obj));
JS_FreeValue(ctx, obj);
break;
} else if (state == JS_PROMISE_PENDING) {
JSContext *ctx1;
int err;
err = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
if (err < 0) {
js_std_dump_error(ctx1);
}
if (os_poll_func)
os_poll_func(ctx);
} else {
/* not a promise */
ret = obj;
break;
}
}
return ret;
}
void js_std_eval_binary(JSContext *ctx, const uint8_t *buf, size_t buf_len,
int load_only)
{
JSValue obj, val;
obj = JS_ReadObject(ctx, buf, buf_len, JS_READ_OBJ_BYTECODE);
if (JS_IsException(obj))
goto exception;
if (load_only) {
if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
js_module_set_import_meta(ctx, obj, FALSE, FALSE);
}
} else {
if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
if (JS_ResolveModule(ctx, obj) < 0) {
JS_FreeValue(ctx, obj);
goto exception;
}
js_module_set_import_meta(ctx, obj, FALSE, TRUE);
val = JS_EvalFunction(ctx, obj);
val = js_std_await(ctx, val);
} else {
val = JS_EvalFunction(ctx, obj);
}
if (JS_IsException(val)) {
exception:
js_std_dump_error(ctx);
exit(1);
}
JS_FreeValue(ctx, val);
}
}
|