Itcl - the [incr Tcl] extension

Check-in [ce5e0de1c4]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:**todo: review** on demand resolver ready (all tests passed)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sebres-on-dmnd-resolver-perf-branch
Files: files | file ages | folders
SHA3-256: ce5e0de1c4da953c08ab4d511f6c91f7985de8649f4719d97d3e1736842e86ed
User & Date: sebres 2019-04-17 20:44:05
Context
2019-04-17
20:44
some clean-ups / review check-in: 7b37e1c98b user: sebres tags: sebres-on-dmnd-resolver-perf-branch
20:44
**todo: review** on demand resolver ready (all tests passed) check-in: ce5e0de1c4 user: sebres tags: sebres-on-dmnd-resolver-perf-branch
20:43
on demand resolver almost ready (check if0-commented cases - inheritance, 2-3 test cases failed) check-in: 87972ed991 user: sebres tags: sebres-on-dmnd-resolver-perf-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/itclClass.c.

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
....
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

1785
1786
1787
1788
1789
1790
1791
....
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
....
1860
1861
1862
1863
1864
1865
1866







1867
1868
1869
1870
1871
1872
1873
....
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
....
1942
1943
1944
1945
1946
1947
1948

1949

1950
1951
1952
1953
1954
1955
1956
1957
    Tcl_HashEntry *reshPtr, *hPtr;

    /* could be resolved directly */
    if ((reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName)) != NULL) {
	return reshPtr;
    } else {

    //!!!printf("====get======== %p : %s -- v: %s ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr), lookupName);

    /* try to build virtual table for this var */
    const char *varName, *simpleName;
    Tcl_DString buffer, buffer2, *bufferC;
    ItclHierIter hier;
    ItclClass* iclsPtr2;
    ItclVarLookup *vlookup;
    ItclVariable *ivPtr;
    Tcl_Namespace* nsPtr;
    Tcl_Obj *vnObjPtr;
    int newEntry, processAncestors;





    
    /* (de)qualify to simple name */
    varName = simpleName = lookupName;
    while(*varName) {
        if (*varName++ == ':' && *varName++ == ':') {
	    simpleName = varName;
	    continue;
        };
        varName++;
    }
    vnObjPtr = Tcl_NewStringObj(simpleName, -1);
    
    processAncestors = simpleName != lookupName;


    
    Tcl_DStringInit(&buffer);
    Tcl_DStringInit(&buffer2);

    /*
     *  Scan through all classes in the hierarchy, from most to
     *  least specific.  Add a lookup entry for each variable
................................................................................
            varName = simpleName;
            bufferC = &buffer;
            nsPtr = iclsPtr2->nsPtr;

            while (1) {
		hPtr = Tcl_CreateHashEntry(&iclsPtr->resolveVars,
		    varName, &newEntry);
                //!!!printf("    **** find %p : %s : %s -> %p\n", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr), varName, hPtr);
		if (!reshPtr) {
		    reshPtr = hPtr;
		}

		/* check for same name in current class */
		if (!newEntry) {
		    vlookup = (ItclVarLookup*)Tcl_GetHashValue(hPtr);

		    if (vlookup->ivPtr != ivPtr && iclsPtr2 == iclsPtr) {
		    	/* if used multiple times - unbind, else - overwrite */
			if (vlookup->usage > 1) {
			    /* correct leastQualName */
			    vlookup->leastQualName = NULL;
			    processAncestors = 1; /* correction in progress */
			    /* should create new lookup */
................................................................................
			} else {
			    /* correct values (overwrite) */
			    vlookup->usage = 0;
			    goto setResVar;
			}
			newEntry = 1;
		    } else {
#if 0
		    	/* var exists and no correction necessary - next var */
			if (!processAncestors) {
			    break;
			}
#endif
			/* check leastQualName correction needed */
			if (!vlookup->leastQualName) {
			    vlookup->leastQualName = 
				Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);
			}
			/* reset vlookup for full-qualified names - new lookup */

			vlookup = NULL;
		    }
		}


		if (newEntry) {
		    if (!vlookup) {
			/* create new (or overwrite) */
			vlookup = (ItclVarLookup *)ckalloc(sizeof(ItclVarLookup));
			vlookup->usage = 0;

		    setResVar:
................................................................................
			}
		    }

		    Tcl_SetHashValue(hPtr, (ClientData)vlookup);
		    vlookup->usage++;
		}








                if (nsPtr == NULL) {
                    break;
                }
                Tcl_DStringSetLength(bufferC, 0);
                Tcl_DStringAppend(bufferC, nsPtr->name, -1);
                Tcl_DStringAppend(bufferC, "::", -1);
                Tcl_DStringAppend(bufferC, varName, -1);
