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
|
/*
* ------------------------------------------------------------------------
* 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 class definitions. Classes are composed of
* data members (public/protected/common) and the member functions
* (methods/procs) that operate on them. Each class has its own
* namespace which manages the class scope.
*
* ========================================================================
* 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"
/*
* This structure is a subclass of Tcl_ResolvedVarInfo that contains the
* ItclVarLookup info needed at runtime.
*/
typedef struct ItclResolvedVarInfo {
Tcl_ResolvedVarInfo vinfo; /* This must be the first element. */
ItclVarLookup *vlookup; /* Pointer to lookup info. */
} ItclResolvedVarInfo;
/*
* FORWARD DECLARATIONS
*/
static void ItclDestroyClass _ANSI_ARGS_((ClientData cdata));
static void ItclDestroyClassNamesp _ANSI_ARGS_((ClientData cdata));
static void ItclFreeClass _ANSI_ARGS_((char* cdata));
static Tcl_Var ItclClassRuntimeVarResolver _ANSI_ARGS_((
Tcl_Interp *interp, Tcl_ResolvedVarInfo *vinfoPtr));
extern int itclCompatFlags;
/*
* ------------------------------------------------------------------------
* Itcl_CreateClass()
*
* Creates a namespace and its associated class definition data.
* If a namespace already exists with that name, then this routine
* returns TCL_ERROR, along with an error message in the interp.
* If successful, it returns TCL_OK and a pointer to the new class
* definition.
* ------------------------------------------------------------------------
*/
int
Itcl_CreateClass(interp, path, info, rPtr)
Tcl_Interp* interp; /* interpreter that will contain new class */
CONST char* path; /* name of new class */
ItclObjectInfo *info; /* info for all known objects */
ItclClass **rPtr; /* returns: pointer to class definition */
{
char *head, *tail;
Tcl_DString buffer;
Tcl_Command cmd;
Tcl_Namespace *classNs;
ItclClass *cdPtr;
ItclVarDefn *vdefn;
Tcl_HashEntry *entry;
int newEntry;
/*
* Make sure that a class with the given name does not
* already exist in the current namespace context. If a
* namespace exists, that's okay. It may have been created
* to contain stubs during a "namespace import" operation.
* We'll just replace the namespace data below with the
* proper class data.
*/
classNs = Tcl_FindNamespace(interp, path,
(Tcl_Namespace*)NULL, /* flags */ 0);
if (classNs != NULL && Itcl_IsClassNamespace(classNs)) {
Tcl_AppendResult(interp,
"class \"", path, "\" already exists",
(char*)NULL);
return TCL_ERROR;
}
/*
* Make sure that a command with the given class name does not
* already exist in the current namespace. This prevents the
* usual Tcl commands from being clobbered when a programmer
* makes a bogus call like "class info".
*/
cmd = Tcl_FindCommand(interp, path,
(Tcl_Namespace*)NULL, /* flags */ TCL_NAMESPACE_ONLY);
if (cmd != NULL && !Itcl_IsStub(cmd)) {
Tcl_AppendResult(interp,
"command \"", path, "\" already exists",
(char*)NULL);
if (strstr(path,"::") == NULL) {
Tcl_AppendResult(interp,
" in namespace \"",
Tcl_GetCurrentNamespace(interp)->fullName, "\"",
(char*)NULL);
}
return TCL_ERROR;
}
/*
* Make sure that the class name does not have any goofy
* characters:
*
* . => reserved for member access like: class.publicVar
*/
Itcl_ParseNamespPath(path, &buffer, &head, &tail);
if (strstr(tail,".")) {
Tcl_AppendResult(interp,
"bad class name \"", tail, "\"",
(char*)NULL);
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
Tcl_DStringFree(&buffer);
/*
* Allocate class definition data.
*/
cdPtr = (ItclClass*)ckalloc(sizeof(ItclClass));
cdPtr->name = NULL;
cdPtr->fullname = NULL;
cdPtr->interp = interp;
cdPtr->info = info; Itcl_PreserveData((ClientData)info);
cdPtr->namesp = NULL;
cdPtr->accessCmd = NULL;
Tcl_InitHashTable(&cdPtr->variables, TCL_STRING_KEYS);
Tcl_InitHashTable(&cdPtr->functions, TCL_STRING_KEYS);
cdPtr->numInstanceVars = 0;
Tcl_InitHashTable(&cdPtr->resolveVars, TCL_STRING_KEYS);
Tcl_InitHashTable(&cdPtr->resolveCmds, TCL_STRING_KEYS);
Itcl_InitList(&cdPtr->bases);
Itcl_InitList(&cdPtr->derived);
cdPtr->initCode = NULL;
cdPtr->unique = 0;
cdPtr->flags = 0;
/*
* Initialize the heritage info--each class starts with its
* own class definition in the heritage. Base classes are
* added to the heritage from the "inherit" statement.
*/
Tcl_InitHashTable(&cdPtr->heritage, TCL_ONE_WORD_KEYS);
(void) Tcl_CreateHashEntry(&cdPtr->heritage, (char*)cdPtr, &newEntry);
/*
* Create a namespace to represent the class. Add the class
* definition info as client data for the namespace. If the
* namespace already exists, then replace any existing client
* data with the class data.
*/
Itcl_PreserveData((ClientData)cdPtr);
if (classNs == NULL) {
classNs = Tcl_CreateNamespace(interp, path,
(ClientData)cdPtr, ItclDestroyClassNamesp);
}
else {
if (classNs->clientData && classNs->deleteProc) {
(*classNs->deleteProc)(classNs->clientData);
}
classNs->clientData = (ClientData)cdPtr;
classNs->deleteProc = ItclDestroyClassNamesp;
}
Itcl_EventuallyFree((ClientData)cdPtr, ItclFreeClass);
if (classNs == NULL) {
Itcl_ReleaseData((ClientData)cdPtr);
return TCL_ERROR;
}
cdPtr->namesp = classNs;
cdPtr->name = (char*)ckalloc((unsigned)(strlen(classNs->name)+1));
strcpy(cdPtr->name, classNs->name);
cdPtr->fullname = (char*)ckalloc((unsigned)(strlen(classNs->fullName)+1));
strcpy(cdPtr->fullname, classNs->fullName);
/*
* Add special name resolution procedures to the class namespace
* so that members are accessed according to the rules for
* [incr Tcl].
*/
Tcl_SetNamespaceResolvers(classNs,
(Tcl_ResolveCmdProc*)Itcl_ClassCmdResolver,
(Tcl_ResolveVarProc*)Itcl_ClassVarResolver,
(Tcl_ResolveCompiledVarProc*)Itcl_ClassCompiledVarResolver);
/*
* Add the built-in "this" variable to the list of data members.
*/
(void) Itcl_CreateVarDefn(interp, cdPtr, "this",
(char*)NULL, (char*)NULL, &vdefn);
vdefn->member->protection = ITCL_PROTECTED; /* always "protected" */
vdefn->member->flags |= ITCL_THIS_VAR; /* mark as "this" variable */
entry = Tcl_CreateHashEntry(&cdPtr->variables, "this", &newEntry);
Tcl_SetHashValue(entry, (ClientData)vdefn);
/*
* Create a command in the current namespace to manage the class:
* <className>
* <className> <objName> ?<constructor-args>?
*/
Itcl_PreserveData((ClientData)cdPtr);
cdPtr->accessCmd = Tcl_CreateObjCommand(interp,
cdPtr->fullname, Itcl_HandleClass,
(ClientData)cdPtr, ItclDestroyClass);
*rPtr = cdPtr;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteClass()
*
* Deletes a class by deleting all derived classes and all objects in
* that class, and finally, by destroying the class namespace. This
* procedure provides a friendly way of doing this. If any errors
* are detected along the way, the process is aborted.
*
* Returns TCL_OK if successful, or TCL_ERROR (along with an error
* message in the interpreter) if anything goes wrong.
* ------------------------------------------------------------------------
*/
int
Itcl_DeleteClass(interp, cdefnPtr)
Tcl_Interp *interp; /* interpreter managing this class */
ItclClass *cdefnPtr; /* class namespace */
{
ItclClass *cdPtr = NULL;
Itcl_ListElem *elem;
ItclObject *contextObj;
Tcl_HashEntry *entry;
Tcl_HashSearch place;
Tcl_DString buffer;
/*
* Destroy all derived classes, since these lose their meaning
* when the base class goes away. If anything goes wrong,
* abort with an error.
*
* TRICKY NOTE: When a derived class is destroyed, it
* automatically deletes itself from the "derived" list.
*/
elem = Itcl_FirstListElem(&cdefnPtr->derived);
while (elem) {
cdPtr = (ItclClass*)Itcl_GetListValue(elem);
elem = Itcl_NextListElem(elem); /* advance here--elem will go away */
if (Itcl_DeleteClass(interp, cdPtr) != TCL_OK) {
goto deleteClassFail;
}
}
/*
* Scan through and find all objects that belong to this class.
* Note that more specialized objects have already been
* destroyed above, when derived classes were destroyed.
* Destroy objects and report any errors.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->info->objects, &place);
while (entry) {
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
if (contextObj->classDefn == cdefnPtr) {
if (Itcl_DeleteObject(interp, contextObj) != TCL_OK) {
cdPtr = cdefnPtr;
goto deleteClassFail;
}
/*
* Fix 227804: Whenever an object to delete was found we
* have to reset the search to the beginning as the
* current entry in the search was deleted and accessing it
* is therefore not allowed anymore.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->info->objects, &place);
continue;
}
entry = Tcl_NextHashEntry(&place);
}
/*
* Destroy the namespace associated with this class.
*
* TRICKY NOTE:
* The cleanup procedure associated with the namespace is
* invoked automatically. It does all of the same things
* above, but it also disconnects this class from its
* base-class lists, and removes the class access command.
*/
Tcl_DeleteNamespace(cdefnPtr->namesp);
return TCL_OK;
deleteClassFail:
Tcl_DStringInit(&buffer);
Tcl_DStringAppend(&buffer, "\n (while deleting class \"", -1);
Tcl_DStringAppend(&buffer, cdPtr->namesp->fullName, -1);
Tcl_DStringAppend(&buffer, "\")", -1);
Tcl_AddErrorInfo(interp, Tcl_DStringValue(&buffer));
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
/*
* ------------------------------------------------------------------------
* ItclDestroyClass()
*
* Invoked whenever the access command for a class is destroyed.
* Destroys the namespace associated with the class, which also
* destroys all objects in the class and all derived classes.
* Disconnects this class from the "derived" class lists of its
* base classes, and releases any claim to the class definition
* data. If this is the last use of that data, the class will
* completely vanish at this point.
* ------------------------------------------------------------------------
*/
static void
ItclDestroyClass(cdata)
ClientData cdata; /* class definition to be destroyed */
{
ItclClass *cdefnPtr = (ItclClass*)cdata;
cdefnPtr->accessCmd = NULL;
Tcl_DeleteNamespace(cdefnPtr->namesp);
Itcl_ReleaseData((ClientData)cdefnPtr);
}
/*
* ------------------------------------------------------------------------
* ItclDestroyClassNamesp()
*
* Invoked whenever the namespace associated with a class is destroyed.
* Destroys all objects associated with this class and all derived
* classes. Disconnects this class from the "derived" class lists
* of its base classes, and removes the class access command. Releases
* any claim to the class definition data. If this is the last use
* of that data, the class will completely vanish at this point.
* ------------------------------------------------------------------------
*/
static void
ItclDestroyClassNamesp(cdata)
ClientData cdata; /* class definition to be destroyed */
{
ItclClass *cdefnPtr = (ItclClass*)cdata;
ItclObject *contextObj;
Itcl_ListElem *elem, *belem;
ItclClass *cdPtr, *basePtr, *derivedPtr;
Tcl_HashEntry *entry;
Tcl_HashSearch place;
/*
* Destroy all derived classes, since these lose their meaning
* when the base class goes away.
*
* TRICKY NOTE: When a derived class is destroyed, it
* automatically deletes itself from the "derived" list.
*/
elem = Itcl_FirstListElem(&cdefnPtr->derived);
while (elem) {
cdPtr = (ItclClass*)Itcl_GetListValue(elem);
Tcl_DeleteNamespace(cdPtr->namesp);
/* As the first namespace is now destroyed we have to get the
* new first element of the hash table. We cannot go to the
* next element from the current one, because the current one
* is deleted. itcl Patch #593112, for Bug #577719.
*/
elem = Itcl_FirstListElem(&cdefnPtr->derived);
}
/*
* Scan through and find all objects that belong to this class.
* Destroy them quietly by deleting their access command.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->info->objects, &place);
while (entry) {
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
if (contextObj->classDefn == cdefnPtr) {
Tcl_DeleteCommandFromToken(cdefnPtr->interp, contextObj->accessCmd);
/*
* Fix 227804: Whenever an object to delete was found we
* have to reset the search to the beginning as the
* current entry in the search was deleted and accessing it
* is therefore not allowed anymore.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->info->objects, &place);
continue;
}
entry = Tcl_NextHashEntry(&place);
}
/*
* Next, remove this class from the "derived" list in
* all base classes.
*/
belem = Itcl_FirstListElem(&cdefnPtr->bases);
while (belem) {
basePtr = (ItclClass*)Itcl_GetListValue(belem);
elem = Itcl_FirstListElem(&basePtr->derived);
while (elem) {
derivedPtr = (ItclClass*)Itcl_GetListValue(elem);
if (derivedPtr == cdefnPtr) {
Itcl_ReleaseData( Itcl_GetListValue(elem) );
elem = Itcl_DeleteListElem(elem);
} else {
elem = Itcl_NextListElem(elem);
}
}
belem = Itcl_NextListElem(belem);
}
/*
* Next, destroy the access command associated with the class.
*/
if (cdefnPtr->accessCmd) {
Command *cmdPtr = (Command*)cdefnPtr->accessCmd;
cmdPtr->deleteProc = Itcl_ReleaseData;
Tcl_DeleteCommandFromToken(cdefnPtr->interp, cdefnPtr->accessCmd);
}
/*
* Release the namespace's claim on the class definition.
*/
Itcl_ReleaseData((ClientData)cdefnPtr);
}
/*
* ------------------------------------------------------------------------
* ItclFreeClass()
*
* Frees all memory associated with a class definition. This is
* usually invoked automatically by Itcl_ReleaseData(), when class
* data is no longer being used.
* ------------------------------------------------------------------------
*/
static void
ItclFreeClass(cdata)
char *cdata; /* class definition to be destroyed */
{
ItclClass *cdefnPtr = (ItclClass*)cdata;
Itcl_ListElem *elem;
Tcl_HashSearch place;
Tcl_HashEntry *entry;
ItclVarDefn *vdefn;
ItclVarLookup *vlookup;
VarInHash *varPtr;
/*
* Tear down the list of derived classes. This list should
* really be empty if everything is working properly, but
* release it here just in case.
*/
elem = Itcl_FirstListElem(&cdefnPtr->derived);
while (elem) {
Itcl_ReleaseData( Itcl_GetListValue(elem) );
elem = Itcl_NextListElem(elem);
}
Itcl_DeleteList(&cdefnPtr->derived);
/*
* Tear down the variable resolution table. Some records
* appear multiple times in the table (for x, foo::x, etc.)
* so each one has a reference count.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->resolveVars, &place);
while (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (--vlookup->usage == 0) {
/*
* If this is a common variable owned by this class,
* then release the class's hold on it. If it's no
* longer being used, move it into a variable table
* for destruction.
*/
if ( (vlookup->vdefn->member->flags & ITCL_COMMON) != 0 &&
vlookup->vdefn->member->classDefn == cdefnPtr ) {
varPtr = (VarInHash*)vlookup->var.common;
if (--ItclVarRefCount(varPtr) == 0) {
/*
* This is called after the namespace is already gone: the
* variable is already unset and ready to be freed.
*/
ckfree((char *)varPtr);
}
}
ckfree((char*)vlookup);
}
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(&cdefnPtr->resolveVars);
/*
* Tear down the virtual method table...
*/
Tcl_DeleteHashTable(&cdefnPtr->resolveCmds);
/*
* Delete all variable definitions.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->variables, &place);
while (entry) {
vdefn = (ItclVarDefn*)Tcl_GetHashValue(entry);
Itcl_DeleteVarDefn(vdefn);
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(&cdefnPtr->variables);
/*
* Delete all function definitions.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->functions, &place);
while (entry) {
Itcl_ReleaseData( Tcl_GetHashValue(entry) );
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(&cdefnPtr->functions);
/*
* Release the claim on all base classes.
*/
elem = Itcl_FirstListElem(&cdefnPtr->bases);
while (elem) {
Itcl_ReleaseData( Itcl_GetListValue(elem) );
elem = Itcl_NextListElem(elem);
}
Itcl_DeleteList(&cdefnPtr->bases);
Tcl_DeleteHashTable(&cdefnPtr->heritage);
/*
* Free up the object initialization code.
*/
if (cdefnPtr->initCode) {
Tcl_DecrRefCount(cdefnPtr->initCode);
}
Itcl_ReleaseData((ClientData)cdefnPtr->info);
ckfree(cdefnPtr->name);
ckfree(cdefnPtr->fullname);
ckfree((char*)cdefnPtr);
}
/*
* ------------------------------------------------------------------------
* Itcl_IsClassNamespace()
*
* Checks to see whether or not the given namespace represents an
* [incr Tcl] class. Returns non-zero if so, and zero otherwise.
* ------------------------------------------------------------------------
*/
int
Itcl_IsClassNamespace(namesp)
Tcl_Namespace *namesp; /* namespace being tested */
{
Namespace *nsPtr = (Namespace*)namesp;
if (nsPtr != NULL) {
return (nsPtr->deleteProc == ItclDestroyClassNamesp);
}
return 0;
}
/*
* ------------------------------------------------------------------------
* Itcl_IsClass()
*
* Checks the given Tcl command to see if it represents an itcl class.
* Returns non-zero if the command is associated with a class.
* ------------------------------------------------------------------------
*/
int
Itcl_IsClass(cmd)
Tcl_Command cmd; /* command being tested */
{
Command *cmdPtr = (Command*)cmd;
if (cmdPtr->deleteProc == ItclDestroyClass) {
return 1;
}
/*
* This may be an imported command. Try to get the real
* command and see if it represents a class.
*/
cmdPtr = (Command*)TclGetOriginalCommand(cmd);
if (cmdPtr && cmdPtr->deleteProc == ItclDestroyClass) {
return 1;
}
return 0;
}
/*
* ------------------------------------------------------------------------
* Itcl_FindClass()
*
* Searches for the specified class in the active namespace. If the
* class is found, this procedure returns a pointer to the class
* definition. Otherwise, if the autoload flag is non-zero, an
* attempt will be made to autoload the class definition. If it
* still can't be found, this procedure returns NULL, along with an
* error message in the interpreter.
* ------------------------------------------------------------------------
*/
ItclClass*
Itcl_FindClass(interp, path, autoload)
Tcl_Interp* interp; /* interpreter containing class */
CONST char* path; /* path name for class */
int autoload; /* should class be loaded */
{
Tcl_Namespace* classNs;
/*
* Search for a namespace with the specified name, and if
* one is found, see if it is a class namespace.
*/
classNs = Itcl_FindClassNamespace(interp, path);
if (classNs && Itcl_IsClassNamespace(classNs)) {
return (ItclClass*)classNs->clientData;
}
/*
* If the autoload flag is set, try to autoload the class
* definition.
*/
if (autoload) {
if (Tcl_VarEval(interp, "::auto_load ", path, (char*)NULL) != TCL_OK) {
char msg[256];
sprintf(msg, "\n (while attempting to autoload class \"%.200s\")", path);
Tcl_AddErrorInfo(interp, msg);
return NULL;
}
Tcl_ResetResult(interp);
classNs = Itcl_FindClassNamespace(interp, path);
if (classNs && Itcl_IsClassNamespace(classNs)) {
return (ItclClass*)classNs->clientData;
}
}
Tcl_AppendResult(interp, "class \"", path, "\" not found in context \"",
Tcl_GetCurrentNamespace(interp)->fullName, "\"",
(char*)NULL);
return NULL;
}
/*
* ------------------------------------------------------------------------
* Itcl_FindClassNamespace()
*
* Searches for the specified class namespace. The normal Tcl procedure
* Tcl_FindNamespace also searches for namespaces, but only in the
* current namespace context. This makes it hard to find one class
* from within another. For example, suppose. you have two namespaces
* Foo and Bar. If you're in the context of Foo and you look for
* Bar, you won't find it with Tcl_FindNamespace. This behavior is
* okay for namespaces, but wrong for classes.
*
* This procedure search for a class namespace. If the name is
* absolute (i.e., starts with "::"), then that one name is checked,
* and the class is either found or not. But if the name is relative,
* it is sought in the current namespace context and in the global
* context, just like the normal command lookup.
*
* This procedure returns a pointer to the desired namespace, or
* NULL if the namespace was not found.
* ------------------------------------------------------------------------
*/
Tcl_Namespace*
Itcl_FindClassNamespace(interp, path)
Tcl_Interp* interp; /* interpreter containing class */
CONST char* path; /* path name for class */
{
Tcl_Namespace* contextNs = Tcl_GetCurrentNamespace(interp);
Tcl_Namespace* classNs;
Tcl_DString buffer;
/*
* Look up the namespace. If the name is not absolute, then
* see if it's the current namespace, and try the global
* namespace as well.
*/
classNs = Tcl_FindNamespace(interp, path,
(Tcl_Namespace*)NULL, /* flags */ 0);
if ( !classNs && contextNs->parentPtr != NULL &&
!(*path == ':' && *(path+1) == ':') ) {
if (strcmp(contextNs->name, path) == 0) {
classNs = contextNs;
}
else {
Tcl_DStringInit(&buffer);
Tcl_DStringAppend(&buffer, "::", -1);
Tcl_DStringAppend(&buffer, path, -1);
classNs = Tcl_FindNamespace(interp, Tcl_DStringValue(&buffer),
(Tcl_Namespace*)NULL, /* flags */ 0);
Tcl_DStringFree(&buffer);
}
}
return classNs;
}
/*
* ------------------------------------------------------------------------
* Itcl_HandleClass()
*
* Invoked by Tcl whenever the user issues the command associated with
* a class name. Handles the following syntax:
*
* <className>
* <className> <objName> ?<args>...?
*
* Without any arguments, the command does nothing. In the olden days,
* this allowed the class name to be invoked by itself to prompt the
* autoloader to load the class definition. Today, this behavior is
* retained for backward compatibility with old releases.
*
* If arguments are specified, then this procedure creates a new
* object named <objName> in the appropriate class. Note that if
* <objName> contains "#auto", that part is automatically replaced
* by a unique string built from the class name.
* ------------------------------------------------------------------------
*/
int
Itcl_HandleClass(clientData, interp, objc, objv)
ClientData clientData; /* class definition */
Tcl_Interp *interp; /* current interpreter */
int objc; /* number of arguments */
Tcl_Obj *CONST objv[]; /* argument objects */
{
ItclClass *cdefnPtr = (ItclClass*)clientData;
int result = TCL_OK;
Tcl_DString buffer; /* buffer used to build object names */
char *token, *objName, *match;
ItclObject *newObj;
Itcl_CallFrame frame;
/*
* If the command is invoked without an object name, then do nothing.
* This used to support autoloading--that the class name could be
* invoked as a command by itself, prompting the autoloader to
* load the class definition. We retain the behavior here for
* backward-compatibility with earlier releases.
*/
if (objc == 1) {
return TCL_OK;
}
/*
* If the object name is "::", and if this is an old-style class
* definition, then treat the remaining arguments as a command
* in the class namespace. This used to be the way of invoking
* a class proc, but the new syntax is "class::proc" (without
* spaces).
*/
token = Tcl_GetStringFromObj(objv[1], (int*)NULL);
if ((*token == ':') && (strcmp(token,"::") == 0) && (objc > 2)) {
if ((cdefnPtr->flags & ITCL_OLD_STYLE) != 0) {
result = Tcl_PushCallFrame(interp, (Tcl_CallFrame *) &frame,
cdefnPtr->namesp, /* isProcCallFrame */ 0);
if (result != TCL_OK) {
return result;
}
result = Itcl_EvalArgs(interp, objc-2, objv+2);
Tcl_PopCallFrame(interp);
return result;
}
/*
* If this is not an old-style class, then return an error
* describing the syntax change.
*/
Tcl_AppendResult(interp,
"syntax \"class :: proc\" is an anachronism\n",
"[incr Tcl] no longer supports this syntax.\n",
"Instead, remove the spaces from your procedure invocations:\n",
" ",
Tcl_GetStringFromObj(objv[0], (int*)NULL), "::",
Tcl_GetStringFromObj(objv[2], (int*)NULL), " ?args?",
(char*)NULL);
return TCL_ERROR;
}
/*
* Otherwise, we have a proper object name. Create a new instance
* with that name. If the name contains "#auto", replace this with
* a uniquely generated string based on the class name.
*/
Tcl_DStringInit(&buffer);
objName = token;
match = strstr(token, "#auto");
if (match != NULL) {
int len;
char unique[TCL_INTEGER_SPACE]; /* for unique part of object names */
Tcl_CmdInfo dummy;
Tcl_UniChar ch;
Tcl_DStringAppend(&buffer, token, (match - token));
/*
* Only lowercase the first char of $class, per itcl #auto semantics
*/
len = Tcl_UtfToUniChar(cdefnPtr->name, &ch);
ch = Tcl_UniCharToLower(ch);
Tcl_UniCharToUtfDString(&ch, 1, &buffer);
Tcl_DStringAppend(&buffer, cdefnPtr->name + len, -1);
/*
* Substitute a unique part in for "#auto", and keep
* incrementing a counter until a valid name is found.
*/
len = Tcl_DStringLength(&buffer);
do {
sprintf(unique, "%d", cdefnPtr->unique++);
Tcl_DStringTrunc(&buffer, len);
Tcl_DStringAppend(&buffer, unique, -1);
Tcl_DStringAppend(&buffer, match+5, -1);
objName = Tcl_DStringValue(&buffer);
/*
* [Fix 227811] Check for any command with the given name, not
* only objects.
*/
if (Tcl_GetCommandInfo (interp, objName, &dummy) == 0) {
break; /* if an error is found, bail out! */
}
} while (1);
}
/*
* Try to create a new object. If successful, return the
* object name as the result of this command.
*/
result = Itcl_CreateObject(interp, objName, cdefnPtr,
objc-2, objv+2, &newObj);
if (result == TCL_OK) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(objName, -1));
}
Tcl_DStringFree(&buffer);
return result;
}
/*
* ------------------------------------------------------------------------
* Itcl_ClassCmdResolver()
*
* Used by the class namespaces to handle name resolution for all
* commands. This procedure looks for references to class methods
* and procs, and returns TCL_OK along with the appropriate Tcl
* command in the rPtr argument. If a particular command is private,
* this procedure returns TCL_ERROR and access to the command is
* denied. If a command is not recognized, this procedure returns
* TCL_CONTINUE, and lookup continues via the normal Tcl name
* resolution rules.
* ------------------------------------------------------------------------
*/
int
Itcl_ClassCmdResolver(interp, name, context, flags, rPtr)
Tcl_Interp *interp; /* current interpreter */
CONST char* name; /* name of the command being accessed */
Tcl_Namespace *context; /* namespace performing the resolution */
int flags; /* TCL_LEAVE_ERR_MSG => leave error messages
* in interp if anything goes wrong */
Tcl_Command *rPtr; /* returns: resolved command */
{
ItclClass *cdefn = (ItclClass*)context->clientData;
Tcl_HashEntry *entry;
ItclMemberFunc *mfunc;
Command *cmdPtr;
/*
* If the command is a member function, and if it is
* accessible, return its Tcl command handle.
*/
entry = Tcl_FindHashEntry(&cdefn->resolveCmds, name);
if (!entry) {
return TCL_CONTINUE;
}
mfunc = (ItclMemberFunc*)Tcl_GetHashValue(entry);
/*
* For protected/private functions, figure out whether or
* not the function is accessible from the current context.
*
* TRICKY NOTE: Use Itcl_GetTrueNamespace to determine
* the current context. If the current call frame is
* "transparent", this handles it properly.
*/
if (mfunc->member->protection != ITCL_PUBLIC) {
context = Itcl_GetTrueNamespace(interp, cdefn->info);
if (!Itcl_CanAccessFunc(mfunc, context)) {
if ((flags & TCL_LEAVE_ERR_MSG) != 0) {
Tcl_AppendResult(interp,
"can't access \"", name, "\": ",
Itcl_ProtectionStr(mfunc->member->protection),
" variable",
(char*)NULL);
}
return TCL_ERROR;
}
}
/*
* Looks like we found an accessible member function.
*
* TRICKY NOTE: Check to make sure that the command handle
* is still valid. If someone has deleted or renamed the
* command, it may not be. This is just the time to catch
* it--as it is being resolved again by the compiler.
*/
cmdPtr = (Command*)mfunc->accessCmd;
if (!cmdPtr || cmdPtr->flags & CMD_IS_DELETED) {
mfunc->accessCmd = NULL;
if ((flags & TCL_LEAVE_ERR_MSG) != 0) {
Tcl_AppendResult(interp,
"can't access \"", name, "\": deleted or redefined\n",
"(use the \"body\" command to redefine methods/procs)",
(char*)NULL);
}
return TCL_ERROR; /* disallow access! */
}
*rPtr = mfunc->accessCmd;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_ClassVarResolver()
*
* Used by the class namespaces to handle name resolution for runtime
* variable accesses. This procedure looks for references to both
* common variables and instance variables at runtime. It is used as
* a second line of defense, to handle references that could not be
* resolved as compiled locals.
*
* If a variable is found, this procedure returns TCL_OK along with
* the appropriate Tcl variable in the rPtr argument. If a particular
* variable is private, this procedure returns TCL_ERROR and access
* to the variable is denied. If a variable is not recognized, this
* procedure returns TCL_CONTINUE, and lookup continues via the normal
* Tcl name resolution rules.
* ------------------------------------------------------------------------
*/
int
Itcl_ClassVarResolver(interp, name, context, flags, rPtr)
Tcl_Interp *interp; /* current interpreter */
CONST char* name; /* name of the variable being accessed */
Tcl_Namespace *context; /* namespace performing the resolution */
int flags; /* TCL_LEAVE_ERR_MSG => leave error messages
* in interp if anything goes wrong */
Tcl_Var *rPtr; /* returns: resolved variable */
{
Interp *iPtr = (Interp *) interp;
ItclCallFrame *varFramePtr = (ItclCallFrame *) iPtr->varFramePtr;
ItclClass *cdefn = (ItclClass*)context->clientData;
ItclObject *contextObj;
Itcl_CallFrame *framePtr;
Tcl_HashEntry *entry;
ItclVarLookup *vlookup;
assert(Itcl_IsClassNamespace(context));
/*
* If this is a global variable, handle it in the usual
* Tcl manner.
*/
if (flags & TCL_GLOBAL_ONLY) {
return TCL_CONTINUE;
}
/*
* See if this is a formal parameter in the current proc scope.
* If so, that variable has precedence. Look it up and return
* it here. This duplicates some of the functionality of
* TclLookupVar, but we return it here (instead of returning
* TCL_CONTINUE) to avoid looking it up again later.
*/
if (varFramePtr && varFramePtr->isProcCallFrame
&& strstr(name,"::") == NULL) {
Proc *procPtr = varFramePtr->procPtr;
/*
* Search through compiled locals first...
*/
if (procPtr) {
int localCt = procPtr->numCompiledLocals;
CompiledLocal *localPtr = procPtr->firstLocalPtr;
Var *localVarPtr = varFramePtr->compiledLocals;
int nameLen = strlen(name);
int i;
for (i=0; i < localCt; i++) {
if (!TclIsVarTemporary(localPtr)) {
register char *localName = localPtr->name;
if ((name[0] == localName[0])
&& (nameLen == localPtr->nameLength)
&& (strcmp(name, localName) == 0)) {
*rPtr = (Tcl_Var)localVarPtr;
return TCL_OK;
}
}
ItclNextLocal(localVarPtr);
localPtr = localPtr->nextPtr;
}
}
/*
* If it's not a compiled local, then look in the frame's
* var hash table next. This variable may have been
* created on the fly.
*/
if (varFramePtr->varTablePtr != NULL) {
*rPtr = (Tcl_Var) ItclVarHashFindVar(varFramePtr->varTablePtr, name);
if (*rPtr) {
return TCL_OK;
}
}
}
/*
* See if the variable is a known data member and accessible.
*/
entry = Tcl_FindHashEntry(&cdefn->resolveVars, name);
if (entry == NULL) {
return TCL_CONTINUE;
}
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (!vlookup->accessible) {
return TCL_CONTINUE;
}
/*
* If this is a common data member, then its variable
* is easy to find. Return it directly.
*/
if ((vlookup->vdefn->member->flags & ITCL_COMMON) != 0) {
*rPtr = vlookup->var.common;
return TCL_OK;
}
/*
* If this is an instance variable, then we have to
* find the object context, then index into its data
* array to get the actual variable.
*/
framePtr = _Tcl_GetCallFrame(interp, 0);
entry = Tcl_FindHashEntry(&cdefn->info->contextFrames, (char*)framePtr);
if (entry == NULL) {
return TCL_CONTINUE;
}
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
/*
* TRICKY NOTE: We've resolved the variable in the current
* class context, but we must also be careful to get its
* index from the most-specific class context. Variables
* are arranged differently depending on which class
* constructed the object.
*/
if (contextObj->classDefn != vlookup->vdefn->member->classDefn) {
entry = Tcl_FindHashEntry(&contextObj->classDefn->resolveVars,
vlookup->vdefn->member->fullname);
if (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
}
}
*rPtr = (Tcl_Var)contextObj->data[vlookup->var.index];
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_ClassCompiledVarResolver()
*
* Used by the class namespaces to handle name resolution for compile
* time variable accesses. This procedure looks for references to
* both common variables and instance variables at compile time. If
* the variables are found, they are characterized in a generic way
* by their ItclVarLookup record. At runtime, Tcl constructs the
* compiled local variables by calling ItclClassRuntimeVarResolver.
*
* If a variable is found, this procedure returns TCL_OK along with
* information about the variable in the rPtr argument. If a particular
* variable is private, this procedure returns TCL_ERROR and access
* to the variable is denied. If a variable is not recognized, this
* procedure returns TCL_CONTINUE, and lookup continues via the normal
* Tcl name resolution rules.
* ------------------------------------------------------------------------
*/
int
Itcl_ClassCompiledVarResolver(interp, name, length, context, rPtr)
Tcl_Interp *interp; /* current interpreter */
CONST char* name; /* name of the variable being accessed */
int length; /* number of characters in name */
Tcl_Namespace *context; /* namespace performing the resolution */
Tcl_ResolvedVarInfo **rPtr; /* returns: info that makes it possible to
* resolve the variable at runtime */
{
ItclClass *cdefn = (ItclClass*)context->clientData;
Tcl_HashEntry *entry;
ItclVarLookup *vlookup;
char *buffer, storage[64];
assert(Itcl_IsClassNamespace(context));
/*
* Copy the name to local storage so we can NULL terminate it.
* If the name is long, allocate extra space for it.
*/
if (length < sizeof(storage)) {
buffer = storage;
} else {
buffer = (char*)ckalloc((unsigned)(length+1));
}
memcpy((void*)buffer, (void*)name, (size_t)length);
buffer[length] = '\0';
entry = Tcl_FindHashEntry(&cdefn->resolveVars, buffer);
if (buffer != storage) {
ckfree(buffer);
}
/*
* If the name is not found, or if it is inaccessible,
* continue on with the normal Tcl name resolution rules.
*/
if (entry == NULL) {
return TCL_CONTINUE;
}
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (!vlookup->accessible) {
return TCL_CONTINUE;
}
/*
* Return the ItclVarLookup record. At runtime, Tcl will
* call ItclClassRuntimeVarResolver with this record, to
* plug in the appropriate variable for the current object
* context.
*/
(*rPtr) = (Tcl_ResolvedVarInfo *) ckalloc(sizeof(ItclResolvedVarInfo));
(*rPtr)->fetchProc = ItclClassRuntimeVarResolver;
(*rPtr)->deleteProc = NULL;
((ItclResolvedVarInfo*)(*rPtr))->vlookup = vlookup;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* ItclClassRuntimeVarResolver()
*
* Invoked when Tcl sets up the call frame for an [incr Tcl] method/proc
* at runtime. Resolves data members identified earlier by
* Itcl_ClassCompiledVarResolver. Returns the Tcl_Var representation
* for the data member.
* ------------------------------------------------------------------------
*/
static Tcl_Var
ItclClassRuntimeVarResolver(interp, resVarInfo)
Tcl_Interp *interp; /* current interpreter */
Tcl_ResolvedVarInfo *resVarInfo; /* contains ItclVarLookup rep
* for variable */
{
ItclVarLookup *vlookup = ((ItclResolvedVarInfo*)resVarInfo)->vlookup;
Itcl_CallFrame *framePtr;
ItclClass *cdefn;
ItclObject *contextObj;
Tcl_HashEntry *entry;
/*
* If this is a common data member, then the associated
* variable is known directly.
*/
if ((vlookup->vdefn->member->flags & ITCL_COMMON) != 0) {
return vlookup->var.common;
}
cdefn = vlookup->vdefn->member->classDefn;
/*
* Otherwise, get the current object context and find the
* variable in its data table.
*
* TRICKY NOTE: Get the index for this variable using the
* virtual table for the MOST-SPECIFIC class.
*/
framePtr = _Tcl_GetCallFrame(interp, 0);
entry = Tcl_FindHashEntry(&cdefn->info->contextFrames, (char*)framePtr);
if (entry) {
contextObj = (ItclObject*)Tcl_GetHashValue(entry);
if (contextObj != NULL) {
if (contextObj->classDefn != vlookup->vdefn->member->classDefn) {
entry = Tcl_FindHashEntry(&contextObj->classDefn->resolveVars,
vlookup->vdefn->member->fullname);
if (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
}
}
return (Tcl_Var)contextObj->data[vlookup->var.index];
}
}
return NULL;
}
/*
* ------------------------------------------------------------------------
* Itcl_BuildVirtualTables()
*
* Invoked whenever the class heritage changes or members are added or
* removed from a class definition to rebuild the member lookup
* tables. There are two tables:
*
* METHODS: resolveCmds
* Used primarily in Itcl_ClassCmdResolver() to resolve all
* command references in a namespace.
*
* DATA MEMBERS: resolveVars
* Used primarily in Itcl_ClassVarResolver() to quickly resolve
* variable references in each class scope.
*
* These tables store every possible name for each command/variable
* (member, class::member, namesp::class::member, etc.). Members
* in a derived class may shadow members with the same name in a
* base class. In that case, the simple name in the resolution
* table will point to the most-specific member.
* ------------------------------------------------------------------------
*/
void
Itcl_BuildVirtualTables(cdefnPtr)
ItclClass* cdefnPtr; /* class definition being updated */
{
Tcl_HashEntry *entry;
Tcl_HashSearch place;
ItclVarLookup *vlookup;
ItclVarDefn *vdefn;
ItclMemberFunc *mfunc;
ItclHierIter hier;
ItclClass *cdPtr;
Namespace* nsPtr;
Tcl_DString buffer, buffer2;
int newEntry;
Tcl_DStringInit(&buffer);
Tcl_DStringInit(&buffer2);
/*
* Clear the variable resolution table.
*/
entry = Tcl_FirstHashEntry(&cdefnPtr->resolveVars, &place);
while (entry) {
vlookup = (ItclVarLookup*)Tcl_GetHashValue(entry);
if (--vlookup->usage == 0) {
ckfree((char*)vlookup);
}
entry = Tcl_NextHashEntry(&place);
}
Tcl_DeleteHashTable(&cdefnPtr->resolveVars);
Tcl_InitHashTable(&cdefnPtr->resolveVars, TCL_STRING_KEYS);
cdefnPtr->numInstanceVars = 0;
/*
* Set aside the first object-specific slot for the built-in
* "this" variable. Only allocate one of these, even though
* there is a definition for "this" in each class scope.
*/
cdefnPtr->numInstanceVars++;
/*
* Scan through all classes in the hierarchy, from most to
* least specific. Add a lookup entry for each variable
* into the table.
*/
Itcl_InitHierIter(&hier, cdefnPtr);
cdPtr = Itcl_AdvanceHierIter(&hier);
while (cdPtr != NULL) {
entry = Tcl_FirstHashEntry(&cdPtr->variables, &place);
while (entry) {
vdefn = (ItclVarDefn*)Tcl_GetHashValue(entry);
vlookup = (ItclVarLookup*)ckalloc(sizeof(ItclVarLookup));
vlookup->vdefn = vdefn;
vlookup->usage = 0;
vlookup->leastQualName = NULL;
/*
* If this variable is PRIVATE to another class scope,
* then mark it as "inaccessible".
*/
vlookup->accessible =
( vdefn->member->protection != ITCL_PRIVATE ||
vdefn->member->classDefn == cdefnPtr );
/*
* If this is a common variable, then keep a reference to
* the variable directly. Otherwise, keep an index into
* the object's variable table.
*/
if ((vdefn->member->flags & ITCL_COMMON) != 0) {
nsPtr = (Namespace*)cdPtr->namesp;
vlookup->var.common = (Tcl_Var) ItclVarHashFindVar(&nsPtr->varTable, vdefn->member->name);
assert(vlookup->var.common != NULL);
}
else {
/*
* If this is a reference to the built-in "this"
* variable, then its index is "0". Otherwise,
* add another slot to the end of the table.
*/
if ((vdefn->member->flags & ITCL_THIS_VAR) != 0) {
vlookup->var.index = 0;
}
else {
vlookup->var.index = cdefnPtr->numInstanceVars++;
}
}
/*
* Create all possible names for this variable and enter
* them into the variable resolution table:
* var
* class::var
* namesp1::class::var
* namesp2::namesp1::class::var
* ...
*/
Tcl_DStringSetLength(&buffer, 0);
Tcl_DStringAppend(&buffer, vdefn->member->name, -1);
nsPtr = (Namespace*)cdPtr->namesp;
while (1) {
entry = Tcl_CreateHashEntry(&cdefnPtr->resolveVars,
Tcl_DStringValue(&buffer), &newEntry);
if (newEntry) {
Tcl_SetHashValue(entry, (ClientData)vlookup);
vlookup->usage++;
if (!vlookup->leastQualName) {
vlookup->leastQualName =
Tcl_GetHashKey(&cdefnPtr->resolveVars, entry);
}
}
if (nsPtr == NULL) {
break;
}
Tcl_DStringSetLength(&buffer2, 0);
Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&buffer), -1);
Tcl_DStringSetLength(&buffer, 0);
Tcl_DStringAppend(&buffer, nsPtr->name, -1);
Tcl_DStringAppend(&buffer, "::", -1);
Tcl_DStringAppend(&buffer, Tcl_DStringValue(&buffer2), -1);
nsPtr = nsPtr->parentPtr;
}
/*
* If this record is not needed, free it now.
*/
if (vlookup->usage == 0) {
ckfree((char*)vlookup);
}
entry = Tcl_NextHashEntry(&place);
}
cdPtr = Itcl_AdvanceHierIter(&hier);
}
Itcl_DeleteHierIter(&hier);
/*
* Clear the command resolution table.
*/
Tcl_DeleteHashTable(&cdefnPtr->resolveCmds);
Tcl_InitHashTable(&cdefnPtr->resolveCmds, TCL_STRING_KEYS);
/*
* Scan through all classes in the hierarchy, from most to
* least specific. Look for the first (most-specific) definition
* of each member function, and enter it into the table.
*/
Itcl_InitHierIter(&hier, cdefnPtr);
cdPtr = Itcl_AdvanceHierIter(&hier);
while (cdPtr != NULL) {
entry = Tcl_FirstHashEntry(&cdPtr->functions, &place);
while (entry) {
mfunc = (ItclMemberFunc*)Tcl_GetHashValue(entry);
/*
* Create all possible names for this function and enter
* them into the command resolution table:
* func
* class::func
* namesp1::class::func
* namesp2::namesp1::class::func
* ...
*/
Tcl_DStringSetLength(&buffer, 0);
Tcl_DStringAppend(&buffer, mfunc->member->name, -1);
nsPtr = (Namespace*)cdPtr->namesp;
while (1) {
entry = Tcl_CreateHashEntry(&cdefnPtr->resolveCmds,
Tcl_DStringValue(&buffer), &newEntry);
if (newEntry) {
Tcl_SetHashValue(entry, (ClientData)mfunc);
}
if (nsPtr == NULL) {
break;
}
Tcl_DStringSetLength(&buffer2, 0);
Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&buffer), -1);
Tcl_DStringSetLength(&buffer, 0);
Tcl_DStringAppend(&buffer, nsPtr->name, -1);
Tcl_DStringAppend(&buffer, "::", -1);
Tcl_DStringAppend(&buffer, Tcl_DStringValue(&buffer2), -1);
nsPtr = nsPtr->parentPtr;
}
entry = Tcl_NextHashEntry(&place);
}
cdPtr = Itcl_AdvanceHierIter(&hier);
}
Itcl_DeleteHierIter(&hier);
Tcl_DStringFree(&buffer);
Tcl_DStringFree(&buffer2);
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateVarDefn()
*
* Creates a new class variable definition. If this is a public
* variable, it may have a bit of "config" code that is used to
* update the object whenever the variable is modified via the
* built-in "configure" method.
*
* Returns TCL_ERROR along with an error message in the specified
* interpreter if anything goes wrong. Otherwise, this returns
* TCL_OK and a pointer to the new variable definition in "vdefnPtr".
* ------------------------------------------------------------------------
*/
int
Itcl_CreateVarDefn(interp, cdefn, name, init, config, vdefnPtr)
Tcl_Interp *interp; /* interpreter managing this transaction */
ItclClass* cdefn; /* class containing this variable */
char* name; /* variable name */
char* init; /* initial value */
char* config; /* code invoked when variable is configured */
ItclVarDefn** vdefnPtr; /* returns: new variable definition */
{
int newEntry;
ItclVarDefn *vdefn;
ItclMemberCode *mcode;
Tcl_HashEntry *entry;
/*
* Add this variable to the variable table for the class.
* Make sure that the variable name does not already exist.
*/
entry = Tcl_CreateHashEntry(&cdefn->variables, name, &newEntry);
if (!newEntry) {
Tcl_AppendResult(interp,
"variable name \"", name, "\" already defined in class \"",
cdefn->fullname, "\"",
(char*)NULL);
return TCL_ERROR;
}
/*
* If this variable has some "config" code, try to capture
* its implementation.
*/
if (config) {
if (Itcl_CreateMemberCode(interp, cdefn, (char*)NULL, config,
&mcode) != TCL_OK) {
Tcl_DeleteHashEntry(entry);
return TCL_ERROR;
}
Itcl_PreserveData((ClientData)mcode);
Itcl_EventuallyFree((ClientData)mcode, (Tcl_FreeProc*) Itcl_DeleteMemberCode);
}
else {
mcode = NULL;
}
/*
* If everything looks good, create the variable definition.
*/
vdefn = (ItclVarDefn*)ckalloc(sizeof(ItclVarDefn));
vdefn->member = Itcl_CreateMember(interp, cdefn, name);
vdefn->member->code = mcode;
if (vdefn->member->protection == ITCL_DEFAULT_PROTECT) {
vdefn->member->protection = ITCL_PROTECTED;
}
if (init) {
vdefn->init = (char*)ckalloc((unsigned)(strlen(init)+1));
strcpy(vdefn->init, init);
}
else {
vdefn->init = NULL;
}
Tcl_SetHashValue(entry, (ClientData)vdefn);
*vdefnPtr = vdefn;
return TCL_OK;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteVarDefn()
*
* Destroys a variable definition created by Itcl_CreateVarDefn(),
* freeing all resources associated with it.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteVarDefn(vdefn)
ItclVarDefn *vdefn; /* variable definition to be destroyed */
{
Itcl_DeleteMember(vdefn->member);
if (vdefn->init) {
ckfree(vdefn->init);
}
ckfree((char*)vdefn);
}
/*
* ------------------------------------------------------------------------
* Itcl_GetCommonVar()
*
* Returns the current value for a common class variable. The member
* name is interpreted with respect to the given class scope. That
* scope is installed as the current context before querying the
* variable. This by-passes the protection level in case the variable
* is "private".
*
* If successful, this procedure returns a pointer to a string value
* which remains alive until the variable changes it value. If
* anything goes wrong, this returns NULL.
* ------------------------------------------------------------------------
*/
CONST char*
Itcl_GetCommonVar(interp, name, contextClass)
Tcl_Interp *interp; /* current interpreter */
CONST char *name; /* name of desired instance variable */
ItclClass *contextClass; /* name is interpreted in this scope */
{
CONST char *val = NULL;
int result;
Itcl_CallFrame frame;
/*
* Activate the namespace for the given class. That installs
* the appropriate name resolution rules and by-passes any
* security restrictions.
*/
result = Tcl_PushCallFrame(interp, (Tcl_CallFrame *) &frame,
contextClass->namesp, /*isProcCallFrame*/ 0);
if (result == TCL_OK) {
val = Tcl_GetVar2(interp, name, (char*)NULL, 0);
Tcl_PopCallFrame(interp);
}
return val;
}
/*
* ------------------------------------------------------------------------
* Itcl_CreateMember()
*
* Creates the data record representing a class member. This is the
* generic representation for a data member or member function.
* Returns a pointer to the new representation.
* ------------------------------------------------------------------------
*/
ItclMember*
Itcl_CreateMember(interp, cdefn, name)
Tcl_Interp* interp; /* interpreter managing this action */
ItclClass *cdefn; /* class definition */
CONST char* name; /* name of new member */
{
ItclMember *memPtr;
int fullsize;
/*
* Allocate the memory for a class member and fill in values.
*/
memPtr = (ItclMember*)ckalloc(sizeof(ItclMember));
memPtr->interp = interp;
memPtr->classDefn = cdefn;
memPtr->flags = 0;
memPtr->protection = Itcl_Protection(interp, 0);
memPtr->code = NULL;
fullsize = strlen(cdefn->fullname) + strlen(name) + 2;
memPtr->fullname = (char*)ckalloc((unsigned)(fullsize+1));
strcpy(memPtr->fullname, cdefn->fullname);
strcat(memPtr->fullname, "::");
strcat(memPtr->fullname, name);
memPtr->name = (char*)ckalloc((unsigned)(strlen(name)+1));
strcpy(memPtr->name, name);
return memPtr;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteMember()
*
* Destroys all data associated with the given member function definition.
* Usually invoked by the interpreter when a member function is deleted.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteMember(memPtr)
ItclMember *memPtr; /* pointer to member function definition */
{
if (memPtr) {
ckfree(memPtr->name);
ckfree(memPtr->fullname);
if (memPtr->code) {
Itcl_ReleaseData((ClientData)memPtr->code);
}
memPtr->code = NULL;
ckfree((char*)memPtr);
}
}
/*
* ------------------------------------------------------------------------
* Itcl_InitHierIter()
*
* Initializes an iterator for traversing the hierarchy of the given
* class. Subsequent calls to Itcl_AdvanceHierIter() will return
* the base classes in order from most-to-least specific.
* ------------------------------------------------------------------------
*/
void
Itcl_InitHierIter(iter,cdefn)
ItclHierIter *iter; /* iterator used for traversal */
ItclClass *cdefn; /* class definition for start of traversal */
{
Itcl_InitStack(&iter->stack);
Itcl_PushStack((ClientData)cdefn, &iter->stack);
iter->current = cdefn;
}
/*
* ------------------------------------------------------------------------
* Itcl_DeleteHierIter()
*
* Destroys an iterator for traversing class hierarchies, freeing
* all memory associated with it.
* ------------------------------------------------------------------------
*/
void
Itcl_DeleteHierIter(iter)
ItclHierIter *iter; /* iterator used for traversal */
{
Itcl_DeleteStack(&iter->stack);
iter->current = NULL;
}
/*
* ------------------------------------------------------------------------
* Itcl_AdvanceHierIter()
*
* Moves a class hierarchy iterator forward to the next base class.
* Returns a pointer to the current class definition, or NULL when
* the end of the hierarchy has been reached.
* ------------------------------------------------------------------------
*/
ItclClass*
Itcl_AdvanceHierIter(iter)
ItclHierIter *iter; /* iterator used for traversal */
{
register Itcl_ListElem *elem;
ItclClass *cdPtr;
iter->current = (ItclClass*)Itcl_PopStack(&iter->stack);
/*
* Push classes onto the stack in reverse order, so that
* they will be popped off in the proper order.
*/
if (iter->current) {
cdPtr = (ItclClass*)iter->current;
elem = Itcl_LastListElem(&cdPtr->bases);
while (elem) {
Itcl_PushStack(Itcl_GetListValue(elem), &iter->stack);
elem = Itcl_PrevListElem(elem);
}
}
return iter->current;
}
|