--- a/generic/tclAnnotObj.c
+++ b/generic/tclAnnotObj.c
@@ -171,6 +171,7 @@
* -------------------------------------------------------------------- */
static Tcl_FreeInternalRepProc FreeAnnotated;
+static void FreeAnnotatedReps(Tcl_Obj *objPtr);
static Tcl_DupInternalRepProc DupAnnotated;
static Tcl_UpdateStringProc UpdateStringOfAnnotated;
static Tcl_SetFromAnyProc SetAnnotatedFromAny;
@@ -219,9 +220,71 @@
const Tcl_ObjType *const TclAnnotatedObjTypePtr = &annotatedObjType;
+/*
+ * Un scalaire annoté (string, int, un élément atomique...) est une liste
+ * d'UN élément, qui est lui-même : foreach f $e rend e, annotation
+ * comprise, et l'atomicité se conserve à tous les niveaux d'imbrication.
+ * getElementsProc doit rendre un tableau qui vive autant que l'objet : une
+ * case (Tcl_Obj *) par objet, dans une table propre au thread, libérée
+ * avec l'objet.
+ */
+
+typedef struct {
+ int init;
+ Tcl_HashTable cells; /* Tcl_Obj* -> Tcl_Obj ** (1 case) */
+} SelfCells;
+
+static Tcl_ThreadDataKey selfCellsKey;
+
+static SelfCells *
+GetSelfCells(void)
+{
+ SelfCells *sc = (SelfCells *) Tcl_GetThreadData(&selfCellsKey,
+ sizeof(SelfCells));
+
+ if (!sc->init) {
+ Tcl_InitHashTable(&sc->cells, TCL_ONE_WORD_KEYS);
+ sc->init = 1;
+ }
+ return sc;
+}
+
+static Tcl_Obj **
+SelfCell(Tcl_Obj *aPtr)
+{
+ SelfCells *sc = GetSelfCells();
+ int isNew;
+ Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(&sc->cells, aPtr, &isNew);
+ Tcl_Obj **cell;
+
+ if (isNew) {
+ cell = (Tcl_Obj **) Tcl_Alloc(sizeof(Tcl_Obj *));
+ *cell = aPtr; /* pas de référence : aPtr survit */
+ Tcl_SetHashValue(hPtr, cell);
+ }
+ return (Tcl_Obj **) Tcl_GetHashValue(hPtr);
+}
+
static void
FreeAnnotated(Tcl_Obj *objPtr)
{
+ SelfCells *sc = (SelfCells *) Tcl_GetThreadData(&selfCellsKey,
+ sizeof(SelfCells));
+
+ if (sc->init && sc->cells.numEntries > 0) {
+ Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&sc->cells, objPtr);
+
+ if (hPtr != NULL) {
+ Tcl_Free(Tcl_GetHashValue(hPtr));
+ Tcl_DeleteHashEntry(hPtr);
+ }
+ }
+ FreeAnnotatedReps(objPtr);
+}
+
+static void
+FreeAnnotatedReps(Tcl_Obj *objPtr)
+{
Tcl_DecrRefCount((Tcl_Obj *) objPtr->internalRep.twoPtrValue.ptr1);
Tcl_DecrRefCount((Tcl_Obj *) objPtr->internalRep.twoPtrValue.ptr2);
}
@@ -423,7 +486,7 @@
AnnotWrapElems(aPtr, v, index, index);
return Tcl_ListObjIndex(interp, v, index, elemPtr);
}
- *elemPtr = (index == 0) ? v : NULL;
+ *elemPtr = (index == 0) ? aPtr : NULL; /* le scalaire est son élément */
return TCL_OK;
}
@@ -437,10 +500,9 @@
AnnotWrapElems(aPtr, v, 0, TCL_SIZE_MAX);
return Tcl_ListObjGetElements(interp, v, objcPtr, objvPtr);
}
- /* Un seul élément : ptr1 de la représentation est justement un Tcl_Obj*
- * qui vit autant que A. */
+ /* Un seul élément : A lui-même (annotation comprise). */
*objcPtr = 1;
- *objvPtr = (Tcl_Obj **) &aPtr->internalRep.twoPtrValue.ptr1;
+ *objvPtr = SelfCell(aPtr);
return TCL_OK;
}