................................................................................
                bufferC = (bufferC == &buffer) ? &buffer2 : &buffer;

                nsPtr = nsPtr->parentPtr;
            }

        }

        iclsPtr2 = Itcl_AdvanceHierIter(&hier);

	/* Stop create vars for ancestors (if already processed once) */
	if (iclsPtr2 != iclsPtr && !processAncestors) {
#if 0
	    if (simpleName == lookupName) {
	    	Tcl_DecrRefCount(vnObjPtr);
		/* simple name */
		return reshPtr;
	    }
	    break;
#endif
	}


    }
    Itcl_DeleteHierIter(&hier);

#if 0 /* DUMP var table */
    //!!!
    printf("======== %p : %s ===== \n", iclsPtr2, Tcl_GetString(iclsPtr->fullNamePtr));

    Itcl_InitHierIter(&hier, iclsPtr);
    iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    while (iclsPtr2 != NULL) {
    	Tcl_HashSearch place;

        //!!!
................................................................................
    Itcl_DeleteHierIter(&hier);
#endif

	Tcl_DStringFree(&buffer);
	Tcl_DStringFree(&buffer2);
	Tcl_DecrRefCount(vnObjPtr);


	reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName);

	//!!!printf("====ret======== %p : %s ===== \n", reshPtr, reshPtr && ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName ? ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName : "<NULL>");
	return reshPtr;
    }
#endif
}
 
