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
|
/*
* ------------------------------------------------------------------------
* PACKAGE: [incr Tcl]
* DESCRIPTION: Object-Oriented Extensions to Tcl
*
* [incr Tcl] provides object-oriented extensions to Tcl, much as
* C++ provides object-oriented extensions to C. It provides a means
* of encapsulating related procedures together with their shared data
* in a local namespace that is hidden from the outside world. It
* promotes code re-use through inheritance. More than anything else,
* it encourages better organization of Tcl applications through the
* object-oriented paradigm, leading to code that is easier to
* understand and maintain.
*
* These procedures handle commands available within a class scope.
* In [incr Tcl], the term "method" is used for a procedure that has
* access to object-specific data, while the term "proc" is used for
* a procedure that has access only to common class data.
*
* ========================================================================
* AUTHOR: Michael J. McLennan
* Bell Labs Innovations for Lucent Technologies
* mmclennan@lucent.com
* http://www.tcltk.com/itcl
* ========================================================================
* Copyright (c) 1993-1998 Lucent Technologies, Inc.
* ------------------------------------------------------------------------
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "itclInt.h"
/*
* FORWARD DECLARATIONS
*/
static int ItclParseConfig _ANSI_ARGS_((Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[], ItclObject *contextObj,
int *rargc, ItclVarDefn ***rvars, char ***rvals));
static int ItclHandleConfig _ANSI_ARGS_((Tcl_Interp *interp,
int argc, ItclVarDefn **vars, char **vals, ItclObject *contextObj));
/*
* ------------------------------------------------------------------------
* Itcl_BodyCmd()
*
* Invoked by Tcl whenever the user issues an "itcl::body" command to
* define or redefine the implementation for a class method/proc.
* Handles the following syntax:
*
* itcl::body <class>::<func> <arglist> <body>
*
* Looks for an existing class member function with the name <func>,
* and if found, tries to assign the implementation. If an argument
* list was specified in the original declaration, it must match
* <arglist> or an error is flagged. If <body> has the form "@name"
* then it is treated as a reference to a C handling procedure;
* otherwise, it is taken as a body of Tcl statements.
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_BodyCmd(dummy, interp, objc, objv)
ClientData dummy; /* unused */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int status = TCL_OK;
char *head, *tail, *token, *arglist, *body;
ItclClass *cdefn;
ItclMemberFunc *mfunc;
Tcl_HashEntry *entry;
Tcl_DString buffer;
if (objc != 4) {
token = Tcl_GetStringFromObj(objv[0], (int*)NULL);
Tcl_AppendResult(interp,
"wrong # args: should be \"",
token, " class::func arglist body\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Parse the member name "namesp::namesp::class::func".
* Make sure that a class name was specified, and that the
* class exists.
*/
token = Tcl_GetStringFromObj(objv[1], (int*)NULL);
Itcl_ParseNamespPath(token, &buffer, &head, &tail);
if (!head || *head == '\0') {
Tcl_AppendResult(interp,
"missing class specifier for body declaration \"", token, "\"",
(char*)NULL);
status = TCL_ERROR;
goto bodyCmdDone;
}
cdefn = Itcl_FindClass(interp, head, /* autoload */ 1);
if (cdefn == NULL) {
status = TCL_ERROR;
goto bodyCmdDone;
}
/*
* Find the function and try to change its implementation.
* Note that command resolution table contains *all* functions,
* even those in a base class. Make sure that the class
* containing the method definition is the requested class.
*/
mfunc = NULL;
entry = Tcl_FindHashEntry(&cdefn->resolveCmds, tail);
if (entry) {
mfunc = (ItclMemberFunc*)Tcl_GetHashValue(entry);
if (mfunc->member->classDefn != cdefn) {
mfunc = NULL;
}
}
if (mfunc == NULL) {
Tcl_AppendResult(interp,
"function \"", tail, "\" is not defined in class \"",
cdefn->fullname, "\"",
(char*)NULL);
status = TCL_ERROR;
goto bodyCmdDone;
}
arglist = Tcl_GetStringFromObj(objv[2], (int*)NULL);
body = Tcl_GetStringFromObj(objv[3], (int*)NULL);
if (Itcl_ChangeMemberFunc(interp, mfunc, arglist, body) != TCL_OK) {
status = TCL_ERROR;
goto bodyCmdDone;
}
bodyCmdDone:
Tcl_DStringFree(&buffer);
return status;
}
/*
* ------------------------------------------------------------------------
* Itcl_ConfigBodyCmd()
*
* Invoked by Tcl whenever the user issues an "itcl::configbody" command
* to define or redefine the configuration code associated with a
* public variable. Handles the following syntax:
*
* itcl::configbody <class>::<publicVar> <body>
*
* Looks for an existing public variable with the name <publicVar>,
* and if found, tries to assign the implementation. If <body> has
* the form "@name" then it is treated as a reference to a C handling
* procedure; otherwise, it is taken as a body of Tcl statements.
*
* Returns TCL_OK/TCL_ERROR to indicate success/failure.
* ------------------------------------------------------------------------
*/
/* ARGSUSED */
int
Itcl_ConfigBodyCmd(dummy, interp, objc, objv)
ClientData dummy; /* unused */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int status = TCL_OK;
char *head, *tail, *token;
Tcl_DString buffer;
ItclClass *cdefn;
ItclVarLookup *vlookup;
ItclMember *member;
ItclMemberCode *mcode;
Tcl_HashEntry *entry;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "class::option body");
return TCL_ERROR;
}
/*
* Parse the member name "namesp::namesp::class::option".
* Make sure that a class name was specified, and that the
* class exists.
*/
token = Tcl_GetStringFromObj(objv[1], (int*)NULL);
Itcl_ParseNamespPath(token, &buffer, &head, &tail);
if (!head || *head == '\0') {
Tcl_AppendResult(interp,
"missing class specifier for body declaration \"", token, "\"",
(char*)NULL);
status = TCL_ERROR;
goto configBodyCmdDone;
}
cdefn = Itcl_FindClass(interp, head, /* autoload */ 1);
if (cdefn == NULL) {
status = TCL_ERROR;
goto configBodyCmdDone;
}
/*
* Find the variable and change its implementation.
* Note that variable resolution table has *all* variables,
* even those in a base class. Make sure that the class
* containing the variable definition is the requested class.
*/
vlookup = NULL;
entry = Tcl_FindHashEntry(&cdefn->resolveVars, tail);
if (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (vlookup->vdefn->member->classDefn != cdefn) {
vlookup = NULL;
}
}
if (vlookup == NULL) {
Tcl_AppendResult(interp,
"option \"", tail, "\" is not defined in class \"",
cdefn->fullname, "\"",
(char*)NULL);
status = TCL_ERROR;
goto configBodyCmdDone;
}
member = vlookup->vdefn->member;
if (member->protection != ITCL_PUBLIC) {
Tcl_AppendResult(interp,
"option \"", member->fullname,
"\" is not a public configuration option",
(char*)NULL);
status = TCL_ERROR;
goto configBodyCmdDone;
}
token = Tcl_GetStringFromObj(objv[2], (int*)NULL);
if (Itcl_CreateMemberCode(interp, cdefn, (char*)NULL, token,
&mcode) != TCL_OK) {
status = TCL_ERROR;
goto configBodyCmdDone;
}
Itcl_PreserveData((ClientData)mcode);
Itcl_EventuallyFree((ClientData)mcode, (Tcl_FreeProc*) Itcl_DeleteMemberCode);
if (member->code) {
Itcl_ReleaseData((ClientData)member->code);
}
member->code = mcode;
configBodyCmdDone:
Tcl_DStringFree(&buffer);
return status;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateMethod()
*
* Installs a method into the namespace associated with a class.
* If another command with the same name is already installed, then
* it is overwritten.
*
* Returns TCL_OK on success, or TCL_ERROR (along with an error message
* in the specified interp) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateMethod(interp, cdefn, name, arglist, body)
Tcl_Interp* interp; /* interpreter managing this action */
ItclClass *cdefn; /* class definition */
CONST char* name; /* name of new method */
CONST char* arglist; /* space-separated list of arg names */
CONST char* body; /* body of commands for the method */
{
ItclMemberFunc *mfunc;
Tcl_DString buffer;
/*
* Make sure that the method name does not contain anything
* goofy like a "::" scope qualifier.
*/
if (strstr(name,"::")) {
Tcl_AppendResult(interp,
"bad method name \"", name, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Create the method definition.
*/
if (Itcl_CreateMemberFunc(interp, cdefn, name, arglist, body, &mfunc)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* Build a fully-qualified name for the method, and install
* the command handler.
*/
Tcl_DStringInit(&buffer);
Tcl_DStringAppend(&buffer, cdefn->namesp->fullName, -1);
Tcl_DStringAppend(&buffer, "::", 2);
Tcl_DStringAppend(&buffer, name, -1);
name = Tcl_DStringValue(&buffer);
Itcl_PreserveData((ClientData)mfunc);
mfunc->accessCmd = Tcl_CreateObjCommand(interp, name,
Itcl_ExecMethod, (ClientData)mfunc, Itcl_ReleaseData);
Tcl_DStringFree(&buffer);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateProc()
*
* Installs a class proc into the namespace associated with a class.
* If another command with the same name is already installed, then
* it is overwritten. Returns TCL_OK on success, or TCL_ERROR (along
* with an error message in the specified interp) if anything goes
* wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateProc(interp, cdefn, name, arglist, body)
Tcl_Interp* interp; /* interpreter managing this action */
ItclClass *cdefn; /* class definition */
CONST char* name; /* name of new proc */
CONST char* arglist; /* space-separated list of arg names */
CONST char* body; /* body of commands for the proc */
{
ItclMemberFunc *mfunc;
Tcl_DString buffer;
/*
* Make sure that the proc name does not contain anything
* goofy like a "::" scope qualifier.
*/
if (strstr(name,"::")) {
Tcl_AppendResult(interp,
"bad proc name \"", name, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Create the proc definition.
*/
if (Itcl_CreateMemberFunc(interp, cdefn, name, arglist, body, &mfunc)
!= TCL_OK) {
return TCL_ERROR;
}
/*
* Mark procs as "common". This distinguishes them from methods.
*/
mfunc->member->flags |= ITCL_COMMON;
/*
* Build a fully-qualified name for the proc, and install
* the command handler.
*/
Tcl_DStringInit(&buffer);
Tcl_DStringAppend(&buffer, cdefn->namesp->fullName, -1);
Tcl_DStringAppend(&buffer, "::", 2);
Tcl_DStringAppend(&buffer, name, -1);
name = Tcl_DStringValue(&buffer);
Itcl_PreserveData((ClientData)mfunc);
mfunc->accessCmd = Tcl_CreateObjCommand(interp, name,
Itcl_ExecProc, (ClientData)mfunc, Itcl_ReleaseData);
Tcl_DStringFree(&buffer);
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateMemberFunc()
*
* Creates the data record representing a member function. This
* includes the argument list and the body of the function. If the
* body is of the form "@name", then it is treated as a label for
* a C procedure registered by Itcl_RegisterC().
*
* If any errors are encountered, this procedure returns TCL_ERROR
* along with an error message in the interpreter. Otherwise, it
* returns TCL_OK, and "mfuncPtr" returns a pointer to the new
* member function.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateMemberFunc(interp, cdefn, name, arglist, body, mfuncPtr)
Tcl_Interp* interp; /* interpreter managing this action */
ItclClass *cdefn; /* class definition */
CONST char* name; /* name of new member */
CONST char* arglist; /* space-separated list of arg names */
CONST char* body; /* body of commands for the method */
ItclMemberFunc** mfuncPtr; /* returns: pointer to new method defn */
{
int newEntry;
ItclMemberFunc *mfunc;
ItclMemberCode *mcode;
Tcl_HashEntry *entry;
/*
* Add the member function to the list of functions for
* the class. Make sure that a member function with the
* same name doesn't already exist.
*/
entry = Tcl_CreateHashEntry(&cdefn->functions, name, &newEntry);
if (!newEntry) {
Tcl_AppendResult(interp,
"\"", name, "\" already defined in class \"",
cdefn->fullname, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* Try to create the implementation for this command member.
*/
if (Itcl_CreateMemberCode(interp, cdefn, arglist, body,
&mcode) != TCL_OK) {
Tcl_DeleteHashEntry(entry);
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)mcode);
Itcl_EventuallyFree((ClientData)mcode, (Tcl_FreeProc*) Itcl_DeleteMemberCode);
/*
* Allocate a member function definition and return.
*/
mfunc = (ItclMemberFunc*)ckalloc(sizeof(ItclMemberFunc));
mfunc->member = Itcl_CreateMember(interp, cdefn, name);
mfunc->member->code = mcode;
if (mfunc->member->protection == ITCL_DEFAULT_PROTECT) {
mfunc->member->protection = ITCL_PUBLIC;
}
mfunc->arglist = NULL;
mfunc->argcount = 0;
mfunc->accessCmd = NULL;
if (arglist) {
mfunc->member->flags |= ITCL_ARG_SPEC;
}
if (mcode->arglist) {
Itcl_CreateArgList(interp, arglist, &mfunc->argcount, &mfunc->arglist);
}
if (strcmp(name,"constructor") == 0) {
mfunc->member->flags |= ITCL_CONSTRUCTOR;
}
if (strcmp(name,"destructor") == 0) {
mfunc->member->flags |= ITCL_DESTRUCTOR;
}
Tcl_SetHashValue(entry, (ClientData)mfunc);
Itcl_PreserveData((ClientData)mfunc);
Itcl_EventuallyFree((ClientData)mfunc, (Tcl_FreeProc*) Itcl_DeleteMemberFunc);
*mfuncPtr = mfunc;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_ChangeMemberFunc()
*
* Modifies the data record representing a member function. This
* is usually the body of the function, but can include the argument
* list if it was not defined when the member was first created.
* If the body is of the form "@name", then it is treated as a label
* for a C procedure registered by Itcl_RegisterC().
*
* If any errors are encountered, this procedure returns TCL_ERROR
* along with an error message in the interpreter. Otherwise, it
* returns TCL_OK, and "mfuncPtr" returns a pointer to the new
* member function.
* ------------------------------------------------------------------------
*/
int
Itcl_ChangeMemberFunc(interp, mfunc, arglist, body)
Tcl_Interp* interp; /* interpreter managing this action */
ItclMemberFunc* mfunc; /* command member being changed */
CONST char* arglist; /* space-separated list of arg names */
CONST char* body; /* body of commands for the method */
{
ItclMemberCode *mcode = NULL;
Tcl_Obj *objPtr;
/*
* Try to create the implementation for this command member.
*/
if (Itcl_CreateMemberCode(interp, mfunc->member->classDefn,
arglist, body, &mcode) != TCL_OK) {
return TCL_ERROR;
}
/*
* If the argument list was defined when the function was
* created, compare the arg lists or usage strings to make sure
* that the interface is not being redefined.
*/
if ((mfunc->member->flags & ITCL_ARG_SPEC) != 0 &&
!Itcl_EquivArgLists(mfunc->arglist, mfunc->argcount,
mcode->arglist, mcode->argcount)) {
objPtr = Itcl_ArgList(mfunc->argcount, mfunc->arglist);
Tcl_IncrRefCount(objPtr);
Tcl_AppendResult(interp,
"argument list changed for function \"",
mfunc->member->fullname, "\": should be \"",
Tcl_GetStringFromObj(objPtr, (int*)NULL), "\"",
(char*)NULL);
Tcl_DecrRefCount(objPtr);
Itcl_DeleteMemberCode((char*)mcode);
return TCL_ERROR;
}
/*
* Free up the old implementation and install the new one.
*/
Itcl_PreserveData((ClientData)mcode);
Itcl_EventuallyFree((ClientData)mcode, (Tcl_FreeProc*) Itcl_DeleteMemberCode);
Itcl_ReleaseData((ClientData)mfunc->member->code);
mfunc->member->code = mcode;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteMemberFunc()
*
* Destroys all data associated with the given member function definition.
* Usually invoked by the interpreter when a member function is deleted.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteMemberFunc(cdata)
CONST char* cdata; /* pointer to member function definition */
{
ItclMemberFunc* mfunc = (ItclMemberFunc*)cdata;
if (mfunc) {
Itcl_DeleteMember(mfunc->member);
if (mfunc->arglist) {
Itcl_DeleteArgList(mfunc->arglist);
}
ckfree((char*)mfunc);
}
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateMemberCode()
*
* Creates the data record representing the implementation behind a
* class member function. This includes the argument list and the body
* of the function. If the body is of the form "@name", then it is
* treated as a label for a C procedure registered by Itcl_RegisterC().
*
* The implementation is kept by the member function definition, and
* controlled by a preserve/release paradigm. That way, if it is in
* use while it is being redefined, it will stay around long enough
* to avoid a core dump.
*
* If any errors are encountered, this procedure returns TCL_ERROR
* along with an error message in the interpreter. Otherwise, it
* returns TCL_OK, and "mcodePtr" returns a pointer to the new
* implementation.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateMemberCode(interp, cdefn, arglist, body, mcodePtr)
Tcl_Interp* interp; /* interpreter managing this action */
ItclClass *cdefn; /* class containing this member */
CONST char* arglist; /* space-separated list of arg names */
CONST char* body; /* body of commands for the method */
ItclMemberCode** mcodePtr; /* returns: pointer to new implementation */
{
int argc;
CompiledLocal *args, *localPtr;
ItclMemberCode *mcode;
Proc *procPtr;
/*
* Allocate some space to hold the implementation.
*/
mcode = (ItclMemberCode*)ckalloc(sizeof(ItclMemberCode));
memset(mcode, 0, sizeof(ItclMemberCode));
if (arglist) {
if (Itcl_CreateArgList(interp, arglist, &argc, &args)
!= TCL_OK) {
Itcl_DeleteMemberCode((char*)mcode);
return TCL_ERROR;
}
mcode->argcount = argc;
mcode->arglist = args;
mcode->flags |= ITCL_ARG_SPEC;
} else {
argc = 0;
args = NULL;
}
/*
* Create a standard Tcl Proc representation for this code body.
* This is required, since the Tcl compiler looks for a proc
* when handling things such as the call frame context and
* compiled locals.
*/
procPtr = (Proc*)ckalloc(sizeof(Proc));
mcode->procPtr = procPtr;
procPtr->iPtr = (Interp*)interp;
procPtr->refCount = 1;
procPtr->cmdPtr = (Command*)ckalloc(sizeof(Command));
memset(procPtr->cmdPtr, 0, sizeof(Command));
procPtr->cmdPtr->nsPtr = (Namespace*)cdefn->namesp;
if (body) {
procPtr->bodyPtr = Tcl_NewStringObj(body, -1);
} else {
procPtr->bodyPtr = Tcl_NewStringObj("", -1);
mcode->flags |= ITCL_IMPLEMENT_NONE;
}
Tcl_IncrRefCount(procPtr->bodyPtr);
/*
* Plug the argument list into the "compiled locals" list.
*
* NOTE: The storage for this argument list is owned by
* the caller, so although we plug it in here, it is not
* our responsibility to free it.
*/
procPtr->firstLocalPtr = args;
procPtr->lastLocalPtr = NULL;
for (localPtr=mcode->arglist; localPtr; localPtr=localPtr->nextPtr) {
procPtr->lastLocalPtr = localPtr;
}
procPtr->numArgs = argc;
procPtr->numCompiledLocals = argc;
/*
* If the body definition starts with '@', then treat the value
* as a symbolic name for a C procedure.
*/
if (body == NULL) {
/* No-op */
}
else if (*body == '@') {
Tcl_CmdProc *argCmdProc;
Tcl_ObjCmdProc *objCmdProc;
ClientData cdata;
if (!Itcl_FindC(interp, body+1, &argCmdProc, &objCmdProc, &cdata)) {
Tcl_AppendResult(interp,
"no registered C procedure with name \"", body+1, "\"",
(char*)NULL);
Itcl_DeleteMemberCode((char*)mcode);
return TCL_ERROR;
}
if (objCmdProc != NULL) {
mcode->flags |= ITCL_IMPLEMENT_OBJCMD;
mcode->cfunc.objCmd = objCmdProc;
mcode->clientData = cdata;
}
else if (argCmdProc != NULL) {
mcode->flags |= ITCL_IMPLEMENT_ARGCMD;
mcode->cfunc.argCmd = argCmdProc;
mcode->clientData = cdata;
}
}
/*
* Otherwise, treat the body as a chunk of Tcl code.
*/
else {
mcode->flags |= ITCL_IMPLEMENT_TCL;
}
*mcodePtr = mcode;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteMemberCode()
*
* Destroys all data associated with the given command implementation.
* Invoked automatically by Itcl_ReleaseData() when the implementation
* is no longer being used.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteMemberCode(cdata)
CONST char* cdata; /* pointer to member function definition */
{
ItclMemberCode* mcode = (ItclMemberCode*)cdata;
/*
* Free the argument list. If empty, free the compiled locals, if any.
*/
if (mcode->arglist) {
Itcl_DeleteArgList(mcode->arglist);
} else if (mcode->procPtr && mcode->procPtr->firstLocalPtr) {
Itcl_DeleteArgList(mcode->procPtr->firstLocalPtr);
}
if (mcode->procPtr) {
ckfree((char*) mcode->procPtr->cmdPtr);
if (mcode->procPtr->bodyPtr) {
Tcl_DecrRefCount(mcode->procPtr->bodyPtr);
}
ckfree((char*)mcode->procPtr);
}
ckfree((char*)mcode);
}
/*
* ------------------------------------------------------------------------
* Itcl_GetMemberCode()
*
* Makes sure that the implementation for an [incr Tcl] code body is
* ready to run. Note that a member function can be declared without
* being defined. The class definition may contain a declaration of
* the member function, but its body may be defined in a separate file.
* If an undefined function is encountered, this routine automatically
* attempts to autoload it. If the body is implemented via Tcl code,
* then it is compiled here as well.
*
* Returns TCL_ERROR (along with an error message in the interpreter)
* if an error is encountered, or if the implementation is not defined
* and cannot be autoloaded. Returns TCL_OK if implementation is
* ready to use.
* ------------------------------------------------------------------------
*/
int
Itcl_GetMemberCode(interp, member)
Tcl_Interp* interp; /* interpreter managing this action */
ItclMember* member; /* member containing code body */
{
int result;
ItclMemberCode *mcode = member->code;
assert(mcode != NULL);
/*
* If the implementation has not yet been defined, try to
* autoload it now.
*/
if (!Itcl_IsMemberCodeImplemented(mcode)) {
result = Tcl_VarEval(interp, "::auto_load ", member->fullname,
(char*)NULL);
if (result != TCL_OK) {
char msg[256];
sprintf(msg, "\n (while autoloading code for \"%.100s\")",
member->fullname);
Tcl_AddErrorInfo(interp, msg);
return result;
}
Tcl_ResetResult(interp); /* get rid of 1/0 status */
}
/*
* If the implementation is still not available, then
* autoloading must have failed.
*
* TRICKY NOTE: If code has been autoloaded, then the
* old mcode pointer is probably invalid. Go back to
* the member and look at the current code pointer again.
*/
mcode = member->code;
assert(mcode != NULL);
if (!Itcl_IsMemberCodeImplemented(mcode)) {
Tcl_AppendResult(interp,
"member function \"", member->fullname,
"\" is not defined and cannot be autoloaded",
(char*)NULL);
return TCL_ERROR;
}
/*
* If the member is a constructor and the class has an
* initialization command, compile it here.
*/
if ((member->flags & ITCL_CONSTRUCTOR) != 0 &&
(member->classDefn->initCode != NULL)) {
result = TclProcCompileProc(interp, mcode->procPtr,
member->classDefn->initCode, (Namespace*)member->classDefn->namesp,
"initialization code for", member->fullname);
if (result != TCL_OK) {
return result;
}
}
/*
* If the code body has a Tcl implementation, then compile it here.
*/
if ((mcode->flags & ITCL_IMPLEMENT_TCL) != 0) {
result = TclProcCompileProc(interp, mcode->procPtr,
mcode->procPtr->bodyPtr, (Namespace*)member->classDefn->namesp,
"body for", member->fullname);
if (result != TCL_OK) {
return result;
}
}
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_EvalMemberCode()
*
* Used to execute an ItclMemberCode representation of a code
* fragment. This code may be a body of Tcl commands, or a C handler
* procedure.
*
* Executes the command with the given arguments (objc,objv) and
* returns an integer status code (TCL_OK/TCL_ERROR). Returns the
* result string or an error message in the interpreter.
* ------------------------------------------------------------------------
*/
int
Itcl_EvalMemberCode(interp, mfunc, member, contextObj, objc, objv)
Tcl_Interp *interp; /* current interpreter */
ItclMemberFunc *mfunc; /* member func, or NULL (for error messages) */
ItclMember *member; /* command member containing code */
ItclObject *contextObj; /* object context, or NULL */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int result = TCL_OK;
Itcl_CallFrame *oldFramePtr = NULL;
int i, transparent, newEntry;
ItclObjectInfo *info;
ItclMemberCode *mcode;
ItclContext context;
Itcl_CallFrame *framePtr, *transFramePtr;
/*
* If this code does not have an implementation yet, then
* try to autoload one. Also, if this is Tcl code, make sure
* that it's compiled and ready to use.
*/
if (Itcl_GetMemberCode(interp, member) != TCL_OK) {
return TCL_ERROR;
}
mcode = member->code;
/*
* Bump the reference count on this code, in case it is
* redefined or deleted during execution.
*/
Itcl_PreserveData((ClientData)mcode);
/*
* Install a new call frame context for the current code.
* If the current call frame is marked as "transparent", then
* do an "uplevel" operation to move past it. Transparent
* call frames are installed by Itcl_HandleInstance. They
* provide a way of entering an object context without
* interfering with the normal call stack.
*/
transparent = 0;
info = member->classDefn->info;
framePtr = _Tcl_GetCallFrame(interp, 0);
for (i = Itcl_GetStackSize(&info->transparentFrames)-1; i >= 0; i--) {
transFramePtr = (Itcl_CallFrame*)
Itcl_GetStackValue(&info->transparentFrames, i);
if (framePtr == transFramePtr) {
transparent = 1;
break;
}
}
if (transparent) {
framePtr = _Tcl_GetCallFrame(interp, 1);
oldFramePtr = _Tcl_ActivateCallFrame(interp, framePtr);
}
if (Itcl_PushContext(interp, member, member->classDefn, contextObj,
&context) != TCL_OK) {
return TCL_ERROR;
}
/*
* If this is a method with a Tcl implementation, or a
* constructor with initCode, then parse its arguments now.
*/
if (mfunc && objc > 0) {
if ((mcode->flags & ITCL_IMPLEMENT_TCL) != 0 ||
( (member->flags & ITCL_CONSTRUCTOR) != 0 &&
(member->classDefn->initCode != NULL) ) ) {
if (Itcl_AssignArgs(interp, objc, objv, mfunc) != TCL_OK) {
result = TCL_ERROR;
goto evalMemberCodeDone;
}
}
}
/*
* If this code is a constructor, and if it is being invoked
* when an object is first constructed (i.e., the "constructed"
* table is still active within the object), then handle the
* "initCode" associated with the constructor and make sure that
* all base classes are properly constructed.
*
* TRICKY NOTE:
* The "initCode" must be executed here. This is the only
* opportunity where the arguments of the constructor are
* available in a call frame.
*/
if ((member->flags & ITCL_CONSTRUCTOR) && contextObj &&
contextObj->constructed) {
result = Itcl_ConstructBase(interp, contextObj, member->classDefn);
if (result != TCL_OK) {
goto evalMemberCodeDone;
}
}
/*
* Execute the code body...
*/
if ((mcode->flags & ITCL_IMPLEMENT_OBJCMD) != 0) {
result = (*mcode->cfunc.objCmd)(mcode->clientData,
interp, objc, objv);
}
else if ((mcode->flags & ITCL_IMPLEMENT_ARGCMD) != 0) {
CONST char **argv;
argv = (CONST char**)ckalloc( (unsigned)(objc*sizeof(char*)) );
for (i=0; i < objc; i++) {
argv[i] = Tcl_GetStringFromObj(objv[i], (int*)NULL);
}
result = (*mcode->cfunc.argCmd)(mcode->clientData,
interp, objc, argv);
ckfree((char*)argv);
}
else if ((mcode->flags & ITCL_IMPLEMENT_TCL) != 0) {
result = Tcl_EvalObj(interp, mcode->procPtr->bodyPtr);
}
else {
Tcl_Panic("itcl: bad implementation flag for %s", member->fullname);
}
/*
* If this is a constructor or destructor, and if it is being
* invoked at the appropriate time, keep track of which methods
* have been called. This information is used to implicitly
* invoke constructors/destructors as needed.
*/
if ((member->flags & ITCL_DESTRUCTOR) && contextObj &&
contextObj->destructed) {
Tcl_CreateHashEntry(contextObj->destructed,
member->classDefn->fullname, &newEntry);
}
if ((member->flags & ITCL_CONSTRUCTOR) && contextObj &&
contextObj->constructed) {
Tcl_CreateHashEntry(contextObj->constructed,
member->classDefn->name, &newEntry);
}
evalMemberCodeDone:
Itcl_PopContext(interp, &context);
if (transparent) {
(void) _Tcl_ActivateCallFrame(interp, oldFramePtr);
}
Itcl_ReleaseData((ClientData)mcode);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateArgList()
*
* Parses a Tcl list representing an argument declaration and returns
* a linked list of CompiledLocal values. Usually invoked as part
* of Itcl_CreateMemberFunc() when a new method or procedure is being
* defined.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateArgList(interp, decl, argcPtr, argPtr)
Tcl_Interp* interp; /* interpreter managing this function */
CONST char* decl; /* string representing argument list */
int* argcPtr; /* returns number of args in argument list */
CompiledLocal** argPtr; /* returns pointer to parsed argument list */
{
int status = TCL_OK; /* assume that this will succeed */
int i, argc, fargc;
CONST char **argv, **fargv;
CompiledLocal *localPtr, *last;
*argPtr = last = NULL;
*argcPtr = 0;
if (decl) {
if (Tcl_SplitList(interp, decl, &argc, &argv)
!= TCL_OK) {
return TCL_ERROR;
}
for (i=0; i < argc && status == TCL_OK; i++) {
if (Tcl_SplitList(interp, argv[i], &fargc, &fargv) != TCL_OK) {
status = TCL_ERROR;
}
else {
localPtr = NULL;
if (fargc == 0 || *fargv[0] == '\0') {
char mesg[100];
sprintf(mesg, "argument #%d has no name", i);
Tcl_SetResult(interp, mesg, TCL_VOLATILE);
status = TCL_ERROR;
}
else if (fargc > 2) {
Tcl_AppendResult(interp,
"too many fields in argument specifier \"",
argv[i], "\"",
(char*)NULL);
status = TCL_ERROR;
}
else if (strstr(fargv[0],"::")) {
Tcl_AppendResult(interp,
"bad argument name \"", fargv[0], "\"",
(char*)NULL);
status = TCL_ERROR;
}
else if (fargc == 1) {
localPtr = Itcl_CreateArg(fargv[0], (char*)NULL);
}
else {
localPtr = Itcl_CreateArg(fargv[0], fargv[1]);
}
if (localPtr) {
localPtr->frameIndex = i;
if (*argPtr == NULL) {
*argPtr = last = localPtr;
}
else {
last->nextPtr = localPtr;
last = localPtr;
}
}
}
ckfree((char*)fargv);
}
ckfree((char*)argv);
}
/*
* If anything went wrong, destroy whatever arguments were
* created and return an error.
*/
if (status == TCL_OK) {
*argcPtr = argc;
} else {
Itcl_DeleteArgList(*argPtr);
*argPtr = NULL;
}
return status;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateArg()
*
* Creates a new Tcl Arg structure and fills it with the given
* information. Returns a pointer to the new Arg structure.
* ------------------------------------------------------------------------
*/
CompiledLocal*
Itcl_CreateArg(name, init)
CONST char* name; /* name of new argument */
CONST char* init; /* initial value */
{
CompiledLocal *localPtr = NULL;
int nameLen;
if (name == NULL) {
name = "";
}
nameLen = strlen(name);
localPtr = (CompiledLocal*)ckalloc(
(unsigned)(sizeof(CompiledLocal)-sizeof(localPtr->name) + nameLen+1)
);
localPtr->nextPtr = NULL;
localPtr->nameLength = nameLen;
localPtr->frameIndex = 0; /* set this later */
ItclInitVarArgument(localPtr);
localPtr->resolveInfo = NULL;
if (init != NULL) {
localPtr->defValuePtr = Tcl_NewStringObj(init, -1);
Tcl_IncrRefCount(localPtr->defValuePtr);
} else {
localPtr->defValuePtr = NULL;
}
strcpy(localPtr->name, name);
return localPtr;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteArgList()
*
* Destroys a chain of arguments acting as an argument list. Usually
* invoked when a method/proc is being destroyed, to discard its
* argument list.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteArgList(arglist)
CompiledLocal *arglist; /* first argument in arg list chain */
{
CompiledLocal *localPtr, *next;
for (localPtr=arglist; localPtr; localPtr=next) {
if (localPtr->defValuePtr != NULL) {
Tcl_DecrRefCount(localPtr->defValuePtr);
}
if (localPtr->resolveInfo) {
if (localPtr->resolveInfo->deleteProc) {
localPtr->resolveInfo->deleteProc(localPtr->resolveInfo);
} else {
ckfree((char*)localPtr->resolveInfo);
}
localPtr->resolveInfo = NULL;
}
next = localPtr->nextPtr;
ckfree((char*)localPtr);
}
}
/*
* ------------------------------------------------------------------------
* Itcl_ArgList()
*
* Returns a Tcl_Obj containing the string representation for the
* given argument list. This object has a reference count of 1.
* The reference count should be decremented when the string is no
* longer needed, and it will free itself.
* ------------------------------------------------------------------------
*/
Tcl_Obj*
Itcl_ArgList(argc, arglist)
int argc; /* number of arguments */
CompiledLocal* arglist; /* first argument in arglist */
{
char *val;
Tcl_Obj *objPtr;
Tcl_DString buffer;
Tcl_DStringInit(&buffer);
while (arglist && argc-- > 0) {
if (arglist->defValuePtr) {
val = Tcl_GetStringFromObj(arglist->defValuePtr, (int*)NULL);
Tcl_DStringStartSublist(&buffer);
Tcl_DStringAppendElement(&buffer, arglist->name);
Tcl_DStringAppendElement(&buffer, val);
Tcl_DStringEndSublist(&buffer);
}
else {
Tcl_DStringAppendElement(&buffer, arglist->name);
}
arglist = arglist->nextPtr;
}
objPtr = Tcl_NewStringObj(Tcl_DStringValue(&buffer),
Tcl_DStringLength(&buffer));
Tcl_DStringFree(&buffer);
return objPtr;
}
/*
* ------------------------------------------------------------------------
* Itcl_EquivArgLists()
*
* Compares two argument lists to see if they are equivalent. The
* first list is treated as a prototype, and the second list must
* match it. Argument names may be different, but they must match in
* meaning. If one argument is optional, the corresponding argument
* must also be optional. If the prototype list ends with the magic
* "args" argument, then it matches everything in the other list.
*
* Returns non-zero if the argument lists are equivalent.
* ------------------------------------------------------------------------
*/
int
Itcl_EquivArgLists(arg1, arg1c, arg2, arg2c)
CompiledLocal* arg1; /* prototype argument list */
int arg1c; /* number of args in prototype arg list */
CompiledLocal* arg2; /* another argument list to match against */
int arg2c; /* number of args in matching list */
{
char *dval1, *dval2;
while (arg1 && arg1c > 0 && arg2 && arg2c > 0) {
/*
* If the prototype argument list ends with the magic "args"
* argument, then it matches everything in the other list.
*/
if (arg1c == 1 && strcmp(arg1->name,"args") == 0) {
return 1;
}
/*
* If one has a default value, then the other must have the
* same default value.
*/
if (arg1->defValuePtr) {
if (arg2->defValuePtr == NULL) {
return 0;
}
dval1 = Tcl_GetStringFromObj(arg1->defValuePtr, (int*)NULL);
dval2 = Tcl_GetStringFromObj(arg2->defValuePtr, (int*)NULL);
if (strcmp(dval1, dval2) != 0) {
return 0;
}
}
else if (arg2->defValuePtr) {
return 0;
}
arg1 = arg1->nextPtr; arg1c--;
arg2 = arg2->nextPtr; arg2c--;
}
if (arg1c == 1 && strcmp(arg1->name,"args") == 0) {
return 1;
}
return (arg1c == 0 && arg2c == 0);
}
/*
* ------------------------------------------------------------------------
* Itcl_GetMemberFuncUsage()
*
* Returns a string showing how a command member should be invoked.
* If the command member is a method, then the specified object name
* is reported as part of the invocation path:
*
* obj method arg ?arg arg ...?
*
* Otherwise, the "obj" pointer is ignored, and the class name is
* used as the invocation path:
*
* class::proc arg ?arg arg ...?
*
* Returns the string by appending it onto the Tcl_Obj passed in as
* an argument.
* ------------------------------------------------------------------------
*/
void
Itcl_GetMemberFuncUsage(mfunc, contextObj, objPtr)
ItclMemberFunc *mfunc; /* command member being examined */
ItclObject *contextObj; /* invoked with respect to this object */
Tcl_Obj *objPtr; /* returns: string showing usage */
{
int argcount;
char *name;
CompiledLocal *arglist, *argPtr;
Tcl_HashEntry *entry;
ItclMemberFunc *mf;
ItclClass *cdefnPtr;
/*
* If the command is a method and an object context was
* specified, then add the object context. If the method
* was a constructor, and if the object is being created,
* then report the invocation via the class creation command.
*/
if ((mfunc->member->flags & ITCL_COMMON) == 0) {
if ((mfunc->member->flags & ITCL_CONSTRUCTOR) != 0 &&
contextObj->constructed) {
cdefnPtr = (ItclClass*)contextObj->classDefn;
mf = NULL;
entry = Tcl_FindHashEntry(&cdefnPtr->resolveCmds, "constructor");
if (entry) {
mf = (ItclMemberFunc*)Tcl_GetHashValue(entry);
}
if (mf == mfunc) {
Tcl_GetCommandFullName(contextObj->classDefn->interp,
contextObj->classDefn->accessCmd, objPtr);
Tcl_AppendToObj(objPtr, " ", -1);
name = (char *) Tcl_GetCommandName(
contextObj->classDefn->interp, contextObj->accessCmd);
Tcl_AppendToObj(objPtr, name, -1);
} else {
Tcl_AppendToObj(objPtr, mfunc->member->fullname, -1);
}
} else if (contextObj && contextObj->accessCmd) {
name = (char *) Tcl_GetCommandName(contextObj->classDefn->interp,
contextObj->accessCmd);
Tcl_AppendStringsToObj(objPtr, name, " ", mfunc->member->name,
(char*)NULL);
} else {
Tcl_AppendStringsToObj(objPtr, "<object> ", mfunc->member->name,
(char*)NULL);
}
} else {
Tcl_AppendToObj(objPtr, mfunc->member->fullname, -1);
}
/*
* Add the argument usage info.
*/
if (mfunc->member->code) {
arglist = mfunc->member->code->arglist;
argcount = mfunc->member->code->argcount;
} else if (mfunc->arglist) {
arglist = mfunc->arglist;
argcount = mfunc->argcount;
} else {
arglist = NULL;
argcount = 0;
}
if (arglist) {
for (argPtr=arglist;
argPtr && argcount > 0;
argPtr=argPtr->nextPtr, argcount--) {
if (argcount == 1 && strcmp(argPtr->name, "args") == 0) {
Tcl_AppendToObj(objPtr, " ?arg arg ...?", -1);
}
else if (argPtr->defValuePtr) {
Tcl_AppendStringsToObj(objPtr, " ?", argPtr->name, "?",
(char*)NULL);
}
else {
Tcl_AppendStringsToObj(objPtr, " ", argPtr->name,
(char*)NULL);
}
}
}
}
/*
* ------------------------------------------------------------------------
* Itcl_ExecMethod()
*
* Invoked by Tcl to handle the execution of a user-defined method.
* A method is similar to the usual Tcl proc, but has access to
* object-specific data. If for some reason there is no current
* object context, then a method call is inappropriate, and an error
* is returned.
*
* Methods are implemented either as Tcl code fragments, or as C-coded
* procedures. For Tcl code fragments, command arguments are parsed
* according to the argument list, and the body is executed in the
* scope of the class where it was defined. For C procedures, the
* arguments are passed in "as-is", and the procedure is executed in
* the most-specific class scope.
* ------------------------------------------------------------------------
*/
int
Itcl_ExecMethod(clientData, interp, objc, objv)
ClientData clientData; /* method definition */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
ItclMemberFunc *mfunc = (ItclMemberFunc*)clientData;
ItclMember *member = mfunc->member;
int result = TCL_OK;
char *token;
Tcl_HashEntry *entry;
ItclClass *contextClass;
ItclObject *contextObj;
/*
* Make sure that the current namespace context includes an
* object that is being manipulated. Methods can be executed
* only if an object context exists.
*/
if (Itcl_GetContext(interp, &contextClass, &contextObj) != TCL_OK) {
return TCL_ERROR;
}
if (contextObj == NULL) {
Tcl_AppendResult(interp,
"cannot access object-specific info without an object context",
(char*)NULL);
return TCL_ERROR;
}
/*
* Make sure that this command member can be accessed from
* the current namespace context.
*/
if (mfunc->member->protection != ITCL_PUBLIC) {
Tcl_Namespace *contextNs = Itcl_GetTrueNamespace(interp,
contextClass->info);
if (!Itcl_CanAccessFunc(mfunc, contextNs)) {
Tcl_AppendResult(interp,
"can't access \"", member->fullname, "\": ",
Itcl_ProtectionStr(member->protection), " function",
(char*)NULL);
return TCL_ERROR;
}
}
/*
* All methods should be "virtual" unless they are invoked with
* a "::" scope qualifier.
*
* To implement the "virtual" behavior, find the most-specific
* implementation for the method by looking in the "resolveCmds"
* table for this class.
*/
token = Tcl_GetStringFromObj(objv[0], (int*)NULL);
if (strstr(token, "::") == NULL) {
entry = Tcl_FindHashEntry(&contextObj->classDefn->resolveCmds,
member->name);
if (entry) {
mfunc = (ItclMemberFunc*)Tcl_GetHashValue(entry);
member = mfunc->member;
}
}
/*
* Execute the code for the method. Be careful to protect
* the method in case it gets deleted during execution.
*/
Itcl_PreserveData((ClientData)mfunc);
result = Itcl_EvalMemberCode(interp, mfunc, member, contextObj,
objc, objv);
result = Itcl_ReportFuncErrors(interp, mfunc, contextObj, result);
Itcl_ReleaseData((ClientData)mfunc);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_ExecProc()
*
* Invoked by Tcl to handle the execution of a user-defined proc.
*
* Procs are implemented either as Tcl code fragments, or as C-coded
* procedures. For Tcl code fragments, command arguments are parsed
* according to the argument list, and the body is executed in the
* scope of the class where it was defined. For C procedures, the
* arguments are passed in "as-is", and the procedure is executed in
* the most-specific class scope.
* ------------------------------------------------------------------------
*/
int
Itcl_ExecProc(clientData, interp, objc, objv)
ClientData clientData; /* proc definition */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
ItclMemberFunc *mfunc = (ItclMemberFunc*)clientData;
ItclMember *member = mfunc->member;
int result = TCL_OK;
/*
* Make sure that this command member can be accessed from
* the current namespace context.
*/
if (mfunc->member->protection != ITCL_PUBLIC) {
Tcl_Namespace *contextNs = Itcl_GetTrueNamespace(interp,
mfunc->member->classDefn->info);
if (!Itcl_CanAccessFunc(mfunc, contextNs)) {
Tcl_AppendResult(interp,
"can't access \"", member->fullname, "\": ",
Itcl_ProtectionStr(member->protection), " function",
(char*)NULL);
return TCL_ERROR;
}
}
/*
* Execute the code for the proc. Be careful to protect
* the proc in case it gets deleted during execution.
*/
Itcl_PreserveData((ClientData)mfunc);
result = Itcl_EvalMemberCode(interp, mfunc, member, (ItclObject*)NULL,
objc, objv);
result = Itcl_ReportFuncErrors(interp, mfunc, (ItclObject*)NULL, result);
Itcl_ReleaseData((ClientData)mfunc);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_PushContext()
*
* Sets up the class/object context so that a body of [incr Tcl]
* code can be executed. This procedure pushes a call frame with
* the proper namespace context for the class. If an object context
* is supplied, the object's instance variables are integrated into
* the call frame so they can be accessed as local variables.
* ------------------------------------------------------------------------
*/
int
Itcl_PushContext(interp, member, contextClass, contextObj, contextPtr)
Tcl_Interp *interp; /* interpreter managing this body of code */
ItclMember *member; /* member containing code body */
ItclClass *contextClass; /* class context */
ItclObject *contextObj; /* object context, or NULL */
ItclContext *contextPtr; /* storage space for class/object context */
{
ItclCallFrame *framePtr = (ItclCallFrame *) &contextPtr->frame;
int result, localCt, newEntry;
ItclMemberCode *mcode;
Proc *procPtr;
Tcl_HashEntry *entry;
/*
* Activate the call frame. If this fails, we'll bail out
* before allocating any resources.
*
* NOTE: Always push a call frame that looks like a proc.
* This causes global variables to be handled properly
* inside methods/procs.
*/
result = Tcl_PushCallFrame(interp, (Tcl_CallFrame*)framePtr,
contextClass->namesp, /* isProcCallFrame */ 1);
if (result != TCL_OK) {
return result;
}
contextPtr->classDefn = contextClass;
contextPtr->compiledLocals = &contextPtr->localStorage[0];
/*
* If this is an object context, register it in a hash table
* of all known contexts. We'll need this later if we
* call Itcl_GetContext to get the object context for the
* current call frame.
*/
if (contextObj) {
entry = Tcl_CreateHashEntry(&contextClass->info->contextFrames,
(char*)framePtr, &newEntry);
Itcl_PreserveData((ClientData)contextObj);
Tcl_SetHashValue(entry, (ClientData)contextObj);
}
/*
* Set up the compiled locals in the call frame and assign
* argument variables.
*/
if (member) {
mcode = member->code;
procPtr = mcode->procPtr;
/*
* Invoking TclInitCompiledLocals with a framePtr->procPtr->bodyPtr
* that is not a compiled byte code type leads to a crash. So
* make sure that the body is compiled here. This needs to
* be done even if the body of the Itcl method is not implemented
* as a Tcl proc or has no implementation. The empty string should
* have been defined as the body if no implementation was defined.
*/
assert(mcode->procPtr->bodyPtr != NULL);
result = TclProcCompileProc(interp, mcode->procPtr,
mcode->procPtr->bodyPtr, (Namespace*)member->classDefn->namesp,
"body for", member->fullname);
if (result != TCL_OK) {
return result;
}
/*
* If there are too many compiled locals to fit in the default
* storage space for the context, then allocate more space.
*/
localCt = procPtr->numCompiledLocals;
if (localCt >
(int)(sizeof(contextPtr->localStorage)/itclVarLocalSize)) {
contextPtr->compiledLocals = (Var*)ckalloc(
(unsigned)(localCt * itclVarLocalSize)
);
}
/*
* Initialize and resolve compiled variable references.
* Class variables will have special resolution rules.
* In that case, we call their "resolver" procs to get our
* hands on the variable, and we make the compiled local a
* link to the real variable.
*/
framePtr->procPtr = procPtr;
framePtr->numCompiledLocals = localCt;
framePtr->compiledLocals = contextPtr->compiledLocals;
TclInitCompiledLocals(interp, (CallFrame *) framePtr,
(Namespace*)contextClass->namesp);
}
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_PopContext()
*
* Removes a class/object context previously set up by Itcl_PushContext.
* Usually called after an [incr Tcl] code body has been executed,
* to clean up.
* ------------------------------------------------------------------------
*/
void
Itcl_PopContext(interp, contextPtr)
Tcl_Interp *interp; /* interpreter managing this body of code */
ItclContext *contextPtr; /* storage space for class/object context */
{
Itcl_CallFrame *framePtr;
ItclObjectInfo *info;
ItclObject *contextObj;
Tcl_HashEntry *entry;
/*
* See if the current call frame has an object context
* associated with it. If so, release the claim on the
* object info.
*/
framePtr = _Tcl_GetCallFrame(interp, 0);
info = contextPtr->classDefn->info;
entry = Tcl_FindHashEntry(&info->contextFrames, (char*)framePtr);
if (entry != NULL) {
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
Itcl_ReleaseData((ClientData)contextObj);
Tcl_DeleteHashEntry(entry);
}
/*
* Remove the call frame.
*/
Tcl_PopCallFrame(interp);
/*
* Free the compiledLocals array if malloc'ed storage was used.
*/
if (contextPtr->compiledLocals != &contextPtr->localStorage[0]) {
ckfree((char*)contextPtr->compiledLocals);
}
}
/*
* ------------------------------------------------------------------------
* Itcl_GetContext()
*
* Convenience routine for looking up the current object/class context.
* Useful in implementing methods/procs to see what class, and perhaps
* what object, is active.
*
* Returns TCL_OK if the current namespace is a class namespace.
* Also returns pointers to the class definition, and to object
* data if an object context is active. Returns TCL_ERROR (along
* with an error message in the interpreter) if a class namespace
* is not active.
* ------------------------------------------------------------------------
*/
int
Itcl_GetContext(interp, cdefnPtr, odefnPtr)
Tcl_Interp *interp; /* current interpreter */
ItclClass **cdefnPtr; /* returns: class definition or NULL */
ItclObject **odefnPtr; /* returns: object data or NULL */
{
Tcl_Namespace *activeNs = Tcl_GetCurrentNamespace(interp);
ItclObjectInfo *info;
Itcl_CallFrame *framePtr;
Tcl_HashEntry *entry;
/*
* Return NULL for anything that cannot be found.
*/
*cdefnPtr = NULL;
*odefnPtr = NULL;
/*
* If the active namespace is a class namespace, then return
* all known info. See if the current call frame is a known
* object context, and if so, return that context.
*/
if (Itcl_IsClassNamespace(activeNs)) {
*cdefnPtr = (ItclClass*)activeNs->clientData;
framePtr = _Tcl_GetCallFrame(interp, 0);
info = (*cdefnPtr)->info;
entry = Tcl_FindHashEntry(&info->contextFrames, (char*)framePtr);
if (entry != NULL) {
*odefnPtr = (ItclObject*)Tcl_GetHashValue(entry);
}
return TCL_OK;
}
/*
* If there is no class/object context, return an error message.
*/
Tcl_AppendResult(interp,
"namespace \"", activeNs->fullName, "\" is not a class namespace",
(char*)NULL);
return TCL_ERROR;
}
/*
* ------------------------------------------------------------------------
* Itcl_AssignArgs()
*
* Matches a list of arguments against a Tcl argument specification.
* Supports all of the rules regarding arguments for Tcl procs, including
* default arguments and variable-length argument lists.
*
* Assumes that a local call frame is already installed. As variables
* are successfully matched, they are stored as variables in the call
* frame. Returns TCL_OK on success, or TCL_ERROR (along with an error
* message in interp->result) on error.
* ------------------------------------------------------------------------
*/
int
Itcl_AssignArgs(interp, objc, objv, mfunc)
Tcl_Interp *interp; /* interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
ItclMemberFunc *mfunc; /* member function info (for error messages) */
{
ItclMemberCode *mcode = mfunc->member->code;
int result = TCL_OK;
int defargc;
CONST char **defargv = NULL;
Tcl_Obj **defobjv = NULL;
int configc = 0;
ItclVarDefn **configVars = NULL;
char **configVals = NULL;
int vi, argsLeft;
ItclClass *contextClass;
ItclObject *contextObj;
CompiledLocal *argPtr;
ItclCallFrame *framePtr;
Var *varPtr;
Tcl_Obj *objPtr, *listPtr;
char *value;
framePtr = (ItclCallFrame *) _Tcl_GetCallFrame(interp, 0);
framePtr->objc = objc;
framePtr->objv = objv; /* ref counts for args are incremented below */
/*
* See if there is a current object context. We may need
* it later on.
*/
(void) Itcl_GetContext(interp, &contextClass, &contextObj);
Tcl_ResetResult(interp);
/*
* Match the actual arguments against the procedure's formal
* parameters to compute local variables.
*/
varPtr = framePtr->compiledLocals;
for (argsLeft=mcode->argcount, argPtr=mcode->arglist, objv++, objc--;
argsLeft > 0;
argPtr=argPtr->nextPtr, argsLeft--, ItclNextLocal(varPtr), objv++, objc--)
{
if (!TclIsVarArgument(argPtr)) {
Tcl_Panic("local variable %s is not argument but should be",
argPtr->name);
return TCL_ERROR;
}
if (TclIsVarTemporary(argPtr)) {
Tcl_Panic("local variable is temporary but should be an argument");
return TCL_ERROR;
}
/*
* Handle the special case of the last formal being "args".
* When it occurs, assign it a list consisting of all the
* remaining actual arguments.
*/
if ((argsLeft == 1) && (strcmp(argPtr->name, "args") == 0)) {
if (objc < 0) objc = 0;
listPtr = Tcl_NewListObj(objc, objv);
ItclVarObjValue(varPtr) = listPtr;
Tcl_IncrRefCount(listPtr); /* local var is a reference */
ItclClearVarUndefined(varPtr);
objc = 0;
break;
}
/*
* Handle the special case of the last formal being "config".
* When it occurs, treat all remaining arguments as public
* variable assignments. Set the local "config" variable
* to the list of public variables assigned.
*/
else if ( (argsLeft == 1) &&
(strcmp(argPtr->name, "config") == 0) &&
contextObj )
{
/*
* If this is not an old-style method, discourage against
* the use of the "config" argument.
*/
if ((mfunc->member->flags & ITCL_OLD_STYLE) == 0) {
Tcl_AppendResult(interp,
"\"config\" argument is an anachronism\n",
"[incr Tcl] no longer supports the \"config\" argument.\n",
"Instead, use the \"args\" argument and then use the\n",
"built-in configure method to handle args like this:\n",
" eval configure $args",
(char*)NULL);
result = TCL_ERROR;
goto argErrors;
}
/*
* Otherwise, handle the "config" argument in the usual way...
* - parse all "-name value" assignments
* - set "config" argument to the list of variable names
*/
if (objc > 0) { /* still have some arguments left? */
result = ItclParseConfig(interp, objc, objv, contextObj,
&configc, &configVars, &configVals);
if (result != TCL_OK) {
goto argErrors;
}
listPtr = Tcl_NewListObj(0, (Tcl_Obj**)NULL);
for (vi=0; vi < configc; vi++) {
objPtr = Tcl_NewStringObj(
configVars[vi]->member->classDefn->name, -1);
Tcl_AppendToObj(objPtr, "::", -1);
Tcl_AppendToObj(objPtr, configVars[vi]->member->name, -1);
Tcl_ListObjAppendElement(interp, listPtr, objPtr);
}
ItclVarObjValue(varPtr) = listPtr;
Tcl_IncrRefCount(listPtr); /* local var is a reference */
ItclClearVarUndefined(varPtr);
objc = 0; /* all remaining args handled */
}
else if (argPtr->defValuePtr) {
value = Tcl_GetStringFromObj(argPtr->defValuePtr, (int*)NULL);
result = Tcl_SplitList(interp, value, &defargc, &defargv);
if (result != TCL_OK) {
goto argErrors;
}
defobjv = (Tcl_Obj**)ckalloc(
(unsigned)(defargc*sizeof(Tcl_Obj*))
);
for (vi=0; vi < defargc; vi++) {
objPtr = Tcl_NewStringObj(defargv[vi], -1);
Tcl_IncrRefCount(objPtr);
defobjv[vi] = objPtr;
}
result = ItclParseConfig(interp, defargc, defobjv, contextObj,
&configc, &configVars, &configVals);
if (result != TCL_OK) {
goto argErrors;
}
listPtr = Tcl_NewListObj(0, (Tcl_Obj**)NULL);
for (vi=0; vi < configc; vi++) {
objPtr = Tcl_NewStringObj(
configVars[vi]->member->classDefn->name, -1);
Tcl_AppendToObj(objPtr, "::", -1);
Tcl_AppendToObj(objPtr, configVars[vi]->member->name, -1);
Tcl_ListObjAppendElement(interp, listPtr, objPtr);
}
ItclVarObjValue(varPtr) = listPtr;
Tcl_IncrRefCount(listPtr); /* local var is a reference */
ItclClearVarUndefined(varPtr);
}
else {
objPtr = Tcl_NewStringObj("", 0);
ItclVarObjValue(varPtr) = objPtr;
Tcl_IncrRefCount(objPtr); /* local var is a reference */
ItclClearVarUndefined(varPtr);
}
}
/*
* Resume the usual processing of arguments...
*/
else if (objc > 0) { /* take next arg as value */
objPtr = *objv;
ItclVarObjValue(varPtr) = objPtr;
ItclClearVarUndefined(varPtr);
Tcl_IncrRefCount(objPtr); /* local var is a reference */
}
else if (argPtr->defValuePtr) { /* ...or use default value */
objPtr = argPtr->defValuePtr;
ItclVarObjValue(varPtr) = objPtr;
ItclClearVarUndefined(varPtr);
Tcl_IncrRefCount(objPtr); /* local var is a reference */
}
else {
if (mfunc) {
objPtr = Tcl_GetObjResult(interp);
Tcl_AppendToObj(objPtr, "wrong # args: should be \"", -1);
Itcl_GetMemberFuncUsage(mfunc, contextObj, objPtr);
Tcl_AppendToObj(objPtr, "\"", -1);
} else {
Tcl_AppendResult(interp,
"no value given for parameter \"", argPtr->name, "\"",
(char*)NULL);
}
result = TCL_ERROR;
goto argErrors;
}
}
if (objc > 0) {
if (mfunc) {
objPtr = Tcl_GetObjResult(interp);
Tcl_AppendToObj(objPtr, "wrong # args: should be \"", -1);
Itcl_GetMemberFuncUsage(mfunc, contextObj, objPtr);
Tcl_AppendToObj(objPtr, "\"", -1);
} else {
Tcl_AppendResult(interp,
"too many arguments",
(char*)NULL);
}
result = TCL_ERROR;
goto argErrors;
}
/*
* Handle any "config" assignments.
*/
if (configc > 0) {
if (ItclHandleConfig(interp, configc, configVars, configVals,
contextObj) != TCL_OK) {
result = TCL_ERROR;
goto argErrors;
}
}
/*
* All arguments were successfully matched.
*/
result = TCL_OK;
/*
* If any errors were found, clean up and return error status.
*/
argErrors:
if (defobjv) {
for (vi=0; vi < defargc; vi++) {
Tcl_DecrRefCount(defobjv[vi]);
}
ckfree((char*)defobjv);
}
if (defargv) {
ckfree((char*)defargv);
}
if (configVars) {
ckfree((char*)configVars);
}
if (configVals) {
ckfree((char*)configVals);
}
return result;
}
/*
* ------------------------------------------------------------------------
* ItclParseConfig()
*
* Parses a set of arguments as "-variable value" assignments.
* Interprets all variable names in the most-specific class scope,
* so that an inherited method with a "config" parameter will work
* correctly. Returns a list of public variable names and their
* corresponding values; both lists should passed to ItclHandleConfig()
* to perform assignments, and freed when no longer in use. Returns a
* status TCL_OK/TCL_ERROR and returns error messages in the interpreter.
* ------------------------------------------------------------------------
*/
static int
ItclParseConfig(interp, objc, objv, contextObj, rargc, rvars, rvals)
Tcl_Interp *interp; /* interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
ItclObject *contextObj; /* object whose public vars are being config'd */
int *rargc; /* return: number of variables accessed */
ItclVarDefn ***rvars; /* return: list of variables */
char ***rvals; /* return: list of values */
{
int result = TCL_OK;
ItclVarLookup *vlookup;
Tcl_HashEntry *entry;
char *varName, *value;
if (objc < 0) objc = 0;
*rargc = 0;
*rvars = (ItclVarDefn**)ckalloc((unsigned)(objc*sizeof(ItclVarDefn*)));
*rvals = (char**)ckalloc((unsigned)(objc*sizeof(char*)));
while (objc-- > 0) {
/*
* Next argument should be "-variable"
*/
varName = Tcl_GetStringFromObj(*objv, (int*)NULL);
if (*varName != '-') {
Tcl_AppendResult(interp,
"syntax error in config assignment \"",
varName, "\": should be \"-variable value\"",
(char*)NULL);
result = TCL_ERROR;
break;
}
else if (objc-- <= 0) {
Tcl_AppendResult(interp,
"syntax error in config assignment \"",
varName, "\": should be \"-variable value\" (missing value)",
(char*)NULL);
result = TCL_ERROR;
break;
}
entry = Tcl_FindHashEntry(&contextObj->classDefn->resolveVars,
varName+1);
if (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
value = Tcl_GetStringFromObj(*(objv+1), (int*)NULL);
(*rvars)[*rargc] = vlookup->vdefn; /* variable definition */
(*rvals)[*rargc] = value; /* config value */
(*rargc)++;
objv += 2;
}
else {
Tcl_AppendResult(interp,
"syntax error in config assignment \"",
varName, "\": unrecognized variable",
(char*)NULL);
result = TCL_ERROR;
break;
}
}
return result;
}
/*
* ------------------------------------------------------------------------
* ItclHandleConfig()
*
* Handles the assignment of "config" values to public variables.
* The list of assignments is parsed in ItclParseConfig(), but the
* actual assignments are performed here. If the variables have any
* associated "config" code, it is invoked here as well. If errors
* are detected during assignment or "config" code execution, the
* variable is set back to its previous value and an error is returned.
*
* Returns a status TCL_OK/TCL_ERROR, and returns any error messages
* in the given interpreter.
* ------------------------------------------------------------------------
*/
static int
ItclHandleConfig(interp, argc, vars, vals, contextObj)
Tcl_Interp *interp; /* interpreter currently in control */
int argc; /* number of assignments */
ItclVarDefn **vars; /* list of public variable definitions */
char **vals; /* list of public variable values */
ItclObject *contextObj; /* object whose public vars are being config'd */
{
int result = TCL_OK;
int i;
CONST char *val;
Tcl_DString lastval;
ItclContext context;
Itcl_CallFrame *oldFramePtr, *uplevelFramePtr;
Tcl_DStringInit(&lastval);
/*
* All "config" assignments are performed in the most-specific
* class scope, so that inherited methods with "config" arguments
* will work correctly.
*/
result = Itcl_PushContext(interp, (ItclMember*)NULL,
contextObj->classDefn, contextObj, &context);
if (result != TCL_OK) {
return TCL_ERROR;
}
/*
* Perform each assignment and execute the "config" code
* associated with each variable. If any errors are encountered,
* set the variable back to its previous value, and return an error.
*/
for (i=0; i < argc; i++) {
val = Tcl_GetVar2(interp, vars[i]->member->fullname, (char*)NULL, 0);
if (!val) {
val = "";
}
Tcl_DStringSetLength(&lastval, 0);
Tcl_DStringAppend(&lastval, val, -1);
/*
* Set the variable to the specified value.
*/
if (!Tcl_SetVar2(interp, vars[i]->member->fullname, (char*)NULL,
vals[i], 0)) {
char msg[256];
sprintf(msg, "\n (while configuring public variable \"%.100s\")", vars[i]->member->fullname);
Tcl_AddErrorInfo(interp, msg);
result = TCL_ERROR;
break;
}
/*
* If the variable has a "config" condition, then execute it.
* If it fails, put the variable back the way it was and return
* an error.
*
* TRICKY NOTE: Be careful to evaluate the code one level
* up in the call stack, so that it's executed in the
* calling context, and not in the context that we've
* set up for public variable access.
*/
if (vars[i]->member->code) {
uplevelFramePtr = _Tcl_GetCallFrame(interp, 1);
oldFramePtr = _Tcl_ActivateCallFrame(interp, uplevelFramePtr);
result = Itcl_EvalMemberCode(interp, (ItclMemberFunc*)NULL,
vars[i]->member, contextObj, 0, (Tcl_Obj* CONST*)NULL);
(void) _Tcl_ActivateCallFrame(interp, oldFramePtr);
if (result != TCL_OK) {
char msg[256];
sprintf(msg, "\n (while configuring public variable \"%.100s\")", vars[i]->member->fullname);
Tcl_AddErrorInfo(interp, msg);
Tcl_SetVar2(interp, vars[i]->member->fullname, (char*)NULL,
Tcl_DStringValue(&lastval), 0);
break;
}
}
}
/*
* Clean up and return.
*/
Itcl_PopContext(interp, &context);
Tcl_DStringFree(&lastval);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_ConstructBase()
*
* Usually invoked just before executing the body of a constructor
* when an object is first created. This procedure makes sure that
* all base classes are properly constructed. If an "initCode" fragment
* was defined with the constructor for the class, then it is invoked.
* After that, the list of base classes is checked for constructors
* that are defined but have not yet been invoked. Each of these is
* invoked implicitly with no arguments.
*
* Assumes that a local call frame is already installed, and that
* constructor arguments have already been matched and are sitting in
* this frame. Returns TCL_OK on success; otherwise, this procedure
* returns TCL_ERROR, along with an error message in the interpreter.
* ------------------------------------------------------------------------
*/
int
Itcl_ConstructBase(interp, contextObj, contextClass)
Tcl_Interp *interp; /* interpreter */
ItclObject *contextObj; /* object being constructed */
ItclClass *contextClass; /* current class being constructed */
{
int result;
Itcl_ListElem *elem;
ItclClass *cdefn;
Tcl_HashEntry *entry;
/*
* If the class has an "initCode", invoke it in the current context.
*
* TRICKY NOTE:
* This context is the call frame containing the arguments
* for the constructor. The "initCode" makes sense right
* now--just before the body of the constructor is executed.
*/
if (contextClass->initCode) {
if (Tcl_EvalObj(interp, contextClass->initCode) != TCL_OK) {
return TCL_ERROR;
}
}
/*
* Scan through the list of base classes and see if any of these
* have not been constructed. Invoke base class constructors
* implicitly, as needed. Go through the list of base classes
* in reverse order, so that least-specific classes are constructed
* first.
*/
elem = Itcl_LastListElem(&contextClass->bases);
while (elem) {
cdefn = (ItclClass*)Itcl_GetListValue(elem);
if (!Tcl_FindHashEntry(contextObj->constructed, cdefn->name)) {
result = Itcl_InvokeMethodIfExists(interp, "constructor",
cdefn, contextObj, 0, (Tcl_Obj* CONST*)NULL);
if (result != TCL_OK) {
return TCL_ERROR;
}
/*
* The base class may not have a constructor, but its
* own base classes could have one. If the constructor
* wasn't found in the last step, then other base classes
* weren't constructed either. Make sure that all of its
* base classes are properly constructed.
*/
entry = Tcl_FindHashEntry(&cdefn->functions, "constructor");
if (entry == NULL) {
result = Itcl_ConstructBase(interp, contextObj, cdefn);
if (result != TCL_OK) {
return TCL_ERROR;
}
}
}
elem = Itcl_PrevListElem(elem);
}
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_InvokeMethodIfExists()
*
* Looks for a particular method in the specified class. If the
* method is found, it is invoked with the given arguments. Any
* protection level (protected/private) for the method is ignored.
* If the method does not exist, this procedure does nothing.
*
* This procedure is used primarily to invoke the constructor/destructor
* when an object is created/destroyed.
*
* Returns TCL_OK on success; otherwise, this procedure returns
* TCL_ERROR along with an error message in the interpreter.
* ------------------------------------------------------------------------
*/
int
Itcl_InvokeMethodIfExists(interp, name, contextClass, contextObj, objc, objv)
Tcl_Interp *interp; /* interpreter */
CONST char *name; /* name of desired method */
ItclClass *contextClass; /* current class being constructed */
ItclObject *contextObj; /* object being constructed */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
int result = TCL_OK;
ItclMemberFunc *mfunc;
ItclMember *member;
Tcl_HashEntry *entry;
Tcl_Obj *cmdlinePtr;
int cmdlinec;
Tcl_Obj **cmdlinev;
/*
* Scan through the list of base classes and see if any of these
* have not been constructed. Invoke base class constructors
* implicitly, as needed. Go through the list of base classes
* in reverse order, so that least-specific classes are constructed
* first.
*/
entry = Tcl_FindHashEntry(&contextClass->functions, name);
if (entry) {
mfunc = (ItclMemberFunc*)Tcl_GetHashValue(entry);
member = mfunc->member;
/*
* Prepend the method name to the list of arguments.
*/
cmdlinePtr = Itcl_CreateArgs(interp, name, objc, objv);
(void) Tcl_ListObjGetElements((Tcl_Interp*)NULL, cmdlinePtr,
&cmdlinec, &cmdlinev);
/*
* Execute the code for the method. Be careful to protect
* the method in case it gets deleted during execution.
*/
Itcl_PreserveData((ClientData)mfunc);
result = Itcl_EvalMemberCode(interp, mfunc, member,
contextObj, cmdlinec, cmdlinev);
result = Itcl_ReportFuncErrors(interp, mfunc,
contextObj, result);
Itcl_ReleaseData((ClientData)mfunc);
Tcl_DecrRefCount(cmdlinePtr);
}
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_ReportFuncErrors()
*
* Used to interpret the status code returned when the body of a
* Tcl-style proc is executed. Handles the "errorInfo" and "errorCode"
* variables properly, and adds error information into the interpreter
* if anything went wrong. Returns a new status code that should be
* treated as the return status code for the command.
*
* This same operation is usually buried in the Tcl InterpProc()
* procedure. It is defined here so that it can be reused more easily.
* ------------------------------------------------------------------------
*/
int
Itcl_ReportFuncErrors(interp, mfunc, contextObj, result)
Tcl_Interp* interp; /* interpreter being modified */
ItclMemberFunc *mfunc; /* command member that was invoked */
ItclObject *contextObj; /* object context for this command */
int result; /* integer status code from proc body */
{
Interp* iPtr = (Interp*)interp;
Tcl_Obj *objPtr;
char num[20];
if (result != TCL_OK) {
if (result == TCL_RETURN) {
result = TclUpdateReturnInfo(iPtr);
}
else if (result == TCL_ERROR) {
objPtr = Tcl_NewStringObj("\n ", -1);
Tcl_IncrRefCount(objPtr);
if (mfunc->member->flags & ITCL_CONSTRUCTOR) {
Tcl_AppendToObj(objPtr, "while constructing object \"", -1);
Tcl_GetCommandFullName(contextObj->classDefn->interp,
contextObj->accessCmd, objPtr);
Tcl_AppendToObj(objPtr, "\" in ", -1);
Tcl_AppendToObj(objPtr, mfunc->member->fullname, -1);
if ((mfunc->member->code->flags & ITCL_IMPLEMENT_TCL) != 0) {
Tcl_AppendToObj(objPtr, " (", -1);
}
}
else if (mfunc->member->flags & ITCL_DESTRUCTOR) {
Tcl_AppendToObj(objPtr, "while deleting object \"", -1);
Tcl_GetCommandFullName(contextObj->classDefn->interp,
contextObj->accessCmd, objPtr);
Tcl_AppendToObj(objPtr, "\" in ", -1);
Tcl_AppendToObj(objPtr, mfunc->member->fullname, -1);
if ((mfunc->member->code->flags & ITCL_IMPLEMENT_TCL) != 0) {
Tcl_AppendToObj(objPtr, " (", -1);
}
}
else {
Tcl_AppendToObj(objPtr, "(", -1);
if (contextObj && contextObj->accessCmd) {
Tcl_AppendToObj(objPtr, "object \"", -1);
Tcl_GetCommandFullName(contextObj->classDefn->interp,
contextObj->accessCmd, objPtr);
Tcl_AppendToObj(objPtr, "\" ", -1);
}
if ((mfunc->member->flags & ITCL_COMMON) != 0) {
Tcl_AppendToObj(objPtr, "procedure", -1);
} else {
Tcl_AppendToObj(objPtr, "method", -1);
}
Tcl_AppendToObj(objPtr, " \"", -1);
Tcl_AppendToObj(objPtr, mfunc->member->fullname, -1);
Tcl_AppendToObj(objPtr, "\" ", -1);
}
if ((mfunc->member->code->flags & ITCL_IMPLEMENT_TCL) != 0) {
Tcl_AppendToObj(objPtr, "body line ", -1);
sprintf(num, "%d", Tcl_GetErrorLine((Tcl_Interp *)iPtr));
Tcl_AppendToObj(objPtr, num, -1);
Tcl_AppendToObj(objPtr, ")", -1);
} else {
Tcl_AppendToObj(objPtr, ")", -1);
}
Tcl_AddErrorInfo(interp, Tcl_GetStringFromObj(objPtr, (int*)NULL));
Tcl_DecrRefCount(objPtr);
}
else if (result == TCL_BREAK) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"invoked \"break\" outside of a loop", -1);
result = TCL_ERROR;
}
else if (result == TCL_CONTINUE) {
Tcl_ResetResult(interp);
Tcl_AppendToObj(Tcl_GetObjResult(interp),
"invoked \"continue\" outside of a loop", -1);
result = TCL_ERROR;
}
}
return result;
}
|