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 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862
|
/*
* API calls related to general value stack manipulation: resizing the value
* stack, pushing and popping values, type checking and reading values,
* coercing values, etc.
*
* Also contains internal functions (such as duk_get_tval()), defined
* in duk_api_internal.h, with semantics similar to the public API.
*/
/* XXX: repetition of stack pre-checks -> helper or macro or inline */
/* XXX: shared api error strings, and perhaps even throw code for rare cases? */
#include "duk_internal.h"
/*
* Forward declarations
*/
DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_uint_t flags, duk_small_uint_t proto_bidx);
/*
* Global state for working around missing variadic macros
*/
#if !defined(DUK_USE_VARIADIC_MACROS)
DUK_EXTERNAL const char *duk_api_global_filename = NULL;
DUK_EXTERNAL duk_int_t duk_api_global_line = 0;
#endif
/*
* Misc helpers
*/
DUK_LOCAL const char * const duk__symbol_type_strings[4] = {
"hidden", "global", "local", "wellknown"
};
#if !defined(DUK_USE_PACKED_TVAL)
DUK_LOCAL const duk_uint_t duk__type_from_tag[] = {
DUK_TYPE_NUMBER,
DUK_TYPE_NUMBER, /* fastint */
DUK_TYPE_UNDEFINED,
DUK_TYPE_NULL,
DUK_TYPE_BOOLEAN,
DUK_TYPE_POINTER,
DUK_TYPE_LIGHTFUNC,
DUK_TYPE_NONE,
DUK_TYPE_STRING,
DUK_TYPE_OBJECT,
DUK_TYPE_BUFFER,
};
DUK_LOCAL const duk_uint_t duk__type_mask_from_tag[] = {
DUK_TYPE_MASK_NUMBER,
DUK_TYPE_MASK_NUMBER, /* fastint */
DUK_TYPE_MASK_UNDEFINED,
DUK_TYPE_MASK_NULL,
DUK_TYPE_MASK_BOOLEAN,
DUK_TYPE_MASK_POINTER,
DUK_TYPE_MASK_LIGHTFUNC,
DUK_TYPE_MASK_NONE,
DUK_TYPE_MASK_STRING,
DUK_TYPE_MASK_OBJECT,
DUK_TYPE_MASK_BUFFER,
};
#endif /* !DUK_USE_PACKED_TVAL */
/* Assert that there's room for one value. */
#define DUK__ASSERT_SPACE() do { \
DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
} while (0)
/* Check that there's room to push one value. */
#if defined(DUK_USE_VALSTACK_UNSAFE)
/* Faster but value stack overruns are memory unsafe. */
#define DUK__CHECK_SPACE() DUK__ASSERT_SPACE()
#else
#define DUK__CHECK_SPACE() do { \
if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \
DUK_ERROR_RANGE_PUSH_BEYOND(thr); \
} \
} while (0)
#endif
DUK_LOCAL duk_small_uint_t duk__get_symbol_type(duk_hstring *h) {
const duk_uint8_t *data;
duk_size_t len;
DUK_ASSERT(h != NULL);
DUK_ASSERT(DUK_HSTRING_HAS_SYMBOL(h));
DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h) >= 1); /* always true, symbol prefix */
data = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
len = DUK_HSTRING_GET_BYTELEN(h);
DUK_ASSERT(len >= 1);
/* XXX: differentiate between 0x82 and 0xff (hidden vs. internal?)? */
if (data[0] == 0xffU) {
return DUK_SYMBOL_TYPE_HIDDEN;
} else if (data[0] == 0x82U) {
return DUK_SYMBOL_TYPE_HIDDEN;
} else if (data[0] == 0x80U) {
return DUK_SYMBOL_TYPE_GLOBAL;
} else if (data[len - 1] != 0xffU) {
return DUK_SYMBOL_TYPE_LOCAL;
} else {
return DUK_SYMBOL_TYPE_WELLKNOWN;
}
}
DUK_LOCAL const char *duk__get_symbol_type_string(duk_hstring *h) {
duk_small_uint_t idx;
idx = duk__get_symbol_type(h);
DUK_ASSERT(idx < sizeof(duk__symbol_type_strings));
return duk__symbol_type_strings[idx];
}
DUK_LOCAL_DECL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t tag);
DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value, duk_bool_t require) {
duk_tval *tv;
duk_small_int_t c;
duk_double_t d;
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
/*
* Special cases like NaN and +/- Infinity are handled explicitly
* because a plain C coercion from double to int handles these cases
* in undesirable ways. For instance, NaN may coerce to INT_MIN
* (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
*
* This double-to-int coercion differs from ToInteger() because it
* has a finite range (ToInteger() allows e.g. +/- Infinity). It
* also differs from ToInt32() because the INT_MIN/INT_MAX clamping
* depends on the size of the int type on the platform. In particular,
* on platforms with a 64-bit int type, the full range is allowed.
*/
#if defined(DUK_USE_FASTINT)
if (DUK_TVAL_IS_FASTINT(tv)) {
duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
#if (DUK_INT_MAX <= 0x7fffffffL)
/* Clamping only necessary for 32-bit ints. */
if (t < DUK_INT_MIN) {
t = DUK_INT_MIN;
} else if (t > DUK_INT_MAX) {
t = DUK_INT_MAX;
}
#endif
return (duk_int_t) t;
}
#endif
if (DUK_TVAL_IS_NUMBER(tv)) {
d = DUK_TVAL_GET_NUMBER(tv);
c = (duk_small_int_t) DUK_FPCLASSIFY(d);
if (c == DUK_FP_NAN) {
return 0;
} else if (d < (duk_double_t) DUK_INT_MIN) {
/* covers -Infinity */
return DUK_INT_MIN;
} else if (d > (duk_double_t) DUK_INT_MAX) {
/* covers +Infinity */
return DUK_INT_MAX;
} else {
/* coerce towards zero */
return (duk_int_t) d;
}
}
if (require) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
DUK_WO_NORETURN(return 0;);
}
return def_value;
}
DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value, duk_bool_t require) {
duk_tval *tv;
duk_small_int_t c;
duk_double_t d;
/* Same as above but for unsigned int range. */
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
#if defined(DUK_USE_FASTINT)
if (DUK_TVAL_IS_FASTINT(tv)) {
duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
if (t < 0) {
t = 0;
}
#if (DUK_UINT_MAX <= 0xffffffffUL)
/* Clamping only necessary for 32-bit ints. */
else if (t > DUK_UINT_MAX) {
t = DUK_UINT_MAX;
}
#endif
return (duk_uint_t) t;
}
#endif
if (DUK_TVAL_IS_NUMBER(tv)) {
d = DUK_TVAL_GET_NUMBER(tv);
c = (duk_small_int_t) DUK_FPCLASSIFY(d);
if (c == DUK_FP_NAN) {
return 0;
} else if (d < 0.0) {
/* covers -Infinity */
return (duk_uint_t) 0;
} else if (d > (duk_double_t) DUK_UINT_MAX) {
/* covers +Infinity */
return (duk_uint_t) DUK_UINT_MAX;
} else {
/* coerce towards zero */
return (duk_uint_t) d;
}
}
if (require) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
DUK_WO_NORETURN(return 0;);
}
return def_value;
}
/*
* Stack index validation/normalization and getting a stack duk_tval ptr.
*
* These are called by many API entrypoints so the implementations must be
* fast and "inlined".
*
* There's some repetition because of this; keep the functions in sync.
*/
DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t vs_size;
duk_uidx_t uidx;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
/* Care must be taken to avoid pointer wrapping in the index
* validation. For instance, on a 32-bit platform with 8-byte
* duk_tval the index 0x20000000UL would wrap the memory space
* once.
*/
/* Assume value stack sizes (in elements) fits into duk_idx_t. */
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (idx < 0) {
uidx = vs_size + (duk_uidx_t) idx;
} else {
/* since index non-negative */
DUK_ASSERT(idx != DUK_INVALID_INDEX);
uidx = (duk_uidx_t) idx;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uidx < vs_size)) {
return (duk_idx_t) uidx;
}
return DUK_INVALID_INDEX;
}
DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t vs_size;
duk_uidx_t uidx;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (idx < 0) {
uidx = vs_size + (duk_uidx_t) idx;
} else {
DUK_ASSERT(idx != DUK_INVALID_INDEX);
uidx = (duk_uidx_t) idx;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uidx < vs_size)) {
return (duk_idx_t) uidx;
}
DUK_ERROR_RANGE_INDEX(thr, idx);
DUK_WO_NORETURN(return 0;);
}
DUK_INTERNAL duk_tval *duk_get_tval(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t vs_size;
duk_uidx_t uidx;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
if (idx < 0) {
uidx = vs_size + (duk_uidx_t) idx;
} else {
DUK_ASSERT(idx != DUK_INVALID_INDEX);
uidx = (duk_uidx_t) idx;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uidx < vs_size)) {
return thr->valstack_bottom + uidx;
}
return NULL;
}
/* Variant of duk_get_tval() which is guaranteed to return a valid duk_tval
* pointer. When duk_get_tval() would return NULL, this variant returns a
* pointer to a duk_tval with tag DUK_TAG_UNUSED. This allows the call site
* to avoid an unnecessary NULL check which sometimes leads to better code.
* The return duk_tval is read only (at least for the UNUSED value).
*/
DUK_LOCAL const duk_tval_unused duk__const_tval_unused = DUK_TVAL_UNUSED_INITIALIZER();
DUK_INTERNAL duk_tval *duk_get_tval_or_unused(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval(thr, idx);
if (tv != NULL) {
return tv;
}
return (duk_tval *) DUK_LOSE_CONST(&duk__const_tval_unused);
}
DUK_INTERNAL duk_tval *duk_require_tval(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t vs_size;
duk_uidx_t uidx;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
/* Use unsigned arithmetic to optimize comparison. */
if (idx < 0) {
uidx = vs_size + (duk_uidx_t) idx;
} else {
DUK_ASSERT(idx != DUK_INVALID_INDEX);
uidx = (duk_uidx_t) idx;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
if (DUK_LIKELY(uidx < vs_size)) {
return thr->valstack_bottom + uidx;
}
DUK_ERROR_RANGE_INDEX(thr, idx);
DUK_WO_NORETURN(return NULL;);
}
/* Non-critical. */
DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
return (duk_normalize_index(thr, idx) >= 0);
}
/* Non-critical. */
DUK_EXTERNAL void duk_require_valid_index(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
if (DUK_UNLIKELY(duk_normalize_index(thr, idx) < 0)) {
DUK_ERROR_RANGE_INDEX(thr, idx);
DUK_WO_NORETURN(return;);
}
}
/*
* Value stack top handling
*/
DUK_EXTERNAL duk_idx_t duk_get_top(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
}
/* Internal helper to get current top but to require a minimum top value
* (TypeError if not met).
*/
DUK_INTERNAL duk_idx_t duk_get_top_require_min(duk_hthread *thr, duk_idx_t min_top) {
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
if (DUK_UNLIKELY(ret < min_top)) {
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return 0;);
}
return ret;
}
/* Set stack top within currently allocated range, but don't reallocate.
* This is performance critical especially for call handling, so whenever
* changing, profile and look at generated code.
*/
DUK_EXTERNAL void duk_set_top(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t vs_size;
duk_uidx_t vs_limit;
duk_uidx_t uidx;
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_INVALID_INDEX < 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom);
if (idx < 0) {
/* Negative indices are always within allocated stack but
* must not go below zero index.
*/
uidx = vs_size + (duk_uidx_t) idx;
} else {
/* Positive index can be higher than valstack top but must
* not go above allocated stack (equality is OK).
*/
uidx = (duk_uidx_t) idx;
}
/* DUK_INVALID_INDEX won't be accepted as a valid index. */
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit);
#if defined(DUK_USE_VALSTACK_UNSAFE)
DUK_ASSERT(uidx <= vs_limit);
DUK_UNREF(vs_limit);
#else
if (DUK_UNLIKELY(uidx > vs_limit)) {
DUK_ERROR_RANGE_INDEX(thr, idx);
DUK_WO_NORETURN(return;);
}
#endif
DUK_ASSERT(uidx <= vs_limit);
/* Handle change in value stack top. Respect value stack
* initialization policy: 'undefined' above top. Note that
* DECREF may cause a side effect that reallocates valstack,
* so must relookup after DECREF.
*/
if (uidx >= vs_size) {
/* Stack size increases or stays the same. */
#if defined(DUK_USE_ASSERTIONS)
duk_uidx_t count;
count = uidx - vs_size;
while (count != 0) {
count--;
tv = thr->valstack_top + count;
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
}
#endif
thr->valstack_top = thr->valstack_bottom + uidx;
} else {
/* Stack size decreases. */
#if defined(DUK_USE_REFERENCE_COUNTING)
duk_uidx_t count;
duk_tval *tv_end;
count = vs_size - uidx;
DUK_ASSERT(count > 0);
tv = thr->valstack_top;
tv_end = tv - count;
DUK_ASSERT(tv > tv_end); /* Because count > 0. */
do {
tv--;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
} while (tv != tv_end);
thr->valstack_top = tv_end;
DUK_REFZERO_CHECK_FAST(thr);
#else /* DUK_USE_REFERENCE_COUNTING */
duk_uidx_t count;
duk_tval *tv_end;
count = vs_size - uidx;
tv = thr->valstack_top;
tv_end = tv - count;
DUK_ASSERT(tv > tv_end);
do {
tv--;
DUK_TVAL_SET_UNDEFINED(tv);
} while (tv != tv_end);
thr->valstack_top = tv_end;
#endif /* DUK_USE_REFERENCE_COUNTING */
}
}
/* Internal variant with a non-negative index and no runtime size checks. */
#if defined(DUK_USE_PREFER_SIZE)
DUK_INTERNAL void duk_set_top_unsafe(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
duk_set_top(thr, idx);
}
#else /* DUK_USE_PREFER_SIZE */
DUK_INTERNAL void duk_set_top_unsafe(duk_hthread *thr, duk_idx_t idx) {
duk_uidx_t uidx;
duk_uidx_t vs_size;
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
DUK_ASSERT(idx >= 0);
DUK_ASSERT(idx <= (duk_idx_t) (thr->valstack_end - thr->valstack_bottom));
/* XXX: byte arithmetic */
uidx = (duk_uidx_t) idx;
vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
if (uidx >= vs_size) {
/* Stack size increases or stays the same. */
#if defined(DUK_USE_ASSERTIONS)
duk_uidx_t count;
count = uidx - vs_size;
while (count != 0) {
count--;
tv = thr->valstack_top + count;
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
}
#endif
thr->valstack_top = thr->valstack_bottom + uidx;
} else {
/* Stack size decreases. */
#if defined(DUK_USE_REFERENCE_COUNTING)
duk_uidx_t count;
duk_tval *tv_end;
count = vs_size - uidx;
DUK_ASSERT(count > 0);
tv = thr->valstack_top;
tv_end = tv - count;
DUK_ASSERT(tv > tv_end); /* Because count > 0. */
do {
tv--;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
} while (tv != tv_end);
thr->valstack_top = tv_end;
DUK_REFZERO_CHECK_FAST(thr);
#else /* DUK_USE_REFERENCE_COUNTING */
duk_uidx_t count;
duk_tval *tv_end;
count = vs_size - uidx;
tv = thr->valstack_top;
tv_end = tv - count;
DUK_ASSERT(tv > tv_end);
do {
tv--;
DUK_TVAL_SET_UNDEFINED(tv);
} while (tv != tv_end);
thr->valstack_top = tv_end;
#endif /* DUK_USE_REFERENCE_COUNTING */
}
}
#endif /* DUK_USE_PREFER_SIZE */
/* Internal helper: set top to 'top', and set [idx_wipe_start,top[ to
* 'undefined' (doing nothing if idx_wipe_start == top). Indices are
* positive and within value stack reserve. This is used by call handling.
*/
DUK_INTERNAL void duk_set_top_and_wipe(duk_hthread *thr, duk_idx_t top, duk_idx_t idx_wipe_start) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(top >= 0);
DUK_ASSERT(idx_wipe_start >= 0);
DUK_ASSERT(idx_wipe_start <= top);
DUK_ASSERT(thr->valstack_bottom + top <= thr->valstack_end);
DUK_ASSERT(thr->valstack_bottom + idx_wipe_start <= thr->valstack_end);
duk_set_top_unsafe(thr, idx_wipe_start);
duk_set_top_unsafe(thr, top);
}
DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_hthread *thr) {
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
if (DUK_UNLIKELY(ret < 0)) {
/* Return invalid index; if caller uses this without checking
* in another API call, the index won't map to a valid stack
* entry.
*/
return DUK_INVALID_INDEX;
}
return ret;
}
/* Internal variant: call assumes there is at least one element on the value
* stack frame; this is only asserted for.
*/
DUK_INTERNAL duk_idx_t duk_get_top_index_unsafe(duk_hthread *thr) {
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
return ret;
}
DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_hthread *thr) {
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
if (DUK_UNLIKELY(ret < 0)) {
DUK_ERROR_RANGE_INDEX(thr, -1);
DUK_WO_NORETURN(return 0;);
}
return ret;
}
/*
* Value stack resizing.
*
* This resizing happens above the current "top": the value stack can be
* grown or shrunk, but the "top" is not affected. The value stack cannot
* be resized to a size below the current reserve.
*
* The low level reallocation primitive must carefully recompute all value
* stack pointers, and must also work if ALL pointers are NULL. The resize
* is quite tricky because the valstack realloc may cause a mark-and-sweep,
* which may run finalizers. Running finalizers may resize the valstack
* recursively (the same value stack we're working on). So, after realloc
* returns, we know that the valstack bottom, top, and reserve should still
* be the same (there should not be live values above the "top"), but its
* underlying size, alloc_end, and base pointer may have changed.
*
* 'new_size' is known to be <= DUK_USE_VALSTACK_LIMIT, which ensures that
* size_t and pointer arithmetic won't wrap in duk__resize_valstack().
*/
/* Low level valstack resize primitive, used for both grow and shrink. All
* adjustments for slack etc have already been done. Doesn't throw but does
* have allocation side effects.
*/
DUK_LOCAL DUK_COLD DUK_NOINLINE duk_bool_t duk__resize_valstack(duk_hthread *thr, duk_size_t new_size) {
duk_tval *pre_valstack;
duk_tval *pre_bottom;
duk_tval *pre_top;
duk_tval *pre_end;
duk_tval *pre_alloc_end;
duk_ptrdiff_t ptr_diff;
duk_tval *new_valstack;
duk_size_t new_alloc_size;
duk_tval *tv_prev_alloc_end;
duk_tval *p;
DUK_ASSERT_HTHREAD_VALID(thr);
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
DUK_ASSERT(new_size <= DUK_USE_VALSTACK_LIMIT); /* valstack limit caller has check, prevents wrapping */
DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
/* Pre-realloc pointer copies for asserts and debug logs. */
pre_valstack = thr->valstack;
pre_bottom = thr->valstack_bottom;
pre_top = thr->valstack_top;
pre_end = thr->valstack_end;
pre_alloc_end = thr->valstack_alloc_end;
DUK_UNREF(pre_valstack);
DUK_UNREF(pre_bottom);
DUK_UNREF(pre_top);
DUK_UNREF(pre_end);
DUK_UNREF(pre_alloc_end);
/* If finalizer torture enabled, force base pointer change every time
* when it would be allowed.
*/
#if defined(DUK_USE_FINALIZER_TORTURE)
if (thr->heap->pf_prevent_count == 0) {
duk_hthread_valstack_torture_realloc(thr);
}
#endif
/* Allocate a new valstack using DUK_REALLOC_DIRECT() to deal with
* a side effect changing the base pointer.
*/
new_alloc_size = sizeof(duk_tval) * new_size;
new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
if (DUK_UNLIKELY(new_valstack == NULL)) {
/* Because new_size != 0, if condition doesn't need to be
* (new_valstack != NULL || new_size == 0).
*/
DUK_ASSERT(new_size != 0);
DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
(unsigned long) new_size, (unsigned long) new_alloc_size));
return 0;
}
/* Debug log any changes in pointer(s) by side effects. These don't
* necessarily imply any incorrect behavior, but should be rare in
* practice.
*/
#if defined(DUK_USE_DEBUG)
if (thr->valstack != pre_valstack) {
DUK_D(DUK_DPRINT("valstack base pointer changed during valstack resize: %p -> %p",
(void *) pre_valstack, (void *) thr->valstack));
}
if (thr->valstack_bottom != pre_bottom) {
DUK_D(DUK_DPRINT("valstack bottom pointer changed during valstack resize: %p -> %p",
(void *) pre_bottom, (void *) thr->valstack_bottom));
}
if (thr->valstack_top != pre_top) {
DUK_D(DUK_DPRINT("valstack top pointer changed during valstack resize: %p -> %p",
(void *) pre_top, (void *) thr->valstack_top));
}
if (thr->valstack_end != pre_end) {
DUK_D(DUK_DPRINT("valstack end pointer changed during valstack resize: %p -> %p",
(void *) pre_end, (void *) thr->valstack_end));
}
if (thr->valstack_alloc_end != pre_alloc_end) {
DUK_D(DUK_DPRINT("valstack alloc_end pointer changed during valstack resize: %p -> %p",
(void *) pre_alloc_end, (void *) thr->valstack_alloc_end));
}
#endif
/* Assertions: offsets for bottom, top, and end (reserve) must not
* have changed even with side effects because they are always
* restored in unwind. For alloc_end there's no guarantee: it may
* have grown or shrunk (but remain above 'end').
*/
DUK_ASSERT(thr->valstack_bottom - thr->valstack == pre_bottom - pre_valstack);
DUK_ASSERT(thr->valstack_top - thr->valstack == pre_top - pre_valstack);
DUK_ASSERT(thr->valstack_end - thr->valstack == pre_end - pre_valstack);
DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
/* Write new pointers. Most pointers can be handled as a pointer
* difference.
*/
ptr_diff = (duk_ptrdiff_t) ((duk_uint8_t *) new_valstack - (duk_uint8_t *) thr->valstack);
tv_prev_alloc_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_alloc_end + ptr_diff);
thr->valstack = new_valstack;
thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + ptr_diff);
thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_top + ptr_diff);
thr->valstack_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_end + ptr_diff);
thr->valstack_alloc_end = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + new_alloc_size);
/* Assertions: pointer sanity after pointer updates. */
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
DUK_D(DUK_DPRINT("resized valstack %lu -> %lu elements (%lu -> %lu bytes): "
"base=%p -> %p, bottom=%p -> %p (%ld), top=%p -> %p (%ld), "
"end=%p -> %p (%ld), alloc_end=%p -> %p (%ld);"
" tv_prev_alloc_end=%p (-> %ld inits; <0 means shrink)",
(unsigned long) (pre_alloc_end - pre_valstack),
(unsigned long) new_size,
(unsigned long) ((duk_uint8_t *) pre_alloc_end - (duk_uint8_t *) pre_valstack),
(unsigned long) new_alloc_size,
(void *) pre_valstack, (void *) thr->valstack,
(void *) pre_bottom, (void *) thr->valstack_bottom, (long) (thr->valstack_bottom - thr->valstack),
(void *) pre_top, (void *) thr->valstack_top, (long) (thr->valstack_top - thr->valstack),
(void *) pre_end, (void *) thr->valstack_end, (long) (thr->valstack_end - thr->valstack),
(void *) pre_alloc_end, (void *) thr->valstack_alloc_end, (long) (thr->valstack_alloc_end - thr->valstack),
(void *) tv_prev_alloc_end, (long) (thr->valstack_alloc_end - tv_prev_alloc_end)));
/* If allocation grew, init any new slots to 'undefined'. */
p = tv_prev_alloc_end;
while (p < thr->valstack_alloc_end) {
/* Never executed if new size is smaller. */
DUK_TVAL_SET_UNDEFINED(p);
p++;
}
/* Assert for value stack initialization policy. */
#if defined(DUK_USE_ASSERTIONS)
p = thr->valstack_top;
while (p < thr->valstack_alloc_end) {
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p));
p++;
}
#endif
return 1;
}
DUK_LOCAL DUK_COLD DUK_NOINLINE duk_bool_t duk__valstack_grow(duk_hthread *thr, duk_size_t min_bytes, duk_bool_t throw_on_error) {
duk_size_t min_size;
duk_size_t new_size;
DUK_ASSERT(min_bytes / sizeof(duk_tval) * sizeof(duk_tval) == min_bytes);
min_size = min_bytes / sizeof(duk_tval); /* from bytes to slots */
#if defined(DUK_USE_VALSTACK_GROW_SHIFT)
/* New size is minimum size plus a proportional slack, e.g. shift of
* 2 means a 25% slack.
*/
new_size = min_size + (min_size >> DUK_USE_VALSTACK_GROW_SHIFT);
#else
/* New size is tight with no slack. This is sometimes preferred in
* low memory environments.
*/
new_size = min_size;
#endif
if (DUK_UNLIKELY(new_size > DUK_USE_VALSTACK_LIMIT || new_size < min_size /*wrap*/)) {
/* Note: may be triggered even if minimal new_size would not reach the limit,
* plan limit accordingly.
*/
if (throw_on_error) {
DUK_ERROR_RANGE(thr, DUK_STR_VALSTACK_LIMIT);
DUK_WO_NORETURN(return 0;);
}
return 0;
}
if (duk__resize_valstack(thr, new_size) == 0) {
if (throw_on_error) {
DUK_ERROR_ALLOC_FAILED(thr);
DUK_WO_NORETURN(return 0;);
}
return 0;
}
thr->valstack_end = thr->valstack + min_size;
DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
return 1;
}
/* Hot, inlined value stack grow check. Because value stack almost never
* grows, the actual resize call is in a NOINLINE helper.
*/
DUK_INTERNAL DUK_INLINE void duk_valstack_grow_check_throw(duk_hthread *thr, duk_size_t min_bytes) {
duk_tval *tv;
tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + min_bytes);
if (DUK_LIKELY(thr->valstack_end >= tv)) {
return;
}
if (DUK_LIKELY(thr->valstack_alloc_end >= tv)) {
/* Values in [valstack_top,valstack_alloc_end[ are initialized
* to 'undefined' so we can just move the end pointer.
*/
thr->valstack_end = tv;
return;
}
(void) duk__valstack_grow(thr, min_bytes, 1 /*throw_on_error*/);
}
/* Hot, inlined value stack grow check which doesn't throw. */
DUK_INTERNAL DUK_INLINE duk_bool_t duk_valstack_grow_check_nothrow(duk_hthread *thr, duk_size_t min_bytes) {
duk_tval *tv;
tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + min_bytes);
if (DUK_LIKELY(thr->valstack_end >= tv)) {
return 1;
}
if (DUK_LIKELY(thr->valstack_alloc_end >= tv)) {
thr->valstack_end = tv;
return 1;
}
return duk__valstack_grow(thr, min_bytes, 0 /*throw_on_error*/);
}
/* Value stack shrink check, called from mark-and-sweep. */
DUK_INTERNAL void duk_valstack_shrink_check_nothrow(duk_hthread *thr, duk_bool_t snug) {
duk_size_t alloc_bytes;
duk_size_t reserve_bytes;
duk_size_t shrink_bytes;
alloc_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_alloc_end - (duk_uint8_t *) thr->valstack);
reserve_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
DUK_ASSERT(alloc_bytes >= reserve_bytes);
/* We're free to shrink the value stack allocation down to
* reserve_bytes but not more. If 'snug' (emergency GC)
* shrink whatever we can. Otherwise only shrink if the new
* size would be considerably smaller.
*/
#if defined(DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT)
if (snug) {
shrink_bytes = reserve_bytes;
} else {
duk_size_t proportion, slack;
/* Require that value stack shrinks by at least X% of its
* current size. For example, shift of 2 means at least
* 25%. The proportion is computed as bytes and may not
* be a multiple of sizeof(duk_tval); that's OK here.
*/
proportion = alloc_bytes >> DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT;
if (alloc_bytes - reserve_bytes < proportion) {
/* Too little would be freed, do nothing. */
return;
}
/* Keep a slack after shrinking. The slack is again a
* proportion of the current size (the proportion should
* of course be smaller than the check proportion above).
*/
#if defined(DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT)
DUK_ASSERT(DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT > DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT);
slack = alloc_bytes >> DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT;
#else
slack = 0;
#endif
shrink_bytes = reserve_bytes +
slack / sizeof(duk_tval) * sizeof(duk_tval); /* multiple of duk_tval */
}
#else /* DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT */
/* Always snug, useful in some low memory environments. */
DUK_UNREF(snug);
shrink_bytes = reserve_bytes;
#endif /* DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT */
DUK_D(DUK_DPRINT("valstack shrink check: alloc_bytes=%ld, reserve_bytes=%ld, shrink_bytes=%ld (unvalidated)",
(long) alloc_bytes, (long) reserve_bytes, (long) shrink_bytes));
DUK_ASSERT(shrink_bytes >= reserve_bytes);
if (shrink_bytes >= alloc_bytes) {
/* Skip if shrink target is same as current one (or higher,
* though that shouldn't happen in practice).
*/
return;
}
DUK_ASSERT(shrink_bytes / sizeof(duk_tval) * sizeof(duk_tval) == shrink_bytes);
DUK_D(DUK_DPRINT("valstack shrink check: decided to shrink, snug: %ld", (long) snug));
duk__resize_valstack(thr, shrink_bytes / sizeof(duk_tval));
}
DUK_EXTERNAL duk_bool_t duk_check_stack(duk_hthread *thr, duk_idx_t extra) {
duk_size_t min_new_bytes;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr != NULL);
if (DUK_UNLIKELY(extra < 0 || extra > DUK_USE_VALSTACK_LIMIT)) {
if (extra < 0) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
extra = 0;
} else {
/* Cause grow check to fail without wrapping arithmetic. */
extra = DUK_USE_VALSTACK_LIMIT;
}
}
min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) thr->valstack) +
sizeof(duk_tval) * ((duk_size_t) extra + DUK_VALSTACK_INTERNAL_EXTRA);
return duk_valstack_grow_check_nothrow(thr, min_new_bytes);
}
DUK_EXTERNAL void duk_require_stack(duk_hthread *thr, duk_idx_t extra) {
duk_size_t min_new_bytes;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr != NULL);
if (DUK_UNLIKELY(extra < 0 || extra > DUK_USE_VALSTACK_LIMIT)) {
if (extra < 0) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
extra = 0;
} else {
/* Cause grow check to fail without wrapping arithmetic. */
extra = DUK_USE_VALSTACK_LIMIT;
}
}
min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) thr->valstack) +
sizeof(duk_tval) * ((duk_size_t) extra + DUK_VALSTACK_INTERNAL_EXTRA);
duk_valstack_grow_check_throw(thr, min_new_bytes);
}
DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_hthread *thr, duk_idx_t top) {
duk_size_t min_new_bytes;
DUK_ASSERT_API_ENTRY(thr);
if (DUK_UNLIKELY(top < 0 || top > DUK_USE_VALSTACK_LIMIT)) {
if (top < 0) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
top = 0;
} else {
/* Cause grow check to fail without wrapping arithmetic. */
top = DUK_USE_VALSTACK_LIMIT;
}
}
DUK_ASSERT(top >= 0);
min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) +
sizeof(duk_tval) * ((duk_size_t) top + DUK_VALSTACK_INTERNAL_EXTRA);
return duk_valstack_grow_check_nothrow(thr, min_new_bytes);
}
DUK_EXTERNAL void duk_require_stack_top(duk_hthread *thr, duk_idx_t top) {
duk_size_t min_new_bytes;
DUK_ASSERT_API_ENTRY(thr);
if (DUK_UNLIKELY(top < 0 || top > DUK_USE_VALSTACK_LIMIT)) {
if (top < 0) {
/* Clamping to zero makes the API more robust to calling code
* calculation errors.
*/
top = 0;
} else {
/* Cause grow check to fail without wrapping arithmetic. */
top = DUK_USE_VALSTACK_LIMIT;
}
}
DUK_ASSERT(top >= 0);
min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) +
sizeof(duk_tval) * ((duk_size_t) top + DUK_VALSTACK_INTERNAL_EXTRA);
duk_valstack_grow_check_throw(thr, min_new_bytes);
}
/*
* Basic stack manipulation: swap, dup, insert, replace, etc
*/
DUK_EXTERNAL void duk_swap(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
duk_tval *tv1;
duk_tval *tv2;
duk_tval tv_tmp;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_require_tval(thr, idx1);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(thr, idx2);
DUK_ASSERT(tv2 != NULL);
/* If tv1==tv2 this is a NOP, no check is needed */
DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
DUK_TVAL_SET_TVAL(tv1, tv2);
DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
}
DUK_EXTERNAL void duk_swap_top(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
duk_swap(thr, idx, -1);
}
DUK_EXTERNAL void duk_dup(duk_hthread *thr, duk_idx_t from_idx) {
duk_tval *tv_from;
duk_tval *tv_to;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_from = duk_require_tval(thr, from_idx);
tv_to = thr->valstack_top++;
DUK_ASSERT(tv_from != NULL);
DUK_ASSERT(tv_to != NULL);
DUK_TVAL_SET_TVAL(tv_to, tv_from);
DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
}
DUK_EXTERNAL void duk_dup_top(duk_hthread *thr) {
#if defined(DUK_USE_PREFER_SIZE)
duk_dup(thr, -1);
#else
duk_tval *tv_from;
duk_tval *tv_to;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
if (DUK_UNLIKELY(thr->valstack_top - thr->valstack_bottom <= 0)) {
DUK_ERROR_RANGE_INDEX(thr, -1);
DUK_WO_NORETURN(return;);
}
tv_from = thr->valstack_top - 1;
tv_to = thr->valstack_top++;
DUK_ASSERT(tv_from != NULL);
DUK_ASSERT(tv_to != NULL);
DUK_TVAL_SET_TVAL(tv_to, tv_from);
DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
#endif
}
DUK_INTERNAL void duk_dup_0(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, 0);
}
DUK_INTERNAL void duk_dup_1(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, 1);
}
DUK_INTERNAL void duk_dup_2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, 2);
}
DUK_INTERNAL void duk_dup_m2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, -2);
}
DUK_INTERNAL void duk_dup_m3(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, -3);
}
DUK_INTERNAL void duk_dup_m4(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_dup(thr, -4);
}
DUK_EXTERNAL void duk_insert(duk_hthread *thr, duk_idx_t to_idx) {
duk_tval *p;
duk_tval *q;
duk_tval tv_tmp;
duk_size_t nbytes;
DUK_ASSERT_API_ENTRY(thr);
p = duk_require_tval(thr, to_idx);
DUK_ASSERT(p != NULL);
q = duk_require_tval(thr, -1);
DUK_ASSERT(q != NULL);
DUK_ASSERT(q >= p);
/* nbytes
* <--------->
* [ ... | p | x | x | q ]
* => [ ... | q | p | x | x ]
*/
nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
DUK_DDD(DUK_DDDPRINT("duk_insert: to_idx=%ld, p=%p, q=%p, nbytes=%lu",
(long) to_idx, (void *) p, (void *) q, (unsigned long) nbytes));
/* No net refcount changes. No need to special case nbytes == 0
* (p == q).
*/
DUK_TVAL_SET_TVAL(&tv_tmp, q);
duk_memmove((void *) (p + 1), (const void *) p, (size_t) nbytes);
DUK_TVAL_SET_TVAL(p, &tv_tmp);
}
DUK_INTERNAL void duk_insert_undefined(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(idx >= 0); /* Doesn't support negative indices. */
duk_push_undefined(thr);
duk_insert(thr, idx);
}
DUK_INTERNAL void duk_insert_undefined_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
duk_tval *tv, *tv_end;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(idx >= 0); /* Doesn't support negative indices or count. */
DUK_ASSERT(count >= 0);
tv = duk_reserve_gap(thr, idx, count);
tv_end = tv + count;
while (tv != tv_end) {
DUK_TVAL_SET_UNDEFINED(tv);
tv++;
}
}
DUK_EXTERNAL void duk_replace(duk_hthread *thr, duk_idx_t to_idx) {
duk_tval *tv1;
duk_tval *tv2;
duk_tval tv_tmp;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_require_tval(thr, -1);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(thr, to_idx);
DUK_ASSERT(tv2 != NULL);
/* For tv1 == tv2, both pointing to stack top, the end result
* is same as duk_pop(thr).
*/
DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
DUK_TVAL_SET_TVAL(tv2, tv1);
DUK_TVAL_SET_UNDEFINED(tv1);
thr->valstack_top--;
DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
}
DUK_EXTERNAL void duk_copy(duk_hthread *thr, duk_idx_t from_idx, duk_idx_t to_idx) {
duk_tval *tv1;
duk_tval *tv2;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_require_tval(thr, from_idx);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(thr, to_idx);
DUK_ASSERT(tv2 != NULL);
/* For tv1 == tv2, this is a no-op (no explicit check needed). */
DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */
}
DUK_EXTERNAL void duk_remove(duk_hthread *thr, duk_idx_t idx) {
duk_tval *p;
duk_tval *q;
#if defined(DUK_USE_REFERENCE_COUNTING)
duk_tval tv_tmp;
#endif
duk_size_t nbytes;
DUK_ASSERT_API_ENTRY(thr);
p = duk_require_tval(thr, idx);
DUK_ASSERT(p != NULL);
q = duk_require_tval(thr, -1);
DUK_ASSERT(q != NULL);
DUK_ASSERT(q >= p);
/* nbytes zero size case
* <--------->
* [ ... | p | x | x | q ] [ ... | p==q ]
* => [ ... | x | x | q ] [ ... ]
*/
#if defined(DUK_USE_REFERENCE_COUNTING)
/* use a temp: decref only when valstack reachable values are correct */
DUK_TVAL_SET_TVAL(&tv_tmp, p);
#endif
nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
duk_memmove((void *) p, (const void *) (p + 1), (size_t) nbytes);
DUK_TVAL_SET_UNDEFINED(q);
thr->valstack_top--;
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
#endif
}
DUK_INTERNAL void duk_remove_unsafe(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
duk_remove(thr, idx); /* XXX: no optimization for now */
}
DUK_INTERNAL void duk_remove_m2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_remove(thr, -2);
}
DUK_INTERNAL void duk_remove_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
#if defined(DUK_USE_PREFER_SIZE)
/* XXX: maybe too slow even when preferring size? */
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(count >= 0);
DUK_ASSERT(idx >= 0);
while (count-- > 0) {
duk_remove(thr, idx);
}
#else /* DUK_USE_PREFER_SIZE */
duk_tval *tv_src;
duk_tval *tv_dst;
duk_tval *tv_newtop;
duk_tval *tv;
duk_size_t bytes;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(count >= 0);
DUK_ASSERT(idx >= 0);
tv_dst = thr->valstack_bottom + idx;
DUK_ASSERT(tv_dst <= thr->valstack_top);
tv_src = tv_dst + count;
DUK_ASSERT(tv_src <= thr->valstack_top);
bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) tv_src);
for (tv = tv_dst; tv < tv_src; tv++) {
DUK_TVAL_DECREF_NORZ(thr, tv);
}
duk_memmove((void *) tv_dst, (const void *) tv_src, bytes);
tv_newtop = thr->valstack_top - count;
for (tv = tv_newtop; tv < thr->valstack_top; tv++) {
DUK_TVAL_SET_UNDEFINED(tv);
}
thr->valstack_top = tv_newtop;
/* When not preferring size, only NORZ macros are used; caller
* is expected to DUK_REFZERO_CHECK().
*/
#endif /* DUK_USE_PREFER_SIZE */
}
DUK_INTERNAL void duk_remove_n_unsafe(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
DUK_ASSERT_API_ENTRY(thr);
duk_remove_n(thr, idx, count); /* XXX: no optimization for now */
}
/*
* Stack slice primitives
*/
DUK_EXTERNAL void duk_xcopymove_raw(duk_hthread *to_thr, duk_hthread *from_thr, duk_idx_t count, duk_bool_t is_copy) {
void *src;
duk_size_t nbytes;
duk_tval *p;
duk_tval *q;
/* XXX: several pointer comparison issues here */
DUK_ASSERT_API_ENTRY(to_thr);
DUK_ASSERT_CTX_VALID(to_thr);
DUK_ASSERT_CTX_VALID(from_thr);
DUK_ASSERT(to_thr->heap == from_thr->heap);
if (DUK_UNLIKELY(to_thr == from_thr)) {
DUK_ERROR_TYPE(to_thr, DUK_STR_INVALID_CONTEXT);
DUK_WO_NORETURN(return;);
}
if (DUK_UNLIKELY((duk_uidx_t) count > (duk_uidx_t) DUK_USE_VALSTACK_LIMIT)) {
/* Maximum value check ensures 'nbytes' won't wrap below.
* Also handles negative count.
*/
DUK_ERROR_RANGE_INVALID_COUNT(to_thr);
DUK_WO_NORETURN(return;);
}
DUK_ASSERT(count >= 0);
nbytes = sizeof(duk_tval) * (duk_size_t) count;
if (DUK_UNLIKELY(nbytes == 0)) {
return;
}
DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end);
if (DUK_UNLIKELY((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes)) {
DUK_ERROR_RANGE_PUSH_BEYOND(to_thr);
DUK_WO_NORETURN(return;);
}
src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes);
if (DUK_UNLIKELY(src < (void *) from_thr->valstack_bottom)) {
DUK_ERROR_RANGE_INVALID_COUNT(to_thr);
DUK_WO_NORETURN(return;);
}
/* Copy values (no overlap even if to_thr == from_thr; that's not
* allowed now anyway).
*/
DUK_ASSERT(nbytes > 0);
duk_memcpy((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes);
p = to_thr->valstack_top;
to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes);
if (is_copy) {
/* Incref copies, keep originals. */
q = to_thr->valstack_top;
while (p < q) {
DUK_TVAL_INCREF(to_thr, p); /* no side effects */
p++;
}
} else {
/* No net refcount change. */
p = from_thr->valstack_top;
q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes);
from_thr->valstack_top = q;
while (p > q) {
p--;
DUK_TVAL_SET_UNDEFINED(p);
/* XXX: fast primitive to set a bunch of values to UNDEFINED */
}
}
}
/* Internal helper: reserve a gap of 'count' elements at 'idx_base' and return a
* pointer to the gap. Values in the gap are garbage and MUST be initialized by
* the caller before any side effects may occur. The caller must ensure there's
* enough stack reserve for 'count' values.
*/
DUK_INTERNAL duk_tval *duk_reserve_gap(duk_hthread *thr, duk_idx_t idx_base, duk_idx_t count) {
duk_tval *tv_src;
duk_tval *tv_dst;
duk_size_t gap_bytes;
duk_size_t copy_bytes;
/* Caller is responsible for ensuring there's enough preallocated
* value stack.
*/
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(count >= 0);
DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack_top) >= (duk_size_t) count);
tv_src = thr->valstack_bottom + idx_base;
gap_bytes = (duk_size_t) count * sizeof(duk_tval);
tv_dst = (duk_tval *) (void *) ((duk_uint8_t *) tv_src + gap_bytes);
copy_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) tv_src);
thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_top + gap_bytes);
duk_memmove((void *) tv_dst, (const void *) tv_src, copy_bytes);
/* Values in the gap are left as garbage: caller must fill them in
* and INCREF them before any side effects.
*/
return tv_src;
}
/*
* Get/opt/require
*/
DUK_EXTERNAL void duk_require_undefined(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_UNDEFINED(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "undefined", DUK_STR_NOT_UNDEFINED);
DUK_WO_NORETURN(return;);
}
}
DUK_EXTERNAL void duk_require_null(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_NULL(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "null", DUK_STR_NOT_NULL);
DUK_WO_NORETURN(return;);
}
}
DUK_LOCAL DUK_ALWAYS_INLINE duk_bool_t duk__get_boolean_raw(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
duk_bool_t ret;
duk_tval *tv;
DUK_ASSERT_CTX_VALID(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BOOLEAN(tv)) {
ret = DUK_TVAL_GET_BOOLEAN(tv);
DUK_ASSERT(ret == 0 || ret == 1);
} else {
ret = def_value;
/* Not guaranteed to be 0 or 1. */
}
return ret;
}
DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_boolean_raw(thr, idx, 0); /* default: false */
}
DUK_EXTERNAL duk_bool_t duk_get_boolean_default(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_boolean_raw(thr, idx, def_value);
}
DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_bool_t ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_LIKELY(DUK_TVAL_IS_BOOLEAN(tv))) {
ret = DUK_TVAL_GET_BOOLEAN(tv);
DUK_ASSERT(ret == 0 || ret == 1);
return ret;
} else {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "boolean", DUK_STR_NOT_BOOLEAN);
DUK_WO_NORETURN(return 0;);
}
}
DUK_EXTERNAL duk_bool_t duk_opt_boolean(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_boolean(thr, idx);
}
DUK_LOCAL DUK_ALWAYS_INLINE duk_double_t duk__get_number_raw(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
duk_double_union ret;
duk_tval *tv;
DUK_ASSERT_CTX_VALID(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
#if defined(DUK_USE_FASTINT)
if (DUK_TVAL_IS_FASTINT(tv)) {
ret.d = (duk_double_t) DUK_TVAL_GET_FASTINT(tv); /* XXX: cast trick */
}
else
#endif
if (DUK_TVAL_IS_DOUBLE(tv)) {
/* When using packed duk_tval, number must be in NaN-normalized form
* for it to be a duk_tval, so no need to normalize. NOP for unpacked
* duk_tval.
*/
ret.d = DUK_TVAL_GET_DOUBLE(tv);
DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&ret));
} else {
ret.d = def_value;
/* Default value (including NaN) may not be normalized. */
}
return ret.d;
}
DUK_EXTERNAL duk_double_t duk_get_number(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_number_raw(thr, idx, DUK_DOUBLE_NAN); /* default: NaN */
}
DUK_EXTERNAL duk_double_t duk_get_number_default(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_number_raw(thr, idx, def_value);
}
DUK_EXTERNAL duk_double_t duk_require_number(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_double_union ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_NUMBER(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
DUK_WO_NORETURN(return 0.0;);
}
ret.d = DUK_TVAL_GET_NUMBER(tv);
/* When using packed duk_tval, number must be in NaN-normalized form
* for it to be a duk_tval, so no need to normalize. NOP for unpacked
* duk_tval.
*/
DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&ret));
return ret.d;
}
DUK_EXTERNAL duk_double_t duk_opt_number(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
/* User provided default is not NaN normalized. */
return def_value;
}
return duk_require_number(thr, idx);
}
DUK_EXTERNAL duk_int_t duk_get_int(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 0 /*require*/);
}
DUK_EXTERNAL duk_uint_t duk_get_uint(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 0 /*require*/);
}
DUK_EXTERNAL duk_int_t duk_get_int_default(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_int_t) duk__api_coerce_d2i(thr, idx, def_value, 0 /*require*/);
}
DUK_EXTERNAL duk_uint_t duk_get_uint_default(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, def_value, 0 /*require*/);
}
DUK_EXTERNAL duk_int_t duk_require_int(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 1 /*require*/);
}
DUK_EXTERNAL duk_uint_t duk_require_uint(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 1 /*require*/);
}
DUK_EXTERNAL duk_int_t duk_opt_int(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_int(thr, idx);
}
DUK_EXTERNAL duk_uint_t duk_opt_uint(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_uint(thr, idx);
}
DUK_EXTERNAL const char *duk_get_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
duk_hstring *h;
const char *ret;
duk_size_t len;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring(thr, idx);
if (h != NULL) {
len = DUK_HSTRING_GET_BYTELEN(h);
ret = (const char *) DUK_HSTRING_GET_DATA(h);
} else {
len = 0;
ret = NULL;
}
if (out_len != NULL) {
*out_len = len;
}
return ret;
}
DUK_EXTERNAL const char *duk_require_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_require_hstring(thr, idx);
DUK_ASSERT(h != NULL);
if (out_len) {
*out_len = DUK_HSTRING_GET_BYTELEN(h);
}
return (const char *) DUK_HSTRING_GET_DATA(h);
}
DUK_INTERNAL const char *duk_require_lstring_notsymbol(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_require_hstring_notsymbol(thr, idx);
DUK_ASSERT(h != NULL);
if (out_len) {
*out_len = DUK_HSTRING_GET_BYTELEN(h);
}
return (const char *) DUK_HSTRING_GET_DATA(h);
}
DUK_EXTERNAL const char *duk_get_string(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring(thr, idx);
if (h != NULL) {
return (const char *) DUK_HSTRING_GET_DATA(h);
} else {
return NULL;
}
}
DUK_EXTERNAL const char *duk_opt_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len, const char *def_ptr, duk_size_t def_len) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
if (out_len != NULL) {
*out_len = def_len;
}
return def_ptr;
}
return duk_require_lstring(thr, idx, out_len);
}
DUK_EXTERNAL const char *duk_opt_string(duk_hthread *thr, duk_idx_t idx, const char *def_ptr) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_ptr;
}
return duk_require_string(thr, idx);
}
DUK_EXTERNAL const char *duk_get_lstring_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len, const char *def_ptr, duk_size_t def_len) {
duk_hstring *h;
const char *ret;
duk_size_t len;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring(thr, idx);
if (h != NULL) {
len = DUK_HSTRING_GET_BYTELEN(h);
ret = (const char *) DUK_HSTRING_GET_DATA(h);
} else {
len = def_len;
ret = def_ptr;
}
if (out_len != NULL) {
*out_len = len;
}
return ret;
}
DUK_EXTERNAL const char *duk_get_string_default(duk_hthread *thr, duk_idx_t idx, const char *def_value) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring(thr, idx);
if (h != NULL) {
return (const char *) DUK_HSTRING_GET_DATA(h);
} else {
return def_value;
}
}
DUK_INTERNAL const char *duk_get_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring_notsymbol(thr, idx);
if (h) {
return (const char *) DUK_HSTRING_GET_DATA(h);
} else {
return NULL;
}
}
DUK_EXTERNAL const char *duk_require_string(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_require_lstring(thr, idx, NULL);
}
DUK_INTERNAL const char *duk_require_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_require_hstring_notsymbol(thr, idx);
DUK_ASSERT(h != NULL);
return (const char *) DUK_HSTRING_GET_DATA(h);
}
DUK_EXTERNAL void duk_require_object(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_OBJECT(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
DUK_WO_NORETURN(return;);
}
}
DUK_LOCAL void *duk__get_pointer_raw(duk_hthread *thr, duk_idx_t idx, void *def_value) {
duk_tval *tv;
void *p;
DUK_ASSERT_CTX_VALID(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (!DUK_TVAL_IS_POINTER(tv)) {
return def_value;
}
p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
return p;
}
DUK_EXTERNAL void *duk_get_pointer(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_pointer_raw(thr, idx, NULL /*def_value*/);
}
DUK_EXTERNAL void *duk_opt_pointer(duk_hthread *thr, duk_idx_t idx, void *def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_pointer(thr, idx);
}
DUK_EXTERNAL void *duk_get_pointer_default(duk_hthread *thr, duk_idx_t idx, void *def_value) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_pointer_raw(thr, idx, def_value);
}
DUK_EXTERNAL void *duk_require_pointer(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
void *p;
DUK_ASSERT_API_ENTRY(thr);
/* Note: here we must be wary of the fact that a pointer may be
* valid and be a NULL.
*/
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_POINTER(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "pointer", DUK_STR_NOT_POINTER);
DUK_WO_NORETURN(return NULL;);
}
p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
return p;
}
#if 0 /*unused*/
DUK_INTERNAL void *duk_get_voidptr(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_heaphdr *h;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (!DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
return NULL;
}
h = DUK_TVAL_GET_HEAPHDR(tv);
DUK_ASSERT(h != NULL);
return (void *) h;
}
#endif
DUK_LOCAL void *duk__get_buffer_helper(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size, duk_bool_t throw_flag) {
duk_hbuffer *h;
void *ret;
duk_size_t len;
duk_tval *tv;
DUK_ASSERT_CTX_VALID(thr);
if (out_size != NULL) {
*out_size = 0;
}
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_LIKELY(DUK_TVAL_IS_BUFFER(tv))) {
h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
len = DUK_HBUFFER_GET_SIZE(h);
ret = DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
} else {
if (throw_flag) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
DUK_WO_NORETURN(return NULL;);
}
len = def_size;
ret = def_ptr;
}
if (out_size != NULL) {
*out_size = len;
}
return ret;
}
DUK_EXTERNAL void *duk_get_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_buffer_helper(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/);
}
DUK_EXTERNAL void *duk_opt_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
if (out_size != NULL) {
*out_size = def_size;
}
return def_ptr;
}
return duk_require_buffer(thr, idx, out_size);
}
DUK_EXTERNAL void *duk_get_buffer_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_len) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_buffer_helper(thr, idx, out_size, def_ptr, def_len, 0 /*throw_flag*/);
}
DUK_EXTERNAL void *duk_require_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_buffer_helper(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 1 /*throw_flag*/);
}
/* Get the active buffer data area for a plain buffer or a buffer object.
* Return NULL if the the value is not a buffer. Note that a buffer may
* have a NULL data pointer when its size is zero, the optional 'out_isbuffer'
* argument allows caller to detect this reliably.
*/
DUK_INTERNAL void *duk_get_buffer_data_raw(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size, duk_bool_t throw_flag, duk_bool_t *out_isbuffer) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
if (out_isbuffer != NULL) {
*out_isbuffer = 0;
}
if (out_size != NULL) {
*out_size = def_size;
}
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BUFFER(tv)) {
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
if (out_size != NULL) {
*out_size = DUK_HBUFFER_GET_SIZE(h);
}
if (out_isbuffer != NULL) {
*out_isbuffer = 1;
}
return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
}
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
else if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
if (DUK_HOBJECT_IS_BUFOBJ(h)) {
/* XXX: this is probably a useful shared helper: for a
* duk_hbufobj, get a validated buffer pointer/length.
*/
duk_hbufobj *h_bufobj = (duk_hbufobj *) h;
DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
if (h_bufobj->buf != NULL &&
DUK_HBUFOBJ_VALID_SLICE(h_bufobj)) {
duk_uint8_t *p;
p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf);
if (out_size != NULL) {
*out_size = (duk_size_t) h_bufobj->length;
}
if (out_isbuffer != NULL) {
*out_isbuffer = 1;
}
return (void *) (p + h_bufobj->offset);
}
/* if slice not fully valid, treat as error */
}
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
if (throw_flag) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
DUK_WO_NORETURN(return NULL;);
}
return def_ptr;
}
DUK_EXTERNAL void *duk_get_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
DUK_ASSERT_API_ENTRY(thr);
return duk_get_buffer_data_raw(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/, NULL);
}
DUK_EXTERNAL void *duk_get_buffer_data_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
DUK_ASSERT_API_ENTRY(thr);
return duk_get_buffer_data_raw(thr, idx, out_size, def_ptr, def_size, 0 /*throw_flag*/, NULL);
}
DUK_EXTERNAL void *duk_opt_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
if (out_size != NULL) {
*out_size = def_size;
}
return def_ptr;
}
return duk_require_buffer_data(thr, idx, out_size);
}
DUK_EXTERNAL void *duk_require_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
DUK_ASSERT_API_ENTRY(thr);
return duk_get_buffer_data_raw(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 1 /*throw_flag*/, NULL);
}
/* Raw helper for getting a value from the stack, checking its tag.
* The tag cannot be a number because numbers don't have an internal
* tag in the packed representation.
*/
DUK_LOCAL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t tag) {
duk_tval *tv;
duk_heaphdr *ret;
DUK_ASSERT_CTX_VALID(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_GET_TAG(tv) != tag) {
return (duk_heaphdr *) NULL;
}
ret = DUK_TVAL_GET_HEAPHDR(tv);
DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */
return ret;
}
DUK_INTERNAL duk_hstring *duk_get_hstring(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
}
DUK_INTERNAL duk_hstring *duk_get_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
if (DUK_UNLIKELY(h && DUK_HSTRING_HAS_SYMBOL(h))) {
return NULL;
}
return h;
}
DUK_INTERNAL duk_hstring *duk_require_hstring(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
if (DUK_UNLIKELY(h == NULL)) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "string", DUK_STR_NOT_STRING);
DUK_WO_NORETURN(return NULL;);
}
return h;
}
DUK_INTERNAL duk_hstring *duk_require_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
if (DUK_UNLIKELY(h == NULL || DUK_HSTRING_HAS_SYMBOL(h))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "string", DUK_STR_NOT_STRING);
DUK_WO_NORETURN(return NULL;);
}
return h;
}
DUK_INTERNAL duk_hobject *duk_get_hobject(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
}
DUK_INTERNAL duk_hobject *duk_require_hobject(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(h == NULL)) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
DUK_WO_NORETURN(return NULL;);
}
return h;
}
DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_hbuffer *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_BUFFER);
}
DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_hthread *thr, duk_idx_t idx) {
duk_hbuffer *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hbuffer *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_BUFFER);
if (DUK_UNLIKELY(h == NULL)) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
DUK_WO_NORETURN(return NULL;);
}
return h;
}
DUK_INTERNAL duk_hthread *duk_get_hthread(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_THREAD(h))) {
h = NULL;
}
return (duk_hthread *) h;
}
DUK_INTERNAL duk_hthread *duk_require_hthread(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_THREAD(h)))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "thread", DUK_STR_NOT_THREAD);
DUK_WO_NORETURN(return NULL;);
}
return (duk_hthread *) h;
}
DUK_INTERNAL duk_hcompfunc *duk_get_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_COMPFUNC(h))) {
h = NULL;
}
return (duk_hcompfunc *) h;
}
DUK_INTERNAL duk_hcompfunc *duk_require_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_COMPFUNC(h)))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "compiledfunction", DUK_STR_NOT_COMPFUNC);
DUK_WO_NORETURN(return NULL;);
}
return (duk_hcompfunc *) h;
}
DUK_INTERNAL duk_hnatfunc *duk_get_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_NATFUNC(h))) {
h = NULL;
}
return (duk_hnatfunc *) h;
}
DUK_INTERNAL duk_hnatfunc *duk_require_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_NATFUNC(h)))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "nativefunction", DUK_STR_NOT_NATFUNC);
DUK_WO_NORETURN(return NULL;);
}
return (duk_hnatfunc *) h;
}
DUK_EXTERNAL duk_c_function duk_get_c_function(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_hobject *h;
duk_hnatfunc *f;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_OBJECT(tv))) {
return NULL;
}
h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
if (DUK_UNLIKELY(!DUK_HOBJECT_IS_NATFUNC(h))) {
return NULL;
}
DUK_ASSERT(DUK_HOBJECT_HAS_NATFUNC(h));
f = (duk_hnatfunc *) h;
return f->func;
}
DUK_EXTERNAL duk_c_function duk_opt_c_function(duk_hthread *thr, duk_idx_t idx, duk_c_function def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_c_function(thr, idx);
}
DUK_EXTERNAL duk_c_function duk_get_c_function_default(duk_hthread *thr, duk_idx_t idx, duk_c_function def_value) {
duk_c_function ret;
DUK_ASSERT_API_ENTRY(thr);
ret = duk_get_c_function(thr, idx);
if (ret != NULL) {
return ret;
}
return def_value;
}
DUK_EXTERNAL duk_c_function duk_require_c_function(duk_hthread *thr, duk_idx_t idx) {
duk_c_function ret;
DUK_ASSERT_API_ENTRY(thr);
ret = duk_get_c_function(thr, idx);
if (DUK_UNLIKELY(!ret)) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "nativefunction", DUK_STR_NOT_NATFUNC);
DUK_WO_NORETURN(return ret;);
}
return ret;
}
DUK_EXTERNAL void duk_require_function(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
if (DUK_UNLIKELY(!duk_is_function(thr, idx))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "function", DUK_STR_NOT_FUNCTION);
DUK_WO_NORETURN(return;);
}
}
DUK_INTERNAL void duk_require_constructable(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_require_hobject_accept_mask(thr, idx, DUK_TYPE_MASK_LIGHTFUNC);
if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_HAS_CONSTRUCTABLE(h))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "constructable", DUK_STR_NOT_CONSTRUCTABLE);
DUK_WO_NORETURN(return;);
}
/* Lightfuncs (h == NULL) are constructable. */
}
DUK_EXTERNAL duk_hthread *duk_get_context(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_get_hthread(thr, idx);
}
DUK_EXTERNAL duk_hthread *duk_require_context(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_require_hthread(thr, idx);
}
DUK_EXTERNAL duk_hthread *duk_opt_context(duk_hthread *thr, duk_idx_t idx, duk_hthread *def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_context(thr, idx);
}
DUK_EXTERNAL duk_hthread *duk_get_context_default(duk_hthread *thr, duk_idx_t idx, duk_hthread *def_value) {
duk_hthread *ret;
DUK_ASSERT_API_ENTRY(thr);
ret = duk_get_context(thr, idx);
if (ret != NULL) {
return ret;
}
return def_value;
}
DUK_EXTERNAL void *duk_get_heapptr(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
void *ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_HEAP_ALLOCATED(tv))) {
return (void *) NULL;
}
ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
DUK_ASSERT(ret != NULL);
return ret;
}
DUK_EXTERNAL void *duk_opt_heapptr(duk_hthread *thr, duk_idx_t idx, void *def_value) {
DUK_ASSERT_API_ENTRY(thr);
if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
return def_value;
}
return duk_require_heapptr(thr, idx);
}
DUK_EXTERNAL void *duk_get_heapptr_default(duk_hthread *thr, duk_idx_t idx, void *def_value) {
void *ret;
DUK_ASSERT_API_ENTRY(thr);
ret = duk_get_heapptr(thr, idx);
if (ret != NULL) {
return ret;
}
return def_value;
}
DUK_EXTERNAL void *duk_require_heapptr(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
void *ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_UNLIKELY(!DUK_TVAL_IS_HEAP_ALLOCATED(tv))) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "heapobject", DUK_STR_UNEXPECTED_TYPE);
DUK_WO_NORETURN(return NULL;);
}
ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
DUK_ASSERT(ret != NULL);
return ret;
}
/* Internal helper for getting/requiring a duk_hobject with possible promotion. */
DUK_LOCAL duk_hobject *duk__get_hobject_promote_mask_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
duk_uint_t val_mask;
duk_hobject *res;
DUK_ASSERT_CTX_VALID(thr);
res = duk_get_hobject(thr, idx); /* common case, not promoted */
if (DUK_LIKELY(res != NULL)) {
DUK_ASSERT(res != NULL);
return res;
}
val_mask = duk_get_type_mask(thr, idx);
if (val_mask & type_mask) {
if (type_mask & DUK_TYPE_MASK_PROMOTE) {
res = duk_to_hobject(thr, idx);
DUK_ASSERT(res != NULL);
return res;
} else {
return NULL; /* accept without promoting */
}
}
if (type_mask & DUK_TYPE_MASK_THROW) {
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
DUK_WO_NORETURN(return NULL;);
}
return NULL;
}
/* Get a duk_hobject * at 'idx'; if the value is not an object but matches the
* supplied 'type_mask', promote it to an object and return the duk_hobject *.
* This is useful for call sites which want an object but also accept a plain
* buffer and/or a lightfunc which gets automatically promoted to an object.
* Return value is NULL if value is neither an object nor a plain type allowed
* by the mask.
*/
DUK_INTERNAL duk_hobject *duk_get_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_PROMOTE);
}
/* Like duk_get_hobject_promote_mask() but throw a TypeError instead of
* returning a NULL.
*/
DUK_INTERNAL duk_hobject *duk_require_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_THROW | DUK_TYPE_MASK_PROMOTE);
}
/* Require a duk_hobject * at 'idx'; if the value is not an object but matches the
* supplied 'type_mask', return a NULL instead. Otherwise throw a TypeError.
*/
DUK_INTERNAL duk_hobject *duk_require_hobject_accept_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
DUK_ASSERT_API_ENTRY(thr);
return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_THROW);
}
DUK_INTERNAL duk_hobject *duk_get_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) != classnum)) {
h = NULL;
}
return h;
}
DUK_INTERNAL duk_hobject *duk_require_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) == classnum))) {
duk_hstring *h_class;
h_class = DUK_HTHREAD_GET_STRING(thr, DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum));
DUK_UNREF(h_class);
DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, (const char *) DUK_HSTRING_GET_DATA(h_class), DUK_STR_UNEXPECTED_TYPE);
DUK_WO_NORETURN(return NULL;);
}
return h;
}
DUK_EXTERNAL duk_size_t duk_get_length(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNDEFINED:
case DUK_TAG_NULL:
case DUK_TAG_BOOLEAN:
case DUK_TAG_POINTER:
return 0;
#if defined(DUK_USE_PREFER_SIZE)
/* String and buffer have a virtual non-configurable .length property
* which is within size_t range so it can be looked up without specific
* type checks. Lightfuncs inherit from %NativeFunctionPrototype%
* which provides an inherited .length accessor; it could be overwritten
* to produce unexpected types or values, but just number convert and
* duk_size_t cast for now.
*/
case DUK_TAG_STRING:
case DUK_TAG_BUFFER:
case DUK_TAG_LIGHTFUNC: {
duk_size_t ret;
duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
ret = (duk_size_t) duk_to_number_m1(thr);
duk_pop_unsafe(thr);
return ret;
}
#else /* DUK_USE_PREFER_SIZE */
case DUK_TAG_STRING: {
duk_hstring *h = DUK_TVAL_GET_STRING(tv);
DUK_ASSERT(h != NULL);
if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
return 0;
}
return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h);
}
case DUK_TAG_BUFFER: {
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
return (duk_size_t) DUK_HBUFFER_GET_SIZE(h);
}
case DUK_TAG_LIGHTFUNC: {
/* We could look up the length from the lightfunc duk_tval,
* but since Duktape 2.2 lightfunc .length comes from
* %NativeFunctionPrototype% which can be overridden, so
* look up the property explicitly.
*/
duk_size_t ret;
duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
ret = (duk_size_t) duk_to_number_m1(thr);
duk_pop_unsafe(thr);
return ret;
}
#endif /* DUK_USE_PREFER_SIZE */
case DUK_TAG_OBJECT: {
duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
return (duk_size_t) duk_hobject_get_length(thr, h);
}
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default:
/* number or 'unused' */
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv) || DUK_TVAL_IS_UNUSED(tv));
return 0;
}
DUK_UNREACHABLE();
}
/*
* duk_known_xxx() helpers
*
* Used internally when we're 100% sure that a certain index is valid and
* contains an object of a certain type. For example, if we duk_push_object()
* we can then safely duk_known_hobject(thr, -1). These helpers just assert
* for the index and type, and if the assumptions are not valid, memory unsafe
* behavior happens.
*/
DUK_LOCAL duk_heaphdr *duk__known_heaphdr(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_heaphdr *h;
DUK_ASSERT_CTX_VALID(thr);
if (idx < 0) {
tv = thr->valstack_top + idx;
} else {
tv = thr->valstack_bottom + idx;
}
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_ASSERT(tv < thr->valstack_top);
h = DUK_TVAL_GET_HEAPHDR(tv);
DUK_ASSERT(h != NULL);
return h;
}
DUK_INTERNAL duk_hstring *duk_known_hstring(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(duk_get_hstring(thr, idx) != NULL);
return (duk_hstring *) duk__known_heaphdr(thr, idx);
}
DUK_INTERNAL duk_hobject *duk_known_hobject(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(duk_get_hobject(thr, idx) != NULL);
return (duk_hobject *) duk__known_heaphdr(thr, idx);
}
DUK_INTERNAL duk_hbuffer *duk_known_hbuffer(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(duk_get_hbuffer(thr, idx) != NULL);
return (duk_hbuffer *) duk__known_heaphdr(thr, idx);
}
DUK_INTERNAL duk_hcompfunc *duk_known_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(duk_get_hcompfunc(thr, idx) != NULL);
return (duk_hcompfunc *) duk__known_heaphdr(thr, idx);
}
DUK_INTERNAL duk_hnatfunc *duk_known_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(duk_get_hnatfunc(thr, idx) != NULL);
return (duk_hnatfunc *) duk__known_heaphdr(thr, idx);
}
DUK_EXTERNAL void duk_set_length(duk_hthread *thr, duk_idx_t idx, duk_size_t len) {
DUK_ASSERT_API_ENTRY(thr);
idx = duk_normalize_index(thr, idx);
duk_push_uint(thr, (duk_uint_t) len);
duk_put_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
}
/*
* Conversions and coercions
*
* The conversion/coercions are in-place operations on the value stack.
* Some operations are implemented here directly, while others call a
* helper in duk_js_ops.c after validating arguments.
*/
/* E5 Section 8.12.8 */
DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t func_stridx) {
if (duk_get_prop_stridx(thr, idx, func_stridx)) {
/* [ ... func ] */
if (duk_is_callable(thr, -1)) {
duk_dup(thr, idx); /* -> [ ... func this ] */
duk_call_method(thr, 0); /* -> [ ... retval ] */
if (duk_is_primitive(thr, -1)) {
duk_replace(thr, idx);
return 1;
}
/* [ ... retval ]; popped below */
}
}
duk_pop_unsafe(thr); /* [ ... func/retval ] -> [ ... ] */
return 0;
}
DUK_EXTERNAL void duk_to_undefined(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
}
DUK_EXTERNAL void duk_to_null(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */
}
/* E5 Section 9.1 */
DUK_LOCAL const char * const duk__toprim_hint_strings[3] = {
"default", "string", "number"
};
DUK_LOCAL void duk__to_primitive_helper(duk_hthread *thr, duk_idx_t idx, duk_int_t hint, duk_bool_t check_symbol) {
/* Inline initializer for coercers[] is not allowed by old compilers like BCC. */
duk_small_uint_t coercers[2];
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING);
idx = duk_require_normalize_index(thr, idx);
/* If already primitive, return as is. */
if (!duk_check_type_mask(thr, idx, DUK_TYPE_MASK_OBJECT |
DUK_TYPE_MASK_LIGHTFUNC |
DUK_TYPE_MASK_BUFFER)) {
DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
return;
}
/* @@toPrimitive lookup. Also do for plain buffers and lightfuncs
* which mimic objects.
*/
if (check_symbol && duk_get_method_stridx(thr, idx, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_PRIMITIVE)) {
DUK_ASSERT(hint >= 0 && (duk_size_t) hint < sizeof(duk__toprim_hint_strings) / sizeof(const char *));
duk_dup(thr, idx);
duk_push_string(thr, duk__toprim_hint_strings[hint]);
duk_call_method(thr, 1); /* [ ... method value hint ] -> [ ... res] */
if (duk_check_type_mask(thr, -1, DUK_TYPE_MASK_OBJECT |
DUK_TYPE_MASK_LIGHTFUNC |
DUK_TYPE_MASK_BUFFER)) {
goto fail;
}
duk_replace(thr, idx);
return;
}
/* Objects are coerced based on E5 specification.
* Lightfuncs are coerced because they behave like
* objects even if they're internally a primitive
* type. Same applies to plain buffers, which behave
* like ArrayBuffer objects since Duktape 2.x.
*/
/* Hint magic for Date is unnecessary in ES2015 because of
* Date.prototype[@@toPrimitive]. However, it is needed if
* symbol support is not enabled.
*/
#if defined(DUK_USE_SYMBOL_BUILTIN)
if (hint == DUK_HINT_NONE) {
hint = DUK_HINT_NUMBER;
}
#else /* DUK_USE_SYMBOL_BUILTIN */
if (hint == DUK_HINT_NONE) {
duk_small_uint_t class_number;
class_number = duk_get_class_number(thr, idx);
if (class_number == DUK_HOBJECT_CLASS_DATE) {
hint = DUK_HINT_STRING;
} else {
hint = DUK_HINT_NUMBER;
}
}
#endif /* DUK_USE_SYMBOL_BUILTIN */
coercers[0] = DUK_STRIDX_VALUE_OF;
coercers[1] = DUK_STRIDX_TO_STRING;
if (hint == DUK_HINT_STRING) {
coercers[0] = DUK_STRIDX_TO_STRING;
coercers[1] = DUK_STRIDX_VALUE_OF;
}
if (duk__defaultvalue_coerce_attempt(thr, idx, coercers[0])) {
DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
return;
}
if (duk__defaultvalue_coerce_attempt(thr, idx, coercers[1])) {
DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
return;
}
fail:
DUK_ERROR_TYPE(thr, DUK_STR_TOPRIMITIVE_FAILED);
DUK_WO_NORETURN(return;);
}
DUK_EXTERNAL void duk_to_primitive(duk_hthread *thr, duk_idx_t idx, duk_int_t hint) {
duk__to_primitive_helper(thr, idx, hint, 1 /*check_symbol*/);
}
#if defined(DUK_USE_SYMBOL_BUILTIN)
DUK_INTERNAL void duk_to_primitive_ordinary(duk_hthread *thr, duk_idx_t idx, duk_int_t hint) {
duk__to_primitive_helper(thr, idx, hint, 0 /*check_symbol*/);
}
#endif
/* E5 Section 9.2 */
DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_bool_t val;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_ASSERT(tv != NULL);
val = duk_js_toboolean(tv);
DUK_ASSERT(val == 0 || val == 1);
/* Note: no need to re-lookup tv, conversion is side effect free. */
DUK_ASSERT(tv != NULL);
DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */
return val;
}
DUK_INTERNAL duk_bool_t duk_to_boolean_top_pop(duk_hthread *thr) {
duk_tval *tv;
duk_bool_t val;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, -1);
DUK_ASSERT(tv != NULL);
val = duk_js_toboolean(tv);
DUK_ASSERT(val == 0 || val == 1);
duk_pop_unsafe(thr);
return val;
}
DUK_EXTERNAL duk_double_t duk_to_number(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_double_t d;
DUK_ASSERT_API_ENTRY(thr);
/* XXX: No need to normalize; the whole operation could be inlined here to
* avoid 'tv' re-lookup.
*/
idx = duk_require_normalize_index(thr, idx);
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_ASSERT(tv != NULL);
d = duk_js_tonumber(thr, tv); /* XXX: fastint coercion? now result will always be a non-fastint */
/* ToNumber() may have side effects so must relookup 'tv'. */
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
return d;
}
DUK_INTERNAL duk_double_t duk_to_number_m1(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
return duk_to_number(thr, -1);
}
DUK_INTERNAL duk_double_t duk_to_number_m2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
return duk_to_number(thr, -2);
}
DUK_INTERNAL duk_double_t duk_to_number_tval(duk_hthread *thr, duk_tval *tv) {
#if defined(DUK_USE_PREFER_SIZE)
duk_double_t res;
DUK_ASSERT_API_ENTRY(thr);
duk_push_tval(thr, tv);
res = duk_to_number_m1(thr);
duk_pop_unsafe(thr);
return res;
#else
duk_double_t res;
duk_tval *tv_dst;
DUK_ASSERT_API_ENTRY(thr);
DUK__ASSERT_SPACE();
tv_dst = thr->valstack_top++;
DUK_TVAL_SET_TVAL(tv_dst, tv);
DUK_TVAL_INCREF(thr, tv_dst); /* decref not necessary */
res = duk_to_number_m1(thr); /* invalidates tv_dst */
tv_dst = --thr->valstack_top;
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_dst));
DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv_dst)); /* plain number */
DUK_TVAL_SET_UNDEFINED(tv_dst); /* valstack init policy */
return res;
#endif
}
/* XXX: combine all the integer conversions: they share everything
* but the helper function for coercion.
*/
typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv);
DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_hthread *thr, duk_idx_t idx, duk__toint_coercer coerce_func) {
duk_tval *tv;
duk_double_t d;
DUK_ASSERT_CTX_VALID(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
#if defined(DUK_USE_FASTINT)
/* If argument is a fastint, guarantee that it remains one.
* There's no downgrade check for other cases.
*/
if (DUK_TVAL_IS_FASTINT(tv)) {
/* XXX: Unnecessary conversion back and forth. */
return (duk_double_t) DUK_TVAL_GET_FASTINT(tv);
}
#endif
d = coerce_func(thr, tv);
/* XXX: fastint? */
/* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
tv = duk_require_tval(thr, idx);
DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
return d;
}
DUK_EXTERNAL duk_int_t duk_to_int(duk_hthread *thr, duk_idx_t idx) {
/* Value coercion (in stack): ToInteger(), E5 Section 9.4,
* API return value coercion: custom.
*/
DUK_ASSERT_API_ENTRY(thr);
(void) duk__to_int_uint_helper(thr, idx, duk_js_tointeger);
return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 0 /*require*/);
}
DUK_EXTERNAL duk_uint_t duk_to_uint(duk_hthread *thr, duk_idx_t idx) {
/* Value coercion (in stack): ToInteger(), E5 Section 9.4,
* API return value coercion: custom.
*/
DUK_ASSERT_API_ENTRY(thr);
(void) duk__to_int_uint_helper(thr, idx, duk_js_tointeger);
return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 0 /*require*/);
}
DUK_EXTERNAL duk_int32_t duk_to_int32(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_int32_t ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
ret = duk_js_toint32(thr, tv);
/* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
tv = duk_require_tval(thr, idx);
DUK_TVAL_SET_I32_UPDREF(thr, tv, ret); /* side effects */
return ret;
}
DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_uint32_t ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
ret = duk_js_touint32(thr, tv);
/* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
tv = duk_require_tval(thr, idx);
DUK_TVAL_SET_U32_UPDREF(thr, tv, ret); /* side effects */
return ret;
}
DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_uint16_t ret;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
ret = duk_js_touint16(thr, tv);
/* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
tv = duk_require_tval(thr, idx);
DUK_TVAL_SET_U32_UPDREF(thr, tv, ret); /* side effects */
return ret;
}
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
/* Special coercion for Uint8ClampedArray. */
DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_hthread *thr, duk_idx_t idx) {
duk_double_t d;
duk_double_t t;
duk_uint8_t ret;
DUK_ASSERT_API_ENTRY(thr);
/* XXX: Simplify this algorithm, should be possible to come up with
* a shorter and faster algorithm by inspecting IEEE representation
* directly.
*/
d = duk_to_number(thr, idx);
if (d <= 0.0) {
return 0;
} else if (d >= 255) {
return 255;
} else if (DUK_ISNAN(d)) {
/* Avoid NaN-to-integer coercion as it is compiler specific. */
return 0;
}
t = d - DUK_FLOOR(d);
if (t == 0.5) {
/* Exact halfway, round to even. */
ret = (duk_uint8_t) d;
ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4
* Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4
*/
} else {
/* Not halfway, round to nearest. */
ret = (duk_uint8_t) (d + 0.5);
}
return ret;
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
DUK_EXTERNAL const char *duk_to_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
DUK_ASSERT_API_ENTRY(thr);
(void) duk_to_string(thr, idx);
DUK_ASSERT(duk_is_string(thr, idx));
return duk_require_lstring(thr, idx, out_len);
}
DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_hthread *thr, void *udata) {
DUK_ASSERT_CTX_VALID(thr);
DUK_UNREF(udata);
duk_to_string(thr, -1);
return 1;
}
DUK_EXTERNAL const char *duk_safe_to_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
/* We intentionally ignore the duk_safe_call() return value and only
* check the output type. This way we don't also need to check that
* the returned value is indeed a string in the success case.
*/
duk_dup(thr, idx);
(void) duk_safe_call(thr, duk__safe_to_string_raw, NULL /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
if (!duk_is_string(thr, -1)) {
/* Error: try coercing error to string once. */
(void) duk_safe_call(thr, duk__safe_to_string_raw, NULL /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
if (!duk_is_string(thr, -1)) {
/* Double error */
duk_pop_unsafe(thr);
duk_push_hstring_stridx(thr, DUK_STRIDX_UC_ERROR);
} else {
;
}
} else {
/* String; may be a symbol, accepted. */
;
}
DUK_ASSERT(duk_is_string(thr, -1));
duk_replace(thr, idx);
DUK_ASSERT(duk_get_string(thr, idx) != NULL);
return duk_get_lstring(thr, idx, out_len);
}
DUK_INTERNAL duk_hstring *duk_to_property_key_hstring(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
duk_to_primitive(thr, idx, DUK_HINT_STRING); /* needed for e.g. Symbol objects */
h = duk_get_hstring(thr, idx);
if (h == NULL) {
/* The "is string?" check may seem unnecessary, but as things
* are duk_to_hstring() invokes ToString() which fails for
* symbols. But since symbols are already strings for Duktape
* C API, we check for that before doing the coercion.
*/
h = duk_to_hstring(thr, idx);
}
DUK_ASSERT(h != NULL);
return h;
}
#if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
DUK_INTERNAL duk_hstring *duk_safe_to_hstring(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
(void) duk_safe_to_string(thr, idx);
DUK_ASSERT(duk_is_string(thr, idx));
DUK_ASSERT(duk_get_hstring(thr, idx) != NULL);
return duk_known_hstring(thr, idx);
}
#endif
/* Push Object.prototype.toString() output for 'tv'. */
#if 0 /* See XXX note why this variant doesn't work. */
DUK_INTERNAL void duk_push_class_string_tval(duk_hthread *thr, duk_tval *tv, duk_bool_t avoid_side_effects) {
duk_uint_t stridx_bidx = 0; /* (prototype_bidx << 16) + default_tag_stridx */
DUK_ASSERT_API_ENTRY(thr);
/* Conceptually for any non-undefined/null value we should do a
* ToObject() coercion and look up @@toStringTag (from the object
* prototype) to see if a custom tag should be used. Avoid the
* actual conversion by doing a prototype lookup without the object
* coercion. However, see problem below.
*/
duk_push_literal(thr, "[object "); /* -> [ ... "[object" ] */
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNUSED: /* Treat like 'undefined', shouldn't happen. */
case DUK_TAG_UNDEFINED: {
stridx_bidx = DUK_STRIDX_UC_UNDEFINED;
goto use_stridx;
}
case DUK_TAG_NULL: {
stridx_bidx = DUK_STRIDX_UC_NULL;
goto use_stridx;
}
case DUK_TAG_BOOLEAN: {
stridx_bidx = (DUK_BIDX_BOOLEAN_PROTOTYPE << 16) + DUK_STRIDX_UC_BOOLEAN;
goto use_proto_bidx;
}
case DUK_TAG_POINTER: {
stridx_bidx = (DUK_BIDX_POINTER_PROTOTYPE << 16) + DUK_STRIDX_UC_POINTER;
goto use_proto_bidx;
}
case DUK_TAG_LIGHTFUNC: {
stridx_bidx = (DUK_BIDX_FUNCTION_PROTOTYPE << 16) + DUK_STRIDX_UC_FUNCTION;
goto use_proto_bidx;
}
case DUK_TAG_STRING: {
duk_hstring *h;
h = DUK_TVAL_GET_STRING(tv);
DUK_ASSERT(h != NULL);
if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
/* Even without DUK_USE_SYMBOL_BUILTIN the Symbol
* prototype exists so we can lookup @@toStringTag
* and provide [object Symbol] for symbol values
* created from C code.
*/
stridx_bidx = (DUK_BIDX_SYMBOL_PROTOTYPE << 16) + DUK_STRIDX_UC_SYMBOL;
} else {
stridx_bidx = (DUK_BIDX_STRING_PROTOTYPE << 16) + DUK_STRIDX_UC_STRING;
}
goto use_proto_bidx;
}
case DUK_TAG_OBJECT: {
duk_push_tval(thr, tv);
stridx_bidx = 0xffffffffUL; /* Marker value. */
goto use_pushed_object;
}
case DUK_TAG_BUFFER: {
stridx_bidx = (DUK_BIDX_UINT8ARRAY_PROTOTYPE << 16) + DUK_STRIDX_UINT8_ARRAY;
goto use_proto_bidx;
}
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
/* Fall through to generic number case. */
#endif
default: {
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* number (maybe fastint) */
stridx_bidx = (DUK_BIDX_NUMBER_PROTOTYPE << 16) + DUK_STRIDX_UC_NUMBER;
goto use_proto_bidx;
}
}
DUK_ASSERT(0); /* Never here. */
use_proto_bidx:
DUK_ASSERT_BIDX_VALID((stridx_bidx >> 16) & 0xffffUL);
duk_push_hobject(thr, thr->builtins[(stridx_bidx >> 16) & 0xffffUL]);
/* Fall through. */
use_pushed_object:
/* [ ... "[object" obj ] */
#if defined(DUK_USE_SYMBOL_BUILTIN)
/* XXX: better handling with avoid_side_effects == 1; lookup tval
* without Proxy or getter side effects, and use it in sanitized
* form if it's a string.
*/
if (!avoid_side_effects) {
/* XXX: The problem with using the prototype object as the
* lookup base is that if @@toStringTag is a getter, its
* 'this' binding must be the ToObject() coerced input value,
* not the prototype object of the type.
*/
(void) duk_get_prop_stridx(thr, -1, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG);
if (duk_is_string_notsymbol(thr, -1)) {
duk_remove_m2(thr);
goto finish;
}
duk_pop_unsafe(thr);
}
#endif
if (stridx_bidx == 0xffffffffUL) {
duk_hobject *h_obj;
duk_small_uint_t classnum;
h_obj = duk_known_hobject(thr, -1);
DUK_ASSERT(h_obj != NULL);
classnum = DUK_HOBJECT_GET_CLASS_NUMBER(h_obj);
stridx_bidx = DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum);
} else {
/* stridx_bidx already has the desired fallback stridx. */
;
}
duk_pop_unsafe(thr);
/* Fall through. */
use_stridx:
/* [ ... "[object" ] */
duk_push_hstring_stridx(thr, stridx_bidx & 0xffffUL);
finish:
/* [ ... "[object" tag ] */
duk_push_literal(thr, "]");
duk_concat(thr, 3); /* [ ... "[object" tag "]" ] -> [ ... res ] */
}
#endif /* 0 */
DUK_INTERNAL void duk_push_class_string_tval(duk_hthread *thr, duk_tval *tv, duk_bool_t avoid_side_effects) {
duk_hobject *h_obj;
duk_small_uint_t classnum;
duk_small_uint_t stridx;
duk_tval tv_tmp;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(tv != NULL);
/* Stabilize 'tv', duk_push_literal() may trigger side effects. */
DUK_TVAL_SET_TVAL(&tv_tmp, tv);
tv = &tv_tmp;
/* Conceptually for any non-undefined/null value we should do a
* ToObject() coercion and look up @@toStringTag (from the object
* prototype) to see if a custom result should be used. We'd like to
* avoid the actual conversion, but even for primitive types the
* prototype may have @@toStringTag. What's worse, the @@toStringTag
* property may be a getter that must get the object coerced value
* (not the prototype) as its 'this' binding.
*
* For now, do an actual object coercion. This could be avoided by
* doing a side effect free lookup to see if a getter would be invoked.
* If not, the value can be read directly and the object coercion could
* be avoided. This may not be worth it in practice, because
* Object.prototype.toString() is usually not performance critical.
*/
duk_push_literal(thr, "[object "); /* -> [ ... "[object" ] */
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNUSED: /* Treat like 'undefined', shouldn't happen. */
case DUK_TAG_UNDEFINED: {
duk_push_hstring_stridx(thr, DUK_STRIDX_UC_UNDEFINED);
goto finish;
}
case DUK_TAG_NULL: {
duk_push_hstring_stridx(thr, DUK_STRIDX_UC_NULL);
goto finish;
}
}
duk_push_tval(thr, tv);
tv = NULL; /* Invalidated by ToObject(). */
duk_to_object(thr, -1);
/* [ ... "[object" obj ] */
#if defined(DUK_USE_SYMBOL_BUILTIN)
/* XXX: better handling with avoid_side_effects == 1; lookup tval
* without Proxy or getter side effects, and use it in sanitized
* form if it's a string.
*/
if (!avoid_side_effects) {
(void) duk_get_prop_stridx(thr, -1, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG);
if (duk_is_string_notsymbol(thr, -1)) {
duk_remove_m2(thr);
goto finish;
}
duk_pop_unsafe(thr);
}
#else
DUK_UNREF(avoid_side_effects);
#endif
h_obj = duk_known_hobject(thr, -1);
DUK_ASSERT(h_obj != NULL);
classnum = DUK_HOBJECT_GET_CLASS_NUMBER(h_obj);
stridx = DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum);
duk_pop_unsafe(thr);
duk_push_hstring_stridx(thr, stridx);
finish:
/* [ ... "[object" tag ] */
duk_push_literal(thr, "]");
duk_concat(thr, 3); /* [ ... "[object" tag "]" ] -> [ ... res ] */
}
/* XXX: other variants like uint, u32 etc */
DUK_INTERNAL duk_int_t duk_to_int_clamped_raw(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped) {
duk_tval *tv;
duk_tval tv_tmp;
duk_double_t d, dmin, dmax;
duk_int_t res;
duk_bool_t clamped = 0;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
DUK_ASSERT(tv != NULL);
d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */
dmin = (duk_double_t) minval;
dmax = (duk_double_t) maxval;
if (d < dmin) {
clamped = 1;
res = minval;
d = dmin;
} else if (d > dmax) {
clamped = 1;
res = maxval;
d = dmax;
} else {
res = (duk_int_t) d;
}
DUK_UNREF(d); /* SCANBUILD: with suitable dmin/dmax limits 'd' is unused */
/* 'd' and 'res' agree here */
/* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */
tv = duk_get_tval(thr, idx);
DUK_ASSERT(tv != NULL); /* not popped by side effect */
DUK_TVAL_SET_TVAL(&tv_tmp, tv);
#if defined(DUK_USE_FASTINT)
#if (DUK_INT_MAX <= 0x7fffffffL)
DUK_TVAL_SET_I32(tv, res);
#else
/* Clamping needed if duk_int_t is 64 bits. */
if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) {
DUK_TVAL_SET_FASTINT(tv, res);
} else {
DUK_TVAL_SET_NUMBER(tv, d);
}
#endif
#else
DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
#endif
DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
if (out_clamped) {
*out_clamped = clamped;
} else {
/* coerced value is updated to value stack even when RangeError thrown */
if (clamped) {
DUK_ERROR_RANGE(thr, DUK_STR_NUMBER_OUTSIDE_RANGE);
DUK_WO_NORETURN(return 0;);
}
}
return res;
}
DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_hthread *thr, duk_idx_t idx, duk_idx_t minval, duk_idx_t maxval) {
duk_bool_t dummy;
DUK_ASSERT_API_ENTRY(thr);
return duk_to_int_clamped_raw(thr, idx, minval, maxval, &dummy);
}
DUK_INTERNAL duk_int_t duk_to_int_check_range(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval) {
DUK_ASSERT_API_ENTRY(thr);
return duk_to_int_clamped_raw(thr, idx, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */
}
DUK_EXTERNAL const char *duk_to_string(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNDEFINED: {
duk_push_hstring_stridx(thr, DUK_STRIDX_LC_UNDEFINED);
break;
}
case DUK_TAG_NULL: {
duk_push_hstring_stridx(thr, DUK_STRIDX_LC_NULL);
break;
}
case DUK_TAG_BOOLEAN: {
if (DUK_TVAL_GET_BOOLEAN(tv)) {
duk_push_hstring_stridx(thr, DUK_STRIDX_TRUE);
} else {
duk_push_hstring_stridx(thr, DUK_STRIDX_FALSE);
}
break;
}
case DUK_TAG_STRING: {
/* Nop for actual strings, TypeError for Symbols.
* Because various internals rely on ToString() coercion of
* internal strings, -allow- (NOP) string coercion for hidden
* symbols.
*/
#if 1
duk_hstring *h;
h = DUK_TVAL_GET_STRING(tv);
DUK_ASSERT(h != NULL);
if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
DUK_ERROR_TYPE(thr, DUK_STR_CANNOT_STRING_COERCE_SYMBOL);
DUK_WO_NORETURN(goto skip_replace;);
} else {
goto skip_replace;
}
#else
goto skip_replace;
#endif
break;
}
case DUK_TAG_BUFFER: /* Go through Uint8Array.prototype.toString() for coercion. */
case DUK_TAG_OBJECT: {
/* Plain buffers: go through ArrayBuffer.prototype.toString()
* for coercion.
*
* Symbol objects: duk_to_primitive() results in a plain symbol
* value, and duk_to_string() then causes a TypeError.
*/
duk_to_primitive(thr, idx, DUK_HINT_STRING);
DUK_ASSERT(!duk_is_buffer(thr, idx)); /* ToPrimitive() must guarantee */
DUK_ASSERT(!duk_is_object(thr, idx));
return duk_to_string(thr, idx); /* Note: recursive call */
}
case DUK_TAG_POINTER: {
void *ptr = DUK_TVAL_GET_POINTER(tv);
if (ptr != NULL) {
duk_push_sprintf(thr, DUK_STR_FMT_PTR, (void *) ptr);
} else {
/* Represent a null pointer as 'null' to be consistent with
* the JX format variant. Native '%p' format for a NULL
* pointer may be e.g. '(nil)'.
*/
duk_push_hstring_stridx(thr, DUK_STRIDX_LC_NULL);
}
break;
}
case DUK_TAG_LIGHTFUNC: {
/* Should match Function.prototype.toString() */
duk_push_lightfunc_tostring(thr, tv);
break;
}
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default: {
/* number */
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
duk_push_tval(thr, tv);
duk_numconv_stringify(thr,
10 /*radix*/,
0 /*precision:shortest*/,
0 /*force_exponential*/);
break;
}
}
duk_replace(thr, idx);
skip_replace:
DUK_ASSERT(duk_is_string(thr, idx));
return duk_require_string(thr, idx);
}
DUK_INTERNAL duk_hstring *duk_to_hstring(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *ret;
DUK_ASSERT_API_ENTRY(thr);
duk_to_string(thr, idx);
ret = duk_get_hstring(thr, idx);
DUK_ASSERT(ret != NULL);
return ret;
}
DUK_INTERNAL duk_hstring *duk_to_hstring_m1(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
return duk_to_hstring(thr, -1);
}
DUK_INTERNAL duk_hstring *duk_to_hstring_acceptsymbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *ret;
DUK_ASSERT_API_ENTRY(thr);
ret = duk_get_hstring(thr, idx);
if (DUK_UNLIKELY(ret && DUK_HSTRING_HAS_SYMBOL(ret))) {
return ret;
}
return duk_to_hstring(thr, idx);
}
/* Convert a plain buffer or any buffer object into a string, using the buffer
* bytes 1:1 in the internal string representation. For views the active byte
* slice (not element slice interpreted as an initializer) is used. This is
* necessary in Duktape 2.x because ToString(plainBuffer) no longer creates a
* string with the same bytes as in the buffer but rather (usually)
* '[object ArrayBuffer]'.
*/
DUK_EXTERNAL const char *duk_buffer_to_string(duk_hthread *thr, duk_idx_t idx) {
void *ptr_src;
duk_size_t len;
const char *res;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
ptr_src = duk_require_buffer_data(thr, idx, &len);
DUK_ASSERT(ptr_src != NULL || len == 0);
res = duk_push_lstring(thr, (const char *) ptr_src, len);
duk_replace(thr, idx);
return res;
}
DUK_EXTERNAL void *duk_to_buffer_raw(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, duk_uint_t mode) {
duk_hbuffer *h_buf;
const duk_uint8_t *src_data;
duk_size_t src_size;
duk_uint8_t *dst_data;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
h_buf = duk_get_hbuffer(thr, idx);
if (h_buf != NULL) {
/* Buffer is kept as is, with the fixed/dynamic nature of the
* buffer only changed if requested. An external buffer
* is converted into a non-external dynamic buffer in a
* duk_to_dynamic_buffer() call.
*/
duk_uint_t tmp;
duk_uint8_t *tmp_ptr;
tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf);
src_data = (const duk_uint8_t *) tmp_ptr;
src_size = DUK_HBUFFER_GET_SIZE(h_buf);
tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED);
if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) ||
mode == DUK_BUF_MODE_DONTCARE) {
/* Note: src_data may be NULL if input is a zero-size
* dynamic buffer.
*/
dst_data = tmp_ptr;
goto skip_copy;
}
} else {
/* Non-buffer value is first ToString() coerced, then converted
* to a buffer (fixed buffer is used unless a dynamic buffer is
* explicitly requested). Symbols are rejected with a TypeError.
* XXX: C API could maybe allow symbol-to-buffer coercion?
*/
src_data = (const duk_uint8_t *) duk_to_lstring(thr, idx, &src_size);
}
dst_data = (duk_uint8_t *) duk_push_buffer(thr, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/);
/* dst_data may be NULL if size is zero. */
duk_memcpy_unsafe((void *) dst_data, (const void *) src_data, (size_t) src_size);
duk_replace(thr, idx);
skip_copy:
if (out_size) {
*out_size = src_size;
}
return dst_data;
}
DUK_EXTERNAL void *duk_to_pointer(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
void *res;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNDEFINED:
case DUK_TAG_NULL:
case DUK_TAG_BOOLEAN:
res = NULL;
break;
case DUK_TAG_POINTER:
res = DUK_TVAL_GET_POINTER(tv);
break;
case DUK_TAG_STRING:
case DUK_TAG_OBJECT:
case DUK_TAG_BUFFER:
/* Heap allocated: return heap pointer which is NOT useful
* for the caller, except for debugging.
*/
res = (void *) DUK_TVAL_GET_HEAPHDR(tv);
break;
case DUK_TAG_LIGHTFUNC:
/* Function pointers do not always cast correctly to void *
* (depends on memory and segmentation model for instance),
* so they coerce to NULL.
*/
res = NULL;
break;
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default:
/* number */
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
res = NULL;
break;
}
duk_push_pointer(thr, res);
duk_replace(thr, idx);
return res;
}
DUK_LOCAL void duk__push_func_from_lightfunc(duk_hthread *thr, duk_c_function func, duk_small_uint_t lf_flags) {
duk_idx_t nargs;
duk_uint_t flags = 0; /* shared flags for a subset of types */
duk_small_uint_t lf_len;
duk_hnatfunc *nf;
nargs = (duk_idx_t) DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
if (nargs == DUK_LFUNC_NARGS_VARARGS) {
nargs = (duk_idx_t) DUK_VARARGS;
}
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_NATFUNC |
DUK_HOBJECT_FLAG_NEWENV |
DUK_HOBJECT_FLAG_STRICT |
DUK_HOBJECT_FLAG_NOTAIL |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
(void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE);
lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
if ((duk_idx_t) lf_len != nargs) {
/* Explicit length is only needed if it differs from 'nargs'. */
duk_push_int(thr, (duk_int_t) lf_len);
duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
}
#if defined(DUK_USE_FUNC_NAME_PROPERTY)
duk_push_lightfunc_name_raw(thr, func, lf_flags);
duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
#endif
nf = duk_known_hnatfunc(thr, -1);
nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
}
DUK_EXTERNAL void duk_to_object(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_uint_t flags = 0; /* shared flags for a subset of types */
duk_small_int_t proto = 0;
DUK_ASSERT_API_ENTRY(thr);
idx = duk_require_normalize_index(thr, idx);
tv = DUK_GET_TVAL_POSIDX(thr, idx);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
#if !defined(DUK_USE_BUFFEROBJECT_SUPPORT)
case DUK_TAG_BUFFER: /* With no bufferobject support, don't object coerce. */
#endif
case DUK_TAG_UNDEFINED:
case DUK_TAG_NULL: {
DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE);
DUK_WO_NORETURN(return;);
break;
}
case DUK_TAG_BOOLEAN: {
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN);
proto = DUK_BIDX_BOOLEAN_PROTOTYPE;
goto create_object;
}
case DUK_TAG_STRING: {
duk_hstring *h;
h = DUK_TVAL_GET_STRING(tv);
DUK_ASSERT(h != NULL);
if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_SYMBOL);
proto = DUK_BIDX_SYMBOL_PROTOTYPE;
} else {
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
proto = DUK_BIDX_STRING_PROTOTYPE;
}
goto create_object;
}
case DUK_TAG_OBJECT: {
/* nop */
break;
}
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
case DUK_TAG_BUFFER: {
/* A plain buffer object coerces to a full ArrayBuffer which
* is not fully transparent behavior (ToObject() should be a
* nop for an object). This behavior matches lightfuncs which
* also coerce to an equivalent Function object. There are
* also downsides to defining ToObject(plainBuffer) as a no-op;
* for example duk_to_hobject() could result in a NULL pointer.
*/
duk_hbuffer *h_buf;
h_buf = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h_buf != NULL);
duk_hbufobj_push_uint8array_from_plain(thr, h_buf);
goto replace_value;
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
case DUK_TAG_POINTER: {
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER);
proto = DUK_BIDX_POINTER_PROTOTYPE;
goto create_object;
}
case DUK_TAG_LIGHTFUNC: {
/* Lightfunc coerces to a Function instance with concrete
* properties. Since 'length' is virtual for Duktape/C
* functions, don't need to define that. The result is made
* extensible to mimic what happens to strings in object
* coercion:
*
* > Object.isExtensible(Object('foo'))
* true
*/
duk_small_uint_t lf_flags;
duk_c_function func;
DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
duk__push_func_from_lightfunc(thr, func, lf_flags);
goto replace_value;
}
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default: {
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
proto = DUK_BIDX_NUMBER_PROTOTYPE;
goto create_object;
}
}
DUK_ASSERT(duk_is_object(thr, idx));
return;
create_object:
(void) duk_push_object_helper(thr, flags, proto);
/* Note: Boolean prototype's internal value property is not writable,
* but duk_xdef_prop_stridx() disregards the write protection. Boolean
* instances are immutable.
*
* String and buffer special behaviors are already enabled which is not
* ideal, but a write to the internal value is not affected by them.
*/
duk_dup(thr, idx);
duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
replace_value:
duk_replace(thr, idx);
DUK_ASSERT(duk_is_object(thr, idx));
}
DUK_INTERNAL duk_hobject *duk_to_hobject(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *ret;
DUK_ASSERT_API_ENTRY(thr);
duk_to_object(thr, idx);
ret = duk_known_hobject(thr, idx);
return ret;
}
/*
* Type checking
*/
DUK_LOCAL duk_bool_t duk__tag_check(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t tag) {
duk_tval *tv;
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
return (DUK_TVAL_GET_TAG(tv) == tag);
}
DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_hthread *thr, duk_idx_t idx, duk_uint_t flag_mask) {
duk_hobject *obj;
DUK_ASSERT_API_ENTRY(thr);
obj = duk_get_hobject(thr, idx);
if (obj) {
return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0);
}
return 0;
}
DUK_INTERNAL duk_int_t duk_get_type_tval(duk_tval *tv) {
DUK_ASSERT(tv != NULL);
#if defined(DUK_USE_PACKED_TVAL)
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNUSED:
return DUK_TYPE_NONE;
case DUK_TAG_UNDEFINED:
return DUK_TYPE_UNDEFINED;
case DUK_TAG_NULL:
return DUK_TYPE_NULL;
case DUK_TAG_BOOLEAN:
return DUK_TYPE_BOOLEAN;
case DUK_TAG_STRING:
return DUK_TYPE_STRING;
case DUK_TAG_OBJECT:
return DUK_TYPE_OBJECT;
case DUK_TAG_BUFFER:
return DUK_TYPE_BUFFER;
case DUK_TAG_POINTER:
return DUK_TYPE_POINTER;
case DUK_TAG_LIGHTFUNC:
return DUK_TYPE_LIGHTFUNC;
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default:
/* Note: number has no explicit tag (in 8-byte representation) */
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
return DUK_TYPE_NUMBER;
}
#else /* DUK_USE_PACKED_TVAL */
DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
DUK_ASSERT(sizeof(duk__type_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
return (duk_int_t) duk__type_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
#endif /* DUK_USE_PACKED_TVAL */
}
DUK_EXTERNAL duk_int_t duk_get_type(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
return duk_get_type_tval(tv);
}
#if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS)
DUK_LOCAL const char * const duk__type_names[] = {
"none",
"undefined",
"null",
"boolean",
"number",
"string",
"object",
"buffer",
"pointer",
"lightfunc"
};
DUK_INTERNAL const char *duk_get_type_name(duk_hthread *thr, duk_idx_t idx) {
duk_int_t type_tag;
DUK_ASSERT_API_ENTRY(thr);
type_tag = duk_get_type(thr, idx);
DUK_ASSERT(type_tag >= DUK_TYPE_MIN && type_tag <= DUK_TYPE_MAX);
DUK_ASSERT(DUK_TYPE_MIN == 0 && sizeof(duk__type_names) / sizeof(const char *) == DUK_TYPE_MAX + 1);
return duk__type_names[type_tag];
}
#endif /* DUK_USE_VERBOSE_ERRORS && DUK_USE_PARANOID_ERRORS */
DUK_INTERNAL duk_small_uint_t duk_get_class_number(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
duk_hobject *obj;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_OBJECT:
obj = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(obj != NULL);
return DUK_HOBJECT_GET_CLASS_NUMBER(obj);
case DUK_TAG_BUFFER:
/* Buffers behave like Uint8Array objects. */
return DUK_HOBJECT_CLASS_UINT8ARRAY;
case DUK_TAG_LIGHTFUNC:
/* Lightfuncs behave like Function objects. */
return DUK_HOBJECT_CLASS_FUNCTION;
default:
/* Primitive or UNUSED, no class number. */
return DUK_HOBJECT_CLASS_NONE;
}
}
DUK_EXTERNAL duk_bool_t duk_check_type(duk_hthread *thr, duk_idx_t idx, duk_int_t type) {
DUK_ASSERT_API_ENTRY(thr);
return (duk_get_type(thr, idx) == type) ? 1 : 0;
}
DUK_INTERNAL duk_uint_t duk_get_type_mask_tval(duk_tval *tv) {
DUK_ASSERT(tv != NULL);
#if defined(DUK_USE_PACKED_TVAL)
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_UNUSED:
return DUK_TYPE_MASK_NONE;
case DUK_TAG_UNDEFINED:
return DUK_TYPE_MASK_UNDEFINED;
case DUK_TAG_NULL:
return DUK_TYPE_MASK_NULL;
case DUK_TAG_BOOLEAN:
return DUK_TYPE_MASK_BOOLEAN;
case DUK_TAG_STRING:
return DUK_TYPE_MASK_STRING;
case DUK_TAG_OBJECT:
return DUK_TYPE_MASK_OBJECT;
case DUK_TAG_BUFFER:
return DUK_TYPE_MASK_BUFFER;
case DUK_TAG_POINTER:
return DUK_TYPE_MASK_POINTER;
case DUK_TAG_LIGHTFUNC:
return DUK_TYPE_MASK_LIGHTFUNC;
#if defined(DUK_USE_FASTINT)
case DUK_TAG_FASTINT:
#endif
default:
/* Note: number has no explicit tag (in 8-byte representation) */
DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
return DUK_TYPE_MASK_NUMBER;
}
#else /* DUK_USE_PACKED_TVAL */
DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
DUK_ASSERT(sizeof(duk__type_mask_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
return duk__type_mask_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
#endif /* DUK_USE_PACKED_TVAL */
}
DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
return duk_get_type_mask_tval(tv);
}
DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t mask) {
DUK_ASSERT_API_ENTRY(thr);
if (DUK_LIKELY((duk_get_type_mask(thr, idx) & mask) != 0U)) {
return 1;
}
if (mask & DUK_TYPE_MASK_THROW) {
DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE);
DUK_WO_NORETURN(return 0;);
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_UNDEFINED);
}
DUK_EXTERNAL duk_bool_t duk_is_null(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_NULL);
}
DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_BOOLEAN);
}
DUK_EXTERNAL duk_bool_t duk_is_number(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
/*
* Number is special because it doesn't have a specific
* tag in the 8-byte representation.
*/
/* XXX: shorter version for unpacked representation? */
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
return DUK_TVAL_IS_NUMBER(tv);
}
DUK_EXTERNAL duk_bool_t duk_is_nan(duk_hthread *thr, duk_idx_t idx) {
/* XXX: This will now return false for non-numbers, even though they would
* coerce to NaN (as a general rule). In particular, duk_get_number()
* returns a NaN for non-numbers, so should this function also return
* true for non-numbers?
*/
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
/* XXX: for packed duk_tval an explicit "is number" check is unnecessary */
if (!DUK_TVAL_IS_NUMBER(tv)) {
return 0;
}
return (duk_bool_t) DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv));
}
DUK_EXTERNAL duk_bool_t duk_is_string(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_STRING);
}
DUK_INTERNAL duk_bool_t duk_is_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_get_hstring_notsymbol(thr, idx) != NULL;
}
DUK_EXTERNAL duk_bool_t duk_is_object(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_OBJECT);
}
DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_BUFFER);
}
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_EXTERNAL duk_bool_t duk_is_buffer_data(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BUFFER(tv)) {
return 1;
} else if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
if (DUK_HOBJECT_IS_BUFOBJ(h)) {
return 1;
}
}
return 0;
}
#else /* DUK_USE_BUFFEROBJECT_SUPPORT */
DUK_EXTERNAL duk_bool_t duk_is_buffer_data(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_is_buffer(thr, idx);
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_POINTER);
}
DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__tag_check(thr, idx, DUK_TAG_LIGHTFUNC);
}
DUK_EXTERNAL duk_bool_t duk_is_symbol(duk_hthread *thr, duk_idx_t idx) {
duk_hstring *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hstring(thr, idx);
/* Use DUK_LIKELY() here because caller may be more likely to type
* check an expected symbol than not.
*/
if (DUK_LIKELY(h != NULL && DUK_HSTRING_HAS_SYMBOL(h))) {
return 1;
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_array(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *obj;
DUK_ASSERT_API_ENTRY(thr);
obj = duk_get_hobject(thr, idx);
if (obj) {
return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0);
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_function(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *h;
h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
}
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
return 1;
}
return 0;
}
DUK_INTERNAL duk_bool_t duk_is_callable_tval(duk_hthread *thr, duk_tval *tv) {
DUK_ASSERT_API_ENTRY(thr);
DUK_UNREF(thr);
if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *h;
h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
}
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
return 1;
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_constructable(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
if (DUK_TVAL_IS_OBJECT(tv)) {
duk_hobject *h;
h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
return DUK_HOBJECT_HAS_CONSTRUCTABLE(h) ? 1 : 0;
}
if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
return 1;
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__obj_flag_any_default_false(thr,
idx,
DUK_HOBJECT_FLAG_NATFUNC);
}
DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__obj_flag_any_default_false(thr,
idx,
DUK_HOBJECT_FLAG_COMPFUNC);
}
DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk__obj_flag_any_default_false(thr,
idx,
DUK_HOBJECT_FLAG_BOUNDFUNC);
}
DUK_EXTERNAL duk_bool_t duk_is_thread(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *obj;
DUK_ASSERT_API_ENTRY(thr);
obj = duk_get_hobject(thr, idx);
if (obj) {
return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_THREAD ? 1 : 0);
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BUFFER(tv)) {
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1);
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BUFFER(tv)) {
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
}
return 0;
}
DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_get_tval_or_unused(thr, idx);
DUK_ASSERT(tv != NULL);
if (DUK_TVAL_IS_BUFFER(tv)) {
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
}
return 0;
}
DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_hthread *thr, duk_idx_t idx) {
duk_hobject *h;
duk_uint_t sanity;
DUK_ASSERT_API_ENTRY(thr);
h = duk_get_hobject(thr, idx);
sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
do {
if (!h) {
return DUK_ERR_NONE;
}
/* XXX: something more convenient? */
if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) {
return DUK_ERR_EVAL_ERROR;
}
if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) {
return DUK_ERR_RANGE_ERROR;
}
if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) {
return DUK_ERR_REFERENCE_ERROR;
}
if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) {
return DUK_ERR_SYNTAX_ERROR;
}
if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) {
return DUK_ERR_TYPE_ERROR;
}
if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) {
return DUK_ERR_URI_ERROR;
}
if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) {
return DUK_ERR_ERROR;
}
h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
} while (--sanity > 0);
return DUK_ERR_NONE;
}
/*
* Pushers
*/
DUK_INTERNAL void duk_push_tval(duk_hthread *thr, duk_tval *tv) {
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(tv != NULL);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_TVAL(tv_slot, tv);
DUK_TVAL_INCREF(thr, tv); /* no side effects */
}
DUK_EXTERNAL void duk_push_undefined(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
/* Because value stack init policy is 'undefined above top',
* we don't need to write, just assert.
*/
thr->valstack_top++;
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
}
DUK_EXTERNAL void duk_push_null(duk_hthread *thr) {
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_NULL(tv_slot);
}
DUK_EXTERNAL void duk_push_boolean(duk_hthread *thr, duk_bool_t val) {
duk_tval *tv_slot;
duk_small_int_t b;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_BOOLEAN(tv_slot, b);
}
DUK_EXTERNAL void duk_push_true(duk_hthread *thr) {
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot);
}
DUK_EXTERNAL void duk_push_false(duk_hthread *thr) {
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot);
}
/* normalize NaN which may not match our canonical internal NaN */
DUK_EXTERNAL void duk_push_number(duk_hthread *thr, duk_double_t val) {
duk_tval *tv_slot;
duk_double_union du;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
du.d = val;
DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_NUMBER(tv_slot, du.d);
}
DUK_EXTERNAL void duk_push_int(duk_hthread *thr, duk_int_t val) {
#if defined(DUK_USE_FASTINT)
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
#if DUK_INT_MAX <= 0x7fffffffL
DUK_TVAL_SET_I32(tv_slot, (duk_int32_t) val);
#else
if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) {
DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
} else {
duk_double_t = (duk_double_t) val;
DUK_TVAL_SET_NUMBER(tv_slot, d);
}
#endif
#else /* DUK_USE_FASTINT */
duk_tval *tv_slot;
duk_double_t d;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
d = (duk_double_t) val;
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_NUMBER(tv_slot, d);
#endif /* DUK_USE_FASTINT */
}
DUK_EXTERNAL void duk_push_uint(duk_hthread *thr, duk_uint_t val) {
#if defined(DUK_USE_FASTINT)
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
#if DUK_UINT_MAX <= 0xffffffffUL
DUK_TVAL_SET_U32(tv_slot, (duk_uint32_t) val);
#else
if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */
/* XXX: take advantage of val being unsigned, no need to mask */
DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
} else {
duk_double_t = (duk_double_t) val;
DUK_TVAL_SET_NUMBER(tv_slot, d);
}
#endif
#else /* DUK_USE_FASTINT */
duk_tval *tv_slot;
duk_double_t d;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
d = (duk_double_t) val;
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_NUMBER(tv_slot, d);
#endif /* DUK_USE_FASTINT */
}
DUK_EXTERNAL void duk_push_nan(duk_hthread *thr) {
duk_tval *tv_slot;
duk_double_union du;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
DUK_DBLUNION_SET_NAN(&du);
DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_NUMBER(tv_slot, du.d);
}
DUK_EXTERNAL const char *duk_push_lstring(duk_hthread *thr, const char *str, duk_size_t len) {
duk_hstring *h;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
/* Check stack before interning (avoid hanging temp). */
DUK__CHECK_SPACE();
/* NULL with zero length represents an empty string; NULL with higher
* length is also now treated like an empty string although it is
* a bit dubious. This is unlike duk_push_string() which pushes a
* 'null' if the input string is a NULL.
*/
if (DUK_UNLIKELY(str == NULL)) {
len = 0U;
}
/* Check for maximum string length. */
if (DUK_UNLIKELY(len > DUK_HSTRING_MAX_BYTELEN)) {
DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG);
DUK_WO_NORETURN(return NULL;);
}
h = duk_heap_strtable_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
DUK_ASSERT(h != NULL);
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_STRING(tv_slot, h);
DUK_HSTRING_INCREF(thr, h); /* no side effects */
return (const char *) DUK_HSTRING_GET_DATA(h);
}
DUK_EXTERNAL const char *duk_push_string(duk_hthread *thr, const char *str) {
DUK_ASSERT_API_ENTRY(thr);
if (str) {
return duk_push_lstring(thr, str, DUK_STRLEN(str));
} else {
duk_push_null(thr);
return NULL;
}
}
#if !defined(DUK_USE_PREFER_SIZE)
#if defined(DUK_USE_LITCACHE_SIZE)
DUK_EXTERNAL const char *duk_push_literal_raw(duk_hthread *thr, const char *str, duk_size_t len) {
duk_hstring *h;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(str != NULL);
DUK_ASSERT(str[len] == (char) 0);
/* Check for maximum string length. */
if (DUK_UNLIKELY(len > DUK_HSTRING_MAX_BYTELEN)) {
DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG);
DUK_WO_NORETURN(return NULL;);
}
h = duk_heap_strtable_intern_literal_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
DUK_ASSERT(h != NULL);
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_STRING(tv_slot, h);
DUK_HSTRING_INCREF(thr, h); /* no side effects */
return (const char *) DUK_HSTRING_GET_DATA(h);
}
#else /* DUK_USE_LITCACHE_SIZE */
DUK_EXTERNAL const char *duk_push_literal_raw(duk_hthread *thr, const char *str, duk_size_t len) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(str != NULL);
DUK_ASSERT(str[len] == (char) 0);
return duk_push_lstring(thr, str, len);
}
#endif /* DUK_USE_LITCACHE_SIZE */
#endif /* !DUK_USE_PREFER_SIZE */
DUK_EXTERNAL void duk_push_pointer(duk_hthread *thr, void *val) {
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_POINTER(tv_slot, val);
}
DUK_INTERNAL duk_hstring *duk_push_uint_to_hstring(duk_hthread *thr, duk_uint_t i) {
duk_hstring *h_tmp;
DUK_ASSERT_API_ENTRY(thr);
/* XXX: this could be a direct DUK_SPRINTF to a buffer followed by duk_push_string() */
duk_push_uint(thr, (duk_uint_t) i);
h_tmp = duk_to_hstring_m1(thr);
DUK_ASSERT(h_tmp != NULL);
return h_tmp;
}
DUK_LOCAL void duk__push_this_helper(duk_hthread *thr, duk_small_uint_t check_object_coercible) {
duk_tval *tv_slot;
DUK__CHECK_SPACE();
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */
tv_slot = thr->valstack_top++;
if (DUK_UNLIKELY(thr->callstack_curr == NULL)) {
if (check_object_coercible) {
goto type_error;
}
/* 'undefined' already on stack top */
} else {
duk_tval *tv;
/* 'this' binding is just before current activation's bottom */
DUK_ASSERT(thr->valstack_bottom > thr->valstack);
tv = thr->valstack_bottom - 1;
if (check_object_coercible &&
(DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv))) {
/* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */
goto type_error;
}
DUK_TVAL_SET_TVAL(tv_slot, tv);
DUK_TVAL_INCREF(thr, tv);
}
return;
type_error:
DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE);
DUK_WO_NORETURN(return;);
}
DUK_EXTERNAL void duk_push_this(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk__push_this_helper(thr, 0 /*check_object_coercible*/);
}
DUK_INTERNAL void duk_push_this_check_object_coercible(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk__push_this_helper(thr, 1 /*check_object_coercible*/);
}
DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_hthread *thr) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
duk__push_this_helper(thr, 1 /*check_object_coercible*/);
h = duk_to_hobject(thr, -1);
DUK_ASSERT(h != NULL);
return h;
}
DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk__push_this_helper(thr, 1 /*check_object_coercible*/);
return duk_to_hstring_m1(thr); /* This will reject all Symbol values; accepts Symbol objects. */
}
DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */
DUK_ASSERT(thr->callstack_curr != NULL); /* caller required to know */
DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */
DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */
return thr->valstack_bottom - 1;
}
DUK_EXTERNAL void duk_push_new_target(duk_hthread *thr) {
duk_activation *act;
DUK_ASSERT_API_ENTRY(thr);
/* https://www.ecma-international.org/ecma-262/6.0/#sec-meta-properties-runtime-semantics-evaluation
* https://www.ecma-international.org/ecma-262/6.0/#sec-getnewtarget
*
* No newTarget support now, so as a first approximation
* use the resolved (non-bound) target function.
*
* Check CONSTRUCT flag from current function, or if running
* direct eval, from a non-direct-eval parent (with possibly
* more than one nested direct eval). An alternative to this
* would be to store [[NewTarget]] as a hidden symbol of the
* lexical scope, and then just look up that variable.
*
* Calls from the application will either be for an empty
* call stack, or a Duktape/C function as the top activation.
*/
act = thr->callstack_curr;
for (;;) {
if (act == NULL) {
break;
}
if (act->flags & DUK_ACT_FLAG_CONSTRUCT) {
duk_push_tval(thr, &act->tv_func);
return;
} else if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
act = act->parent;
} else {
break;
}
}
duk_push_undefined(thr);
}
DUK_EXTERNAL void duk_push_current_function(duk_hthread *thr) {
duk_activation *act;
DUK_ASSERT_API_ENTRY(thr);
act = thr->callstack_curr;
if (act != NULL) {
duk_push_tval(thr, &act->tv_func);
} else {
duk_push_undefined(thr);
}
}
DUK_EXTERNAL void duk_push_current_thread(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
if (thr->heap->curr_thread) {
duk_push_hobject(thr, (duk_hobject *) thr->heap->curr_thread);
} else {
duk_push_undefined(thr);
}
}
DUK_EXTERNAL void duk_push_global_object(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_push_hobject_bidx(thr, DUK_BIDX_GLOBAL);
}
/* XXX: size optimize */
DUK_LOCAL void duk__push_stash(duk_hthread *thr) {
if (!duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE)) {
DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use"));
duk_pop_unsafe(thr);
duk_push_bare_object(thr);
duk_dup_top(thr);
duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */
}
duk_remove_m2(thr);
}
DUK_EXTERNAL void duk_push_heap_stash(duk_hthread *thr) {
duk_heap *heap;
DUK_ASSERT_API_ENTRY(thr);
heap = thr->heap;
DUK_ASSERT(heap->heap_object != NULL);
duk_push_hobject(thr, heap->heap_object);
duk__push_stash(thr);
}
DUK_EXTERNAL void duk_push_global_stash(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_push_global_object(thr);
duk__push_stash(thr);
}
DUK_EXTERNAL void duk_push_thread_stash(duk_hthread *thr, duk_hthread *target_thr) {
DUK_ASSERT_API_ENTRY(thr);
if (DUK_UNLIKELY(target_thr == NULL)) {
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return;);
}
duk_push_hobject(thr, (duk_hobject *) target_thr);
duk__push_stash(thr);
}
/* XXX: duk_ssize_t would be useful here */
DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_hthread *thr, void *buf, duk_size_t sz, const char *fmt, va_list ap) {
duk_int_t len;
DUK_ASSERT_CTX_VALID(thr);
DUK_UNREF(thr);
/* NUL terminator handling doesn't matter here */
len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap);
if (len < (duk_int_t) sz) {
/* Return value of 'sz' or more indicates output was (potentially)
* truncated.
*/
return (duk_int_t) len;
}
return -1;
}
DUK_EXTERNAL const char *duk_push_vsprintf(duk_hthread *thr, const char *fmt, va_list ap) {
duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE];
duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
duk_bool_t pushed_buf = 0;
void *buf;
duk_int_t len; /* XXX: duk_ssize_t */
const char *res;
DUK_ASSERT_API_ENTRY(thr);
/* special handling of fmt==NULL */
if (!fmt) {
duk_hstring *h_str;
duk_push_hstring_empty(thr);
h_str = duk_known_hstring(thr, -1);
return (const char *) DUK_HSTRING_GET_DATA(h_str);
}
/* initial estimate based on format string */
sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */
if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) {
sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
}
DUK_ASSERT(sz > 0);
/* Try to make do with a stack buffer to avoid allocating a temporary buffer.
* This works 99% of the time which is quite nice.
*/
for (;;) {
va_list ap_copy; /* copied so that 'ap' can be reused */
if (sz <= sizeof(stack_buf)) {
buf = stack_buf;
} else if (!pushed_buf) {
pushed_buf = 1;
buf = duk_push_dynamic_buffer(thr, sz);
} else {
buf = duk_resize_buffer(thr, -1, sz);
}
DUK_ASSERT(buf != NULL);
DUK_VA_COPY(ap_copy, ap);
len = duk__try_push_vsprintf(thr, buf, sz, fmt, ap_copy);
va_end(ap_copy);
if (len >= 0) {
break;
}
/* failed, resize and try again */
sz = sz * 2;
if (DUK_UNLIKELY(sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT)) {
DUK_ERROR_RANGE(thr, DUK_STR_RESULT_TOO_LONG);
DUK_WO_NORETURN(return NULL;);
}
}
/* Cannot use duk_buffer_to_string() on the buffer because it is
* usually larger than 'len'; 'buf' is also usually a stack buffer.
*/
res = duk_push_lstring(thr, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */
if (pushed_buf) {
duk_remove_m2(thr);
}
return res;
}
DUK_EXTERNAL const char *duk_push_sprintf(duk_hthread *thr, const char *fmt, ...) {
va_list ap;
const char *ret;
DUK_ASSERT_API_ENTRY(thr);
/* allow fmt==NULL */
va_start(ap, fmt);
ret = duk_push_vsprintf(thr, fmt, ap);
va_end(ap);
return ret;
}
DUK_INTERNAL duk_hobject *duk_push_object_helper(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
duk_tval *tv_slot;
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(prototype_bidx == -1 ||
(prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS));
DUK__CHECK_SPACE();
h = duk_hobject_alloc(thr, hobject_flags_and_class);
DUK_ASSERT(h != NULL);
DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags));
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, h);
DUK_HOBJECT_INCREF(thr, h); /* no side effects */
thr->valstack_top++;
/* object is now reachable */
if (prototype_bidx >= 0) {
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, h, thr->builtins[prototype_bidx]);
} else {
DUK_ASSERT(prototype_bidx == -1);
DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
}
return h;
}
DUK_INTERNAL duk_hobject *duk_push_object_helper_proto(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_hobject *proto) {
duk_hobject *h;
DUK_ASSERT_API_ENTRY(thr);
h = duk_push_object_helper(thr, hobject_flags_and_class, -1);
DUK_ASSERT(h != NULL);
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, h, proto);
return h;
}
DUK_EXTERNAL duk_idx_t duk_push_object(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
(void) duk_push_object_helper(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
DUK_BIDX_OBJECT_PROTOTYPE);
return duk_get_top_index_unsafe(thr);
}
DUK_EXTERNAL duk_idx_t duk_push_array(duk_hthread *thr) {
duk_uint_t flags;
duk_harray *obj;
duk_idx_t ret;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_ARRAY_PART |
DUK_HOBJECT_FLAG_EXOTIC_ARRAY |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY);
obj = duk_harray_alloc(thr, flags);
DUK_ASSERT(obj != NULL);
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_ARRAY_PROTOTYPE]);
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HOBJECT_INCREF(thr, obj); /* XXX: could preallocate with refcount = 1 */
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
thr->valstack_top++;
DUK_ASSERT(obj->length == 0); /* Array .length starts at zero. */
return ret;
}
DUK_INTERNAL duk_harray *duk_push_harray(duk_hthread *thr) {
/* XXX: API call could do this directly, cast to void in API macro. */
duk_harray *a;
DUK_ASSERT_API_ENTRY(thr);
(void) duk_push_array(thr);
DUK_ASSERT(DUK_TVAL_IS_OBJECT(thr->valstack_top - 1));
a = (duk_harray *) DUK_TVAL_GET_OBJECT(thr->valstack_top - 1);
DUK_ASSERT(a != NULL);
return a;
}
/* Push a duk_harray with preallocated size (.length also set to match size).
* Caller may then populate array part of the duk_harray directly.
*/
DUK_INTERNAL duk_harray *duk_push_harray_with_size(duk_hthread *thr, duk_uint32_t size) {
duk_harray *a;
DUK_ASSERT_API_ENTRY(thr);
a = duk_push_harray(thr);
duk_hobject_realloc_props(thr,
(duk_hobject *) a,
0,
size,
0,
0);
a->length = size;
return a;
}
DUK_INTERNAL duk_tval *duk_push_harray_with_size_outptr(duk_hthread *thr, duk_uint32_t size) {
duk_harray *a;
DUK_ASSERT_API_ENTRY(thr);
a = duk_push_harray_with_size(thr, size);
DUK_ASSERT(a != NULL);
return DUK_HOBJECT_A_GET_BASE(thr->heap, (duk_hobject *) a);
}
DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_hthread *thr, duk_uint_t flags) {
duk_hthread *obj;
duk_idx_t ret;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
obj = duk_hthread_alloc(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
DUK_ASSERT(obj != NULL);
obj->state = DUK_HTHREAD_STATE_INACTIVE;
#if defined(DUK_USE_ROM_STRINGS)
/* Nothing to initialize, strs[] is in ROM. */
#else
#if defined(DUK_USE_HEAPPTR16)
obj->strs16 = thr->strs16;
#else
obj->strs = thr->strs;
#endif
#endif
DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
/* make the new thread reachable */
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HTHREAD_INCREF(thr, obj);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
thr->valstack_top++;
/* important to do this *after* pushing, to make the thread reachable for gc */
if (DUK_UNLIKELY(!duk_hthread_init_stacks(thr->heap, obj))) {
DUK_ERROR_ALLOC_FAILED(thr);
DUK_WO_NORETURN(return 0;);
}
/* initialize built-ins - either by copying or creating new ones */
if (flags & DUK_THREAD_NEW_GLOBAL_ENV) {
duk_hthread_create_builtin_objects(obj);
} else {
duk_hthread_copy_builtin_objects(thr, obj);
}
/* default prototype */
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
/* Initial stack size satisfies the stack slack constraints so there
* is no need to require stack here.
*/
DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >=
DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
return ret;
}
DUK_INTERNAL duk_hcompfunc *duk_push_hcompfunc(duk_hthread *thr) {
duk_hcompfunc *obj;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
/* Template functions are not strictly constructable (they don't
* have a "prototype" property for instance), so leave the
* DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here.
*/
obj = duk_hcompfunc_alloc(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_FLAG_COMPFUNC |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
if (DUK_UNLIKELY(obj == NULL)) {
DUK_ERROR_ALLOC_FAILED(thr);
DUK_WO_NORETURN(return NULL;);
}
DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HOBJECT_INCREF(thr, obj);
thr->valstack_top++;
/* default prototype */
DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) obj) == NULL);
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
return obj;
}
DUK_INTERNAL duk_hboundfunc *duk_push_hboundfunc(duk_hthread *thr) {
duk_hboundfunc *obj;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
obj = duk_hboundfunc_alloc(thr->heap,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_BOUNDFUNC |
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
if (!obj) {
DUK_ERROR_ALLOC_FAILED(thr);
DUK_WO_NORETURN(return NULL;);
}
tv_slot = thr->valstack_top++;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HOBJECT_INCREF(thr, obj);
/* Prototype is left as NULL because the caller always sets it (and
* it depends on the target function).
*/
DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) obj) == NULL);
return obj;
}
DUK_LOCAL duk_idx_t duk__push_c_function_raw(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_uint_t flags, duk_small_uint_t proto_bidx) {
duk_hnatfunc *obj;
duk_idx_t ret;
duk_tval *tv_slot;
duk_int16_t func_nargs;
DUK_ASSERT_CTX_VALID(thr);
DUK__CHECK_SPACE();
if (DUK_UNLIKELY(func == NULL)) {
goto api_error;
}
if (nargs >= 0 && nargs < DUK_HNATFUNC_NARGS_MAX) {
func_nargs = (duk_int16_t) nargs;
} else if (nargs == DUK_VARARGS) {
func_nargs = DUK_HNATFUNC_NARGS_VARARGS;
} else {
goto api_error;
}
obj = duk_hnatfunc_alloc(thr, flags);
DUK_ASSERT(obj != NULL);
obj->func = func;
obj->nargs = func_nargs;
DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld",
(unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs));
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HOBJECT_INCREF(thr, obj);
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
thr->valstack_top++;
DUK_ASSERT_BIDX_VALID(proto_bidx);
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[proto_bidx]);
return ret;
api_error:
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return 0;);
}
DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
duk_uint_t flags;
DUK_ASSERT_API_ENTRY(thr);
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_NATFUNC |
DUK_HOBJECT_FLAG_NEWENV |
DUK_HOBJECT_FLAG_STRICT |
DUK_HOBJECT_FLAG_NOTAIL |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
/* Default prototype is a Duktape specific %NativeFunctionPrototype%
* which provides .length and .name getters.
*/
return duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE);
}
DUK_INTERNAL void duk_push_c_function_builtin(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
duk_uint_t flags;
DUK_ASSERT_API_ENTRY(thr);
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_CONSTRUCTABLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_NATFUNC |
DUK_HOBJECT_FLAG_NEWENV |
DUK_HOBJECT_FLAG_STRICT |
DUK_HOBJECT_FLAG_NOTAIL |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
/* Must use Function.prototype for standard built-in functions. */
(void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_FUNCTION_PROTOTYPE);
}
DUK_INTERNAL void duk_push_c_function_builtin_noconstruct(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
duk_uint_t flags;
DUK_ASSERT_API_ENTRY(thr);
flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_CALLABLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_FLAG_NATFUNC |
DUK_HOBJECT_FLAG_NEWENV |
DUK_HOBJECT_FLAG_STRICT |
DUK_HOBJECT_FLAG_NOTAIL |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
/* Must use Function.prototype for standard built-in functions. */
(void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_FUNCTION_PROTOTYPE);
}
DUK_EXTERNAL duk_idx_t duk_push_c_lightfunc(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic) {
duk_small_uint_t lf_flags;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) {
/* as is */
} else if (nargs == DUK_VARARGS) {
nargs = DUK_LFUNC_NARGS_VARARGS;
} else {
goto api_error;
}
if (DUK_UNLIKELY(!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX))) {
goto api_error;
}
if (DUK_UNLIKELY(!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX))) {
goto api_error;
}
lf_flags = DUK_LFUNC_FLAGS_PACK((duk_small_int_t) magic, (duk_small_uint_t) length, (duk_small_uint_t) nargs);
tv_slot = thr->valstack_top++;
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv_slot));
DUK_TVAL_SET_LIGHTFUNC(tv_slot, func, lf_flags);
DUK_ASSERT(tv_slot >= thr->valstack_bottom);
return (duk_idx_t) (tv_slot - thr->valstack_bottom);
api_error:
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return 0;);
}
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_INTERNAL duk_hbufobj *duk_push_bufobj_raw(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
duk_hbufobj *obj;
duk_tval *tv_slot;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(prototype_bidx >= 0);
DUK__CHECK_SPACE();
obj = duk_hbufobj_alloc(thr, hobject_flags_and_class);
DUK_ASSERT(obj != NULL);
DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]);
DUK_ASSERT_HBUFOBJ_VALID(obj);
tv_slot = thr->valstack_top;
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
DUK_HOBJECT_INCREF(thr, obj);
thr->valstack_top++;
return obj;
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
/* XXX: There's quite a bit of overlap with buffer creation handling in
* duk_bi_buffer.c. Look for overlap and refactor.
*/
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
#define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,istypedarray) \
(((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (istypedarray))
static const duk_uint32_t duk__bufobj_flags_lookup[] = {
/* Node.js Buffers are Uint8Array instances which inherit from Buffer.prototype. */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_ARRAYBUFFER, DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_ARRAYBUFFER */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_NODEJS_BUFFER */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_DATAVIEW, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_DATAVIEW */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT8ARRAY, DUK_BIDX_INT8ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT8, 0, 1), /* DUK_BUFOBJ_INT8ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_UINT8ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8CLAMPED, 0, 1), /* DUK_BUFOBJ_UINT8CLAMPEDARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT16ARRAY, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT16, 1, 1), /* DUK_BUFOBJ_INT16ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT16ARRAY, DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT16, 1, 1), /* DUK_BUFOBJ_UINT16ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT32ARRAY, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT32, 2, 1), /* DUK_BUFOBJ_INT32ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT32ARRAY, DUK_BIDX_UINT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT32, 2, 1), /* DUK_BUFOBJ_UINT32ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT32ARRAY, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_FLOAT32, 2, 1), /* DUK_BUFOBJ_FLOAT32ARRAY */
DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT64ARRAY, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_FLOAT64, 3, 1) /* DUK_BUFOBJ_FLOAT64ARRAY */
};
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_EXTERNAL void duk_push_buffer_object(duk_hthread *thr, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
duk_hbufobj *h_bufobj;
duk_hbuffer *h_val;
duk_hobject *h_arraybuf;
duk_uint32_t tmp;
duk_uint_t classnum;
duk_uint_t protobidx;
duk_uint_t lookupidx;
duk_uint_t uint_offset, uint_length, uint_added;
DUK_ASSERT_API_ENTRY(thr);
/* The underlying types for offset/length in duk_hbufobj is
* duk_uint_t; make sure argument values fit.
*/
uint_offset = (duk_uint_t) byte_offset;
uint_length = (duk_uint_t) byte_length;
if (sizeof(duk_size_t) != sizeof(duk_uint_t)) {
if (DUK_UNLIKELY((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length)) {
goto range_error;
}
}
DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */
lookupidx = flags;
if (DUK_UNLIKELY(lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t))) {
goto arg_error;
}
tmp = duk__bufobj_flags_lookup[lookupidx];
classnum = tmp >> 24;
protobidx = (tmp >> 16) & 0xff;
h_arraybuf = duk_get_hobject(thr, idx_buffer);
if (h_arraybuf != NULL && /* argument is an object */
flags != DUK_BUFOBJ_ARRAYBUFFER && /* creating a view */
DUK_HOBJECT_GET_CLASS_NUMBER(h_arraybuf) == DUK_HOBJECT_CLASS_ARRAYBUFFER /* argument is ArrayBuffer */) {
duk_uint_t tmp_offset;
DUK_ASSERT_HBUFOBJ_VALID((duk_hbufobj *) h_arraybuf);
h_val = ((duk_hbufobj *) h_arraybuf)->buf;
if (DUK_UNLIKELY(h_val == NULL)) {
goto arg_error;
}
tmp_offset = uint_offset + ((duk_hbufobj *) h_arraybuf)->offset;
if (DUK_UNLIKELY(tmp_offset < uint_offset)) {
goto range_error;
}
uint_offset = tmp_offset;
/* Note intentional difference to new TypedArray(): we allow
* caller to create an uncovered typed array (which is memory
* safe); new TypedArray() rejects it.
*/
} else {
/* Handle unexpected object arguments here too, for nice error
* messages.
*/
h_arraybuf = NULL;
h_val = duk_require_hbuffer(thr, idx_buffer);
}
/* Wrap check for offset+length. */
uint_added = uint_offset + uint_length;
if (DUK_UNLIKELY(uint_added < uint_offset)) {
goto range_error;
}
DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length);
DUK_ASSERT(h_val != NULL);
h_bufobj = duk_push_bufobj_raw(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_BUFOBJ |
DUK_HOBJECT_CLASS_AS_FLAGS(classnum),
(duk_small_int_t) protobidx);
DUK_ASSERT(h_bufobj != NULL);
h_bufobj->buf = h_val;
DUK_HBUFFER_INCREF(thr, h_val);
h_bufobj->buf_prop = h_arraybuf;
DUK_HOBJECT_INCREF_ALLOWNULL(thr, h_arraybuf);
h_bufobj->offset = uint_offset;
h_bufobj->length = uint_length;
h_bufobj->shift = (tmp >> 4) & 0x0f;
h_bufobj->elem_type = (tmp >> 8) & 0xff;
h_bufobj->is_typedarray = tmp & 0x0f;
DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
/* TypedArray views need an automatic ArrayBuffer which must be
* provided as .buffer property of the view. The ArrayBuffer is
* referenced via duk_hbufobj->buf_prop and an inherited .buffer
* accessor returns it. The ArrayBuffer is created lazily on first
* access if necessary so we don't need to do anything more here.
*/
return;
range_error:
DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
DUK_WO_NORETURN(return;);
arg_error:
DUK_ERROR_TYPE(thr, DUK_STR_INVALID_ARGS);
DUK_WO_NORETURN(return;);
}
#else /* DUK_USE_BUFFEROBJECT_SUPPORT */
DUK_EXTERNAL void duk_push_buffer_object(duk_hthread *thr, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
DUK_ASSERT_API_ENTRY(thr);
DUK_UNREF(idx_buffer);
DUK_UNREF(byte_offset);
DUK_UNREF(byte_length);
DUK_UNREF(flags);
DUK_ERROR_UNSUPPORTED(thr);
DUK_WO_NORETURN(return;);
}
#endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
duk_hobject *proto;
#if defined(DUK_USE_AUGMENT_ERROR_CREATE)
duk_small_uint_t augment_flags;
#endif
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr != NULL);
DUK_UNREF(filename);
DUK_UNREF(line);
/* Error code also packs a tracedata related flag. */
#if defined(DUK_USE_AUGMENT_ERROR_CREATE)
augment_flags = 0;
if (err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE) {
augment_flags = DUK_AUGMENT_FLAG_NOBLAME_FILELINE;
}
#endif
err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE);
/* error gets its 'name' from the prototype */
proto = duk_error_prototype_from_code(thr, err_code);
(void) duk_push_object_helper_proto(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR),
proto);
/* ... and its 'message' from an instance property */
if (fmt) {
duk_push_vsprintf(thr, fmt, ap);
duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
} else {
/* If no explicit message given, put error code into message field
* (as a number). This is not fully in keeping with the ECMAScript
* error model because messages are supposed to be strings (Error
* constructors use ToString() on their argument). However, it's
* probably more useful than having a separate 'code' property.
*/
duk_push_int(thr, err_code);
duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
}
/* XXX: .code = err_code disabled, not sure if useful */
/* Creation time error augmentation */
#if defined(DUK_USE_AUGMENT_ERROR_CREATE)
/* filename may be NULL in which case file/line is not recorded */
duk_err_augment_error_create(thr, thr, filename, line, augment_flags); /* may throw an error */
#endif
return duk_get_top_index_unsafe(thr);
}
DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
va_list ap;
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
va_start(ap, fmt);
ret = duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
va_end(ap);
return ret;
}
#if !defined(DUK_USE_VARIADIC_MACROS)
DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, ...) {
const char *filename = duk_api_global_filename;
duk_int_t line = duk_api_global_line;
va_list ap;
duk_idx_t ret;
DUK_ASSERT_API_ENTRY(thr);
duk_api_global_filename = NULL;
duk_api_global_line = 0;
va_start(ap, fmt);
ret = duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
va_end(ap);
return ret;
}
#endif /* DUK_USE_VARIADIC_MACROS */
DUK_EXTERNAL void *duk_push_buffer_raw(duk_hthread *thr, duk_size_t size, duk_small_uint_t flags) {
duk_tval *tv_slot;
duk_hbuffer *h;
void *buf_data;
DUK_ASSERT_API_ENTRY(thr);
DUK__CHECK_SPACE();
/* Check for maximum buffer length. */
if (DUK_UNLIKELY(size > DUK_HBUFFER_MAX_BYTELEN)) {
DUK_ERROR_RANGE(thr, DUK_STR_BUFFER_TOO_LONG);
DUK_WO_NORETURN(return NULL;);
}
h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data);
if (DUK_UNLIKELY(h == NULL)) {
DUK_ERROR_ALLOC_FAILED(thr);
DUK_WO_NORETURN(return NULL;);
}
tv_slot = thr->valstack_top;
DUK_TVAL_SET_BUFFER(tv_slot, h);
DUK_HBUFFER_INCREF(thr, h);
thr->valstack_top++;
return (void *) buf_data;
}
DUK_INTERNAL void *duk_push_fixed_buffer_nozero(duk_hthread *thr, duk_size_t len) {
DUK_ASSERT_API_ENTRY(thr);
return duk_push_buffer_raw(thr, len, DUK_BUF_FLAG_NOZERO);
}
DUK_INTERNAL void *duk_push_fixed_buffer_zero(duk_hthread *thr, duk_size_t len) {
void *ptr;
DUK_ASSERT_API_ENTRY(thr);
ptr = duk_push_buffer_raw(thr, len, 0);
DUK_ASSERT(ptr != NULL);
#if !defined(DUK_USE_ZERO_BUFFER_DATA)
/* ES2015 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA
* is not set.
*/
duk_memzero((void *) ptr, (size_t) len);
#endif
return ptr;
}
#if defined(DUK_USE_ES6_PROXY)
DUK_EXTERNAL duk_idx_t duk_push_proxy(duk_hthread *thr, duk_uint_t proxy_flags) {
duk_hobject *h_target;
duk_hobject *h_handler;
duk_hproxy *h_proxy;
duk_tval *tv_slot;
duk_uint_t flags;
DUK_ASSERT_API_ENTRY(thr);
DUK_UNREF(proxy_flags);
/* DUK__CHECK_SPACE() unnecessary because the Proxy is written to
* value stack in-place.
*/
#if 0
DUK__CHECK_SPACE();
#endif
/* Reject a proxy object as the target because it would need
* special handling in property lookups. (ES2015 has no such
* restriction.)
*/
h_target = duk_require_hobject_promote_mask(thr, -2, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
DUK_ASSERT(h_target != NULL);
if (DUK_HOBJECT_IS_PROXY(h_target)) {
goto fail_args;
}
/* Reject a proxy object as the handler because it would cause
* potentially unbounded recursion. (ES2015 has no such
* restriction.)
*
* There's little practical reason to use a lightfunc or a plain
* buffer as the handler table: one could only provide traps via
* their prototype objects (Function.prototype and ArrayBuffer.prototype).
* Even so, as lightfuncs and plain buffers mimic their object
* counterparts, they're promoted and accepted here.
*/
h_handler = duk_require_hobject_promote_mask(thr, -1, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
DUK_ASSERT(h_handler != NULL);
if (DUK_HOBJECT_IS_PROXY(h_handler)) {
goto fail_args;
}
/* XXX: Proxy object currently has no prototype, so ToPrimitive()
* coercion fails which is a bit confusing.
*/
/* CALLABLE and CONSTRUCTABLE flags are copied from the (initial)
* target, see ES2015 Sections 9.5.15 and 9.5.13.
*/
flags = DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h_target) &
(DUK_HOBJECT_FLAG_CALLABLE | DUK_HOBJECT_FLAG_CONSTRUCTABLE);
flags |= DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ;
if (flags & DUK_HOBJECT_FLAG_CALLABLE) {
flags |= DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION) |
DUK_HOBJECT_FLAG_SPECIAL_CALL;
} else {
flags |= DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT);
}
h_proxy = duk_hproxy_alloc(thr, flags);
DUK_ASSERT(h_proxy != NULL);
DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_proxy) == NULL);
/* Initialize Proxy target and handler references; avoid INCREF
* by stealing the value stack refcounts via direct value stack
* manipulation. INCREF is needed for the Proxy itself however.
*/
DUK_ASSERT(h_target != NULL);
h_proxy->target = h_target;
DUK_ASSERT(h_handler != NULL);
h_proxy->handler = h_handler;
DUK_ASSERT_HPROXY_VALID(h_proxy);
DUK_ASSERT(duk_get_hobject(thr, -2) == h_target);
DUK_ASSERT(duk_get_hobject(thr, -1) == h_handler);
tv_slot = thr->valstack_top - 2;
DUK_ASSERT(tv_slot >= thr->valstack_bottom);
DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) h_proxy);
DUK_HOBJECT_INCREF(thr, (duk_hobject *) h_proxy);
tv_slot++;
DUK_TVAL_SET_UNDEFINED(tv_slot); /* [ ... target handler ] -> [ ... proxy undefined ] */
thr->valstack_top = tv_slot; /* -> [ ... proxy ] */
DUK_DD(DUK_DDPRINT("created Proxy: %!iT", duk_get_tval(thr, -1)));
return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom - 1);
fail_args:
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return 0;);
}
#else /* DUK_USE_ES6_PROXY */
DUK_EXTERNAL duk_idx_t duk_push_proxy(duk_hthread *thr, duk_uint_t proxy_flags) {
DUK_ASSERT_API_ENTRY(thr);
DUK_UNREF(proxy_flags);
DUK_ERROR_UNSUPPORTED(thr);
DUK_WO_NORETURN(return 0;);
}
#endif /* DUK_USE_ES6_PROXY */
#if defined(DUK_USE_ASSERTIONS)
DUK_LOCAL void duk__validate_push_heapptr(duk_hthread *thr, void *ptr) {
duk_heaphdr *h;
duk_heaphdr *curr;
duk_bool_t found = 0;
h = (duk_heaphdr *) ptr;
if (h == NULL) {
/* Allowed. */
return;
}
DUK_ASSERT(h != NULL);
DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
/* One particular problem case is where an object has been
* queued for finalization but the finalizer hasn't yet been
* executed.
*
* Corner case: we're running in a finalizer for object X, and
* user code calls duk_push_heapptr() for X itself. In this
* case X will be in finalize_list, and we can detect the case
* by seeing that X's FINALIZED flag is set (which is done before
* the finalizer starts executing).
*/
#if defined(DUK_USE_FINALIZER_SUPPORT)
for (curr = thr->heap->finalize_list;
curr != NULL;
curr = DUK_HEAPHDR_GET_NEXT(thr->heap, curr)) {
/* FINALIZABLE is set for all objects on finalize_list
* except for an object being finalized right now. So
* can't assert here.
*/
#if 0
DUK_ASSERT(DUK_HEAPHDR_HAS_FINALIZABLE(curr));
#endif
if (curr == h) {
if (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h)) {
/* Object is currently being finalized. */
DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
found = 1;
} else {
/* Not being finalized but on finalize_list,
* allowed since Duktape 2.1.
*/
DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
found = 1;
}
}
}
#endif /* DUK_USE_FINALIZER_SUPPORT */
#if defined(DUK_USE_REFERENCE_COUNTING)
/* Because refzero_list is now processed to completion inline with
* no side effects, it's always empty here.
*/
DUK_ASSERT(thr->heap->refzero_list == NULL);
#endif
/* If not present in finalize_list (or refzero_list), it
* must be either in heap_allocated or the string table.
*/
if (DUK_HEAPHDR_IS_STRING(h)) {
duk_uint32_t i;
duk_hstring *str;
duk_heap *heap = thr->heap;
DUK_ASSERT(found == 0);
for (i = 0; i < heap->st_size; i++) {
#if defined(DUK_USE_STRTAB_PTRCOMP)
str = DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, heap->strtable16[i]);
#else
str = heap->strtable[i];
#endif
while (str != NULL) {
if (str == (duk_hstring *) h) {
DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
found = 1;
break;
}
str = str->hdr.h_next;
}
}
DUK_ASSERT(found != 0);
} else {
for (curr = thr->heap->heap_allocated;
curr != NULL;
curr = DUK_HEAPHDR_GET_NEXT(thr->heap, curr)) {
if (curr == h) {
DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
found = 1;
}
}
DUK_ASSERT(found != 0);
}
}
#endif /* DUK_USE_ASSERTIONS */
DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_hthread *thr, void *ptr) {
duk_idx_t ret;
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
/* Reviving an object using a heap pointer is a dangerous API
* operation: if the application doesn't guarantee that the
* pointer target is always reachable, difficult-to-diagnose
* problems may ensue. Try to validate the 'ptr' argument to
* the extent possible.
*/
#if defined(DUK_USE_ASSERTIONS)
duk__validate_push_heapptr(thr, ptr);
#endif
DUK__CHECK_SPACE();
ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
tv = thr->valstack_top++;
if (ptr == NULL) {
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
return ret;
}
DUK_ASSERT_HEAPHDR_VALID((duk_heaphdr *) ptr);
/* If the argument is on finalize_list it has technically been
* unreachable before duk_push_heapptr() but it's still safe to
* push it. Starting from Duktape 2.1 allow application code to
* do so. There are two main cases:
*
* (1) The object is on the finalize_list and we're called by
* the finalizer for the object being finalized. In this
* case do nothing: finalize_list handling will deal with
* the object queueing. This is detected by the object not
* having a FINALIZABLE flag despite being on the finalize_list;
* the flag is cleared for the object being finalized only.
*
* (2) The object is on the finalize_list but is not currently
* being processed. In this case the object can be queued
* back to heap_allocated with a few flags cleared, in effect
* cancelling the finalizer.
*/
if (DUK_UNLIKELY(DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) ptr))) {
duk_heaphdr *curr;
DUK_D(DUK_DPRINT("duk_push_heapptr() with a pointer on finalize_list, autorescue"));
curr = (duk_heaphdr *) ptr;
DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
/* Because FINALIZED is set prior to finalizer call, it will
* be set for the object being currently finalized, but not
* for other objects on finalize_list.
*/
DUK_HEAPHDR_CLEAR_FINALIZED(curr);
/* Dequeue object from finalize_list and queue it back to
* heap_allocated.
*/
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) >= 1); /* Preincremented on finalize_list insert. */
DUK_HEAPHDR_PREDEC_REFCOUNT(curr);
#endif
DUK_HEAP_REMOVE_FROM_FINALIZE_LIST(thr->heap, curr);
DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(thr->heap, curr);
/* Continue with the rest. */
}
switch (DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) {
case DUK_HTYPE_STRING:
DUK_TVAL_SET_STRING(tv, (duk_hstring *) ptr);
break;
case DUK_HTYPE_OBJECT:
DUK_TVAL_SET_OBJECT(tv, (duk_hobject *) ptr);
break;
default:
DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr) == DUK_HTYPE_BUFFER);
DUK_TVAL_SET_BUFFER(tv, (duk_hbuffer *) ptr);
break;
}
DUK_HEAPHDR_INCREF(thr, (duk_heaphdr *) ptr);
return ret;
}
/* Push object with no prototype, i.e. a "bare" object. */
DUK_EXTERNAL duk_idx_t duk_push_bare_object(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
(void) duk_push_object_helper(thr,
DUK_HOBJECT_FLAG_EXTENSIBLE |
DUK_HOBJECT_FLAG_FASTREFS |
DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
-1); /* no prototype */
return duk_get_top_index_unsafe(thr);
}
DUK_INTERNAL void duk_push_hstring(duk_hthread *thr, duk_hstring *h) {
duk_tval tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(h != NULL);
DUK_TVAL_SET_STRING(&tv, h);
duk_push_tval(thr, &tv);
}
DUK_INTERNAL void duk_push_hstring_stridx(duk_hthread *thr, duk_small_uint_t stridx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT_STRIDX_VALID(stridx);
duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
}
DUK_INTERNAL void duk_push_hstring_empty(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, DUK_STRIDX_EMPTY_STRING));
}
DUK_INTERNAL void duk_push_hobject(duk_hthread *thr, duk_hobject *h) {
duk_tval tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(h != NULL);
DUK_TVAL_SET_OBJECT(&tv, h);
duk_push_tval(thr, &tv);
}
DUK_INTERNAL void duk_push_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
duk_tval tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(h != NULL);
DUK_TVAL_SET_BUFFER(&tv, h);
duk_push_tval(thr, &tv);
}
DUK_INTERNAL void duk_push_hobject_bidx(duk_hthread *thr, duk_small_int_t builtin_idx) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS);
DUK_ASSERT(thr->builtins[builtin_idx] != NULL);
duk_push_hobject(thr, thr->builtins[builtin_idx]);
}
/*
* Poppers
*/
DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_n_unsafe_raw(duk_hthread *thr, duk_idx_t count) {
duk_tval *tv;
#if defined(DUK_USE_REFERENCE_COUNTING)
duk_tval *tv_end;
#endif
DUK_ASSERT_CTX_VALID(thr);
DUK_ASSERT(count >= 0);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) count);
#if defined(DUK_USE_REFERENCE_COUNTING)
tv = thr->valstack_top;
tv_end = tv - count;
while (tv != tv_end) {
tv--;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
}
thr->valstack_top = tv;
DUK_REFZERO_CHECK_FAST(thr);
#else
tv = thr->valstack_top;
while (count > 0) {
count--;
tv--;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED(tv);
}
thr->valstack_top = tv;
#endif
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
DUK_EXTERNAL void duk_pop_n(duk_hthread *thr, duk_idx_t count) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
if (DUK_UNLIKELY((duk_uidx_t) (thr->valstack_top - thr->valstack_bottom) < (duk_uidx_t) count)) {
DUK_ERROR_RANGE_INVALID_COUNT(thr);
DUK_WO_NORETURN(return;);
}
DUK_ASSERT(count >= 0);
duk__pop_n_unsafe_raw(thr, count);
}
#if defined(DUK_USE_PREFER_SIZE)
DUK_INTERNAL void duk_pop_n_unsafe(duk_hthread *thr, duk_idx_t count) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n(thr, count);
}
#else /* DUK_USE_PREFER_SIZE */
DUK_INTERNAL void duk_pop_n_unsafe(duk_hthread *thr, duk_idx_t count) {
DUK_ASSERT_API_ENTRY(thr);
duk__pop_n_unsafe_raw(thr, count);
}
#endif /* DUK_USE_PREFER_SIZE */
/* Pop N elements without DECREF (in effect "stealing" any actual refcounts). */
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_INTERNAL void duk_pop_n_nodecref_unsafe(duk_hthread *thr, duk_idx_t count) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(count >= 0);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) count);
tv = thr->valstack_top;
while (count > 0) {
count--;
tv--;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED(tv);
}
thr->valstack_top = tv;
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
#else /* DUK_USE_REFERENCE_COUNTING */
DUK_INTERNAL void duk_pop_n_nodecref_unsafe(duk_hthread *thr, duk_idx_t count) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_unsafe(thr, count);
}
#endif /* DUK_USE_REFERENCE_COUNTING */
/* Popping one element is called so often that when footprint is not an issue,
* compile a specialized function for it.
*/
#if defined(DUK_USE_PREFER_SIZE)
DUK_EXTERNAL void duk_pop(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n(thr, 1);
}
DUK_INTERNAL void duk_pop_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_unsafe(thr, 1);
}
DUK_INTERNAL void duk_pop_nodecref_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_nodecref_unsafe(thr, 1);
}
#else /* DUK_USE_PREFER_SIZE */
DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_unsafe_raw(duk_hthread *thr) {
duk_tval *tv;
DUK_ASSERT_CTX_VALID(thr);
DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
tv = --thr->valstack_top;
DUK_ASSERT(tv >= thr->valstack_bottom);
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
#else
DUK_TVAL_SET_UNDEFINED(tv);
#endif
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
DUK_EXTERNAL void duk_pop(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
DUK_ERROR_RANGE_INVALID_COUNT(thr);
DUK_WO_NORETURN(return;);
}
duk__pop_unsafe_raw(thr);
}
DUK_INTERNAL void duk_pop_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk__pop_unsafe_raw(thr);
}
DUK_INTERNAL void duk_pop_nodecref_unsafe(duk_hthread *thr) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
tv = --thr->valstack_top;
DUK_ASSERT(tv >= thr->valstack_bottom);
DUK_TVAL_SET_UNDEFINED(tv);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
#endif /* !DUK_USE_PREFER_SIZE */
#if defined(DUK_USE_PREFER_SIZE)
DUK_INTERNAL void duk_pop_undefined(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_nodecref_unsafe(thr);
}
#else /* DUK_USE_PREFER_SIZE */
DUK_INTERNAL void duk_pop_undefined(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
thr->valstack_top--;
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
#endif /* !DUK_USE_PREFER_SIZE */
#if defined(DUK_USE_PREFER_SIZE)
DUK_EXTERNAL void duk_pop_2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n(thr, 2);
}
DUK_INTERNAL void duk_pop_2_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_unsafe(thr, 2);
}
DUK_INTERNAL void duk_pop_2_nodecref_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_nodecref_unsafe(thr, 2);
}
#else
DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_2_unsafe_raw(duk_hthread *thr) {
duk_tval *tv;
DUK_ASSERT_CTX_VALID(thr);
DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 2);
tv = --thr->valstack_top;
DUK_ASSERT(tv >= thr->valstack_bottom);
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
#else
DUK_TVAL_SET_UNDEFINED(tv);
#endif
tv = --thr->valstack_top;
DUK_ASSERT(tv >= thr->valstack_bottom);
#if defined(DUK_USE_REFERENCE_COUNTING)
DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
#else
DUK_TVAL_SET_UNDEFINED(tv);
#endif
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
DUK_EXTERNAL void duk_pop_2(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
if (DUK_UNLIKELY(thr->valstack_top - 2 < thr->valstack_bottom)) {
DUK_ERROR_RANGE_INVALID_COUNT(thr);
DUK_WO_NORETURN(return;);
}
duk__pop_2_unsafe_raw(thr);
}
DUK_INTERNAL void duk_pop_2_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk__pop_2_unsafe_raw(thr);
}
DUK_INTERNAL void duk_pop_2_nodecref_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 2);
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 2));
thr->valstack_top -= 2;
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
}
#endif /* !DUK_USE_PREFER_SIZE */
DUK_EXTERNAL void duk_pop_3(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n(thr, 3);
}
DUK_INTERNAL void duk_pop_3_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_unsafe(thr, 3);
}
DUK_INTERNAL void duk_pop_3_nodecref_unsafe(duk_hthread *thr) {
DUK_ASSERT_API_ENTRY(thr);
duk_pop_n_nodecref_unsafe(thr, 3);
}
/*
* Pack and unpack (pack value stack entries into an array and vice versa)
*/
/* XXX: pack index range? array index offset? */
DUK_INTERNAL void duk_pack(duk_hthread *thr, duk_idx_t count) {
duk_tval *tv_src;
duk_tval *tv_dst;
duk_tval *tv_curr;
duk_tval *tv_limit;
duk_idx_t top;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
top = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
DUK_ASSERT(top >= 0);
if (DUK_UNLIKELY((duk_uidx_t) count > (duk_uidx_t) top)) {
/* Also handles negative count. */
DUK_ERROR_RANGE_INVALID_COUNT(thr);
DUK_WO_NORETURN(return;);
}
DUK_ASSERT(count >= 0);
/* Wrapping is controlled by the check above: value stack top can be
* at most DUK_USE_VALSTACK_LIMIT which is low enough so that
* multiplying with sizeof(duk_tval) won't wrap.
*/
DUK_ASSERT(count >= 0 && count <= (duk_idx_t) DUK_USE_VALSTACK_LIMIT);
DUK_ASSERT((duk_size_t) count <= DUK_SIZE_MAX / sizeof(duk_tval)); /* no wrapping */
tv_dst = duk_push_harray_with_size_outptr(thr, (duk_uint32_t) count); /* XXX: uninitialized would be OK */
DUK_ASSERT(count == 0 || tv_dst != NULL);
/* Copy value stack values directly to the array part without
* any refcount updates: net refcount changes are zero.
*/
tv_src = thr->valstack_top - count - 1;
duk_memcpy_unsafe((void *) tv_dst, (const void *) tv_src, (size_t) count * sizeof(duk_tval));
/* Overwrite result array to final value stack location and wipe
* the rest; no refcount operations needed.
*/
tv_dst = tv_src; /* when count == 0, same as tv_src (OK) */
tv_src = thr->valstack_top - 1;
DUK_TVAL_SET_TVAL(tv_dst, tv_src);
/* XXX: internal helper to wipe a value stack segment? */
tv_curr = tv_dst + 1;
tv_limit = thr->valstack_top;
while (tv_curr != tv_limit) {
/* Wipe policy: keep as 'undefined'. */
DUK_TVAL_SET_UNDEFINED(tv_curr);
tv_curr++;
}
thr->valstack_top = tv_dst + 1;
}
DUK_INTERNAL duk_idx_t duk_unpack_array_like(duk_hthread *thr, duk_idx_t idx) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
tv = duk_require_tval(thr, idx);
if (DUK_LIKELY(DUK_TVAL_IS_OBJECT(tv))) {
duk_hobject *h;
duk_uint32_t len;
duk_uint32_t i;
h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
DUK_UNREF(h);
#if defined(DUK_USE_ARRAY_FASTPATH) /* close enough */
if (DUK_LIKELY(DUK_HOBJECT_IS_ARRAY(h) &&
((duk_harray *) h)->length <= DUK_HOBJECT_GET_ASIZE(h))) {
duk_harray *h_arr;
duk_tval *tv_src;
duk_tval *tv_dst;
h_arr = (duk_harray *) h;
len = h_arr->length;
if (DUK_UNLIKELY(len >= 0x80000000UL)) {
goto fail_over_2g;
}
duk_require_stack(thr, (duk_idx_t) len);
/* The potential allocation in duk_require_stack() may
* run a finalizer which modifies the argArray so that
* e.g. becomes sparse. So, we need to recheck that the
* array didn't change size and that there's still a
* valid backing array part.
*
* XXX: alternatively, could prevent finalizers for the
* duration.
*/
if (DUK_UNLIKELY(len != h_arr->length ||
h_arr->length > DUK_HOBJECT_GET_ASIZE((duk_hobject *) h_arr))) {
goto skip_fast;
}
/* Main fast path: arguments array is almost always
* an actual array (though it might also be an arguments
* object).
*/
DUK_DDD(DUK_DDDPRINT("fast path for %ld elements", (long) h_arr->length));
tv_src = DUK_HOBJECT_A_GET_BASE(thr->heap, h);
tv_dst = thr->valstack_top;
while (len-- > 0) {
DUK_ASSERT(tv_dst < thr->valstack_end);
if (DUK_UNLIKELY(DUK_TVAL_IS_UNUSED(tv_src))) {
/* Gaps are very unlikely. Skip over them,
* without an ancestor lookup (technically
* not compliant).
*/
DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv_dst)); /* valstack policy */
} else {
DUK_TVAL_SET_TVAL(tv_dst, tv_src);
DUK_TVAL_INCREF(thr, tv_dst);
}
tv_src++;
tv_dst++;
}
DUK_ASSERT(tv_dst <= thr->valstack_end);
thr->valstack_top = tv_dst;
return (duk_idx_t) h_arr->length;
}
skip_fast:
#endif /* DUK_USE_ARRAY_FASTPATH */
/* Slow path: actual lookups. The initial 'length' lookup
* decides the output length, regardless of side effects that
* may resize or change the argArray while we read the
* indices.
*/
idx = duk_normalize_index(thr, idx);
duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
len = duk_to_uint32(thr, -1); /* ToUint32() coercion required */
if (DUK_UNLIKELY(len >= 0x80000000UL)) {
goto fail_over_2g;
}
duk_pop_unsafe(thr);
DUK_DDD(DUK_DDDPRINT("slow path for %ld elements", (long) len));
duk_require_stack(thr, (duk_idx_t) len);
for (i = 0; i < len; i++) {
duk_get_prop_index(thr, idx, (duk_uarridx_t) i);
}
return (duk_idx_t) len;
} else if (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv)) {
return 0;
}
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return 0;);
fail_over_2g:
DUK_ERROR_RANGE_INVALID_LENGTH(thr);
DUK_WO_NORETURN(return 0;);
}
/*
* Error throwing
*/
DUK_EXTERNAL void duk_throw_raw(duk_hthread *thr) {
duk_tval *tv_val;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
DUK_ERROR_TYPE_INVALID_ARGS(thr);
DUK_WO_NORETURN(return;);
}
/* Errors are augmented when they are created, not when they are
* thrown or re-thrown. The current error handler, however, runs
* just before an error is thrown.
*/
/* Sync so that augmentation sees up-to-date activations, NULL
* thr->ptr_curr_pc so that it's not used if side effects occur
* in augmentation or longjmp handling.
*/
duk_hthread_sync_and_null_currpc(thr);
#if defined(DUK_USE_AUGMENT_ERROR_THROW)
DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(thr, -1)));
duk_err_augment_error_throw(thr);
#endif
DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(thr, -1)));
tv_val = DUK_GET_TVAL_NEGIDX(thr, -1);
duk_err_setup_ljstate1(thr, DUK_LJ_TYPE_THROW, tv_val);
#if defined(DUK_USE_DEBUGGER_SUPPORT)
duk_err_check_debugger_integration(thr);
#endif
/* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't
* need to check that here. If the value is NULL, a fatal error occurs
* because we can't return.
*/
duk_err_longjmp(thr);
DUK_UNREACHABLE();
}
DUK_EXTERNAL void duk_fatal_raw(duk_hthread *thr, const char *err_msg) {
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
DUK_ASSERT(thr->heap->fatal_func != NULL);
DUK_D(DUK_DPRINT("fatal error occurred: %s", err_msg ? err_msg : "NULL"));
/* fatal_func should be noreturn, but noreturn declarations on function
* pointers has a very spotty support apparently so it's not currently
* done.
*/
thr->heap->fatal_func(thr->heap->heap_udata, err_msg);
/* If the fatal handler returns, all bets are off. It'd be nice to
* print something here but since we don't want to depend on stdio,
* there's no way to do so portably.
*/
DUK_D(DUK_DPRINT("fatal error handler returned, all bets are off!"));
for (;;) {
/* loop forever, don't return (function marked noreturn) */
}
}
DUK_EXTERNAL void duk_error_va_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
DUK_ASSERT_API_ENTRY(thr);
duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
(void) duk_throw(thr);
DUK_WO_NORETURN(return;);
}
DUK_EXTERNAL void duk_error_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
va_list ap;
DUK_ASSERT_API_ENTRY(thr);
va_start(ap, fmt);
duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
va_end(ap);
(void) duk_throw(thr);
DUK_WO_NORETURN(return;);
}
#if !defined(DUK_USE_VARIADIC_MACROS)
DUK_NORETURN(DUK_LOCAL_DECL void duk__throw_error_from_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, va_list ap));
DUK_LOCAL void duk__throw_error_from_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, va_list ap) {
const char *filename;
duk_int_t line;
DUK_ASSERT_CTX_VALID(thr);
filename = duk_api_global_filename;
line = duk_api_global_line;
duk_api_global_filename = NULL;
duk_api_global_line = 0;
duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
(void) duk_throw(thr);
DUK_WO_NORETURN(return;);
}
#define DUK__ERROR_STASH_SHARED(code) do { \
va_list ap; \
va_start(ap, fmt); \
duk__throw_error_from_stash(thr, (code), fmt, ap); \
va_end(ap); \
DUK_WO_NORETURN(return 0;); \
} while (0)
DUK_EXTERNAL duk_ret_t duk_error_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(err_code);
}
DUK_EXTERNAL duk_ret_t duk_generic_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_eval_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_EVAL_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_range_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_RANGE_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_reference_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_REFERENCE_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_syntax_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_SYNTAX_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_type_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_TYPE_ERROR);
}
DUK_EXTERNAL duk_ret_t duk_uri_error_stash(duk_hthread *thr, const char *fmt, ...) {
DUK_ASSERT_API_ENTRY(thr);
DUK__ERROR_STASH_SHARED(DUK_ERR_URI_ERROR);
}
#endif /* DUK_USE_VARIADIC_MACROS */
/*
* Comparison
*/
DUK_EXTERNAL duk_bool_t duk_equals(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
duk_tval *tv1, *tv2;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_get_tval(thr, idx1);
tv2 = duk_get_tval(thr, idx2);
if ((tv1 == NULL) || (tv2 == NULL)) {
return 0;
}
/* Coercion may be needed, the helper handles that by pushing the
* tagged values to the stack.
*/
return duk_js_equals(thr, tv1, tv2);
}
DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
duk_tval *tv1, *tv2;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_get_tval(thr, idx1);
tv2 = duk_get_tval(thr, idx2);
if ((tv1 == NULL) || (tv2 == NULL)) {
return 0;
}
/* No coercions or other side effects, so safe */
return duk_js_strict_equals(tv1, tv2);
}
DUK_EXTERNAL duk_bool_t duk_samevalue(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
duk_tval *tv1, *tv2;
DUK_ASSERT_API_ENTRY(thr);
tv1 = duk_get_tval(thr, idx1);
tv2 = duk_get_tval(thr, idx2);
if ((tv1 == NULL) || (tv2 == NULL)) {
return 0;
}
/* No coercions or other side effects, so safe */
return duk_js_samevalue(tv1, tv2);
}
/*
* instanceof
*/
DUK_EXTERNAL duk_bool_t duk_instanceof(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
duk_tval *tv1, *tv2;
DUK_ASSERT_API_ENTRY(thr);
/* Index validation is strict, which differs from duk_equals().
* The strict behavior mimics how instanceof itself works, e.g.
* it is a TypeError if rval is not a -callable- object. It would
* be somewhat inconsistent if rval would be allowed to be
* non-existent without a TypeError.
*/
tv1 = duk_require_tval(thr, idx1);
DUK_ASSERT(tv1 != NULL);
tv2 = duk_require_tval(thr, idx2);
DUK_ASSERT(tv2 != NULL);
return duk_js_instanceof(thr, tv1, tv2);
}
/*
* Lightfunc
*/
DUK_INTERNAL void duk_push_lightfunc_name_raw(duk_hthread *thr, duk_c_function func, duk_small_uint_t lf_flags) {
/* Lightfunc name, includes Duktape/C native function pointer, which
* can often be used to locate the function from a symbol table.
* The name also includes the 16-bit duk_tval flags field because it
* includes the magic value. Because a single native function often
* provides different functionality depending on the magic value, it
* seems reasonably to include it in the name.
*
* On the other hand, a complicated name increases string table
* pressure in low memory environments (but only when function name
* is accessed).
*/
DUK_ASSERT_API_ENTRY(thr);
duk_push_literal(thr, "light_");
duk_push_string_funcptr(thr, (duk_uint8_t *) &func, sizeof(func));
duk_push_sprintf(thr, "_%04x", (unsigned int) lf_flags);
duk_concat(thr, 3);
}
DUK_INTERNAL void duk_push_lightfunc_name(duk_hthread *thr, duk_tval *tv) {
duk_c_function func;
duk_small_uint_t lf_flags;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
duk_push_lightfunc_name_raw(thr, func, lf_flags);
}
DUK_INTERNAL void duk_push_lightfunc_tostring(duk_hthread *thr, duk_tval *tv) {
duk_c_function func;
duk_small_uint_t lf_flags;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags); /* read before 'tv' potentially invalidated */
duk_push_literal(thr, "function ");
duk_push_lightfunc_name_raw(thr, func, lf_flags);
duk_push_literal(thr, "() { [lightfunc code] }");
duk_concat(thr, 3);
}
/*
* Function pointers
*
* Printing function pointers is non-portable, so we do that by hex printing
* bytes from memory.
*/
DUK_INTERNAL void duk_push_string_funcptr(duk_hthread *thr, duk_uint8_t *ptr, duk_size_t sz) {
duk_uint8_t buf[32 * 2];
duk_uint8_t *p, *q;
duk_small_uint_t i;
duk_small_uint_t t;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */
p = buf;
#if defined(DUK_USE_INTEGER_LE)
q = ptr + sz;
#else
q = ptr;
#endif
for (i = 0; i < sz; i++) {
#if defined(DUK_USE_INTEGER_LE)
t = *(--q);
#else
t = *(q++);
#endif
*p++ = duk_lc_digits[t >> 4];
*p++ = duk_lc_digits[t & 0x0f];
}
duk_push_lstring(thr, (const char *) buf, sz * 2);
}
/*
* Push readable string summarizing duk_tval. The operation is side effect
* free and will only throw from internal errors (e.g. out of memory).
* This is used by e.g. property access code to summarize a key/base safely,
* and is not intended to be fast (but small and safe).
*/
/* String limits for summary strings. */
#define DUK__READABLE_SUMMARY_MAXCHARS 96 /* maximum supported by helper */
#define DUK__READABLE_STRING_MAXCHARS 32 /* for strings/symbols */
#define DUK__READABLE_ERRMSG_MAXCHARS 96 /* for error messages */
/* String sanitizer which escapes ASCII control characters and a few other
* ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with
* question marks. No errors are thrown for any input string, except in out
* of memory situations.
*/
DUK_LOCAL void duk__push_hstring_readable_unicode(duk_hthread *thr, duk_hstring *h_input, duk_small_uint_t maxchars) {
const duk_uint8_t *p, *p_start, *p_end;
duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH * DUK__READABLE_SUMMARY_MAXCHARS +
2 /*quotes*/ + 3 /*periods*/];
duk_uint8_t *q;
duk_ucodepoint_t cp;
duk_small_uint_t nchars;
DUK_ASSERT_CTX_VALID(thr);
DUK_ASSERT(h_input != NULL);
DUK_ASSERT(maxchars <= DUK__READABLE_SUMMARY_MAXCHARS);
p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
p = p_start;
q = buf;
nchars = 0;
*q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
for (;;) {
if (p >= p_end) {
break;
}
if (nchars == maxchars) {
*q++ = (duk_uint8_t) DUK_ASC_PERIOD;
*q++ = (duk_uint8_t) DUK_ASC_PERIOD;
*q++ = (duk_uint8_t) DUK_ASC_PERIOD;
break;
}
if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) {
DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */
DUK_ASSERT((cp >> 4) <= 0x0f);
*q++ = (duk_uint8_t) DUK_ASC_BACKSLASH;
*q++ = (duk_uint8_t) DUK_ASC_LC_X;
*q++ = (duk_uint8_t) duk_lc_digits[cp >> 4];
*q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f];
} else {
q += duk_unicode_encode_xutf8(cp, q);
}
} else {
p++; /* advance manually */
*q++ = (duk_uint8_t) DUK_ASC_QUESTION;
}
nchars++;
}
*q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
duk_push_lstring(thr, (const char *) buf, (duk_size_t) (q - buf));
}
DUK_LOCAL const char *duk__push_string_tval_readable(duk_hthread *thr, duk_tval *tv, duk_bool_t error_aware) {
DUK_ASSERT_CTX_VALID(thr);
/* 'tv' may be NULL */
if (tv == NULL) {
duk_push_literal(thr, "none");
} else {
switch (DUK_TVAL_GET_TAG(tv)) {
case DUK_TAG_STRING: {
duk_hstring *h = DUK_TVAL_GET_STRING(tv);
if (DUK_HSTRING_HAS_SYMBOL(h)) {
/* XXX: string summary produces question marks
* so this is not very ideal.
*/
duk_push_literal(thr, "[Symbol ");
duk_push_string(thr, duk__get_symbol_type_string(h));
duk_push_literal(thr, " ");
duk__push_hstring_readable_unicode(thr, h, DUK__READABLE_STRING_MAXCHARS);
duk_push_literal(thr, "]");
duk_concat(thr, 5);
break;
}
duk__push_hstring_readable_unicode(thr, h, DUK__READABLE_STRING_MAXCHARS);
break;
}
case DUK_TAG_OBJECT: {
duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
DUK_ASSERT(h != NULL);
if (error_aware &&
duk_hobject_prototype_chain_contains(thr, h, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE], 1 /*ignore_loop*/)) {
/* Get error message in a side effect free way if
* possible; if not, summarize as a generic object.
* Error message currently gets quoted.
*/
/* XXX: better internal getprop call; get without side effects
* but traverse inheritance chain.
*/
duk_tval *tv_msg;
tv_msg = duk_hobject_find_existing_entry_tval_ptr(thr->heap, h, DUK_HTHREAD_STRING_MESSAGE(thr));
if (tv_msg != NULL && DUK_TVAL_IS_STRING(tv_msg)) {
/* It's critical to avoid recursion so
* only summarize a string .message.
*/
duk__push_hstring_readable_unicode(thr, DUK_TVAL_GET_STRING(tv_msg), DUK__READABLE_ERRMSG_MAXCHARS);
break;
}
}
duk_push_class_string_tval(thr, tv, 1 /*avoid_side_effects*/);
break;
}
case DUK_TAG_BUFFER: {
/* While plain buffers mimic Uint8Arrays, they summarize differently.
* This is useful so that the summarized string accurately reflects the
* internal type which may matter for figuring out bugs etc.
*/
/* XXX: Hex encoded, length limited buffer summary here? */
duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
DUK_ASSERT(h != NULL);
duk_push_sprintf(thr, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h));
break;
}
case DUK_TAG_POINTER: {
/* Surround with parentheses like in JX, ensures NULL pointer
* is distinguishable from null value ("(null)" vs "null").
*/
duk_push_tval(thr, tv);
duk_push_sprintf(thr, "(%s)", duk_to_string(thr, -1));
duk_remove_m2(thr);
break;
}
default: {
duk_push_tval(thr, tv);
break;
}
}
}
return duk_to_string(thr, -1);
}
DUK_INTERNAL const char *duk_push_string_tval_readable(duk_hthread *thr, duk_tval *tv) {
DUK_ASSERT_API_ENTRY(thr);
return duk__push_string_tval_readable(thr, tv, 0 /*error_aware*/);
}
DUK_INTERNAL const char *duk_push_string_readable(duk_hthread *thr, duk_idx_t idx) {
DUK_ASSERT_API_ENTRY(thr);
return duk_push_string_tval_readable(thr, duk_get_tval(thr, idx));
}
DUK_INTERNAL const char *duk_push_string_tval_readable_error(duk_hthread *thr, duk_tval *tv) {
DUK_ASSERT_API_ENTRY(thr);
return duk__push_string_tval_readable(thr, tv, 1 /*error_aware*/);
}
DUK_INTERNAL void duk_push_symbol_descriptive_string(duk_hthread *thr, duk_hstring *h) {
const duk_uint8_t *p;
const duk_uint8_t *p_end;
const duk_uint8_t *q;
DUK_ASSERT_API_ENTRY(thr);
/* .toString() */
duk_push_literal(thr, "Symbol(");
p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
p_end = p + DUK_HSTRING_GET_BYTELEN(h);
DUK_ASSERT(p[0] == 0xff || (p[0] & 0xc0) == 0x80);
p++;
for (q = p; q < p_end; q++) {
if (*q == 0xffU) {
/* Terminate either at end-of-string (but NUL MUST
* be accepted without terminating description) or
* 0xFF, which is used to mark start of unique trailer
* (and cannot occur in CESU-8 / extended UTF-8).
*/
break;
}
}
duk_push_lstring(thr, (const char *) p, (duk_size_t) (q - p));
duk_push_literal(thr, ")");
duk_concat(thr, 3);
}
/*
* Functions
*/
#if 0 /* not used yet */
DUK_INTERNAL void duk_push_hnatfunc_name(duk_hthread *thr, duk_hnatfunc *h) {
duk_c_function func;
DUK_ASSERT_API_ENTRY(thr);
DUK_ASSERT(h != NULL);
DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC((duk_hobject *) h));
duk_push_sprintf(thr, "native_");
func = h->func;
duk_push_string_funcptr(thr, (duk_uint8_t *) &func, sizeof(func));
duk_push_sprintf(thr, "_%04x_%04x",
(unsigned int) (duk_uint16_t) h->nargs,
(unsigned int) (duk_uint16_t) h->magic);
duk_concat(thr, 3);
}
#endif
/*
* duk_tval slice copy
*/
DUK_INTERNAL void duk_copy_tvals_incref(duk_hthread *thr, duk_tval *tv_dst, duk_tval *tv_src, duk_size_t count) {
duk_tval *tv;
DUK_ASSERT_API_ENTRY(thr);
DUK_UNREF(thr);
DUK_ASSERT(count * sizeof(duk_tval) >= count); /* no wrap */
duk_memcpy_unsafe((void *) tv_dst, (const void *) tv_src, count * sizeof(duk_tval));
tv = tv_dst;
while (count-- > 0) {
DUK_TVAL_INCREF(thr, tv);
tv++;
}
}
|