/*
 * ------------------------------------------------------------------------






<
<










>
>
>
>
>




|
|
<

<




>
>







 







|
<
<
<




>







 







|











>
|
|
|
>
>







 







>
>
>
>
>
>
>







 







<
<
|
|
|
<
<
|
<
<



>
>





|







 







>
|
>
|







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
....
1773
1774
1775
1776
1777
1778
1779
1780



1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
....
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
....
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
....
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
....
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
    Tcl_HashEntry *reshPtr, *hPtr;

    /* could be resolved directly */
    if ((reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName)) != NULL) {
	return reshPtr;
    } else {



    /* try to build virtual table for this var */
    const char *varName, *simpleName;
    Tcl_DString buffer, buffer2, *bufferC;
    ItclHierIter hier;
    ItclClass* iclsPtr2;
    ItclVarLookup *vlookup;
    ItclVariable *ivPtr;
    Tcl_Namespace* nsPtr;
    Tcl_Obj *vnObjPtr;
    int newEntry, processAncestors;

    /* simple speedup: avoid lookup for ::itcl::internal's - not resolvable in a class */
    if (strncmp(lookupName, ITCL_NAMESPACE, sizeof(ITCL_NAMESPACE)-1) == 0) {
	return NULL;
    }
    
    /* (de)qualify to simple name */
    varName = simpleName = lookupName;
    while(*varName) {
        if (*varName++ == ':') {
            if (*varName++ == ':') { simpleName = varName; }

        };

    }
    vnObjPtr = Tcl_NewStringObj(simpleName, -1);
    
    processAncestors = simpleName != lookupName;

    //!!! printf("====get======== %p : %s -- v: %s (%s), pa: %d ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr), lookupName, simpleName, processAncestors);
    
    Tcl_DStringInit(&buffer);
    Tcl_DStringInit(&buffer2);

    /*
     *  Scan through all classes in the hierarchy, from most to
     *  least specific.  Add a lookup entry for each variable
................................................................................
            varName = simpleName;
            bufferC = &buffer;
            nsPtr = iclsPtr2->nsPtr;

            while (1) {
		hPtr = Tcl_CreateHashEntry(&iclsPtr->resolveVars,
		    varName, &newEntry);
                //!!! printf("    **** %s %p : %s : %s -> %p\n", newEntry ? "find   " : "created", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr), varName, hPtr);




		/* check for same name in current class */
		if (!newEntry) {
		    vlookup = (ItclVarLookup*)Tcl_GetHashValue(hPtr);
		    //!!! printf("    **** exist %p : %p != %p / %s != %s && %d\n", vlookup, vlookup->ivPtr, ivPtr, Tcl_GetString(vlookup->ivPtr->fullNamePtr), Tcl_GetString(ivPtr->fullNamePtr), iclsPtr2 == iclsPtr);
		    if (vlookup->ivPtr != ivPtr && iclsPtr2 == iclsPtr) {
		    	/* if used multiple times - unbind, else - overwrite */
			if (vlookup->usage > 1) {
			    /* correct leastQualName */
			    vlookup->leastQualName = NULL;
			    processAncestors = 1; /* correction in progress */
			    /* should create new lookup */
................................................................................
			} else {
			    /* correct values (overwrite) */
			    vlookup->usage = 0;
			    goto setResVar;
			}
			newEntry = 1;
		    } else {
#if 1
		    	/* var exists and no correction necessary - next var */
			if (!processAncestors) {
			    break;
			}
#endif
			/* check leastQualName correction needed */
			if (!vlookup->leastQualName) {
			    vlookup->leastQualName = 
				Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);
			}
			/* reset vlookup for full-qualified names - new lookup */
			if (vlookup->ivPtr != ivPtr) {
			    vlookup = NULL;
			}
		    }
		}
		//!!! printf("    **** newEntry %d, vlookup: %p\n", newEntry, vlookup);
		if (newEntry) {
		    if (!vlookup) {
			/* create new (or overwrite) */
			vlookup = (ItclVarLookup *)ckalloc(sizeof(ItclVarLookup));
			vlookup->usage = 0;

		    setResVar:
................................................................................
			}
		    }

		    Tcl_SetHashValue(hPtr, (ClientData)vlookup);
		    vlookup->usage++;
		}

                /* if we have found it */
                if (simpleName == lookupName || strcmp(varName, lookupName) == 0) {
		    if (!reshPtr) {
			reshPtr = hPtr;
		    }
		    break;
                }
                if (nsPtr == NULL) {
                    break;
                }
                Tcl_DStringSetLength(bufferC, 0);
                Tcl_DStringAppend(bufferC, nsPtr->name, -1);
                Tcl_DStringAppend(bufferC, "::", -1);
                Tcl_DStringAppend(bufferC, varName, -1);
................................................................................
                bufferC = (bufferC == &buffer) ? &buffer2 : &buffer;

                nsPtr = nsPtr->parentPtr;
            }

        }



	/* Stop create vars for ancestors (if not needed) */
	if (!processAncestors && reshPtr) {
#if 1


	    /* simple name - don't need to check ancestors */


	    break;
#endif
	}

        iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    }
    Itcl_DeleteHierIter(&hier);

#if 0 /* DUMP var table */
    //!!!
    printf("===DUMP===== %p : %s ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr));

    Itcl_InitHierIter(&hier, iclsPtr);
    iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    while (iclsPtr2 != NULL) {
    	Tcl_HashSearch place;

        //!!!
................................................................................
    Itcl_DeleteHierIter(&hier);
#endif

	Tcl_DStringFree(&buffer);
	Tcl_DStringFree(&buffer2);
	Tcl_DecrRefCount(vnObjPtr);

	if (reshPtr == NULL) {
	    reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName);
	}
	//!!! printf("====ret======== %p : %s ===== \n", reshPtr, reshPtr && ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName ? ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName : "<NULL>");
	return reshPtr;
    }
#endif
}
 
/*
 * ------------------------------------------------------------------------