From 1178b99000dfc1c8c537abe0cd1dfeb6b640f595 Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 27 Jan 2024 12:24:42 +0000 Subject: Work in progress: Implementing properties with more C for greater speed --- generic/tclOOBasic.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++ generic/tclOOInfo.c | 13 ++--- generic/tclOOInt.h | 1 + 3 files changed, 161 insertions(+), 7 deletions(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 5a38dee..251ae34 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1341,6 +1341,160 @@ TclOOCopyObjectCmd( return TCL_OK; } +static int +ReadProp( + Tcl_Interp *interp, + Object *oPtr, + Tcl_Obj *propObj) +{ + Tcl_Obj *args[] = { + Tcl_NewStringObj("my", 2), + Tcl_ObjPrintf("", TclGetString(propObj)) + }; + int code; + + Tcl_IncrRefCount(args[0]); + Tcl_IncrRefCount(args[1]); + code = TclOOPrivateObjectCmd(oPtr, interp, 2, args); + Tcl_DecrRefCount(args[0]); + Tcl_DecrRefCount(args[1]); + return code; +} + +static int +WriteProp( + Tcl_Interp *interp, + Object *oPtr, + Tcl_Obj *propObj, + Tcl_Obj *valueObj) +{ + Tcl_Obj *args[] = { + Tcl_NewStringObj("my", 2), + Tcl_ObjPrintf("", TclGetString(propObj)), + valueObj + }; + int code; + + Tcl_IncrRefCount(args[0]); + Tcl_IncrRefCount(args[1]); + Tcl_IncrRefCount(args[2]); + code = TclOOPrivateObjectCmd(oPtr, interp, 3, args); + Tcl_DecrRefCount(args[0]); + Tcl_DecrRefCount(args[1]); + Tcl_DecrRefCount(args[2]); + return code; +} + +/* Look up a property full name. */ +static Tcl_Obj * +GetPropertyName( + Tcl_Interp *interp, /* Context and error reporting. */ + Object *oPtr, /* Object to get property name from. */ + int writable, /* Are we looking for a writable property? */ + Tcl_Obj *namePtr) /* The name supplied by the user. */ +{ + int allocated; + Tcl_Size objc, index, i; + Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, writable, &allocated); + Tcl_Obj **objv; + if (allocated) { + TclOOSortPropList(listPtr); + } + ListObjGetElements(listPtr, objc, objv); + char **tablePtr = TclStackAlloc(interp, sizeof(char*) * objc); + for (int i = 0; i < objc; i++) { + tablePtr[i] = TclGetString(objv[i]); + } + int result = Tcl_GetIndexFromObjStruct(interp, namePtr, tablePtr, + sizeof(char *), "property", TCL_INDEX_TEMP_TABLE, &index); + TclStackFree(interp, tablePtr); + if (result != TCL_OK) { + return NULL; + } + return objv[index]; +} + +int +TclOO_Configurable_Configure( + TCL_UNUSED(void *), + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + Tcl_Size objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Object *oPtr = (Object *) Tcl_ObjectContextObject(context); + Tcl_Size skip = Tcl_ObjectContextSkippedArgs(context); + Tcl_Size numArgs = objc - skip; + Tcl_Obj *namePtr; + Tcl_Size i; + int code; + + if (numArgs == 0) { + /* + * Read all properties. + */ + + Tcl_Size namec; + int allocated = 0; + Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, 0, &allocated); + Tcl_Obj *resultPtr = Tcl_NewObj(), **namev; + + if (allocated) { + TclOOSortPropList(listPtr); + } + ListObjGetElements(listPtr, namec, namev); + + for (i = 0; i < namec; ) { + code = ReadProp(interp, oPtr, namev[i]); + if (code != TCL_OK) { + Tcl_DecrRefCount(resultPtr); + return code; + } + Tcl_DictObjPut(NULL, resultPtr, namev[i], Tcl_GetObjResult(interp)); + if (++i >= namec) { + Tcl_SetObjResult(interp, resultPtr); + break; + } + Tcl_SetObjResult(interp, Tcl_NewObj()); + } + } else if (numArgs == 1) { + /* + * Read a single named property. + */ + + namePtr = GetPropertyName(interp, oPtr, 0, objv[skip]); + if (namePtr == NULL) { + return TCL_ERROR; + } + return ReadProp(interp, oPtr, namePtr); + } else if (numArgs % 2) { + /* + * Bad (odd > 1) number of arguments. + */ + + Tcl_WrongNumArgs(interp, skip, objv, "?-option value ...?"); + return TCL_ERROR; + } else { + /* + * Write properties. + */ + + objv += skip; + for (i = 0; i < numArgs; i += 2) { + namePtr = GetPropertyName(interp, oPtr, 1, objv[i]); + if (namePtr == NULL) { + return TCL_ERROR; + } + code = WriteProp(interp, oPtr, namePtr, objv[i + 1]); + if (code != TCL_OK) { + return code; + } + } + } + return TCL_OK; +} + /* * Local Variables: * mode: c diff --git a/generic/tclOOInfo.c b/generic/tclOOInfo.c index eba658b..bc04748 100644 --- a/generic/tclOOInfo.c +++ b/generic/tclOOInfo.c @@ -17,7 +17,6 @@ #include "tclOOInt.h" static inline Class * GetClassFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr); -static void SortPropList(Tcl_Obj *list); static Tcl_ObjCmdProc InfoObjectCallCmd; static Tcl_ObjCmdProc InfoObjectClassCmd; static Tcl_ObjCmdProc InfoObjectDefnCmd; @@ -1784,7 +1783,7 @@ InfoClassPropCmd( if (all) { result = TclOOGetAllClassProperties(clsPtr, writable, &allocated); if (allocated) { - SortPropList(result); + TclOOSortPropList(result); } } else { TclNewObj(result); @@ -1797,7 +1796,7 @@ InfoClassPropCmd( Tcl_ListObjAppendElement(NULL, result, propObj); } } - SortPropList(result); + TclOOSortPropList(result); } Tcl_SetObjResult(interp, result); return TCL_OK; @@ -1847,7 +1846,7 @@ InfoObjectPropCmd( if (all) { result = TclOOGetAllObjectProperties(oPtr, writable, &allocated); if (allocated) { - SortPropList(result); + TclOOSortPropList(result); } } else { TclNewObj(result); @@ -1860,7 +1859,7 @@ InfoObjectPropCmd( Tcl_ListObjAppendElement(NULL, result, propObj); } } - SortPropList(result); + TclOOSortPropList(result); } Tcl_SetObjResult(interp, result); return TCL_OK; @@ -1888,8 +1887,8 @@ PropNameCompare( return strcmp(Tcl_GetString(first), Tcl_GetString(second)); } -static void -SortPropList( +void +TclOOSortPropList( Tcl_Obj *list) { Tcl_Size ec; diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 031b910..d9f4ed8 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -589,6 +589,7 @@ MODULE_SCOPE int TclOORemoveFromSubclasses(Class *subPtr, Class *superPtr); MODULE_SCOPE Tcl_Obj * TclOORenderCallChain(Tcl_Interp *interp, CallChain *callPtr); +MODULE_SCOPE void TclOOSortPropList(Tcl_Obj *listPtr); MODULE_SCOPE void TclOOStashContext(Tcl_Obj *objPtr, CallContext *contextPtr); MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); -- cgit v0.12 From 6469f49c68c8f3006fe38c70f1cc16621207873e Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 27 Jan 2024 22:05:59 +0000 Subject: Now passing tests --- generic/tcl.h | 121 +++++++++++----------- generic/tclCompile.h | 100 ++++++++++--------- generic/tclEnsemble.c | 3 +- generic/tclIO.h | 8 +- generic/tclIORChan.c | 21 ++-- generic/tclIOUtil.c | 24 ++--- generic/tclInt.h | 250 ++++++++++++++++++++++++++-------------------- generic/tclLoad.c | 5 +- generic/tclNamesp.c | 3 +- generic/tclOO.c | 45 ++++++--- generic/tclOOBasic.c | 226 ++++++++++++++++++++++++++++++++--------- generic/tclOOCall.c | 25 ++--- generic/tclOODefineCmds.c | 8 +- generic/tclOOInfo.c | 7 +- generic/tclOOInt.h | 27 ++--- generic/tclOOMethod.c | 18 ++-- generic/tclOOScript.h | 103 +------------------ generic/tclProcess.c | 6 +- tools/tclOOScript.tcl | 145 +-------------------------- win/Makefile.in | 10 ++ win/tclWinConsole.c | 5 +- win/tclWinSock.c | 5 +- 22 files changed, 550 insertions(+), 615 deletions(-) diff --git a/generic/tcl.h b/generic/tcl.h index 0f53228..2db88b1 100644 --- a/generic/tcl.h +++ b/generic/tcl.h @@ -453,7 +453,7 @@ typedef void (Tcl_ThreadCreateProc) (void *clientData); * Flags values passed to Tcl_RegExpExecObj. */ -#define TCL_REG_NOTBOL 0001 /* Beginning of string does not match ^. */ +#define TCL_REG_NOTBOL 0001 /* Beginning of string does not match ^. */ #define TCL_REG_NOTEOL 0002 /* End of string does not match $. */ /* @@ -464,9 +464,9 @@ typedef void (Tcl_ThreadCreateProc) (void *clientData); typedef struct Tcl_RegExpIndices { #if TCL_MAJOR_VERSION > 8 - Tcl_Size start; /* Character offset of first character in + Tcl_Size start; /* Character offset of first character in * match. */ - Tcl_Size end; /* Character offset of first character after + Tcl_Size end; /* Character offset of first character after * the match. */ #else long start; @@ -475,11 +475,11 @@ typedef struct Tcl_RegExpIndices { } Tcl_RegExpIndices; typedef struct Tcl_RegExpInfo { - Tcl_Size nsubs; /* Number of subexpressions in the compiled + Tcl_Size nsubs; /* Number of subexpressions in the compiled * expression. */ Tcl_RegExpIndices *matches; /* Array of nsubs match offset pairs. */ #if TCL_MAJOR_VERSION > 8 - Tcl_Size extendStart; /* The offset at which a subsequent match + Tcl_Size extendStart; /* The offset at which a subsequent match * might begin. */ #else long extendStart; @@ -617,26 +617,25 @@ typedef void (Tcl_FinalizeNotifierProc) (void *clientData); typedef void (Tcl_MainLoopProc) (void); /* Abstract List functions */ -typedef Tcl_Size (Tcl_ObjTypeLengthProc) (struct Tcl_Obj *listPtr); -typedef int (Tcl_ObjTypeIndexProc) (Tcl_Interp *interp, struct Tcl_Obj *listPtr, - Tcl_Size index, struct Tcl_Obj** elemObj); -typedef int (Tcl_ObjTypeSliceProc) (Tcl_Interp *interp, struct Tcl_Obj *listPtr, - Tcl_Size fromIdx, Tcl_Size toIdx, - struct Tcl_Obj **newObjPtr); -typedef int (Tcl_ObjTypeReverseProc) (Tcl_Interp *interp, struct Tcl_Obj *listPtr, - struct Tcl_Obj **newObjPtr); -typedef int (Tcl_ObjTypeGetElements) (Tcl_Interp *interp, struct Tcl_Obj *listPtr, - Tcl_Size *objcptr, struct Tcl_Obj ***objvptr); -typedef struct Tcl_Obj* (Tcl_ObjTypeSetElement) (Tcl_Interp *interp, struct Tcl_Obj *listPtr, - Tcl_Size indexCount, - struct Tcl_Obj *const indexArray[], - struct Tcl_Obj *valueObj); -typedef int (Tcl_ObjTypeReplaceProc) (Tcl_Interp *interp, struct Tcl_Obj *listObj, - Tcl_Size first, Tcl_Size numToDelete, - Tcl_Size numToInsert, - struct Tcl_Obj *const insertObjs[]); -typedef int (Tcl_ObjTypeInOperatorProc) (Tcl_Interp *interp, struct Tcl_Obj *valueObj, - struct Tcl_Obj *listObj, int *boolResult); +typedef Tcl_Size (Tcl_ObjTypeLengthProc) (struct Tcl_Obj *listPtr); +typedef int (Tcl_ObjTypeIndexProc) (Tcl_Interp *interp, + struct Tcl_Obj *listPtr, Tcl_Size index, struct Tcl_Obj** elemObj); +typedef int (Tcl_ObjTypeSliceProc) (Tcl_Interp *interp, + struct Tcl_Obj *listPtr, Tcl_Size fromIdx, Tcl_Size toIdx, + struct Tcl_Obj **newObjPtr); +typedef int (Tcl_ObjTypeReverseProc) (Tcl_Interp *interp, + struct Tcl_Obj *listPtr, struct Tcl_Obj **newObjPtr); +typedef int (Tcl_ObjTypeGetElements) (Tcl_Interp *interp, + struct Tcl_Obj *listPtr, Tcl_Size *objcptr, + struct Tcl_Obj ***objvptr); +typedef struct Tcl_Obj* (Tcl_ObjTypeSetElement) (Tcl_Interp *interp, + struct Tcl_Obj *listPtr, Tcl_Size indexCount, + struct Tcl_Obj *const indexArray[], struct Tcl_Obj *valueObj); +typedef int (Tcl_ObjTypeReplaceProc) (Tcl_Interp *interp, + struct Tcl_Obj *listObj, Tcl_Size first, Tcl_Size numToDelete, + Tcl_Size numToInsert, struct Tcl_Obj *const insertObjs[]); +typedef int (Tcl_ObjTypeInOperatorProc) (Tcl_Interp *interp, + struct Tcl_Obj *valueObj, struct Tcl_Obj *listObj, int *boolResult); #ifndef TCL_NO_DEPRECATED # define Tcl_PackageInitProc Tcl_LibraryInitProc @@ -667,26 +666,30 @@ typedef struct Tcl_ObjType { * to this type. Frees the internal rep of the * old type. Returns TCL_ERROR on failure. */ #if TCL_MAJOR_VERSION > 8 - size_t version; + size_t version; /* Version field for future-proofing. */ /* List emulation functions - ObjType Version 1 */ - Tcl_ObjTypeLengthProc *lengthProc; /* Return the [llength] of the - ** AbstractList */ - Tcl_ObjTypeIndexProc *indexProc; /* Return a value (Tcl_Obj) for - ** [lindex $al $index] */ - Tcl_ObjTypeSliceProc *sliceProc; /* Return an AbstractList for - ** [lrange $al $start $end] */ - Tcl_ObjTypeReverseProc *reverseProc; /* Return an AbstractList for - ** [lreverse $al] */ - Tcl_ObjTypeGetElements *getElementsProc; /* Return an objv[] of all elements in - ** the list */ - Tcl_ObjTypeSetElement *setElementProc; /* Replace the element at the indicie - ** with the given valueObj. */ - Tcl_ObjTypeReplaceProc *replaceProc; /* Replace subset with subset */ - Tcl_ObjTypeInOperatorProc *inOperProc; /* "in" and "ni" expr list - ** operation Determine if the given - ** string value matches an element in - ** the list */ + Tcl_ObjTypeLengthProc *lengthProc; + /* Return the [llength] of the AbstractList */ + Tcl_ObjTypeIndexProc *indexProc; + /* Return a value (Tcl_Obj) for + * [lindex $al $index] */ + Tcl_ObjTypeSliceProc *sliceProc; + /* Return an AbstractList for + * [lrange $al $start $end] */ + Tcl_ObjTypeReverseProc *reverseProc; + /* Return an AbstractList for [lreverse $al] */ + Tcl_ObjTypeGetElements *getElementsProc; + /* Return an objv[] of all elements in the list */ + Tcl_ObjTypeSetElement *setElementProc; + /* Replace the element at the indicies with the + * given valueObj. */ + Tcl_ObjTypeReplaceProc *replaceProc; + /* Replace sublist with sublist. */ + Tcl_ObjTypeInOperatorProc *inOperProc; + /* "in" and "ni" expr list operation. Determine + * if the given string value matches an element + * in the list. */ #endif } Tcl_ObjType; @@ -749,7 +752,8 @@ typedef struct Tcl_Obj { * corresponds to the type of the object's * internal rep. NULL indicates the object has * no internal rep (has no type). */ - Tcl_ObjInternalRep internalRep; /* The internal representation: */ + Tcl_ObjInternalRep internalRep; + /* The internal representation: */ } Tcl_Obj; @@ -841,11 +845,11 @@ typedef struct { Tcl_ObjCmdProc *objProc; /* Command's object-based function. */ void *objClientData; /* ClientData for object proc. */ Tcl_CmdProc *proc; /* Command's string-based function. */ - void *clientData; /* ClientData for string proc. */ + void *clientData; /* ClientData for string proc. */ Tcl_CmdDeleteProc *deleteProc; /* Function to call when command is * deleted. */ - void *deleteData; /* Value to pass to deleteProc (usually the + void *deleteData; /* Value to pass to deleteProc (usually the * same as clientData). */ Tcl_Namespace *namespacePtr;/* Points to the namespace that contains this * command. Note that Tcl_SetCmdInfo will not @@ -1077,7 +1081,7 @@ struct Tcl_HashEntry { * or NULL for end of chain. */ Tcl_HashTable *tablePtr; /* Pointer to table containing entry. */ size_t hash; /* Hash value. */ - void *clientData; /* Application stores something here with + void *clientData; /* Application stores something here with * Tcl_SetHashValue. */ union { /* Key has one of these forms: */ char *oneWordValue; /* One-word value for key. */ @@ -1165,11 +1169,11 @@ struct Tcl_HashTable { Tcl_HashEntry *staticBuckets[TCL_SMALL_HASH_TABLE]; /* Bucket array used for small tables (to * avoid mallocs and frees). */ - Tcl_Size numBuckets; /* Total number of buckets allocated at + Tcl_Size numBuckets; /* Total number of buckets allocated at * **bucketPtr. */ - Tcl_Size numEntries; /* Total number of entries present in + Tcl_Size numEntries; /* Total number of entries present in * table. */ - Tcl_Size rebuildSize; /* Enlarge table when numEntries gets to be + Tcl_Size rebuildSize; /* Enlarge table when numEntries gets to be * this large. */ #if TCL_MAJOR_VERSION > 8 size_t mask; /* Mask value used in hashing function. */ @@ -1675,7 +1679,7 @@ typedef struct Tcl_Filesystem { * 'file attributes'. */ Tcl_FSFileAttrsSetProc *fileAttrsSetProc; /* Called by 'Tcl_FSFileAttrsSet()' and by - * 'file attributes'. */ + * 'file attributes'. */ Tcl_FSCreateDirectoryProc *createDirectoryProc; /* Called by 'Tcl_FSCreateDirectory()'. May be * NULL if the filesystem is read-only. */ @@ -1768,8 +1772,8 @@ typedef struct Tcl_Token { int type; /* Type of token, such as TCL_TOKEN_WORD; see * below for valid types. */ const char *start; /* First character in token. */ - Tcl_Size size; /* Number of bytes in token. */ - Tcl_Size numComponents; /* If this token is composed of other tokens, + Tcl_Size size; /* Number of bytes in token. */ + Tcl_Size numComponents; /* If this token is composed of other tokens, * this field tells how many of them there are * (including components of components, etc.). * The component tokens immediately follow @@ -1883,13 +1887,13 @@ typedef struct Tcl_Token { typedef struct Tcl_Parse { const char *commentStart; /* Pointer to # that begins the first of one * or more comments preceding the command. */ - Tcl_Size commentSize; /* Number of bytes in comments (up through + Tcl_Size commentSize; /* Number of bytes in comments (up through * newline character that terminates the last * comment). If there were no comments, this * field is 0. */ const char *commandStart; /* First character in first word of * command. */ - Tcl_Size commandSize; /* Number of bytes in command, including first + Tcl_Size commandSize; /* Number of bytes in command, including first * character of first word, up through the * terminating newline, close bracket, or * semicolon. */ @@ -1956,10 +1960,9 @@ typedef struct Tcl_EncodingType { Tcl_EncodingConvertProc *fromUtfProc; /* Function to convert from UTF-8 into * external encoding. */ - Tcl_FreeProc *freeProc; - /* If non-NULL, function to call when this + Tcl_FreeProc *freeProc; /* If non-NULL, function to call when this * encoding is deleted. */ - void *clientData; /* Arbitrary value associated with encoding + void *clientData; /* Arbitrary value associated with encoding * type. Passed to conversion functions. */ Tcl_Size nullSize; /* Number of zero bytes that signify * end-of-string in this encoding. This number @@ -2165,7 +2168,7 @@ typedef struct { * depends on type.*/ const char *helpStr; /* Documentation message describing this * option. */ - void *clientData; /* Word to pass to function callbacks. */ + void *clientData; /* Word to pass to function callbacks. */ } Tcl_ArgvInfo; /* diff --git a/generic/tclCompile.h b/generic/tclCompile.h index 2ea2565..48196e3 100644 --- a/generic/tclCompile.h +++ b/generic/tclCompile.h @@ -89,20 +89,20 @@ typedef enum { typedef struct { ExceptionRangeType type; /* The kind of ExceptionRange. */ - Tcl_Size nestingLevel; /* Static depth of the exception range. Used + Tcl_Size nestingLevel; /* Static depth of the exception range. Used * to find the most deeply-nested range * surrounding a PC at runtime. */ - Tcl_Size codeOffset; /* Offset of the first instruction byte of the + Tcl_Size codeOffset; /* Offset of the first instruction byte of the * code range. */ - Tcl_Size numCodeBytes; /* Number of bytes in the code range. */ - Tcl_Size breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC + Tcl_Size numCodeBytes; /* Number of bytes in the code range. */ + Tcl_Size breakOffset; /* If LOOP_EXCEPTION_RANGE, the target PC * offset for a break command in the range. */ - Tcl_Size continueOffset; /* If LOOP_EXCEPTION_RANGE and not TCL_INDEX_NONE, the + Tcl_Size continueOffset; /* If LOOP_EXCEPTION_RANGE and not TCL_INDEX_NONE, the * target PC offset for a continue command in * the code range. Otherwise, ignore this * range when processing a continue * command. */ - Tcl_Size catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC + Tcl_Size catchOffset; /* If a CATCH_EXCEPTION_RANGE, the target PC * offset for any "exception" in range. */ } ExceptionRange; @@ -118,11 +118,11 @@ typedef struct ExceptionAux { * one (see [for] next-clause) then we must * not pick up the range when scanning for a * target to continue to. */ - Tcl_Size stackDepth; /* The stack depth at the point where the + Tcl_Size stackDepth; /* The stack depth at the point where the * exception range was created. This is used * to calculate the number of POPs required to * restore the stack to its prior state. */ - Tcl_Size expandTarget; /* The number of expansions expected on the + Tcl_Size expandTarget; /* The number of expansions expected on the * auxData stack at the time the loop starts; * we can't currently discard them except by * doing INST_INVOKE_EXPANDED; this is a known @@ -135,14 +135,14 @@ typedef struct ExceptionAux { Tcl_Size numBreakTargets; /* The number of [break]s that want to be * targeted to the place where this loop * exception will be bound to. */ - TCL_HASH_TYPE *breakTargets; /* The offsets of the INST_JUMP4 instructions + TCL_HASH_TYPE *breakTargets;/* The offsets of the INST_JUMP4 instructions * issued by the [break]s that we must * update. Note that resizing a jump (via * TclFixupForwardJump) can cause the contents * of this array to be updated. When * numBreakTargets==0, this is NULL. */ Tcl_Size allocBreakTargets; /* The size of the breakTargets array. */ - Tcl_Size numContinueTargets; /* The number of [continue]s that want to be + Tcl_Size numContinueTargets;/* The number of [continue]s that want to be * targeted to the place where this loop * exception will be bound to. */ TCL_HASH_TYPE *continueTargets; /* The offsets of the INST_JUMP4 instructions @@ -151,7 +151,8 @@ typedef struct ExceptionAux { * TclFixupForwardJump) can cause the contents * of this array to be updated. When * numContinueTargets==0, this is NULL. */ - Tcl_Size allocContinueTargets; /* The size of the continueTargets array. */ + Tcl_Size allocContinueTargets; + /* The size of the continueTargets array. */ } ExceptionAux; /* @@ -163,10 +164,10 @@ typedef struct ExceptionAux { */ typedef struct { - Tcl_Size codeOffset; /* Offset of first byte of command code. */ - Tcl_Size numCodeBytes; /* Number of bytes for command's code. */ + Tcl_Size codeOffset; /* Offset of first byte of command code. */ + Tcl_Size numCodeBytes; /* Number of bytes for command's code. */ Tcl_Size srcOffset; /* Offset of first char of the command. */ - Tcl_Size numSrcBytes; /* Number of command source chars. */ + Tcl_Size numSrcBytes; /* Number of command source chars. */ } CmdLocation; /* @@ -182,10 +183,10 @@ typedef struct { typedef struct { Tcl_Size srcOffset; /* Command location to find the entry. */ - Tcl_Size nline; /* Number of words in the command */ - Tcl_Size *line; /* Line information for all words in the + Tcl_Size nline; /* Number of words in the command */ + Tcl_Size *line; /* Line information for all words in the * command. */ - Tcl_Size **next; /* Transient information used by the compiler + Tcl_Size **next; /* Transient information used by the compiler * for tracking of hidden continuation * lines. */ } ECL; @@ -198,8 +199,8 @@ typedef struct { Tcl_Obj *path; /* Path of the sourced file the command is * in. */ ECL *loc; /* Command word locations (lines). */ - Tcl_Size nloc; /* Number of allocated entries in 'loc'. */ - Tcl_Size nuloc; /* Number of used entries in 'loc'. */ + Tcl_Size nloc; /* Number of allocated entries in 'loc'. */ + Tcl_Size nuloc; /* Number of used entries in 'loc'. */ } ExtCmdLoc; /* @@ -290,21 +291,21 @@ typedef struct CompileEnv { * SetByteCodeFromAny. This pointer is not * owned by the CompileEnv and must not be * freed or changed by it. */ - Tcl_Size numSrcBytes; /* Number of bytes in source. */ + Tcl_Size numSrcBytes; /* Number of bytes in source. */ Proc *procPtr; /* If a procedure is being compiled, a pointer * to its Proc structure; otherwise NULL. Used * to compile local variables. Set from * information provided by ObjInterpProc in * tclProc.c. */ - Tcl_Size numCommands; /* Number of commands compiled. */ - Tcl_Size exceptDepth; /* Current exception range nesting level; TCL_INDEX_NONE + Tcl_Size numCommands; /* Number of commands compiled. */ + Tcl_Size exceptDepth; /* Current exception range nesting level; TCL_INDEX_NONE * if not in any range currently. */ - Tcl_Size maxExceptDepth; /* Max nesting level of exception ranges; TCL_INDEX_NONE + Tcl_Size maxExceptDepth; /* Max nesting level of exception ranges; TCL_INDEX_NONE * if no ranges have been compiled. */ - Tcl_Size maxStackDepth; /* Maximum number of stack elements needed to + Tcl_Size maxStackDepth; /* Maximum number of stack elements needed to * execute the code. Set by compilation * procedures before returning. */ - Tcl_Size currStackDepth; /* Current stack depth. */ + Tcl_Size currStackDepth; /* Current stack depth. */ LiteralTable localLitTable; /* Contains LiteralEntry's describing all Tcl * objects referenced by this compiled code. * Indexed by the string representations of @@ -333,7 +334,7 @@ typedef struct CompileEnv { * exceptArrayNext is the number of ranges and * (exceptArrayNext-1) is the index of the * current range's array entry. */ - Tcl_Size exceptArrayEnd; /* Index after the last ExceptionRange array + Tcl_Size exceptArrayEnd; /* Index after the last ExceptionRange array * entry. */ #if TCL_MAJOR_VERSION < 9 int mallocedExceptArray; @@ -379,7 +380,7 @@ typedef struct CompileEnv { /* TIP #280 */ ExtCmdLoc *extCmdMapPtr; /* Extended command location information for * 'info frame'. */ - Tcl_Size line; /* First line of the script, based on the + Tcl_Size line; /* First line of the script, based on the * invoking context, then the line of the * command currently compiled. */ int atCmdStart; /* Flag to say whether an INST_START_CMD @@ -388,11 +389,11 @@ typedef struct CompileEnv { * inefficient. If set to 2, that instruction * should not be issued at all (by the generic * part of the command compiler). */ - Tcl_Size expandCount; /* Number of INST_EXPAND_START instructions + Tcl_Size expandCount; /* Number of INST_EXPAND_START instructions * encountered that have not yet been paired * with a corresponding * INST_INVOKE_EXPANDED. */ - Tcl_Size *clNext; /* If not NULL, it refers to the next slot in + Tcl_Size *clNext; /* If not NULL, it refers to the next slot in * clLoc to check for an invisible * continuation line. */ } CompileEnv; @@ -427,7 +428,7 @@ typedef struct ByteCode { * procs are specific to an interpreter so the * code emitted will depend on the * interpreter. */ - Tcl_Size compileEpoch; /* Value of iPtr->compileEpoch when this + Tcl_Size compileEpoch; /* Value of iPtr->compileEpoch when this * ByteCode was compiled. Used to invalidate * code when, e.g., commands with compile * procs are redefined. */ @@ -459,17 +460,17 @@ typedef struct ByteCode { * itself. Does not include heap space for * literal Tcl objects or storage referenced * by AuxData entries. */ - Tcl_Size numCommands; /* Number of commands compiled. */ - Tcl_Size numSrcBytes; /* Number of source bytes compiled. */ - Tcl_Size numCodeBytes; /* Number of code bytes. */ - Tcl_Size numLitObjects; /* Number of objects in literal array. */ + Tcl_Size numCommands; /* Number of commands compiled. */ + Tcl_Size numSrcBytes; /* Number of source bytes compiled. */ + Tcl_Size numCodeBytes; /* Number of code bytes. */ + Tcl_Size numLitObjects; /* Number of objects in literal array. */ Tcl_Size numExceptRanges; /* Number of ExceptionRange array elems. */ Tcl_Size numAuxDataItems; /* Number of AuxData items. */ - Tcl_Size numCmdLocBytes; /* Number of bytes needed for encoded command + Tcl_Size numCmdLocBytes; /* Number of bytes needed for encoded command * location information. */ - Tcl_Size maxExceptDepth; /* Maximum nesting level of ExceptionRanges; + Tcl_Size maxExceptDepth; /* Maximum nesting level of ExceptionRanges; * TCL_INDEX_NONE if no ranges were compiled. */ - Tcl_Size maxStackDepth; /* Maximum number of stack elements needed to + Tcl_Size maxStackDepth; /* Maximum number of stack elements needed to * execute the code. */ unsigned char *codeStart; /* Points to the first byte of the code. This * is just after the final ByteCode member @@ -829,11 +830,11 @@ enum TclInstruction { INST_DICT_GET_DEF, - /* TIP 461 */ - INST_STR_LT, - INST_STR_GT, - INST_STR_LE, - INST_STR_GE, + /* TIP 461 */ + INST_STR_LT, + INST_STR_GT, + INST_STR_LE, + INST_STR_GE, INST_LREPLACE4, @@ -969,8 +970,8 @@ typedef struct JumpFixup { typedef struct JumpFixupArray { JumpFixup *fixup; /* Points to start of jump fixup array. */ - Tcl_Size next; /* Index of next free array entry. */ - Tcl_Size end; /* Index of last usable entry in array. */ + Tcl_Size next; /* Index of next free array entry. */ + Tcl_Size end; /* Index of last usable entry in array. */ int mallocedArray; /* 1 if array was expanded and fixups points * into the heap, else 0. */ JumpFixup staticFixupSpace[JUMPFIXUP_INIT_ENTRIES]; @@ -1004,9 +1005,9 @@ typedef struct ForeachVarList { typedef struct ForeachInfo { Tcl_Size numLists; /* The number of both the variable and value * lists of the foreach command. */ - Tcl_Size firstValueTemp; /* Index of the first temp var in a proc frame + Tcl_Size firstValueTemp; /* Index of the first temp var in a proc frame * used to point to a value list. */ - Tcl_Size loopCtTemp; /* Index of temp var in a proc frame holding + Tcl_Size loopCtTemp; /* Index of temp var in a proc frame holding * the loop's iteration count. Used to * determine next value list element to assign * each loop var. */ @@ -1041,7 +1042,8 @@ MODULE_SCOPE const AuxDataType tclJumptableInfoType; typedef struct { Tcl_Size length; /* Size of array */ - Tcl_Size varIndices[TCLFLEXARRAY]; /* Array of variable indices to manage when + Tcl_Size varIndices[TCLFLEXARRAY]; + /* Array of variable indices to manage when * processing the start and end of a [dict * update]. There is really more than one * entry, and the structure is allocated to @@ -1280,7 +1282,7 @@ MODULE_SCOPE int TclPushProcCallFrame(void *clientData, if (_delta == INT_MIN) { \ _delta = 1 - (i); \ } \ - TclAdjustStackDepth(_delta, envPtr); \ + TclAdjustStackDepth(_delta, envPtr); \ } \ } while (0) @@ -1394,7 +1396,7 @@ MODULE_SCOPE int TclPushProcCallFrame(void *clientData, #define TclEmitPush(objIndex, envPtr) \ do { \ - int _objIndexCopy = (objIndex); \ + int _objIndexCopy = (objIndex); \ if (_objIndexCopy <= 255) { \ TclEmitInstInt1(INST_PUSH1, _objIndexCopy, (envPtr)); \ } else { \ diff --git a/generic/tclEnsemble.c b/generic/tclEnsemble.c index 8614171..f3a814c 100644 --- a/generic/tclEnsemble.c +++ b/generic/tclEnsemble.c @@ -1818,8 +1818,7 @@ NsEnsembleImplementationCmdNR( */ const char *subcmdName; /* Name of the subcommand or unique prefix of - * it (a non-unique prefix produces an error). - */ + * it (a non-unique prefix produces an error). */ char *fullName = NULL; /* Full name of the subcommand. */ Tcl_Size stringLength, i; Tcl_Size tableLength = ensemblePtr->subcommandTable.numEntries; diff --git a/generic/tclIO.h b/generic/tclIO.h index 08fff44..b8abcc6 100644 --- a/generic/tclIO.h +++ b/generic/tclIO.h @@ -39,12 +39,12 @@ typedef struct ChannelBuffer { Tcl_Size refCount; /* Current uses count */ Tcl_Size nextAdded; /* The next position into which a character * will be put in the buffer. */ - Tcl_Size nextRemoved; /* Position of next byte to be removed from + Tcl_Size nextRemoved; /* Position of next byte to be removed from * the buffer. */ Tcl_Size bufLength; /* How big is the buffer? */ struct ChannelBuffer *nextPtr; /* Next buffer in chain. */ - char buf[TCLFLEXARRAY]; /* Placeholder for real buffer. The real + char buf[TCLFLEXARRAY]; /* Placeholder for real buffer. The real * buffer occupies this space + bufSize-1 * bytes. This must be the last field in the * structure. */ @@ -96,7 +96,7 @@ typedef struct EventScriptRecord { typedef struct Channel { struct ChannelState *state; /* Split out state information */ - void *instanceData; /* Instance-specific data provided by creator + void *instanceData; /* Instance-specific data provided by creator * of channel. */ const Tcl_ChannelType *typePtr; /* Pointer to channel type structure. */ struct Channel *downChanPtr;/* Refers to channel this one was stacked @@ -215,7 +215,7 @@ typedef struct ChannelState { */ Tcl_Obj* chanMsg; - Tcl_Obj* unreportedMsg; /* Non-NULL if an error report was deferred + Tcl_Obj* unreportedMsg; /* Non-NULL if an error report was deferred * because it happened in the background. The * value is the chanMg, if any. #219's * companion to 'unreportedError'. */ diff --git a/generic/tclIORChan.c b/generic/tclIORChan.c index e8ce5f1..8d156d2 100644 --- a/generic/tclIORChan.c +++ b/generic/tclIORChan.c @@ -96,8 +96,7 @@ typedef struct { * Tcl level part of the channel. NULL here * signals the channel is dead because the * interpreter/thread containing its Tcl - * command is gone. - */ + * command is gone. */ #if TCL_THREADS Tcl_ThreadId thread; /* Thread the 'interp' belongs to. == Handler thread */ Tcl_ThreadId owner; /* Thread owning the structure. == Channel thread */ @@ -113,16 +112,12 @@ typedef struct { int dead; /* Boolean signal that some operations * should no longer be attempted. */ - Tcl_TimerToken readTimer; /* - A token for the timer that is scheduled in - order to call Tcl_NotifyChannel when the - channel is readable - */ - Tcl_TimerToken writeTimer; /* - A token for the timer that is scheduled in - order to call Tcl_NotifyChannel when the - channel is writable - */ + Tcl_TimerToken readTimer; /* A token for the timer that is scheduled in + * order to call Tcl_NotifyChannel when the + * channel is readable */ + Tcl_TimerToken writeTimer; /* A token for the timer that is scheduled in + * order to call Tcl_NotifyChannel when the + * channel is writable */ /* * Note regarding the usage of timers. @@ -266,7 +261,7 @@ typedef struct { struct ForwardParamInput { ForwardParamBase base; /* "Supertype". MUST COME FIRST. */ char *buf; /* O: Where to store the read bytes */ - Tcl_Size toRead; /* I: #bytes to read, + Tcl_Size toRead; /* I: #bytes to read, * O: #bytes actually read */ }; struct ForwardParamOutput { diff --git a/generic/tclIOUtil.c b/generic/tclIOUtil.c index 921d79e..3a56abf 100644 --- a/generic/tclIOUtil.c +++ b/generic/tclIOUtil.c @@ -51,13 +51,11 @@ typedef struct FilesystemRecord { typedef struct { int initialized; size_t cwdPathEpoch; /* Compared with the global cwdPathEpoch to - * determine whether cwdPathPtr is stale. - */ + * determine whether cwdPathPtr is stale. */ size_t filesystemEpoch; Tcl_Obj *cwdPathPtr; /* A private copy of cwdPathPtr. Updated when * the value is accessed and cwdPathEpoch has - * changed. - */ + * changed. */ void *cwdClientData; FilesystemRecord *filesystemList; size_t claims; @@ -328,8 +326,8 @@ Tcl_Stat( /* Obsolete */ int Tcl_Access( - const char *path, /* Pathname of file to access (in current CP). - */ + const char *path, /* Pathname of file to access (in + * current CP). */ int mode) /* Permission setting. */ { int ret; @@ -1104,8 +1102,7 @@ FsAddMountsToGlobResult( Tcl_Obj *pathPtr, /* The directory that was searched. */ const char *pattern, /* Pattern to match mounts against. */ Tcl_GlobTypeData *types) /* Acceptable types. May be NULL. The - * directory flag is particularly significant. - */ + * directory flag is particularly significant. */ { Tcl_Size mLength, gLength, i; int dir = (types == NULL || (types->type & TCL_GLOB_TYPE_DIR)); @@ -3012,8 +3009,8 @@ Tcl_FSChdir( int Tcl_FSLoadFile( Tcl_Interp *interp, /* Used for error reporting. */ - Tcl_Obj *pathPtr, /* Pathname of the file containing the dynamic shared object. - */ + Tcl_Obj *pathPtr, /* Pathname of the file containing the dynamic + * shared object. */ const char *sym1, const char *sym2, /* Names of two functions to find in the * dynamic shared object. */ @@ -3647,9 +3644,7 @@ Tcl_FSUnloadFile( Tcl_Obj * Tcl_FSLink( Tcl_Obj *pathPtr, /* Pathaname of file. */ - Tcl_Obj *toPtr, /* - * NULL or the pathname of a file to link to. - */ + Tcl_Obj *toPtr, /* NULL or the pathname of a file to link to. */ int linkAction) /* Action to perform. */ { const Tcl_Filesystem *fsPtr = Tcl_FSGetFileSystemForPath(pathPtr); @@ -3954,8 +3949,7 @@ TclFSNonnativePathType( * the filesystem for this pathname when it is * an absolute pathname. */ Tcl_Size *driveNameLengthPtr,/* If not NULL, a place to store the length of - * the volume name if the pathname is absolute. - */ + * the volume name if the pathname is absolute. */ Tcl_Obj **driveNameRef) /* If not NULL, a place to store a pointer to * an object having its its refCount already * incremented, and contining the name of the diff --git a/generic/tclInt.h b/generic/tclInt.h index 77eebb8..fffd0eb 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -257,8 +257,8 @@ typedef struct Namespace { * synonym. */ char *fullName; /* The namespace's fully qualified name. This * starts with ::. */ - void *clientData; /* An arbitrary value associated with this - * namespace. */ + void *clientData; /* An arbitrary value associated with this + * namespace. (Used by TclOO!) */ Tcl_NamespaceDeleteProc *deleteProc; /* Procedure invoked when deleting the * namespace to, e.g., free clientData. */ @@ -279,7 +279,7 @@ typedef struct Namespace { #else unsigned long nsId; #endif - Tcl_Interp *interp; /* The interpreter containing this + Tcl_Interp *interp; /* The interpreter containing this * namespace. */ int flags; /* OR-ed combination of the namespace status * flags NS_DYING and NS_DEAD listed below. */ @@ -312,12 +312,12 @@ typedef struct Namespace { * registered using "namespace export". */ Tcl_Size maxExportPatterns; /* Number of export patterns for which space * is currently allocated. */ - Tcl_Size cmdRefEpoch; /* Incremented if a newly added command + Tcl_Size cmdRefEpoch; /* Incremented if a newly added command * shadows a command for which this namespace * has already cached a Command* pointer; this * causes all its cached Command* pointers to * be invalidated. */ - Tcl_Size resolverEpoch; /* Incremented whenever (a) the name + Tcl_Size resolverEpoch; /* Incremented whenever (a) the name * resolution rules change for this namespace * or (b) a newly added command shadows a * command that is compiled to bytecodes. This @@ -444,7 +444,7 @@ typedef struct EnsembleConfig { * if the command has been deleted (or never * existed; the global namespace never has an * ensemble command.) */ - Tcl_Size epoch; /* The epoch at which this ensemble's table of + Tcl_Size epoch; /* The epoch at which this ensemble's table of * exported commands is valid. */ char **subcommandArrayPtr; /* Array of ensemble subcommand names. At all * consistent points, this will have the same @@ -501,7 +501,7 @@ typedef struct EnsembleConfig { * core, presumably because the ensemble * itself has been updated. */ Tcl_Obj *parameterList; /* List of ensemble parameter names. */ - Tcl_Size numParameters; /* Cached number of parameters. This is either + Tcl_Size numParameters; /* Cached number of parameters. This is either * 0 (if the parameterList field is NULL) or * the length of the list in the parameterList * field. */ @@ -970,9 +970,9 @@ typedef struct CompiledLocal { /* Next compiler-recognized local variable for * this procedure, or NULL if this is the last * local. */ - Tcl_Size nameLength; /* The number of bytes in local variable's name. + Tcl_Size nameLength; /* The number of bytes in local variable's name. * Among others used to speed up var lookups. */ - Tcl_Size frameIndex; /* Index in the array of compiler-assigned + Tcl_Size frameIndex; /* Index in the array of compiler-assigned * variables in the procedure call frame. */ #if TCL_MAJOR_VERSION < 9 int flags; @@ -993,7 +993,7 @@ typedef struct CompiledLocal { * although only VAR_ARGUMENT, VAR_TEMPORARY, * and VAR_RESOLVED make sense. */ #endif - char name[TCLFLEXARRAY]; /* Name of the local variable starts here. If + char name[TCLFLEXARRAY]; /* Name of the local variable starts here. If * the name is NULL, this will just be '\0'. * The actual size of this field will be large * enough to hold the name. MUST BE THE LAST @@ -1055,7 +1055,7 @@ typedef struct Trace { #else Tcl_CmdObjTraceProc *proc; /* Procedure to call to trace command. */ #endif - void *clientData; /* Arbitrary value to pass to proc. */ + void *clientData; /* Arbitrary value to pass to proc. */ struct Trace *nextPtr; /* Next in list of traces for this interp. */ int flags; /* Flags governing the trace - see * Tcl_CreateObjTrace for details. */ @@ -1247,7 +1247,7 @@ typedef struct CallFrame { * If FRAME_IS_PROC is set, the frame was * pushed to execute a Tcl procedure and may * have local vars. */ - Tcl_Size objc; /* This and objv below describe the arguments + Tcl_Size objc; /* This and objv below describe the arguments * for this procedure call. */ Tcl_Obj *const *objv; /* Array of argument objects. */ struct CallFrame *callerPtr; @@ -1261,7 +1261,7 @@ typedef struct CallFrame { * callerPtr unless an "uplevel" command or * something equivalent was active in the * caller). */ - Tcl_Size level; /* Level of this procedure, for "uplevel" + Tcl_Size level; /* Level of this procedure, for "uplevel" * purposes (i.e. corresponds to nesting of * callerVarPtr's, not callerPtr's). 1 for * outermost procedure, 0 for top-level. */ @@ -1281,7 +1281,7 @@ typedef struct CallFrame { * recognized by the compiler. The compiler * emits code that refers to these variables * using an index into this array. */ - void *clientData; /* Pointer to some context that is used by + void *clientData; /* Pointer to some context that is used by * object systems. The meaning of the contents * of this field is defined by the code that * sets it, and it should only ever be set by @@ -1381,7 +1381,7 @@ typedef struct CmdFrame { } data; Tcl_Obj *cmdObj; const char *cmd; /* The executed command, if possible... */ - Tcl_Size len; /* ... and its length. */ + Tcl_Size len; /* ... and its length. */ const struct CFWordBC *litarg; /* Link to set of literal arguments which have * ben pushed on the lineLABCPtr stack by @@ -1391,16 +1391,16 @@ typedef struct CmdFrame { typedef struct CFWord { CmdFrame *framePtr; /* CmdFrame to access. */ - Tcl_Size word; /* Index of the word in the command. */ + Tcl_Size word; /* Index of the word in the command. */ Tcl_Size refCount; /* Number of times the word is on the * stack. */ } CFWord; typedef struct CFWordBC { CmdFrame *framePtr; /* CmdFrame to access. */ - Tcl_Size pc; /* Instruction pointer of a command in + Tcl_Size pc; /* Instruction pointer of a command in * ExtCmdLoc.loc[.] */ - Tcl_Size word; /* Index of word in + Tcl_Size word; /* Index of word in * ExtCmdLoc.loc[cmd]->line[.] */ struct CFWordBC *prevPtr; /* Previous entry in stack for same Tcl_Obj. */ struct CFWordBC *nextPtr; /* Next entry for same command call. See @@ -1429,7 +1429,7 @@ typedef struct CFWordBC { #define CLL_END (-1) typedef struct ContLineLoc { - Tcl_Size num; /* Number of entries in loc, not counting the + Tcl_Size num; /* Number of entries in loc, not counting the * final -1 marker entry. */ Tcl_Size loc[TCLFLEXARRAY];/* Table of locations, as character offsets. * The table is allocated as part of the @@ -1472,14 +1472,14 @@ typedef struct ContLineLoc { typedef Tcl_Obj * (GetFrameInfoValueProc)(void *clientData); typedef struct { const char *name; /* Name of this field. */ - GetFrameInfoValueProc *proc; /* Function to generate a Tcl_Obj* from the + GetFrameInfoValueProc *proc;/* Function to generate a Tcl_Obj* from the * clientData, or just use the clientData * directly (after casting) if NULL. */ - void *clientData; /* Context for above function, or Tcl_Obj* if + void *clientData; /* Context for above function, or Tcl_Obj* if * proc field is NULL. */ } ExtraFrameInfoField; typedef struct { - Tcl_Size length; /* Length of array. */ + Tcl_Size length; /* Length of array. */ ExtraFrameInfoField fields[2]; /* Really as long as necessary, but this is * long enough for nearly anything. */ @@ -1571,12 +1571,14 @@ typedef int (CompileHookProc)(Tcl_Interp *interp, */ typedef struct ExecStack { - struct ExecStack *prevPtr; - struct ExecStack *nextPtr; + struct ExecStack *prevPtr; /* Previous stack in list. */ + struct ExecStack *nextPtr; /* Next stack in list. */ Tcl_Obj **markerPtr; - Tcl_Obj **endPtr; - Tcl_Obj **tosPtr; + Tcl_Obj **endPtr; /* Where the end is. */ + Tcl_Obj **tosPtr; /* Where the top of stack is. */ Tcl_Obj *stackWords[TCLFLEXARRAY]; + /* The actual stack space, following this + * structure in memory. */ } ExecStack; /* @@ -1604,20 +1606,20 @@ typedef struct CoroutineData { * coroutine. */ CorContext caller; CorContext running; - Tcl_HashTable *lineLABCPtr; /* See Interp.lineLABCPtr */ + Tcl_HashTable *lineLABCPtr; /* See Interp.lineLABCPtr */ void *stackLevel; - Tcl_Size auxNumLevels; /* While the coroutine is running the + Tcl_Size auxNumLevels; /* While the coroutine is running the * numLevels of the create/resume command is * stored here; for suspended coroutines it * holds the nesting numLevels at yield. */ - Tcl_Size nargs; /* Number of args required for resuming this - * coroutine; COROUTINE_ARGUMENTS_SINGLE_OPTIONAL means "0 or 1" - * (default), COROUTINE_ARGUMENTS_ARBITRARY means "any" */ + Tcl_Size nargs; /* Number of args required for resuming this + * coroutine; COROUTINE_ARGUMENTS_SINGLE_OPTIONAL + * means "0 or 1" (default), + * COROUTINE_ARGUMENTS_ARBITRARY means "any" */ Tcl_Obj *yieldPtr; /* The command to yield to. Stored here in * order to reset splice point in * TclNRCoroutineActivateCallback if the - * coroutine is busy. - */ + * coroutine is busy. */ } CoroutineData; typedef struct ExecEnv { @@ -1691,10 +1693,10 @@ typedef struct LiteralTable { #ifdef TCL_COMPILE_STATS typedef struct ByteCodeStats { - size_t numExecutions; /* Number of ByteCodes executed. */ + size_t numExecutions; /* Number of ByteCodes executed. */ size_t numCompilations; /* Number of ByteCodes created. */ size_t numByteCodesFreed; /* Number of ByteCodes destroyed. */ - size_t instructionCount[256]; /* Number of times each instruction was + size_t instructionCount[256]; /* Number of times each instruction was * executed. */ double totalSrcBytes; /* Total source bytes ever compiled. */ @@ -1702,7 +1704,7 @@ typedef struct ByteCodeStats { double currentSrcBytes; /* Src bytes for all current ByteCodes. */ double currentByteCodeBytes;/* Code bytes in all current ByteCodes. */ - size_t srcCount[32]; /* Source size distribution: # of srcs of + size_t srcCount[32]; /* Source size distribution: # of srcs of * size [2**(n-1)..2**n), n in [0..32). */ size_t byteCodeCount[32]; /* ByteCode size distribution. */ size_t lifetimeCount[32]; /* ByteCode lifetime distribution (ms). */ @@ -1961,8 +1963,7 @@ typedef struct Interp { * enabled extensions check for a NULL pointer value * and for a TCL_STUBS_MAGIC value to verify they * are not [load]ing into one of those pre-stubs - * interps. - */ + * interps. */ TclHandle handle; /* Handle used to keep track of when this * interp is deleted. */ @@ -2098,7 +2099,8 @@ typedef struct Interp { /* First in list of active traces for interp, * or NULL if no active traces. */ - Tcl_Size tracesForbiddingInline; /* Count of traces (in the list headed by + Tcl_Size tracesForbiddingInline; + /* Count of traces (in the list headed by * tracePtr) that forbid inline bytecode * compilation. */ @@ -2128,7 +2130,7 @@ typedef struct Interp { * as flag values the same as the 'active' * field. */ - Tcl_Size cmdCount; /* Limit for how many commands to execute in + Tcl_Size cmdCount; /* Limit for how many commands to execute in * the interpreter. */ LimitHandler *cmdHandlers; /* Handlers to execute when the limit is @@ -2164,9 +2166,10 @@ typedef struct Interp { * *root* ensemble command? (Nested ensembles * don't rewrite this.) NULL if we're not * processing an ensemble. */ - Tcl_Size numRemovedObjs; /* How many arguments have been stripped off + Tcl_Size numRemovedObjs;/* How many arguments have been stripped off * because of ensemble processing. */ - Tcl_Size numInsertedObjs; /* How many of the current arguments were + Tcl_Size numInsertedObjs; + /* How many of the current arguments were * inserted by an ensemble. */ } ensembleRewrite; @@ -2226,8 +2229,7 @@ typedef struct Interp { * used by function ...() in the same file. * It does for the eval/direct path of script * execution what CompileEnv.clLoc does for - * the bytecode compiler. - */ + * the bytecode compiler. */ /* * TIP #268. The currently active selection mode, i.e. the package require * preferences. @@ -2299,7 +2301,7 @@ typedef struct Interp { Tcl_Obj *callLiteral; /* "CALL" literal for [info errorstack] */ Tcl_Obj *innerLiteral; /* "INNER" literal for [info errorstack] */ Tcl_Obj *innerContext; /* cached list for fast reallocation */ - int resetErrorStack; /* controls cleaning up of ::errorStack */ + int resetErrorStack; /* controls cleaning up of ::errorStack */ #ifdef TCL_COMPILE_STATS /* @@ -2565,17 +2567,17 @@ typedef enum TclEolTranslation { * */ typedef struct ListStore { - Tcl_Size firstUsed; /* Index of first slot in use within slots[] */ - Tcl_Size numUsed; /* Number of slots in use (starting firstUsed) */ - Tcl_Size numAllocated; /* Total number of slots[] array slots. */ - size_t refCount; /* Number of references to this instance */ - int flags; /* LISTSTORE_* flags */ - Tcl_Obj *slots[TCLFLEXARRAY]; /* Variable size array. Grown as needed */ + Tcl_Size firstUsed; /* Index of first slot in use within slots[] */ + Tcl_Size numUsed; /* Number of slots in use (starting firstUsed) */ + Tcl_Size numAllocated; /* Total number of slots[] array slots. */ + size_t refCount; /* Number of references to this instance */ + int flags; /* LISTSTORE_* flags */ + Tcl_Obj *slots[TCLFLEXARRAY]; /* Variable size array. Grown as needed */ } ListStore; -#define LISTSTORE_CANONICAL 0x1 /* All Tcl_Obj's referencing this - store have their string representation - derived from the list representation */ +#define LISTSTORE_CANONICAL 0x1 /* All Tcl_Obj's referencing this store have + * their string representation derived from + * the list representation. */ /* Max number of elements that can be contained in a list */ #define LIST_MAX \ @@ -2590,11 +2592,11 @@ typedef struct ListStore { * See comments above for ListStore */ typedef struct ListSpan { - Tcl_Size spanStart; /* Starting index of the span */ - Tcl_Size spanLength; /* Number of elements in the span */ - size_t refCount; /* Count of references to this span record */ + Tcl_Size spanStart; /* Starting index of the span */ + Tcl_Size spanLength; /* Number of elements in the span */ + size_t refCount; /* Count of references to this span record */ } ListSpan; -#ifndef LIST_SPAN_THRESHOLD /* May be set on build line */ +#ifndef LIST_SPAN_THRESHOLD /* May be set on build line */ #define LIST_SPAN_THRESHOLD 101 #endif @@ -2603,9 +2605,11 @@ typedef struct ListSpan { * See comments above for ListStore */ typedef struct ListRep { - ListStore *storePtr;/* element array shared amongst different lists */ - ListSpan *spanPtr; /* If not NULL, the span holds the range of slots - within *storePtr that contain this list elements. */ + ListStore *storePtr; /* Element array shared amongst different + * lists. */ + ListSpan *spanPtr; /* If not NULL, the span holds the range of + * slots within *storePtr that contain this + * list elements. */ } ListRep; /* @@ -2909,7 +2913,7 @@ typedef void (TclInitProcessGlobalValueProc)(char **valuePtr, TCL_HASH_TYPE *len */ typedef struct ProcessGlobalValue { - Tcl_Size epoch; /* Epoch counter to detect changes in the + Tcl_Size epoch; /* Epoch counter to detect changes in the * global value. */ TCL_HASH_TYPE numBytes; /* Length of the global string. */ char *value; /* The global string value. */ @@ -2968,6 +2972,7 @@ typedef struct ProcessGlobalValue { /* *---------------------------------------------------------------------- + * * Common functions for calculating overallocation. Trivial but allows for * experimenting with growth factors without having to change code in * multiple places. See TclAttemptAllocElemsEx and similar for usage @@ -2977,23 +2982,29 @@ typedef struct ProcessGlobalValue { * *---------------------------------------------------------------------- */ + static inline Tcl_Size -TclUpsizeAlloc(TCL_UNUSED(Tcl_Size) /* oldSize. For future experiments with - * some growth algorithms that use this - * information. */, - Tcl_Size needed, - Tcl_Size limit) +TclUpsizeAlloc( + TCL_UNUSED(Tcl_Size), /* oldSize. For future experiments with + * some growth algorithms that use this + * information. */ + Tcl_Size needed, + Tcl_Size limit) { /* assert (oldCapacity < needed <= limit) */ if (needed < (limit - needed/2)) { return needed + needed / 2; - } - else { + } else { return limit; } } -static inline Tcl_Size TclUpsizeRetry(Tcl_Size needed, Tcl_Size lastAttempt) { - /* assert (needed < lastAttempt) */ + +static inline Tcl_Size +TclUpsizeRetry( + Tcl_Size needed, + Tcl_Size lastAttempt) +{ + /* assert (needed < lastAttempt) */ if (needed < lastAttempt - 1) { /* (needed+lastAttempt)/2 but that formula may overflow Tcl_Size */ return needed + (lastAttempt - needed) / 2; @@ -3001,37 +3012,64 @@ static inline Tcl_Size TclUpsizeRetry(Tcl_Size needed, Tcl_Size lastAttempt) { return needed; } } -MODULE_SCOPE void *TclAllocElemsEx(Tcl_Size elemCount, Tcl_Size elemSize, - Tcl_Size leadSize, Tcl_Size *capacityPtr); -MODULE_SCOPE void *TclReallocElemsEx(void *oldPtr, Tcl_Size elemCount, - Tcl_Size elemSize, Tcl_Size leadSize, - Tcl_Size *capacityPtr); -MODULE_SCOPE void *TclAttemptReallocElemsEx(void *oldPtr, - Tcl_Size elemCount, Tcl_Size elemSize, - Tcl_Size leadSize, Tcl_Size *capacityPtr); + +MODULE_SCOPE void * TclAllocElemsEx(Tcl_Size elemCount, Tcl_Size elemSize, + Tcl_Size leadSize, Tcl_Size *capacityPtr); +MODULE_SCOPE void * TclReallocElemsEx(void *oldPtr, Tcl_Size elemCount, + Tcl_Size elemSize, Tcl_Size leadSize, + Tcl_Size *capacityPtr); +MODULE_SCOPE void * TclAttemptReallocElemsEx(void *oldPtr, + Tcl_Size elemCount, Tcl_Size elemSize, + Tcl_Size leadSize, Tcl_Size *capacityPtr); + /* Alloc elemCount elements of size elemSize with leadSize header * returning actual capacity (in elements) in *capacityPtr. */ -static inline void *TclAttemptAllocElemsEx(Tcl_Size elemCount, Tcl_Size elemSize, - Tcl_Size leadSize, Tcl_Size *capacityPtr) { +static inline void * +TclAttemptAllocElemsEx( + Tcl_Size elemCount, + Tcl_Size elemSize, + Tcl_Size leadSize, + Tcl_Size *capacityPtr) +{ return TclAttemptReallocElemsEx( NULL, elemCount, elemSize, leadSize, capacityPtr); } + /* Alloc numByte bytes, returning actual capacity in *capacityPtr. */ -static inline void *TclAllocEx(Tcl_Size numBytes, Tcl_Size *capacityPtr) { +static inline void * +TclAllocEx( + Tcl_Size numBytes, + Tcl_Size *capacityPtr) +{ return TclAllocElemsEx(numBytes, 1, 0, capacityPtr); } + /* Alloc numByte bytes, returning actual capacity in *capacityPtr. */ static inline void * -TclAttemptAllocEx(Tcl_Size numBytes, Tcl_Size *capacityPtr) +TclAttemptAllocEx( + Tcl_Size numBytes, + Tcl_Size *capacityPtr) { return TclAttemptAllocElemsEx(numBytes, 1, 0, capacityPtr); } + /* Realloc numByte bytes, returning actual capacity in *capacityPtr. */ -static inline void *TclReallocEx(void *oldPtr, Tcl_Size numBytes, Tcl_Size *capacityPtr) { +static inline void * +TclReallocEx( + void *oldPtr, + Tcl_Size numBytes, + Tcl_Size *capacityPtr) +{ return TclReallocElemsEx(oldPtr, numBytes, 1, 0, capacityPtr); } + /* Realloc numByte bytes, returning actual capacity in *capacityPtr. */ -static inline void *TclAttemptReallocEx(void *oldPtr, Tcl_Size numBytes, Tcl_Size *capacityPtr) { +static inline void * +TclAttemptReallocEx( + void *oldPtr, + Tcl_Size numBytes, + Tcl_Size *capacityPtr) +{ return TclAttemptReallocElemsEx(oldPtr, numBytes, 1, 0, capacityPtr); } @@ -3052,13 +3090,12 @@ MODULE_SCOPE TclPlatformType tclPlatform; MODULE_SCOPE Tcl_Encoding tclIdentityEncoding; MODULE_SCOPE Tcl_Encoding tclUtf8Encoding; -MODULE_SCOPE int -TclEncodingProfileNameToId(Tcl_Interp *interp, - const char *profileName, - int *profilePtr); +MODULE_SCOPE int TclEncodingProfileNameToId(Tcl_Interp *interp, + const char *profileName, + int *profilePtr); MODULE_SCOPE const char *TclEncodingProfileIdToName(Tcl_Interp *interp, - int profileId); -MODULE_SCOPE void TclGetEncodingProfiles(Tcl_Interp *interp); + int profileId); +MODULE_SCOPE void TclGetEncodingProfiles(Tcl_Interp *interp); /* * TIP #233 (Virtualized Time) @@ -3178,7 +3215,7 @@ typedef struct ForIterData { Tcl_Obj *body; /* Loop body. */ Tcl_Obj *next; /* Loop step script, NULL for 'while'. */ const char *msg; /* Error message part. */ - Tcl_Size word; /* Index of the body script in the command */ + Tcl_Size word; /* Index of the body script in the command */ } ForIterData; /* TIP #357 - Structure doing the bookkeeping of handles for Tcl_LoadFile @@ -3458,7 +3495,7 @@ MODULE_SCOPE int TclNewArithSeriesObj(Tcl_Interp *interp, Tcl_Obj **arithSeriesP MODULE_SCOPE Tcl_Obj * TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep, Tcl_Size len); MODULE_SCOPE void TclpAlertNotifier(void *clientData); -MODULE_SCOPE void *TclpNotifierData(void); +MODULE_SCOPE void * TclpNotifierData(void); MODULE_SCOPE void TclpServiceModeHook(int mode); MODULE_SCOPE void TclpSetTimer(const Tcl_Time *timePtr); MODULE_SCOPE int TclpWaitForEvent(const Tcl_Time *timePtr); @@ -3487,7 +3524,7 @@ MODULE_SCOPE Tcl_Size TclpFindVariable(const char *name, Tcl_Size *lengthPtr); MODULE_SCOPE void TclpInitLibraryPath(char **valuePtr, TCL_HASH_TYPE *lengthPtr, Tcl_Encoding *encodingPtr); MODULE_SCOPE void TclpInitLock(void); -MODULE_SCOPE void *TclpInitNotifier(void); +MODULE_SCOPE void * TclpInitNotifier(void); MODULE_SCOPE void TclpInitPlatform(void); MODULE_SCOPE void TclpInitUnlock(void); MODULE_SCOPE Tcl_Obj * TclpObjListVolumes(void); @@ -3575,7 +3612,7 @@ MODULE_SCOPE Tcl_Size TclTrimLeft(const char *bytes, Tcl_Size numBytes, MODULE_SCOPE Tcl_Size TclTrimRight(const char *bytes, Tcl_Size numBytes, const char *trim, Tcl_Size numTrim); MODULE_SCOPE const char*TclGetCommandTypeName(Tcl_Command command); -MODULE_SCOPE int TclObjInterpProc(void *clientData, +MODULE_SCOPE int TclObjInterpProc(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); MODULE_SCOPE void TclRegisterCommandTypeName( @@ -3607,13 +3644,13 @@ MODULE_SCOPE double TclpWideClickInMicrosec(void); #else # ifdef _WIN32 # define TCL_WIDE_CLICKS 1 -MODULE_SCOPE long long TclpGetWideClicks(void); +MODULE_SCOPE long long TclpGetWideClicks(void); MODULE_SCOPE double TclpWideClickInMicrosec(void); # define TclpWideClicksToNanoseconds(clicks) \ ((double)(clicks) * TclpWideClickInMicrosec() * 1000) # endif #endif -MODULE_SCOPE long long TclpGetMicroseconds(void); +MODULE_SCOPE long long TclpGetMicroseconds(void); MODULE_SCOPE int TclZlibInit(Tcl_Interp *interp); MODULE_SCOPE void * TclpThreadCreateKey(void); @@ -4004,13 +4041,13 @@ MODULE_SCOPE int TclFullFinalizationRequested(void); * TIP #542 */ -MODULE_SCOPE size_t TclUniCharLen(const Tcl_UniChar *uniStr); -MODULE_SCOPE int TclUniCharNcmp(const Tcl_UniChar *ucs, - const Tcl_UniChar *uct, size_t numChars); -MODULE_SCOPE int TclUniCharNcasecmp(const Tcl_UniChar *ucs, - const Tcl_UniChar *uct, size_t numChars); -MODULE_SCOPE int TclUniCharCaseMatch(const Tcl_UniChar *uniStr, - const Tcl_UniChar *uniPattern, int nocase); +MODULE_SCOPE size_t TclUniCharLen(const Tcl_UniChar *uniStr); +MODULE_SCOPE int TclUniCharNcmp(const Tcl_UniChar *ucs, + const Tcl_UniChar *uct, size_t numChars); +MODULE_SCOPE int TclUniCharNcasecmp(const Tcl_UniChar *ucs, + const Tcl_UniChar *uct, size_t numChars); +MODULE_SCOPE int TclUniCharCaseMatch(const Tcl_UniChar *uniStr, + const Tcl_UniChar *uniPattern, int nocase); /* @@ -4049,7 +4086,7 @@ MODULE_SCOPE void TclProcessCreated(Tcl_Pid pid); MODULE_SCOPE TclProcessWaitStatus TclProcessWait(Tcl_Pid pid, int options, int *codePtr, Tcl_Obj **msgObjPtr, Tcl_Obj **errorObjPtr); -MODULE_SCOPE int TclClose(Tcl_Interp *, Tcl_Channel chan); +MODULE_SCOPE int TclClose(Tcl_Interp *, Tcl_Channel chan); /* * TIP #508: [array default] @@ -4070,7 +4107,8 @@ MODULE_SCOPE Tcl_Size TclIndexDecode(int encoded, Tcl_Size endValue); /* * Error message utility functions */ -MODULE_SCOPE int TclCommandWordLimitError(Tcl_Interp *interp, Tcl_Size count); +MODULE_SCOPE int TclCommandWordLimitError( + Tcl_Interp *interp, Tcl_Size count); #endif /* TCL_MAJOR_VERSION > 8 */ diff --git a/generic/tclLoad.c b/generic/tclLoad.c index a2d1919..ac0d00d 100644 --- a/generic/tclLoad.c +++ b/generic/tclLoad.c @@ -1107,9 +1107,8 @@ TclGetLoadedLibraries( * NULL, return info about all interps; * otherwise, just return info about this * interpreter. */ - const char *prefix) /* Prefix or NULL. If NULL, return info - * for all prefixes. - */ + const char *prefix) /* Prefix or NULL. If NULL, return info + * for all prefixes. */ { Tcl_Interp *target; LoadedLibrary *libraryPtr; diff --git a/generic/tclNamesp.c b/generic/tclNamesp.c index a0668be..41abf98 100644 --- a/generic/tclNamesp.c +++ b/generic/tclNamesp.c @@ -4928,8 +4928,7 @@ TclLogCommandInfo( const char *command, /* First character in command that generated * the error. */ Tcl_Size length, /* Number of bytes in command (< 0 means use - * all bytes up to first null byte). - */ + * all bytes up to first null byte). */ const unsigned char *pc, /* Current pc of bytecode execution context */ Tcl_Obj **tosPtr) /* Current stack of bytecode execution * context */ diff --git a/generic/tclOO.c b/generic/tclOO.c index 1d72fb0..7a57105 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -120,6 +120,9 @@ static const DeclaredClassMethod objMethods[] = { DCM("new", 1, TclOO_Class_New), DCM("createWithNamespace", 0, TclOO_Class_CreateNs), {NULL, 0, {0, NULL, NULL, NULL, NULL}} +}, cfgMethods[] = { + DCM("configure", 1, TclOO_Configurable_Configure), + {NULL, 0, {0, NULL, NULL, NULL, NULL}} }; /* @@ -427,6 +430,18 @@ InitFoundation( } /* + * Make the configurable class and install its standard defined method. + */ + + Tcl_Object cfgCls = Tcl_NewObjectInstance(interp, + (Tcl_Class) fPtr->classCls, + "::oo::configuresupport::configurable", NULL, -1, NULL, 0); + for (i = 0 ; cfgMethods[i].name ; i++) { + TclOONewBasicMethod(interp, ((Object *) cfgCls)->classPtr, + &cfgMethods[i]); + } + + /* * Evaluate the remaining definitions, which are a compiled-in Tcl script. */ @@ -457,11 +472,11 @@ InitClassSystemRoots( fPtr->objectCls = &fakeCls; /* referenced in TclOOAllocClass to increment the refCount. */ fakeCls.thisPtr = &fakeObject; - fakeObject.refCount = 0; /* Do not increment an uninitialized value. */ + fakeObject.refCount = 0; // Do not increment an uninitialized value. fPtr->objectCls = TclOOAllocClass(interp, AllocObject(interp, "object", (Namespace *)fPtr->ooNs, NULL)); - /* Corresponding TclOODecrRefCount in KillFoudation */ + // Corresponding TclOODecrRefCount in KillFoundation AddRef(fPtr->objectCls->thisPtr); /* @@ -486,7 +501,7 @@ InitClassSystemRoots( fPtr->classCls = TclOOAllocClass(interp, AllocObject(interp, "class", (Namespace *)fPtr->ooNs, NULL)); - /* Corresponding TclOODecrRefCount in KillFoudation */ + // Corresponding TclOODecrRefCount in KillFoundation AddRef(fPtr->classCls->thisPtr); /* @@ -576,8 +591,8 @@ DeletedHelpersNamespace( static void KillFoundation( TCL_UNUSED(void *), - Tcl_Interp *interp) /* The interpreter containing the OO system - * foundation. */ + Tcl_Interp *interp) /* The interpreter containing the OO system + * foundation. */ { Foundation *fPtr = GetFoundation(interp); @@ -791,7 +806,7 @@ SquelchCachedName( static void MyDeleted( - void *clientData) /* Reference to the object whose [my] has been + void *clientData) /* Reference to the object whose [my] has been * squelched. */ { Object *oPtr = (Object *)clientData; @@ -822,7 +837,7 @@ MyClassDeleted( static void ObjectRenamedTrace( - void *clientData, /* The object being deleted. */ + void *clientData, /* The object being deleted. */ TCL_UNUSED(Tcl_Interp *), TCL_UNUSED(const char *) /*oldName*/, TCL_UNUSED(const char *) /*newName*/, @@ -1135,7 +1150,7 @@ TclOOReleaseClassContents( static void ObjectNamespaceDeleted( - void *clientData) /* Pointer to the class whose namespace is + void *clientData) /* Pointer to the class whose namespace is * being deleted. */ { Object *oPtr = (Object *)clientData; @@ -1235,7 +1250,7 @@ ObjectNamespaceDeleted( * methods on the object. */ - /* TODO: Should this be protected with a !IsRoot() condition? */ + // TODO: Should this be protected with a !IsRoot() condition? TclOORemoveFromInstances(oPtr, oPtr->selfCls); if (oPtr->mixins.num > 0) { @@ -1715,10 +1730,10 @@ Tcl_NewObjectInstance( const char *nsNameStr, /* Name of namespace to create inside object, * or NULL to ask the code to pick its own * unique name. */ - Tcl_Size objc, /* Number of arguments. Negative value means + Tcl_Size objc, /* Number of arguments. Negative value means * do not call constructor. */ Tcl_Obj *const *objv, /* Argument list. */ - Tcl_Size skip) /* Number of arguments to _not_ pass to the + Tcl_Size skip) /* Number of arguments to _not_ pass to the * constructor. */ { Class *classPtr = (Class *) cls; @@ -1783,10 +1798,10 @@ TclNRNewObjectInstance( const char *nsNameStr, /* Name of namespace to create inside object, * or NULL to ask the code to pick its own * unique name. */ - Tcl_Size objc, /* Number of arguments. Negative value means + Tcl_Size objc, /* Number of arguments. Negative value means * do not call constructor. */ Tcl_Obj *const *objv, /* Argument list. */ - Tcl_Size skip, /* Number of arguments to _not_ pass to the + Tcl_Size skip, /* Number of arguments to _not_ pass to the * constructor. */ Tcl_Object *objectPtr) /* Place to write the object reference upon * successful allocation. */ @@ -2604,7 +2619,7 @@ TclOOInvokeObject( * (PRIVATE_METHOD), or a *really* private * context (any other value; conventionally * 0). */ - Tcl_Size objc, /* Number of arguments. */ + Tcl_Size objc, /* Number of arguments. */ Tcl_Obj *const *objv) /* Array of argument objects. It is assumed * that the name of the method to invoke will * be at index 1. */ @@ -2675,7 +2690,7 @@ int TclOOObjectCmdCore( Object *oPtr, /* The object being invoked. */ Tcl_Interp *interp, /* The interpreter containing the object. */ - Tcl_Size objc, /* How many arguments are being passed in. */ + Tcl_Size objc, /* How many arguments are being passed in. */ Tcl_Obj *const *objv, /* The array of arguments. */ int flags, /* Whether this is an invocation through the * public or the private command interface. */ diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 251ae34..0e642ef 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1341,8 +1341,22 @@ TclOOCopyObjectCmd( return TCL_OK; } +/* + * ---------------------------------------------------------------------- + * + * TclOO_Configurable_Configure -- + * + * Implementation of the oo::configurable->configure method. + * + * ---------------------------------------------------------------------- + */ + +/* + * Ugly thunks to read and write a property by calling the right method in + * the right way. + */ static int -ReadProp( +ReadProperty( Tcl_Interp *interp, Object *oPtr, Tcl_Obj *propObj) @@ -1358,11 +1372,22 @@ ReadProp( code = TclOOPrivateObjectCmd(oPtr, interp, 2, args); Tcl_DecrRefCount(args[0]); Tcl_DecrRefCount(args[1]); - return code; + switch (code) { + case TCL_BREAK: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property getter for %s did a break", TclGetString(propObj))); + return TCL_ERROR; + case TCL_CONTINUE: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property getter for %s did a continue", TclGetString(propObj))); + return TCL_ERROR; + default: + return code; + } } static int -WriteProp( +WriteProperty( Tcl_Interp *interp, Object *oPtr, Tcl_Obj *propObj, @@ -1382,117 +1407,226 @@ WriteProp( Tcl_DecrRefCount(args[0]); Tcl_DecrRefCount(args[1]); Tcl_DecrRefCount(args[2]); - return code; + switch (code) { + case TCL_BREAK: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property setter for %s did a break", TclGetString(propObj))); + return TCL_ERROR; + case TCL_CONTINUE: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property setter for %s did a continue", TclGetString(propObj))); + return TCL_ERROR; + default: + return code; + } } +/* Short-term cache for GetPropertyName(). */ +struct Cache { + Tcl_Obj *listPtr; /* Holds references to names. */ + char *names[TCLFLEXARRAY]; /* NULL-terminated table of names. */ +}; + +enum GPNFlags { + GPN_WRITABLE = 1, /* Are we looking for a writable property? */ + GPN_FALLING_BACK = 2 /* Are we doing a recursive call to determine + * if the property is of the other type? */ +}; + /* Look up a property full name. */ static Tcl_Obj * GetPropertyName( Tcl_Interp *interp, /* Context and error reporting. */ Object *oPtr, /* Object to get property name from. */ - int writable, /* Are we looking for a writable property? */ - Tcl_Obj *namePtr) /* The name supplied by the user. */ + int flags, /* Are we looking for a writable property? + * Can we do a fallback message? + * See GPNFlags for possible values */ + Tcl_Obj *namePtr, /* The name supplied by the user. */ + struct Cache **cachePtr) /* Where to cache the table, if the caller + * wants that. The contents are to be freed + * with Tcl_Free if the cache is used. */ { - int allocated; Tcl_Size objc, index, i; - Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, writable, &allocated); + Tcl_Obj *listPtr = TclOOGetAllObjectProperties( + oPtr, flags & GPN_WRITABLE); Tcl_Obj **objv; - if (allocated) { - TclOOSortPropList(listPtr); - } - ListObjGetElements(listPtr, objc, objv); - char **tablePtr = TclStackAlloc(interp, sizeof(char*) * objc); - for (int i = 0; i < objc; i++) { - tablePtr[i] = TclGetString(objv[i]); + struct Cache *tablePtr; + + (void) Tcl_ListObjGetElements(NULL, listPtr, &objc, &objv); + if (cachePtr && *cachePtr) { + tablePtr = *cachePtr; + } else { + tablePtr = (struct Cache *) Tcl_Alloc( + offsetof(struct Cache, names) + sizeof(char *) * (objc + 1)); + + for (i = 0; i < objc; i++) { + tablePtr->names[i] = TclGetString(objv[i]); + } + tablePtr->names[objc] = NULL; + if (cachePtr) { + /* + * Have a cache, but nothing in it so far. + * + * We cache the list here so it doesn't vanish from under our + * feet if a property implementation does something crazy like + * changing the set of properties. The type of copy this does + * means that the copy holds the references to the names in the + * table. + */ + tablePtr->listPtr = TclListObjCopy(NULL, listPtr); + Tcl_IncrRefCount(tablePtr->listPtr); + *cachePtr = tablePtr; + } else { + tablePtr->listPtr = NULL; + } } - int result = Tcl_GetIndexFromObjStruct(interp, namePtr, tablePtr, + int result = Tcl_GetIndexFromObjStruct(interp, namePtr, tablePtr->names, sizeof(char *), "property", TCL_INDEX_TEMP_TABLE, &index); - TclStackFree(interp, tablePtr); + if (result == TCL_ERROR && !(flags & GPN_FALLING_BACK)) { + /* + * If property can be accessed the other way, use a special message. + * We use a recursive call to look this up. + */ + + Tcl_InterpState foo = Tcl_SaveInterpState(interp, result); + Tcl_Obj *otherName = GetPropertyName(interp, oPtr, + flags ^ (GPN_WRITABLE | GPN_FALLING_BACK), namePtr, NULL); + result = Tcl_RestoreInterpState(interp, foo); + if (otherName != NULL) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property \"%s\" is %s only", + TclGetString(otherName), + (flags & GPN_WRITABLE) ? "read" : "write")); + } + } + if (!cachePtr) { + Tcl_Free(tablePtr); + } if (result != TCL_OK) { return NULL; } return objv[index]; } +/* Release the cache made by GetPropertyName(). */ +static void +ReleasePropertyNameCache( + struct Cache **cachePtr) +{ + if (*cachePtr) { + struct Cache *tablePtr = *cachePtr; + if (tablePtr->listPtr) { + Tcl_DecrRefCount(tablePtr->listPtr); + } + Tcl_Free(tablePtr); + *cachePtr = NULL; + } +} + int TclOO_Configurable_Configure( TCL_UNUSED(void *), Tcl_Interp *interp, /* Interpreter used for the result, error * reporting, etc. */ Tcl_ObjectContext context, /* The object/call context. */ - Tcl_Size objc, /* Number of arguments. */ + int objc, /* Number of arguments. */ Tcl_Obj *const *objv) /* The actual arguments. */ { Object *oPtr = (Object *) Tcl_ObjectContextObject(context); Tcl_Size skip = Tcl_ObjectContextSkippedArgs(context); - Tcl_Size numArgs = objc - skip; Tcl_Obj *namePtr; - Tcl_Size i; - int code; + Tcl_Size i, namec; + int code = TCL_OK; - if (numArgs == 0) { + objc -= skip; + if ((objc & 1) && (objc != 1)) { + /* + * Bad (odd > 1) number of arguments. + */ + + Tcl_WrongNumArgs(interp, skip, objv, "?-option value ...?"); + return TCL_ERROR; + } + + objv += skip; + if (objc == 0) { /* * Read all properties. */ - Tcl_Size namec; - int allocated = 0; - Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, 0, &allocated); + Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, 0); Tcl_Obj *resultPtr = Tcl_NewObj(), **namev; - if (allocated) { - TclOOSortPropList(listPtr); - } + Tcl_IncrRefCount(listPtr); ListObjGetElements(listPtr, namec, namev); for (i = 0; i < namec; ) { - code = ReadProp(interp, oPtr, namev[i]); + code = ReadProperty(interp, oPtr, namev[i]); if (code != TCL_OK) { Tcl_DecrRefCount(resultPtr); - return code; + break; } - Tcl_DictObjPut(NULL, resultPtr, namev[i], Tcl_GetObjResult(interp)); + Tcl_DictObjPut(NULL, resultPtr, namev[i], + Tcl_GetObjResult(interp)); if (++i >= namec) { Tcl_SetObjResult(interp, resultPtr); break; } Tcl_SetObjResult(interp, Tcl_NewObj()); } - } else if (numArgs == 1) { + Tcl_DecrRefCount(listPtr); + return code; + } else if (objc == 1) { /* * Read a single named property. */ - namePtr = GetPropertyName(interp, oPtr, 0, objv[skip]); + namePtr = GetPropertyName(interp, oPtr, 0, objv[0], NULL); if (namePtr == NULL) { return TCL_ERROR; } - return ReadProp(interp, oPtr, namePtr); - } else if (numArgs % 2) { + return ReadProperty(interp, oPtr, namePtr); + } else if (objc == 2) { /* - * Bad (odd > 1) number of arguments. + * Special case for writing to one property. Saves fiddling with the + * cache in this common case. */ - Tcl_WrongNumArgs(interp, skip, objv, "?-option value ...?"); - return TCL_ERROR; + namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[0], NULL); + if (namePtr == NULL) { + return TCL_ERROR; + } + code = WriteProperty(interp, oPtr, namePtr, objv[1]); + if (code == TCL_OK) { + Tcl_ResetResult(interp); + } + return code; } else { /* - * Write properties. + * Write properties. Slightly tricky because we want to cache the + * table of property names. */ + struct Cache *cache = NULL; - objv += skip; - for (i = 0; i < numArgs; i += 2) { - namePtr = GetPropertyName(interp, oPtr, 1, objv[i]); + code = TCL_OK; + for (i = 0; i < objc; i += 2) { + namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[i], + &cache); if (namePtr == NULL) { - return TCL_ERROR; + code = TCL_ERROR; + break; } - code = WriteProp(interp, oPtr, namePtr, objv[i + 1]); + code = WriteProperty(interp, oPtr, namePtr, objv[i + 1]); if (code != TCL_OK) { - return code; + break; } } + if (code == TCL_OK) { + Tcl_ResetResult(interp); + } + ReleasePropertyNameCache(&cache); + return code; } - return TCL_OK; } /* diff --git a/generic/tclOOCall.c b/generic/tclOOCall.c index 7695483..773bb56 100644 --- a/generic/tclOOCall.c +++ b/generic/tclOOCall.c @@ -25,7 +25,7 @@ struct ChainBuilder { CallChain *callChainPtr; /* The call chain being built. */ - size_t filterLength; /* Number of entries in the call chain that + size_t filterLength; /* Number of entries in the call chain that * are due to processing filters and not the * main call chain. */ Object *oPtr; /* The object that we are building the chain @@ -309,7 +309,7 @@ FreeMethodNameRep( int TclOOInvokeContext( - void *clientData, /* The method call context. */ + void *clientData, /* The method call context. */ Tcl_Interp *interp, /* Interpreter for error reporting, and many * other sorts of context handling (e.g., * commands, variables) depending on method @@ -678,7 +678,7 @@ CmpStr( static void AddClassMethodNames( Class *clsPtr, /* Class to get method names from. */ - int flags, /* Whether we are interested in just the + int flags, /* Whether we are interested in just the * public method names. */ Tcl_HashTable *const namesPtr, /* Reference to the hash table to put the @@ -2039,8 +2039,9 @@ AddSimpleClassDefineNamespaces( static inline void AddDefinitionNamespaceToChain( - Class *const definerCls, /* What class defines this entry. */ - Tcl_Obj *const namespaceName, /* The name for this entry (or NULL, a + Class *const definerCls, /* What class defines this entry. */ + Tcl_Obj *const namespaceName, + /* The name for this entry (or NULL, a * no-op). */ DefineChain *const definePtr, /* The define chain to add the method @@ -2290,9 +2291,8 @@ TclOOGetAllClassProperties( * * TclOOGetAllObjectProperties -- * - * Get the list of all properties known to a object, including to its - * classes. Manages a cache so this operation is usually cheap. - * The order of properties in the resulting list is undefined. + * Get the sorted list of all properties known to a object, including to its + * its classes. Manages a cache so this operation is usually cheap. * * ---------------------------------------------------------------------- */ @@ -2300,12 +2300,9 @@ TclOOGetAllClassProperties( Tcl_Obj * TclOOGetAllObjectProperties( Object *oPtr, /* The object to inspect. Must exist. */ - int writable, /* Whether to get writable properties. If + int writable) /* Whether to get writable properties. If * false, readable properties will be returned * instead. */ - int *allocated) /* Address of variable to set to true if a - * Tcl_Obj was allocated and may be safely - * modified by the caller. */ { Tcl_HashTable hashTable; FOREACH_HASH_DECLS; @@ -2319,12 +2316,10 @@ TclOOGetAllObjectProperties( if (oPtr->properties.epoch == oPtr->fPtr->epoch) { if (writable) { if (oPtr->properties.allWritableCache) { - *allocated = 0; return oPtr->properties.allWritableCache; } } else { if (oPtr->properties.allReadableCache) { - *allocated = 0; return oPtr->properties.allReadableCache; } } @@ -2334,7 +2329,6 @@ TclOOGetAllObjectProperties( * Gather the information. Unsorted! (Caller will sort.) */ - *allocated = 1; Tcl_InitObjHashTable(&hashTable); FindObjectProps(oPtr, writable, &hashTable); TclNewObj(result); @@ -2342,6 +2336,7 @@ TclOOGetAllObjectProperties( Tcl_ListObjAppendElement(NULL, result, propName); } Tcl_DeleteHashTable(&hashTable); + TclOOSortPropList(result); /* * Cache the information. diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 1a0bb43..7112039 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -330,7 +330,7 @@ TclOOObjectSetFilters( */ Tcl_Obj **filtersList; - int size = sizeof(Tcl_Obj *) * numFilters; /* should be size_t */ + size_t size = sizeof(Tcl_Obj *) * numFilters; if (oPtr->filters.num == 0) { filtersList = (Tcl_Obj **)Tcl_Alloc(size); @@ -345,7 +345,7 @@ TclOOObjectSetFilters( oPtr->filters.num = numFilters; oPtr->flags &= ~USE_CLASS_CACHE; } - BumpInstanceEpoch(oPtr); /* Only this object can be affected. */ + BumpInstanceEpoch(oPtr); // Only this object can be affected. } /* @@ -389,7 +389,7 @@ TclOOClassSetFilters( */ Tcl_Obj **filtersList; - int size = sizeof(Tcl_Obj *) * numFilters; /* should be size_t */ + size_t size = sizeof(Tcl_Obj *) * numFilters; if (classPtr->filters.num == 0) { filtersList = (Tcl_Obj **)Tcl_Alloc(size); @@ -1119,7 +1119,7 @@ MagicDefinitionInvoke( Tcl_GetCommandFullName(interp, cmd, obj2Ptr); } Tcl_ListObjAppendElement(NULL, objPtr, obj2Ptr); - /* TODO: overflow? */ + // TODO: overflow? Tcl_ListObjReplace(NULL, objPtr, 1, 0, objc - offset, objv + offset); TclListObjGetElementsM(NULL, objPtr, &dummy, &objs); diff --git a/generic/tclOOInfo.c b/generic/tclOOInfo.c index bc04748..958e330 100644 --- a/generic/tclOOInfo.c +++ b/generic/tclOOInfo.c @@ -1810,7 +1810,7 @@ InfoObjectPropCmd( Tcl_Obj *const objv[]) { Object *oPtr; - int i, idx, all = 0, writable = 0, allocated = 0; + int i, idx, all = 0, writable = 0; Tcl_Obj *result, *propObj; if (objc < 2) { @@ -1844,10 +1844,7 @@ InfoObjectPropCmd( */ if (all) { - result = TclOOGetAllObjectProperties(oPtr, writable, &allocated); - if (allocated) { - TclOOSortPropList(result); - } + result = TclOOGetAllObjectProperties(oPtr, writable); } else { TclNewObj(result); if (writable) { diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index d9f4ed8..b35cd13 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -209,9 +209,9 @@ typedef struct Object { * references; this mechanism exists to * avoid Tcl_Preserve. */ int flags; - Tcl_Size creationEpoch; /* Unique value to make comparisons of objects + Tcl_Size creationEpoch; /* Unique value to make comparisons of objects * easier. */ - Tcl_Size epoch; /* Per-object epoch, incremented when the way + Tcl_Size epoch; /* Per-object epoch, incremented when the way * an object should resolve call chains is * changed. */ Tcl_HashTable *metadataPtr; /* Mapping from pointers to metadata type to @@ -472,7 +472,7 @@ typedef struct { *---------------------------------------------------------------- */ -MODULE_SCOPE int TclOOInit(Tcl_Interp *interp); +MODULE_SCOPE int TclOOInit(Tcl_Interp *interp); MODULE_SCOPE Tcl_ObjCmdProc TclOODefineObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOObjDefObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineConstructorObjCmd; @@ -507,6 +507,7 @@ MODULE_SCOPE Tcl_MethodCallProc TclOO_Object_Eval; MODULE_SCOPE Tcl_MethodCallProc TclOO_Object_LinkVar; MODULE_SCOPE Tcl_MethodCallProc TclOO_Object_Unknown; MODULE_SCOPE Tcl_MethodCallProc TclOO_Object_VarName; +MODULE_SCOPE Tcl_MethodCallProc TclOO_Configurable_Configure; /* * Private definitions, some of which perhaps ought to be exposed properly or @@ -518,17 +519,17 @@ MODULE_SCOPE void TclOOAddToMixinSubs(Class *subPtr, Class *mixinPtr); MODULE_SCOPE void TclOOAddToSubclasses(Class *subPtr, Class *superPtr); MODULE_SCOPE Class * TclOOAllocClass(Tcl_Interp *interp, Object *useThisObj); -MODULE_SCOPE int TclMethodIsType(Tcl_Method method, - const Tcl_MethodType *typePtr, - void **clientDataPtr); +MODULE_SCOPE int TclMethodIsType(Tcl_Method method, + const Tcl_MethodType *typePtr, + void **clientDataPtr); MODULE_SCOPE Tcl_Method TclNewInstanceMethod(Tcl_Interp *interp, - Tcl_Object object, Tcl_Obj *nameObj, - int flags, const Tcl_MethodType *typePtr, - void *clientData); + Tcl_Object object, Tcl_Obj *nameObj, + int flags, const Tcl_MethodType *typePtr, + void *clientData); MODULE_SCOPE Tcl_Method TclNewMethod(Tcl_Interp *interp, Tcl_Class cls, - Tcl_Obj *nameObj, int flags, - const Tcl_MethodType *typePtr, - void *clientData); + Tcl_Obj *nameObj, int flags, + const Tcl_MethodType *typePtr, + void *clientData); MODULE_SCOPE int TclNRNewObjectInstance(Tcl_Interp *interp, Tcl_Class cls, const char *nameStr, const char *nsNameStr, Tcl_Size objc, @@ -550,7 +551,7 @@ MODULE_SCOPE void TclOODelMethodRef(Method *method); MODULE_SCOPE Tcl_Obj * TclOOGetAllClassProperties(Class *clsPtr, int writable, int *allocated); MODULE_SCOPE Tcl_Obj * TclOOGetAllObjectProperties(Object *oPtr, - int writable, int *allocated); + int writable); MODULE_SCOPE CallContext *TclOOGetCallContext(Object *oPtr, Tcl_Obj *methodNameObj, int flags, Object *contextObjPtr, Class *contextClsPtr, diff --git a/generic/tclOOMethod.c b/generic/tclOOMethod.c index 4711695..f1d96f0 100644 --- a/generic/tclOOMethod.c +++ b/generic/tclOOMethod.c @@ -536,7 +536,7 @@ TclOOMakeProcInstanceMethod( * NULL. */ const Tcl_MethodType *typePtr, /* The type of the method to create. */ - void *clientData, /* The per-method type-specific data. */ + void *clientData, /* The per-method type-specific data. */ Proc **procPtrPtr) /* A pointer to the variable in which to write * the procedure record reference. Presumably * inside the structure indicated by the @@ -649,7 +649,7 @@ TclOOMakeProcMethod( * NULL. */ const Tcl_MethodType *typePtr, /* The type of the method to create. */ - void *clientData, /* The per-method type-specific data. */ + void *clientData, /* The per-method type-specific data. */ Proc **procPtrPtr) /* A pointer to the variable in which to write * the procedure record reference. Presumably * inside the structure indicated by the @@ -744,7 +744,7 @@ TclOOMakeProcMethod( static int InvokeProcedureMethod( - void *clientData, /* Pointer to some per-method context. */ + void *clientData, /* Pointer to some per-method context. */ Tcl_Interp *interp, Tcl_ObjectContext context, /* The method calling context. */ int objc, /* Number of arguments. */ @@ -1038,7 +1038,7 @@ ProcedureMethodVarResolver( Tcl_Interp *interp, const char *varName, Tcl_Namespace *contextNs, - TCL_UNUSED(int) /*flags*/, /* Ignoring variable access flags (???) */ + TCL_UNUSED(int) /*flags*/, // Ignoring variable access flags (???) Tcl_Var *varPtr) { int result; @@ -1257,7 +1257,7 @@ RenderDeclarerName( * ---------------------------------------------------------------------- */ -/* TODO: Check whether Tcl_AppendLimitedToObj() can work here. */ +// TODO: Check whether Tcl_AppendLimitedToObj() can work here. #define LIMIT 60 #define ELLIPSIFY(str,len) \ @@ -1267,7 +1267,7 @@ static void MethodErrorHandler( Tcl_Interp *interp, TCL_UNUSED(Tcl_Obj *) /*methodNameObj*/) - /* We pull the method name out of context instead of from argument */ + // We pull the method name out of context instead of from argument { Tcl_Size nameLen, objectNameLen; CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; @@ -1299,7 +1299,7 @@ static void ConstructorErrorHandler( Tcl_Interp *interp, TCL_UNUSED(Tcl_Obj *) /*methodNameObj*/) - /* Ignore. We know it is the constructor. */ + // Ignore. We know it is the constructor. { CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; @@ -1329,7 +1329,7 @@ static void DestructorErrorHandler( Tcl_Interp *interp, TCL_UNUSED(Tcl_Obj *) /*methodNameObj*/) - /* Ignore. We know it is the destructor. */ + // Ignore. We know it is the destructor. { CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; @@ -1545,7 +1545,7 @@ TclOONewForwardMethod( static int InvokeForwardMethod( - void *clientData, /* Pointer to some per-method context. */ + void *clientData, /* Pointer to some per-method context. */ Tcl_Interp *interp, Tcl_ObjectContext context, /* The method calling context. */ int objc, /* Number of arguments. */ diff --git a/generic/tclOOScript.h b/generic/tclOOScript.h index eb6a96e..1903b49 100644 --- a/generic/tclOOScript.h +++ b/generic/tclOOScript.h @@ -364,108 +364,7 @@ static const char *tclOOSetupScript = "\t\t\t::namespace path ::oo::objdefine\n" "\t\t\t::namespace export property\n" "\t\t}\n" -"\t\tproc ReadAll {object my} {\n" -"\t\t\tset result {}\n" -"\t\t\tforeach prop [info object properties $object -all -readable] {\n" -"\t\t\t\ttry {\n" -"\t\t\t\t\tdict set result $prop [$my ]\n" -"\t\t\t\t} on error {msg opt} {\n" -"\t\t\t\t\tdict set opt -level 2\n" -"\t\t\t\t\treturn -options $opt $msg\n" -"\t\t\t\t} on return {msg opt} {\n" -"\t\t\t\t\tdict incr opt -level 2\n" -"\t\t\t\t\treturn -options $opt $msg\n" -"\t\t\t\t} on break {} {\n" -"\t\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\t\"property getter for $prop did a break\"\n" -"\t\t\t\t} on continue {} {\n" -"\t\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\t\"property getter for $prop did a continue\"\n" -"\t\t\t\t}\n" -"\t\t\t}\n" -"\t\t\treturn $result\n" -"\t\t}\n" -"\t\tproc ReadOne {object my propertyName} {\n" -"\t\t\tset props [info object properties $object -all -readable]\n" -"\t\t\ttry {\n" -"\t\t\t\tset prop [prefix match -message \"property\" $props $propertyName]\n" -"\t\t\t} on error {msg} {\n" -"\t\t\t\tcatch {\n" -"\t\t\t\t\tset wps [info object properties $object -all -writable]\n" -"\t\t\t\t\tset wprop [prefix match $wps $propertyName]\n" -"\t\t\t\t\tset msg \"property \\\"$wprop\\\" is write only\"\n" -"\t\t\t\t}\n" -"\t\t\t\treturn -code error -level 2 -errorcode [list \\\n" -"\t\t\t\t\t\tTCL LOOKUP INDEX property $propertyName] $msg\n" -"\t\t\t}\n" -"\t\t\ttry {\n" -"\t\t\t\tset value [$my ]\n" -"\t\t\t} on error {msg opt} {\n" -"\t\t\t\tdict set opt -level 2\n" -"\t\t\t\treturn -options $opt $msg\n" -"\t\t\t} on return {msg opt} {\n" -"\t\t\t\tdict incr opt -level 2\n" -"\t\t\t\treturn -options $opt $msg\n" -"\t\t\t} on break {} {\n" -"\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\"property getter for $prop did a break\"\n" -"\t\t\t} on continue {} {\n" -"\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\"property getter for $prop did a continue\"\n" -"\t\t\t}\n" -"\t\t\treturn $value\n" -"\t\t}\n" -"\t\tproc WriteMany {object my setterMap} {\n" -"\t\t\tset props [info object properties $object -all -writable]\n" -"\t\t\tforeach {prop value} $setterMap {\n" -"\t\t\t\ttry {\n" -"\t\t\t\t\tset prop [prefix match -message \"property\" $props $prop]\n" -"\t\t\t\t} on error {msg} {\n" -"\t\t\t\t\tcatch {\n" -"\t\t\t\t\t\tset rps [info object properties $object -all -readable]\n" -"\t\t\t\t\t\tset rprop [prefix match $rps $prop]\n" -"\t\t\t\t\t\tset msg \"property \\\"$rprop\\\" is read only\"\n" -"\t\t\t\t\t}\n" -"\t\t\t\t\treturn -code error -level 2 -errorcode [list \\\n" -"\t\t\t\t\t\t\tTCL LOOKUP INDEX property $prop] $msg\n" -"\t\t\t\t}\n" -"\t\t\t\ttry {\n" -"\t\t\t\t\t$my $value\n" -"\t\t\t\t} on error {msg opt} {\n" -"\t\t\t\t\tdict set opt -level 2\n" -"\t\t\t\t\treturn -options $opt $msg\n" -"\t\t\t\t} on return {msg opt} {\n" -"\t\t\t\t\tdict incr opt -level 2\n" -"\t\t\t\t\treturn -options $opt $msg\n" -"\t\t\t\t} on break {} {\n" -"\t\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\t\"property setter for $prop did a break\"\n" -"\t\t\t\t} on continue {} {\n" -"\t\t\t\t\treturn -code error -level 2 -errorcode {TCLOO SHENANIGANS} \\\n" -"\t\t\t\t\t\t\"property setter for $prop did a continue\"\n" -"\t\t\t\t}\n" -"\t\t\t}\n" -"\t\t\treturn\n" -"\t\t}\n" -"\t\t::oo::class create configurable {\n" -"\t\t\tprivate variable my\n" -"\t\t\tmethod configure -export args {\n" -"\t\t\t\t::if {![::info exists my]} {\n" -"\t\t\t\t\t::set my [::namespace which my]\n" -"\t\t\t\t}\n" -"\t\t\t\t::if {[::llength $args] == 0} {\n" -"\t\t\t\t\t::oo::configuresupport::ReadAll [self] $my\n" -"\t\t\t\t} elseif {[::llength $args] == 1} {\n" -"\t\t\t\t\t::oo::configuresupport::ReadOne [self] $my \\\n" -"\t\t\t\t\t\t[::lindex $args 0]\n" -"\t\t\t\t} elseif {[::llength $args] % 2 == 0} {\n" -"\t\t\t\t\t::oo::configuresupport::WriteMany [self] $my $args\n" -"\t\t\t\t} else {\n" -"\t\t\t\t\t::return -code error -errorcode {TCL WRONGARGS} \\\n" -"\t\t\t\t\t\t[::format {wrong # args: should be \"%s\"} \\\n" -"\t\t\t\t\t\t\t\"[self] configure \?-option value ...\?\"]\n" -"\t\t\t\t}\n" -"\t\t\t}\n" +"\t\t::oo::define configurable {\n" "\t\t\tdefinitionnamespace -instance configurableobject\n" "\t\t\tdefinitionnamespace -class configurableclass\n" "\t\t}\n" diff --git a/generic/tclProcess.c b/generic/tclProcess.c index b16c73d..83ae4d6 100644 --- a/generic/tclProcess.c +++ b/generic/tclProcess.c @@ -192,8 +192,7 @@ WaitProcessStatus( * - errno in case of error. * - non-zero exit code for abormal exit. * - signal number if killed or suspended. - * - Tcl_WaitPid status in all other cases. - */ + * - Tcl_WaitPid status in all other cases. */ Tcl_Obj **msgObjPtr, /* If non-NULL, will receive error message. */ Tcl_Obj **errorObjPtr) /* If non-NULL, will receive error code. */ { @@ -864,8 +863,7 @@ TclProcessWait( * - errno in case of error. * - non-zero exit code for abormal exit. * - signal number if killed or suspended. - * - Tcl_WaitPid status in all other cases. - */ + * - Tcl_WaitPid status in all other cases. */ Tcl_Obj **msgObjPtr, /* If non-NULL, will receive error message. */ Tcl_Obj **errorObjPtr) /* If non-NULL, will receive error code. */ { diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 4591a1b..2843dff 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -612,156 +612,15 @@ # ------------------------------------------------------------------ # - # oo::configuresupport::ReadAll -- - # - # The implementation of [$o configure] with no extra arguments. - # - # ------------------------------------------------------------------ - - proc ReadAll {object my} { - set result {} - foreach prop [info object properties $object -all -readable] { - try { - dict set result $prop [$my ] - } on error {msg opt} { - dict set opt -level 2 - return -options $opt $msg - } on return {msg opt} { - dict incr opt -level 2 - return -options $opt $msg - } on break {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property getter for $prop did a break" - } on continue {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property getter for $prop did a continue" - } - } - return $result - } - - # ------------------------------------------------------------------ - # - # oo::configuresupport::ReadOne -- - # - # The implementation of [$o configure -prop] with that single - # extra argument. - # - # ------------------------------------------------------------------ - - proc ReadOne {object my propertyName} { - set props [info object properties $object -all -readable] - try { - set prop [prefix match -message "property" $props $propertyName] - } on error {msg} { - catch { - set wps [info object properties $object -all -writable] - set wprop [prefix match $wps $propertyName] - set msg "property \"$wprop\" is write only" - } - return -code error -level 2 -errorcode [list \ - TCL LOOKUP INDEX property $propertyName] $msg - } - try { - set value [$my ] - } on error {msg opt} { - dict set opt -level 2 - return -options $opt $msg - } on return {msg opt} { - dict incr opt -level 2 - return -options $opt $msg - } on break {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property getter for $prop did a break" - } on continue {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property getter for $prop did a continue" - } - return $value - } - - # ------------------------------------------------------------------ - # - # oo::configuresupport::WriteMany -- - # - # The implementation of [$o configure -prop val ?-prop val...?]. - # - # ------------------------------------------------------------------ - - proc WriteMany {object my setterMap} { - set props [info object properties $object -all -writable] - foreach {prop value} $setterMap { - try { - set prop [prefix match -message "property" $props $prop] - } on error {msg} { - catch { - set rps [info object properties $object -all -readable] - set rprop [prefix match $rps $prop] - set msg "property \"$rprop\" is read only" - } - return -code error -level 2 -errorcode [list \ - TCL LOOKUP INDEX property $prop] $msg - } - try { - $my $value - } on error {msg opt} { - dict set opt -level 2 - return -options $opt $msg - } on return {msg opt} { - dict incr opt -level 2 - return -options $opt $msg - } on break {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property setter for $prop did a break" - } on continue {} { - return -code error -level 2 -errorcode {TCLOO SHENANIGANS} \ - "property setter for $prop did a continue" - } - } - return - } - - # ------------------------------------------------------------------ - # # oo::configuresupport::configurable -- # # The class that contains the implementation of the actual # 'configure' method (mixed into actually configurable classes). - # Great care needs to be taken in these methods as they are - # potentially used in classes where the current namespace is set - # up very strangely. + # The 'configure' method is in tclOOBasic.c. # # ------------------------------------------------------------------ - ::oo::class create configurable { - private variable my - # - # configure -- - # Method for providing client access to the property mechanism. - # Has a user-facing API similar to that of [chan configure]. - # - method configure -export args { - ::if {![::info exists my]} { - ::set my [::namespace which my] - } - ::if {[::llength $args] == 0} { - # Read all properties - ::oo::configuresupport::ReadAll [self] $my - } elseif {[::llength $args] == 1} { - # Read a single property - ::oo::configuresupport::ReadOne [self] $my \ - [::lindex $args 0] - } elseif {[::llength $args] % 2 == 0} { - # Set properties, one or several - ::oo::configuresupport::WriteMany [self] $my $args - } else { - # Invalid call - ::return -code error -errorcode {TCL WRONGARGS} \ - [::format {wrong # args: should be "%s"} \ - "[self] configure ?-option value ...?"] - } - } - + ::oo::define configurable { definitionnamespace -instance configurableobject definitionnamespace -class configurableclass } diff --git a/win/Makefile.in b/win/Makefile.in index 877c4f3..b79d808 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -103,6 +103,7 @@ GENERIC_DIR = $(TOP_DIR)/generic WIN_DIR = $(TOP_DIR)/win COMPAT_DIR = $(TOP_DIR)/compat PKGS_DIR = $(TOP_DIR)/pkgs +TOOL_DIR = $(TOP_DIR)/tools ZLIB_DIR = $(COMPAT_DIR)/zlib MINIZIP_DIR = $(ZLIB_DIR)/contrib/minizip TOMMATH_DIR = $(TOP_DIR)/libtommath @@ -1133,6 +1134,11 @@ $(GENERIC_DIR)/tclStubInit.c: $(GENERIC_DIR)/tcl.decls \ @echo "Developers may want to run \"make genstubs\" to regenerate." @echo "This warning can be safely ignored, do not report as a bug!" +$(GENERIC_DIR)/tclOOScript.h: $(TOOL_DIR)/tclOOScript.tcl + @echo "Warning: tclOOScript.h may be out of date." + @echo "Developers may want to run \"make genscript\" to regenerate." + @echo "This warning can be safely ignored, do not report as a bug!" + genstubs: $(TCL_EXE) "$(ROOT_DIR_NATIVE)/tools/genStubs.tcl" \ "$(GENERIC_DIR_NATIVE)" \ @@ -1143,6 +1149,10 @@ genstubs: "$(GENERIC_DIR_NATIVE)" \ "$(GENERIC_DIR_NATIVE)/tclOO.decls" +genscript: + $(TCL_EXE) $(ROOT_DIR_NATIVE)/tools/makeHeader.tcl \ + $(ROOT_DIR_NATIVE)/tools/tclOOScript.tcl $(GENERIC_DIR)/tclOOScript.h + # # This target creates the HTML folder for Tcl & Tk and places it in # DISTDIR/html. It uses the tcltk-man2html.tcl tool from the Tcl group's tool diff --git a/win/tclWinConsole.c b/win/tclWinConsole.c index 5b30fc4..dfaf7331d 100644 --- a/win/tclWinConsole.c +++ b/win/tclWinConsole.c @@ -1468,10 +1468,9 @@ ConsoleEventProc( static void ConsoleWatchProc( - void *instanceData, /* Console state. */ + void *instanceData, /* Console state. */ int newMask) /* What events to watch for, one of - * of TCL_READABLE, TCL_WRITABLE - */ + * of TCL_READABLE, TCL_WRITABLE */ { ConsoleChannelInfo **nextPtrPtr, *ptr; ConsoleChannelInfo *chanInfoPtr = (ConsoleChannelInfo *)instanceData; diff --git a/win/tclWinSock.c b/win/tclWinSock.c index f54d8a1..f83a293 100644 --- a/win/tclWinSock.c +++ b/win/tclWinSock.c @@ -2899,10 +2899,9 @@ NewSocketInfo(SOCKET socket) static int WaitForSocketEvent( - TcpState *statePtr, /* Information about this socket. */ + TcpState *statePtr, /* Information about this socket. */ int events, /* Events to look for. May be one of - * FD_READ or FD_WRITE. - */ + * FD_READ or FD_WRITE. */ int *errorCodePtr) /* Where to store errors? */ { int result = 1; -- cgit v0.12 From b13d82a5dbe1709dad049d401028e6c889027f31 Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 27 Jan 2024 22:09:37 +0000 Subject: Change back memory management to how I wanted it to be --- generic/tclOOBasic.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 0e642ef..4553e50 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1456,7 +1456,7 @@ GetPropertyName( if (cachePtr && *cachePtr) { tablePtr = *cachePtr; } else { - tablePtr = (struct Cache *) Tcl_Alloc( + tablePtr = (struct Cache *) TclStackAlloc(interp, offsetof(struct Cache, names) + sizeof(char *) * (objc + 1)); for (i = 0; i < objc; i++) { @@ -1500,7 +1500,7 @@ GetPropertyName( } } if (!cachePtr) { - Tcl_Free(tablePtr); + TclStackFree(interp, tablePtr); } if (result != TCL_OK) { return NULL; @@ -1511,6 +1511,7 @@ GetPropertyName( /* Release the cache made by GetPropertyName(). */ static void ReleasePropertyNameCache( + Tcl_Interp *interp, struct Cache **cachePtr) { if (*cachePtr) { @@ -1518,7 +1519,7 @@ ReleasePropertyNameCache( if (tablePtr->listPtr) { Tcl_DecrRefCount(tablePtr->listPtr); } - Tcl_Free(tablePtr); + TclStackFree(interp, tablePtr); *cachePtr = NULL; } } @@ -1624,7 +1625,7 @@ TclOO_Configurable_Configure( if (code == TCL_OK) { Tcl_ResetResult(interp); } - ReleasePropertyNameCache(&cache); + ReleasePropertyNameCache(interp, &cache); return code; } } -- cgit v0.12 From 406da00d29838733c9dbee62d3f224db20823e12 Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 27 Jan 2024 23:16:53 +0000 Subject: win/Makefile/in tinkering --- win/Makefile.in | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/win/Makefile.in b/win/Makefile.in index b79d808..faf8d4b 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -119,6 +119,7 @@ TCL_LIBRARY_NATIVE = $(shell $(CYGPATH) '$(TCL_LIBRARY)') GENERIC_DIR_NATIVE = $(shell $(CYGPATH) '$(GENERIC_DIR)') WIN_DIR_NATIVE = $(shell $(CYGPATH) '$(WIN_DIR)') ROOT_DIR_NATIVE = $(shell $(CYGPATH) '$(ROOT_DIR)') +TOOL_DIR_NATIVE = $(shell $(CYGPATH) '$(TOOL_DIR)') SCRIPT_INSTALL_DIR_NATIVE = $(shell $(CYGPATH) '$(SCRIPT_INSTALL_DIR)') INCLUDE_INSTALL_DIR_NATIVE = $(shell $(CYGPATH) '$(INCLUDE_INSTALL_DIR)') MAN_INSTALL_DIR_NATIVE = $(shell $(CYGPATH) '$(MAN_INSTALL_DIR)') @@ -1140,18 +1141,19 @@ $(GENERIC_DIR)/tclOOScript.h: $(TOOL_DIR)/tclOOScript.tcl @echo "This warning can be safely ignored, do not report as a bug!" genstubs: - $(TCL_EXE) "$(ROOT_DIR_NATIVE)/tools/genStubs.tcl" \ + $(TCL_EXE) "$(TOOL_DIR_NATIVE)/genStubs.tcl" \ "$(GENERIC_DIR_NATIVE)" \ "$(GENERIC_DIR_NATIVE)/tcl.decls" \ "$(GENERIC_DIR_NATIVE)/tclInt.decls" \ "$(GENERIC_DIR_NATIVE)/tclTomMath.decls" - $(TCL_EXE) "$(ROOT_DIR_NATIVE)/tools/genStubs.tcl" \ + $(TCL_EXE) "$(TOOL_DIR_NATIVE)/genStubs.tcl" \ "$(GENERIC_DIR_NATIVE)" \ "$(GENERIC_DIR_NATIVE)/tclOO.decls" genscript: - $(TCL_EXE) $(ROOT_DIR_NATIVE)/tools/makeHeader.tcl \ - $(ROOT_DIR_NATIVE)/tools/tclOOScript.tcl $(GENERIC_DIR)/tclOOScript.h + $(TCL_EXE) "$(TOOL_DIR_NATIVE)/makeHeader.tcl" \ + "$(TOOL_DIR_NATIVE)/tclOOScript.tcl" \ + "$(GENERIC_DIR_NATIVE)/tclOOScript.h" # # This target creates the HTML folder for Tcl & Tk and places it in -- cgit v0.12 From 19cb1decae47b6a5c4ebbb6414be019e12aa3476 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 28 Jan 2024 15:40:29 +0000 Subject: Property implementations: not plugged into Tcl level yet --- generic/tclOOBasic.c | 271 +++++++++++++++++++++++++++++++++++++++++++-------- generic/tclOOInt.h | 6 ++ generic/tclVar.c | 12 +-- 3 files changed, 243 insertions(+), 46 deletions(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 4553e50..f8c9eb5 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -720,34 +720,22 @@ TclOO_Object_LinkVar( /* * ---------------------------------------------------------------------- * - * TclOO_Object_VarName -- + * LookupObjectVar -- * - * Implementation of the oo::object->varname method. + * Look up a variable in an object. Tricky because of private variables. * * ---------------------------------------------------------------------- */ -int -TclOO_Object_VarName( - TCL_UNUSED(void *), - Tcl_Interp *interp, /* Interpreter in which to create the object; - * also used for error reporting. */ - Tcl_ObjectContext context, /* The object/call context. */ - int objc, /* Number of arguments. */ - Tcl_Obj *const *objv) /* The actual arguments. */ +static Tcl_Var +LookupObjectVar( + Tcl_Interp *interp, + Tcl_Object object, + Tcl_Obj *varName, + Tcl_Var *aryPtr) { - Var *varPtr, *aryVar; - Tcl_Obj *varNamePtr, *argPtr; - CallFrame *framePtr = ((Interp *) interp)->varFramePtr; - const char *arg; - - if ((int)Tcl_ObjectContextSkippedArgs(context)+1 != objc) { - Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, - "varName"); - return TCL_ERROR; - } - argPtr = objv[objc-1]; - arg = TclGetString(argPtr); + const char *arg = TclGetString(varName); + Tcl_Obj *varNamePtr; /* * Convert the variable name to fully-qualified form if it wasn't already. @@ -759,10 +747,10 @@ TclOO_Object_VarName( */ if (arg[0] == ':' && arg[1] == ':') { - varNamePtr = argPtr; + varNamePtr = varName; } else { - Tcl_Namespace *namespacePtr = - Tcl_GetObjectNamespace(Tcl_ObjectContextObject(context)); + Tcl_Namespace *namespacePtr = Tcl_GetObjectNamespace(object); + CallFrame *framePtr = ((Interp *) interp)->varFramePtr; /* * Private method handling. [TIP 500] @@ -775,7 +763,7 @@ TclOO_Object_VarName( */ if (framePtr->isProcCallFrame & FRAME_IS_METHOD) { - Object *oPtr = (Object *) Tcl_ObjectContextObject(context); + Object *oPtr = (Object *) object; CallContext *callerContext = (CallContext *)framePtr->clientData; Method *mPtr = callerContext->callPtr->chain[ callerContext->index].mPtr; @@ -785,8 +773,8 @@ TclOO_Object_VarName( if (mPtr->declaringObjectPtr == oPtr) { FOREACH_STRUCT(pvPtr, oPtr->privateVariables) { if (!strcmp(TclGetString(pvPtr->variableObj), - TclGetString(argPtr))) { - argPtr = pvPtr->fullNameObj; + TclGetString(varName))) { + varName = pvPtr->fullNameObj; break; } } @@ -807,8 +795,8 @@ TclOO_Object_VarName( if (isInstance) { FOREACH_STRUCT(pvPtr, clsPtr->privateVariables) { if (!strcmp(TclGetString(pvPtr->variableObj), - TclGetString(argPtr))) { - argPtr = pvPtr->fullNameObj; + TclGetString(varName))) { + varName = pvPtr->fullNameObj; break; } } @@ -816,16 +804,53 @@ TclOO_Object_VarName( } } + // The namespace isn't the global one; necessarily true for any object! varNamePtr = Tcl_NewStringObj(namespacePtr->fullName, -1); Tcl_AppendToObj(varNamePtr, "::", 2); - Tcl_AppendObjToObj(varNamePtr, argPtr); + Tcl_AppendObjToObj(varNamePtr, varName); } Tcl_IncrRefCount(varNamePtr); - varPtr = TclObjLookupVar(interp, varNamePtr, NULL, - TCL_NAMESPACE_ONLY|TCL_LEAVE_ERR_MSG, "refer to", 1, 1, &aryVar); + Tcl_Var var = (Tcl_Var) TclObjLookupVar(interp, varNamePtr, NULL, + TCL_NAMESPACE_ONLY|TCL_LEAVE_ERR_MSG, "refer to", 1, 1, + (Var **) aryPtr); Tcl_DecrRefCount(varNamePtr); + if (var == NULL) { + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARIABLE", arg, (void *) NULL); + } + return var; +} + +/* + * ---------------------------------------------------------------------- + * + * TclOO_Object_VarName -- + * + * Implementation of the oo::object->varname method. + * + * ---------------------------------------------------------------------- + */ + +int +TclOO_Object_VarName( + TCL_UNUSED(void *), + Tcl_Interp *interp, /* Interpreter in which to create the object; + * also used for error reporting. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Tcl_Var varPtr, aryVar; + Tcl_Obj *varNamePtr; + + if ((int) Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, + "varName"); + return TCL_ERROR; + } + + varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), + objv[objc - 1], &aryVar); if (varPtr == NULL) { - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARIABLE", arg, (void *)NULL); return TCL_ERROR; } @@ -836,18 +861,16 @@ TclOO_Object_VarName( TclNewObj(varNamePtr); if (aryVar != NULL) { - Tcl_GetVariableFullName(interp, (Tcl_Var) aryVar, varNamePtr); + Tcl_GetVariableFullName(interp, aryVar, varNamePtr); /* * WARNING! This code pokes inside the implementation of hash tables! */ - Tcl_AppendToObj(varNamePtr, "(", -1); - Tcl_AppendObjToObj(varNamePtr, ((VarInHash *) - varPtr)->entry.key.objPtr); - Tcl_AppendToObj(varNamePtr, ")", -1); + Tcl_AppendPrintfToObj(varNamePtr, "(%s)", Tcl_GetString( + ((VarInHash *) varPtr)->entry.key.objPtr)); } else { - Tcl_GetVariableFullName(interp, (Tcl_Var) varPtr, varNamePtr); + Tcl_GetVariableFullName(interp, varPtr, varNamePtr); } Tcl_SetObjResult(interp, varNamePtr); return TCL_OK; @@ -1631,6 +1654,174 @@ TclOO_Configurable_Configure( } /* + * ---------------------------------------------------------------------- + * + * Configurable_Getter, Configurable_Setter -- + * + * Standard property implementation. The clientData is a simple Tcl_Obj* + * that contains the name of the property. + * + * TclOOImplementObjectProperty, TclOOImplementClassProperty -- + * + * Installs a basic property implementation for a property, either on + * an instance or on a class. It's up to the code that calls these + * to ensure that the property name is syntactically valid. + * + * ---------------------------------------------------------------------- + */ + +static int +Configurable_Getter( + void *clientData, /* Which property to read. + * Actually a Tcl_Obj* reference. */ + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_Var varPtr, aryVar; + Tcl_Obj *valuePtr; + + if ((int) Tcl_ObjectContextSkippedArgs(context) != objc) { + Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), + objv, NULL); + return TCL_ERROR; + } + + varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), + propNamePtr, &aryVar); + if (varPtr == NULL) { + return TCL_ERROR; + } + + valuePtr = TclPtrGetVar(interp, varPtr, aryVar, propNamePtr, NULL, + TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG); + if (valuePtr == NULL) { + return TCL_ERROR; + } + Tcl_SetObjResult(interp, valuePtr); + return TCL_OK; +} + +static int +Configurable_Setter( + void *clientData, /* Which property to write. + * Actually a Tcl_Obj* reference. */ + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_Var varPtr, aryVar; + + if ((int) Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), + objv, "value"); + return TCL_ERROR; + } + + varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), + propNamePtr, &aryVar); + if (varPtr == NULL) { + return TCL_ERROR; + } + + if (TclPtrSetVar(interp, varPtr, aryVar, propNamePtr, NULL, + objv[objc - 1], TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG) == NULL) { + return TCL_ERROR; + } + return TCL_OK; +} + +// Simple support functions +void DetailsDeleter( + void *clientData) +{ + // Just drop the reference count + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_DecrRefCount(propNamePtr); +} + +int DetailsCloner( + TCL_UNUSED(Tcl_Interp *), + void *oldClientData, + void **newClientData) +{ + // Just add another reference to this name; easy! + Tcl_Obj *propNamePtr = (Tcl_Obj *) oldClientData; + Tcl_IncrRefCount(propNamePtr); + *newClientData = propNamePtr; + return TCL_OK; +} + +// Method descriptors +static Tcl_MethodType GetterType = { + TCL_OO_METHOD_VERSION_1, + "PropertyGetter", + Configurable_Getter, + DetailsDeleter, + DetailsCloner +}; + +static Tcl_MethodType SetterType = { + TCL_OO_METHOD_VERSION_1, + "PropertySetter", + Configurable_Setter, + DetailsDeleter, + DetailsCloner +}; + +void +TclOOImplementObjectProperty( + Tcl_Object targetObject, + Tcl_Obj *propNamePtr, + int installGetter, + int installSetter) +{ + if (installGetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_IncrRefCount(methodName); + Tcl_IncrRefCount(propNamePtr); + TclNewInstanceMethod(NULL, targetObject, methodName, 0, &GetterType, propNamePtr); + Tcl_DecrRefCount(methodName); + } + if (installSetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_IncrRefCount(methodName); + Tcl_IncrRefCount(propNamePtr); + TclNewInstanceMethod(NULL, targetObject, methodName, 0, &SetterType, propNamePtr); + Tcl_DecrRefCount(methodName); + } +} + +void +TclOOImplementClassProperty( + Tcl_Class targetClass, + Tcl_Obj *propNamePtr, + int installGetter, + int installSetter) +{ + if (installGetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_IncrRefCount(methodName); + Tcl_IncrRefCount(propNamePtr); + TclNewMethod(NULL, targetClass, methodName, 0, &GetterType, propNamePtr); + Tcl_DecrRefCount(methodName); + } + if (installSetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_IncrRefCount(methodName); + Tcl_IncrRefCount(propNamePtr); + TclNewMethod(NULL, targetClass, methodName, 0, &SetterType, propNamePtr); + Tcl_DecrRefCount(methodName); + } +} + +/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index b35cd13..9f1b5fd 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -569,6 +569,12 @@ MODULE_SCOPE size_t TclOOGetSortedClassMethodList(Class *clsPtr, MODULE_SCOPE int TclOOGetSortedMethodList(Object *oPtr, Object *contextObj, Class *contextCls, int flags, const char ***stringsPtr); +MODULE_SCOPE void TclOOImplementObjectProperty(Tcl_Object targetObject, + Tcl_Obj *propNamePtr, int installGetter, + int installSetter); +MODULE_SCOPE void TclOOImplementClassProperty(Tcl_Class targetObject, + Tcl_Obj *propNamePtr, int installGetter, + int installSetter); MODULE_SCOPE int TclOOInit(Tcl_Interp *interp); MODULE_SCOPE void TclOOInitInfo(Tcl_Interp *interp); MODULE_SCOPE int TclOOInvokeContext(void *clientData, diff --git a/generic/tclVar.c b/generic/tclVar.c index 125091a..490c369 100644 --- a/generic/tclVar.c +++ b/generic/tclVar.c @@ -1277,10 +1277,10 @@ Tcl_Obj * Tcl_ObjGetVar2( Tcl_Interp *interp, /* Command interpreter in which variable is to * be looked up. */ - Tcl_Obj *part1Ptr, /* Points to an object holding the name of an + Tcl_Obj *part1Ptr, /* Points to an object holding the name of an * array (if part2 is non-NULL) or the name of * a variable. */ - Tcl_Obj *part2Ptr, /* If non-null, points to an object holding + Tcl_Obj *part2Ptr, /* If non-null, points to an object holding * the name of an element in the array * part1Ptr. */ int flags) /* OR-ed combination of TCL_GLOBAL_ONLY and @@ -1336,7 +1336,7 @@ TclPtrGetVar( * the name of a variable. */ Tcl_Obj *part2Ptr, /* If non-NULL, gives the name of an element * in the array part1. */ - int flags) /* OR-ed combination of TCL_GLOBAL_ONLY, and + int flags) /* OR-ed combination of TCL_GLOBAL_ONLY, and * TCL_LEAVE_ERR_MSG bits. */ { if (varPtr == NULL) { @@ -1375,14 +1375,14 @@ Tcl_Obj * TclPtrGetVarIdx( Tcl_Interp *interp, /* Command interpreter in which variable is to * be looked up. */ - Var *varPtr, /* The variable to be read.*/ + Var *varPtr, /* The variable to be read.*/ Var *arrayPtr, /* NULL for scalar variables, pointer to the * containing array otherwise. */ Tcl_Obj *part1Ptr, /* Name of an array (if part2 is non-NULL) or * the name of a variable. */ Tcl_Obj *part2Ptr, /* If non-NULL, gives the name of an element * in the array part1. */ - int flags, /* OR-ed combination of TCL_GLOBAL_ONLY, and + int flags, /* OR-ed combination of TCL_GLOBAL_ONLY, and * TCL_LEAVE_ERR_MSG bits. */ int index) /* Index into the local variable table of the * variable, or -1. Only used when part1Ptr is @@ -1483,7 +1483,7 @@ TclPtrGetVarIdx( int Tcl_SetObjCmd( TCL_UNUSED(void *), - Tcl_Interp *interp,/* Current interpreter. */ + Tcl_Interp *interp, /* Current interpreter. */ int objc, /* Number of arguments. */ Tcl_Obj *const objv[]) /* Argument objects. */ { -- cgit v0.12 From 71180305d2a85f9bec0b5eafb9705d4b13f5ba10 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 28 Jan 2024 19:15:28 +0000 Subject: Property implementation 'definitions', not yet plumbed into documented API --- generic/tclOO.c | 4 + generic/tclOOBasic.c | 12 +- generic/tclOODefineCmds.c | 365 +++++++++++++++++++++++----------------------- generic/tclOOInt.h | 1 + 4 files changed, 191 insertions(+), 191 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index 7a57105..7e39858 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -440,6 +440,10 @@ InitFoundation( TclOONewBasicMethod(interp, ((Object *) cfgCls)->classPtr, &cfgMethods[i]); } + Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdObjectProperties", + TclOOInstallStdPropertyImpls, (void *) 1, NULL); + Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdClassProperties", + TclOOInstallStdPropertyImpls, (void *) 0, NULL); /* * Evaluate the remaining definitions, which are a compiled-in Tcl script. diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index f8c9eb5..2b4f220 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1778,19 +1778,19 @@ static Tcl_MethodType SetterType = { void TclOOImplementObjectProperty( Tcl_Object targetObject, - Tcl_Obj *propNamePtr, + Tcl_Obj *propNamePtr, /* Property name, without leading - */ int installGetter, int installSetter) { if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); TclNewInstanceMethod(NULL, targetObject, methodName, 0, &GetterType, propNamePtr); Tcl_DecrRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); TclNewInstanceMethod(NULL, targetObject, methodName, 0, &SetterType, propNamePtr); @@ -1801,19 +1801,19 @@ TclOOImplementObjectProperty( void TclOOImplementClassProperty( Tcl_Class targetClass, - Tcl_Obj *propNamePtr, + Tcl_Obj *propNamePtr, /* Property name, without leading - */ int installGetter, int installSetter) { if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); TclNewMethod(NULL, targetClass, methodName, 0, &GetterType, propNamePtr); Tcl_DecrRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); TclNewMethod(NULL, targetClass, methodName, 0, &SetterType, propNamePtr); diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 7112039..3c4bbdd 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -955,6 +955,23 @@ TclOOGetDefineCmdContext( } return object; } + +static Class * +GetClassDefineCmdContext( + Tcl_Interp *interp) +{ + Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + if (oPtr == NULL) { + return NULL; + } + if (!oPtr->classPtr) { + Tcl_SetObjResult(interp, Tcl_NewStringObj( + "attempt to misuse API", -1)); + Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", NULL); + return NULL; + } + return oPtr->classPtr; +} /* * ---------------------------------------------------------------------- @@ -1585,27 +1602,17 @@ TclOODefineConstructorObjCmd( int objc, Tcl_Obj *const *objv) { - Object *oPtr; - Class *clsPtr; + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Method method; Tcl_Size bodyLength; - if (objc != 3) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, "arguments body"); return TCL_ERROR; } - /* - * Extract and validate the context, which is the class that we wish to - * modify. - */ - - oPtr = (Object *) TclOOGetDefineCmdContext(interp); - if (oPtr == NULL) { - return TCL_ERROR; - } - clsPtr = oPtr->classPtr; - (void)Tcl_GetStringFromObj(objv[2], &bodyLength); if (bodyLength > 0) { /* @@ -1660,21 +1667,13 @@ TclOODefineDefnNsObjCmd( NULL }; int kind = 0; - Object *oPtr; + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Namespace *nsPtr; Tcl_Obj *nsNamePtr, **storagePtr; - oPtr = (Object *) TclOOGetDefineCmdContext(interp); - if (oPtr == NULL) { - return TCL_ERROR; - } - if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); + if (clsPtr == NULL) { return TCL_ERROR; - } - if (oPtr->flags & (ROOT_OBJECT | ROOT_CLASS)) { + } else if (clsPtr->thisPtr->flags & (ROOT_OBJECT | ROOT_CLASS)) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "may not modify the definition namespace of the root classes", -1)); @@ -1710,9 +1709,9 @@ TclOODefineDefnNsObjCmd( */ if (kind) { - storagePtr = &oPtr->classPtr->objDefinitionNs; + storagePtr = &clsPtr->objDefinitionNs; } else { - storagePtr = &oPtr->classPtr->clsDefinitionNs; + storagePtr = &clsPtr->clsDefinitionNs; } if (*storagePtr != NULL) { Tcl_DecrRefCount(*storagePtr); @@ -1796,21 +1795,17 @@ TclOODefineDestructorObjCmd( int objc, Tcl_Obj *const *objv) { - Object *oPtr; - Class *clsPtr; Tcl_Method method; Tcl_Size bodyLength; + Class *clsPtr = GetClassDefineCmdContext(interp); - if (objc != 2) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "body"); return TCL_ERROR; } - oPtr = (Object *) TclOOGetDefineCmdContext(interp); - if (oPtr == NULL) { - return TCL_ERROR; - } - clsPtr = oPtr->classPtr; (void)Tcl_GetStringFromObj(objv[1], &bodyLength); if (bodyLength > 0) { @@ -2376,26 +2371,20 @@ ClassFilterGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj, *filterObj; Tcl_Size i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); - FOREACH(filterObj, oPtr->classPtr->filters) { + FOREACH(filterObj, clsPtr->filters) { Tcl_ListObjAppendElement(NULL, resultObj, filterObj); } Tcl_SetObjResult(interp, resultObj); @@ -2410,30 +2399,25 @@ ClassFilterSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size filterc; Tcl_Obj **filterv; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "filterList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (TclListObjGetElementsM(interp, objv[0], &filterc, + if (TclListObjGetElementsM(interp, objv[0], &filterc, &filterv) != TCL_OK) { return TCL_ERROR; } - TclOOClassSetFilters(interp, oPtr->classPtr, filterc, filterv); + TclOOClassSetFilters(interp, clsPtr, filterc, filterv); return TCL_OK; } @@ -2456,27 +2440,21 @@ ClassMixinGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Class *mixinPtr; Tcl_Size i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); - FOREACH(mixinPtr, oPtr->classPtr->mixins) { + FOREACH(mixinPtr, clsPtr->mixins) { Tcl_ListObjAppendElement(NULL, resultObj, TclOOObjectName(interp, mixinPtr->thisPtr)); } @@ -2493,7 +2471,7 @@ ClassMixinSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size mixinc, i; Tcl_Obj **mixinv; Class **mixins; /* The references to the classes to actually @@ -2503,21 +2481,16 @@ ClassMixinSet( * values and keys are always pointers. */ int isNew; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "mixinList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (TclListObjGetElementsM(interp, objv[0], &mixinc, + if (TclListObjGetElementsM(interp, objv[0], &mixinc, &mixinv) != TCL_OK) { return TCL_ERROR; } @@ -2539,7 +2512,7 @@ ClassMixinSet( Tcl_SetErrorCode(interp, "TCL", "OO", "REPETITIOUS",NULL); goto freeAndError; } - if (TclOOIsReachable(oPtr->classPtr, mixins[i])) { + if (TclOOIsReachable(clsPtr, mixins[i])) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "may not mix a class into itself", -1)); Tcl_SetErrorCode(interp, "TCL", "OO", "SELF_MIXIN", (void *)NULL); @@ -2547,7 +2520,7 @@ ClassMixinSet( } } - TclOOClassSetMixins(interp, oPtr->classPtr, mixinc, mixins); + TclOOClassSetMixins(interp, clsPtr, mixinc, mixins); Tcl_DeleteHashTable(&uniqueCheck); TclStackFree(interp, mixins); return TCL_OK; @@ -2577,27 +2550,21 @@ ClassSuperGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Class *superPtr; Tcl_Size i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); - FOREACH(superPtr, oPtr->classPtr->superclasses) { + FOREACH(superPtr, clsPtr->superclasses) { Tcl_ListObjAppendElement(NULL, resultObj, TclOOObjectName(interp, superPtr->thisPtr)); } @@ -2613,27 +2580,23 @@ ClassSuperSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size superc, j; Tcl_Size i; Tcl_Obj **superv; Class **superclasses, *superPtr; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "superclassList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (oPtr == oPtr->fPtr->objectCls->thisPtr) { + Foundation *fPtr = clsPtr->thisPtr->fPtr; + if (clsPtr == fPtr->objectCls) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "may not modify the superclass of the root object", -1)); Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); @@ -2658,10 +2621,10 @@ ClassSuperSet( if (superc == 0) { superclasses = (Class **)Tcl_Realloc(superclasses, sizeof(Class *)); - if (TclOOIsReachable(oPtr->fPtr->classCls, oPtr->classPtr)) { - superclasses[0] = oPtr->fPtr->classCls; + if (TclOOIsReachable(fPtr->classCls, clsPtr)) { + superclasses[0] = fPtr->classCls; } else { - superclasses[0] = oPtr->fPtr->objectCls; + superclasses[0] = fPtr->objectCls; } superc = 1; AddRef(superclasses[0]->thisPtr); @@ -2681,7 +2644,7 @@ ClassSuperSet( goto failedAfterAlloc; } } - if (TclOOIsReachable(oPtr->classPtr, superclasses[i])) { + if (TclOOIsReachable(clsPtr, superclasses[i])) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "attempt to form circular dependency graph", -1)); Tcl_SetErrorCode(interp, "TCL", "OO", "CIRCULARITY", (void *)NULL); @@ -2709,19 +2672,19 @@ ClassSuperSet( * subclass list. */ - if (oPtr->classPtr->superclasses.num != 0) { - FOREACH(superPtr, oPtr->classPtr->superclasses) { - TclOORemoveFromSubclasses(oPtr->classPtr, superPtr); + if (clsPtr->superclasses.num != 0) { + FOREACH(superPtr, clsPtr->superclasses) { + TclOORemoveFromSubclasses(clsPtr, superPtr); TclOODecrRefCount(superPtr->thisPtr); } - Tcl_Free(oPtr->classPtr->superclasses.list); + Tcl_Free(clsPtr->superclasses.list); } - oPtr->classPtr->superclasses.list = superclasses; - oPtr->classPtr->superclasses.num = superc; - FOREACH(superPtr, oPtr->classPtr->superclasses) { - TclOOAddToSubclasses(oPtr->classPtr, superPtr); + clsPtr->superclasses.list = superclasses; + clsPtr->superclasses.num = superc; + FOREACH(superPtr, clsPtr->superclasses) { + TclOOAddToSubclasses(clsPtr, superPtr); } - BumpGlobalEpoch(interp, oPtr->classPtr); + BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -2745,35 +2708,29 @@ ClassVarsGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Tcl_Size i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); if (IsPrivateDefine(interp)) { PrivateVariableMapping *privatePtr; - FOREACH_STRUCT(privatePtr, oPtr->classPtr->privateVariables) { + FOREACH_STRUCT(privatePtr, clsPtr->privateVariables) { Tcl_ListObjAppendElement(NULL, resultObj, privatePtr->variableObj); } } else { Tcl_Obj *variableObj; - FOREACH(variableObj, oPtr->classPtr->variables) { + FOREACH(variableObj, clsPtr->variables) { Tcl_ListObjAppendElement(NULL, resultObj, variableObj); } } @@ -2789,26 +2746,21 @@ ClassVarsSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size i; Tcl_Size varc; Tcl_Obj **varv; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "filterList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (TclListObjGetElementsM(interp, objv[0], &varc, + if (TclListObjGetElementsM(interp, objv[0], &varc, &varv) != TCL_OK) { return TCL_ERROR; } @@ -2833,10 +2785,10 @@ ClassVarsSet( } if (IsPrivateDefine(interp)) { - InstallPrivateVariableMapping(&oPtr->classPtr->privateVariables, - varc, varv, oPtr->classPtr->thisPtr->creationEpoch); + InstallPrivateVariableMapping(&clsPtr->privateVariables, + varc, varv, clsPtr->thisPtr->creationEpoch); } else { - InstallStandardVariableMapping(&oPtr->classPtr->variables, varc, varv); + InstallStandardVariableMapping(&clsPtr->variables, varc, varv); } return TCL_OK; } @@ -3243,26 +3195,20 @@ ClassRPropsGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj, *propNameObj; int i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); - FOREACH(propNameObj, oPtr->classPtr->properties.readable) { + FOREACH(propNameObj, clsPtr->properties.readable) { Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); } Tcl_SetObjResult(interp, resultObj); @@ -3277,31 +3223,26 @@ ClassRPropsSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size varc; Tcl_Obj **varv; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "filterList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (Tcl_ListObjGetElements(interp, objv[0], &varc, + if (Tcl_ListObjGetElements(interp, objv[0], &varc, &varv) != TCL_OK) { return TCL_ERROR; } - InstallReadableProps(&oPtr->classPtr->properties, varc, varv); - BumpGlobalEpoch(interp, oPtr->classPtr); + InstallReadableProps(&clsPtr->properties, varc, varv); + BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3440,26 +3381,20 @@ ClassWPropsGet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Obj *resultObj, *propNameObj; int i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } TclNewObj(resultObj); - FOREACH(propNameObj, oPtr->classPtr->properties.writable) { + FOREACH(propNameObj, clsPtr->properties.writable) { Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); } Tcl_SetObjResult(interp, resultObj); @@ -3474,31 +3409,26 @@ ClassWPropsSet( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + Class *clsPtr = GetClassDefineCmdContext(interp); Tcl_Size varc; Tcl_Obj **varv; - if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + if (clsPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, "propertyList"); return TCL_ERROR; } objv += Tcl_ObjectContextSkippedArgs(context); - if (oPtr == NULL) { - return TCL_ERROR; - } else if (!oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (void *)NULL); - return TCL_ERROR; - } else if (Tcl_ListObjGetElements(interp, objv[0], &varc, + if (Tcl_ListObjGetElements(interp, objv[0], &varc, &varv) != TCL_OK) { return TCL_ERROR; } - InstallWritableProps(&oPtr->classPtr->properties, varc, varv); - BumpGlobalEpoch(interp, oPtr->classPtr); + InstallWritableProps(&clsPtr->properties, varc, varv); + BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3561,6 +3491,71 @@ ObjWPropsSet( return TCL_OK; } +int +TclOOInstallStdPropertyImpls( + void *useInstance, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const *objv) +{ + int readable, writable; + Tcl_Obj *propName; + const char *name, *reason; + Tcl_Size len; + char flag = TCL_DONT_QUOTE_HASH; + + if (objc != 4) { + Tcl_WrongNumArgs(interp, 1, objv, "propName readable writable"); + return TCL_ERROR; + } + propName = objv[1]; + name = Tcl_GetStringFromObj(propName, &len); + if (Tcl_StringMatch("-*", name)) { + reason = "must not begin with -"; + goto badProp; + } + if (TclScanElement(name, len, &flag) != len) { + reason = "must be a simple word"; + goto badProp; + } + if (Tcl_StringMatch("*::*", name)) { + reason = "must not contain namespace separators"; + goto badProp; + } + if (Tcl_StringMatch("*[()]*", name)) { + reason = "must not contain parentheses"; + goto badProp; + } + if (Tcl_GetBooleanFromObj(interp, objv[2], &readable) != TCL_OK) { + return TCL_ERROR; + } + if (Tcl_GetBooleanFromObj(interp, objv[3], &writable) != TCL_OK) { + return TCL_ERROR; + } + + if (useInstance) { + Tcl_Object object = TclOOGetDefineCmdContext(interp); + if (!object) { + return TCL_ERROR; + } + TclOOImplementObjectProperty(object, propName, readable, writable); + } else { + Tcl_Class cls = (Tcl_Class) GetClassDefineCmdContext(interp); + if (!cls) { + return TCL_ERROR; + } + TclOOImplementClassProperty(cls, propName, readable, writable); + } + return TCL_OK; + + badProp: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "bad property name \"%s\": %s", name, reason)); + Tcl_SetErrorCode(interp, "TCLOO", "PROPERTY_FORMAT", NULL); + return TCL_ERROR; +} + + /* * Local Variables: * mode: c diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 9f1b5fd..f4d1b45 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -488,6 +488,7 @@ MODULE_SCOPE Tcl_ObjCmdProc TclOODefineClassObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineObjSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefinePrivateObjCmd; +MODULE_SCOPE Tcl_ObjCmdProc TclOOInstallStdPropertyImpls; MODULE_SCOPE Tcl_ObjCmdProc TclOOUnknownDefinition; MODULE_SCOPE Tcl_ObjCmdProc TclOOCopyObjectCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOONextObjCmd; -- cgit v0.12 From 025b1d2b0807cab028100573e9d14d8e23bb8ba6 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 28 Jan 2024 21:56:08 +0000 Subject: Plug implementation in... and fix the silly bugs --- generic/tclOODefineCmds.c | 6 +++--- generic/tclOOScript.h | 45 +++++++++++++++++-------------------------- tools/makeHeader.tcl | 1 + tools/tclOOScript.tcl | 49 +++++++++++++++++++---------------------------- 4 files changed, 42 insertions(+), 59 deletions(-) diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 3c4bbdd..86503c6 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -3510,7 +3510,7 @@ TclOOInstallStdPropertyImpls( } propName = objv[1]; name = Tcl_GetStringFromObj(propName, &len); - if (Tcl_StringMatch("-*", name)) { + if (Tcl_StringMatch(name, "-*")) { reason = "must not begin with -"; goto badProp; } @@ -3518,11 +3518,11 @@ TclOOInstallStdPropertyImpls( reason = "must be a simple word"; goto badProp; } - if (Tcl_StringMatch("*::*", name)) { + if (Tcl_StringMatch(name, "*::*")) { reason = "must not contain namespace separators"; goto badProp; } - if (Tcl_StringMatch("*[()]*", name)) { + if (Tcl_StringMatch(name, "*[()]*")) { reason = "must not contain parentheses"; goto badProp; } diff --git a/generic/tclOOScript.h b/generic/tclOOScript.h index 1903b49..c6a60a6 100644 --- a/generic/tclOOScript.h +++ b/generic/tclOOScript.h @@ -260,32 +260,11 @@ static const char *tclOOSetupScript = "\t}\n" "\t::namespace eval configuresupport {\n" "\t\tnamespace path ::tcl\n" -"\t\tproc PropertyImpl {readslot writeslot args} {\n" +"\t\tproc PropertyImpl {stdInstaller readslot writeslot args} {\n" "\t\t\tfor {set i 0} {$i < [llength $args]} {incr i} {\n" "\t\t\t\tset prop [lindex $args $i]\n" -"\t\t\t\tif {[string match \"-*\" $prop]} {\n" -"\t\t\t\t\treturn -code error -level 2 \\\n" -"\t\t\t\t\t\t-errorcode {TCLOO PROPERTY_FORMAT} \\\n" -"\t\t\t\t\t\t\"bad property name \\\"$prop\\\": must not begin with -\"\n" -"\t\t\t\t}\n" -"\t\t\t\tif {$prop ne [list $prop]} {\n" -"\t\t\t\t\treturn -code error -level 2 \\\n" -"\t\t\t\t\t\t-errorcode {TCLOO PROPERTY_FORMAT} \\\n" -"\t\t\t\t\t\t\"bad property name \\\"$prop\\\": must be a simple word\"\n" -"\t\t\t\t}\n" -"\t\t\t\tif {[string first \"::\" $prop] != -1} {\n" -"\t\t\t\t\treturn -code error -level 2 \\\n" -"\t\t\t\t\t\t-errorcode {TCLOO PROPERTY_FORMAT} \\\n" -"\t\t\t\t\t\t\"bad property name \\\"$prop\\\": must not contain namespace separators\"\n" -"\t\t\t\t}\n" -"\t\t\t\tif {[string match {*[()]*} $prop]} {\n" -"\t\t\t\t\treturn -code error -level 2 \\\n" -"\t\t\t\t\t\t-errorcode {TCLOO PROPERTY_FORMAT} \\\n" -"\t\t\t\t\t\t\"bad property name \\\"$prop\\\": must not contain parentheses\"\n" -"\t\t\t\t}\n" "\t\t\t\tset realprop [string cat \"-\" $prop]\n" -"\t\t\t\tset getter [format {::set [my varname %s]} $prop]\n" -"\t\t\t\tset setter [format {::set [my varname %s] $value} $prop]\n" +"\t\t\t\tunset -nocomplain getter setter\n" "\t\t\t\tset kind readwrite\n" "\t\t\t\twhile {[set next [lindex $args [expr {$i + 1}]]\n" "\t\t\t\t\t\tstring match \"-*\" $next]} {\n" @@ -324,29 +303,40 @@ static const char *tclOOSetupScript = "\t\t\t\t}\n" "\t\t\t\tset reader \n" "\t\t\t\tset writer \n" +"\t\t\t\tset addReader [expr {$kind ne \"writable\" && ![info exist getter]}]\n" +"\t\t\t\tset addWriter [expr {$kind ne \"readable\" && ![info exist setter]}]\n" +"\t\t\t\ttry {\n" +"\t\t\t\t\tuplevel 2 [list $stdInstaller $prop $addReader $addWriter]\n" +"\t\t\t\t} on error {msg opt} {\n" +"\t\t\t\t\treturn -code error -level 2 \\\n" +"\t\t\t\t\t\t\t-errorcode [dict get $opt -errorcode] $msg\n" +"\t\t\t\t}\n" "\t\t\t\tswitch $kind {\n" "\t\t\t\t\treadable {\n" "\t\t\t\t\t\tuplevel 2 [list $readslot -append $realprop]\n" "\t\t\t\t\t\tuplevel 2 [list $writeslot -remove $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list method $reader -unexport {} $getter]\n" "\t\t\t\t\t}\n" "\t\t\t\t\twritable {\n" "\t\t\t\t\t\tuplevel 2 [list $readslot -remove $realprop]\n" "\t\t\t\t\t\tuplevel 2 [list $writeslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list method $writer -unexport {value} $setter]\n" "\t\t\t\t\t}\n" "\t\t\t\t\treadwrite {\n" "\t\t\t\t\t\tuplevel 2 [list $readslot -append $realprop]\n" "\t\t\t\t\t\tuplevel 2 [list $writeslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list method $reader -unexport {} $getter]\n" -"\t\t\t\t\t\tuplevel 2 [list method $writer -unexport {value} $setter]\n" "\t\t\t\t\t}\n" "\t\t\t\t}\n" +"\t\t\t\tif {[info exist getter]} {\n" +"\t\t\t\t\tuplevel 2 [list method $reader -unexport {} $getter]\n" +"\t\t\t\t}\n" +"\t\t\t\tif {[info exist setter]} {\n" +"\t\t\t\t\tuplevel 2 [list method $writer -unexport {value} $setter]\n" +"\t\t\t\t}\n" "\t\t\t}\n" "\t\t}\n" "\t\tnamespace eval configurableclass {\n" "\t\t\t::proc property args {\n" "\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" +"\t\t\t\t\t::oo::configuresupport::StdClassProperties \\\n" "\t\t\t\t\t::oo::configuresupport::readableproperties \\\n" "\t\t\t\t\t::oo::configuresupport::writableproperties {*}$args\n" "\t\t\t}\n" @@ -357,6 +347,7 @@ static const char *tclOOSetupScript = "\t\tnamespace eval configurableobject {\n" "\t\t\t::proc property args {\n" "\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" +"\t\t\t\t\t::oo::configuresupport::StdObjectProperties \\\n" "\t\t\t\t\t::oo::configuresupport::objreadableproperties \\\n" "\t\t\t\t\t::oo::configuresupport::objwritableproperties {*}$args\n" "\t\t\t}\n" diff --git a/tools/makeHeader.tcl b/tools/makeHeader.tcl index 17526e0..e20e336 100644 --- a/tools/makeHeader.tcl +++ b/tools/makeHeader.tcl @@ -108,6 +108,7 @@ namespace eval makeHeader { proc updateTemplateFile {headerFile scriptLines} { set f [open $headerFile "r+"] try { + chan configure $f -translation {auto lf} set content [split [chan read -nonewline $f] "\n"] updateTemplate content [stripSurround $scriptLines] chan seek $f 0 diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 2843dff..cbceb39 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -486,36 +486,15 @@ # # ------------------------------------------------------------------ - proc PropertyImpl {readslot writeslot args} { + proc PropertyImpl {stdInstaller readslot writeslot args} { for {set i 0} {$i < [llength $args]} {incr i} { # Parse the property name set prop [lindex $args $i] - if {[string match "-*" $prop]} { - return -code error -level 2 \ - -errorcode {TCLOO PROPERTY_FORMAT} \ - "bad property name \"$prop\": must not begin with -" - } - if {$prop ne [list $prop]} { - return -code error -level 2 \ - -errorcode {TCLOO PROPERTY_FORMAT} \ - "bad property name \"$prop\": must be a simple word" - } - if {[string first "::" $prop] != -1} { - return -code error -level 2 \ - -errorcode {TCLOO PROPERTY_FORMAT} \ - "bad property name \"$prop\": must not contain namespace separators" - } - if {[string match {*[()]*} $prop]} { - return -code error -level 2 \ - -errorcode {TCLOO PROPERTY_FORMAT} \ - "bad property name \"$prop\": must not contain parentheses" - } set realprop [string cat "-" $prop] - set getter [format {::set [my varname %s]} $prop] - set setter [format {::set [my varname %s] $value} $prop] + unset -nocomplain getter setter set kind readwrite - # Parse the extra options + # Parse the extra options for the property while {[set next [lindex $args [expr {$i + 1}]] string match "-*" $next]} { set arg [lindex $args [incr i 2]] @@ -552,27 +531,37 @@ } } - # Install the option + # Install the property set reader set writer + set addReader [expr {$kind ne "writable" && ![info exist getter]}] + set addWriter [expr {$kind ne "readable" && ![info exist setter]}] + try { + uplevel 2 [list $stdInstaller $prop $addReader $addWriter] + } on error {msg opt} { + return -code error -level 2 \ + -errorcode [dict get $opt -errorcode] $msg + } switch $kind { readable { uplevel 2 [list $readslot -append $realprop] uplevel 2 [list $writeslot -remove $realprop] - uplevel 2 [list method $reader -unexport {} $getter] } writable { uplevel 2 [list $readslot -remove $realprop] uplevel 2 [list $writeslot -append $realprop] - uplevel 2 [list method $writer -unexport {value} $setter] } readwrite { uplevel 2 [list $readslot -append $realprop] uplevel 2 [list $writeslot -append $realprop] - uplevel 2 [list method $reader -unexport {} $getter] - uplevel 2 [list method $writer -unexport {value} $setter] } } + if {[info exist getter]} { + uplevel 2 [list method $reader -unexport {} $getter] + } + if {[info exist setter]} { + uplevel 2 [list method $writer -unexport {value} $setter] + } } } @@ -589,6 +578,7 @@ namespace eval configurableclass { ::proc property args { ::oo::configuresupport::PropertyImpl \ + ::oo::configuresupport::StdClassProperties \ ::oo::configuresupport::readableproperties \ ::oo::configuresupport::writableproperties {*}$args } @@ -601,6 +591,7 @@ namespace eval configurableobject { ::proc property args { ::oo::configuresupport::PropertyImpl \ + ::oo::configuresupport::StdObjectProperties \ ::oo::configuresupport::objreadableproperties \ ::oo::configuresupport::objwritableproperties {*}$args } -- cgit v0.12 From 254a7ffd9e9012f7b59121e6d6679ce2e661800f Mon Sep 17 00:00:00 2001 From: dkf Date: Mon, 29 Jan 2024 20:48:15 +0000 Subject: Add doc comments --- generic/tclOOBasic.c | 48 +++++++++++++++++++++++++++++------------------ generic/tclOODefineCmds.c | 28 ++++++++++++++++++++++++++- generic/tclOOInt.h | 2 +- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 2b4f220..2732036 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1777,46 +1777,58 @@ static Tcl_MethodType SetterType = { void TclOOImplementObjectProperty( - Tcl_Object targetObject, + Tcl_Object targetObject, /* What to install into. */ Tcl_Obj *propNamePtr, /* Property name, without leading - */ - int installGetter, - int installSetter) + int installGetter, /* Whether to install a standard getter. */ + int installSetter) /* Whether to install a standard setter. */ { if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + // Don't know if TclNewInstanceMethod will retain a ref to the method name Tcl_IncrRefCount(methodName); - Tcl_IncrRefCount(propNamePtr); - TclNewInstanceMethod(NULL, targetObject, methodName, 0, &GetterType, propNamePtr); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewInstanceMethod( + NULL, targetObject, methodName, 0, &GetterType, propNamePtr); Tcl_DecrRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + // Don't know if TclNewInstanceMethod will retain a ref to the method name Tcl_IncrRefCount(methodName); - Tcl_IncrRefCount(propNamePtr); - TclNewInstanceMethod(NULL, targetObject, methodName, 0, &SetterType, propNamePtr); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewInstanceMethod( + NULL, targetObject, methodName, 0, &SetterType, propNamePtr); Tcl_DecrRefCount(methodName); } } void TclOOImplementClassProperty( - Tcl_Class targetClass, + Tcl_Class targetClass, /* What to install into. */ Tcl_Obj *propNamePtr, /* Property name, without leading - */ - int installGetter, - int installSetter) + int installGetter, /* Whether to install a standard getter. */ + int installSetter) /* Whether to install a standard setter. */ { if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + // Don't know if TclNewMethod will retain a ref to the method name Tcl_IncrRefCount(methodName); - Tcl_IncrRefCount(propNamePtr); - TclNewMethod(NULL, targetClass, methodName, 0, &GetterType, propNamePtr); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewMethod( + NULL, targetClass, methodName, 0, &GetterType, propNamePtr); Tcl_DecrRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf("", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + // Don't know if TclNewMethod will retain a ref to the method name Tcl_IncrRefCount(methodName); - Tcl_IncrRefCount(propNamePtr); - TclNewMethod(NULL, targetClass, methodName, 0, &SetterType, propNamePtr); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewMethod( + NULL, targetClass, methodName, 0, &SetterType, propNamePtr); Tcl_DecrRefCount(methodName); } } diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 86503c6..1ab099f 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -3491,6 +3491,21 @@ ObjWPropsSet( return TCL_OK; } +/* + * ---------------------------------------------------------------------- + * + * TclOOInstallStdPropertyImpls -- + * + * Implementations of the "StdClassProperties" hidden definition for + * classes and the "StdObjectProperties" hidden definition for + * instances. Both are located in the ::oo::configuresupport namespace. + * + * Validates a (dashless) property name, and installs implementation + * methods if asked to do so (readable and writable flags). + * + * ---------------------------------------------------------------------- + */ + int TclOOInstallStdPropertyImpls( void *useInstance, @@ -3504,6 +3519,14 @@ TclOOInstallStdPropertyImpls( Tcl_Size len; char flag = TCL_DONT_QUOTE_HASH; + /* + * Parse the arguments and validate the property name. Note that just + * calling TclScanElement() is cheaper than actually formatting a list + * and comparing the string version of that with the original, as + * TclScanElement() is one of the core parts of doing that; this skips + * a whole load of irrelevant memory allocations! + */ + if (objc != 4) { Tcl_WrongNumArgs(interp, 1, objv, "propName readable writable"); return TCL_ERROR; @@ -3533,6 +3556,10 @@ TclOOInstallStdPropertyImpls( return TCL_ERROR; } + /* + * Install the implementations... if asked to do so. + */ + if (useInstance) { Tcl_Object object = TclOOGetDefineCmdContext(interp); if (!object) { @@ -3554,7 +3581,6 @@ TclOOInstallStdPropertyImpls( Tcl_SetErrorCode(interp, "TCLOO", "PROPERTY_FORMAT", NULL); return TCL_ERROR; } - /* * Local Variables: diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index f4d1b45..d0f9858 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -47,7 +47,7 @@ typedef struct Method { * special flag record which is just used for * the setting of the flags field. */ Tcl_Size refCount; - void *clientData; /* Type-specific data. */ + void *clientData; /* Type-specific data. */ Tcl_Obj *namePtr; /* Name of the method. */ struct Object *declaringObjectPtr; /* The object that declares this method, or -- cgit v0.12 From 14aa907da2feb65059a73ffd3aafc0677381a0ee Mon Sep 17 00:00:00 2001 From: dkf Date: Thu, 18 Jul 2024 15:38:06 +0000 Subject: Fix small bug --- generic/tclOOBasic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 9ab801b..34cc272 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -858,7 +858,7 @@ TclOO_Object_VarName( /* * The variable reference must not disappear too soon. [Bug 74b6110204] */ - TclSetVarNamespaceVar(varPtr); + TclSetVarNamespaceVar((Var *) varPtr); /* * Now that we've pinned down what variable we're really talking about -- cgit v0.12 From 0233d18558901f93d39710f9d68357b0e9bd891d Mon Sep 17 00:00:00 2001 From: dkf Date: Thu, 18 Jul 2024 15:43:48 +0000 Subject: Correction to error code --- generic/tclOODefineCmds.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 77835e9..9e4f879 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -3571,7 +3571,7 @@ TclOOInstallStdPropertyImpls( badProp: Tcl_SetObjResult(interp, Tcl_ObjPrintf( "bad property name \"%s\": %s", name, reason)); - Tcl_SetErrorCode(interp, "TCLOO", "PROPERTY_FORMAT", NULL); + Tcl_SetErrorCode(interp, "TCL", "OO", "PROPERTY_FORMAT", NULL); return TCL_ERROR; } -- cgit v0.12 From 47f3f735713593f0bb95d3197fb3053cad00a3ba Mon Sep 17 00:00:00 2001 From: dkf Date: Thu, 18 Jul 2024 16:03:53 +0000 Subject: Improve some comments --- tools/tclOOScript.tcl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 6b44ba7..3e80981 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -480,9 +480,16 @@ # ------------------------------------------------------------------ # - # oo::configuresupport -- + # oo::configuresupport::PropertyImpl -- # - # A metaclass that is used to make classes that can be configured. + # The implementation of the [property] configuration command. + # This assumes there are two stack frames above it to care about, + # first the hull [property] command (in the correct context for + # what is being confgured) and then the context scope of + # [oo::define] or [oo::objdefine]. + # + # TODO: + # Convert to C code. # # ------------------------------------------------------------------ -- cgit v0.12 From 5cb0d0d08cecd336d707cefe07834baf85d6887f Mon Sep 17 00:00:00 2001 From: dkf Date: Fri, 19 Jul 2024 08:52:07 +0000 Subject: Slightly simpler script --- generic/tclOOScript.h | 44 ++++++++++++++++++++++---------------------- tools/tclOOScript.tcl | 44 ++++++++++++++++++++++---------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/generic/tclOOScript.h b/generic/tclOOScript.h index 374c11d..7538d48 100644 --- a/generic/tclOOScript.h +++ b/generic/tclOOScript.h @@ -269,11 +269,11 @@ static const char *tclOOSetupScript = "\t\t\t\twhile {[set next [lindex $args [expr {$i + 1}]]\n" "\t\t\t\t\t\tstring match \"-*\" $next]} {\n" "\t\t\t\t\tset arg [lindex $args [incr i 2]]\n" -"\t\t\t\t\tswitch [prefix match -error [list -level 2 -errorcode \\\n" +"\t\t\t\t\tswitch [prefix match -error [list -level 1 -errorcode \\\n" "\t\t\t\t\t\t\t[list TCL LOOKUP INDEX option $next]] {-get -kind -set} $next] {\n" "\t\t\t\t\t\t-get {\n" "\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 2 \\\n" +"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" "\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" "\t\t\t\t\t\t\t\t\t\"missing body to go with -get option\"\n" "\t\t\t\t\t\t\t}\n" @@ -281,7 +281,7 @@ static const char *tclOOSetupScript = "\t\t\t\t\t\t}\n" "\t\t\t\t\t\t-set {\n" "\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 2 \\\n" +"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" "\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" "\t\t\t\t\t\t\t\t\t\"missing body to go with -set option\"\n" "\t\t\t\t\t\t\t}\n" @@ -289,7 +289,7 @@ static const char *tclOOSetupScript = "\t\t\t\t\t\t}\n" "\t\t\t\t\t\t-kind {\n" "\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 2\\\n" +"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" "\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" "\t\t\t\t\t\t\t\t\t\"missing kind value to go with -kind option\"\n" "\t\t\t\t\t\t\t}\n" @@ -306,51 +306,51 @@ static const char *tclOOSetupScript = "\t\t\t\tset addReader [expr {$kind ne \"writable\" && ![info exist getter]}]\n" "\t\t\t\tset addWriter [expr {$kind ne \"readable\" && ![info exist setter]}]\n" "\t\t\t\ttry {\n" -"\t\t\t\t\tuplevel 2 [list $stdInstaller $prop $addReader $addWriter]\n" +"\t\t\t\t\tuplevel 1 [list $stdInstaller $prop $addReader $addWriter]\n" "\t\t\t\t} on error {msg opt} {\n" -"\t\t\t\t\treturn -code error -level 2 \\\n" +"\t\t\t\t\treturn -code error -level 1 \\\n" "\t\t\t\t\t\t\t-errorcode [dict get $opt -errorcode] $msg\n" "\t\t\t\t}\n" "\t\t\t\tswitch $kind {\n" "\t\t\t\t\treadable {\n" -"\t\t\t\t\t\tuplevel 2 [list $readslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list $writeslot -remove $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $readslot -append $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $writeslot -remove $realprop]\n" "\t\t\t\t\t}\n" "\t\t\t\t\twritable {\n" -"\t\t\t\t\t\tuplevel 2 [list $readslot -remove $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list $writeslot -append $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $readslot -remove $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $writeslot -append $realprop]\n" "\t\t\t\t\t}\n" "\t\t\t\t\treadwrite {\n" -"\t\t\t\t\t\tuplevel 2 [list $readslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 2 [list $writeslot -append $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $readslot -append $realprop]\n" +"\t\t\t\t\t\tuplevel 1 [list $writeslot -append $realprop]\n" "\t\t\t\t\t}\n" "\t\t\t\t}\n" "\t\t\t\tif {[info exist getter]} {\n" -"\t\t\t\t\tuplevel 2 [list method $reader -unexport {} $getter]\n" +"\t\t\t\t\tuplevel 1 [list method $reader -unexport {} $getter]\n" "\t\t\t\t}\n" "\t\t\t\tif {[info exist setter]} {\n" -"\t\t\t\t\tuplevel 2 [list method $writer -unexport {value} $setter]\n" +"\t\t\t\t\tuplevel 1 [list method $writer -unexport {value} $setter]\n" "\t\t\t\t}\n" "\t\t\t}\n" "\t\t}\n" "\t\tnamespace eval configurableclass {\n" -"\t\t\t::proc property args {\n" -"\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" +"\t\t\t::interp alias \\\n" +"\t\t\t\t\t{} ::oo::configuresupport::configurableclass::property {} \\\n" +"\t\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" "\t\t\t\t\t::oo::configuresupport::StdClassProperties \\\n" "\t\t\t\t\t::oo::configuresupport::readableproperties \\\n" -"\t\t\t\t\t::oo::configuresupport::writableproperties {*}$args\n" -"\t\t\t}\n" +"\t\t\t\t\t::oo::configuresupport::writableproperties\n" "\t\t\t::proc properties args {::tailcall property {*}$args}\n" "\t\t\t::namespace path ::oo::define\n" "\t\t\t::namespace export property\n" "\t\t}\n" "\t\tnamespace eval configurableobject {\n" -"\t\t\t::proc property args {\n" -"\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" +"\t\t\t::interp alias \\\n" +"\t\t\t\t\t{} ::oo::configuresupport::configurableobject::property {} \\\n" +"\t\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" "\t\t\t\t\t::oo::configuresupport::StdObjectProperties \\\n" "\t\t\t\t\t::oo::configuresupport::objreadableproperties \\\n" -"\t\t\t\t\t::oo::configuresupport::objwritableproperties {*}$args\n" -"\t\t\t}\n" +"\t\t\t\t\t::oo::configuresupport::objwritableproperties\n" "\t\t\t::proc properties args {::tailcall property {*}$args}\n" "\t\t\t::namespace path ::oo::objdefine\n" "\t\t\t::namespace export property\n" diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 3e80981..95ffbde 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -505,11 +505,11 @@ while {[set next [lindex $args [expr {$i + 1}]] string match "-*" $next]} { set arg [lindex $args [incr i 2]] - switch [prefix match -error [list -level 2 -errorcode \ + switch [prefix match -error [list -level 1 -errorcode \ [list TCL LOOKUP INDEX option $next]] {-get -kind -set} $next] { -get { if {$i >= [llength $args]} { - return -code error -level 2 \ + return -code error -level 1 \ -errorcode {TCL WRONGARGS} \ "missing body to go with -get option" } @@ -517,7 +517,7 @@ } -set { if {$i >= [llength $args]} { - return -code error -level 2 \ + return -code error -level 1 \ -errorcode {TCL WRONGARGS} \ "missing body to go with -set option" } @@ -525,7 +525,7 @@ } -kind { if {$i >= [llength $args]} { - return -code error -level 2\ + return -code error -level 1 \ -errorcode {TCL WRONGARGS} \ "missing kind value to go with -kind option" } @@ -544,30 +544,30 @@ set addReader [expr {$kind ne "writable" && ![info exist getter]}] set addWriter [expr {$kind ne "readable" && ![info exist setter]}] try { - uplevel 2 [list $stdInstaller $prop $addReader $addWriter] + uplevel 1 [list $stdInstaller $prop $addReader $addWriter] } on error {msg opt} { - return -code error -level 2 \ + return -code error -level 1 \ -errorcode [dict get $opt -errorcode] $msg } switch $kind { readable { - uplevel 2 [list $readslot -append $realprop] - uplevel 2 [list $writeslot -remove $realprop] + uplevel 1 [list $readslot -append $realprop] + uplevel 1 [list $writeslot -remove $realprop] } writable { - uplevel 2 [list $readslot -remove $realprop] - uplevel 2 [list $writeslot -append $realprop] + uplevel 1 [list $readslot -remove $realprop] + uplevel 1 [list $writeslot -append $realprop] } readwrite { - uplevel 2 [list $readslot -append $realprop] - uplevel 2 [list $writeslot -append $realprop] + uplevel 1 [list $readslot -append $realprop] + uplevel 1 [list $writeslot -append $realprop] } } if {[info exist getter]} { - uplevel 2 [list method $reader -unexport {} $getter] + uplevel 1 [list method $reader -unexport {} $getter] } if {[info exist setter]} { - uplevel 2 [list method $writer -unexport {value} $setter] + uplevel 1 [list method $writer -unexport {value} $setter] } } } @@ -583,12 +583,12 @@ # ------------------------------------------------------------------ namespace eval configurableclass { - ::proc property args { - ::oo::configuresupport::PropertyImpl \ + ::interp alias \ + {} ::oo::configuresupport::configurableclass::property {} \ + ::oo::configuresupport::PropertyImpl \ ::oo::configuresupport::StdClassProperties \ ::oo::configuresupport::readableproperties \ - ::oo::configuresupport::writableproperties {*}$args - } + ::oo::configuresupport::writableproperties # Plural alias just in case; deliberately NOT documented! ::proc properties args {::tailcall property {*}$args} ::namespace path ::oo::define @@ -596,12 +596,12 @@ } namespace eval configurableobject { - ::proc property args { - ::oo::configuresupport::PropertyImpl \ + ::interp alias \ + {} ::oo::configuresupport::configurableobject::property {} \ + ::oo::configuresupport::PropertyImpl \ ::oo::configuresupport::StdObjectProperties \ ::oo::configuresupport::objreadableproperties \ - ::oo::configuresupport::objwritableproperties {*}$args - } + ::oo::configuresupport::objwritableproperties # Plural alias just in case; deliberately NOT documented! ::proc properties args {::tailcall property {*}$args} ::namespace path ::oo::objdefine -- cgit v0.12 From ee031194b91cda2a1aad058cf44f3dcba20394ac Mon Sep 17 00:00:00 2001 From: dkf Date: Fri, 19 Jul 2024 09:03:26 +0000 Subject: Improve some documentation (for myself) --- tools/tclOOScript.tcl | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 95ffbde..22f0e91 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -483,14 +483,40 @@ # oo::configuresupport::PropertyImpl -- # # The implementation of the [property] configuration command. - # This assumes there are two stack frames above it to care about, - # first the hull [property] command (in the correct context for - # what is being confgured) and then the context scope of - # [oo::define] or [oo::objdefine]. + # This assumes there is a context scope of [oo::define] or + # [oo::objdefine] as the stack frame that is up one level. # # TODO: # Convert to C code. # + # Arguments: + # stdInstaller + # How to install a property implementation of the right type. + # Must be evaluated in the definition scope. + # readslot + # Slot of readable properties. + # Must be evaluated in the definition scope. + # writeslot + # Slot of writable properties. + # Must be evaluated in the definition scope. + # args + # Arguments supplied by user. See user documentation. + # + # Results: + # None + # + # Errors: + # TCL WRONGARGS + # if an argument is missing. + # TCL LOOKUP INDEX + # if an option or property kind can't be parsed. + # TCL OO PROPERTY_FORMAT + # if an property name is illegal. + # + # Side effects: + # Adjusts properties. Declares non-exported methods with special + # names. + # # ------------------------------------------------------------------ proc PropertyImpl {stdInstaller readslot writeslot args} { -- cgit v0.12 From ce7649530f21f52c43e065104e759a52f3ff1c6d Mon Sep 17 00:00:00 2001 From: dkf Date: Sat, 20 Jul 2024 21:42:43 +0000 Subject: Make the [property] definition itself go to C --- generic/tclOO.c | 10 +- generic/tclOODefineCmds.c | 354 ++++++++++++++++++++++++++++++++++++++++++---- generic/tclOOInt.h | 14 +- generic/tclOOScript.h | 92 +----------- tools/tclOOScript.tcl | 152 ++------------------ 5 files changed, 364 insertions(+), 258 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index 5ceae4e..8ae30e9 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -441,9 +441,15 @@ InitFoundation( &cfgMethods[i]); } Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdObjectProperties", - TclOOInstallStdPropertyImpls, (void *) 1, NULL); + TclOOInstallStdPropertyImplsCmd, (void *) 1, NULL); Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdClassProperties", - TclOOInstallStdPropertyImpls, (void *) 0, NULL); + TclOOInstallStdPropertyImplsCmd, (void *) 0, NULL); + Tcl_CreateObjCommand(interp, + "::oo::configuresupport::configurableobject::property", + TclOOPropertyDefinitionCmd, (void *) 1, NULL); + Tcl_CreateObjCommand(interp, + "::oo::configuresupport::configurableclass::property", + TclOOPropertyDefinitionCmd, (void *) 0, NULL); /* * Evaluate the remaining definitions, which are a compiled-in Tcl script. diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 9e4f879..5f784d9 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -75,6 +75,9 @@ static inline Tcl_Namespace *GetNamespaceInOuterContext(Tcl_Interp *interp, static inline int InitDefineContext(Tcl_Interp *interp, Tcl_Namespace *namespacePtr, Object *oPtr, int objc, Tcl_Obj *const objv[]); +static int InstallStdPropertyImpls(void *useInstance, + Tcl_Interp *interp, Tcl_Obj *propName, + int readable, int writable); static inline void RecomputeClassCacheFlag(Object *oPtr); static int RenameDeleteMethod(Tcl_Interp *interp, Object *oPtr, int useClass, Tcl_Obj *const fromPtr, @@ -3487,7 +3490,286 @@ ObjWPropsSet( /* * ---------------------------------------------------------------------- * - * TclOOInstallStdPropertyImpls -- + * TclOORegisterProperty, TclOORegisterInstanceProperty -- + * + * Helpers to add or remove a name from the property slots of a class or + * instance. + * + * BuildPropertyList -- + * + * Helper for the helpers. Scans a property list and does the filtering + * or adding of the property to add or remove + * + * ---------------------------------------------------------------------- + */ + +static int +BuildPropertyList( + PropertyList *propsList, /* Property list to scan. */ + Tcl_Obj *propName, /* Property to add/remove. */ + int addingProp, /* True if we're adding, false if removing. */ + Tcl_Obj *listObj) /* The list of property names we're building */ +{ + int present = 0, changed = 0, i; + Tcl_Obj *other; + + Tcl_SetListObj(listObj, 0, NULL); + FOREACH(other, *propsList) { + if (strcmp(TclGetString(propName), TclGetString(other)) == 0) { + present = 1; + if (!addingProp) { + changed = 1; + continue; + } + } + Tcl_ListObjAppendElement(NULL, listObj, other); + } + if (!present && addingProp) { + Tcl_ListObjAppendElement(NULL, listObj, propName); + changed = 1; + } + return changed; +} + +void +TclOORegisterInstanceProperty( + Object *oPtr, /* Object that owns the property slots. */ + Tcl_Obj *propName, /* Property to add/remove. Must include the + * hyphen if one is desired; this is the value + * that is actually placed in the slot. */ + int registerReader, /* True if we're adding the property name to + * the readable property slot. False if we're + * removing the property name from the slot. */ + int registerWriter) /* True if we're adding the property name to + * the writable property slot. False if we're + * removing the property name from the slot. */ +{ + Tcl_Obj *listObj = Tcl_NewObj(); /* Working buffer. */ + Tcl_Obj **objv; + Tcl_Size count; + + if (BuildPropertyList(&oPtr->properties.readable, propName, registerReader, + listObj)) { + TclListObjGetElements(NULL, listObj, &count, &objv); + InstallReadableProps(&oPtr->properties, count, objv); + } + + if (BuildPropertyList(&oPtr->properties.writable, propName, registerWriter, + listObj)) { + TclListObjGetElements(NULL, listObj, &count, &objv); + InstallWritableProps(&oPtr->properties, count, objv); + } + Tcl_DecrRefCount(listObj); +} + +void +TclOORegisterProperty( + Class *clsPtr, /* Class that owns the property slots. */ + Tcl_Obj *propName, /* Property to add/remove. Must include the + * hyphen if one is desired; this is the value + * that is actually placed in the slot. */ + int registerReader, /* True if we're adding the property name to + * the readable property slot. False if we're + * removing the property name from the slot. */ + int registerWriter) /* True if we're adding the property name to + * the writable property slot. False if we're + * removing the property name from the slot. */ +{ + Tcl_Obj *listObj = Tcl_NewObj(); /* Working buffer. */ + Tcl_Obj **objv; + Tcl_Size count; + int changed = 0; + + if (BuildPropertyList(&clsPtr->properties.readable, propName, + registerReader, listObj)) { + TclListObjGetElements(NULL, listObj, &count, &objv); + InstallReadableProps(&clsPtr->properties, count, objv); + changed = 1; + } + + if (BuildPropertyList(&clsPtr->properties.writable, propName, + registerWriter, listObj)) { + TclListObjGetElements(NULL, listObj, &count, &objv); + InstallWritableProps(&clsPtr->properties, count, objv); + changed = 1; + } + Tcl_DecrRefCount(listObj); + if (changed) { + BumpGlobalEpoch(clsPtr->thisPtr->fPtr->interp, clsPtr); + } +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOPropertyDefinitionCmd -- + * + * Implementation of the "property" definition for classes and instances + * governed by the [oo::configurable] metaclass. + * + * ---------------------------------------------------------------------- + */ + +int +TclOOPropertyDefinitionCmd( + void *useInstance, /* NULL for class, non-NULL for object. */ + Tcl_Interp *interp, /* For error reporting and lookup. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* Arguments. */ +{ + int i; + const char *const options[] = { + "-get", "-kind", "-set", NULL + }; + enum Options { + OPT_GET, OPT_KIND, OPT_SET + }; + const char *const kinds[] = { + "readable", "readwrite", "writable", NULL + }; + enum Kinds { + KIND_RO, KIND_RW, KIND_WO + }; + Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + + if (oPtr == NULL) { + return TCL_ERROR; + } + if (!useInstance && !oPtr->classPtr) { + Tcl_SetObjResult(interp, Tcl_NewStringObj( + "attempt to misuse API", -1)); + Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL); + return TCL_ERROR; + } + + for (i = 1; i < objc; i++) { + Tcl_Obj *propObj = objv[i], *nextObj, *argObj, *hyphenated; + Tcl_Obj *getterScript = NULL, *setterScript = NULL; + + /* + * Parse the extra options for the property. + */ + + int kind = KIND_RW; + while (i + 1 < objc) { + int option; + + nextObj = objv[i + 1]; + if (TclGetString(nextObj)[0] != '-') { + break; + } + if (Tcl_GetIndexFromObj(interp, nextObj, options, "option", 0, + &option) != TCL_OK) { + return TCL_ERROR; + } + if (i + 2 >= objc) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "missing %s to go with %s option", + (option == OPT_KIND ? "kind value" : "body"), + options[option])); + Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", NULL); + return TCL_ERROR; + } + argObj = objv[i + 2]; + i += 2; + switch (option) { + case OPT_GET: + getterScript = argObj; + break; + case OPT_SET: + setterScript = argObj; + break; + case OPT_KIND: + if (Tcl_GetIndexFromObj(interp, argObj, kinds, "kind", 0, + &kind) != TCL_OK) { + return TCL_ERROR; + } + break; + } + } + + /* + * Install the property. Note that InstallStdPropertyImpls + * validates the property name as well. + */ + + if (InstallStdPropertyImpls(useInstance, interp, propObj, + kind != KIND_WO && getterScript == NULL, + kind != KIND_RO && setterScript == NULL) != TCL_OK) { + return TCL_ERROR; + } + + hyphenated = Tcl_ObjPrintf("-%s", TclGetString(propObj)); + Tcl_IncrRefCount(hyphenated); + if (useInstance) { + TclOORegisterInstanceProperty(oPtr, hyphenated, + kind != KIND_WO, kind != KIND_RO); + } else { + TclOORegisterProperty(oPtr->classPtr, hyphenated, + kind != KIND_WO, kind != KIND_RO); + } + Tcl_DecrRefCount(hyphenated); + + /* + * Create property implementation methods by using the right + * back-end API, but only if the user has given us the bodies of the + * methods we'll make. + */ + + if (getterScript != NULL) { + Tcl_Obj *getterName = Tcl_ObjPrintf("", + TclGetString(propObj)); + Tcl_Obj *argsPtr = Tcl_NewObj(); + Method *mPtr; + + Tcl_IncrRefCount(getterName); + Tcl_IncrRefCount(argsPtr); + Tcl_IncrRefCount(getterScript); + if (useInstance) { + mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, + getterName, argsPtr, getterScript, NULL); + } else { + mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, + getterName, argsPtr, getterScript, NULL); + } + Tcl_DecrRefCount(getterName); + Tcl_DecrRefCount(argsPtr); + Tcl_DecrRefCount(getterScript); + if (mPtr == NULL) { + return TCL_ERROR; + } + } + if (setterScript != NULL) { + Tcl_Obj *setterName = Tcl_ObjPrintf("", + TclGetString(propObj)); + Tcl_Obj *argsPtr = Tcl_NewStringObj("value", -1); + Method *mPtr; + + Tcl_IncrRefCount(setterName); + Tcl_IncrRefCount(argsPtr); + Tcl_IncrRefCount(setterScript); + if (useInstance) { + mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, + setterName, argsPtr, setterScript, NULL); + } else { + mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, + setterName, argsPtr, setterScript, NULL); + } + Tcl_DecrRefCount(setterName); + Tcl_DecrRefCount(argsPtr); + Tcl_DecrRefCount(setterScript); + if (mPtr == NULL) { + return TCL_ERROR; + } + } + } + return TCL_OK; +} + +/* + * ---------------------------------------------------------------------- + * + * InstallStdPropertyImpls, TclOOInstallStdPropertyImplsCmd -- * * Implementations of the "StdClassProperties" hidden definition for * classes and the "StdObjectProperties" hidden definition for @@ -3499,32 +3781,26 @@ ObjWPropsSet( * ---------------------------------------------------------------------- */ -int -TclOOInstallStdPropertyImpls( +static int +InstallStdPropertyImpls( void *useInstance, Tcl_Interp *interp, - int objc, - Tcl_Obj *const *objv) + Tcl_Obj *propName, + int readable, + int writable) { - int readable, writable; - Tcl_Obj *propName; const char *name, *reason; Tcl_Size len; char flag = TCL_DONT_QUOTE_HASH; /* - * Parse the arguments and validate the property name. Note that just - * calling TclScanElement() is cheaper than actually formatting a list - * and comparing the string version of that with the original, as - * TclScanElement() is one of the core parts of doing that; this skips - * a whole load of irrelevant memory allocations! + * Validate the property name. Note that just calling TclScanElement() is + * cheaper than actually formatting a list and comparing the string + * version of that with the original, as TclScanElement() is one of the + * core parts of doing that; this skips a whole load of irrelevant memory + * allocations! */ - if (objc != 4) { - Tcl_WrongNumArgs(interp, 1, objv, "propName readable writable"); - return TCL_ERROR; - } - propName = objv[1]; name = Tcl_GetStringFromObj(propName, &len); if (Tcl_StringMatch(name, "-*")) { reason = "must not begin with -"; @@ -3542,12 +3818,6 @@ TclOOInstallStdPropertyImpls( reason = "must not contain parentheses"; goto badProp; } - if (Tcl_GetBooleanFromObj(interp, objv[2], &readable) != TCL_OK) { - return TCL_ERROR; - } - if (Tcl_GetBooleanFromObj(interp, objv[3], &writable) != TCL_OK) { - return TCL_ERROR; - } /* * Install the implementations... if asked to do so. @@ -3572,7 +3842,43 @@ TclOOInstallStdPropertyImpls( Tcl_SetObjResult(interp, Tcl_ObjPrintf( "bad property name \"%s\": %s", name, reason)); Tcl_SetErrorCode(interp, "TCL", "OO", "PROPERTY_FORMAT", NULL); - return TCL_ERROR; + return TCL_ERROR; +} + +int +TclOOInstallStdPropertyImplsCmd( + void *useInstance, + Tcl_Interp *interp, + int objc, + Tcl_Obj *const *objv) +{ + int readable, writable; + Tcl_Obj *propName; + + /* + * Parse the arguments. + */ + + if (objc != 4) { + Tcl_WrongNumArgs(interp, 1, objv, "propName readable writable"); + return TCL_ERROR; + } + propName = objv[1]; + if (Tcl_GetBooleanFromObj(interp, objv[2], &readable) != TCL_OK) { + return TCL_ERROR; + } + if (Tcl_GetBooleanFromObj(interp, objv[3], &writable) != TCL_OK) { + return TCL_ERROR; + } + + + /* + * Validate the property name and install the implementations... if asked + * to do so. + */ + + return InstallStdPropertyImpls(useInstance, interp, propName, readable, + writable); } /* diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 07408f8..c31c461 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -169,16 +169,15 @@ typedef struct { typedef LIST_STATIC(Tcl_Obj *) VariableNameList; typedef LIST_STATIC(PrivateVariableMapping) PrivateVariableList; +typedef LIST_STATIC(Tcl_Obj *) PropertyList; /* * This type is used in various places. */ typedef struct { - LIST_STATIC(Tcl_Obj *) readable; - /* The readable properties slot. */ - LIST_STATIC(Tcl_Obj *) writable; - /* The writable properties slot. */ + PropertyList readable; /* The readable properties slot. */ + PropertyList writable; /* The writable properties slot. */ Tcl_Obj *allReadableCache; /* The cache of all readable properties * exposed by this object or class (in its * stereotypical instancs). Contains a sorted @@ -498,7 +497,8 @@ MODULE_SCOPE Tcl_ObjCmdProc TclOODefineClassObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineObjSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefinePrivateObjCmd; -MODULE_SCOPE Tcl_ObjCmdProc TclOOInstallStdPropertyImpls; +MODULE_SCOPE Tcl_ObjCmdProc TclOOInstallStdPropertyImplsCmd; +MODULE_SCOPE Tcl_ObjCmdProc TclOOPropertyDefinitionCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOUnknownDefinition; MODULE_SCOPE Tcl_ObjCmdProc TclOOCopyObjectCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOONextObjCmd; @@ -611,6 +611,10 @@ MODULE_SCOPE void TclOOSortPropList(Tcl_Obj *listPtr); MODULE_SCOPE void TclOOStashContext(Tcl_Obj *objPtr, CallContext *contextPtr); MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); +MODULE_SCOPE void TclOORegisterProperty(Class *clsPtr, + Tcl_Obj *propName, int mayRead, int mayWrite); +MODULE_SCOPE void TclOORegisterInstanceProperty(Object *oPtr, + Tcl_Obj *propName, int mayRead, int mayWrite); /* * Include all the private API, generated from tclOO.decls. diff --git a/generic/tclOOScript.h b/generic/tclOOScript.h index 7538d48..98fa20e 100644 --- a/generic/tclOOScript.h +++ b/generic/tclOOScript.h @@ -258,99 +258,13 @@ static const char *tclOOSetupScript = "\t\tsuperclass class\n" "\t\tunexport create createWithNamespace new\n" "\t}\n" -"\t::namespace eval configuresupport {\n" -"\t\tnamespace path ::tcl\n" -"\t\tproc PropertyImpl {stdInstaller readslot writeslot args} {\n" -"\t\t\tfor {set i 0} {$i < [llength $args]} {incr i} {\n" -"\t\t\t\tset prop [lindex $args $i]\n" -"\t\t\t\tset realprop [string cat \"-\" $prop]\n" -"\t\t\t\tunset -nocomplain getter setter\n" -"\t\t\t\tset kind readwrite\n" -"\t\t\t\twhile {[set next [lindex $args [expr {$i + 1}]]\n" -"\t\t\t\t\t\tstring match \"-*\" $next]} {\n" -"\t\t\t\t\tset arg [lindex $args [incr i 2]]\n" -"\t\t\t\t\tswitch [prefix match -error [list -level 1 -errorcode \\\n" -"\t\t\t\t\t\t\t[list TCL LOOKUP INDEX option $next]] {-get -kind -set} $next] {\n" -"\t\t\t\t\t\t-get {\n" -"\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" -"\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" -"\t\t\t\t\t\t\t\t\t\"missing body to go with -get option\"\n" -"\t\t\t\t\t\t\t}\n" -"\t\t\t\t\t\t\tset getter $arg\n" -"\t\t\t\t\t\t}\n" -"\t\t\t\t\t\t-set {\n" -"\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" -"\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" -"\t\t\t\t\t\t\t\t\t\"missing body to go with -set option\"\n" -"\t\t\t\t\t\t\t}\n" -"\t\t\t\t\t\t\tset setter $arg\n" -"\t\t\t\t\t\t}\n" -"\t\t\t\t\t\t-kind {\n" -"\t\t\t\t\t\t\tif {$i >= [llength $args]} {\n" -"\t\t\t\t\t\t\t\treturn -code error -level 1 \\\n" -"\t\t\t\t\t\t\t\t\t-errorcode {TCL WRONGARGS} \\\n" -"\t\t\t\t\t\t\t\t\t\"missing kind value to go with -kind option\"\n" -"\t\t\t\t\t\t\t}\n" -"\t\t\t\t\t\t\tset kind [prefix match -message \"kind\" -error [list \\\n" -"\t\t\t\t\t\t\t\t\t-level 2 \\\n" -"\t\t\t\t\t\t\t\t\t-errorcode [list TCL LOOKUP INDEX kind $arg]] {\n" -"\t\t\t\t\t\t\t\treadable readwrite writable\n" -"\t\t\t\t\t\t\t} $arg]\n" -"\t\t\t\t\t\t}\n" -"\t\t\t\t\t}\n" -"\t\t\t\t}\n" -"\t\t\t\tset reader \n" -"\t\t\t\tset writer \n" -"\t\t\t\tset addReader [expr {$kind ne \"writable\" && ![info exist getter]}]\n" -"\t\t\t\tset addWriter [expr {$kind ne \"readable\" && ![info exist setter]}]\n" -"\t\t\t\ttry {\n" -"\t\t\t\t\tuplevel 1 [list $stdInstaller $prop $addReader $addWriter]\n" -"\t\t\t\t} on error {msg opt} {\n" -"\t\t\t\t\treturn -code error -level 1 \\\n" -"\t\t\t\t\t\t\t-errorcode [dict get $opt -errorcode] $msg\n" -"\t\t\t\t}\n" -"\t\t\t\tswitch $kind {\n" -"\t\t\t\t\treadable {\n" -"\t\t\t\t\t\tuplevel 1 [list $readslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 1 [list $writeslot -remove $realprop]\n" -"\t\t\t\t\t}\n" -"\t\t\t\t\twritable {\n" -"\t\t\t\t\t\tuplevel 1 [list $readslot -remove $realprop]\n" -"\t\t\t\t\t\tuplevel 1 [list $writeslot -append $realprop]\n" -"\t\t\t\t\t}\n" -"\t\t\t\t\treadwrite {\n" -"\t\t\t\t\t\tuplevel 1 [list $readslot -append $realprop]\n" -"\t\t\t\t\t\tuplevel 1 [list $writeslot -append $realprop]\n" -"\t\t\t\t\t}\n" -"\t\t\t\t}\n" -"\t\t\t\tif {[info exist getter]} {\n" -"\t\t\t\t\tuplevel 1 [list method $reader -unexport {} $getter]\n" -"\t\t\t\t}\n" -"\t\t\t\tif {[info exist setter]} {\n" -"\t\t\t\t\tuplevel 1 [list method $writer -unexport {value} $setter]\n" -"\t\t\t\t}\n" -"\t\t\t}\n" -"\t\t}\n" -"\t\tnamespace eval configurableclass {\n" -"\t\t\t::interp alias \\\n" -"\t\t\t\t\t{} ::oo::configuresupport::configurableclass::property {} \\\n" -"\t\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" -"\t\t\t\t\t::oo::configuresupport::StdClassProperties \\\n" -"\t\t\t\t\t::oo::configuresupport::readableproperties \\\n" -"\t\t\t\t\t::oo::configuresupport::writableproperties\n" +"\tnamespace eval configuresupport {\n" +"\t\t::namespace eval configurableclass {\n" "\t\t\t::proc properties args {::tailcall property {*}$args}\n" "\t\t\t::namespace path ::oo::define\n" "\t\t\t::namespace export property\n" "\t\t}\n" -"\t\tnamespace eval configurableobject {\n" -"\t\t\t::interp alias \\\n" -"\t\t\t\t\t{} ::oo::configuresupport::configurableobject::property {} \\\n" -"\t\t\t\t\t::oo::configuresupport::PropertyImpl \\\n" -"\t\t\t\t\t::oo::configuresupport::StdObjectProperties \\\n" -"\t\t\t\t\t::oo::configuresupport::objreadableproperties \\\n" -"\t\t\t\t\t::oo::configuresupport::objwritableproperties\n" +"\t\t::namespace eval configurableobject {\n" "\t\t\t::proc properties args {::tailcall property {*}$args}\n" "\t\t\t::namespace path ::oo::objdefine\n" "\t\t\t::namespace export property\n" diff --git a/tools/tclOOScript.tcl b/tools/tclOOScript.tcl index 22f0e91..fc0927c 100644 --- a/tools/tclOOScript.tcl +++ b/tools/tclOOScript.tcl @@ -470,134 +470,20 @@ # * objreadableproperties # * objwritableproperties # - # Those are all slot implementations that provide access to the C layer + # These are all slot implementations that provide access to the C layer # of property support (i.e., very fast cached lookup of property names). # + # * StdClassProperties + # * StdObjectPropertes + # + # These cause very fast basic implementation methods for a property + # following the standard model of property implementation naming. + # Property schemes that use other models (such as to be more Tk-like) + # should not use these (or the oo::cconfigurable metaclass). + # # ---------------------------------------------------------------------- - ::namespace eval configuresupport { - namespace path ::tcl - - # ------------------------------------------------------------------ - # - # oo::configuresupport::PropertyImpl -- - # - # The implementation of the [property] configuration command. - # This assumes there is a context scope of [oo::define] or - # [oo::objdefine] as the stack frame that is up one level. - # - # TODO: - # Convert to C code. - # - # Arguments: - # stdInstaller - # How to install a property implementation of the right type. - # Must be evaluated in the definition scope. - # readslot - # Slot of readable properties. - # Must be evaluated in the definition scope. - # writeslot - # Slot of writable properties. - # Must be evaluated in the definition scope. - # args - # Arguments supplied by user. See user documentation. - # - # Results: - # None - # - # Errors: - # TCL WRONGARGS - # if an argument is missing. - # TCL LOOKUP INDEX - # if an option or property kind can't be parsed. - # TCL OO PROPERTY_FORMAT - # if an property name is illegal. - # - # Side effects: - # Adjusts properties. Declares non-exported methods with special - # names. - # - # ------------------------------------------------------------------ - - proc PropertyImpl {stdInstaller readslot writeslot args} { - for {set i 0} {$i < [llength $args]} {incr i} { - # Parse the property name - set prop [lindex $args $i] - set realprop [string cat "-" $prop] - unset -nocomplain getter setter - set kind readwrite - - # Parse the extra options for the property - while {[set next [lindex $args [expr {$i + 1}]] - string match "-*" $next]} { - set arg [lindex $args [incr i 2]] - switch [prefix match -error [list -level 1 -errorcode \ - [list TCL LOOKUP INDEX option $next]] {-get -kind -set} $next] { - -get { - if {$i >= [llength $args]} { - return -code error -level 1 \ - -errorcode {TCL WRONGARGS} \ - "missing body to go with -get option" - } - set getter $arg - } - -set { - if {$i >= [llength $args]} { - return -code error -level 1 \ - -errorcode {TCL WRONGARGS} \ - "missing body to go with -set option" - } - set setter $arg - } - -kind { - if {$i >= [llength $args]} { - return -code error -level 1 \ - -errorcode {TCL WRONGARGS} \ - "missing kind value to go with -kind option" - } - set kind [prefix match -message "kind" -error [list \ - -level 2 \ - -errorcode [list TCL LOOKUP INDEX kind $arg]] { - readable readwrite writable - } $arg] - } - } - } - - # Install the property - set reader - set writer - set addReader [expr {$kind ne "writable" && ![info exist getter]}] - set addWriter [expr {$kind ne "readable" && ![info exist setter]}] - try { - uplevel 1 [list $stdInstaller $prop $addReader $addWriter] - } on error {msg opt} { - return -code error -level 1 \ - -errorcode [dict get $opt -errorcode] $msg - } - switch $kind { - readable { - uplevel 1 [list $readslot -append $realprop] - uplevel 1 [list $writeslot -remove $realprop] - } - writable { - uplevel 1 [list $readslot -remove $realprop] - uplevel 1 [list $writeslot -append $realprop] - } - readwrite { - uplevel 1 [list $readslot -append $realprop] - uplevel 1 [list $writeslot -append $realprop] - } - } - if {[info exist getter]} { - uplevel 1 [list method $reader -unexport {} $getter] - } - if {[info exist setter]} { - uplevel 1 [list method $writer -unexport {value} $setter] - } - } - } - + namespace eval configuresupport { # ------------------------------------------------------------------ # # oo::configuresupport::configurableclass, @@ -605,29 +491,19 @@ # # Namespaces used as implementation vectors for oo::define and # oo::objdefine when the class/instance is configurable. + # Note that these also contain commands implemented in C, + # especially the [property] definition command. # # ------------------------------------------------------------------ - namespace eval configurableclass { - ::interp alias \ - {} ::oo::configuresupport::configurableclass::property {} \ - ::oo::configuresupport::PropertyImpl \ - ::oo::configuresupport::StdClassProperties \ - ::oo::configuresupport::readableproperties \ - ::oo::configuresupport::writableproperties + ::namespace eval configurableclass { # Plural alias just in case; deliberately NOT documented! ::proc properties args {::tailcall property {*}$args} ::namespace path ::oo::define ::namespace export property } - namespace eval configurableobject { - ::interp alias \ - {} ::oo::configuresupport::configurableobject::property {} \ - ::oo::configuresupport::PropertyImpl \ - ::oo::configuresupport::StdObjectProperties \ - ::oo::configuresupport::objreadableproperties \ - ::oo::configuresupport::objwritableproperties + ::namespace eval configurableobject { # Plural alias just in case; deliberately NOT documented! ::proc properties args {::tailcall property {*}$args} ::namespace path ::oo::objdefine -- cgit v0.12 From b66dcfd46ed96ddfacaa74856ac4f09fb06a9b37 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 21 Jul 2024 14:14:42 +0000 Subject: Tidying up the properties code --- generic/tclOOBasic.c | 26 ++- generic/tclOODefineCmds.c | 416 ++++++++++++++++++++++++---------------------- 2 files changed, 226 insertions(+), 216 deletions(-) diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 34cc272..e8b4e13 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -1678,8 +1678,9 @@ TclOO_Configurable_Configure( static int Configurable_Getter( - void *clientData, /* Which property to read. - * Actually a Tcl_Obj* reference. */ + void *clientData, /* Which property to read. Actually a Tcl_Obj* + * reference that is the name of the variable + * in the cpntext object. */ Tcl_Interp *interp, /* Interpreter used for the result, error * reporting, etc. */ Tcl_ObjectContext context, /* The object/call context. */ @@ -1713,8 +1714,9 @@ Configurable_Getter( static int Configurable_Setter( - void *clientData, /* Which property to write. - * Actually a Tcl_Obj* reference. */ + void *clientData, /* Which property to write. Actually a Tcl_Obj* + * reference that is the name of the variable + * in the cpntext object. */ Tcl_Interp *interp, /* Interpreter used for the result, error * reporting, etc. */ Tcl_ObjectContext context, /* The object/call context. */ @@ -1791,22 +1793,18 @@ TclOOImplementObjectProperty( if (installGetter) { Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); - // Don't know if TclNewInstanceMethod will retain a ref to the method name - Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewInstanceMethod( NULL, targetObject, methodName, 0, &GetterType, propNamePtr); - Tcl_DecrRefCount(methodName); + Tcl_BounceRefCount(methodName); } if (installSetter) { Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); - // Don't know if TclNewInstanceMethod will retain a ref to the method name - Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewInstanceMethod( NULL, targetObject, methodName, 0, &SetterType, propNamePtr); - Tcl_DecrRefCount(methodName); + Tcl_BounceRefCount(methodName); } } @@ -1820,22 +1818,18 @@ TclOOImplementClassProperty( if (installGetter) { Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); - // Don't know if TclNewMethod will retain a ref to the method name - Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewMethod( NULL, targetClass, methodName, 0, &GetterType, propNamePtr); - Tcl_DecrRefCount(methodName); + Tcl_BounceRefCount(methodName); } if (installSetter) { Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); - // Don't know if TclNewMethod will retain a ref to the method name - Tcl_IncrRefCount(methodName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewMethod( NULL, targetClass, methodName, 0, &SetterType, propNamePtr); - Tcl_DecrRefCount(methodName); + Tcl_BounceRefCount(methodName); } } diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 5f784d9..d3ec410 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -82,52 +82,72 @@ static inline void RecomputeClassCacheFlag(Object *oPtr); static int RenameDeleteMethod(Tcl_Interp *interp, Object *oPtr, int useClass, Tcl_Obj *const fromPtr, Tcl_Obj *const toPtr); -static int ClassFilterGet(void *clientData, +static int ClassFilter_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassFilterSet(void *clientData, +static int ClassFilter_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassMixinGet(void *clientData, +static int ClassMixin_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassMixinSet(void *clientData, +static int ClassMixin_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassSuperGet(void *clientData, +static int ClassSuper_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassSuperSet(void *clientData, +static int ClassSuper_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassVarsGet(void *clientData, +static int ClassVars_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ClassVarsSet(void *clientData, +static int ClassVars_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static Tcl_MethodCallProc ClassRPropsGet, ClassRPropsSet; -static Tcl_MethodCallProc ClassWPropsGet, ClassWPropsSet; -static int ObjFilterGet(void *clientData, +static int ObjFilter_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ObjFilterSet(void *clientData, +static int ObjFilter_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ObjMixinGet(void *clientData, +static int ObjMixin_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ObjMixinSet(void *clientData, +static int ObjMixin_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ObjVarsGet(void *clientData, +static int ObjVars_Get(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static int ObjVarsSet(void *clientData, +static int ObjVars_Set(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ClassReadableProps_Get(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ClassReadableProps_Set(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ClassWritableProps_Get(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ClassWritableProps_Set(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ObjectReadableProps_Get(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ObjectReadableProps_Set(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ObjectWritableProps_Get(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_ObjectWritableProps_Set(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); -static Tcl_MethodCallProc ObjRPropsGet, ObjRPropsSet; -static Tcl_MethodCallProc ObjWPropsGet, ObjWPropsSet; static int ResolveClass(void *clientData, Tcl_Interp *interp, Tcl_ObjectContext context, int objc, Tcl_Obj *const *objv); @@ -137,21 +157,25 @@ static int ResolveClass(void *clientData, */ static const struct DeclaredSlot slots[] = { - SLOT("define::filter", ClassFilterGet, ClassFilterSet, NULL), - SLOT("define::mixin", ClassMixinGet, ClassMixinSet, ResolveClass), - SLOT("define::superclass", ClassSuperGet, ClassSuperSet, ResolveClass), - SLOT("define::variable", ClassVarsGet, ClassVarsSet, NULL), - SLOT("objdefine::filter", ObjFilterGet, ObjFilterSet, NULL), - SLOT("objdefine::mixin", ObjMixinGet, ObjMixinSet, ResolveClass), - SLOT("objdefine::variable", ObjVarsGet, ObjVarsSet, NULL), + SLOT("define::filter", ClassFilter_Get, ClassFilter_Set, NULL), + SLOT("define::mixin", ClassMixin_Get, ClassMixin_Set, ResolveClass), + SLOT("define::superclass", ClassSuper_Get, ClassSuper_Set, ResolveClass), + SLOT("define::variable", ClassVars_Get, ClassVars_Set, NULL), + SLOT("objdefine::filter", ObjFilter_Get, ObjFilter_Set, NULL), + SLOT("objdefine::mixin", ObjMixin_Get, ObjMixin_Set, ResolveClass), + SLOT("objdefine::variable", ObjVars_Get, ObjVars_Set, NULL), SLOT("configuresupport::readableproperties", - ClassRPropsGet, ClassRPropsSet, NULL), + Configurable_ClassReadableProps_Get, + Configurable_ClassReadableProps_Set, NULL), SLOT("configuresupport::writableproperties", - ClassWPropsGet, ClassWPropsSet, NULL), + Configurable_ClassWritableProps_Get, + Configurable_ClassWritableProps_Set, NULL), SLOT("configuresupport::objreadableproperties", - ObjRPropsGet, ObjRPropsSet, NULL), + Configurable_ObjectReadableProps_Get, + Configurable_ObjectReadableProps_Set, NULL), SLOT("configuresupport::objwritableproperties", - ObjWPropsGet, ObjWPropsSet, NULL), + Configurable_ObjectWritableProps_Get, + Configurable_ObjectWritableProps_Set, NULL), {NULL, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}} }; @@ -612,10 +636,12 @@ InstallPrivateVariableMapping( if (varc == 0) { Tcl_Free(pvlPtr->list); } else if (i) { - pvlPtr->list = (PrivateVariableMapping *)Tcl_Realloc(pvlPtr->list, - sizeof(PrivateVariableMapping) * varc); + pvlPtr->list = (PrivateVariableMapping *) + Tcl_Realloc(pvlPtr->list, + sizeof(PrivateVariableMapping) * varc); } else { - pvlPtr->list = (PrivateVariableMapping *)Tcl_Alloc(sizeof(PrivateVariableMapping) * varc); + pvlPtr->list = (PrivateVariableMapping *) + Tcl_Alloc(sizeof(PrivateVariableMapping) * varc); } } @@ -780,7 +806,8 @@ TclOOUnknownDefinition( } hPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search); while (hPtr != NULL) { - const char *nameStr = (const char *)Tcl_GetHashKey(&nsPtr->cmdTable, hPtr); + const char *nameStr = (const char *) + Tcl_GetHashKey(&nsPtr->cmdTable, hPtr); if (strncmp(soughtStr, nameStr, soughtLen) == 0) { if (matchedStr != NULL) { @@ -2208,7 +2235,8 @@ TclOODefineUnexportObjCmd( if (isInstanceUnexport) { if (!oPtr->methodsPtr) { - oPtr->methodsPtr = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->methodsPtr = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(oPtr->methodsPtr); oPtr->flags &= ~USE_CLASS_CACHE; } @@ -2330,9 +2358,6 @@ TclOODefineSlots( if (slotCls == NULL) { return TCL_ERROR; } - Tcl_IncrRefCount(getName); - Tcl_IncrRefCount(setName); - Tcl_IncrRefCount(resolveName); for (slotInfoPtr = slots ; slotInfoPtr->name ; slotInfoPtr++) { Tcl_Object slotObject = Tcl_NewObjectInstance(fPtr->interp, (Tcl_Class) slotCls, slotInfoPtr->name, NULL, TCL_INDEX_NONE, NULL, 0); @@ -2349,16 +2374,16 @@ TclOODefineSlots( &slotInfoPtr->resolverType, NULL); } } - Tcl_DecrRefCount(getName); - Tcl_DecrRefCount(setName); - Tcl_DecrRefCount(resolveName); + Tcl_BounceRefCount(getName); + Tcl_BounceRefCount(setName); + Tcl_BounceRefCount(resolveName); return TCL_OK; } /* * ---------------------------------------------------------------------- * - * ClassFilterGet, ClassFilterSet -- + * ClassFilter_Get, ClassFilter_Set -- * * Implementation of the "filter" slot accessors of the "oo::define" * command. @@ -2367,7 +2392,7 @@ TclOODefineSlots( */ static int -ClassFilterGet( +ClassFilter_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2395,7 +2420,7 @@ ClassFilterGet( } static int -ClassFilterSet( +ClassFilter_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2427,7 +2452,7 @@ ClassFilterSet( /* * ---------------------------------------------------------------------- * - * ClassMixinGet, ClassMixinSet -- + * ClassMixin_Get, ClassMixin_Set -- * * Implementation of the "mixin" slot accessors of the "oo::define" * command. @@ -2436,7 +2461,7 @@ ClassFilterSet( */ static int -ClassMixinGet( +ClassMixin_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2467,7 +2492,7 @@ ClassMixinGet( } static int -ClassMixinSet( +ClassMixin_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2536,7 +2561,7 @@ ClassMixinSet( /* * ---------------------------------------------------------------------- * - * ClassSuperGet, ClassSuperSet -- + * ClassSuper_Get, ClassSuper_Set -- * * Implementation of the "superclass" slot accessors of the "oo::define" * command. @@ -2545,7 +2570,7 @@ ClassMixinSet( */ static int -ClassSuperGet( +ClassSuper_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2575,7 +2600,7 @@ ClassSuperGet( } static int -ClassSuperSet( +ClassSuper_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2694,7 +2719,7 @@ ClassSuperSet( /* * ---------------------------------------------------------------------- * - * ClassVarsGet, ClassVarsSet -- + * ClassVars_Get, ClassVars_Set -- * * Implementation of the "variable" slot accessors of the "oo::define" * command. @@ -2703,7 +2728,7 @@ ClassSuperSet( */ static int -ClassVarsGet( +ClassVars_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2741,7 +2766,7 @@ ClassVarsGet( } static int -ClassVarsSet( +ClassVars_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2797,7 +2822,7 @@ ClassVarsSet( /* * ---------------------------------------------------------------------- * - * ObjectFilterGet, ObjectFilterSet -- + * ObjFilter_Get, ObjFilter_Set -- * * Implementation of the "filter" slot accessors of the "oo::objdefine" * command. @@ -2806,7 +2831,7 @@ ClassVarsSet( */ static int -ObjFilterGet( +ObjFilter_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2834,7 +2859,7 @@ ObjFilterGet( } static int -ObjFilterSet( +ObjFilter_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2864,7 +2889,7 @@ ObjFilterSet( /* * ---------------------------------------------------------------------- * - * ObjectMixinGet, ObjectMixinSet -- + * ObjMixin_Get, ObjMixin_Set -- * * Implementation of the "mixin" slot accessors of the "oo::objdefine" * command. @@ -2873,7 +2898,7 @@ ObjFilterSet( */ static int -ObjMixinGet( +ObjMixin_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2905,7 +2930,7 @@ ObjMixinGet( } static int -ObjMixinSet( +ObjMixin_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -2966,7 +2991,7 @@ ObjMixinSet( /* * ---------------------------------------------------------------------- * - * ObjectVarsGet, ObjectVarsSet -- + * ObjVars_Get, ObjVars_Set -- * * Implementation of the "variable" slot accessors of the "oo::objdefine" * command. @@ -2975,7 +3000,7 @@ ObjMixinSet( */ static int -ObjVarsGet( +ObjVars_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3013,7 +3038,7 @@ ObjVarsGet( } static int -ObjVarsSet( +ObjVars_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3120,73 +3145,151 @@ ResolveClass( /* * ---------------------------------------------------------------------- * - * ClassRPropsGet, ClassRPropsSet, ObjRPropsGet, ObjRPropsSet -- + * SetPropertyList -- * - * Implementations of the "readableproperties" slot accessors for classes - * and instances. + * Helper for writing a property list (which is actually a set). * * ---------------------------------------------------------------------- */ - -static void -InstallReadableProps( - PropertyStorage *props, - Tcl_Size objc, - Tcl_Obj *const objv[]) +static inline void +SetPropertyList( + PropertyList *propList, /* The property list to write. Replaces the + * property list's contents. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ { - Tcl_Obj *propObj; Tcl_Size i, n; + Tcl_Obj *propObj; int created; Tcl_HashTable uniqueTable; - if (props->allReadableCache) { - Tcl_DecrRefCount(props->allReadableCache); - props->allReadableCache = NULL; - } - for (i=0 ; ireadable) { + FOREACH(propObj, *propList) { Tcl_DecrRefCount(propObj); } if (i != objc) { if (objc == 0) { - Tcl_Free(props->readable.list); + Tcl_Free(propList->list); } else if (i) { - props->readable.list = (Tcl_Obj **)Tcl_Realloc(props->readable.list, - sizeof(Tcl_Obj *) * objc); + propList->list = (Tcl_Obj **) + Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * objc); } else { - props->readable.list = (Tcl_Obj **)Tcl_Alloc(sizeof(Tcl_Obj *) * objc); + propList->list = (Tcl_Obj **) + Tcl_Alloc(sizeof(Tcl_Obj *) * objc); } } - props->readable.num = 0; + propList->num = 0; if (objc > 0) { Tcl_InitObjHashTable(&uniqueTable); for (i=n=0 ; ireadable.list[n++] = objv[i]; + propList->list[n++] = objv[i]; } else { Tcl_DecrRefCount(objv[i]); } } - props->readable.num = n; + propList->num = n; /* * Shouldn't be necessary, but maintain num/list invariant. */ if (n != objc) { - props->readable.list = (Tcl_Obj **)Tcl_Realloc(props->readable.list, - sizeof(Tcl_Obj *) * n); + propList->list = (Tcl_Obj **) + Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * n); } Tcl_DeleteHashTable(&uniqueTable); } } + +/* + * ---------------------------------------------------------------------- + * + * InstallReadableProps -- + * + * Helper for writing the readable property list (which is actually a set) + * that includes flushing the name cache. + * + * ---------------------------------------------------------------------- + */ +static inline void +InstallReadableProps( + PropertyStorage *props, /* Which property list to install into. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ +{ + if (props->allReadableCache) { + Tcl_DecrRefCount(props->allReadableCache); + props->allReadableCache = NULL; + } + + SetPropertyList(&props->readable, objc, objv); +} + +/* + * ---------------------------------------------------------------------- + * + * InstallWritableProps -- + * + * Helper for writing the writable property list (which is actually a set) + * that includes flushing the name cache. + * + * ---------------------------------------------------------------------- + */ +static inline void +InstallWritableProps( + PropertyStorage *props, /* Which property list to install into. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ +{ + if (props->allWritableCache) { + Tcl_DecrRefCount(props->allWritableCache); + props->allWritableCache = NULL; + } + + SetPropertyList(&props->writable, objc, objv); +} + +/* + * ---------------------------------------------------------------------- + * + * GetPropertyList -- + * + * Helper for reading a property list. + * + * ---------------------------------------------------------------------- + */ +static inline Tcl_Obj * +GetPropertyList( + PropertyList *propList) /* The property list to read. */ +{ + Tcl_Obj *resultObj, *propNameObj; + Tcl_Size i; + + TclNewObj(resultObj); + FOREACH(propNameObj, *propList) { + Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); + } + return resultObj; +} + +/* + * ---------------------------------------------------------------------- + * + * Configurable_ClassReadableProps_Get, Configurable_ClassReadableProps_Set, + * Configurable_ObjectReadableProps_Get, Configurable_ObjectReadableProps_Set -- + * + * Implementations of the "readableproperties" slot accessors for classes + * and instances. + * + * ---------------------------------------------------------------------- + */ static int -ClassRPropsGet( +Configurable_ClassReadableProps_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3194,8 +3297,6 @@ ClassRPropsGet( Tcl_Obj *const *objv) { Class *clsPtr = GetClassDefineCmdContext(interp); - Tcl_Obj *resultObj, *propNameObj; - int i; if (clsPtr == NULL) { return TCL_ERROR; @@ -3205,16 +3306,12 @@ ClassRPropsGet( return TCL_ERROR; } - TclNewObj(resultObj); - FOREACH(propNameObj, clsPtr->properties.readable) { - Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); - } - Tcl_SetObjResult(interp, resultObj); + Tcl_SetObjResult(interp, GetPropertyList(&clsPtr->properties.readable)); return TCL_OK; } static int -ClassRPropsSet( +Configurable_ClassReadableProps_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3244,7 +3341,7 @@ ClassRPropsSet( } static int -ObjRPropsGet( +Configurable_ObjectReadableProps_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3252,28 +3349,21 @@ ObjRPropsGet( Tcl_Obj *const *objv) { Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); - Tcl_Obj *resultObj, *propNameObj; - int i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (oPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } - TclNewObj(resultObj); - FOREACH(propNameObj, oPtr->properties.readable) { - Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); - } - Tcl_SetObjResult(interp, resultObj); + Tcl_SetObjResult(interp, GetPropertyList(&oPtr->properties.readable)); return TCL_OK; } static int -ObjRPropsSet( +Configurable_ObjectReadableProps_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3305,7 +3395,8 @@ ObjRPropsSet( /* * ---------------------------------------------------------------------- * - * ClassWPropsGet, ClassWPropsSet, ObjWPropsGet, ObjWPropsSet -- + * Configurable_ClassWritableProps_Get, Configurable_ClassWritableProps_Set, + * Configurable_ObjectWritableProps_Get, Configurable_ObjectWritableProps_Set -- * * Implementations of the "writableproperties" slot accessors for classes * and instances. @@ -3313,65 +3404,8 @@ ObjRPropsSet( * ---------------------------------------------------------------------- */ -static void -InstallWritableProps( - PropertyStorage *props, - Tcl_Size objc, - Tcl_Obj *const objv[]) -{ - Tcl_Obj *propObj; - Tcl_Size i, n; - int created; - Tcl_HashTable uniqueTable; - - if (props->allWritableCache) { - Tcl_DecrRefCount(props->allWritableCache); - props->allWritableCache = NULL; - } - - for (i=0 ; iwritable) { - Tcl_DecrRefCount(propObj); - } - if (i != objc) { - if (objc == 0) { - Tcl_Free(props->writable.list); - } else if (i) { - props->writable.list = (Tcl_Obj **)Tcl_Realloc(props->writable.list, - sizeof(Tcl_Obj *) * objc); - } else { - props->writable.list = (Tcl_Obj **)Tcl_Alloc(sizeof(Tcl_Obj *) * objc); - } - } - props->writable.num = 0; - if (objc > 0) { - Tcl_InitObjHashTable(&uniqueTable); - for (i=n=0 ; iwritable.list[n++] = objv[i]; - } else { - Tcl_DecrRefCount(objv[i]); - } - } - props->writable.num = n; - - /* - * Shouldn't be necessary, but maintain num/list invariant. - */ - - if (n != objc) { - props->writable.list = (Tcl_Obj **)Tcl_Realloc(props->writable.list, - sizeof(Tcl_Obj *) * n); - } - Tcl_DeleteHashTable(&uniqueTable); - } -} - static int -ClassWPropsGet( +Configurable_ClassWritableProps_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3379,8 +3413,6 @@ ClassWPropsGet( Tcl_Obj *const *objv) { Class *clsPtr = GetClassDefineCmdContext(interp); - Tcl_Obj *resultObj, *propNameObj; - int i; if (clsPtr == NULL) { return TCL_ERROR; @@ -3390,16 +3422,12 @@ ClassWPropsGet( return TCL_ERROR; } - TclNewObj(resultObj); - FOREACH(propNameObj, clsPtr->properties.writable) { - Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); - } - Tcl_SetObjResult(interp, resultObj); + Tcl_SetObjResult(interp, GetPropertyList(&clsPtr->properties.writable)); return TCL_OK; } static int -ClassWPropsSet( +Configurable_ClassWritableProps_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3429,7 +3457,7 @@ ClassWPropsSet( } static int -ObjWPropsGet( +Configurable_ObjectWritableProps_Get( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3437,28 +3465,21 @@ ObjWPropsGet( Tcl_Obj *const *objv) { Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); - Tcl_Obj *resultObj, *propNameObj; - int i; - if (Tcl_ObjectContextSkippedArgs(context) != objc) { + if (oPtr == NULL) { + return TCL_ERROR; + } else if (Tcl_ObjectContextSkippedArgs(context) != objc) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; } - if (oPtr == NULL) { - return TCL_ERROR; - } - TclNewObj(resultObj); - FOREACH(propNameObj, oPtr->properties.writable) { - Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); - } - Tcl_SetObjResult(interp, resultObj); + Tcl_SetObjResult(interp, GetPropertyList(&oPtr->properties.writable)); return TCL_OK; } static int -ObjWPropsSet( +Configurable_ObjectWritableProps_Set( TCL_UNUSED(void *), Tcl_Interp *interp, Tcl_ObjectContext context, @@ -3559,7 +3580,7 @@ TclOORegisterInstanceProperty( TclListObjGetElements(NULL, listObj, &count, &objv); InstallWritableProps(&oPtr->properties, count, objv); } - Tcl_DecrRefCount(listObj); + Tcl_BounceRefCount(listObj); } void @@ -3593,7 +3614,7 @@ TclOORegisterProperty( InstallWritableProps(&clsPtr->properties, count, objv); changed = 1; } - Tcl_DecrRefCount(listObj); + Tcl_BounceRefCount(listObj); if (changed) { BumpGlobalEpoch(clsPtr->thisPtr->fPtr->interp, clsPtr); } @@ -3700,7 +3721,6 @@ TclOOPropertyDefinitionCmd( } hyphenated = Tcl_ObjPrintf("-%s", TclGetString(propObj)); - Tcl_IncrRefCount(hyphenated); if (useInstance) { TclOORegisterInstanceProperty(oPtr, hyphenated, kind != KIND_WO, kind != KIND_RO); @@ -3708,7 +3728,7 @@ TclOOPropertyDefinitionCmd( TclOORegisterProperty(oPtr->classPtr, hyphenated, kind != KIND_WO, kind != KIND_RO); } - Tcl_DecrRefCount(hyphenated); + Tcl_BounceRefCount(hyphenated); /* * Create property implementation methods by using the right @@ -3722,8 +3742,6 @@ TclOOPropertyDefinitionCmd( Tcl_Obj *argsPtr = Tcl_NewObj(); Method *mPtr; - Tcl_IncrRefCount(getterName); - Tcl_IncrRefCount(argsPtr); Tcl_IncrRefCount(getterScript); if (useInstance) { mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, @@ -3732,8 +3750,8 @@ TclOOPropertyDefinitionCmd( mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, getterName, argsPtr, getterScript, NULL); } - Tcl_DecrRefCount(getterName); - Tcl_DecrRefCount(argsPtr); + Tcl_BounceRefCount(getterName); + Tcl_BounceRefCount(argsPtr); Tcl_DecrRefCount(getterScript); if (mPtr == NULL) { return TCL_ERROR; @@ -3745,8 +3763,6 @@ TclOOPropertyDefinitionCmd( Tcl_Obj *argsPtr = Tcl_NewStringObj("value", -1); Method *mPtr; - Tcl_IncrRefCount(setterName); - Tcl_IncrRefCount(argsPtr); Tcl_IncrRefCount(setterScript); if (useInstance) { mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, @@ -3755,8 +3771,8 @@ TclOOPropertyDefinitionCmd( mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, setterName, argsPtr, setterScript, NULL); } - Tcl_DecrRefCount(setterName); - Tcl_DecrRefCount(argsPtr); + Tcl_BounceRefCount(setterName); + Tcl_BounceRefCount(argsPtr); Tcl_DecrRefCount(setterScript); if (mPtr == NULL) { return TCL_ERROR; -- cgit v0.12 From 2b1ffc590c432efd1b8033462c7596945b7fd183 Mon Sep 17 00:00:00 2001 From: dkf Date: Sun, 21 Jul 2024 16:43:47 +0000 Subject: Reduce number of allocations on property access path. --- generic/tclOO.c | 3 +++ generic/tclOOBasic.c | 51 ++++++++++++++++++++++++++------------------------- generic/tclOOInt.h | 1 + 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index 8ae30e9..b6f9497 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -338,11 +338,13 @@ InitFoundation( TclNewLiteralStringObj(fPtr->destructorName, ""); TclNewLiteralStringObj(fPtr->clonedName, ""); TclNewLiteralStringObj(fPtr->defineName, "::oo::define"); + TclNewLiteralStringObj(fPtr->myName, "my"); Tcl_IncrRefCount(fPtr->unknownMethodNameObj); Tcl_IncrRefCount(fPtr->constructorName); Tcl_IncrRefCount(fPtr->destructorName); Tcl_IncrRefCount(fPtr->clonedName); Tcl_IncrRefCount(fPtr->defineName); + Tcl_IncrRefCount(fPtr->myName); Tcl_CreateObjCommand(interp, "::oo::UnknownDefinition", TclOOUnknownDefinition, NULL, NULL); TclNewLiteralStringObj(namePtr, "::oo::UnknownDefinition"); @@ -611,6 +613,7 @@ KillFoundation( TclDecrRefCount(fPtr->destructorName); TclDecrRefCount(fPtr->clonedName); TclDecrRefCount(fPtr->defineName); + TclDecrRefCount(fPtr->myName); TclOODecrRefCount(fPtr->objectCls->thisPtr); TclOODecrRefCount(fPtr->classCls->thisPtr); diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index e8b4e13..2f2583d 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -23,6 +23,18 @@ static Tcl_NRPostProc DecrRefsPostClassConstructor; static Tcl_NRPostProc FinalizeConstruction; static Tcl_NRPostProc FinalizeEval; static Tcl_NRPostProc NextRestoreFrame; + +/* Short-term cache for GetPropertyName(). */ +typedef struct GPNCache { + Tcl_Obj *listPtr; /* Holds references to names. */ + char *names[TCLFLEXARRAY]; /* NULL-terminated table of names. */ +} GPNCache; + +enum GPNFlags { + GPN_WRITABLE = 1, /* Are we looking for a writable property? */ + GPN_FALLING_BACK = 2 /* Are we doing a recursive call to determine + * if the property is of the other type? */ +}; /* * ---------------------------------------------------------------------- @@ -1382,16 +1394,17 @@ TclOOCopyObjectCmd( /* * Ugly thunks to read and write a property by calling the right method in - * the right way. + * the right way. Note that we MUST be correct in holding references to Tcl_Obj + * values, as this is potentially a call into user code. */ -static int +static inline int ReadProperty( Tcl_Interp *interp, Object *oPtr, Tcl_Obj *propObj) { Tcl_Obj *args[] = { - Tcl_NewStringObj("my", 2), + oPtr->fPtr->myName, Tcl_ObjPrintf("", TclGetString(propObj)) }; int code; @@ -1415,7 +1428,7 @@ ReadProperty( } } -static int +static inline int WriteProperty( Tcl_Interp *interp, Object *oPtr, @@ -1423,7 +1436,7 @@ WriteProperty( Tcl_Obj *valueObj) { Tcl_Obj *args[] = { - Tcl_NewStringObj("my", 2), + oPtr->fPtr->myName, Tcl_ObjPrintf("", TclGetString(propObj)), valueObj }; @@ -1450,18 +1463,6 @@ WriteProperty( } } -/* Short-term cache for GetPropertyName(). */ -struct Cache { - Tcl_Obj *listPtr; /* Holds references to names. */ - char *names[TCLFLEXARRAY]; /* NULL-terminated table of names. */ -}; - -enum GPNFlags { - GPN_WRITABLE = 1, /* Are we looking for a writable property? */ - GPN_FALLING_BACK = 2 /* Are we doing a recursive call to determine - * if the property is of the other type? */ -}; - /* Look up a property full name. */ static Tcl_Obj * GetPropertyName( @@ -1471,7 +1472,7 @@ GetPropertyName( * Can we do a fallback message? * See GPNFlags for possible values */ Tcl_Obj *namePtr, /* The name supplied by the user. */ - struct Cache **cachePtr) /* Where to cache the table, if the caller + GPNCache **cachePtr) /* Where to cache the table, if the caller * wants that. The contents are to be freed * with Tcl_Free if the cache is used. */ { @@ -1479,14 +1480,14 @@ GetPropertyName( Tcl_Obj *listPtr = TclOOGetAllObjectProperties( oPtr, flags & GPN_WRITABLE); Tcl_Obj **objv; - struct Cache *tablePtr; + GPNCache *tablePtr; (void) Tcl_ListObjGetElements(NULL, listPtr, &objc, &objv); if (cachePtr && *cachePtr) { tablePtr = *cachePtr; } else { - tablePtr = (struct Cache *) TclStackAlloc(interp, - offsetof(struct Cache, names) + sizeof(char *) * (objc + 1)); + tablePtr = (GPNCache *) TclStackAlloc(interp, + offsetof(GPNCache, names) + sizeof(char *) * (objc + 1)); for (i = 0; i < objc; i++) { tablePtr->names[i] = TclGetString(objv[i]); @@ -1538,13 +1539,13 @@ GetPropertyName( } /* Release the cache made by GetPropertyName(). */ -static void +static inline void ReleasePropertyNameCache( Tcl_Interp *interp, - struct Cache **cachePtr) + GPNCache **cachePtr) { if (*cachePtr) { - struct Cache *tablePtr = *cachePtr; + GPNCache *tablePtr = *cachePtr; if (tablePtr->listPtr) { Tcl_DecrRefCount(tablePtr->listPtr); } @@ -1636,7 +1637,7 @@ TclOO_Configurable_Configure( * Write properties. Slightly tricky because we want to cache the * table of property names. */ - struct Cache *cache = NULL; + GPNCache *cache = NULL; code = TCL_OK; for (i = 0; i < objc; i += 2) { diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index c31c461..124953d 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -402,6 +402,7 @@ typedef struct Foundation { Tcl_Obj *clonedName; /* Shared object containing the name of a * "" pseudo-constructor. */ Tcl_Obj *defineName; /* Fully qualified name of oo::define. */ + Tcl_Obj *myName; /* The "my" shared object. */ } Foundation; /* -- cgit v0.12 From 8eabb5e423efa1626ec924809aec72bc373787bf Mon Sep 17 00:00:00 2001 From: dkf Date: Mon, 29 Jul 2024 08:58:56 +0000 Subject: Factor out CmdFrame init and fix bug with body line numbers --- generic/tclOOMethod.c | 163 +++++++++++++++++++------------------------------- tests/oo.test | 70 ++++++++++++++++++++++ 2 files changed, 132 insertions(+), 101 deletions(-) diff --git a/generic/tclOOMethod.c b/generic/tclOOMethod.c index 6f14198..f60b1c2 100644 --- a/generic/tclOOMethod.c +++ b/generic/tclOOMethod.c @@ -502,45 +502,20 @@ TclOONewProcMethod( /* * ---------------------------------------------------------------------- * - * TclOOMakeProcInstanceMethod -- + * InitCmdFrame -- * - * The guts of the code to make a procedure-like method for an object. - * Split apart so that it is easier for other extensions to reuse (in - * particular, it frees them from having to pry so deeply into Tcl's - * guts). + * Set up a CmdFrame to record the source location for a procedure + * method. Assumes that the body is the last argument to the command + * creating the method, a good assumption because putting the body + * elsewhere is ugly. * * ---------------------------------------------------------------------- */ - -Tcl_Method -TclOOMakeProcInstanceMethod( - Tcl_Interp *interp, /* The interpreter containing the object. */ - Object *oPtr, /* The object to modify. */ - int flags, /* Whether this is a public method. */ - Tcl_Obj *nameObj, /* The name of the method, which _must not_ be - * NULL. */ - Tcl_Obj *argsObj, /* The formal argument list for the method, - * which _must not_ be NULL. */ - Tcl_Obj *bodyObj, /* The body of the method, which _must not_ be - * NULL. */ - const Tcl_MethodType *typePtr, - /* The type of the method to create. */ - void *clientData, /* The per-method type-specific data. */ - Proc **procPtrPtr) /* A pointer to the variable in which to write - * the procedure record reference. Presumably - * inside the structure indicated by the - * pointer in clientData. */ +static inline void +InitCmdFrame( + Interp *iPtr, /* Where source locations are recorded. */ + Proc *procPtr) /* Guts of the method being made. */ { - Interp *iPtr = (Interp *) interp; - Proc *procPtr; - - if (TclCreateProc(interp, NULL, TclGetString(nameObj), argsObj, bodyObj, - procPtrPtr) != TCL_OK) { - return NULL; - } - procPtr = *procPtrPtr; - procPtr->cmdPtr = NULL; - if (iPtr->cmdFramePtr) { CmdFrame context = *iPtr->cmdFramePtr; @@ -565,20 +540,23 @@ TclOOMakeProcInstanceMethod( if (context.type == TCL_LOCATION_SOURCE) { /* * We can account for source location within a proc only if the - * proc body was not created by substitution. + * proc body was not created by substitution. This is where we + * assume that the body is the last argument; the index of the body + * is NOT a fixed count of arguments in because of the alternate + * form of [oo::define]/[oo::objdefine]. * (FIXME: check that this is sane and correct!) */ - if (context.line - && (context.nline >= 4) && (context.line[3] >= 0)) { + if (context.line && context.nline > 1 + && (context.line[context.nline - 1] >= 0)) { int isNew; CmdFrame *cfPtr = (CmdFrame *)Tcl_Alloc(sizeof(CmdFrame)); Tcl_HashEntry *hPtr; cfPtr->level = -1; cfPtr->type = context.type; - cfPtr->line = (Tcl_Size *)Tcl_Alloc(sizeof(Tcl_Size)); - cfPtr->line[0] = context.line[3]; + cfPtr->line = (Tcl_Size *) Tcl_Alloc(sizeof(Tcl_Size)); + cfPtr->line[0] = context.line[context.nline - 1]; cfPtr->nline = 1; cfPtr->framePtr = NULL; cfPtr->nextPtr = NULL; @@ -602,7 +580,51 @@ TclOOMakeProcInstanceMethod( Tcl_DecrRefCount(context.data.eval.path); context.data.eval.path = NULL; } + }} + +/* + * ---------------------------------------------------------------------- + * + * TclOOMakeProcInstanceMethod -- + * + * The guts of the code to make a procedure-like method for an object. + * Split apart so that it is easier for other extensions to reuse (in + * particular, it frees them from having to pry so deeply into Tcl's + * guts). + * + * ---------------------------------------------------------------------- + */ + +Tcl_Method +TclOOMakeProcInstanceMethod( + Tcl_Interp *interp, /* The interpreter containing the object. */ + Object *oPtr, /* The object to modify. */ + int flags, /* Whether this is a public method. */ + Tcl_Obj *nameObj, /* The name of the method, which _must not_ be + * NULL. */ + Tcl_Obj *argsObj, /* The formal argument list for the method, + * which _must not_ be NULL. */ + Tcl_Obj *bodyObj, /* The body of the method, which _must not_ be + * NULL. */ + const Tcl_MethodType *typePtr, + /* The type of the method to create. */ + void *clientData, /* The per-method type-specific data. */ + Proc **procPtrPtr) /* A pointer to the variable in which to write + * the procedure record reference. Presumably + * inside the structure indicated by the + * pointer in clientData. */ +{ + Interp *iPtr = (Interp *) interp; + Proc *procPtr; + + if (TclCreateProc(interp, NULL, TclGetString(nameObj), argsObj, bodyObj, + procPtrPtr) != TCL_OK) { + return NULL; } + procPtr = *procPtrPtr; + procPtr->cmdPtr = NULL; + + InitCmdFrame(iPtr, procPtr); return TclNewInstanceMethod(interp, (Tcl_Object) oPtr, nameObj, flags, typePtr, clientData); @@ -654,68 +676,7 @@ TclOOMakeProcMethod( procPtr = *procPtrPtr; procPtr->cmdPtr = NULL; - if (iPtr->cmdFramePtr) { - CmdFrame context = *iPtr->cmdFramePtr; - - if (context.type == TCL_LOCATION_BC) { - /* - * Retrieve source information from the bytecode, if possible. If - * the information is retrieved successfully, context.type will be - * TCL_LOCATION_SOURCE and the reference held by - * context.data.eval.path will be counted. - */ - - TclGetSrcInfoForPc(&context); - } else if (context.type == TCL_LOCATION_SOURCE) { - /* - * The copy into 'context' up above has created another reference - * to 'context.data.eval.path'; account for it. - */ - - Tcl_IncrRefCount(context.data.eval.path); - } - - if (context.type == TCL_LOCATION_SOURCE) { - /* - * We can account for source location within a proc only if the - * proc body was not created by substitution. - * (FIXME: check that this is sane and correct!) - */ - - if (context.line - && (context.nline >= 4) && (context.line[3] >= 0)) { - int isNew; - CmdFrame *cfPtr = (CmdFrame *)Tcl_Alloc(sizeof(CmdFrame)); - Tcl_HashEntry *hPtr; - - cfPtr->level = -1; - cfPtr->type = context.type; - cfPtr->line = (Tcl_Size *)Tcl_Alloc(sizeof(Tcl_Size)); - cfPtr->line[0] = context.line[3]; - cfPtr->nline = 1; - cfPtr->framePtr = NULL; - cfPtr->nextPtr = NULL; - - cfPtr->data.eval.path = context.data.eval.path; - Tcl_IncrRefCount(cfPtr->data.eval.path); - - cfPtr->cmd = NULL; - cfPtr->len = 0; - - hPtr = Tcl_CreateHashEntry(iPtr->linePBodyPtr, - procPtr, &isNew); - Tcl_SetHashValue(hPtr, cfPtr); - } - - /* - * 'context' is going out of scope; account for the reference that - * it's holding to the path name. - */ - - Tcl_DecrRefCount(context.data.eval.path); - context.data.eval.path = NULL; - } - } + InitCmdFrame(iPtr, procPtr); return TclNewMethod(interp, (Tcl_Class) clsPtr, nameObj, flags, typePtr, clientData); diff --git a/tests/oo.test b/tests/oo.test index 769a96b..6f8d5b5 100644 --- a/tests/oo.test +++ b/tests/oo.test @@ -3620,6 +3620,76 @@ test oo-22.6 {OO and coroutines and info frame: Bug 87271f7cd6} -body { catch {B destroy} } -match glob -result {{* method Work class ::A *} {* method Work class ::A *}} WorkerBase destroy +test oo-22.7 {oo::define and info frame: correct argument line} -setup { + oo::class create C { + variable base + constructor {info} {set base [dict get $info line]} + method Relative {} { + set info [next] + if {![dict exists $info file]} { + error "no file-relative line info: $info" + } + expr {[dict get $info line] - $base} + } + filter Relative + } +} -body { + C create o [info frame 0] + oo::define C { + method line1 {} {info frame 0} + method line2 { + } {info frame 0} + method line3 { + } { + info frame 0 + } + } + oo::define C method line4 {} {info frame 0} + oo::define C method line5 { + } {info frame 0} + oo::define C method line6 { + } { + info frame 0 + } + list [o line1] [o line2] [o line3] [o line4] [o line5] [o line6] +} -cleanup { + C destroy +} -result {2 4 7 10 12 15} +test oo-22.8 {oo::objdefine and info frame: correct argument line} -setup { + oo::class create C { + variable base + constructor {info} {set base [dict get $info line]} + method Relative {} { + set info [next] + if {![dict exists $info file]} { + error "no file-relative line info: $info" + } + expr {[dict get $info line] - $base} + } + filter Relative + } +} -body { + C create o [info frame 0] + oo::objdefine o { + method line1 {} {info frame 0} + method line2 { + } {info frame 0} + method line3 { + } { + info frame 0 + } + } + oo::objdefine o method line4 {} {info frame 0} + oo::objdefine o method line5 { + } {info frame 0} + oo::objdefine o method line6 { + } { + info frame 0 + } + list [o line1] [o line2] [o line3] [o line4] [o line5] [o line6] +} -cleanup { + C destroy +} -result {2 4 7 10 12 15} # Prove that the issue in [Bug 1865054] isn't an issue any more test oo-23.1 {Self-like derivation; complex case!} -setup { -- cgit v0.12 From e078e228c6de04b8a147c5a30d6a7d3e2632a1e0 Mon Sep 17 00:00:00 2001 From: dkf Date: Thu, 1 Aug 2024 15:38:25 +0000 Subject: Use Tcl's internal API a bit better --- generic/tclOO.c | 269 +++++++++++++++++++++++++--------------------- generic/tclOO.h | 29 ++--- generic/tclOOBasic.c | 70 ++++++------ generic/tclOOCall.c | 170 ++++++++++++++--------------- generic/tclOODefineCmds.c | 105 ++++++++++-------- generic/tclOOInfo.c | 126 +++++++++++++++------- generic/tclOOInt.h | 243 +++++++++++++++++++++-------------------- generic/tclOOMethod.c | 128 +++++++++++----------- 8 files changed, 620 insertions(+), 520 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index b6f9497..288b7f2 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -67,8 +67,6 @@ static int CloneClassMethod(Tcl_Interp *interp, Class *clsPtr, Method **newMPtrPtr); static int CloneObjectMethod(Tcl_Interp *interp, Object *oPtr, Method *mPtr, Tcl_Obj *namePtr); -static void DeletedDefineNamespace(void *clientData); -static void DeletedObjdefNamespace(void *clientData); static void DeletedHelpersNamespace(void *clientData); static Tcl_NRPostProc FinalizeAlloc; static Tcl_NRPostProc FinalizeNext; @@ -291,6 +289,37 @@ TclOOGetFoundation( /* * ---------------------------------------------------------------------- * + * CreateCmdInNS -- + * + * Create a command in a namespace. Supports setting various + * implementation functions, but not a deletion callback or a clientData; + * it's suitable for use-cases in this file, no more. + * + * ---------------------------------------------------------------------- + */ +static inline void +CreateCmdInNS( + Tcl_Interp *interp, + Tcl_Namespace *namespacePtr, + const char *name, + Tcl_ObjCmdProc *cmdProc, + Tcl_ObjCmdProc *nreProc, + CompileProc *compileProc) +{ + Command *cmdPtr; + + if (cmdProc == NULL && nreProc == NULL) { + Tcl_Panic("must supply at least one implementation function"); + } + cmdPtr = (Command *) TclCreateObjCommandInNs(interp, name, + namespacePtr, cmdProc, NULL, NULL); + cmdPtr->nreProc = nreProc; + cmdPtr->compileProc = compileProc; +} + +/* + * ---------------------------------------------------------------------- + * * InitFoundation -- * * Set up the core of the OO core class system. This is a structure @@ -305,12 +334,11 @@ InitFoundation( Tcl_Interp *interp) { static Tcl_ThreadDataKey tsdKey; - ThreadLocalData *tsdPtr = - (ThreadLocalData *)Tcl_GetThreadData(&tsdKey, sizeof(ThreadLocalData)); - Foundation *fPtr = (Foundation *)Tcl_Alloc(sizeof(Foundation)); + ThreadLocalData *tsdPtr = (ThreadLocalData *) + Tcl_GetThreadData(&tsdKey, sizeof(ThreadLocalData)); + Foundation *fPtr = (Foundation *) Tcl_Alloc(sizeof(Foundation)); + Tcl_Namespace *define, *objdef, *cfg; Tcl_Obj *namePtr; - Tcl_DString buffer; - Command *cmdPtr; size_t i; /* @@ -324,15 +352,14 @@ InitFoundation( fPtr->interp = interp; fPtr->ooNs = Tcl_CreateNamespace(interp, "::oo", fPtr, NULL); Tcl_Export(interp, fPtr->ooNs, "[a-z]*", 1); - fPtr->defineNs = Tcl_CreateNamespace(interp, "::oo::define", fPtr, - DeletedDefineNamespace); - fPtr->objdefNs = Tcl_CreateNamespace(interp, "::oo::objdefine", fPtr, - DeletedObjdefNamespace); + define = Tcl_CreateNamespace(interp, "::oo::define", fPtr, NULL); + objdef = Tcl_CreateNamespace(interp, "::oo::objdefine", fPtr, NULL); fPtr->helpersNs = Tcl_CreateNamespace(interp, "::oo::Helpers", fPtr, DeletedHelpersNamespace); - Tcl_CreateNamespace(interp, "::oo::configuresupport", NULL, NULL); + cfg = Tcl_CreateNamespace(interp, "::oo::configuresupport", NULL, NULL); fPtr->epoch = 1; fPtr->tsdPtr = tsdPtr; + TclNewLiteralStringObj(fPtr->unknownMethodNameObj, "unknown"); TclNewLiteralStringObj(fPtr->constructorName, ""); TclNewLiteralStringObj(fPtr->destructorName, ""); @@ -345,30 +372,25 @@ InitFoundation( Tcl_IncrRefCount(fPtr->clonedName); Tcl_IncrRefCount(fPtr->defineName); Tcl_IncrRefCount(fPtr->myName); - Tcl_CreateObjCommand(interp, "::oo::UnknownDefinition", + + TclCreateObjCommandInNs(interp, "UnknownDefinition", fPtr->ooNs, TclOOUnknownDefinition, NULL, NULL); TclNewLiteralStringObj(namePtr, "::oo::UnknownDefinition"); - Tcl_SetNamespaceUnknownHandler(interp, fPtr->defineNs, namePtr); - Tcl_SetNamespaceUnknownHandler(interp, fPtr->objdefNs, namePtr); + Tcl_SetNamespaceUnknownHandler(interp, define, namePtr); + Tcl_SetNamespaceUnknownHandler(interp, objdef, namePtr); + Tcl_BounceRefCount(namePtr); /* * Create the subcommands in the oo::define and oo::objdefine spaces. */ - Tcl_DStringInit(&buffer); for (i = 0 ; defineCmds[i].name ; i++) { - TclDStringAppendLiteral(&buffer, "::oo::define::"); - Tcl_DStringAppend(&buffer, defineCmds[i].name, -1); - Tcl_CreateObjCommand(interp, Tcl_DStringValue(&buffer), + TclCreateObjCommandInNs(interp, defineCmds[i].name, define, defineCmds[i].objProc, INT2PTR(defineCmds[i].flag), NULL); - Tcl_DStringFree(&buffer); } for (i = 0 ; objdefCmds[i].name ; i++) { - TclDStringAppendLiteral(&buffer, "::oo::objdefine::"); - Tcl_DStringAppend(&buffer, objdefCmds[i].name, -1); - Tcl_CreateObjCommand(interp, Tcl_DStringValue(&buffer), + TclCreateObjCommandInNs(interp, objdefCmds[i].name, objdef, objdefCmds[i].objProc, INT2PTR(objdefCmds[i].flag), NULL); - Tcl_DStringFree(&buffer); } Tcl_CallWhenDeleted(interp, KillFoundation, NULL); @@ -384,10 +406,10 @@ InitFoundation( */ for (i = 0 ; objMethods[i].name ; i++) { - TclOONewBasicMethod(interp, fPtr->objectCls, &objMethods[i]); + TclOONewBasicMethod(fPtr->objectCls, &objMethods[i]); } for (i = 0 ; clsMethods[i].name ; i++) { - TclOONewBasicMethod(interp, fPtr->classCls, &clsMethods[i]); + TclOONewBasicMethod(fPtr->classCls, &clsMethods[i]); } /* @@ -399,7 +421,8 @@ InitFoundation( TclNewLiteralStringObj(namePtr, "new"); TclNewInstanceMethod(interp, (Tcl_Object) fPtr->classCls->thisPtr, namePtr /* keeps ref */, 0 /* private */, NULL, NULL); - fPtr->classCls->constructorPtr = (Method *) TclNewMethod(interp, + Tcl_BounceRefCount(namePtr); + fPtr->classCls->constructorPtr = (Method *) TclNewMethod( (Tcl_Class) fPtr->classCls, NULL, 0, &classConstructor, NULL); /* @@ -407,20 +430,17 @@ InitFoundation( * ensemble. */ - cmdPtr = (Command *) Tcl_NRCreateCommand(interp, "::oo::Helpers::next", - NULL, TclOONextObjCmd, NULL, NULL); - cmdPtr->compileProc = TclCompileObjectNextCmd; - cmdPtr = (Command *) Tcl_NRCreateCommand(interp, "::oo::Helpers::nextto", - NULL, TclOONextToObjCmd, NULL, NULL); - cmdPtr->compileProc = TclCompileObjectNextToCmd; - cmdPtr = (Command *) Tcl_CreateObjCommand(interp, "::oo::Helpers::self", - TclOOSelfObjCmd, NULL, NULL); - cmdPtr->compileProc = TclCompileObjectSelfCmd; - Tcl_CreateObjCommand(interp, "::oo::define", TclOODefineObjCmd, NULL, - NULL); - Tcl_CreateObjCommand(interp, "::oo::objdefine", TclOOObjDefObjCmd, NULL, - NULL); - Tcl_CreateObjCommand(interp, "::oo::copy", TclOOCopyObjectCmd, NULL,NULL); + CreateCmdInNS(interp, fPtr->helpersNs, "next", + NULL, TclOONextObjCmd, TclCompileObjectNextCmd); + CreateCmdInNS(interp, fPtr->helpersNs, "nextto", + NULL, TclOONextToObjCmd, TclCompileObjectNextToCmd); + CreateCmdInNS(interp, fPtr->helpersNs, "self", + TclOOSelfObjCmd, NULL, TclCompileObjectSelfCmd); + + CreateCmdInNS(interp, fPtr->ooNs, "define", TclOODefineObjCmd, NULL, NULL); + CreateCmdInNS(interp, fPtr->ooNs, "objdefine", TclOOObjDefObjCmd, NULL, NULL); + CreateCmdInNS(interp, fPtr->ooNs, "copy", TclOOCopyObjectCmd, NULL, NULL); + TclOOInitInfo(interp); /* @@ -439,13 +459,17 @@ InitFoundation( (Tcl_Class) fPtr->classCls, "::oo::configuresupport::configurable", NULL, -1, NULL, 0); for (i = 0 ; cfgMethods[i].name ; i++) { - TclOONewBasicMethod(interp, ((Object *) cfgCls)->classPtr, - &cfgMethods[i]); + TclOONewBasicMethod(((Object *) cfgCls)->classPtr, &cfgMethods[i]); } - Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdObjectProperties", + TclCreateObjCommandInNs(interp, "StdObjectProperties", cfg, TclOOInstallStdPropertyImplsCmd, (void *) 1, NULL); - Tcl_CreateObjCommand(interp, "::oo::configuresupport::StdClassProperties", + TclCreateObjCommandInNs(interp, "StdClassProperties", cfg, TclOOInstallStdPropertyImplsCmd, (void *) 0, NULL); + + /* + * Don't have handles to these namespaces, so use Tcl_CreateObjCommand. + */ + Tcl_CreateObjCommand(interp, "::oo::configuresupport::configurableobject::property", TclOOPropertyDefinitionCmd, (void *) 1, NULL); @@ -487,7 +511,7 @@ InitClassSystemRoots( fakeObject.refCount = 0; // Do not increment an uninitialized value. fPtr->objectCls = TclOOAllocClass(interp, - AllocObject(interp, "object", (Namespace *)fPtr->ooNs, NULL)); + AllocObject(interp, "object", (Namespace *) fPtr->ooNs, NULL)); // Corresponding TclOODecrRefCount in KillFoundation AddRef(fPtr->objectCls->thisPtr); @@ -512,7 +536,7 @@ InitClassSystemRoots( Tcl_IncrRefCount(defNsName); fPtr->classCls = TclOOAllocClass(interp, - AllocObject(interp, "class", (Namespace *)fPtr->ooNs, NULL)); + AllocObject(interp, "class", (Namespace *) fPtr->ooNs, NULL)); // Corresponding TclOODecrRefCount in KillFoundation AddRef(fPtr->classCls->thisPtr); @@ -554,37 +578,19 @@ InitClassSystemRoots( /* * ---------------------------------------------------------------------- * - * DeletedDefineNamespace, DeletedObjdefNamespace, DeletedHelpersNamespace -- + * DeletedHelpersNamespace -- * - * Simple helpers used to clear fields of the foundation when they no + * Simple helper used to clear fields of the foundation when they no * longer hold useful information. * * ---------------------------------------------------------------------- */ static void -DeletedDefineNamespace( - void *clientData) -{ - Foundation *fPtr = (Foundation *)clientData; - - fPtr->defineNs = NULL; -} - -static void -DeletedObjdefNamespace( - void *clientData) -{ - Foundation *fPtr = (Foundation *)clientData; - - fPtr->objdefNs = NULL; -} - -static void DeletedHelpersNamespace( void *clientData) { - Foundation *fPtr = (Foundation *)clientData; + Foundation *fPtr = (Foundation *) clientData; fPtr->helpersNs = NULL; } @@ -618,6 +624,12 @@ KillFoundation( TclOODecrRefCount(fPtr->classCls->thisPtr); Tcl_Free(fPtr); + + /* + * Don't leave the interpreter field pointing to freed data. + */ + + ((Interp *) interp)->objectFoundation = NULL; } /* @@ -657,7 +669,7 @@ AllocObject( CommandTrace *tracePtr; size_t creationEpoch; - oPtr = (Object *)Tcl_Alloc(sizeof(Object)); + oPtr = (Object *) Tcl_Alloc(sizeof(Object)); memset(oPtr, 0, sizeof(Object)); /* @@ -684,7 +696,8 @@ AllocObject( while (1) { char objName[10 + TCL_INTEGER_SPACE]; - snprintf(objName, sizeof(objName), "::oo::Obj%" TCL_Z_MODIFIER "u", ++fPtr->tsdPtr->nsCount); + snprintf(objName, sizeof(objName), "::oo::Obj%" TCL_Z_MODIFIER "u", + ++fPtr->tsdPtr->nsCount); oPtr->namespacePtr = Tcl_CreateNamespace(interp, objName, oPtr, NULL); if (oPtr->namespacePtr != NULL) { creationEpoch = fPtr->tsdPtr->nsCount; @@ -753,13 +766,13 @@ AllocObject( if (!nameStr) { nameStr = oPtr->namespacePtr->name; - nsPtr = (Namespace *)oPtr->namespacePtr; + nsPtr = (Namespace *) oPtr->namespacePtr; if (nsPtr->parentPtr != NULL) { nsPtr = nsPtr->parentPtr; } } oPtr->command = TclCreateObjCommandInNs(interp, nameStr, - (Tcl_Namespace *)nsPtr, TclOOPublicObjectCmd, oPtr, NULL); + (Tcl_Namespace *) nsPtr, TclOOPublicObjectCmd, oPtr, NULL); /* * Add the NRE command and trace directly. While this breaks a number of @@ -768,7 +781,8 @@ AllocObject( cmdPtr = (Command *) oPtr->command; cmdPtr->nreProc = PublicNRObjectCmd; - cmdPtr->tracePtr = tracePtr = (CommandTrace *)Tcl_Alloc(sizeof(CommandTrace)); + cmdPtr->tracePtr = tracePtr = (CommandTrace *) + Tcl_Alloc(sizeof(CommandTrace)); tracePtr->traceProc = ObjectRenamedTrace; tracePtr->clientData = oPtr; tracePtr->flags = TCL_TRACE_RENAME|TCL_TRACE_DELETE; @@ -822,7 +836,7 @@ MyDeleted( void *clientData) /* Reference to the object whose [my] has been * squelched. */ { - Object *oPtr = (Object *)clientData; + Object *oPtr = (Object *) clientData; oPtr->myCommand = NULL; } @@ -831,7 +845,7 @@ static void MyClassDeleted( void *clientData) { - Object *oPtr = (Object *)clientData; + Object *oPtr = (Object *) clientData; oPtr->myclassCommand = NULL; } @@ -856,7 +870,7 @@ ObjectRenamedTrace( TCL_UNUSED(const char *) /*newName*/, int flags) /* Why was the object deleted? */ { - Object *oPtr = (Object *)clientData; + Object *oPtr = (Object *) clientData; /* * If this is a rename and not a delete of the object, we just flush the @@ -1166,7 +1180,7 @@ ObjectNamespaceDeleted( void *clientData) /* Pointer to the class whose namespace is * being deleted. */ { - Object *oPtr = (Object *)clientData; + Object *oPtr = (Object *) clientData; Foundation *fPtr = oPtr->fPtr; FOREACH_HASH_DECLS; Class *mixinPtr; @@ -1248,14 +1262,14 @@ ObjectNamespaceDeleted( * as well. */ - Tcl_DeleteCommandFromToken(oPtr->fPtr->interp, oPtr->command); + Tcl_DeleteCommandFromToken(interp, oPtr->command); } if (oPtr->myclassCommand) { - Tcl_DeleteCommandFromToken(oPtr->fPtr->interp, oPtr->myclassCommand); + Tcl_DeleteCommandFromToken(interp, oPtr->myclassCommand); } if (oPtr->myCommand) { - Tcl_DeleteCommandFromToken(oPtr->fPtr->interp, oPtr->myCommand); + Tcl_DeleteCommandFromToken(interp, oPtr->myCommand); } /* @@ -1372,7 +1386,7 @@ ObjectNamespaceDeleted( * Delete the object structure itself. */ - TclNsDecrRefCount((Namespace *)oPtr->namespacePtr); + TclNsDecrRefCount((Namespace *) oPtr->namespacePtr); oPtr->namespacePtr = NULL; TclOODecrRefCount(oPtr->selfCls->thisPtr); oPtr->selfCls = NULL; @@ -1477,10 +1491,12 @@ TclOOAddToInstances( if (clsPtr->instances.num >= clsPtr->instances.size) { clsPtr->instances.size += ALLOC_CHUNK; if (clsPtr->instances.size == ALLOC_CHUNK) { - clsPtr->instances.list = (Object **)Tcl_Alloc(sizeof(Object *) * ALLOC_CHUNK); + clsPtr->instances.list = (Object **) + Tcl_Alloc(sizeof(Object *) * ALLOC_CHUNK); } else { - clsPtr->instances.list = (Object **)Tcl_Realloc(clsPtr->instances.list, - sizeof(Object *) * clsPtr->instances.size); + clsPtr->instances.list = (Object **) + Tcl_Realloc(clsPtr->instances.list, + sizeof(Object *) * clsPtr->instances.size); } } clsPtr->instances.list[clsPtr->instances.num++] = oPtr; @@ -1578,10 +1594,12 @@ TclOOAddToSubclasses( if (superPtr->subclasses.num >= superPtr->subclasses.size) { superPtr->subclasses.size += ALLOC_CHUNK; if (superPtr->subclasses.size == ALLOC_CHUNK) { - superPtr->subclasses.list = (Class **)Tcl_Alloc(sizeof(Class *) * ALLOC_CHUNK); + superPtr->subclasses.list = (Class **) + Tcl_Alloc(sizeof(Class *) * ALLOC_CHUNK); } else { - superPtr->subclasses.list = (Class **)Tcl_Realloc(superPtr->subclasses.list, - sizeof(Class *) * superPtr->subclasses.size); + superPtr->subclasses.list = (Class **) + Tcl_Realloc(superPtr->subclasses.list, + sizeof(Class *) * superPtr->subclasses.size); } } superPtr->subclasses.list[superPtr->subclasses.num++] = subPtr; @@ -1644,10 +1662,12 @@ TclOOAddToMixinSubs( if (superPtr->mixinSubs.num >= superPtr->mixinSubs.size) { superPtr->mixinSubs.size += ALLOC_CHUNK; if (superPtr->mixinSubs.size == ALLOC_CHUNK) { - superPtr->mixinSubs.list = (Class **)Tcl_Alloc(sizeof(Class *) * ALLOC_CHUNK); + superPtr->mixinSubs.list = (Class **) + Tcl_Alloc(sizeof(Class *) * ALLOC_CHUNK); } else { - superPtr->mixinSubs.list = (Class **)Tcl_Realloc(superPtr->mixinSubs.list, - sizeof(Class *) * superPtr->mixinSubs.size); + superPtr->mixinSubs.list = (Class **) + Tcl_Realloc(superPtr->mixinSubs.list, + sizeof(Class *) * superPtr->mixinSubs.size); } } superPtr->mixinSubs.list[superPtr->mixinSubs.num++] = subPtr; @@ -1692,7 +1712,7 @@ TclOOAllocClass( * representation. */ { Foundation *fPtr = GetFoundation(interp); - Class *clsPtr = (Class *)Tcl_Alloc(sizeof(Class)); + Class *clsPtr = (Class *) Tcl_Alloc(sizeof(Class)); memset(clsPtr, 0, sizeof(Class)); clsPtr->thisPtr = useThisObj; @@ -1709,7 +1729,7 @@ TclOOAllocClass( */ clsPtr->superclasses.num = 1; - clsPtr->superclasses.list = (Class **)Tcl_Alloc(sizeof(Class *)); + clsPtr->superclasses.list = (Class **) Tcl_Alloc(sizeof(Class *)); clsPtr->superclasses.list[0] = fPtr->objectCls; AddRef(fPtr->objectCls->thisPtr); @@ -1937,10 +1957,10 @@ FinalizeAlloc( Tcl_Interp *interp, int result) { - CallContext *contextPtr = (CallContext *)data[0]; - Object *oPtr = (Object *)data[1]; - Tcl_InterpState state = (Tcl_InterpState)data[2]; - Tcl_Object *objectPtr = (Tcl_Object *)data[3]; + CallContext *contextPtr = (CallContext *) data[0]; + Object *oPtr = (Object *) data[1]; + Tcl_InterpState state = (Tcl_InterpState) data[2]; + Tcl_Object *objectPtr = (Tcl_Object *) data[3]; /* * Ensure an error if the object was deleted in the constructor. Don't @@ -2161,11 +2181,12 @@ Tcl_CopyObjectInstance( TclOODecrRefCount(superPtr->thisPtr); } if (cls2Ptr->superclasses.num) { - cls2Ptr->superclasses.list = (Class **)Tcl_Realloc(cls2Ptr->superclasses.list, - sizeof(Class *) * clsPtr->superclasses.num); + cls2Ptr->superclasses.list = (Class **) + Tcl_Realloc(cls2Ptr->superclasses.list, + sizeof(Class *) * clsPtr->superclasses.num); } else { - cls2Ptr->superclasses.list = - (Class **)Tcl_Alloc(sizeof(Class *) * clsPtr->superclasses.num); + cls2Ptr->superclasses.list = (Class **) + Tcl_Alloc(sizeof(Class *) * clsPtr->superclasses.num); } memcpy(cls2Ptr->superclasses.list, clsPtr->superclasses.list, sizeof(Class *) * clsPtr->superclasses.num); @@ -2359,7 +2380,7 @@ CloneClassMethod( Method *m2Ptr; if (mPtr->typePtr == NULL) { - m2Ptr = (Method *) TclNewMethod(interp, (Tcl_Class) clsPtr, + m2Ptr = (Method *) TclNewMethod((Tcl_Class) clsPtr, namePtr, mPtr->flags & PUBLIC_METHOD, NULL, NULL); } else if (mPtr->typePtr->cloneProc) { void *newClientData; @@ -2368,11 +2389,11 @@ CloneClassMethod( &newClientData) != TCL_OK) { return TCL_ERROR; } - m2Ptr = (Method *) TclNewMethod(interp, (Tcl_Class) clsPtr, + m2Ptr = (Method *) TclNewMethod((Tcl_Class) clsPtr, namePtr, mPtr->flags & PUBLIC_METHOD, mPtr->typePtr, newClientData); } else { - m2Ptr = (Method *) TclNewMethod(interp, (Tcl_Class) clsPtr, + m2Ptr = (Method *) TclNewMethod((Tcl_Class) clsPtr, namePtr, mPtr->flags & PUBLIC_METHOD, mPtr->typePtr, mPtr->clientData); } @@ -2459,7 +2480,8 @@ Tcl_ClassSetMetadata( if (metadata == NULL) { return; } - clsPtr->metadataPtr = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + clsPtr->metadataPtr = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitHashTable(clsPtr->metadataPtr, TCL_ONE_WORD_KEYS); } @@ -2539,7 +2561,7 @@ Tcl_ObjectSetMetadata( if (metadata == NULL) { return; } - oPtr->metadataPtr = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->metadataPtr = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitHashTable(oPtr->metadataPtr, TCL_ONE_WORD_KEYS); } @@ -2588,7 +2610,7 @@ TclOOPublicObjectCmd( int objc, Tcl_Obj *const *objv) { - return Tcl_NRCallObjProc(interp, PublicNRObjectCmd, clientData,objc,objv); + return Tcl_NRCallObjProc(interp, PublicNRObjectCmd, clientData, objc, objv); } static int @@ -2598,8 +2620,8 @@ PublicNRObjectCmd( int objc, Tcl_Obj *const *objv) { - return TclOOObjectCmdCore((Object *)clientData, interp, objc, objv, PUBLIC_METHOD, - NULL); + return TclOOObjectCmdCore((Object *) clientData, interp, objc, objv, + PUBLIC_METHOD, NULL); } int @@ -2609,7 +2631,7 @@ TclOOPrivateObjectCmd( int objc, Tcl_Obj *const *objv) { - return Tcl_NRCallObjProc(interp, PrivateNRObjectCmd,clientData,objc,objv); + return Tcl_NRCallObjProc(interp, PrivateNRObjectCmd, clientData, objc, objv); } static int @@ -2619,7 +2641,7 @@ PrivateNRObjectCmd( int objc, Tcl_Obj *const *objv) { - return TclOOObjectCmdCore((Object *)clientData, interp, objc, objv, 0, NULL); + return TclOOObjectCmdCore((Object *) clientData, interp, objc, objv, 0, NULL); } int @@ -2680,7 +2702,7 @@ MyClassNRObjCmd( int objc, Tcl_Obj *const *objv) { - Object *oPtr = (Object *)clientData; + Object *oPtr = (Object *) clientData; if (objc < 2) { Tcl_WrongNumArgs(interp, 1, objv, "methodName ?arg ...?"); @@ -2739,7 +2761,7 @@ TclOOObjectCmdCore( */ if (framePtr->isProcCallFrame & FRAME_IS_METHOD) { - CallContext *callerContextPtr = (CallContext *)framePtr->clientData; + CallContext *callerContextPtr = (CallContext *) framePtr->clientData; Method *callerMethodPtr = callerContextPtr->callPtr->chain[callerContextPtr->index].mPtr; @@ -2816,8 +2838,7 @@ TclOOObjectCmdCore( if (startCls != NULL) { for (; contextPtr->index < contextPtr->callPtr->numChain; contextPtr->index++) { - struct MInvoke *miPtr = - &contextPtr->callPtr->chain[contextPtr->index]; + MInvoke *miPtr = &contextPtr->callPtr->chain[contextPtr->index]; if (miPtr->isFilter) { continue; @@ -2856,7 +2877,7 @@ FinalizeObjectCall( * structure. */ - TclOODeleteContext((CallContext *)data[0]); + TclOODeleteContext((CallContext *) data[0]); return result; } @@ -3012,7 +3033,7 @@ FinalizeNext( TCL_UNUSED(Tcl_Interp *), int result) { - CallContext *contextPtr = (CallContext *)data[0]; + CallContext *contextPtr = (CallContext *) data[0]; /* * Restore the call chain context index as we've finished the inner invoke @@ -3053,7 +3074,7 @@ Tcl_GetObjectFromObj( goto notAnObject; } } - return (Tcl_Object)cmdPtr->objClientData; + return (Tcl_Object) cmdPtr->objClientData; notAnObject: Tcl_SetObjResult(interp, Tcl_ObjPrintf( @@ -3169,49 +3190,49 @@ Tcl_Object Tcl_ObjectContextObject( Tcl_ObjectContext context) { - return (Tcl_Object) ((CallContext *)context)->oPtr; + return (Tcl_Object) ((CallContext *) context)->oPtr; } Tcl_Size Tcl_ObjectContextSkippedArgs( Tcl_ObjectContext context) { - return ((CallContext *)context)->skip; + return ((CallContext *) context)->skip; } Tcl_Namespace * Tcl_GetObjectNamespace( Tcl_Object object) { - return ((Object *)object)->namespacePtr; + return ((Object *) object)->namespacePtr; } Tcl_Command Tcl_GetObjectCommand( Tcl_Object object) { - return ((Object *)object)->command; + return ((Object *) object)->command; } Tcl_Class Tcl_GetObjectAsClass( Tcl_Object object) { - return (Tcl_Class) ((Object *)object)->classPtr; + return (Tcl_Class) ((Object *) object)->classPtr; } int Tcl_ObjectDeleted( Tcl_Object object) { - return ((Object *)object)->command == NULL; + return ((Object *) object)->command == NULL; } Tcl_Object Tcl_GetClassAsObject( Tcl_Class clazz) { - return (Tcl_Object) ((Class *)clazz)->thisPtr; + return (Tcl_Object) ((Class *) clazz)->thisPtr; } Tcl_ObjectMapMethodNameProc * diff --git a/generic/tclOO.h b/generic/tclOO.h index 7cda876..7adf559 100644 --- a/generic/tclOO.h +++ b/generic/tclOO.h @@ -81,7 +81,7 @@ typedef int (Tcl_ObjectMapMethodNameProc)(Tcl_Interp *interp, * how to create a clone of it (when the object or class is copied). */ -typedef struct { +typedef struct Tcl_MethodType { int version; /* Structure version field. Always to be equal * to TCL_OO_METHOD_VERSION_(1|CURRENT) in * declarations. */ @@ -99,7 +99,7 @@ typedef struct { } Tcl_MethodType; #if TCL_MAJOR_VERSION > 8 -typedef struct { +typedef struct Tcl_MethodType2 { int version; /* Structure version field. Always to be equal * to TCL_OO_METHOD_VERSION_2 in * declarations. */ @@ -124,19 +124,21 @@ typedef struct { * This allows new versions of the structure to be introduced without breaking * binary compatibility. */ - -#define TCL_OO_METHOD_VERSION_1 1 -#define TCL_OO_METHOD_VERSION_2 2 -#define TCL_OO_METHOD_VERSION_CURRENT 1 +enum TclOOMethodVersion { + TCL_OO_METHOD_VERSION_1 = 1, + TCL_OO_METHOD_VERSION_2 = 2 +}; +#define TCL_OO_METHOD_VERSION_CURRENT TCL_OO_METHOD_VERSION_1 /* * Visibility constants for the flags parameter to Tcl_NewMethod and * Tcl_NewInstanceMethod. */ - -#define TCL_OO_METHOD_PUBLIC 1 -#define TCL_OO_METHOD_UNEXPORTED 0 -#define TCL_OO_METHOD_PRIVATE 0x20 +enum TclOOMethodVisibilityFlags { + TCL_OO_METHOD_PUBLIC = 1, + TCL_OO_METHOD_UNEXPORTED = 0, + TCL_OO_METHOD_PRIVATE = 0x20 +}; /* * The type of some object (or class) metadata. This describes how to delete @@ -144,7 +146,7 @@ typedef struct { * clone of it (when the object or class is copied). */ -typedef struct { +typedef struct Tcl_ObjectMetadataType { int version; /* Structure version field. Always to be equal * to TCL_OO_METADATA_VERSION_CURRENT in * declarations. */ @@ -163,7 +165,10 @@ typedef struct { * without breaking binary compatibility. */ -#define TCL_OO_METADATA_VERSION_CURRENT 1 +enum TclOOMetadataVersion { + TCL_OO_METADATA_VERSION_1 = 1 +}; +#define TCL_OO_METADATA_VERSION_CURRENT TCL_OO_METADATA_VERSION_1 /* * Include all the public API, generated from tclOO.decls. diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index 2f2583d..cfb7cb5 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -68,7 +68,7 @@ FinalizeConstruction( Tcl_Interp *interp, int result) { - Object *oPtr = (Object *)data[0]; + Object *oPtr = (Object *) data[0]; if (result != TCL_OK) { return result; @@ -99,11 +99,11 @@ TclOO_Class_Constructor( Tcl_Obj **invoke, *nameObj; size_t skip = Tcl_ObjectContextSkippedArgs(context); - if ((size_t)objc > skip + 1) { + if ((size_t) objc > skip + 1) { Tcl_WrongNumArgs(interp, skip, objv, "?definitionScript?"); return TCL_ERROR; - } else if ((size_t)objc == skip) { + } else if ((size_t) objc == skip) { return TCL_OK; } @@ -112,17 +112,17 @@ TclOO_Class_Constructor( * here (and the class definition delegate doesn't run any constructors). */ - nameObj = Tcl_NewStringObj(oPtr->namespacePtr->fullName, -1); - Tcl_AppendToObj(nameObj, ":: oo ::delegate", -1); + nameObj = Tcl_ObjPrintf("%s:: oo ::delegate", + oPtr->namespacePtr->fullName); Tcl_NewObjectInstance(interp, (Tcl_Class) oPtr->fPtr->classCls, TclGetString(nameObj), NULL, -1, NULL, -1); - Tcl_DecrRefCount(nameObj); + Tcl_BounceRefCount(nameObj); /* * Delegate to [oo::define] to do the work. */ - invoke = (Tcl_Obj **)Tcl_Alloc(3 * sizeof(Tcl_Obj *)); + invoke = (Tcl_Obj **) TclStackAlloc(interp, 3 * sizeof(Tcl_Obj *)); invoke[0] = oPtr->fPtr->defineName; invoke[1] = TclOOObjectName(interp, oPtr); invoke[2] = objv[objc-1]; @@ -152,8 +152,8 @@ DecrRefsPostClassConstructor( Tcl_Interp *interp, int result) { - Tcl_Obj **invoke = (Tcl_Obj **)data[0]; - Object *oPtr = (Object *)data[1]; + Tcl_Obj **invoke = (Tcl_Obj **) data[0]; + Object *oPtr = (Object *) data[1]; Tcl_InterpState saved; int code; @@ -168,7 +168,7 @@ DecrRefsPostClassConstructor( code = Tcl_EvalObjv(interp, 2, invoke, 0); TclDecrRefCount(invoke[0]); TclDecrRefCount(invoke[1]); - Tcl_Free(invoke); + TclStackFree(interp, invoke); if (code != TCL_OK) { Tcl_DiscardInterpState(saved); return code; @@ -380,7 +380,7 @@ TclOO_Object_Destroy( Object *oPtr = (Object *) Tcl_ObjectContextObject(context); CallContext *contextPtr; - if (objc != (int)Tcl_ObjectContextSkippedArgs(context)) { + if (objc != (int) Tcl_ObjectContextSkippedArgs(context)) { Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), objv, NULL); return TCL_ERROR; @@ -410,7 +410,7 @@ AfterNRDestructor( Tcl_Interp *interp, int result) { - CallContext *contextPtr = (CallContext *)data[0]; + CallContext *contextPtr = (CallContext *) data[0]; if (contextPtr->oPtr->command) { Tcl_DeleteCommandFromToken(interp, contextPtr->oPtr->command); @@ -445,7 +445,7 @@ TclOO_Object_Eval( Tcl_Obj *scriptPtr; CmdFrame *invoker; - if ((size_t)objc < skip + 1) { + if ((size_t) objc < skip + 1) { Tcl_WrongNumArgs(interp, skip, objv, "arg ?arg ...?"); return TCL_ERROR; } @@ -474,7 +474,7 @@ TclOO_Object_Eval( * object when it decrements its refcount after eval'ing it. */ - if ((size_t)objc != skip+1) { + if ((size_t) objc != skip+1) { scriptPtr = Tcl_ConcatObj(objc-skip, objv+skip); invoker = NULL; } else { @@ -498,7 +498,7 @@ FinalizeEval( int result) { if (result == TCL_ERROR) { - Object *oPtr = (Object *)data[0]; + Object *oPtr = (Object *) data[0]; const char *namePtr; if (oPtr) { @@ -556,7 +556,7 @@ TclOO_Object_Unknown( * name without an error). */ - if ((size_t)objc < skip+1) { + if ((size_t) objc < skip + 1) { Tcl_WrongNumArgs(interp, skip, objv, "method ?arg ...?"); return TCL_ERROR; } @@ -567,7 +567,7 @@ TclOO_Object_Unknown( */ if (framePtr->isProcCallFrame & FRAME_IS_METHOD) { - CallContext *callerContext = (CallContext *)framePtr->clientData; + CallContext *callerContext = (CallContext *) framePtr->clientData; Method *mPtr = callerContext->callPtr->chain[ callerContext->index].mPtr; @@ -621,7 +621,7 @@ TclOO_Object_Unknown( Tcl_AppendToObj(errorMsg, " or ", -1); } Tcl_AppendToObj(errorMsg, methodNames[i], -1); - Tcl_Free((void *)methodNames); + Tcl_Free((void *) methodNames); Tcl_SetObjResult(interp, errorMsg); Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", TclGetString(objv[skip]), (char *)NULL); @@ -777,7 +777,7 @@ LookupObjectVar( if (framePtr->isProcCallFrame & FRAME_IS_METHOD) { Object *oPtr = (Object *) object; - CallContext *callerContext = (CallContext *)framePtr->clientData; + CallContext *callerContext = (CallContext *) framePtr->clientData; Method *mPtr = callerContext->callPtr->chain[ callerContext->index].mPtr; PrivateVariableMapping *pvPtr; @@ -818,9 +818,8 @@ LookupObjectVar( } // The namespace isn't the global one; necessarily true for any object! - varNamePtr = Tcl_NewStringObj(namespacePtr->fullName, -1); - Tcl_AppendToObj(varNamePtr, "::", 2); - Tcl_AppendObjToObj(varNamePtr, varName); + varNamePtr = Tcl_ObjPrintf("%s::%s", + namespacePtr->fullName, TclGetString(varName)); } Tcl_IncrRefCount(varNamePtr); Tcl_Var var = (Tcl_Var) TclObjLookupVar(interp, varNamePtr, NULL, @@ -930,7 +929,7 @@ TclOONextObjCmd( Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); return TCL_ERROR; } - context = (Tcl_ObjectContext)framePtr->clientData; + context = (Tcl_ObjectContext) framePtr->clientData; /* * Invoke the (advanced) method call context in the caller context. Note @@ -970,7 +969,7 @@ TclOONextToObjCmd( Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); return TCL_ERROR; } - contextPtr = (CallContext *)framePtr->clientData; + contextPtr = (CallContext *) framePtr->clientData; /* * Sanity check the arguments; we need the first one to refer to a class. @@ -984,7 +983,7 @@ TclOONextToObjCmd( if (object == NULL) { return TCL_ERROR; } - classPtr = ((Object *)object)->classPtr; + classPtr = ((Object *) object)->classPtr; if (classPtr == NULL) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( "\"%s\" is not a class", TclGetString(objv[1]))); @@ -999,7 +998,7 @@ TclOONextToObjCmd( */ for (i=contextPtr->index+1 ; icallPtr->numChain ; i++) { - struct MInvoke *miPtr = contextPtr->callPtr->chain + i; + MInvoke *miPtr = &contextPtr->callPtr->chain[i]; if (!miPtr->isFilter && miPtr->mPtr->declaringClassPtr == classPtr) { /* @@ -1030,7 +1029,7 @@ TclOONextToObjCmd( } for (i=contextPtr->index ; i != TCL_INDEX_NONE ; i--) { - struct MInvoke *miPtr = contextPtr->callPtr->chain + i; + MInvoke *miPtr = &contextPtr->callPtr->chain[i]; if (!miPtr->isFilter && miPtr->mPtr->declaringClassPtr == classPtr) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( @@ -1055,9 +1054,9 @@ NextRestoreFrame( int result) { Interp *iPtr = (Interp *) interp; - CallContext *contextPtr = (CallContext *)data[1]; + CallContext *contextPtr = (CallContext *) data[1]; - iPtr->varFramePtr = (CallFrame *)data[0]; + iPtr->varFramePtr = (CallFrame *) data[0]; if (contextPtr != NULL) { contextPtr->index = PTR2UINT(data[2]); } @@ -1110,7 +1109,7 @@ TclOOSelfObjCmd( return TCL_ERROR; } - contextPtr = (CallContext*)framePtr->clientData; + contextPtr = (CallContext *) framePtr->clientData; /* * Now we do "conventional" argument parsing for a while. Note that no @@ -1165,7 +1164,7 @@ TclOOSelfObjCmd( Tcl_SetErrorCode(interp, "TCL", "OO", "UNMATCHED_CONTEXT", (char *)NULL); return TCL_ERROR; } else { - struct MInvoke *miPtr = &CurrentlyInvoked(contextPtr); + MInvoke *miPtr = &CurrentlyInvoked(contextPtr); Object *oPtr; const char *type; @@ -1191,7 +1190,8 @@ TclOOSelfObjCmd( Tcl_SetErrorCode(interp, "TCL", "OO", "CONTEXT_REQUIRED", (char *)NULL); return TCL_ERROR; } else { - CallContext *callerPtr = (CallContext *)framePtr->callerVarPtr->clientData; + CallContext *callerPtr = (CallContext *) + framePtr->callerVarPtr->clientData; Method *mPtr = callerPtr->callPtr->chain[callerPtr->index].mPtr; Object *declarerPtr; @@ -1820,16 +1820,14 @@ TclOOImplementClassProperty( Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewMethod( - NULL, targetClass, methodName, 0, &GetterType, propNamePtr); + TclNewMethod(targetClass, methodName, 0, &GetterType, propNamePtr); Tcl_BounceRefCount(methodName); } if (installSetter) { Tcl_Obj *methodName = Tcl_ObjPrintf( "", TclGetString(propNamePtr)); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewMethod( - NULL, targetClass, methodName, 0, &SetterType, propNamePtr); + TclNewMethod(targetClass, methodName, 0, &SetterType, propNamePtr); Tcl_BounceRefCount(methodName); } } diff --git a/generic/tclOOCall.c b/generic/tclOOCall.c index e1cf778..27d8233 100644 --- a/generic/tclOOCall.c +++ b/generic/tclOOCall.c @@ -19,30 +19,29 @@ #include /* - * Structure containing a CallContext and any other values needed only during - * the construction of the CallContext. + * Structure containing a CallChain and any other values needed only during + * the construction of the CallChain. */ - -struct ChainBuilder { +typedef struct ChainBuilder { CallChain *callChainPtr; /* The call chain being built. */ size_t filterLength; /* Number of entries in the call chain that * are due to processing filters and not the * main call chain. */ Object *oPtr; /* The object that we are building the chain * for. */ -}; +} ChainBuilder; /* * Structures used for traversing the class hierarchy to find out where * definitions are supposed to be done. */ -typedef struct { +typedef struct DefineEntry { Class *definerCls; Tcl_Obj *namespaceName; } DefineEntry; -typedef struct { +typedef struct DefineChain { DefineEntry *list; int num; int size; @@ -51,15 +50,17 @@ typedef struct { /* * Extra flags used for call chain management. */ +enum CallChainFlags { + DEFINITE_PROTECTED = 0x100000, + DEFINITE_PUBLIC = 0x200000, + KNOWN_STATE = (DEFINITE_PROTECTED | DEFINITE_PUBLIC), + SPECIAL = (CONSTRUCTOR | DESTRUCTOR | FORCE_UNKNOWN), + BUILDING_MIXINS = 0x400000, + TRAVERSED_MIXIN = 0x800000, + OBJECT_MIXIN = 0x1000000, + DEFINE_FOR_CLASS = 0x2000000 +}; -#define DEFINITE_PROTECTED 0x100000 -#define DEFINITE_PUBLIC 0x200000 -#define KNOWN_STATE (DEFINITE_PROTECTED | DEFINITE_PUBLIC) -#define SPECIAL (CONSTRUCTOR | DESTRUCTOR | FORCE_UNKNOWN) -#define BUILDING_MIXINS 0x400000 -#define TRAVERSED_MIXIN 0x800000 -#define OBJECT_MIXIN 0x1000000 -#define DEFINE_FOR_CLASS 0x2000000 #define MIXIN_CONSISTENT(flags) \ (((flags) & OBJECT_MIXIN) || \ !((flags) & BUILDING_MIXINS) == !((flags) & TRAVERSED_MIXIN)) @@ -87,11 +88,19 @@ typedef struct { (((flags) & TRUE_PRIVATE_METHOD) != 0) /* + * Name the bits used in the names table values. + */ +enum NameTableValues { + IN_LIST = 1, /* Seen an implementation. */ + NO_IMPLEMENTATION = 2 /* Seen, but not implemented yet. */ +}; + +/* * Function declarations for things defined in this file. */ static void AddClassFiltersToCallContext(Object *const oPtr, - Class *clsPtr, struct ChainBuilder *const cbPtr, + Class *clsPtr, ChainBuilder *const cbPtr, Tcl_HashTable *const doneFilters, int flags); static void AddClassMethodNames(Class *clsPtr, int flags, Tcl_HashTable *const namesPtr, @@ -100,12 +109,12 @@ static inline void AddDefinitionNamespaceToChain(Class *const definerCls, Tcl_Obj *const namespaceName, DefineChain *const definePtr, int flags); static inline void AddMethodToCallChain(Method *const mPtr, - struct ChainBuilder *const cbPtr, + ChainBuilder *const cbPtr, Tcl_HashTable *const doneFilters, Class *const filterDecl, int flags); static inline int AddInstancePrivateToCallContext(Object *const oPtr, Tcl_Obj *const methodNameObj, - struct ChainBuilder *const cbPtr, int flags); + ChainBuilder *const cbPtr, int flags); static inline void AddStandardMethodName(int flags, Tcl_Obj *namePtr, Method *mPtr, Tcl_HashTable *namesPtr); static inline void AddPrivateMethodNames(Tcl_HashTable *methodsTablePtr, @@ -113,18 +122,18 @@ static inline void AddPrivateMethodNames(Tcl_HashTable *methodsTablePtr, static inline int AddSimpleChainToCallContext(Object *const oPtr, Class *const contextCls, Tcl_Obj *const methodNameObj, - struct ChainBuilder *const cbPtr, + ChainBuilder *const cbPtr, Tcl_HashTable *const doneFilters, int flags, Class *const filterDecl); static int AddPrivatesFromClassChainToCallContext(Class *classPtr, Class *const contextCls, Tcl_Obj *const methodNameObj, - struct ChainBuilder *const cbPtr, + ChainBuilder *const cbPtr, Tcl_HashTable *const doneFilters, int flags, Class *const filterDecl); static int AddSimpleClassChainToCallContext(Class *classPtr, Tcl_Obj *const methodNameObj, - struct ChainBuilder *const cbPtr, + ChainBuilder *const cbPtr, Tcl_HashTable *const doneFilters, int flags, Class *const filterDecl); static void AddSimpleClassDefineNamespaces(Class *classPtr, @@ -281,16 +290,16 @@ DupMethodNameRep( Tcl_Obj *srcPtr, Tcl_Obj *dstPtr) { - StashCallChain(dstPtr, - (CallChain *)TclFetchInternalRep(srcPtr, &methodNameType)->twoPtrValue.ptr1); + StashCallChain(dstPtr, (CallChain *) + TclFetchInternalRep(srcPtr, &methodNameType)->twoPtrValue.ptr1); } static void FreeMethodNameRep( Tcl_Obj *objPtr) { - TclOODeleteChain( - (CallChain *)TclFetchInternalRep(objPtr, &methodNameType)->twoPtrValue.ptr1); + TclOODeleteChain((CallChain *) + TclFetchInternalRep(objPtr, &methodNameType)->twoPtrValue.ptr1); } /* @@ -316,7 +325,7 @@ TclOOInvokeContext( int objc, /* The number of arguments. */ Tcl_Obj *const objv[]) /* The arguments as actually seen. */ { - CallContext *const contextPtr = (CallContext *)clientData; + CallContext *const contextPtr = (CallContext *) clientData; Method *const mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; const int isFilter = contextPtr->callPtr->chain[contextPtr->index].isFilter; @@ -375,7 +384,7 @@ TclOOInvokeContext( return (mPtr->typePtr->callProc)(mPtr->clientData, interp, (Tcl_ObjectContext) contextPtr, objc, objv); } - return ((Tcl_MethodCallProc2 *)(void *)(mPtr->typePtr->callProc))(mPtr->clientData, interp, + return (mPtr->type2Ptr->callProc)(mPtr->clientData, interp, (Tcl_ObjectContext) contextPtr, objc, objv); } @@ -385,7 +394,7 @@ SetFilterFlags( TCL_UNUSED(Tcl_Interp *), int result) { - CallContext *contextPtr = (CallContext *)data[0]; + CallContext *contextPtr = (CallContext *) data[0]; contextPtr->oPtr->flags |= FILTER_HANDLING; return result; @@ -397,7 +406,7 @@ ResetFilterFlags( TCL_UNUSED(Tcl_Interp *), int result) { - CallContext *contextPtr = (CallContext *)data[0]; + CallContext *contextPtr = (CallContext *) data[0]; contextPtr->oPtr->flags &= ~FILTER_HANDLING; return result; @@ -409,7 +418,7 @@ FinalizeMethodRefs( TCL_UNUSED(Tcl_Interp *), int result) { - CallContext *contextPtr = (CallContext *)data[0]; + CallContext *contextPtr = (CallContext *) data[0]; Tcl_Size i; for (i = 0 ; i < contextPtr->callPtr->numChain ; i++) { @@ -460,12 +469,6 @@ TclOOGetSortedMethodList( Tcl_InitHashTable(&examinedClasses, TCL_ONE_WORD_KEYS); /* - * Name the bits used in the names table values. - */ -#define IN_LIST 1 -#define NO_IMPLEMENTATION 2 - - /* * Process method names due to the object. */ @@ -619,7 +622,7 @@ SortMethodNames( * sorted when it is long enough to matter. */ - strings = (const char **)Tcl_Alloc(sizeof(char *) * namesPtr->numEntries); + strings = (const char **) Tcl_Alloc(sizeof(char *) * namesPtr->numEntries); FOREACH_HASH(namePtr, isWanted, namesPtr) { if (!WANT_PUBLIC(flags) || (PTR2INT(isWanted) & IN_LIST)) { if (PTR2INT(isWanted) & NO_IMPLEMENTATION) { @@ -641,7 +644,7 @@ SortMethodNames( } *stringsPtr = strings; } else { - Tcl_Free((void *)strings); + Tcl_Free((void *) strings); *stringsPtr = NULL; } return i; @@ -808,9 +811,6 @@ AddStandardMethodName( } } } - -#undef IN_LIST -#undef NO_IMPLEMENTATION /* * ---------------------------------------------------------------------- @@ -830,8 +830,7 @@ AddInstancePrivateToCallContext( Object *const oPtr, /* Object to add call chain entries for. */ Tcl_Obj *const methodName, /* Name of method to add the call chain * entries for. */ - struct ChainBuilder *const cbPtr, - /* Where to add the call chain entries. */ + ChainBuilder *const cbPtr, /* Where to add the call chain entries. */ int flags) /* What sort of call chain are we building. */ { Tcl_HashEntry *hPtr; @@ -841,7 +840,7 @@ AddInstancePrivateToCallContext( if (oPtr->methodsPtr) { hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, methodName); if (hPtr != NULL) { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (IS_PRIVATE(mPtr)) { AddMethodToCallChain(mPtr, cbPtr, NULL, NULL, flags); donePrivate = 1; @@ -873,8 +872,7 @@ AddSimpleChainToCallContext( Tcl_Obj *const methodNameObj, /* Name of method to add the call chain * entries for. */ - struct ChainBuilder *const cbPtr, - /* Where to add the call chain entries. */ + ChainBuilder *const cbPtr, /* Where to add the call chain entries. */ Tcl_HashTable *const doneFilters, /* Where to record what call chain entries * have been processed. */ @@ -892,7 +890,7 @@ AddSimpleChainToCallContext( hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, methodNameObj); if (hPtr != NULL) { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (!IS_PRIVATE(mPtr)) { if (WANT_PUBLIC(flags)) { if (!IS_PUBLIC(mPtr)) { @@ -922,7 +920,7 @@ AddSimpleChainToCallContext( if (oPtr->methodsPtr && !blockedUnexported) { hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, methodNameObj); if (hPtr != NULL) { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (!IS_PRIVATE(mPtr)) { AddMethodToCallChain(mPtr, cbPtr, doneFilters, filterDecl, flags); @@ -960,8 +958,7 @@ static inline void AddMethodToCallChain( Method *const mPtr, /* Actual method implementation to add to call * chain (or NULL, a no-op). */ - struct ChainBuilder *const cbPtr, - /* The call chain to add the method + ChainBuilder *const cbPtr, /* The call chain to add the method * implementation to. */ Tcl_HashTable *const doneFilters, /* Where to record what filters have been @@ -1046,13 +1043,13 @@ AddMethodToCallChain( */ if (callPtr->numChain == CALL_CHAIN_STATIC_SIZE) { - callPtr->chain = - (struct MInvoke *)Tcl_Alloc(sizeof(struct MInvoke) * (callPtr->numChain + 1)); + callPtr->chain = (MInvoke *) + Tcl_Alloc(sizeof(MInvoke) * (callPtr->numChain + 1)); memcpy(callPtr->chain, callPtr->staticChain, - sizeof(struct MInvoke) * callPtr->numChain); + sizeof(MInvoke) * callPtr->numChain); } else if (callPtr->numChain > CALL_CHAIN_STATIC_SIZE) { - callPtr->chain = (struct MInvoke *)Tcl_Realloc(callPtr->chain, - sizeof(struct MInvoke) * (callPtr->numChain + 1)); + callPtr->chain = (MInvoke *) Tcl_Realloc(callPtr->chain, + sizeof(MInvoke) * (callPtr->numChain + 1)); } callPtr->chain[i].mPtr = mPtr; callPtr->chain[i].isFilter = (doneFilters != NULL); @@ -1178,7 +1175,7 @@ TclOOGetCallContext( { CallContext *contextPtr; CallChain *callPtr; - struct ChainBuilder cb; + ChainBuilder cb; Tcl_Size i, count; int doFilters, donePrivate = 0; Tcl_HashEntry *hPtr; @@ -1224,7 +1221,7 @@ TclOOGetCallContext( const int reuseMask = (WANT_PUBLIC(flags) ? ~0 : ~PUBLIC_METHOD); if ((irPtr = TclFetchInternalRep(cacheInThisObj, &methodNameType))) { - callPtr = (CallChain *)irPtr->twoPtrValue.ptr1; + callPtr = (CallChain *) irPtr->twoPtrValue.ptr1; if (IsStillValid(callPtr, oPtr, flags, reuseMask)) { callPtr->refCount++; goto returnContext; @@ -1257,7 +1254,7 @@ TclOOGetCallContext( } if (hPtr != NULL && Tcl_GetHashValue(hPtr) != NULL) { - callPtr = (CallChain *)Tcl_GetHashValue(hPtr); + callPtr = (CallChain *) Tcl_GetHashValue(hPtr); if (IsStillValid(callPtr, oPtr, flags, reuseMask)) { callPtr->refCount++; goto returnContext; @@ -1269,7 +1266,7 @@ TclOOGetCallContext( doFilters = 1; } - callPtr = (CallChain *)Tcl_Alloc(sizeof(CallChain)); + callPtr = (CallChain *) Tcl_Alloc(sizeof(CallChain)); InitCallChain(callPtr, oPtr, flags); cb.callChainPtr = callPtr; @@ -1374,8 +1371,8 @@ TclOOGetCallContext( int isNew; if (oPtr->flags & USE_CLASS_CACHE) { if (oPtr->selfCls->classChainCache == NULL) { - oPtr->selfCls->classChainCache = - (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->selfCls->classChainCache = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(oPtr->selfCls->classChainCache); } @@ -1383,7 +1380,8 @@ TclOOGetCallContext( methodNameObj, &isNew); } else { if (oPtr->chainCache == NULL) { - oPtr->chainCache = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->chainCache = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(oPtr->chainCache); } @@ -1409,7 +1407,8 @@ TclOOGetCallContext( } returnContext: - contextPtr = (CallContext *)TclStackAlloc(oPtr->fPtr->interp, sizeof(CallContext)); + contextPtr = (CallContext *) + TclStackAlloc(oPtr->fPtr->interp, sizeof(CallContext)); contextPtr->oPtr = oPtr; /* @@ -1447,7 +1446,7 @@ TclOOGetStereotypeCallChain( * FILTER_HANDLING are useful. */ { CallChain *callPtr; - struct ChainBuilder cb; + ChainBuilder cb; Tcl_Size count; Foundation *fPtr = clsPtr->thisPtr->fPtr; Tcl_HashEntry *hPtr; @@ -1489,7 +1488,7 @@ TclOOGetStereotypeCallChain( if (hPtr != NULL && Tcl_GetHashValue(hPtr) != NULL) { const int reuseMask = (WANT_PUBLIC(flags) ? ~0 : ~PUBLIC_METHOD); - callPtr = (CallChain *)Tcl_GetHashValue(hPtr); + callPtr = (CallChain *) Tcl_GetHashValue(hPtr); if (IsStillValid(callPtr, &obj, flags, reuseMask)) { callPtr->refCount++; return callPtr; @@ -1501,7 +1500,7 @@ TclOOGetStereotypeCallChain( hPtr = NULL; } - callPtr = (CallChain *)Tcl_Alloc(sizeof(CallChain)); + callPtr = (CallChain *) Tcl_Alloc(sizeof(CallChain)); memset(callPtr, 0, sizeof(CallChain)); callPtr->flags = flags & (PUBLIC_METHOD|PRIVATE_METHOD|FILTER_HANDLING); callPtr->epoch = fPtr->epoch; @@ -1557,7 +1556,8 @@ TclOOGetStereotypeCallChain( if (hPtr == NULL) { int isNew; if (clsPtr->classChainCache == NULL) { - clsPtr->classChainCache = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + clsPtr->classChainCache = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(clsPtr->classChainCache); } hPtr = Tcl_CreateHashEntry(clsPtr->classChainCache, @@ -1585,8 +1585,7 @@ static void AddClassFiltersToCallContext( Object *const oPtr, /* Object that the filters operate on. */ Class *clsPtr, /* Class to get the filters from. */ - struct ChainBuilder *const cbPtr, - /* Context to fill with call chain entries. */ + ChainBuilder *const cbPtr, /* Context to fill with call chain entries. */ Tcl_HashTable *const doneFilters, /* Where to record what filters have been * processed. Keys are objects, values are @@ -1673,8 +1672,7 @@ AddPrivatesFromClassChainToCallContext( * also be added. */ Tcl_Obj *const methodName, /* Name of method to add the call chain * entries for. */ - struct ChainBuilder *const cbPtr, - /* Where to add the call chain entries. */ + ChainBuilder *const cbPtr, /* Where to add the call chain entries. */ Tcl_HashTable *const doneFilters, /* Where to record what call chain entries * have been processed. */ @@ -1715,7 +1713,7 @@ AddPrivatesFromClassChainToCallContext( methodName); if (hPtr != NULL) { - Method *mPtr = (Method *)Tcl_GetHashValue(hPtr); + Method *mPtr = (Method *) Tcl_GetHashValue(hPtr); if (IS_PRIVATE(mPtr)) { AddMethodToCallChain(mPtr, cbPtr, doneFilters, filterDecl, @@ -1758,8 +1756,7 @@ AddSimpleClassChainToCallContext( Tcl_Obj *const methodNameObj, /* Name of method to add the call chain * entries for. */ - struct ChainBuilder *const cbPtr, - /* Where to add the call chain entries. */ + ChainBuilder *const cbPtr, /* Where to add the call chain entries. */ Tcl_HashTable *const doneFilters, /* Where to record what call chain entries * have been processed. */ @@ -1804,7 +1801,7 @@ AddSimpleClassChainToCallContext( privateDanger |= 1; } if (hPtr != NULL) { - Method *mPtr = (Method *)Tcl_GetHashValue(hPtr); + Method *mPtr = (Method *) Tcl_GetHashValue(hPtr); if (!IS_PRIVATE(mPtr)) { if (!(flags & KNOWN_STATE)) { @@ -1864,13 +1861,9 @@ TclOORenderCallChain( */ TclNewLiteralStringObj(filterLiteral, "filter"); - Tcl_IncrRefCount(filterLiteral); TclNewLiteralStringObj(methodLiteral, "method"); - Tcl_IncrRefCount(methodLiteral); TclNewLiteralStringObj(objectLiteral, "object"); - Tcl_IncrRefCount(objectLiteral); TclNewLiteralStringObj(privateLiteral, "private"); - Tcl_IncrRefCount(privateLiteral); /* * Do the actual construction of the descriptions. They consist of a list @@ -1884,9 +1877,10 @@ TclOORenderCallChain( * method (or "object" if it is declared on the instance). */ - objv = (Tcl_Obj **)TclStackAlloc(interp, callPtr->numChain * sizeof(Tcl_Obj *)); + objv = (Tcl_Obj **) + TclStackAlloc(interp, callPtr->numChain * sizeof(Tcl_Obj *)); for (i = 0 ; i < callPtr->numChain ; i++) { - struct MInvoke *miPtr = &callPtr->chain[i]; + MInvoke *miPtr = &callPtr->chain[i]; descObjs[0] = miPtr->isFilter ? filterLiteral : @@ -1911,10 +1905,10 @@ TclOORenderCallChain( * they'll live on the description itself. */ - Tcl_DecrRefCount(filterLiteral); - Tcl_DecrRefCount(methodLiteral); - Tcl_DecrRefCount(objectLiteral); - Tcl_DecrRefCount(privateLiteral); + Tcl_BounceRefCount(filterLiteral); + Tcl_BounceRefCount(methodLiteral); + Tcl_BounceRefCount(objectLiteral); + Tcl_BounceRefCount(privateLiteral); /* * Finish building the description and return it. @@ -2152,12 +2146,12 @@ AddDefinitionNamespaceToChain( if (definePtr->num == DEFINE_CHAIN_STATIC_SIZE) { DefineEntry *staticList = definePtr->list; - definePtr->list = - (DefineEntry *)Tcl_Alloc(sizeof(DefineEntry) * definePtr->size); + definePtr->list = (DefineEntry *) + Tcl_Alloc(sizeof(DefineEntry) * definePtr->size); memcpy(definePtr->list, staticList, sizeof(DefineEntry) * definePtr->num); } else { - definePtr->list = (DefineEntry *)Tcl_Realloc(definePtr->list, + definePtr->list = (DefineEntry *) Tcl_Realloc(definePtr->list, sizeof(DefineEntry) * definePtr->size); } } diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index d3ec410..882ca52 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -32,13 +32,12 @@ /* * Some things that make it easier to declare a slot. */ - -struct DeclaredSlot { +typedef struct DeclaredSlot { const char *name; const Tcl_MethodType getterType; const Tcl_MethodType setterType; const Tcl_MethodType resolverType; -}; +} DeclaredSlot; #define SLOT(name,getter,setter,resolver) \ {"::oo::" name, \ @@ -156,7 +155,7 @@ static int ResolveClass(void *clientData, * Now define the slots used in declarations. */ -static const struct DeclaredSlot slots[] = { +static const DeclaredSlot slots[] = { SLOT("define::filter", ClassFilter_Get, ClassFilter_Set, NULL), SLOT("define::mixin", ClassMixin_Get, ClassMixin_Set, ResolveClass), SLOT("define::superclass", ClassSuper_Get, ClassSuper_Set, ResolveClass), @@ -360,9 +359,9 @@ TclOOObjectSetFilters( size_t size = sizeof(Tcl_Obj *) * numFilters; if (oPtr->filters.num == 0) { - filtersList = (Tcl_Obj **)Tcl_Alloc(size); + filtersList = (Tcl_Obj **) Tcl_Alloc(size); } else { - filtersList = (Tcl_Obj **)Tcl_Realloc(oPtr->filters.list, size); + filtersList = (Tcl_Obj **) Tcl_Realloc(oPtr->filters.list, size); } for (i = 0 ; i < numFilters ; i++) { filtersList[i] = filters[i]; @@ -419,9 +418,10 @@ TclOOClassSetFilters( size_t size = sizeof(Tcl_Obj *) * numFilters; if (classPtr->filters.num == 0) { - filtersList = (Tcl_Obj **)Tcl_Alloc(size); + filtersList = (Tcl_Obj **) Tcl_Alloc(size); } else { - filtersList = (Tcl_Obj **)Tcl_Realloc(classPtr->filters.list, size); + filtersList = (Tcl_Obj **) + Tcl_Realloc(classPtr->filters.list, size); } for (i = 0 ; i < numFilters ; i++) { filtersList[i] = filters[i]; @@ -475,10 +475,11 @@ TclOOObjectSetMixins( } TclOODecrRefCount(mixinPtr->thisPtr); } - oPtr->mixins.list = (Class **)Tcl_Realloc(oPtr->mixins.list, + oPtr->mixins.list = (Class **) Tcl_Realloc(oPtr->mixins.list, sizeof(Class *) * numMixins); } else { - oPtr->mixins.list = (Class **)Tcl_Alloc(sizeof(Class *) * numMixins); + oPtr->mixins.list = (Class **) + Tcl_Alloc(sizeof(Class *) * numMixins); oPtr->flags &= ~USE_CLASS_CACHE; } oPtr->mixins.num = numMixins; @@ -533,10 +534,12 @@ TclOOClassSetMixins( TclOORemoveFromMixinSubs(classPtr, mixinPtr); TclOODecrRefCount(mixinPtr->thisPtr); } - classPtr->mixins.list = (Class **)Tcl_Realloc(classPtr->mixins.list, - sizeof(Class *) * numMixins); + classPtr->mixins.list = (Class **) + Tcl_Realloc(classPtr->mixins.list, + sizeof(Class *) * numMixins); } else { - classPtr->mixins.list = (Class **)Tcl_Alloc(sizeof(Class *) * numMixins); + classPtr->mixins.list = (Class **) + Tcl_Alloc(sizeof(Class *) * numMixins); } classPtr->mixins.num = numMixins; memcpy(classPtr->mixins.list, mixins, sizeof(Class *) * numMixins); @@ -584,9 +587,10 @@ InstallStandardVariableMapping( if (varc == 0) { Tcl_Free(vnlPtr->list); } else if (i) { - vnlPtr->list = (Tcl_Obj **)Tcl_Realloc(vnlPtr->list, sizeof(Tcl_Obj *) * varc); + vnlPtr->list = (Tcl_Obj **) + Tcl_Realloc(vnlPtr->list, sizeof(Tcl_Obj *) * varc); } else { - vnlPtr->list = (Tcl_Obj **)Tcl_Alloc(sizeof(Tcl_Obj *) * varc); + vnlPtr->list = (Tcl_Obj **) Tcl_Alloc(sizeof(Tcl_Obj *) * varc); } } vnlPtr->num = 0; @@ -607,7 +611,8 @@ InstallStandardVariableMapping( */ if (n != varc) { - vnlPtr->list = (Tcl_Obj **)Tcl_Realloc(vnlPtr->list, sizeof(Tcl_Obj *) * n); + vnlPtr->list = (Tcl_Obj **) + Tcl_Realloc(vnlPtr->list, sizeof(Tcl_Obj *) * n); } Tcl_DeleteHashTable(&uniqueTable); } @@ -668,7 +673,7 @@ InstallPrivateVariableMapping( */ if (n != varc) { - pvlPtr->list = (PrivateVariableMapping *)Tcl_Realloc(pvlPtr->list, + pvlPtr->list = (PrivateVariableMapping *) Tcl_Realloc(pvlPtr->list, sizeof(PrivateVariableMapping) * n); } Tcl_DeleteHashTable(&uniqueTable); @@ -748,7 +753,7 @@ RenameDeleteMethod( * Complete the splicing by changing the method's name. */ - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (toPtr) { Tcl_IncrRefCount(toPtr); Tcl_DecrRefCount(mPtr->namePtr); @@ -975,7 +980,7 @@ TclOOGetDefineCmdContext( Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL); return NULL; } - object = (Tcl_Object)iPtr->varFramePtr->clientData; + object = (Tcl_Object) iPtr->varFramePtr->clientData; if (Tcl_ObjectDeleted(object)) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "this command cannot be called when the object has been" @@ -1107,7 +1112,7 @@ GenerateErrorInfo( Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( "\n (in definition script for %s \"%.*s%s\" line %d)", - typeOfSubject, (overflow ? limit : (int)length), objName, + typeOfSubject, (overflow ? limit : (int) length), objName, (overflow ? "..." : ""), Tcl_GetErrorLine(interp))); } @@ -1237,7 +1242,7 @@ TclOODefineObjCmd( Tcl_IncrRefCount(objNameObj); result = TclEvalObjEx(interp, objv[2], 0, - ((Interp *)interp)->cmdFramePtr, 2); + ((Interp *) interp)->cmdFramePtr, 2); if (result == TCL_ERROR) { GenerateErrorInfo(interp, oPtr, objNameObj, "class"); } @@ -1306,7 +1311,7 @@ TclOOObjDefObjCmd( Tcl_IncrRefCount(objNameObj); result = TclEvalObjEx(interp, objv[2], 0, - ((Interp *)interp)->cmdFramePtr, 2); + ((Interp *) interp)->cmdFramePtr, 2); if (result == TCL_ERROR) { GenerateErrorInfo(interp, oPtr, objNameObj, "object"); } @@ -1380,7 +1385,7 @@ TclOODefineSelfObjCmd( Tcl_IncrRefCount(objNameObj); result = TclEvalObjEx(interp, objv[1], 0, - ((Interp *)interp)->cmdFramePtr, 1); + ((Interp *) interp)->cmdFramePtr, 1); if (result == TCL_ERROR) { GenerateErrorInfo(interp, oPtr, objNameObj, "class object"); } @@ -1643,7 +1648,7 @@ TclOODefineConstructorObjCmd( return TCL_ERROR; } - (void)TclGetStringFromObj(objv[2], &bodyLength); + (void) TclGetStringFromObj(objv[2], &bodyLength); if (bodyLength > 0) { /* * Create the method structure. @@ -1837,7 +1842,7 @@ TclOODefineDestructorObjCmd( } - (void)TclGetStringFromObj(objv[1], &bodyLength); + (void) TclGetStringFromObj(objv[1], &bodyLength); if (bodyLength > 0) { /* * Create the method structure. @@ -1922,7 +1927,8 @@ TclOODefineExportObjCmd( if (isInstanceExport) { if (!oPtr->methodsPtr) { - oPtr->methodsPtr = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->methodsPtr = (Tcl_HashTable *) + Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(oPtr->methodsPtr); oPtr->flags &= ~USE_CLASS_CACHE; } @@ -1934,14 +1940,14 @@ TclOODefineExportObjCmd( } if (isNew) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); memset(mPtr, 0, sizeof(Method)); mPtr->refCount = 1; mPtr->namePtr = objv[i]; Tcl_IncrRefCount(objv[i]); Tcl_SetHashValue(hPtr, mPtr); } else { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); } if (isNew || !(mPtr->flags & (PUBLIC_METHOD | PRIVATE_METHOD))) { mPtr->flags |= PUBLIC_METHOD; @@ -2248,14 +2254,14 @@ TclOODefineUnexportObjCmd( } if (isNew) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); memset(mPtr, 0, sizeof(Method)); mPtr->refCount = 1; mPtr->namePtr = objv[i]; Tcl_IncrRefCount(objv[i]); Tcl_SetHashValue(hPtr, mPtr); } else { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); } if (isNew || mPtr->flags & (PUBLIC_METHOD | TRUE_PRIVATE_METHOD)) { mPtr->flags &= ~(PUBLIC_METHOD | TRUE_PRIVATE_METHOD); @@ -2347,30 +2353,38 @@ int TclOODefineSlots( Foundation *fPtr) { - const struct DeclaredSlot *slotInfoPtr; - Tcl_Obj *getName = Tcl_NewStringObj("Get", -1); - Tcl_Obj *setName = Tcl_NewStringObj("Set", -1); - Tcl_Obj *resolveName = Tcl_NewStringObj("Resolve", -1); + const DeclaredSlot *slotInfoPtr; + Tcl_Interp *interp = fPtr->interp; + Tcl_Obj *getName, *setName, *resolveName; + Tcl_Object object = Tcl_NewObjectInstance(interp, (Tcl_Class) + fPtr->classCls, "::oo::Slot", NULL, TCL_INDEX_NONE, NULL, 0); Class *slotCls; - slotCls = ((Object *) Tcl_NewObjectInstance(fPtr->interp, (Tcl_Class) - fPtr->classCls, "::oo::Slot", NULL, TCL_INDEX_NONE, NULL, 0))->classPtr; + if (object == NULL) { + return TCL_ERROR; + } + slotCls = ((Object *) object)->classPtr; if (slotCls == NULL) { return TCL_ERROR; } + + TclNewLiteralStringObj(getName, "Get"); + TclNewLiteralStringObj(setName, "Set"); + TclNewLiteralStringObj(resolveName, "Resolve"); for (slotInfoPtr = slots ; slotInfoPtr->name ; slotInfoPtr++) { - Tcl_Object slotObject = Tcl_NewObjectInstance(fPtr->interp, - (Tcl_Class) slotCls, slotInfoPtr->name, NULL, TCL_INDEX_NONE, NULL, 0); + Tcl_Object slotObject = Tcl_NewObjectInstance(interp, + (Tcl_Class) slotCls, slotInfoPtr->name, NULL, TCL_INDEX_NONE, + NULL, 0); if (slotObject == NULL) { continue; } - TclNewInstanceMethod(fPtr->interp, slotObject, getName, 0, + TclNewInstanceMethod(interp, slotObject, getName, 0, &slotInfoPtr->getterType, NULL); - TclNewInstanceMethod(fPtr->interp, slotObject, setName, 0, + TclNewInstanceMethod(interp, slotObject, setName, 0, &slotInfoPtr->setterType, NULL); if (slotInfoPtr->resolverType.callProc) { - TclNewInstanceMethod(fPtr->interp, slotObject, resolveName, 0, + TclNewInstanceMethod(interp, slotObject, resolveName, 0, &slotInfoPtr->resolverType, NULL); } } @@ -2522,7 +2536,7 @@ ClassMixin_Set( return TCL_ERROR; } - mixins = (Class **)TclStackAlloc(interp, sizeof(Class *) * mixinc); + mixins = (Class **) TclStackAlloc(interp, sizeof(Class *) * mixinc); Tcl_InitHashTable(&uniqueCheck, TCL_ONE_WORD_KEYS); for (i = 0; i < mixinc; i++) { @@ -2647,7 +2661,7 @@ ClassSuper_Set( */ if (superc == 0) { - superclasses = (Class **)Tcl_Realloc(superclasses, sizeof(Class *)); + superclasses = (Class **) Tcl_Realloc(superclasses, sizeof(Class *)); if (TclOOIsReachable(fPtr->classCls, clsPtr)) { superclasses[0] = fPtr->classCls; } else { @@ -2959,7 +2973,7 @@ ObjMixin_Set( return TCL_ERROR; } - mixins = (Class **)TclStackAlloc(interp, sizeof(Class *) * mixinc); + mixins = (Class **) TclStackAlloc(interp, sizeof(Class *) * mixinc); Tcl_InitHashTable(&uniqueCheck, TCL_ONE_WORD_KEYS); for (i = 0; i < mixinc; i++) { @@ -3760,9 +3774,10 @@ TclOOPropertyDefinitionCmd( if (setterScript != NULL) { Tcl_Obj *setterName = Tcl_ObjPrintf("", TclGetString(propObj)); - Tcl_Obj *argsPtr = Tcl_NewStringObj("value", -1); + Tcl_Obj *argsPtr; Method *mPtr; + TclNewLiteralStringObj(argsPtr, "value"); Tcl_IncrRefCount(setterScript); if (useInstance) { mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, diff --git a/generic/tclOOInfo.c b/generic/tclOOInfo.c index 4555c39..feac2c0 100644 --- a/generic/tclOOInfo.c +++ b/generic/tclOOInfo.c @@ -95,6 +95,23 @@ static const EnsembleImplMap infoClassCmds[] = { /* * ---------------------------------------------------------------------- * + * LocalVarName -- + * + * Get the name of a local variable (especially a method argument) as a + * Tcl value. + * + * ---------------------------------------------------------------------- + */ +static inline Tcl_Obj * +LocalVarName( + CompiledLocal *localPtr) +{ + return Tcl_NewStringObj(localPtr->name, TCL_AUTO_LENGTH); +} + +/* + * ---------------------------------------------------------------------- + * * TclOOInitInfo -- * * Adjusts the Tcl core [info] command to contain subcommands ("object" @@ -256,22 +273,17 @@ InfoObjectDefnCmd( } hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, objv[2]); if (hPtr == NULL) { - unknownMethod: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "unknown method \"%s\"", TclGetString(objv[2]))); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto unknownMethod; } - procPtr = TclOOGetProcFromMethod((Method *)Tcl_GetHashValue(hPtr)); + procPtr = TclOOGetProcFromMethod((Method *) Tcl_GetHashValue(hPtr)); if (procPtr == NULL) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "definition not available for this kind of method", -1)); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto wrongType; } + /* + * We now have the method to describe the definition of. + */ + TclNewObj(resultObjs[0]); for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL; localPtr=localPtr->nextPtr) { @@ -279,17 +291,34 @@ InfoObjectDefnCmd( Tcl_Obj *argObj; TclNewObj(argObj); - Tcl_ListObjAppendElement(NULL, argObj, - Tcl_NewStringObj(localPtr->name, -1)); + Tcl_ListObjAppendElement(NULL, argObj, LocalVarName(localPtr)); if (localPtr->defValuePtr != NULL) { Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr); } Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj); } } - resultObjs[1] = TclOOGetMethodBody((Method *)Tcl_GetHashValue(hPtr)); + resultObjs[1] = TclOOGetMethodBody((Method *) Tcl_GetHashValue(hPtr)); Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs)); return TCL_OK; + + /* + * Errors... + */ + + unknownMethod: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "unknown method \"%s\"", TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; + + wrongType: + Tcl_SetObjResult(interp, Tcl_NewStringObj( + "definition not available for this kind of method", -1)); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; } /* @@ -367,25 +396,38 @@ InfoObjectForwardCmd( } hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, objv[2]); if (hPtr == NULL) { - unknownMethod: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "unknown method \"%s\"", TclGetString(objv[2]))); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto unknownMethod; } - prefixObj = TclOOGetFwdFromMethod((Method *)Tcl_GetHashValue(hPtr)); + prefixObj = TclOOGetFwdFromMethod((Method *) Tcl_GetHashValue(hPtr)); if (prefixObj == NULL) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "prefix argument list not available for this kind of method", - -1)); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto wrongType; } + /* + * Describe the valid forward method. + */ + Tcl_SetObjResult(interp, prefixObj); return TCL_OK; + + /* + * Errors... + */ + + unknownMethod: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "unknown method \"%s\"", TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; + + wrongType: + Tcl_SetObjResult(interp, Tcl_NewStringObj( + "prefix argument list not available for this kind of method", + -1)); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; } /* @@ -544,6 +586,10 @@ InfoObjectMethodsCmd( SCOPE_LOCALPRIVATE }; + /* + * Parse arguments. + */ + if (objc < 2) { Tcl_WrongNumArgs(interp, 1, objv, "objName ?-option value ...?"); return TCL_ERROR; @@ -604,6 +650,10 @@ InfoObjectMethodsCmd( } } + /* + * List matching methods. + */ + TclNewObj(resultObj); if (recurse) { const char **names; @@ -615,7 +665,7 @@ InfoObjectMethodsCmd( Tcl_NewStringObj(names[i], -1)); } if (numNames > 0) { - Tcl_Free((void *)names); + Tcl_Free((void *) names); } } else if (oPtr->methodsPtr) { if (scope == -1) { @@ -684,7 +734,7 @@ InfoObjectMethodTypeCmd( TclGetString(objv[2]), (char *)NULL); return TCL_ERROR; } - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr == NULL) { /* * Special entry for visibility control: pretend the method doesnt @@ -970,8 +1020,7 @@ InfoClassConstrCmd( Tcl_Obj *argObj; TclNewObj(argObj); - Tcl_ListObjAppendElement(NULL, argObj, - Tcl_NewStringObj(localPtr->name, -1)); + Tcl_ListObjAppendElement(NULL, argObj, LocalVarName(localPtr)); if (localPtr->defValuePtr != NULL) { Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr); } @@ -1022,7 +1071,7 @@ InfoClassDefnCmd( TclGetString(objv[2]), (char *)NULL); return TCL_ERROR; } - procPtr = TclOOGetProcFromMethod((Method *)Tcl_GetHashValue(hPtr)); + procPtr = TclOOGetProcFromMethod((Method *) Tcl_GetHashValue(hPtr)); if (procPtr == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "definition not available for this kind of method", -1)); @@ -1038,15 +1087,14 @@ InfoClassDefnCmd( Tcl_Obj *argObj; TclNewObj(argObj); - Tcl_ListObjAppendElement(NULL, argObj, - Tcl_NewStringObj(localPtr->name, -1)); + Tcl_ListObjAppendElement(NULL, argObj, LocalVarName(localPtr)); if (localPtr->defValuePtr != NULL) { Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr); } Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj); } } - resultObjs[1] = TclOOGetMethodBody((Method *)Tcl_GetHashValue(hPtr)); + resultObjs[1] = TclOOGetMethodBody((Method *) Tcl_GetHashValue(hPtr)); Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs)); return TCL_OK; } @@ -1220,7 +1268,7 @@ InfoClassForwardCmd( TclGetString(objv[2]), (char *)NULL); return TCL_ERROR; } - prefixObj = TclOOGetFwdFromMethod((Method *)Tcl_GetHashValue(hPtr)); + prefixObj = TclOOGetFwdFromMethod((Method *) Tcl_GetHashValue(hPtr)); if (prefixObj == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "prefix argument list not available for this kind of method", @@ -1383,7 +1431,7 @@ InfoClassMethodsCmd( Tcl_NewStringObj(names[i], -1)); } if (numNames > 0) { - Tcl_Free((void *)names); + Tcl_Free((void *) names); } } else { FOREACH_HASH_DECLS; @@ -1450,7 +1498,7 @@ InfoClassMethodTypeCmd( TclGetString(objv[2]), (char *)NULL); return TCL_ERROR; } - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr == NULL) { /* * Special entry for visibility control: pretend the method doesnt diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 124953d..182d982 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -30,35 +30,45 @@ * Forward declarations. */ -struct CallChain; -struct Class; -struct Foundation; -struct Object; +typedef struct CallChain CallChain; +typedef struct CallContext CallContext; +typedef struct Class Class; +typedef struct DeclaredClassMethod DeclaredClassMethod; +typedef struct ForwardMethod ForwardMethod; +typedef struct Foundation Foundation; +typedef struct Method Method; +typedef struct MInvoke MInvoke; +typedef struct Object Object; +typedef struct PrivateVariableMapping PrivateVariableMapping; +typedef struct ProcedureMethod ProcedureMethod; +typedef struct PropertyStorage PropertyStorage; /* * The data that needs to be stored per method. This record is used to collect * information about all sorts of methods, including forwards, constructors * and destructors. */ - -typedef struct Method { - const Tcl_MethodType *typePtr; - /* The type of method. If NULL, this is a +struct Method { + union { + const Tcl_MethodType *typePtr; + const Tcl_MethodType2 *type2Ptr; + }; /* The type of method. If NULL, this is a * special flag record which is just used for - * the setting of the flags field. */ + * the setting of the flags field. Note that + * this is a union of two pointer types that + * have the same layout at least as far as the + * internal version field. */ Tcl_Size refCount; void *clientData; /* Type-specific data. */ Tcl_Obj *namePtr; /* Name of the method. */ - struct Object *declaringObjectPtr; - /* The object that declares this method, or + Object *declaringObjectPtr; /* The object that declares this method, or * NULL if it was declared by a class. */ - struct Class *declaringClassPtr; - /* The class that declares this method, or + Class *declaringClassPtr; /* The class that declares this method, or * NULL if it was declared directly on an * object. */ int flags; /* Assorted flags. Includes whether this * method is public/exported or not. */ -} Method; +}; /* * Pre- and post-call callbacks, to allow procedure-like methods to be fine @@ -75,10 +85,9 @@ typedef void *(TclOO_PmCDCloneProc)(void *clientData); /* * Procedure-like methods have the following extra information. */ - -typedef struct ProcedureMethod { +struct ProcedureMethod { int version; /* Version of this structure. Currently must - * be 0. */ + * be TCLOO_PROCEDURE_METHOD_VERSION_1. */ Proc *procPtr; /* Core of the implementation of the method; * includes the argument definition and the * body bytecodes. */ @@ -107,44 +116,47 @@ typedef struct ProcedureMethod { * destructor, which we can't know until then * for messy reasons. Other flags are variable * but not used. */ -} ProcedureMethod; +}; -#define TCLOO_PROCEDURE_METHOD_VERSION 0 +enum ProcedureMethodVersion { + TCLOO_PROCEDURE_METHOD_VERSION_1 = 0 +}; +#define TCLOO_PROCEDURE_METHOD_VERSION TCLOO_PROCEDURE_METHOD_VERSION_1 /* * Flags for use in a ProcedureMethod. * - * When the USE_DECLARER_NS flag is set, the method will use the namespace of - * the object or class that declared it (or the clone of it, if it was from - * such that the implementation of the method came to the particular use) - * instead of the namespace of the object on which the method was invoked. - * This flag must be distinct from all others that are associated with - * methods. */ - -#define USE_DECLARER_NS 0x80 +enum ProceudreMethodFlags { + USE_DECLARER_NS = 0x80 /* When set, the method will use the namespace + * of the object or class that declared it (or + * the clone of it, if it was from such that + * the implementation of the method came to the + * particular use) instead of the namespace of + * the object on which the method was invoked. + * This flag must be distinct from all others + * that are associated with methods. */ +}; /* * Forwarded methods have the following extra information. */ - -typedef struct ForwardMethod { +struct ForwardMethod { Tcl_Obj *prefixObj; /* The list of values to use to replace the * object and method name with. Will be a * non-empty list. */ -} ForwardMethod; +}; /* * Structure used in private variable mappings. Describes the mapping of a * single variable from the user's local name to the system's storage name. * [TIP #500] */ - -typedef struct { +struct PrivateVariableMapping { Tcl_Obj *variableObj; /* Name used within methods. This is the part * that is properly under user control. */ Tcl_Obj *fullNameObj; /* Name used at the instance namespace level. */ -} PrivateVariableMapping; +}; /* * Helper definitions that declare a "list" array. The two varieties are @@ -167,15 +179,19 @@ typedef struct { * These types are needed in function arguments. */ +typedef LIST_STATIC(Class *) ClassList; +typedef LIST_DYNAMIC(Class *) VarClassList; +typedef LIST_STATIC(Tcl_Obj *) FilterList; +typedef LIST_DYNAMIC(Object *) ObjectList; typedef LIST_STATIC(Tcl_Obj *) VariableNameList; typedef LIST_STATIC(PrivateVariableMapping) PrivateVariableList; typedef LIST_STATIC(Tcl_Obj *) PropertyList; /* - * This type is used in various places. + * This type is used in various places. It holds the parts of an object or + * class relating to property information. */ - -typedef struct { +struct PropertyStorage { PropertyList readable; /* The readable properties slot. */ PropertyList writable; /* The writable properties slot. */ Tcl_Obj *allReadableCache; /* The cache of all readable properties @@ -187,37 +203,33 @@ typedef struct { * stereotypical instances). Contains a sorted * unique list if not NULL. */ int epoch; /* The epoch that the caches are valid for. */ -} PropertyStorage; +}; /* * Now, the definition of what an object actually is. */ -typedef struct Object { - struct Foundation *fPtr; /* The basis for the object system. Putting - * this here allows the avoidance of quite a - * lot of hash lookups on the critical path - * for object invocation and creation. */ +struct Object { + Foundation *fPtr; /* The basis for the object system, which is + * conceptually part of the interpreter. */ Tcl_Namespace *namespacePtr;/* This object's namespace. */ Tcl_Command command; /* Reference to this object's public * command. */ Tcl_Command myCommand; /* Reference to this object's internal * command. */ - struct Class *selfCls; /* This object's class. */ + Class *selfCls; /* This object's class. */ Tcl_HashTable *methodsPtr; /* Object-local Tcl_Obj (method name) to * Method* mapping. */ - LIST_STATIC(struct Class *) mixins; - /* Classes mixed into this object. */ - LIST_STATIC(Tcl_Obj *) filters; - /* List of filter names. */ - struct Class *classPtr; /* This is non-NULL for all classes, and NULL + ClassList mixins; /* Classes mixed into this object. */ + FilterList filters; /* List of filter names. */ + Class *classPtr; /* This is non-NULL for all classes, and NULL * for everything else. It points to the class * structure. */ Tcl_Size refCount; /* Number of strong references to this object. * Note that there may be many more weak * references; this mechanism exists to * avoid Tcl_Preserve. */ - int flags; + int flags; /* See ObjectFlags. */ Tcl_Size creationEpoch; /* Unique value to make comparisons of objects * easier. */ Tcl_Size epoch; /* Per-object epoch, incremented when the way @@ -243,67 +255,62 @@ typedef struct Object { PropertyStorage properties; /* Information relating to the lists of * properties that this object *claims* to * support. */ -} Object; +}; -#define OBJECT_DESTRUCTING 1 /* Indicates that an object is being or has +enum ObjectFlags { + OBJECT_DESTRUCTING = 1, /* Indicates that an object is being or has * been destroyed */ -#define DESTRUCTOR_CALLED 2 /* Indicates that evaluation of destructor + DESTRUCTOR_CALLED = 2, /* Indicates that evaluation of destructor * script for the object has began */ -#define OO_UNUSED_4 4 /* No longer used. */ -#define ROOT_OBJECT 0x1000 /* Flag to say that this object is the root of + ROOT_OBJECT = 0x1000, /* Flag to say that this object is the root of * the class hierarchy and should be treated * specially during teardown. */ -#define FILTER_HANDLING 0x2000 /* Flag set when the object is processing a + FILTER_HANDLING = 0x2000, /* Flag set when the object is processing a * filter; when set, filters are *not* * processed on the object, preventing nasty * recursive filtering problems. */ -#define USE_CLASS_CACHE 0x4000 /* Flag set to say that the object is a pure + USE_CLASS_CACHE = 0x4000, /* Flag set to say that the object is a pure * instance of the class, and has had nothing * added that changes the dispatch chain (i.e. * no methods, mixins, or filters. */ -#define ROOT_CLASS 0x8000 /* Flag to say that this object is the root + ROOT_CLASS = 0x8000, /* Flag to say that this object is the root * class of classes, and should be treated * specially during teardown (and in a few * other spots). */ -#define FORCE_UNKNOWN 0x10000 /* States that we are *really* looking up the + FORCE_UNKNOWN = 0x10000, /* States that we are *really* looking up the * unknown method handler at that point. */ -#define DONT_DELETE 0x20000 /* Inhibit deletion of this object. Used + DONT_DELETE = 0x20000, /* Inhibit deletion of this object. Used * during fundamental object type mutation to * make sure that the object actually survives * to the end of the operation. */ -#define HAS_PRIVATE_METHODS 0x40000 + HAS_PRIVATE_METHODS = 0x40000 /* Object/class has (or had) private methods, * and so shouldn't be cached so * aggressively. */ +}; /* * And the definition of a class. Note that every class also has an associated * object, through which it is manipulated. */ -typedef struct Class { +struct Class { Object *thisPtr; /* Reference to the object associated with * this class. */ int flags; /* Assorted flags. */ - LIST_STATIC(struct Class *) superclasses; - /* List of superclasses, used for generation + ClassList superclasses; /* List of superclasses, used for generation * of method call chains. */ - LIST_DYNAMIC(struct Class *) subclasses; - /* List of subclasses, used to ensure deletion + VarClassList subclasses; /* List of subclasses, used to ensure deletion * of dependent entities happens properly when * the class itself is deleted. */ - LIST_DYNAMIC(Object *) instances; - /* List of instances, used to ensure deletion + ObjectList instances; /* List of instances, used to ensure deletion * of dependent entities happens properly when * the class itself is deleted. */ - LIST_STATIC(Tcl_Obj *) filters; - /* List of filter names, used for generation + FilterList filters; /* List of filter names, used for generation * of method call chains. */ - LIST_STATIC(struct Class *) mixins; - /* List of mixin classes, used for generation + ClassList mixins; /* List of mixin classes, used for generation * of method call chains. */ - LIST_DYNAMIC(struct Class *) mixinSubs; - /* List of classes that this class is mixed + VarClassList mixinSubs; /* List of classes that this class is mixed * into, used to ensure deletion of dependent * entities happens properly when the class * itself is deleted. */ @@ -319,8 +326,8 @@ typedef struct Class { * of each piece of attached metadata. This * field starts out as NULL and is only * allocated if metadata is attached. */ - struct CallChain *constructorChainPtr; - struct CallChain *destructorChainPtr; + CallChain *constructorChainPtr; + CallChain *destructorChainPtr; Tcl_HashTable *classChainCache; /* Places where call chains are stored. For * constructors, the class chain is always @@ -354,15 +361,12 @@ typedef struct Class { PropertyStorage properties; /* Information relating to the lists of * properties that this class *claims* to * support. */ -} Class; +}; /* - * The foundation of the object system within an interpreter contains - * references to the key classes and namespaces, together with a few other - * useful bits and pieces. Probably ought to eventually go in the Interp - * structure itself. + * Master epoch counter for making unique IDs for objects that can be compared + * cheaply. */ - typedef struct ThreadLocalData { Tcl_Size nsCount; /* Epoch counter is used for keeping * the values used in Tcl_Obj internal @@ -372,19 +376,17 @@ typedef struct ThreadLocalData { * generally cross threads). */ } ThreadLocalData; -typedef struct Foundation { - Tcl_Interp *interp; +/* + * The foundation of the object system within an interpreter contains + * references to the key classes and namespaces, together with a few other + * useful bits and pieces. Probably ought to eventually go in the Interp + * structure itself. + */ +struct Foundation { + Tcl_Interp *interp; /* The interpreter this is attached to. */ Class *objectCls; /* The root of the object system. */ Class *classCls; /* The class of all classes. */ Tcl_Namespace *ooNs; /* ::oo namespace. */ - Tcl_Namespace *defineNs; /* Namespace containing special commands for - * manipulating objects and classes. The - * "oo::define" command acts as a special kind - * of ensemble for this namespace. */ - Tcl_Namespace *objdefNs; /* Namespace containing special commands for - * manipulating objects and classes. The - * "oo::objdefine" command acts as a special - * kind of ensemble for this namespace. */ Tcl_Namespace *helpersNs; /* Namespace containing the commands that are * only valid when executing inside a * procedural method. */ @@ -403,17 +405,18 @@ typedef struct Foundation { * "" pseudo-constructor. */ Tcl_Obj *defineName; /* Fully qualified name of oo::define. */ Tcl_Obj *myName; /* The "my" shared object. */ -} Foundation; +}; /* - * A call context structure is built when a method is called. It contains the - * chain of method implementations that are to be invoked by a particular - * call, and the process of calling walks the chain, with the [next] command - * proceeding to the next entry in the chain. + * The number of MInvoke records in the CallChain before we allocate + * separately. */ - #define CALL_CHAIN_STATIC_SIZE 4 +/* + * Information relating to the invocation of a particular method implementation + * in a call chain. + */ struct MInvoke { Method *mPtr; /* Reference to the method implementation * record. */ @@ -422,7 +425,10 @@ struct MInvoke { * NULL, it was added by the object. */ }; -typedef struct CallChain { +/* + * The cacheable part of a call context. + */ +struct CallChain { Tcl_Size objectCreationEpoch;/* The object's creation epoch. Note that the * object reference is not stored in the call * chain; it is in the call context. */ @@ -433,13 +439,19 @@ typedef struct CallChain { int flags; /* Assorted flags, see below. */ Tcl_Size refCount; /* Reference count. */ Tcl_Size numChain; /* Size of the call chain. */ - struct MInvoke *chain; /* Array of call chain entries. May point to + MInvoke *chain; /* Array of call chain entries. May point to * staticChain if the number of entries is * small. */ - struct MInvoke staticChain[CALL_CHAIN_STATIC_SIZE]; -} CallChain; + MInvoke staticChain[CALL_CHAIN_STATIC_SIZE]; +}; -typedef struct CallContext { +/* + * A call context structure is built when a method is called. It contains the + * chain of method implementations that are to be invoked by a particular + * call, and the process of calling walks the chain, with the [next] command + * proceeding to the next entry in the chain. + */ +struct CallContext { Object *oPtr; /* The object associated with this call. */ Tcl_Size index; /* Index into the call chain of the currently * executing method implementation. */ @@ -448,33 +460,32 @@ typedef struct CallContext { * method call or a continuation via the * [next] command. */ CallChain *callPtr; /* The actual call chain. */ -} CallContext; +}; /* * Bits for the 'flags' field of the call chain. */ - -#define PUBLIC_METHOD 0x01 /* This is a public (exported) method. */ -#define PRIVATE_METHOD 0x02 /* This is a private (class's direct instances +enum TclOOCallChainFlags { + PUBLIC_METHOD = 0x01, /* This is a public (exported) method. */ + PRIVATE_METHOD = 0x02, /* This is a private (class's direct instances * only) method. Supports itcl. */ -#define OO_UNKNOWN_METHOD 0x04 /* This is an unknown method. */ -#define CONSTRUCTOR 0x08 /* This is a constructor. */ -#define DESTRUCTOR 0x10 /* This is a destructor. */ -#define TRUE_PRIVATE_METHOD 0x20 - /* This is a private method only accessible + OO_UNKNOWN_METHOD = 0x04, /* This is an unknown method. */ + CONSTRUCTOR = 0x08, /* This is a constructor. */ + DESTRUCTOR = 0x10, /* This is a destructor. */ + TRUE_PRIVATE_METHOD = 0x20 /* This is a private method only accessible * from other methods defined on this class * or instance. [TIP #500] */ +}; #define SCOPE_FLAGS (PUBLIC_METHOD | PRIVATE_METHOD | TRUE_PRIVATE_METHOD) /* * Structure containing definition information about basic class methods. */ - -typedef struct { +struct DeclaredClassMethod { const char *name; /* Name of the method in question. */ int isPublic; /* Whether the method is public by default. */ Tcl_MethodType definition; /* How to call the method. */ -} DeclaredClassMethod; +}; /* *---------------------------------------------------------------- @@ -538,7 +549,7 @@ MODULE_SCOPE Tcl_Method TclNewInstanceMethod(Tcl_Interp *interp, Tcl_Object object, Tcl_Obj *nameObj, int flags, const Tcl_MethodType *typePtr, void *clientData); -MODULE_SCOPE Tcl_Method TclNewMethod(Tcl_Interp *interp, Tcl_Class cls, +MODULE_SCOPE Tcl_Method TclNewMethod(Tcl_Class cls, Tcl_Obj *nameObj, int flags, const Tcl_MethodType *typePtr, void *clientData); @@ -595,7 +606,7 @@ MODULE_SCOPE int TclOOInvokeContext(void *clientData, MODULE_SCOPE int TclNRObjectContextInvokeNext(Tcl_Interp *interp, Tcl_ObjectContext context, Tcl_Size objc, Tcl_Obj *const *objv, Tcl_Size skip); -MODULE_SCOPE void TclOONewBasicMethod(Tcl_Interp *interp, Class *clsPtr, +MODULE_SCOPE void TclOONewBasicMethod(Class *clsPtr, const DeclaredClassMethod *dcm); MODULE_SCOPE Tcl_Obj * TclOOObjectName(Tcl_Interp *interp, Object *oPtr); MODULE_SCOPE void TclOOReleaseClassContents(Tcl_Interp *interp, diff --git a/generic/tclOOMethod.c b/generic/tclOOMethod.c index f60b1c2..89e4d4e 100644 --- a/generic/tclOOMethod.c +++ b/generic/tclOOMethod.c @@ -21,7 +21,7 @@ * used in a procedure-like method. */ -typedef struct { +typedef struct PMFrameData { CallFrame *framePtr; /* Reference to the call frame itself (it's * actually allocated on the Tcl stack). */ ProcErrorProc *errProc; /* The error handler for the body. */ @@ -34,7 +34,7 @@ typedef struct { * on-the-ground resolvers used when working with resolved compiled variables. */ -typedef struct { +typedef struct OOResVarInfo { Tcl_ResolvedVarInfo info; /* "Type" information so that the compiled * variable can be linked to the namespace * variable at the right time. */ @@ -146,25 +146,25 @@ TclNewInstanceMethod( int isNew; if (nameObj == NULL) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); mPtr->namePtr = NULL; mPtr->refCount = 1; goto populate; } if (!oPtr->methodsPtr) { - oPtr->methodsPtr = (Tcl_HashTable *)Tcl_Alloc(sizeof(Tcl_HashTable)); + oPtr->methodsPtr = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable)); Tcl_InitObjHashTable(oPtr->methodsPtr); oPtr->flags &= ~USE_CLASS_CACHE; } hPtr = Tcl_CreateHashEntry(oPtr->methodsPtr, nameObj, &isNew); if (isNew) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); mPtr->namePtr = nameObj; mPtr->refCount = 1; Tcl_IncrRefCount(nameObj); Tcl_SetHashValue(hPtr, mPtr); } else { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr != NULL && mPtr->typePtr->deleteProc != NULL) { mPtr->typePtr->deleteProc(mPtr->clientData); } @@ -203,10 +203,11 @@ Tcl_NewInstanceMethod( * method to be created. */ { if (typePtr->version > TCL_OO_METHOD_VERSION_1) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_1", "Tcl_NewInstanceMethod"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_NewInstanceMethod", "TCL_OO_METHOD_VERSION_1"); } - return TclNewInstanceMethod(NULL, object, nameObj, flags, - (const Tcl_MethodType *)typePtr, clientData); + return TclNewInstanceMethod(NULL, object, nameObj, flags, typePtr, + clientData); } Tcl_Method Tcl_NewInstanceMethod2( @@ -225,10 +226,11 @@ Tcl_NewInstanceMethod2( * method to be created. */ { if (typePtr->version < TCL_OO_METHOD_VERSION_2) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_2", "Tcl_NewInstanceMethod2"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_NewInstanceMethod2", "TCL_OO_METHOD_VERSION_2"); } return TclNewInstanceMethod(NULL, object, nameObj, flags, - (const Tcl_MethodType *)typePtr, clientData); + (const Tcl_MethodType *) typePtr, clientData); } /* @@ -243,7 +245,6 @@ Tcl_NewInstanceMethod2( Tcl_Method TclNewMethod( - TCL_UNUSED(Tcl_Interp *), Tcl_Class cls, /* The class to attach the method to. */ Tcl_Obj *nameObj, /* The name of the object. May be NULL (e.g., * for constructors or destructors); if so, up @@ -262,20 +263,20 @@ TclNewMethod( int isNew; if (nameObj == NULL) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); mPtr->namePtr = NULL; mPtr->refCount = 1; goto populate; } - hPtr = Tcl_CreateHashEntry(&clsPtr->classMethods, nameObj,&isNew); + hPtr = Tcl_CreateHashEntry(&clsPtr->classMethods, nameObj, &isNew); if (isNew) { - mPtr = (Method *)Tcl_Alloc(sizeof(Method)); + mPtr = (Method *) Tcl_Alloc(sizeof(Method)); mPtr->refCount = 1; mPtr->namePtr = nameObj; Tcl_IncrRefCount(nameObj); Tcl_SetHashValue(hPtr, mPtr); } else { - mPtr = (Method *)Tcl_GetHashValue(hPtr); + mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr != NULL && mPtr->typePtr->deleteProc != NULL) { mPtr->typePtr->deleteProc(mPtr->clientData); } @@ -315,9 +316,10 @@ Tcl_NewMethod( * method to be created. */ { if (typePtr->version > TCL_OO_METHOD_VERSION_1) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_1", "Tcl_NewMethod"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_NewMethod", "TCL_OO_METHOD_VERSION_1"); } - return TclNewMethod(NULL, cls, nameObj, flags, typePtr, clientData); + return TclNewMethod(cls, nameObj, flags, typePtr, clientData); } Tcl_Method @@ -336,9 +338,11 @@ Tcl_NewMethod2( * method to be created. */ { if (typePtr->version < TCL_OO_METHOD_VERSION_2) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_2", "Tcl_NewMethod2"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_NewMethod2", "TCL_OO_METHOD_VERSION_2"); } - return TclNewMethod(NULL, cls, nameObj, flags, (const Tcl_MethodType *)typePtr, clientData); + return TclNewMethod(cls, nameObj, flags, + (const Tcl_MethodType *) typePtr, clientData); } /* @@ -380,7 +384,6 @@ TclOODelMethodRef( void TclOONewBasicMethod( - Tcl_Interp *interp, Class *clsPtr, /* Class to attach the method to. */ const DeclaredClassMethod *dcm) /* Name of the method, whether it is public, @@ -388,10 +391,9 @@ TclOONewBasicMethod( { Tcl_Obj *namePtr = Tcl_NewStringObj(dcm->name, -1); - Tcl_IncrRefCount(namePtr); - TclNewMethod(interp, (Tcl_Class) clsPtr, namePtr, + TclNewMethod((Tcl_Class) clsPtr, namePtr, (dcm->isPublic ? PUBLIC_METHOD : 0), &dcm->definition, NULL); - Tcl_DecrRefCount(namePtr); + Tcl_BounceRefCount(namePtr); } /* @@ -550,7 +552,7 @@ InitCmdFrame( if (context.line && context.nline > 1 && (context.line[context.nline - 1] >= 0)) { int isNew; - CmdFrame *cfPtr = (CmdFrame *)Tcl_Alloc(sizeof(CmdFrame)); + CmdFrame *cfPtr = (CmdFrame *) Tcl_Alloc(sizeof(CmdFrame)); Tcl_HashEntry *hPtr; cfPtr->level = -1; @@ -678,8 +680,8 @@ TclOOMakeProcMethod( InitCmdFrame(iPtr, procPtr); - return TclNewMethod(interp, (Tcl_Class) clsPtr, nameObj, flags, typePtr, - clientData); + return TclNewMethod( + (Tcl_Class) clsPtr, nameObj, flags, typePtr, clientData); } /* @@ -700,7 +702,7 @@ InvokeProcedureMethod( int objc, /* Number of arguments. */ Tcl_Obj *const *objv) /* Arguments as actually seen. */ { - ProcedureMethod *pmPtr = (ProcedureMethod *)clientData; + ProcedureMethod *pmPtr = (ProcedureMethod *) clientData; int result; PMFrameData *fdPtr; /* Important data that has to have a lifetime * matched by this function (or rather, by the @@ -711,7 +713,7 @@ InvokeProcedureMethod( * the next thing in the chain. */ - if (TclOOObjectDestroyed(((CallContext *)context)->oPtr) + if (TclOOObjectDestroyed(((CallContext *) context)->oPtr) || Tcl_InterpDeleted(interp)) { return TclNRObjectContextInvokeNext(interp, context, objc, objv, Tcl_ObjectContextSkippedArgs(context)); @@ -752,7 +754,7 @@ InvokeProcedureMethod( * Allocate the special frame data. */ - fdPtr = (PMFrameData *)TclStackAlloc(interp, sizeof(PMFrameData)); + fdPtr = (PMFrameData *) TclStackAlloc(interp, sizeof(PMFrameData)); /* * Create a call frame for this method. @@ -802,9 +804,9 @@ FinalizePMCall( Tcl_Interp *interp, int result) { - ProcedureMethod *pmPtr = (ProcedureMethod *)data[0]; - Tcl_ObjectContext context = (Tcl_ObjectContext)data[1]; - PMFrameData *fdPtr = (PMFrameData *)data[2]; + ProcedureMethod *pmPtr = (ProcedureMethod *) data[0]; + Tcl_ObjectContext context = (Tcl_ObjectContext) data[1]; + PMFrameData *fdPtr = (PMFrameData *) data[2]; /* * Give the post-call callback a chance to do some cleanup. Note that at @@ -998,7 +1000,7 @@ ProcedureMethodCompiledVarConnect( if (framePtr == NULL || !(framePtr->isProcCallFrame & FRAME_IS_METHOD)) { return NULL; } - contextPtr = (CallContext *)framePtr->clientData; + contextPtr = (CallContext *) framePtr->clientData; /* * If we've done the work before (in a comparable context) then reuse that @@ -1118,7 +1120,7 @@ ProcedureMethodCompiledVarResolver( return TCL_CONTINUE; } - infoPtr = (OOResVarInfo *)Tcl_Alloc(sizeof(OOResVarInfo)); + infoPtr = (OOResVarInfo *) Tcl_Alloc(sizeof(OOResVarInfo)); infoPtr->info.fetchProc = ProcedureMethodCompiledVarConnect; infoPtr->info.deleteProc = ProcedureMethodCompiledVarDelete; infoPtr->cachedObjectVar = NULL; @@ -1208,7 +1210,8 @@ MethodErrorHandler( // We pull the method name out of context instead of from argument { Tcl_Size nameLen, objectNameLen; - CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; + CallContext *contextPtr = (CallContext *) + ((Interp *) interp)->varFramePtr->clientData; Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; const char *objectName, *kindName, *methodName = Tcl_GetStringFromObj(mPtr->namePtr, &nameLen); @@ -1239,7 +1242,8 @@ ConstructorErrorHandler( TCL_UNUSED(Tcl_Obj *) /*methodNameObj*/) // Ignore. We know it is the constructor. { - CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; + CallContext *contextPtr = (CallContext *) + ((Interp *) interp)->varFramePtr->clientData; Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; Object *declarerPtr; const char *objectName, *kindName; @@ -1269,7 +1273,8 @@ DestructorErrorHandler( TCL_UNUSED(Tcl_Obj *) /*methodNameObj*/) // Ignore. We know it is the destructor. { - CallContext *contextPtr = (CallContext *)((Interp *) interp)->varFramePtr->clientData; + CallContext *contextPtr = (CallContext *) + ((Interp *) interp)->varFramePtr->clientData; Method *mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr; Object *declarerPtr; const char *objectName, *kindName; @@ -1318,7 +1323,7 @@ static void DeleteProcedureMethod( void *clientData) { - ProcedureMethod *pmPtr = (ProcedureMethod *)clientData; + ProcedureMethod *pmPtr = (ProcedureMethod *) clientData; if (pmPtr->refCount-- <= 1) { DeleteProcedureMethodRecord(pmPtr); @@ -1331,7 +1336,7 @@ CloneProcedureMethod( void *clientData, void **newClientData) { - ProcedureMethod *pmPtr = (ProcedureMethod *)clientData; + ProcedureMethod *pmPtr = (ProcedureMethod *) clientData; ProcedureMethod *pm2Ptr; Tcl_Obj *bodyObj, *argsObj; CompiledLocal *localPtr; @@ -1370,7 +1375,7 @@ CloneProcedureMethod( * record. */ - pm2Ptr = (ProcedureMethod *)Tcl_Alloc(sizeof(ProcedureMethod)); + pm2Ptr = (ProcedureMethod *) Tcl_Alloc(sizeof(ProcedureMethod)); memcpy(pm2Ptr, pmPtr, sizeof(ProcedureMethod)); pm2Ptr->refCount = 1; pm2Ptr->cmd.clientData = &pm2Ptr->efi; @@ -1426,7 +1431,7 @@ TclOONewForwardInstanceMethod( return NULL; } - fmPtr = (ForwardMethod *)Tcl_Alloc(sizeof(ForwardMethod)); + fmPtr = (ForwardMethod *) Tcl_Alloc(sizeof(ForwardMethod)); fmPtr->prefixObj = prefixObj; Tcl_IncrRefCount(prefixObj); return (Method *) TclNewInstanceMethod(interp, (Tcl_Object) oPtr, @@ -1465,10 +1470,10 @@ TclOONewForwardMethod( return NULL; } - fmPtr = (ForwardMethod *)Tcl_Alloc(sizeof(ForwardMethod)); + fmPtr = (ForwardMethod *) Tcl_Alloc(sizeof(ForwardMethod)); fmPtr->prefixObj = prefixObj; Tcl_IncrRefCount(prefixObj); - return (Method *) TclNewMethod(interp, (Tcl_Class) clsPtr, nameObj, + return (Method *) TclNewMethod((Tcl_Class) clsPtr, nameObj, flags, &fwdMethodType, fmPtr); } @@ -1492,7 +1497,7 @@ InvokeForwardMethod( Tcl_Obj *const *objv) /* Arguments as actually seen. */ { CallContext *contextPtr = (CallContext *) context; - ForwardMethod *fmPtr = (ForwardMethod *)clientData; + ForwardMethod *fmPtr = (ForwardMethod *) clientData; Tcl_Obj **argObjs, **prefixObjs; Tcl_Size numPrefixes, skip = contextPtr->skip; int len; @@ -1513,8 +1518,8 @@ InvokeForwardMethod( * of the TCL_EVAL_NOERR flag results in an evaluation configuration * very much like TCL_EVAL_INVOKE. */ - ((Interp *)interp)->lookupNsPtr - = (Namespace *) contextPtr->oPtr->namespacePtr; + ((Interp *) interp)->lookupNsPtr = (Namespace *) + contextPtr->oPtr->namespacePtr; return TclNREvalObjv(interp, len, argObjs, TCL_EVAL_NOERR, NULL); } @@ -1524,7 +1529,7 @@ FinalizeForwardCall( Tcl_Interp *interp, int result) { - Tcl_Obj **argObjs = (Tcl_Obj **)data[0]; + Tcl_Obj **argObjs = (Tcl_Obj **) data[0]; TclStackFree(interp, argObjs); return result; @@ -1544,7 +1549,7 @@ static void DeleteForwardMethod( void *clientData) { - ForwardMethod *fmPtr = (ForwardMethod *)clientData; + ForwardMethod *fmPtr = (ForwardMethod *) clientData; Tcl_DecrRefCount(fmPtr->prefixObj); Tcl_Free(fmPtr); @@ -1556,8 +1561,8 @@ CloneForwardMethod( void *clientData, void **newClientData) { - ForwardMethod *fmPtr = (ForwardMethod *)clientData; - ForwardMethod *fm2Ptr = (ForwardMethod *)Tcl_Alloc(sizeof(ForwardMethod)); + ForwardMethod *fmPtr = (ForwardMethod *) clientData; + ForwardMethod *fm2Ptr = (ForwardMethod *) Tcl_Alloc(sizeof(ForwardMethod)); fm2Ptr->prefixObj = fmPtr->prefixObj; Tcl_IncrRefCount(fm2Ptr->prefixObj); @@ -1581,7 +1586,7 @@ TclOOGetProcFromMethod( Method *mPtr) { if (mPtr->typePtr == &procMethodType) { - ProcedureMethod *pmPtr = (ProcedureMethod *)mPtr->clientData; + ProcedureMethod *pmPtr = (ProcedureMethod *) mPtr->clientData; return pmPtr->procPtr; } @@ -1593,7 +1598,7 @@ TclOOGetMethodBody( Method *mPtr) { if (mPtr->typePtr == &procMethodType) { - ProcedureMethod *pmPtr = (ProcedureMethod *)mPtr->clientData; + ProcedureMethod *pmPtr = (ProcedureMethod *) mPtr->clientData; (void) TclGetString(pmPtr->procPtr->bodyPtr); return pmPtr->procPtr->bodyPtr; @@ -1606,7 +1611,7 @@ TclOOGetFwdFromMethod( Method *mPtr) { if (mPtr->typePtr == &fwdMethodType) { - ForwardMethod *fwPtr = (ForwardMethod *)mPtr->clientData; + ForwardMethod *fwPtr = (ForwardMethod *) mPtr->clientData; return fwPtr->prefixObj; } @@ -1648,7 +1653,8 @@ InitEnsembleRewrite( * array of rewritten arguments. */ { size_t len = rewriteLength + objc - toRewrite; - Tcl_Obj **argObjs = (Tcl_Obj **)TclStackAlloc(interp, sizeof(Tcl_Obj *) * len); + Tcl_Obj **argObjs = (Tcl_Obj **) + TclStackAlloc(interp, sizeof(Tcl_Obj *) * len); memcpy(argObjs, rewriteObjs, rewriteLength * sizeof(Tcl_Obj *)); memcpy(argObjs + rewriteLength, objv + toRewrite, @@ -1725,7 +1731,8 @@ Tcl_MethodIsType( Method *mPtr = (Method *) method; if (typePtr->version > TCL_OO_METHOD_VERSION_1) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_1", "Tcl_MethodIsType"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_MethodIsType", "TCL_OO_METHOD_VERSION_1"); } if (mPtr->typePtr == typePtr) { if (clientDataPtr != NULL) { @@ -1745,9 +1752,10 @@ Tcl_MethodIsType2( Method *mPtr = (Method *) method; if (typePtr->version < TCL_OO_METHOD_VERSION_2) { - Tcl_Panic("%s: Wrong version in typePtr->version, should be TCL_OO_METHOD_VERSION_2", "Tcl_MethodIsType2"); + Tcl_Panic("%s: Wrong version in typePtr->version, should be %s", + "Tcl_MethodIsType2", "TCL_OO_METHOD_VERSION_2"); } - if (mPtr->typePtr == (const Tcl_MethodType *)typePtr) { + if (mPtr->typePtr == (const Tcl_MethodType *) typePtr) { if (clientDataPtr != NULL) { *clientDataPtr = mPtr->clientData; } @@ -1760,14 +1768,14 @@ int Tcl_MethodIsPublic( Tcl_Method method) { - return (((Method *)method)->flags & PUBLIC_METHOD) ? 1 : 0; + return (((Method *) method)->flags & PUBLIC_METHOD) ? 1 : 0; } int Tcl_MethodIsPrivate( Tcl_Method method) { - return (((Method *)method)->flags & TRUE_PRIVATE_METHOD) ? 1 : 0; + return (((Method *) method)->flags & TRUE_PRIVATE_METHOD) ? 1 : 0; } /* -- cgit v0.12 From 11cd975ea6174cc9851a4907713d02d8c8aa7271 Mon Sep 17 00:00:00 2001 From: dkf Date: Thu, 1 Aug 2024 19:40:04 +0000 Subject: Remove redundant code. --- generic/tclOO.c | 10 +++------- generic/tclOODefineCmds.c | 42 +----------------------------------------- generic/tclOOInt.h | 1 - 3 files changed, 4 insertions(+), 49 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index 288b7f2..2ffc0d4 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -17,7 +17,7 @@ #include "tclOOInt.h" /* - * Commands in oo::define. + * Commands in oo::define and oo::objdefine. */ static const struct { @@ -337,7 +337,7 @@ InitFoundation( ThreadLocalData *tsdPtr = (ThreadLocalData *) Tcl_GetThreadData(&tsdKey, sizeof(ThreadLocalData)); Foundation *fPtr = (Foundation *) Tcl_Alloc(sizeof(Foundation)); - Tcl_Namespace *define, *objdef, *cfg; + Tcl_Namespace *define, *objdef; Tcl_Obj *namePtr; size_t i; @@ -356,7 +356,7 @@ InitFoundation( objdef = Tcl_CreateNamespace(interp, "::oo::objdefine", fPtr, NULL); fPtr->helpersNs = Tcl_CreateNamespace(interp, "::oo::Helpers", fPtr, DeletedHelpersNamespace); - cfg = Tcl_CreateNamespace(interp, "::oo::configuresupport", NULL, NULL); + Tcl_CreateNamespace(interp, "::oo::configuresupport", NULL, NULL); fPtr->epoch = 1; fPtr->tsdPtr = tsdPtr; @@ -461,10 +461,6 @@ InitFoundation( for (i = 0 ; cfgMethods[i].name ; i++) { TclOONewBasicMethod(((Object *) cfgCls)->classPtr, &cfgMethods[i]); } - TclCreateObjCommandInNs(interp, "StdObjectProperties", cfg, - TclOOInstallStdPropertyImplsCmd, (void *) 1, NULL); - TclCreateObjCommandInNs(interp, "StdClassProperties", cfg, - TclOOInstallStdPropertyImplsCmd, (void *) 0, NULL); /* * Don't have handles to these namespaces, so use Tcl_CreateObjCommand. diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 882ca52..0e81cc8 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -3800,11 +3800,7 @@ TclOOPropertyDefinitionCmd( /* * ---------------------------------------------------------------------- * - * InstallStdPropertyImpls, TclOOInstallStdPropertyImplsCmd -- - * - * Implementations of the "StdClassProperties" hidden definition for - * classes and the "StdObjectProperties" hidden definition for - * instances. Both are located in the ::oo::configuresupport namespace. + * InstallStdPropertyImpls -- * * Validates a (dashless) property name, and installs implementation * methods if asked to do so (readable and writable flags). @@ -3875,42 +3871,6 @@ InstallStdPropertyImpls( Tcl_SetErrorCode(interp, "TCL", "OO", "PROPERTY_FORMAT", NULL); return TCL_ERROR; } - -int -TclOOInstallStdPropertyImplsCmd( - void *useInstance, - Tcl_Interp *interp, - int objc, - Tcl_Obj *const *objv) -{ - int readable, writable; - Tcl_Obj *propName; - - /* - * Parse the arguments. - */ - - if (objc != 4) { - Tcl_WrongNumArgs(interp, 1, objv, "propName readable writable"); - return TCL_ERROR; - } - propName = objv[1]; - if (Tcl_GetBooleanFromObj(interp, objv[2], &readable) != TCL_OK) { - return TCL_ERROR; - } - if (Tcl_GetBooleanFromObj(interp, objv[3], &writable) != TCL_OK) { - return TCL_ERROR; - } - - - /* - * Validate the property name and install the implementations... if asked - * to do so. - */ - - return InstallStdPropertyImpls(useInstance, interp, propName, readable, - writable); -} /* * Local Variables: diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 182d982..5377ca3 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -509,7 +509,6 @@ MODULE_SCOPE Tcl_ObjCmdProc TclOODefineClassObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineObjSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefinePrivateObjCmd; -MODULE_SCOPE Tcl_ObjCmdProc TclOOInstallStdPropertyImplsCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOPropertyDefinitionCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOUnknownDefinition; MODULE_SCOPE Tcl_ObjCmdProc TclOOCopyObjectCmd; -- cgit v0.12 From 05011a884a6681e0e91d7e760e54833e73a80f5b Mon Sep 17 00:00:00 2001 From: dkf Date: Fri, 2 Aug 2024 12:03:24 +0000 Subject: Factor out most property-related C code into its own file. --- generic/tclOO.c | 42 +- generic/tclOOBasic.c | 471 +----------------- generic/tclOOCall.c | 247 ---------- generic/tclOODefineCmds.c | 276 ++--------- generic/tclOOInfo.c | 218 +-------- generic/tclOOInt.h | 20 +- generic/tclOOProp.c | 1174 +++++++++++++++++++++++++++++++++++++++++++++ unix/Makefile.in | 6 +- win/Makefile.in | 1 + win/makefile.vc | 1 + 10 files changed, 1258 insertions(+), 1198 deletions(-) create mode 100644 generic/tclOOProp.c diff --git a/generic/tclOO.c b/generic/tclOO.c index 2ffc0d4..7384232 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -1004,7 +1004,7 @@ TclOOReleaseClassContents( Class *clsPtr = oPtr->classPtr, *tmpClsPtr; Method *mPtr; Foundation *fPtr = oPtr->fPtr; - Tcl_Obj *variableObj, *propertyObj; + Tcl_Obj *variableObj; PrivateVariableMapping *privateVariable; /* @@ -1061,24 +1061,7 @@ TclOOReleaseClassContents( * Squelch the property lists. */ - if (clsPtr->properties.allReadableCache) { - Tcl_DecrRefCount(clsPtr->properties.allReadableCache); - } - if (clsPtr->properties.allWritableCache) { - Tcl_DecrRefCount(clsPtr->properties.allWritableCache); - } - if (clsPtr->properties.readable.num) { - FOREACH(propertyObj, clsPtr->properties.readable) { - Tcl_DecrRefCount(propertyObj); - } - Tcl_Free(clsPtr->properties.readable.list); - } - if (clsPtr->properties.writable.num) { - FOREACH(propertyObj, clsPtr->properties.writable) { - Tcl_DecrRefCount(propertyObj); - } - Tcl_Free(clsPtr->properties.writable.list); - } + TclOOReleasePropertyStorage(&clsPtr->properties); /* * Squelch our filter list. @@ -1181,7 +1164,7 @@ ObjectNamespaceDeleted( FOREACH_HASH_DECLS; Class *mixinPtr; Method *mPtr; - Tcl_Obj *filterObj, *variableObj, *propertyObj; + Tcl_Obj *filterObj, *variableObj; PrivateVariableMapping *privateVariable; Tcl_Interp *interp = oPtr->fPtr->interp; Tcl_Size i; @@ -1338,24 +1321,7 @@ ObjectNamespaceDeleted( * Squelch the property lists. */ - if (oPtr->properties.allReadableCache) { - Tcl_DecrRefCount(oPtr->properties.allReadableCache); - } - if (oPtr->properties.allWritableCache) { - Tcl_DecrRefCount(oPtr->properties.allWritableCache); - } - if (oPtr->properties.readable.num) { - FOREACH(propertyObj, oPtr->properties.readable) { - Tcl_DecrRefCount(propertyObj); - } - Tcl_Free(oPtr->properties.readable.list); - } - if (oPtr->properties.writable.num) { - FOREACH(propertyObj, oPtr->properties.writable) { - Tcl_DecrRefCount(propertyObj); - } - Tcl_Free(oPtr->properties.writable.list); - } + TclOOReleasePropertyStorage(&oPtr->properties); /* * Because an object can be a class that is an instance of itself, the diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index cfb7cb5..c9a5c7a 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -23,18 +23,6 @@ static Tcl_NRPostProc DecrRefsPostClassConstructor; static Tcl_NRPostProc FinalizeConstruction; static Tcl_NRPostProc FinalizeEval; static Tcl_NRPostProc NextRestoreFrame; - -/* Short-term cache for GetPropertyName(). */ -typedef struct GPNCache { - Tcl_Obj *listPtr; /* Holds references to names. */ - char *names[TCLFLEXARRAY]; /* NULL-terminated table of names. */ -} GPNCache; - -enum GPNFlags { - GPN_WRITABLE = 1, /* Are we looking for a writable property? */ - GPN_FALLING_BACK = 2 /* Are we doing a recursive call to determine - * if the property is of the other type? */ -}; /* * ---------------------------------------------------------------------- @@ -733,15 +721,14 @@ TclOO_Object_LinkVar( /* * ---------------------------------------------------------------------- * - * LookupObjectVar -- + * TclOOLookupObjectVar -- * * Look up a variable in an object. Tricky because of private variables. * * ---------------------------------------------------------------------- */ - -static Tcl_Var -LookupObjectVar( +Tcl_Var +TclOOLookupObjectVar( Tcl_Interp *interp, Tcl_Object object, Tcl_Obj *varName, @@ -860,7 +847,7 @@ TclOO_Object_VarName( return TCL_ERROR; } - varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), + varPtr = TclOOLookupObjectVar(interp, Tcl_ObjectContextObject(context), objv[objc - 1], &aryVar); if (varPtr == NULL) { return TCL_ERROR; @@ -1383,456 +1370,6 @@ TclOOCopyObjectCmd( } /* - * ---------------------------------------------------------------------- - * - * TclOO_Configurable_Configure -- - * - * Implementation of the oo::configurable->configure method. - * - * ---------------------------------------------------------------------- - */ - -/* - * Ugly thunks to read and write a property by calling the right method in - * the right way. Note that we MUST be correct in holding references to Tcl_Obj - * values, as this is potentially a call into user code. - */ -static inline int -ReadProperty( - Tcl_Interp *interp, - Object *oPtr, - Tcl_Obj *propObj) -{ - Tcl_Obj *args[] = { - oPtr->fPtr->myName, - Tcl_ObjPrintf("", TclGetString(propObj)) - }; - int code; - - Tcl_IncrRefCount(args[0]); - Tcl_IncrRefCount(args[1]); - code = TclOOPrivateObjectCmd(oPtr, interp, 2, args); - Tcl_DecrRefCount(args[0]); - Tcl_DecrRefCount(args[1]); - switch (code) { - case TCL_BREAK: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property getter for %s did a break", TclGetString(propObj))); - return TCL_ERROR; - case TCL_CONTINUE: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property getter for %s did a continue", TclGetString(propObj))); - return TCL_ERROR; - default: - return code; - } -} - -static inline int -WriteProperty( - Tcl_Interp *interp, - Object *oPtr, - Tcl_Obj *propObj, - Tcl_Obj *valueObj) -{ - Tcl_Obj *args[] = { - oPtr->fPtr->myName, - Tcl_ObjPrintf("", TclGetString(propObj)), - valueObj - }; - int code; - - Tcl_IncrRefCount(args[0]); - Tcl_IncrRefCount(args[1]); - Tcl_IncrRefCount(args[2]); - code = TclOOPrivateObjectCmd(oPtr, interp, 3, args); - Tcl_DecrRefCount(args[0]); - Tcl_DecrRefCount(args[1]); - Tcl_DecrRefCount(args[2]); - switch (code) { - case TCL_BREAK: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property setter for %s did a break", TclGetString(propObj))); - return TCL_ERROR; - case TCL_CONTINUE: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property setter for %s did a continue", TclGetString(propObj))); - return TCL_ERROR; - default: - return code; - } -} - -/* Look up a property full name. */ -static Tcl_Obj * -GetPropertyName( - Tcl_Interp *interp, /* Context and error reporting. */ - Object *oPtr, /* Object to get property name from. */ - int flags, /* Are we looking for a writable property? - * Can we do a fallback message? - * See GPNFlags for possible values */ - Tcl_Obj *namePtr, /* The name supplied by the user. */ - GPNCache **cachePtr) /* Where to cache the table, if the caller - * wants that. The contents are to be freed - * with Tcl_Free if the cache is used. */ -{ - Tcl_Size objc, index, i; - Tcl_Obj *listPtr = TclOOGetAllObjectProperties( - oPtr, flags & GPN_WRITABLE); - Tcl_Obj **objv; - GPNCache *tablePtr; - - (void) Tcl_ListObjGetElements(NULL, listPtr, &objc, &objv); - if (cachePtr && *cachePtr) { - tablePtr = *cachePtr; - } else { - tablePtr = (GPNCache *) TclStackAlloc(interp, - offsetof(GPNCache, names) + sizeof(char *) * (objc + 1)); - - for (i = 0; i < objc; i++) { - tablePtr->names[i] = TclGetString(objv[i]); - } - tablePtr->names[objc] = NULL; - if (cachePtr) { - /* - * Have a cache, but nothing in it so far. - * - * We cache the list here so it doesn't vanish from under our - * feet if a property implementation does something crazy like - * changing the set of properties. The type of copy this does - * means that the copy holds the references to the names in the - * table. - */ - tablePtr->listPtr = TclListObjCopy(NULL, listPtr); - Tcl_IncrRefCount(tablePtr->listPtr); - *cachePtr = tablePtr; - } else { - tablePtr->listPtr = NULL; - } - } - int result = Tcl_GetIndexFromObjStruct(interp, namePtr, tablePtr->names, - sizeof(char *), "property", TCL_INDEX_TEMP_TABLE, &index); - if (result == TCL_ERROR && !(flags & GPN_FALLING_BACK)) { - /* - * If property can be accessed the other way, use a special message. - * We use a recursive call to look this up. - */ - - Tcl_InterpState foo = Tcl_SaveInterpState(interp, result); - Tcl_Obj *otherName = GetPropertyName(interp, oPtr, - flags ^ (GPN_WRITABLE | GPN_FALLING_BACK), namePtr, NULL); - result = Tcl_RestoreInterpState(interp, foo); - if (otherName != NULL) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property \"%s\" is %s only", - TclGetString(otherName), - (flags & GPN_WRITABLE) ? "read" : "write")); - } - } - if (!cachePtr) { - TclStackFree(interp, tablePtr); - } - if (result != TCL_OK) { - return NULL; - } - return objv[index]; -} - -/* Release the cache made by GetPropertyName(). */ -static inline void -ReleasePropertyNameCache( - Tcl_Interp *interp, - GPNCache **cachePtr) -{ - if (*cachePtr) { - GPNCache *tablePtr = *cachePtr; - if (tablePtr->listPtr) { - Tcl_DecrRefCount(tablePtr->listPtr); - } - TclStackFree(interp, tablePtr); - *cachePtr = NULL; - } -} - -int -TclOO_Configurable_Configure( - TCL_UNUSED(void *), - Tcl_Interp *interp, /* Interpreter used for the result, error - * reporting, etc. */ - Tcl_ObjectContext context, /* The object/call context. */ - int objc, /* Number of arguments. */ - Tcl_Obj *const *objv) /* The actual arguments. */ -{ - Object *oPtr = (Object *) Tcl_ObjectContextObject(context); - Tcl_Size skip = Tcl_ObjectContextSkippedArgs(context); - Tcl_Obj *namePtr; - Tcl_Size i, namec; - int code = TCL_OK; - - objc -= skip; - if ((objc & 1) && (objc != 1)) { - /* - * Bad (odd > 1) number of arguments. - */ - - Tcl_WrongNumArgs(interp, skip, objv, "?-option value ...?"); - return TCL_ERROR; - } - - objv += skip; - if (objc == 0) { - /* - * Read all properties. - */ - - Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, 0); - Tcl_Obj *resultPtr = Tcl_NewObj(), **namev; - - Tcl_IncrRefCount(listPtr); - ListObjGetElements(listPtr, namec, namev); - - for (i = 0; i < namec; ) { - code = ReadProperty(interp, oPtr, namev[i]); - if (code != TCL_OK) { - Tcl_DecrRefCount(resultPtr); - break; - } - Tcl_DictObjPut(NULL, resultPtr, namev[i], - Tcl_GetObjResult(interp)); - if (++i >= namec) { - Tcl_SetObjResult(interp, resultPtr); - break; - } - Tcl_SetObjResult(interp, Tcl_NewObj()); - } - Tcl_DecrRefCount(listPtr); - return code; - } else if (objc == 1) { - /* - * Read a single named property. - */ - - namePtr = GetPropertyName(interp, oPtr, 0, objv[0], NULL); - if (namePtr == NULL) { - return TCL_ERROR; - } - return ReadProperty(interp, oPtr, namePtr); - } else if (objc == 2) { - /* - * Special case for writing to one property. Saves fiddling with the - * cache in this common case. - */ - - namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[0], NULL); - if (namePtr == NULL) { - return TCL_ERROR; - } - code = WriteProperty(interp, oPtr, namePtr, objv[1]); - if (code == TCL_OK) { - Tcl_ResetResult(interp); - } - return code; - } else { - /* - * Write properties. Slightly tricky because we want to cache the - * table of property names. - */ - GPNCache *cache = NULL; - - code = TCL_OK; - for (i = 0; i < objc; i += 2) { - namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[i], - &cache); - if (namePtr == NULL) { - code = TCL_ERROR; - break; - } - code = WriteProperty(interp, oPtr, namePtr, objv[i + 1]); - if (code != TCL_OK) { - break; - } - } - if (code == TCL_OK) { - Tcl_ResetResult(interp); - } - ReleasePropertyNameCache(interp, &cache); - return code; - } -} - -/* - * ---------------------------------------------------------------------- - * - * Configurable_Getter, Configurable_Setter -- - * - * Standard property implementation. The clientData is a simple Tcl_Obj* - * that contains the name of the property. - * - * TclOOImplementObjectProperty, TclOOImplementClassProperty -- - * - * Installs a basic property implementation for a property, either on - * an instance or on a class. It's up to the code that calls these - * to ensure that the property name is syntactically valid. - * - * ---------------------------------------------------------------------- - */ - -static int -Configurable_Getter( - void *clientData, /* Which property to read. Actually a Tcl_Obj* - * reference that is the name of the variable - * in the cpntext object. */ - Tcl_Interp *interp, /* Interpreter used for the result, error - * reporting, etc. */ - Tcl_ObjectContext context, /* The object/call context. */ - int objc, /* Number of arguments. */ - Tcl_Obj *const *objv) /* The actual arguments. */ -{ - Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; - Tcl_Var varPtr, aryVar; - Tcl_Obj *valuePtr; - - if ((int) Tcl_ObjectContextSkippedArgs(context) != objc) { - Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), - objv, NULL); - return TCL_ERROR; - } - - varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), - propNamePtr, &aryVar); - if (varPtr == NULL) { - return TCL_ERROR; - } - - valuePtr = TclPtrGetVar(interp, varPtr, aryVar, propNamePtr, NULL, - TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG); - if (valuePtr == NULL) { - return TCL_ERROR; - } - Tcl_SetObjResult(interp, valuePtr); - return TCL_OK; -} - -static int -Configurable_Setter( - void *clientData, /* Which property to write. Actually a Tcl_Obj* - * reference that is the name of the variable - * in the cpntext object. */ - Tcl_Interp *interp, /* Interpreter used for the result, error - * reporting, etc. */ - Tcl_ObjectContext context, /* The object/call context. */ - int objc, /* Number of arguments. */ - Tcl_Obj *const *objv) /* The actual arguments. */ -{ - Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; - Tcl_Var varPtr, aryVar; - - if ((int) Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { - Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), - objv, "value"); - return TCL_ERROR; - } - - varPtr = LookupObjectVar(interp, Tcl_ObjectContextObject(context), - propNamePtr, &aryVar); - if (varPtr == NULL) { - return TCL_ERROR; - } - - if (TclPtrSetVar(interp, varPtr, aryVar, propNamePtr, NULL, - objv[objc - 1], TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG) == NULL) { - return TCL_ERROR; - } - return TCL_OK; -} - -// Simple support functions -void DetailsDeleter( - void *clientData) -{ - // Just drop the reference count - Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; - Tcl_DecrRefCount(propNamePtr); -} - -int DetailsCloner( - TCL_UNUSED(Tcl_Interp *), - void *oldClientData, - void **newClientData) -{ - // Just add another reference to this name; easy! - Tcl_Obj *propNamePtr = (Tcl_Obj *) oldClientData; - Tcl_IncrRefCount(propNamePtr); - *newClientData = propNamePtr; - return TCL_OK; -} - -// Method descriptors -static Tcl_MethodType GetterType = { - TCL_OO_METHOD_VERSION_1, - "PropertyGetter", - Configurable_Getter, - DetailsDeleter, - DetailsCloner -}; - -static Tcl_MethodType SetterType = { - TCL_OO_METHOD_VERSION_1, - "PropertySetter", - Configurable_Setter, - DetailsDeleter, - DetailsCloner -}; - -void -TclOOImplementObjectProperty( - Tcl_Object targetObject, /* What to install into. */ - Tcl_Obj *propNamePtr, /* Property name, without leading - */ - int installGetter, /* Whether to install a standard getter. */ - int installSetter) /* Whether to install a standard setter. */ -{ - if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); - Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewInstanceMethod( - NULL, targetObject, methodName, 0, &GetterType, propNamePtr); - Tcl_BounceRefCount(methodName); - } - if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); - Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewInstanceMethod( - NULL, targetObject, methodName, 0, &SetterType, propNamePtr); - Tcl_BounceRefCount(methodName); - } -} - -void -TclOOImplementClassProperty( - Tcl_Class targetClass, /* What to install into. */ - Tcl_Obj *propNamePtr, /* Property name, without leading - */ - int installGetter, /* Whether to install a standard getter. */ - int installSetter) /* Whether to install a standard setter. */ -{ - if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); - Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewMethod(targetClass, methodName, 0, &GetterType, propNamePtr); - Tcl_BounceRefCount(methodName); - } - if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); - Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter - TclNewMethod(targetClass, methodName, 0, &SetterType, propNamePtr); - Tcl_BounceRefCount(methodName); - } -} - -/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOOCall.c b/generic/tclOOCall.c index 27d8233..6ce1ef3 100644 --- a/generic/tclOOCall.c +++ b/generic/tclOOCall.c @@ -2161,253 +2161,6 @@ AddDefinitionNamespaceToChain( } /* - * ---------------------------------------------------------------------- - * - * FindClassProps -- - * - * Discover the properties known to a class and its superclasses. - * The property names become the keys in the accumulator hash table - * (which is used as a set). - * - * ---------------------------------------------------------------------- - */ - -static void -FindClassProps( - Class *clsPtr, /* The object to inspect. Must exist. */ - int writable, /* Whether we're after the readable or writable - * property set. */ - Tcl_HashTable *accumulator) /* Where to gather the names. */ -{ - int i, dummy; - Tcl_Obj *propName; - Class *mixin, *sup; - - tailRecurse: - if (writable) { - FOREACH(propName, clsPtr->properties.writable) { - Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); - } - } else { - FOREACH(propName, clsPtr->properties.readable) { - Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); - } - } - if (clsPtr->thisPtr->flags & ROOT_OBJECT) { - /* - * We do *not* traverse upwards from the root! - */ - return; - } - FOREACH(mixin, clsPtr->mixins) { - FindClassProps(mixin, writable, accumulator); - } - if (clsPtr->superclasses.num == 1) { - clsPtr = clsPtr->superclasses.list[0]; - goto tailRecurse; - } - FOREACH(sup, clsPtr->superclasses) { - FindClassProps(sup, writable, accumulator); - } -} - -/* - * ---------------------------------------------------------------------- - * - * FindObjectProps -- - * - * Discover the properties known to an object and all its classes. - * The property names become the keys in the accumulator hash table - * (which is used as a set). - * - * ---------------------------------------------------------------------- - */ - -static void -FindObjectProps( - Object *oPtr, /* The object to inspect. Must exist. */ - int writable, /* Whether we're after the readable or writable - * property set. */ - Tcl_HashTable *accumulator) /* Where to gather the names. */ -{ - int i, dummy; - Tcl_Obj *propName; - Class *mixin; - - if (writable) { - FOREACH(propName, oPtr->properties.writable) { - Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); - } - } else { - FOREACH(propName, oPtr->properties.readable) { - Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); - } - } - FOREACH(mixin, oPtr->mixins) { - FindClassProps(mixin, writable, accumulator); - } - FindClassProps(oPtr->selfCls, writable, accumulator); -} - -/* - * ---------------------------------------------------------------------- - * - * TclOOGetAllClassProperties -- - * - * Get the list of all properties known to a class, including to its - * superclasses. Manages a cache so this operation is usually cheap. - * The order of properties in the resulting list is undefined. - * - * ---------------------------------------------------------------------- - */ - -Tcl_Obj * -TclOOGetAllClassProperties( - Class *clsPtr, /* The class to inspect. Must exist. */ - int writable, /* Whether to get writable properties. If - * false, readable properties will be returned - * instead. */ - int *allocated) /* Address of variable to set to true if a - * Tcl_Obj was allocated and may be safely - * modified by the caller. */ -{ - Tcl_HashTable hashTable; - FOREACH_HASH_DECLS; - Tcl_Obj *propName, *result; - void *dummy; - - /* - * Look in the cache. - */ - - if (clsPtr->properties.epoch == clsPtr->thisPtr->fPtr->epoch) { - if (writable) { - if (clsPtr->properties.allWritableCache) { - *allocated = 0; - return clsPtr->properties.allWritableCache; - } - } else { - if (clsPtr->properties.allReadableCache) { - *allocated = 0; - return clsPtr->properties.allReadableCache; - } - } - } - - /* - * Gather the information. Unsorted! (Caller will sort.) - */ - - *allocated = 1; - Tcl_InitObjHashTable(&hashTable); - FindClassProps(clsPtr, writable, &hashTable); - TclNewObj(result); - FOREACH_HASH(propName, dummy, &hashTable) { - Tcl_ListObjAppendElement(NULL, result, propName); - } - Tcl_DeleteHashTable(&hashTable); - - /* - * Cache the information. Also purges the cache. - */ - - if (clsPtr->properties.epoch != clsPtr->thisPtr->fPtr->epoch) { - if (clsPtr->properties.allWritableCache) { - Tcl_DecrRefCount(clsPtr->properties.allWritableCache); - clsPtr->properties.allWritableCache = NULL; - } - if (clsPtr->properties.allReadableCache) { - Tcl_DecrRefCount(clsPtr->properties.allReadableCache); - clsPtr->properties.allReadableCache = NULL; - } - } - clsPtr->properties.epoch = clsPtr->thisPtr->fPtr->epoch; - if (writable) { - clsPtr->properties.allWritableCache = result; - } else { - clsPtr->properties.allReadableCache = result; - } - Tcl_IncrRefCount(result); - return result; -} - -/* - * ---------------------------------------------------------------------- - * - * TclOOGetAllObjectProperties -- - * - * Get the sorted list of all properties known to a object, including to its - * its classes. Manages a cache so this operation is usually cheap. - * - * ---------------------------------------------------------------------- - */ - -Tcl_Obj * -TclOOGetAllObjectProperties( - Object *oPtr, /* The object to inspect. Must exist. */ - int writable) /* Whether to get writable properties. If - * false, readable properties will be returned - * instead. */ -{ - Tcl_HashTable hashTable; - FOREACH_HASH_DECLS; - Tcl_Obj *propName, *result; - void *dummy; - - /* - * Look in the cache. - */ - - if (oPtr->properties.epoch == oPtr->fPtr->epoch) { - if (writable) { - if (oPtr->properties.allWritableCache) { - return oPtr->properties.allWritableCache; - } - } else { - if (oPtr->properties.allReadableCache) { - return oPtr->properties.allReadableCache; - } - } - } - - /* - * Gather the information. Unsorted! (Caller will sort.) - */ - - Tcl_InitObjHashTable(&hashTable); - FindObjectProps(oPtr, writable, &hashTable); - TclNewObj(result); - FOREACH_HASH(propName, dummy, &hashTable) { - Tcl_ListObjAppendElement(NULL, result, propName); - } - Tcl_DeleteHashTable(&hashTable); - TclOOSortPropList(result); - - /* - * Cache the information. - */ - - if (oPtr->properties.epoch != oPtr->fPtr->epoch) { - if (oPtr->properties.allWritableCache) { - Tcl_DecrRefCount(oPtr->properties.allWritableCache); - oPtr->properties.allWritableCache = NULL; - } - if (oPtr->properties.allReadableCache) { - Tcl_DecrRefCount(oPtr->properties.allReadableCache); - oPtr->properties.allReadableCache = NULL; - } - } - oPtr->properties.epoch = oPtr->fPtr->epoch; - if (writable) { - oPtr->properties.allWritableCache = result; - } else { - oPtr->properties.allReadableCache = result; - } - Tcl_IncrRefCount(result); - return result; -} - -/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index 0e81cc8..ceb65ef 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -74,9 +74,6 @@ static inline Tcl_Namespace *GetNamespaceInOuterContext(Tcl_Interp *interp, static inline int InitDefineContext(Tcl_Interp *interp, Tcl_Namespace *namespacePtr, Object *oPtr, int objc, Tcl_Obj *const objv[]); -static int InstallStdPropertyImpls(void *useInstance, - Tcl_Interp *interp, Tcl_Obj *propName, - int readable, int writable); static inline void RecomputeClassCacheFlag(Object *oPtr); static int RenameDeleteMethod(Tcl_Interp *interp, Object *oPtr, int useClass, Tcl_Obj *const fromPtr, @@ -956,7 +953,7 @@ InitDefineContext( /* * ---------------------------------------------------------------------- * - * TclOOGetDefineCmdContext -- + * TclOOGetDefineCmdContext, TclOOGetClassDefineCmdContext -- * * Extracts the magic token from the current stack frame, or returns NULL * (and leaves an error message) otherwise. @@ -991,8 +988,8 @@ TclOOGetDefineCmdContext( return object; } -static Class * -GetClassDefineCmdContext( +Class * +TclOOGetClassDefineCmdContext( Tcl_Interp *interp) { Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); @@ -1637,7 +1634,7 @@ TclOODefineConstructorObjCmd( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Method method; Tcl_Size bodyLength; @@ -1702,7 +1699,7 @@ TclOODefineDefnNsObjCmd( NULL }; int kind = 0; - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Namespace *nsPtr; Tcl_Obj *nsNamePtr, **storagePtr; @@ -1832,7 +1829,7 @@ TclOODefineDestructorObjCmd( { Tcl_Method method; Tcl_Size bodyLength; - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); if (clsPtr == NULL) { return TCL_ERROR; @@ -2413,7 +2410,7 @@ ClassFilter_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Obj *resultObj, *filterObj; Tcl_Size i; @@ -2441,7 +2438,7 @@ ClassFilter_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size filterc; Tcl_Obj **filterv; @@ -2482,7 +2479,7 @@ ClassMixin_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Class *mixinPtr; Tcl_Size i; @@ -2513,7 +2510,7 @@ ClassMixin_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size mixinc, i; Tcl_Obj **mixinv; Class **mixins; /* The references to the classes to actually @@ -2591,7 +2588,7 @@ ClassSuper_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Class *superPtr; Tcl_Size i; @@ -2621,7 +2618,7 @@ ClassSuper_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size superc, j; Tcl_Size i; Tcl_Obj **superv; @@ -2749,7 +2746,7 @@ ClassVars_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Obj *resultObj; Tcl_Size i; @@ -2787,7 +2784,7 @@ ClassVars_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size i; Tcl_Size varc; Tcl_Obj **varv; @@ -3159,140 +3156,6 @@ ResolveClass( /* * ---------------------------------------------------------------------- * - * SetPropertyList -- - * - * Helper for writing a property list (which is actually a set). - * - * ---------------------------------------------------------------------- - */ -static inline void -SetPropertyList( - PropertyList *propList, /* The property list to write. Replaces the - * property list's contents. */ - Tcl_Size objc, /* Number of property names. */ - Tcl_Obj *const objv[]) /* Property names. */ -{ - Tcl_Size i, n; - Tcl_Obj *propObj; - int created; - Tcl_HashTable uniqueTable; - - for (i=0 ; ilist); - } else if (i) { - propList->list = (Tcl_Obj **) - Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * objc); - } else { - propList->list = (Tcl_Obj **) - Tcl_Alloc(sizeof(Tcl_Obj *) * objc); - } - } - propList->num = 0; - if (objc > 0) { - Tcl_InitObjHashTable(&uniqueTable); - for (i=n=0 ; ilist[n++] = objv[i]; - } else { - Tcl_DecrRefCount(objv[i]); - } - } - propList->num = n; - - /* - * Shouldn't be necessary, but maintain num/list invariant. - */ - - if (n != objc) { - propList->list = (Tcl_Obj **) - Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * n); - } - Tcl_DeleteHashTable(&uniqueTable); - } -} - -/* - * ---------------------------------------------------------------------- - * - * InstallReadableProps -- - * - * Helper for writing the readable property list (which is actually a set) - * that includes flushing the name cache. - * - * ---------------------------------------------------------------------- - */ -static inline void -InstallReadableProps( - PropertyStorage *props, /* Which property list to install into. */ - Tcl_Size objc, /* Number of property names. */ - Tcl_Obj *const objv[]) /* Property names. */ -{ - if (props->allReadableCache) { - Tcl_DecrRefCount(props->allReadableCache); - props->allReadableCache = NULL; - } - - SetPropertyList(&props->readable, objc, objv); -} - -/* - * ---------------------------------------------------------------------- - * - * InstallWritableProps -- - * - * Helper for writing the writable property list (which is actually a set) - * that includes flushing the name cache. - * - * ---------------------------------------------------------------------- - */ -static inline void -InstallWritableProps( - PropertyStorage *props, /* Which property list to install into. */ - Tcl_Size objc, /* Number of property names. */ - Tcl_Obj *const objv[]) /* Property names. */ -{ - if (props->allWritableCache) { - Tcl_DecrRefCount(props->allWritableCache); - props->allWritableCache = NULL; - } - - SetPropertyList(&props->writable, objc, objv); -} - -/* - * ---------------------------------------------------------------------- - * - * GetPropertyList -- - * - * Helper for reading a property list. - * - * ---------------------------------------------------------------------- - */ -static inline Tcl_Obj * -GetPropertyList( - PropertyList *propList) /* The property list to read. */ -{ - Tcl_Obj *resultObj, *propNameObj; - Tcl_Size i; - - TclNewObj(resultObj); - FOREACH(propNameObj, *propList) { - Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); - } - return resultObj; -} - -/* - * ---------------------------------------------------------------------- - * * Configurable_ClassReadableProps_Get, Configurable_ClassReadableProps_Set, * Configurable_ObjectReadableProps_Get, Configurable_ObjectReadableProps_Set -- * @@ -3310,7 +3173,7 @@ Configurable_ClassReadableProps_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); if (clsPtr == NULL) { return TCL_ERROR; @@ -3320,7 +3183,7 @@ Configurable_ClassReadableProps_Get( return TCL_ERROR; } - Tcl_SetObjResult(interp, GetPropertyList(&clsPtr->properties.readable)); + Tcl_SetObjResult(interp, TclOOGetPropertyList(&clsPtr->properties.readable)); return TCL_OK; } @@ -3332,7 +3195,7 @@ Configurable_ClassReadableProps_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size varc; Tcl_Obj **varv; @@ -3349,7 +3212,7 @@ Configurable_ClassReadableProps_Set( return TCL_ERROR; } - InstallReadableProps(&clsPtr->properties, varc, varv); + TclOOInstallReadableProps(&clsPtr->properties, varc, varv); BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3372,7 +3235,7 @@ Configurable_ObjectReadableProps_Get( return TCL_ERROR; } - Tcl_SetObjResult(interp, GetPropertyList(&oPtr->properties.readable)); + Tcl_SetObjResult(interp, TclOOGetPropertyList(&oPtr->properties.readable)); return TCL_OK; } @@ -3402,7 +3265,7 @@ Configurable_ObjectReadableProps_Set( return TCL_ERROR; } - InstallReadableProps(&oPtr->properties, varc, varv); + TclOOInstallReadableProps(&oPtr->properties, varc, varv); return TCL_OK; } @@ -3426,7 +3289,7 @@ Configurable_ClassWritableProps_Get( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); if (clsPtr == NULL) { return TCL_ERROR; @@ -3436,7 +3299,7 @@ Configurable_ClassWritableProps_Get( return TCL_ERROR; } - Tcl_SetObjResult(interp, GetPropertyList(&clsPtr->properties.writable)); + Tcl_SetObjResult(interp, TclOOGetPropertyList(&clsPtr->properties.writable)); return TCL_OK; } @@ -3448,7 +3311,7 @@ Configurable_ClassWritableProps_Set( int objc, Tcl_Obj *const *objv) { - Class *clsPtr = GetClassDefineCmdContext(interp); + Class *clsPtr = TclOOGetClassDefineCmdContext(interp); Tcl_Size varc; Tcl_Obj **varv; @@ -3465,7 +3328,7 @@ Configurable_ClassWritableProps_Set( return TCL_ERROR; } - InstallWritableProps(&clsPtr->properties, varc, varv); + TclOOInstallWritableProps(&clsPtr->properties, varc, varv); BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3488,7 +3351,7 @@ Configurable_ObjectWritableProps_Get( return TCL_ERROR; } - Tcl_SetObjResult(interp, GetPropertyList(&oPtr->properties.writable)); + Tcl_SetObjResult(interp, TclOOGetPropertyList(&oPtr->properties.writable)); return TCL_OK; } @@ -3518,7 +3381,7 @@ Configurable_ObjectWritableProps_Set( return TCL_ERROR; } - InstallWritableProps(&oPtr->properties, varc, varv); + TclOOInstallWritableProps(&oPtr->properties, varc, varv); return TCL_OK; } @@ -3586,13 +3449,13 @@ TclOORegisterInstanceProperty( if (BuildPropertyList(&oPtr->properties.readable, propName, registerReader, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - InstallReadableProps(&oPtr->properties, count, objv); + TclOOInstallReadableProps(&oPtr->properties, count, objv); } if (BuildPropertyList(&oPtr->properties.writable, propName, registerWriter, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - InstallWritableProps(&oPtr->properties, count, objv); + TclOOInstallWritableProps(&oPtr->properties, count, objv); } Tcl_BounceRefCount(listObj); } @@ -3618,14 +3481,14 @@ TclOORegisterProperty( if (BuildPropertyList(&clsPtr->properties.readable, propName, registerReader, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - InstallReadableProps(&clsPtr->properties, count, objv); + TclOOInstallReadableProps(&clsPtr->properties, count, objv); changed = 1; } if (BuildPropertyList(&clsPtr->properties.writable, propName, registerWriter, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - InstallWritableProps(&clsPtr->properties, count, objv); + TclOOInstallWritableProps(&clsPtr->properties, count, objv); changed = 1; } Tcl_BounceRefCount(listObj); @@ -3724,11 +3587,11 @@ TclOOPropertyDefinitionCmd( } /* - * Install the property. Note that InstallStdPropertyImpls + * Install the property. Note that TclOOInstallStdPropertyImpls * validates the property name as well. */ - if (InstallStdPropertyImpls(useInstance, interp, propObj, + if (TclOOInstallStdPropertyImpls(useInstance, interp, propObj, kind != KIND_WO && getterScript == NULL, kind != KIND_RO && setterScript == NULL) != TCL_OK) { return TCL_ERROR; @@ -3798,81 +3661,6 @@ TclOOPropertyDefinitionCmd( } /* - * ---------------------------------------------------------------------- - * - * InstallStdPropertyImpls -- - * - * Validates a (dashless) property name, and installs implementation - * methods if asked to do so (readable and writable flags). - * - * ---------------------------------------------------------------------- - */ - -static int -InstallStdPropertyImpls( - void *useInstance, - Tcl_Interp *interp, - Tcl_Obj *propName, - int readable, - int writable) -{ - const char *name, *reason; - Tcl_Size len; - char flag = TCL_DONT_QUOTE_HASH; - - /* - * Validate the property name. Note that just calling TclScanElement() is - * cheaper than actually formatting a list and comparing the string - * version of that with the original, as TclScanElement() is one of the - * core parts of doing that; this skips a whole load of irrelevant memory - * allocations! - */ - - name = Tcl_GetStringFromObj(propName, &len); - if (Tcl_StringMatch(name, "-*")) { - reason = "must not begin with -"; - goto badProp; - } - if (TclScanElement(name, len, &flag) != len) { - reason = "must be a simple word"; - goto badProp; - } - if (Tcl_StringMatch(name, "*::*")) { - reason = "must not contain namespace separators"; - goto badProp; - } - if (Tcl_StringMatch(name, "*[()]*")) { - reason = "must not contain parentheses"; - goto badProp; - } - - /* - * Install the implementations... if asked to do so. - */ - - if (useInstance) { - Tcl_Object object = TclOOGetDefineCmdContext(interp); - if (!object) { - return TCL_ERROR; - } - TclOOImplementObjectProperty(object, propName, readable, writable); - } else { - Tcl_Class cls = (Tcl_Class) GetClassDefineCmdContext(interp); - if (!cls) { - return TCL_ERROR; - } - TclOOImplementClassProperty(cls, propName, readable, writable); - } - return TCL_OK; - - badProp: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "bad property name \"%s\": %s", name, reason)); - Tcl_SetErrorCode(interp, "TCL", "OO", "PROPERTY_FORMAT", NULL); - return TCL_ERROR; -} - -/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOOInfo.c b/generic/tclOOInfo.c index feac2c0..26241b2 100644 --- a/generic/tclOOInfo.c +++ b/generic/tclOOInfo.c @@ -16,7 +16,6 @@ #include "tclInt.h" #include "tclOOInt.h" -static inline Class * GetClassFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr); static Tcl_ObjCmdProc InfoObjectCallCmd; static Tcl_ObjCmdProc InfoObjectClassCmd; static Tcl_ObjCmdProc InfoObjectDefnCmd; @@ -28,7 +27,6 @@ static Tcl_ObjCmdProc InfoObjectMethodsCmd; static Tcl_ObjCmdProc InfoObjectMethodTypeCmd; static Tcl_ObjCmdProc InfoObjectMixinsCmd; static Tcl_ObjCmdProc InfoObjectNsCmd; -static Tcl_ObjCmdProc InfoObjectPropCmd; static Tcl_ObjCmdProc InfoObjectVarsCmd; static Tcl_ObjCmdProc InfoObjectVariablesCmd; static Tcl_ObjCmdProc InfoClassCallCmd; @@ -42,7 +40,6 @@ static Tcl_ObjCmdProc InfoClassInstancesCmd; static Tcl_ObjCmdProc InfoClassMethodsCmd; static Tcl_ObjCmdProc InfoClassMethodTypeCmd; static Tcl_ObjCmdProc InfoClassMixinsCmd; -static Tcl_ObjCmdProc InfoClassPropCmd; static Tcl_ObjCmdProc InfoClassSubsCmd; static Tcl_ObjCmdProc InfoClassSupersCmd; static Tcl_ObjCmdProc InfoClassVariablesCmd; @@ -63,7 +60,7 @@ static const EnsembleImplMap infoObjectCmds[] = { {"methodtype", InfoObjectMethodTypeCmd, TclCompileBasic2ArgCmd, NULL, NULL, 0}, {"mixins", InfoObjectMixinsCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, {"namespace", InfoObjectNsCmd, TclCompileInfoObjectNamespaceCmd, NULL, NULL, 0}, - {"properties", InfoObjectPropCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0}, + {"properties", TclOOInfoObjectPropCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0}, {"variables", InfoObjectVariablesCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0}, {"vars", InfoObjectVarsCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0}, {NULL, NULL, NULL, NULL, NULL, 0} @@ -85,7 +82,7 @@ static const EnsembleImplMap infoClassCmds[] = { {"methods", InfoClassMethodsCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0}, {"methodtype", InfoClassMethodTypeCmd, TclCompileBasic2ArgCmd, NULL, NULL, 0}, {"mixins", InfoClassMixinsCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, - {"properties", InfoClassPropCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0}, + {"properties", TclOOInfoClassPropCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0}, {"subclasses", InfoClassSubsCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0}, {"superclasses", InfoClassSupersCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, {"variables", InfoClassVariablesCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0}, @@ -150,7 +147,7 @@ TclOOInitInfo( /* * ---------------------------------------------------------------------- * - * GetClassFromObj -- + * TclOOGetClassFromObj -- * * How to correctly get a class from a Tcl_Obj. Just a wrapper round * Tcl_GetObjectFromObj, but this is an idiom that was used heavily. @@ -158,8 +155,8 @@ TclOOInitInfo( * ---------------------------------------------------------------------- */ -static inline Class * -GetClassFromObj( +Class * +TclOOGetClassFromObj( Tcl_Interp *interp, Tcl_Obj *objPtr) { @@ -215,7 +212,7 @@ InfoObjectClassCmd( Class *mixinPtr, *o2clsPtr; Tcl_Size i; - o2clsPtr = GetClassFromObj(interp, objv[2]); + o2clsPtr = TclOOGetClassFromObj(interp, objv[2]); if (o2clsPtr == NULL) { return TCL_ERROR; } @@ -998,7 +995,7 @@ InfoClassConstrCmd( Tcl_WrongNumArgs(interp, 1, objv, "className"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1059,7 +1056,7 @@ InfoClassDefnCmd( Tcl_WrongNumArgs(interp, 1, objv, "className methodName"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1129,7 +1126,7 @@ InfoClassDefnNsCmd( Tcl_WrongNumArgs(interp, 1, objv, "className ?kind?"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1173,7 +1170,7 @@ InfoClassDestrCmd( Tcl_WrongNumArgs(interp, 1, objv, "className"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1218,7 +1215,7 @@ InfoClassFiltersCmd( Tcl_WrongNumArgs(interp, 1, objv, "className"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1256,7 +1253,7 @@ InfoClassForwardCmd( Tcl_WrongNumArgs(interp, 1, objv, "className methodName"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1309,7 +1306,7 @@ InfoClassInstancesCmd( Tcl_WrongNumArgs(interp, 1, objv, "className ?pattern?"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1368,7 +1365,7 @@ InfoClassMethodsCmd( Tcl_WrongNumArgs(interp, 1, objv, "className ?-option value ...?"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1484,7 +1481,7 @@ InfoClassMethodTypeCmd( Tcl_WrongNumArgs(interp, 1, objv, "className methodName"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1536,7 +1533,7 @@ InfoClassMixinsCmd( Tcl_WrongNumArgs(interp, 1, objv, "className"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1579,7 +1576,7 @@ InfoClassSubsCmd( Tcl_WrongNumArgs(interp, 1, objv, "className ?pattern?"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1633,7 +1630,7 @@ InfoClassSupersCmd( Tcl_WrongNumArgs(interp, 1, objv, "className"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1679,7 +1676,7 @@ InfoClassVariablesCmd( } isPrivate = 1; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1772,7 +1769,7 @@ InfoClassCallCmd( Tcl_WrongNumArgs(interp, 1, objv, "className methodName"); return TCL_ERROR; } - clsPtr = GetClassFromObj(interp, objv[1]); + clsPtr = TclOOGetClassFromObj(interp, objv[1]); if (clsPtr == NULL) { return TCL_ERROR; } @@ -1793,181 +1790,6 @@ InfoClassCallCmd( } /* - * ---------------------------------------------------------------------- - * - * InfoClassPropCmd, InfoObjectPropCmd -- - * - * Implements [info class properties $clsName ?$option...?] and - * [info object properties $objName ?$option...?] - * - * ---------------------------------------------------------------------- - */ - -enum PropOpt { - PROP_ALL, PROP_READABLE, PROP_WRITABLE -}; -static const char *const propOptNames[] = { - "-all", "-readable", "-writable", - NULL -}; - -static int -InfoClassPropCmd( - TCL_UNUSED(void *), - Tcl_Interp *interp, - int objc, - Tcl_Obj *const objv[]) -{ - Class *clsPtr; - int i, idx, all = 0, writable = 0, allocated = 0; - Tcl_Obj *result, *propObj; - - if (objc < 2) { - Tcl_WrongNumArgs(interp, 1, objv, "className ?options...?"); - return TCL_ERROR; - } - clsPtr = GetClassFromObj(interp, objv[1]); - if (clsPtr == NULL) { - return TCL_ERROR; - } - for (i = 2; i < objc; i++) { - if (Tcl_GetIndexFromObj(interp, objv[i], propOptNames, "option", 0, - &idx) != TCL_OK) { - return TCL_ERROR; - } - switch (idx) { - case PROP_ALL: - all = 1; - break; - case PROP_READABLE: - writable = 0; - break; - case PROP_WRITABLE: - writable = 1; - break; - } - } - - /* - * Get the properties. - */ - - if (all) { - result = TclOOGetAllClassProperties(clsPtr, writable, &allocated); - if (allocated) { - TclOOSortPropList(result); - } - } else { - TclNewObj(result); - if (writable) { - FOREACH(propObj, clsPtr->properties.writable) { - Tcl_ListObjAppendElement(NULL, result, propObj); - } - } else { - FOREACH(propObj, clsPtr->properties.readable) { - Tcl_ListObjAppendElement(NULL, result, propObj); - } - } - TclOOSortPropList(result); - } - Tcl_SetObjResult(interp, result); - return TCL_OK; -} - -static int -InfoObjectPropCmd( - TCL_UNUSED(void *), - Tcl_Interp *interp, - int objc, - Tcl_Obj *const objv[]) -{ - Object *oPtr; - int i, idx, all = 0, writable = 0; - Tcl_Obj *result, *propObj; - - if (objc < 2) { - Tcl_WrongNumArgs(interp, 1, objv, "objName ?options...?"); - return TCL_ERROR; - } - oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[1]); - if (oPtr == NULL) { - return TCL_ERROR; - } - for (i = 2; i < objc; i++) { - if (Tcl_GetIndexFromObj(interp, objv[i], propOptNames, "option", 0, - &idx) != TCL_OK) { - return TCL_ERROR; - } - switch (idx) { - case PROP_ALL: - all = 1; - break; - case PROP_READABLE: - writable = 0; - break; - case PROP_WRITABLE: - writable = 1; - break; - } - } - - /* - * Get the properties. - */ - - if (all) { - result = TclOOGetAllObjectProperties(oPtr, writable); - } else { - TclNewObj(result); - if (writable) { - FOREACH(propObj, oPtr->properties.writable) { - Tcl_ListObjAppendElement(NULL, result, propObj); - } - } else { - FOREACH(propObj, oPtr->properties.readable) { - Tcl_ListObjAppendElement(NULL, result, propObj); - } - } - TclOOSortPropList(result); - } - Tcl_SetObjResult(interp, result); - return TCL_OK; -} - -/* - * ---------------------------------------------------------------------- - * - * SortPropList -- - * Sort a list of names of properties. Simple support function. Assumes - * that the list Tcl_Obj is unshared and doesn't have a string - * representation. - * - * ---------------------------------------------------------------------- - */ - -static int -PropNameCompare( - const void *a, - const void *b) -{ - Tcl_Obj *first = *(Tcl_Obj **) a; - Tcl_Obj *second = *(Tcl_Obj **) b; - - return strcmp(TclGetString(first), TclGetString(second)); -} - -void -TclOOSortPropList( - Tcl_Obj *list) -{ - Tcl_Size ec; - Tcl_Obj **ev; - - Tcl_ListObjGetElements(NULL, list, &ec, &ev); - qsort(ev, ec, sizeof(Tcl_Obj *), PropNameCompare); -} - -/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 5377ca3..88ce0e7 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -515,6 +515,8 @@ MODULE_SCOPE Tcl_ObjCmdProc TclOOCopyObjectCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOONextObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOONextToObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOSelfObjCmd; +MODULE_SCOPE Tcl_ObjCmdProc TclOOInfoObjectPropCmd; +MODULE_SCOPE Tcl_ObjCmdProc TclOOInfoClassPropCmd; /* * Method implementations (in tclOOBasic.c). @@ -570,14 +572,15 @@ MODULE_SCOPE void TclOODeleteContext(CallContext *contextPtr); MODULE_SCOPE void TclOODeleteDescendants(Tcl_Interp *interp, Object *oPtr); MODULE_SCOPE void TclOODelMethodRef(Method *method); -MODULE_SCOPE Tcl_Obj * TclOOGetAllClassProperties(Class *clsPtr, - int writable, int *allocated); MODULE_SCOPE Tcl_Obj * TclOOGetAllObjectProperties(Object *oPtr, int writable); MODULE_SCOPE CallContext *TclOOGetCallContext(Object *oPtr, Tcl_Obj *methodNameObj, int flags, Object *contextObjPtr, Class *contextClsPtr, Tcl_Obj *cacheInThisObj); +MODULE_SCOPE Class * TclOOGetClassDefineCmdContext(Tcl_Interp *interp); +MODULE_SCOPE Class * TclOOGetClassFromObj(Tcl_Interp *interp, + Tcl_Obj *objPtr); MODULE_SCOPE Tcl_Namespace *TclOOGetDefineContextNamespace( Tcl_Interp *interp, Object *oPtr, int forClass); MODULE_SCOPE CallChain *TclOOGetStereotypeCallChain(Class *clsPtr, @@ -602,6 +605,9 @@ MODULE_SCOPE void TclOOInitInfo(Tcl_Interp *interp); MODULE_SCOPE int TclOOInvokeContext(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); +MODULE_SCOPE Tcl_Var TclOOLookupObjectVar(Tcl_Interp *interp, + Tcl_Object object, Tcl_Obj *varName, + Tcl_Var *aryPtr); MODULE_SCOPE int TclNRObjectContextInvokeNext(Tcl_Interp *interp, Tcl_ObjectContext context, Tcl_Size objc, Tcl_Obj *const *objv, Tcl_Size skip); @@ -618,7 +624,15 @@ MODULE_SCOPE int TclOORemoveFromSubclasses(Class *subPtr, Class *superPtr); MODULE_SCOPE Tcl_Obj * TclOORenderCallChain(Tcl_Interp *interp, CallChain *callPtr); -MODULE_SCOPE void TclOOSortPropList(Tcl_Obj *listPtr); +MODULE_SCOPE Tcl_Obj * TclOOGetPropertyList(PropertyList *propList); +MODULE_SCOPE void TclOOReleasePropertyStorage(PropertyStorage *propsPtr); +MODULE_SCOPE void TclOOInstallReadableProps(PropertyStorage *props, + Tcl_Size objc, Tcl_Obj *const objv[]); +MODULE_SCOPE void TclOOInstallWritableProps(PropertyStorage *props, + Tcl_Size objc, Tcl_Obj *const objv[]); +MODULE_SCOPE int TclOOInstallStdPropertyImpls(void *useInstance, + Tcl_Interp *interp, Tcl_Obj *propName, + int readable, int writable); MODULE_SCOPE void TclOOStashContext(Tcl_Obj *objPtr, CallContext *contextPtr); MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); diff --git a/generic/tclOOProp.c b/generic/tclOOProp.c new file mode 100644 index 0000000..ef66f4e --- /dev/null +++ b/generic/tclOOProp.c @@ -0,0 +1,1174 @@ +/* + * tclOOProp.c -- + * + * This file contains implementations of the configurable property + * mecnanisms. + * + * Copyright © 2023-2024 Donal K. Fellows + * + * See the file "license.terms" for information on usage and redistribution of + * this file, and for a DISCLAIMER OF ALL WARRANTIES. + */ + +#include "tclOOInt.h" + +/* Short-term cache for GetPropertyName(). */ +typedef struct GPNCache { + Tcl_Obj *listPtr; /* Holds references to names. */ + char *names[TCLFLEXARRAY]; /* NULL-terminated table of names. */ +} GPNCache; + +enum GPNFlags { + GPN_WRITABLE = 1, /* Are we looking for a writable property? */ + GPN_FALLING_BACK = 2 /* Are we doing a recursive call to determine + * if the property is of the other type? */ +}; + +/* + * Shared bits for [property] declarations. + */ +enum PropOpt { + PROP_ALL, PROP_READABLE, PROP_WRITABLE +}; +static const char *const propOptNames[] = { + "-all", "-readable", "-writable", + NULL +}; + +/* + * Forward declarations. + */ + +static int Configurable_Getter(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static int Configurable_Setter(void *clientData, + Tcl_Interp *interp, Tcl_ObjectContext context, + int objc, Tcl_Obj *const *objv); +static void DetailsDeleter(void *clientData); +static int DetailsCloner(Tcl_Interp *, void *oldClientData, + void **newClientData); + +/* + * Method descriptors + */ + +static const Tcl_MethodType GetterType = { + TCL_OO_METHOD_VERSION_1, + "PropertyGetter", + Configurable_Getter, + DetailsDeleter, + DetailsCloner +}; + +static const Tcl_MethodType SetterType = { + TCL_OO_METHOD_VERSION_1, + "PropertySetter", + Configurable_Setter, + DetailsDeleter, + DetailsCloner +}; + +/* + * ---------------------------------------------------------------------- + * + * TclOO_Configurable_Configure -- + * + * Implementation of the oo::configurable->configure method. + * + * ---------------------------------------------------------------------- + */ + +/* + * Ugly thunks to read and write a property by calling the right method in + * the right way. Note that we MUST be correct in holding references to Tcl_Obj + * values, as this is potentially a call into user code. + */ +static inline int +ReadProperty( + Tcl_Interp *interp, + Object *oPtr, + Tcl_Obj *propObj) +{ + Tcl_Obj *args[] = { + oPtr->fPtr->myName, + Tcl_ObjPrintf("", TclGetString(propObj)) + }; + int code; + + Tcl_IncrRefCount(args[0]); + Tcl_IncrRefCount(args[1]); + code = TclOOPrivateObjectCmd(oPtr, interp, 2, args); + Tcl_DecrRefCount(args[0]); + Tcl_DecrRefCount(args[1]); + switch (code) { + case TCL_BREAK: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property getter for %s did a break", TclGetString(propObj))); + return TCL_ERROR; + case TCL_CONTINUE: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property getter for %s did a continue", TclGetString(propObj))); + return TCL_ERROR; + default: + return code; + } +} + +static inline int +WriteProperty( + Tcl_Interp *interp, + Object *oPtr, + Tcl_Obj *propObj, + Tcl_Obj *valueObj) +{ + Tcl_Obj *args[] = { + oPtr->fPtr->myName, + Tcl_ObjPrintf("", TclGetString(propObj)), + valueObj + }; + int code; + + Tcl_IncrRefCount(args[0]); + Tcl_IncrRefCount(args[1]); + Tcl_IncrRefCount(args[2]); + code = TclOOPrivateObjectCmd(oPtr, interp, 3, args); + Tcl_DecrRefCount(args[0]); + Tcl_DecrRefCount(args[1]); + Tcl_DecrRefCount(args[2]); + switch (code) { + case TCL_BREAK: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property setter for %s did a break", TclGetString(propObj))); + return TCL_ERROR; + case TCL_CONTINUE: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property setter for %s did a continue", TclGetString(propObj))); + return TCL_ERROR; + default: + return code; + } +} + +/* Look up a property full name. */ +static Tcl_Obj * +GetPropertyName( + Tcl_Interp *interp, /* Context and error reporting. */ + Object *oPtr, /* Object to get property name from. */ + int flags, /* Are we looking for a writable property? + * Can we do a fallback message? + * See GPNFlags for possible values */ + Tcl_Obj *namePtr, /* The name supplied by the user. */ + GPNCache **cachePtr) /* Where to cache the table, if the caller + * wants that. The contents are to be freed + * with Tcl_Free if the cache is used. */ +{ + Tcl_Size objc, index, i; + Tcl_Obj *listPtr = TclOOGetAllObjectProperties( + oPtr, flags & GPN_WRITABLE); + Tcl_Obj **objv; + GPNCache *tablePtr; + + (void) Tcl_ListObjGetElements(NULL, listPtr, &objc, &objv); + if (cachePtr && *cachePtr) { + tablePtr = *cachePtr; + } else { + tablePtr = (GPNCache *) TclStackAlloc(interp, + offsetof(GPNCache, names) + sizeof(char *) * (objc + 1)); + + for (i = 0; i < objc; i++) { + tablePtr->names[i] = TclGetString(objv[i]); + } + tablePtr->names[objc] = NULL; + if (cachePtr) { + /* + * Have a cache, but nothing in it so far. + * + * We cache the list here so it doesn't vanish from under our + * feet if a property implementation does something crazy like + * changing the set of properties. The type of copy this does + * means that the copy holds the references to the names in the + * table. + */ + tablePtr->listPtr = TclListObjCopy(NULL, listPtr); + Tcl_IncrRefCount(tablePtr->listPtr); + *cachePtr = tablePtr; + } else { + tablePtr->listPtr = NULL; + } + } + int result = Tcl_GetIndexFromObjStruct(interp, namePtr, tablePtr->names, + sizeof(char *), "property", TCL_INDEX_TEMP_TABLE, &index); + if (result == TCL_ERROR && !(flags & GPN_FALLING_BACK)) { + /* + * If property can be accessed the other way, use a special message. + * We use a recursive call to look this up. + */ + + Tcl_InterpState foo = Tcl_SaveInterpState(interp, result); + Tcl_Obj *otherName = GetPropertyName(interp, oPtr, + flags ^ (GPN_WRITABLE | GPN_FALLING_BACK), namePtr, NULL); + result = Tcl_RestoreInterpState(interp, foo); + if (otherName != NULL) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "property \"%s\" is %s only", + TclGetString(otherName), + (flags & GPN_WRITABLE) ? "read" : "write")); + } + } + if (!cachePtr) { + TclStackFree(interp, tablePtr); + } + if (result != TCL_OK) { + return NULL; + } + return objv[index]; +} + +/* Release the cache made by GetPropertyName(). */ +static inline void +ReleasePropertyNameCache( + Tcl_Interp *interp, + GPNCache **cachePtr) +{ + if (*cachePtr) { + GPNCache *tablePtr = *cachePtr; + if (tablePtr->listPtr) { + Tcl_DecrRefCount(tablePtr->listPtr); + } + TclStackFree(interp, tablePtr); + *cachePtr = NULL; + } +} + +int +TclOO_Configurable_Configure( + TCL_UNUSED(void *), + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Object *oPtr = (Object *) Tcl_ObjectContextObject(context); + Tcl_Size skip = Tcl_ObjectContextSkippedArgs(context); + Tcl_Obj *namePtr; + Tcl_Size i, namec; + int code = TCL_OK; + + objc -= skip; + if ((objc & 1) && (objc != 1)) { + /* + * Bad (odd > 1) number of arguments. + */ + + Tcl_WrongNumArgs(interp, skip, objv, "?-option value ...?"); + return TCL_ERROR; + } + + objv += skip; + if (objc == 0) { + /* + * Read all properties. + */ + + Tcl_Obj *listPtr = TclOOGetAllObjectProperties(oPtr, 0); + Tcl_Obj *resultPtr = Tcl_NewObj(), **namev; + + Tcl_IncrRefCount(listPtr); + ListObjGetElements(listPtr, namec, namev); + + for (i = 0; i < namec; ) { + code = ReadProperty(interp, oPtr, namev[i]); + if (code != TCL_OK) { + Tcl_DecrRefCount(resultPtr); + break; + } + Tcl_DictObjPut(NULL, resultPtr, namev[i], + Tcl_GetObjResult(interp)); + if (++i >= namec) { + Tcl_SetObjResult(interp, resultPtr); + break; + } + Tcl_SetObjResult(interp, Tcl_NewObj()); + } + Tcl_DecrRefCount(listPtr); + return code; + } else if (objc == 1) { + /* + * Read a single named property. + */ + + namePtr = GetPropertyName(interp, oPtr, 0, objv[0], NULL); + if (namePtr == NULL) { + return TCL_ERROR; + } + return ReadProperty(interp, oPtr, namePtr); + } else if (objc == 2) { + /* + * Special case for writing to one property. Saves fiddling with the + * cache in this common case. + */ + + namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[0], NULL); + if (namePtr == NULL) { + return TCL_ERROR; + } + code = WriteProperty(interp, oPtr, namePtr, objv[1]); + if (code == TCL_OK) { + Tcl_ResetResult(interp); + } + return code; + } else { + /* + * Write properties. Slightly tricky because we want to cache the + * table of property names. + */ + GPNCache *cache = NULL; + + code = TCL_OK; + for (i = 0; i < objc; i += 2) { + namePtr = GetPropertyName(interp, oPtr, GPN_WRITABLE, objv[i], + &cache); + if (namePtr == NULL) { + code = TCL_ERROR; + break; + } + code = WriteProperty(interp, oPtr, namePtr, objv[i + 1]); + if (code != TCL_OK) { + break; + } + } + if (code == TCL_OK) { + Tcl_ResetResult(interp); + } + ReleasePropertyNameCache(interp, &cache); + return code; + } +} + +/* + * ---------------------------------------------------------------------- + * + * Configurable_Getter, Configurable_Setter -- + * + * Standard property implementation. The clientData is a simple Tcl_Obj* + * that contains the name of the property. + * + * ---------------------------------------------------------------------- + */ + +static int +Configurable_Getter( + void *clientData, /* Which property to read. Actually a Tcl_Obj* + * reference that is the name of the variable + * in the cpntext object. */ + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_Var varPtr, aryVar; + Tcl_Obj *valuePtr; + + if ((int) Tcl_ObjectContextSkippedArgs(context) != objc) { + Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), + objv, NULL); + return TCL_ERROR; + } + + varPtr = TclOOLookupObjectVar(interp, Tcl_ObjectContextObject(context), + propNamePtr, &aryVar); + if (varPtr == NULL) { + return TCL_ERROR; + } + + valuePtr = TclPtrGetVar(interp, varPtr, aryVar, propNamePtr, NULL, + TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG); + if (valuePtr == NULL) { + return TCL_ERROR; + } + Tcl_SetObjResult(interp, valuePtr); + return TCL_OK; +} + +static int +Configurable_Setter( + void *clientData, /* Which property to write. Actually a Tcl_Obj* + * reference that is the name of the variable + * in the cpntext object. */ + Tcl_Interp *interp, /* Interpreter used for the result, error + * reporting, etc. */ + Tcl_ObjectContext context, /* The object/call context. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* The actual arguments. */ +{ + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_Var varPtr, aryVar; + + if ((int) Tcl_ObjectContextSkippedArgs(context) + 1 != objc) { + Tcl_WrongNumArgs(interp, Tcl_ObjectContextSkippedArgs(context), + objv, "value"); + return TCL_ERROR; + } + + varPtr = TclOOLookupObjectVar(interp, Tcl_ObjectContextObject(context), + propNamePtr, &aryVar); + if (varPtr == NULL) { + return TCL_ERROR; + } + + if (TclPtrSetVar(interp, varPtr, aryVar, propNamePtr, NULL, + objv[objc - 1], TCL_NAMESPACE_ONLY | TCL_LEAVE_ERR_MSG) == NULL) { + return TCL_ERROR; + } + return TCL_OK; +} + +// Simple support functions +static void +DetailsDeleter( + void *clientData) +{ + // Just drop the reference count + Tcl_Obj *propNamePtr = (Tcl_Obj *) clientData; + Tcl_DecrRefCount(propNamePtr); +} + +static int +DetailsCloner( + TCL_UNUSED(Tcl_Interp *), + void *oldClientData, + void **newClientData) +{ + // Just add another reference to this name; easy! + Tcl_Obj *propNamePtr = (Tcl_Obj *) oldClientData; + Tcl_IncrRefCount(propNamePtr); + *newClientData = propNamePtr; + return TCL_OK; +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOImplementObjectProperty, TclOOImplementClassProperty -- + * + * Installs a basic property implementation for a property, either on + * an instance or on a class. It's up to the code that calls these + * to ensure that the property name is syntactically valid. + * + * ---------------------------------------------------------------------- + */ + +void +TclOOImplementObjectProperty( + Tcl_Object targetObject, /* What to install into. */ + Tcl_Obj *propNamePtr, /* Property name, without leading - */ + int installGetter, /* Whether to install a standard getter. */ + int installSetter) /* Whether to install a standard setter. */ +{ + if (installGetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewInstanceMethod( + NULL, targetObject, methodName, 0, &GetterType, propNamePtr); + Tcl_BounceRefCount(methodName); + } + if (installSetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewInstanceMethod( + NULL, targetObject, methodName, 0, &SetterType, propNamePtr); + Tcl_BounceRefCount(methodName); + } +} + +void +TclOOImplementClassProperty( + Tcl_Class targetClass, /* What to install into. */ + Tcl_Obj *propNamePtr, /* Property name, without leading - */ + int installGetter, /* Whether to install a standard getter. */ + int installSetter) /* Whether to install a standard setter. */ +{ + if (installGetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewMethod(targetClass, methodName, 0, &GetterType, propNamePtr); + Tcl_BounceRefCount(methodName); + } + if (installSetter) { + Tcl_Obj *methodName = Tcl_ObjPrintf( + "", TclGetString(propNamePtr)); + Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter + TclNewMethod(targetClass, methodName, 0, &SetterType, propNamePtr); + Tcl_BounceRefCount(methodName); + } +} + +/* + * ---------------------------------------------------------------------- + * + * FindClassProps -- + * + * Discover the properties known to a class and its superclasses. + * The property names become the keys in the accumulator hash table + * (which is used as a set). + * + * ---------------------------------------------------------------------- + */ + +static void +FindClassProps( + Class *clsPtr, /* The object to inspect. Must exist. */ + int writable, /* Whether we're after the readable or writable + * property set. */ + Tcl_HashTable *accumulator) /* Where to gather the names. */ +{ + int i, dummy; + Tcl_Obj *propName; + Class *mixin, *sup; + + tailRecurse: + if (writable) { + FOREACH(propName, clsPtr->properties.writable) { + Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); + } + } else { + FOREACH(propName, clsPtr->properties.readable) { + Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); + } + } + if (clsPtr->thisPtr->flags & ROOT_OBJECT) { + /* + * We do *not* traverse upwards from the root! + */ + return; + } + FOREACH(mixin, clsPtr->mixins) { + FindClassProps(mixin, writable, accumulator); + } + if (clsPtr->superclasses.num == 1) { + clsPtr = clsPtr->superclasses.list[0]; + goto tailRecurse; + } + FOREACH(sup, clsPtr->superclasses) { + FindClassProps(sup, writable, accumulator); + } +} + +/* + * ---------------------------------------------------------------------- + * + * FindObjectProps -- + * + * Discover the properties known to an object and all its classes. + * The property names become the keys in the accumulator hash table + * (which is used as a set). + * + * ---------------------------------------------------------------------- + */ + +static void +FindObjectProps( + Object *oPtr, /* The object to inspect. Must exist. */ + int writable, /* Whether we're after the readable or writable + * property set. */ + Tcl_HashTable *accumulator) /* Where to gather the names. */ +{ + int i, dummy; + Tcl_Obj *propName; + Class *mixin; + + if (writable) { + FOREACH(propName, oPtr->properties.writable) { + Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); + } + } else { + FOREACH(propName, oPtr->properties.readable) { + Tcl_CreateHashEntry(accumulator, (void *) propName, &dummy); + } + } + FOREACH(mixin, oPtr->mixins) { + FindClassProps(mixin, writable, accumulator); + } + FindClassProps(oPtr->selfCls, writable, accumulator); +} + +/* + * ---------------------------------------------------------------------- + * + * GetAllClassProperties -- + * + * Get the list of all properties known to a class, including to its + * superclasses. Manages a cache so this operation is usually cheap. + * The order of properties in the resulting list is undefined. + * + * ---------------------------------------------------------------------- + */ + +static Tcl_Obj * +GetAllClassProperties( + Class *clsPtr, /* The class to inspect. Must exist. */ + int writable, /* Whether to get writable properties. If + * false, readable properties will be returned + * instead. */ + int *allocated) /* Address of variable to set to true if a + * Tcl_Obj was allocated and may be safely + * modified by the caller. */ +{ + Tcl_HashTable hashTable; + FOREACH_HASH_DECLS; + Tcl_Obj *propName, *result; + void *dummy; + + /* + * Look in the cache. + */ + + if (clsPtr->properties.epoch == clsPtr->thisPtr->fPtr->epoch) { + if (writable) { + if (clsPtr->properties.allWritableCache) { + *allocated = 0; + return clsPtr->properties.allWritableCache; + } + } else { + if (clsPtr->properties.allReadableCache) { + *allocated = 0; + return clsPtr->properties.allReadableCache; + } + } + } + + /* + * Gather the information. Unsorted! (Caller will sort.) + */ + + *allocated = 1; + Tcl_InitObjHashTable(&hashTable); + FindClassProps(clsPtr, writable, &hashTable); + TclNewObj(result); + FOREACH_HASH(propName, dummy, &hashTable) { + Tcl_ListObjAppendElement(NULL, result, propName); + } + Tcl_DeleteHashTable(&hashTable); + + /* + * Cache the information. Also purges the cache. + */ + + if (clsPtr->properties.epoch != clsPtr->thisPtr->fPtr->epoch) { + if (clsPtr->properties.allWritableCache) { + Tcl_DecrRefCount(clsPtr->properties.allWritableCache); + clsPtr->properties.allWritableCache = NULL; + } + if (clsPtr->properties.allReadableCache) { + Tcl_DecrRefCount(clsPtr->properties.allReadableCache); + clsPtr->properties.allReadableCache = NULL; + } + } + clsPtr->properties.epoch = clsPtr->thisPtr->fPtr->epoch; + if (writable) { + clsPtr->properties.allWritableCache = result; + } else { + clsPtr->properties.allReadableCache = result; + } + Tcl_IncrRefCount(result); + return result; +} + +/* + * ---------------------------------------------------------------------- + * + * SortPropList -- + * Sort a list of names of properties. Simple support function. Assumes + * that the list Tcl_Obj is unshared and doesn't have a string + * representation. + * + * ---------------------------------------------------------------------- + */ + +static int +PropNameCompare( + const void *a, + const void *b) +{ + Tcl_Obj *first = *(Tcl_Obj **) a; + Tcl_Obj *second = *(Tcl_Obj **) b; + + return strcmp(TclGetString(first), TclGetString(second)); +} + +static inline void +SortPropList( + Tcl_Obj *list) +{ + Tcl_Size ec; + Tcl_Obj **ev; + + Tcl_ListObjGetElements(NULL, list, &ec, &ev); + qsort(ev, ec, sizeof(Tcl_Obj *), PropNameCompare); +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOGetAllObjectProperties -- + * + * Get the sorted list of all properties known to a object, including to its + * its classes. Manages a cache so this operation is usually cheap. + * + * ---------------------------------------------------------------------- + */ + +Tcl_Obj * +TclOOGetAllObjectProperties( + Object *oPtr, /* The object to inspect. Must exist. */ + int writable) /* Whether to get writable properties. If + * false, readable properties will be returned + * instead. */ +{ + Tcl_HashTable hashTable; + FOREACH_HASH_DECLS; + Tcl_Obj *propName, *result; + void *dummy; + + /* + * Look in the cache. + */ + + if (oPtr->properties.epoch == oPtr->fPtr->epoch) { + if (writable) { + if (oPtr->properties.allWritableCache) { + return oPtr->properties.allWritableCache; + } + } else { + if (oPtr->properties.allReadableCache) { + return oPtr->properties.allReadableCache; + } + } + } + + /* + * Gather the information. Unsorted! (Caller will sort.) + */ + + Tcl_InitObjHashTable(&hashTable); + FindObjectProps(oPtr, writable, &hashTable); + TclNewObj(result); + FOREACH_HASH(propName, dummy, &hashTable) { + Tcl_ListObjAppendElement(NULL, result, propName); + } + Tcl_DeleteHashTable(&hashTable); + SortPropList(result); + + /* + * Cache the information. + */ + + if (oPtr->properties.epoch != oPtr->fPtr->epoch) { + if (oPtr->properties.allWritableCache) { + Tcl_DecrRefCount(oPtr->properties.allWritableCache); + oPtr->properties.allWritableCache = NULL; + } + if (oPtr->properties.allReadableCache) { + Tcl_DecrRefCount(oPtr->properties.allReadableCache); + oPtr->properties.allReadableCache = NULL; + } + } + oPtr->properties.epoch = oPtr->fPtr->epoch; + if (writable) { + oPtr->properties.allWritableCache = result; + } else { + oPtr->properties.allReadableCache = result; + } + Tcl_IncrRefCount(result); + return result; +} + +/* + * ---------------------------------------------------------------------- + * + * SetPropertyList -- + * + * Helper for writing a property list (which is actually a set). + * + * ---------------------------------------------------------------------- + */ +static inline void +SetPropertyList( + PropertyList *propList, /* The property list to write. Replaces the + * property list's contents. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ +{ + Tcl_Size i, n; + Tcl_Obj *propObj; + int created; + Tcl_HashTable uniqueTable; + + for (i=0 ; ilist); + } else if (i) { + propList->list = (Tcl_Obj **) + Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * objc); + } else { + propList->list = (Tcl_Obj **) + Tcl_Alloc(sizeof(Tcl_Obj *) * objc); + } + } + propList->num = 0; + if (objc > 0) { + Tcl_InitObjHashTable(&uniqueTable); + for (i=n=0 ; ilist[n++] = objv[i]; + } else { + Tcl_DecrRefCount(objv[i]); + } + } + propList->num = n; + + /* + * Shouldn't be necessary, but maintain num/list invariant. + */ + + if (n != objc) { + propList->list = (Tcl_Obj **) + Tcl_Realloc(propList->list, sizeof(Tcl_Obj *) * n); + } + Tcl_DeleteHashTable(&uniqueTable); + } +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOInstallReadableProps -- + * + * Helper for writing the readable property list (which is actually a set) + * that includes flushing the name cache. + * + * ---------------------------------------------------------------------- + */ +void +TclOOInstallReadableProps( + PropertyStorage *props, /* Which property list to install into. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ +{ + if (props->allReadableCache) { + Tcl_DecrRefCount(props->allReadableCache); + props->allReadableCache = NULL; + } + + SetPropertyList(&props->readable, objc, objv); +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOInstallWritableProps -- + * + * Helper for writing the writable property list (which is actually a set) + * that includes flushing the name cache. + * + * ---------------------------------------------------------------------- + */ +void +TclOOInstallWritableProps( + PropertyStorage *props, /* Which property list to install into. */ + Tcl_Size objc, /* Number of property names. */ + Tcl_Obj *const objv[]) /* Property names. */ +{ + if (props->allWritableCache) { + Tcl_DecrRefCount(props->allWritableCache); + props->allWritableCache = NULL; + } + + SetPropertyList(&props->writable, objc, objv); +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOGetPropertyList -- + * + * Helper for reading a property list. + * + * ---------------------------------------------------------------------- + */ +Tcl_Obj * +TclOOGetPropertyList( + PropertyList *propList) /* The property list to read. */ +{ + Tcl_Obj *resultObj, *propNameObj; + Tcl_Size i; + + TclNewObj(resultObj); + FOREACH(propNameObj, *propList) { + Tcl_ListObjAppendElement(NULL, resultObj, propNameObj); + } + return resultObj; +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOInstallStdPropertyImpls -- + * + * Validates a (dashless) property name, and installs implementation + * methods if asked to do so (readable and writable flags). + * + * ---------------------------------------------------------------------- + */ +int +TclOOInstallStdPropertyImpls( + void *useInstance, + Tcl_Interp *interp, + Tcl_Obj *propName, + int readable, + int writable) +{ + const char *name, *reason; + Tcl_Size len; + char flag = TCL_DONT_QUOTE_HASH; + + /* + * Validate the property name. Note that just calling TclScanElement() is + * cheaper than actually formatting a list and comparing the string + * version of that with the original, as TclScanElement() is one of the + * core parts of doing that; this skips a whole load of irrelevant memory + * allocations! + */ + + name = Tcl_GetStringFromObj(propName, &len); + if (Tcl_StringMatch(name, "-*")) { + reason = "must not begin with -"; + goto badProp; + } + if (TclScanElement(name, len, &flag) != len) { + reason = "must be a simple word"; + goto badProp; + } + if (Tcl_StringMatch(name, "*::*")) { + reason = "must not contain namespace separators"; + goto badProp; + } + if (Tcl_StringMatch(name, "*[()]*")) { + reason = "must not contain parentheses"; + goto badProp; + } + + /* + * Install the implementations... if asked to do so. + */ + + if (useInstance) { + Tcl_Object object = TclOOGetDefineCmdContext(interp); + if (!object) { + return TCL_ERROR; + } + TclOOImplementObjectProperty(object, propName, readable, writable); + } else { + Tcl_Class cls = (Tcl_Class) TclOOGetClassDefineCmdContext(interp); + if (!cls) { + return TCL_ERROR; + } + TclOOImplementClassProperty(cls, propName, readable, writable); + } + return TCL_OK; + + badProp: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "bad property name \"%s\": %s", name, reason)); + Tcl_SetErrorCode(interp, "TCL", "OO", "PROPERTY_FORMAT", NULL); + return TCL_ERROR; +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOInfoClassPropCmd, TclOOInfoObjectPropCmd -- + * + * Implements [info class properties $clsName ?$option...?] and + * [info object properties $objName ?$option...?] + * + * ---------------------------------------------------------------------- + */ + +int +TclOOInfoClassPropCmd( + TCL_UNUSED(void *), + Tcl_Interp *interp, + int objc, + Tcl_Obj *const objv[]) +{ + Class *clsPtr; + int i, idx, all = 0, writable = 0, allocated = 0; + Tcl_Obj *result; + + if (objc < 2) { + Tcl_WrongNumArgs(interp, 1, objv, "className ?options...?"); + return TCL_ERROR; + } + clsPtr = TclOOGetClassFromObj(interp, objv[1]); + if (clsPtr == NULL) { + return TCL_ERROR; + } + for (i = 2; i < objc; i++) { + if (Tcl_GetIndexFromObj(interp, objv[i], propOptNames, "option", 0, + &idx) != TCL_OK) { + return TCL_ERROR; + } + switch (idx) { + case PROP_ALL: + all = 1; + break; + case PROP_READABLE: + writable = 0; + break; + case PROP_WRITABLE: + writable = 1; + break; + } + } + + /* + * Get the properties. + */ + + if (all) { + result = GetAllClassProperties(clsPtr, writable, &allocated); + if (allocated) { + SortPropList(result); + } + } else { + if (writable) { + result = TclOOGetPropertyList(&clsPtr->properties.writable); + } else { + result = TclOOGetPropertyList(&clsPtr->properties.readable); + } + SortPropList(result); + } + Tcl_SetObjResult(interp, result); + return TCL_OK; +} + +int +TclOOInfoObjectPropCmd( + TCL_UNUSED(void *), + Tcl_Interp *interp, + int objc, + Tcl_Obj *const objv[]) +{ + Object *oPtr; + int i, idx, all = 0, writable = 0; + Tcl_Obj *result; + + if (objc < 2) { + Tcl_WrongNumArgs(interp, 1, objv, "objName ?options...?"); + return TCL_ERROR; + } + oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[1]); + if (oPtr == NULL) { + return TCL_ERROR; + } + for (i = 2; i < objc; i++) { + if (Tcl_GetIndexFromObj(interp, objv[i], propOptNames, "option", 0, + &idx) != TCL_OK) { + return TCL_ERROR; + } + switch (idx) { + case PROP_ALL: + all = 1; + break; + case PROP_READABLE: + writable = 0; + break; + case PROP_WRITABLE: + writable = 1; + break; + } + } + + /* + * Get the properties. + */ + + if (all) { + result = TclOOGetAllObjectProperties(oPtr, writable); + } else { + if (writable) { + result = TclOOGetPropertyList(&oPtr->properties.writable); + } else { + result = TclOOGetPropertyList(&oPtr->properties.readable); + } + SortPropList(result); + } + Tcl_SetObjResult(interp, result); + return TCL_OK; +} + +/* + * ---------------------------------------------------------------------- + * + * TclOOReleasePropertyStorage -- + * + * Delete the memory associated with a class or object's properties. + * + * ---------------------------------------------------------------------- + */ + +static inline void +ReleasePropertyList( + PropertyList *propList) +{ + Tcl_Obj *propertyObj; + Tcl_Size i; + + FOREACH(propertyObj, *propList) { + Tcl_DecrRefCount(propertyObj); + } + Tcl_Free(propList->list); + propList->list = NULL; + propList->num = 0; +} + +void +TclOOReleasePropertyStorage( + PropertyStorage *propsPtr) +{ + if (propsPtr->allReadableCache) { + Tcl_DecrRefCount(propsPtr->allReadableCache); + } + if (propsPtr->allWritableCache) { + Tcl_DecrRefCount(propsPtr->allWritableCache); + } + if (propsPtr->readable.num) { + ReleasePropertyList(&propsPtr->readable); + } + if (propsPtr->writable.num) { + ReleasePropertyList(&propsPtr->writable); + } +} + +/* + * Local Variables: + * mode: c + * c-basic-offset: 4 + * fill-column: 78 + * End: + */ diff --git a/unix/Makefile.in b/unix/Makefile.in index 6e20947..0c5462e 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -321,7 +321,7 @@ GENERIC_OBJS = regcomp.o regexec.o regfree.o regerror.o tclAlloc.o \ tclTomMathInterface.o tclZipfs.o OO_OBJS = tclOO.o tclOOBasic.o tclOOCall.o tclOODefineCmds.o tclOOInfo.o \ - tclOOMethod.o tclOOStubInit.o + tclOOMethod.o tclOOProp.o tclOOStubInit.o TOMMATH_OBJS = bn_s_mp_reverse.o bn_s_mp_mul_digs_fast.o \ bn_s_mp_sqr_fast.o bn_mp_add.o bn_mp_and.o \ @@ -494,6 +494,7 @@ OO_SRCS = \ $(GENERIC_DIR)/tclOODefineCmds.c \ $(GENERIC_DIR)/tclOOInfo.c \ $(GENERIC_DIR)/tclOOMethod.c \ + $(GENERIC_DIR)/tclOOProp.c \ $(GENERIC_DIR)/tclOOStubInit.c STUB_SRCS = \ @@ -1478,6 +1479,9 @@ tclOOInfo.o: $(GENERIC_DIR)/tclOOInfo.c tclOOMethod.o: $(GENERIC_DIR)/tclOOMethod.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclOOMethod.c +tclOOProp.o: $(GENERIC_DIR)/tclOOProp.c + $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclOOProp.c + tclOOStubInit.o: $(GENERIC_DIR)/tclOOStubInit.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclOOStubInit.c diff --git a/win/Makefile.in b/win/Makefile.in index 9df1a95..b3e5ace 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -342,6 +342,7 @@ GENERIC_OBJS = \ tclOODefineCmds.$(OBJEXT) \ tclOOInfo.$(OBJEXT) \ tclOOMethod.$(OBJEXT) \ + tclOOProp.$(OBJEXT) \ tclOOStubInit.$(OBJEXT) \ tclObj.$(OBJEXT) \ tclOptimize.$(OBJEXT) \ diff --git a/win/makefile.vc b/win/makefile.vc index 82fcc74..cba51ee 100644 --- a/win/makefile.vc +++ b/win/makefile.vc @@ -301,6 +301,7 @@ COREOBJS = \ $(TMP_DIR)\tclOODefineCmds.obj \ $(TMP_DIR)\tclOOInfo.obj \ $(TMP_DIR)\tclOOMethod.obj \ + $(TMP_DIR)\tclOOProp.obj \ $(TMP_DIR)\tclOOStubInit.obj \ $(TMP_DIR)\tclObj.obj \ $(TMP_DIR)\tclOptimize.obj \ -- cgit v0.12 From 41fc7d8d1a34b569804152fdf02957a97ebb62ff Mon Sep 17 00:00:00 2001 From: dkf Date: Mon, 5 Aug 2024 14:09:59 +0000 Subject: More cleaning up. --- generic/tclOO.c | 4 +- generic/tclOOBasic.c | 8 +- generic/tclOODefineCmds.c | 181 ++-------------------------------- generic/tclOOInfo.c | 38 +++++--- generic/tclOOInt.h | 22 ++--- generic/tclOOProp.c | 244 ++++++++++++++++++++++++++++++++++++++++------ tests/compile.test | 17 ++-- 7 files changed, 270 insertions(+), 244 deletions(-) diff --git a/generic/tclOO.c b/generic/tclOO.c index 7384232..b314df7 100644 --- a/generic/tclOO.c +++ b/generic/tclOO.c @@ -468,10 +468,10 @@ InitFoundation( Tcl_CreateObjCommand(interp, "::oo::configuresupport::configurableobject::property", - TclOOPropertyDefinitionCmd, (void *) 1, NULL); + TclOODefinePropertyCmd, (void *) 1, NULL); Tcl_CreateObjCommand(interp, "::oo::configuresupport::configurableclass::property", - TclOOPropertyDefinitionCmd, (void *) 0, NULL); + TclOODefinePropertyCmd, (void *) 0, NULL); /* * Evaluate the remaining definitions, which are a compiled-in Tcl script. diff --git a/generic/tclOOBasic.c b/generic/tclOOBasic.c index c9a5c7a..1506a34 100644 --- a/generic/tclOOBasic.c +++ b/generic/tclOOBasic.c @@ -772,8 +772,8 @@ TclOOLookupObjectVar( if (mPtr->declaringObjectPtr == oPtr) { FOREACH_STRUCT(pvPtr, oPtr->privateVariables) { - if (!strcmp(TclGetString(pvPtr->variableObj), - TclGetString(varName))) { + if (!TclStringCmp(pvPtr->variableObj, varName, 1, 0, + TCL_INDEX_NONE)) { varName = pvPtr->fullNameObj; break; } @@ -794,8 +794,8 @@ TclOOLookupObjectVar( } if (isInstance) { FOREACH_STRUCT(pvPtr, clsPtr->privateVariables) { - if (!strcmp(TclGetString(pvPtr->variableObj), - TclGetString(varName))) { + if (!TclStringCmp(pvPtr->variableObj, varName, 1, 0, + TCL_INDEX_NONE)) { varName = pvPtr->fullNameObj; break; } diff --git a/generic/tclOODefineCmds.c b/generic/tclOODefineCmds.c index ceb65ef..70f0381 100644 --- a/generic/tclOODefineCmds.c +++ b/generic/tclOODefineCmds.c @@ -3212,7 +3212,7 @@ Configurable_ClassReadableProps_Set( return TCL_ERROR; } - TclOOInstallReadableProps(&clsPtr->properties, varc, varv); + TclOOInstallReadableProperties(&clsPtr->properties, varc, varv); BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3265,7 +3265,7 @@ Configurable_ObjectReadableProps_Set( return TCL_ERROR; } - TclOOInstallReadableProps(&oPtr->properties, varc, varv); + TclOOInstallReadableProperties(&oPtr->properties, varc, varv); return TCL_OK; } @@ -3328,7 +3328,7 @@ Configurable_ClassWritableProps_Set( return TCL_ERROR; } - TclOOInstallWritableProps(&clsPtr->properties, varc, varv); + TclOOInstallWritableProperties(&clsPtr->properties, varc, varv); BumpGlobalEpoch(interp, clsPtr); return TCL_OK; } @@ -3381,7 +3381,7 @@ Configurable_ObjectWritableProps_Set( return TCL_ERROR; } - TclOOInstallWritableProps(&oPtr->properties, varc, varv); + TclOOInstallWritableProperties(&oPtr->properties, varc, varv); return TCL_OK; } @@ -3413,7 +3413,7 @@ BuildPropertyList( Tcl_SetListObj(listObj, 0, NULL); FOREACH(other, *propsList) { - if (strcmp(TclGetString(propName), TclGetString(other)) == 0) { + if (!TclStringCmp(propName, other, 1, 0, TCL_INDEX_NONE)) { present = 1; if (!addingProp) { changed = 1; @@ -3449,13 +3449,13 @@ TclOORegisterInstanceProperty( if (BuildPropertyList(&oPtr->properties.readable, propName, registerReader, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - TclOOInstallReadableProps(&oPtr->properties, count, objv); + TclOOInstallReadableProperties(&oPtr->properties, count, objv); } if (BuildPropertyList(&oPtr->properties.writable, propName, registerWriter, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - TclOOInstallWritableProps(&oPtr->properties, count, objv); + TclOOInstallWritableProperties(&oPtr->properties, count, objv); } Tcl_BounceRefCount(listObj); } @@ -3481,14 +3481,14 @@ TclOORegisterProperty( if (BuildPropertyList(&clsPtr->properties.readable, propName, registerReader, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - TclOOInstallReadableProps(&clsPtr->properties, count, objv); + TclOOInstallReadableProperties(&clsPtr->properties, count, objv); changed = 1; } if (BuildPropertyList(&clsPtr->properties.writable, propName, registerWriter, listObj)) { TclListObjGetElements(NULL, listObj, &count, &objv); - TclOOInstallWritableProps(&clsPtr->properties, count, objv); + TclOOInstallWritableProperties(&clsPtr->properties, count, objv); changed = 1; } Tcl_BounceRefCount(listObj); @@ -3498,169 +3498,6 @@ TclOORegisterProperty( } /* - * ---------------------------------------------------------------------- - * - * TclOOPropertyDefinitionCmd -- - * - * Implementation of the "property" definition for classes and instances - * governed by the [oo::configurable] metaclass. - * - * ---------------------------------------------------------------------- - */ - -int -TclOOPropertyDefinitionCmd( - void *useInstance, /* NULL for class, non-NULL for object. */ - Tcl_Interp *interp, /* For error reporting and lookup. */ - int objc, /* Number of arguments. */ - Tcl_Obj *const *objv) /* Arguments. */ -{ - int i; - const char *const options[] = { - "-get", "-kind", "-set", NULL - }; - enum Options { - OPT_GET, OPT_KIND, OPT_SET - }; - const char *const kinds[] = { - "readable", "readwrite", "writable", NULL - }; - enum Kinds { - KIND_RO, KIND_RW, KIND_WO - }; - Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); - - if (oPtr == NULL) { - return TCL_ERROR; - } - if (!useInstance && !oPtr->classPtr) { - Tcl_SetObjResult(interp, Tcl_NewStringObj( - "attempt to misuse API", -1)); - Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL); - return TCL_ERROR; - } - - for (i = 1; i < objc; i++) { - Tcl_Obj *propObj = objv[i], *nextObj, *argObj, *hyphenated; - Tcl_Obj *getterScript = NULL, *setterScript = NULL; - - /* - * Parse the extra options for the property. - */ - - int kind = KIND_RW; - while (i + 1 < objc) { - int option; - - nextObj = objv[i + 1]; - if (TclGetString(nextObj)[0] != '-') { - break; - } - if (Tcl_GetIndexFromObj(interp, nextObj, options, "option", 0, - &option) != TCL_OK) { - return TCL_ERROR; - } - if (i + 2 >= objc) { - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "missing %s to go with %s option", - (option == OPT_KIND ? "kind value" : "body"), - options[option])); - Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", NULL); - return TCL_ERROR; - } - argObj = objv[i + 2]; - i += 2; - switch (option) { - case OPT_GET: - getterScript = argObj; - break; - case OPT_SET: - setterScript = argObj; - break; - case OPT_KIND: - if (Tcl_GetIndexFromObj(interp, argObj, kinds, "kind", 0, - &kind) != TCL_OK) { - return TCL_ERROR; - } - break; - } - } - - /* - * Install the property. Note that TclOOInstallStdPropertyImpls - * validates the property name as well. - */ - - if (TclOOInstallStdPropertyImpls(useInstance, interp, propObj, - kind != KIND_WO && getterScript == NULL, - kind != KIND_RO && setterScript == NULL) != TCL_OK) { - return TCL_ERROR; - } - - hyphenated = Tcl_ObjPrintf("-%s", TclGetString(propObj)); - if (useInstance) { - TclOORegisterInstanceProperty(oPtr, hyphenated, - kind != KIND_WO, kind != KIND_RO); - } else { - TclOORegisterProperty(oPtr->classPtr, hyphenated, - kind != KIND_WO, kind != KIND_RO); - } - Tcl_BounceRefCount(hyphenated); - - /* - * Create property implementation methods by using the right - * back-end API, but only if the user has given us the bodies of the - * methods we'll make. - */ - - if (getterScript != NULL) { - Tcl_Obj *getterName = Tcl_ObjPrintf("", - TclGetString(propObj)); - Tcl_Obj *argsPtr = Tcl_NewObj(); - Method *mPtr; - - Tcl_IncrRefCount(getterScript); - if (useInstance) { - mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, - getterName, argsPtr, getterScript, NULL); - } else { - mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, - getterName, argsPtr, getterScript, NULL); - } - Tcl_BounceRefCount(getterName); - Tcl_BounceRefCount(argsPtr); - Tcl_DecrRefCount(getterScript); - if (mPtr == NULL) { - return TCL_ERROR; - } - } - if (setterScript != NULL) { - Tcl_Obj *setterName = Tcl_ObjPrintf("", - TclGetString(propObj)); - Tcl_Obj *argsPtr; - Method *mPtr; - - TclNewLiteralStringObj(argsPtr, "value"); - Tcl_IncrRefCount(setterScript); - if (useInstance) { - mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, - setterName, argsPtr, setterScript, NULL); - } else { - mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, - setterName, argsPtr, setterScript, NULL); - } - Tcl_BounceRefCount(setterName); - Tcl_BounceRefCount(argsPtr); - Tcl_DecrRefCount(setterScript); - if (mPtr == NULL) { - return TCL_ERROR; - } - } - } - return TCL_OK; -} - -/* * Local Variables: * mode: c * c-basic-offset: 4 diff --git a/generic/tclOOInfo.c b/generic/tclOOInfo.c index 26241b2..914ed38 100644 --- a/generic/tclOOInfo.c +++ b/generic/tclOOInfo.c @@ -724,12 +724,7 @@ InfoObjectMethodTypeCmd( } hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, objv[2]); if (hPtr == NULL) { - unknownMethod: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "unknown method \"%s\"", TclGetString(objv[2]))); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto unknownMethod; } mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr == NULL) { @@ -743,6 +738,13 @@ InfoObjectMethodTypeCmd( Tcl_SetObjResult(interp, Tcl_NewStringObj(mPtr->typePtr->name, -1)); return TCL_OK; + + unknownMethod: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "unknown method \"%s\"", TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; } /* @@ -881,6 +883,10 @@ InfoObjectVariablesCmd( } if (objc == 3) { if (strcmp("-private", TclGetString(objv[2])) != 0) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "option \"%s\" is not exactly \"-private\"", + TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_ARG"); return TCL_ERROR; } isPrivate = 1; @@ -1488,12 +1494,7 @@ InfoClassMethodTypeCmd( hPtr = Tcl_FindHashEntry(&clsPtr->classMethods, objv[2]); if (hPtr == NULL) { - unknownMethod: - Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "unknown method \"%s\"", TclGetString(objv[2]))); - Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", - TclGetString(objv[2]), (char *)NULL); - return TCL_ERROR; + goto unknownMethod; } mPtr = (Method *) Tcl_GetHashValue(hPtr); if (mPtr->typePtr == NULL) { @@ -1506,6 +1507,13 @@ InfoClassMethodTypeCmd( } Tcl_SetObjResult(interp, Tcl_NewStringObj(mPtr->typePtr->name, -1)); return TCL_OK; + + unknownMethod: + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "unknown method \"%s\"", TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD", + TclGetString(objv[2]), (char *)NULL); + return TCL_ERROR; } /* @@ -1672,6 +1680,10 @@ InfoClassVariablesCmd( } if (objc == 3) { if (strcmp("-private", TclGetString(objv[2])) != 0) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "option \"%s\" is not exactly \"-private\"", + TclGetString(objv[2]))); + Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_ARG"); return TCL_ERROR; } isPrivate = 1; @@ -1737,6 +1749,7 @@ InfoObjectCallCmd( if (contextPtr == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "cannot construct any call chain", -1)); + Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_CALL_CHAIN"); return TCL_ERROR; } Tcl_SetObjResult(interp, @@ -1782,6 +1795,7 @@ InfoClassCallCmd( if (callPtr == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "cannot construct any call chain", -1)); + Tcl_SetErrorCode(interp, "TCL", "OO", "BAD_CALL_CHAIN"); return TCL_ERROR; } Tcl_SetObjResult(interp, TclOORenderCallChain(interp, callPtr)); diff --git a/generic/tclOOInt.h b/generic/tclOOInt.h index 88ce0e7..3ef395c 100644 --- a/generic/tclOOInt.h +++ b/generic/tclOOInt.h @@ -509,7 +509,7 @@ MODULE_SCOPE Tcl_ObjCmdProc TclOODefineClassObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefineObjSelfObjCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOODefinePrivateObjCmd; -MODULE_SCOPE Tcl_ObjCmdProc TclOOPropertyDefinitionCmd; +MODULE_SCOPE Tcl_ObjCmdProc TclOODefinePropertyCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOOUnknownDefinition; MODULE_SCOPE Tcl_ObjCmdProc TclOOCopyObjectCmd; MODULE_SCOPE Tcl_ObjCmdProc TclOONextObjCmd; @@ -572,8 +572,6 @@ MODULE_SCOPE void TclOODeleteContext(CallContext *contextPtr); MODULE_SCOPE void TclOODeleteDescendants(Tcl_Interp *interp, Object *oPtr); MODULE_SCOPE void TclOODelMethodRef(Method *method); -MODULE_SCOPE Tcl_Obj * TclOOGetAllObjectProperties(Object *oPtr, - int writable); MODULE_SCOPE CallContext *TclOOGetCallContext(Object *oPtr, Tcl_Obj *methodNameObj, int flags, Object *contextObjPtr, Class *contextClsPtr, @@ -594,12 +592,6 @@ MODULE_SCOPE size_t TclOOGetSortedClassMethodList(Class *clsPtr, MODULE_SCOPE int TclOOGetSortedMethodList(Object *oPtr, Object *contextObj, Class *contextCls, int flags, const char ***stringsPtr); -MODULE_SCOPE void TclOOImplementObjectProperty(Tcl_Object targetObject, - Tcl_Obj *propNamePtr, int installGetter, - int installSetter); -MODULE_SCOPE void TclOOImplementClassProperty(Tcl_Class targetObject, - Tcl_Obj *propNamePtr, int installGetter, - int installSetter); MODULE_SCOPE int TclOOInit(Tcl_Interp *interp); MODULE_SCOPE void TclOOInitInfo(Tcl_Interp *interp); MODULE_SCOPE int TclOOInvokeContext(void *clientData, @@ -624,18 +616,20 @@ MODULE_SCOPE int TclOORemoveFromSubclasses(Class *subPtr, Class *superPtr); MODULE_SCOPE Tcl_Obj * TclOORenderCallChain(Tcl_Interp *interp, CallChain *callPtr); +MODULE_SCOPE void TclOOStashContext(Tcl_Obj *objPtr, + CallContext *contextPtr); +MODULE_SCOPE Tcl_Obj * TclOOGetAllObjectProperties(Object *oPtr, + int writable); +MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); MODULE_SCOPE Tcl_Obj * TclOOGetPropertyList(PropertyList *propList); MODULE_SCOPE void TclOOReleasePropertyStorage(PropertyStorage *propsPtr); -MODULE_SCOPE void TclOOInstallReadableProps(PropertyStorage *props, +MODULE_SCOPE void TclOOInstallReadableProperties(PropertyStorage *props, Tcl_Size objc, Tcl_Obj *const objv[]); -MODULE_SCOPE void TclOOInstallWritableProps(PropertyStorage *props, +MODULE_SCOPE void TclOOInstallWritableProperties(PropertyStorage *props, Tcl_Size objc, Tcl_Obj *const objv[]); MODULE_SCOPE int TclOOInstallStdPropertyImpls(void *useInstance, Tcl_Interp *interp, Tcl_Obj *propName, int readable, int writable); -MODULE_SCOPE void TclOOStashContext(Tcl_Obj *objPtr, - CallContext *contextPtr); -MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); MODULE_SCOPE void TclOORegisterProperty(Class *clsPtr, Tcl_Obj *propName, int mayRead, int mayWrite); MODULE_SCOPE void TclOORegisterInstanceProperty(Object *oPtr, diff --git a/generic/tclOOProp.c b/generic/tclOOProp.c index ef66f4e..4cff300 100644 --- a/generic/tclOOProp.c +++ b/generic/tclOOProp.c @@ -48,6 +48,12 @@ static int Configurable_Setter(void *clientData, static void DetailsDeleter(void *clientData); static int DetailsCloner(Tcl_Interp *, void *oldClientData, void **newClientData); +static void ImplementObjectProperty(Tcl_Object targetObject, + Tcl_Obj *propNamePtr, int installGetter, + int installSetter); +static void ImplementClassProperty(Tcl_Class targetObject, + Tcl_Obj *propNamePtr, int installGetter, + int installSetter); /* * Method descriptors @@ -88,11 +94,11 @@ static inline int ReadProperty( Tcl_Interp *interp, Object *oPtr, - Tcl_Obj *propObj) + const char *propName) { Tcl_Obj *args[] = { oPtr->fPtr->myName, - Tcl_ObjPrintf("", TclGetString(propObj)) + Tcl_ObjPrintf("", propName) }; int code; @@ -104,11 +110,11 @@ ReadProperty( switch (code) { case TCL_BREAK: Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property getter for %s did a break", TclGetString(propObj))); + "property getter for %s did a break", propName)); return TCL_ERROR; case TCL_CONTINUE: Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property getter for %s did a continue", TclGetString(propObj))); + "property getter for %s did a continue", propName)); return TCL_ERROR; default: return code; @@ -119,12 +125,12 @@ static inline int WriteProperty( Tcl_Interp *interp, Object *oPtr, - Tcl_Obj *propObj, + const char *propName, Tcl_Obj *valueObj) { Tcl_Obj *args[] = { oPtr->fPtr->myName, - Tcl_ObjPrintf("", TclGetString(propObj)), + Tcl_ObjPrintf("", propName), valueObj }; int code; @@ -139,11 +145,11 @@ WriteProperty( switch (code) { case TCL_BREAK: Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property setter for %s did a break", TclGetString(propObj))); + "property setter for %s did a break", propName)); return TCL_ERROR; case TCL_CONTINUE: Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "property setter for %s did a continue", TclGetString(propObj))); + "property setter for %s did a continue", propName)); return TCL_ERROR; default: return code; @@ -279,7 +285,7 @@ TclOO_Configurable_Configure( ListObjGetElements(listPtr, namec, namev); for (i = 0; i < namec; ) { - code = ReadProperty(interp, oPtr, namev[i]); + code = ReadProperty(interp, oPtr, TclGetString(namev[i])); if (code != TCL_OK) { Tcl_DecrRefCount(resultPtr); break; @@ -303,7 +309,7 @@ TclOO_Configurable_Configure( if (namePtr == NULL) { return TCL_ERROR; } - return ReadProperty(interp, oPtr, namePtr); + return ReadProperty(interp, oPtr, TclGetString(namePtr)); } else if (objc == 2) { /* * Special case for writing to one property. Saves fiddling with the @@ -314,7 +320,7 @@ TclOO_Configurable_Configure( if (namePtr == NULL) { return TCL_ERROR; } - code = WriteProperty(interp, oPtr, namePtr, objv[1]); + code = WriteProperty(interp, oPtr, TclGetString(namePtr), objv[1]); if (code == TCL_OK) { Tcl_ResetResult(interp); } @@ -334,7 +340,8 @@ TclOO_Configurable_Configure( code = TCL_ERROR; break; } - code = WriteProperty(interp, oPtr, namePtr, objv[i + 1]); + code = WriteProperty(interp, oPtr, TclGetString(namePtr), + objv[i + 1]); if (code != TCL_OK) { break; } @@ -453,7 +460,7 @@ DetailsCloner( /* * ---------------------------------------------------------------------- * - * TclOOImplementObjectProperty, TclOOImplementClassProperty -- + * ImplementObjectProperty, ImplementClassProperty -- * * Installs a basic property implementation for a property, either on * an instance or on a class. It's up to the code that calls these @@ -463,23 +470,26 @@ DetailsCloner( */ void -TclOOImplementObjectProperty( +ImplementObjectProperty( Tcl_Object targetObject, /* What to install into. */ - Tcl_Obj *propNamePtr, /* Property name, without leading - */ + Tcl_Obj *propNamePtr, /* Property name. */ int installGetter, /* Whether to install a standard getter. */ int installSetter) /* Whether to install a standard setter. */ { + const char *propName = TclGetString(propNamePtr); + + while (propName[0] == '-') { + propName++; + } if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", propName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewInstanceMethod( NULL, targetObject, methodName, 0, &GetterType, propNamePtr); Tcl_BounceRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", propName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewInstanceMethod( NULL, targetObject, methodName, 0, &SetterType, propNamePtr); @@ -488,22 +498,25 @@ TclOOImplementObjectProperty( } void -TclOOImplementClassProperty( +ImplementClassProperty( Tcl_Class targetClass, /* What to install into. */ - Tcl_Obj *propNamePtr, /* Property name, without leading - */ + Tcl_Obj *propNamePtr, /* Property name. */ int installGetter, /* Whether to install a standard getter. */ int installSetter) /* Whether to install a standard setter. */ { + const char *propName = TclGetString(propNamePtr); + + while (propName[0] == '-') { + propName++; + } if (installGetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", propName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewMethod(targetClass, methodName, 0, &GetterType, propNamePtr); Tcl_BounceRefCount(methodName); } if (installSetter) { - Tcl_Obj *methodName = Tcl_ObjPrintf( - "", TclGetString(propNamePtr)); + Tcl_Obj *methodName = Tcl_ObjPrintf("", propName); Tcl_IncrRefCount(propNamePtr); // Paired with DetailsDeleter TclNewMethod(targetClass, methodName, 0, &SetterType, propNamePtr); Tcl_BounceRefCount(methodName); @@ -700,7 +713,7 @@ PropNameCompare( Tcl_Obj *first = *(Tcl_Obj **) a; Tcl_Obj *second = *(Tcl_Obj **) b; - return strcmp(TclGetString(first), TclGetString(second)); + return TclStringCmp(first, second, 0, 0, TCL_INDEX_NONE); } static inline void @@ -710,7 +723,11 @@ SortPropList( Tcl_Size ec; Tcl_Obj **ev; + if (Tcl_IsShared(list)) { + Tcl_Panic("shared property list cannot be sorted"); + } Tcl_ListObjGetElements(NULL, list, &ec, &ev); + TclInvalidateStringRep(list); qsort(ev, ec, sizeof(Tcl_Obj *), PropNameCompare); } @@ -856,7 +873,7 @@ SetPropertyList( /* * ---------------------------------------------------------------------- * - * TclOOInstallReadableProps -- + * TclOOInstallReadableProperties -- * * Helper for writing the readable property list (which is actually a set) * that includes flushing the name cache. @@ -864,7 +881,7 @@ SetPropertyList( * ---------------------------------------------------------------------- */ void -TclOOInstallReadableProps( +TclOOInstallReadableProperties( PropertyStorage *props, /* Which property list to install into. */ Tcl_Size objc, /* Number of property names. */ Tcl_Obj *const objv[]) /* Property names. */ @@ -880,7 +897,7 @@ TclOOInstallReadableProps( /* * ---------------------------------------------------------------------- * - * TclOOInstallWritableProps -- + * TclOOInstallWritableProperties -- * * Helper for writing the writable property list (which is actually a set) * that includes flushing the name cache. @@ -888,7 +905,7 @@ TclOOInstallReadableProps( * ---------------------------------------------------------------------- */ void -TclOOInstallWritableProps( +TclOOInstallWritableProperties( PropertyStorage *props, /* Which property list to install into. */ Tcl_Size objc, /* Number of property names. */ Tcl_Obj *const objv[]) /* Property names. */ @@ -981,13 +998,13 @@ TclOOInstallStdPropertyImpls( if (!object) { return TCL_ERROR; } - TclOOImplementObjectProperty(object, propName, readable, writable); + ImplementObjectProperty(object, propName, readable, writable); } else { Tcl_Class cls = (Tcl_Class) TclOOGetClassDefineCmdContext(interp); if (!cls) { return TCL_ERROR; } - TclOOImplementClassProperty(cls, propName, readable, writable); + ImplementClassProperty(cls, propName, readable, writable); } return TCL_OK; @@ -1001,6 +1018,169 @@ TclOOInstallStdPropertyImpls( /* * ---------------------------------------------------------------------- * + * TclOODefinePropertyCmd -- + * + * Implementation of the "property" definition for classes and instances + * governed by the [oo::configurable] metaclass. + * + * ---------------------------------------------------------------------- + */ + +int +TclOODefinePropertyCmd( + void *useInstance, /* NULL for class, non-NULL for object. */ + Tcl_Interp *interp, /* For error reporting and lookup. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const *objv) /* Arguments. */ +{ + int i; + const char *const options[] = { + "-get", "-kind", "-set", NULL + }; + enum Options { + OPT_GET, OPT_KIND, OPT_SET + }; + const char *const kinds[] = { + "readable", "readwrite", "writable", NULL + }; + enum Kinds { + KIND_RO, KIND_RW, KIND_WO + }; + Object *oPtr = (Object *) TclOOGetDefineCmdContext(interp); + + if (oPtr == NULL) { + return TCL_ERROR; + } + if (!useInstance && !oPtr->classPtr) { + Tcl_SetObjResult(interp, Tcl_NewStringObj( + "attempt to misuse API", -1)); + Tcl_SetErrorCode(interp, "TCL", "OO", "MONKEY_BUSINESS", (char *)NULL); + return TCL_ERROR; + } + + for (i = 1; i < objc; i++) { + Tcl_Obj *propObj = objv[i], *nextObj, *argObj, *hyphenated; + Tcl_Obj *getterScript = NULL, *setterScript = NULL; + + /* + * Parse the extra options for the property. + */ + + int kind = KIND_RW; + while (i + 1 < objc) { + int option; + + nextObj = objv[i + 1]; + if (TclGetString(nextObj)[0] != '-') { + break; + } + if (Tcl_GetIndexFromObj(interp, nextObj, options, "option", 0, + &option) != TCL_OK) { + return TCL_ERROR; + } + if (i + 2 >= objc) { + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "missing %s to go with %s option", + (option == OPT_KIND ? "kind value" : "body"), + options[option])); + Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", NULL); + return TCL_ERROR; + } + argObj = objv[i + 2]; + i += 2; + switch (option) { + case OPT_GET: + getterScript = argObj; + break; + case OPT_SET: + setterScript = argObj; + break; + case OPT_KIND: + if (Tcl_GetIndexFromObj(interp, argObj, kinds, "kind", 0, + &kind) != TCL_OK) { + return TCL_ERROR; + } + break; + } + } + + /* + * Install the property. Note that TclOOInstallStdPropertyImpls + * validates the property name as well. + */ + + if (TclOOInstallStdPropertyImpls(useInstance, interp, propObj, + kind != KIND_WO && getterScript == NULL, + kind != KIND_RO && setterScript == NULL) != TCL_OK) { + return TCL_ERROR; + } + + hyphenated = Tcl_ObjPrintf("-%s", TclGetString(propObj)); + if (useInstance) { + TclOORegisterInstanceProperty(oPtr, hyphenated, + kind != KIND_WO, kind != KIND_RO); + } else { + TclOORegisterProperty(oPtr->classPtr, hyphenated, + kind != KIND_WO, kind != KIND_RO); + } + Tcl_BounceRefCount(hyphenated); + + /* + * Create property implementation methods by using the right + * back-end API, but only if the user has given us the bodies of the + * methods we'll make. + */ + + if (getterScript != NULL) { + Tcl_Obj *getterName = Tcl_ObjPrintf("", + TclGetString(propObj)); + Tcl_Obj *argsPtr = Tcl_NewObj(); + Method *mPtr; + + Tcl_IncrRefCount(getterScript); + if (useInstance) { + mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, + getterName, argsPtr, getterScript, NULL); + } else { + mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, + getterName, argsPtr, getterScript, NULL); + } + Tcl_BounceRefCount(getterName); + Tcl_BounceRefCount(argsPtr); + Tcl_DecrRefCount(getterScript); + if (mPtr == NULL) { + return TCL_ERROR; + } + } + if (setterScript != NULL) { + Tcl_Obj *setterName = Tcl_ObjPrintf("", + TclGetString(propObj)); + Tcl_Obj *argsPtr; + Method *mPtr; + + TclNewLiteralStringObj(argsPtr, "value"); + Tcl_IncrRefCount(setterScript); + if (useInstance) { + mPtr = TclOONewProcInstanceMethod(interp, oPtr, 0, + setterName, argsPtr, setterScript, NULL); + } else { + mPtr = TclOONewProcMethod(interp, oPtr->classPtr, 0, + setterName, argsPtr, setterScript, NULL); + } + Tcl_BounceRefCount(setterName); + Tcl_BounceRefCount(argsPtr); + Tcl_DecrRefCount(setterScript); + if (mPtr == NULL) { + return TCL_ERROR; + } + } + } + return TCL_OK; +} + +/* + * ---------------------------------------------------------------------- + * * TclOOInfoClassPropCmd, TclOOInfoObjectPropCmd -- * * Implements [info class properties $clsName ?$option...?] and diff --git a/tests/compile.test b/tests/compile.test index cf552e2..72eef5e 100644 --- a/tests/compile.test +++ b/tests/compile.test @@ -827,6 +827,7 @@ test compile-18.19 {disassembler - basics} -setup { # There never was a compile-18.20. # The keys of the dictionary produced by [getbytecode] are defined. set bytecodekeys {literals variables exception instructions auxiliary commands script namespace stackdepth exceptdepth} +set allbytecodekeys [list {*}$bytecodekeys initiallinenumber sourcefile] test compile-18.21 {disassembler - basics} -returnCodes error -body { tcl::unsupported::getbytecode } -match glob -result {wrong # args: should be "*"} @@ -841,7 +842,7 @@ test compile-18.24 {disassembler - basics} -returnCodes error -body { } -result "can't interpret \"\{\" as a lambda expression" test compile-18.25 {disassembler - basics} -body { dict keys [tcl::unsupported::getbytecode lambda {{} {}}] -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.26 {disassembler - basics} -returnCodes error -body { tcl::unsupported::getbytecode proc } -match glob -result {wrong # args: should be "* proc procName"} @@ -854,7 +855,7 @@ test compile-18.28 {disassembler - basics} -setup { dict keys [tcl::unsupported::getbytecode proc chewonthis] } -cleanup { rename chewonthis {} -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.28.1 {disassembler - tricky bit} -setup { eval [list proc chewonthis {} {}] } -body { @@ -868,7 +869,7 @@ test compile-18.28.2 {disassembler - tricky bit} -setup { dict keys [tcl::unsupported::getbytecode proc chewonthis] } -cleanup { rename chewonthis {} -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.28.3 {disassembler - tricky bit} -setup { proc Proc {n a b} { proc $n $a $b @@ -890,7 +891,7 @@ test compile-18.28.4 {disassembler - tricky bit} -setup { } -cleanup { rename Proc {} rename chewonthis {} -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.29 {disassembler - basics} -returnCodes error -body { tcl::unsupported::getbytecode script } -match glob -result {wrong # args: should be "* script script"} @@ -919,7 +920,7 @@ test compile-18.35 {disassembler - basics} -setup { dict keys [tcl::unsupported::getbytecode method foo bar] } -cleanup { foo destroy -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.36 {disassembler - basics} -returnCodes error -body { tcl::unsupported::getbytecode objmethod } -match glob -result {wrong # args: should be "* objmethod objectName methodName"} @@ -936,7 +937,7 @@ test compile-18.39 {disassembler - basics} -setup { dict keys [tcl::unsupported::getbytecode objmethod foo bar] } -cleanup { foo destroy -} -result "$bytecodekeys initiallinenumber sourcefile" +} -result $allbytecodekeys test compile-18.40 {disassembler - basics} -returnCodes error -body { tcl::unsupported::disassemble constructor } -match glob -result {wrong # args: should be "* constructor className"} @@ -984,7 +985,7 @@ test compile-18.48 {disassembler - basics} -setup { dict keys [tcl::unsupported::getbytecode constructor foo] } -cleanup { foo destroy -} -result "$bytecodekeys" +} -result $allbytecodekeys # There is no compile-18.49 test compile-18.50 {disassembler - basics} -returnCodes error -body { tcl::unsupported::disassemble destructor @@ -1033,7 +1034,7 @@ test compile-18.58 {disassembler - basics} -setup { dict keys [tcl::unsupported::getbytecode destructor foo] } -cleanup { foo destroy -} -result "$bytecodekeys" +} -result $allbytecodekeys test compile-19.0 {Bug 3614102: reset stack housekeeping} -body { # This will panic in a --enable-symbols=compile build, unless bug is fixed. -- cgit v0.12 From 85a7228bbfd49a9b78fbb4925362673f33db39ab Mon Sep 17 00:00:00 2001 From: dkf Date: Tue, 6 Aug 2024 09:14:24 +0000 Subject: Remove unwanted change --- generic/tclInt.h | 1 - 1 file changed, 1 deletion(-) diff --git a/generic/tclInt.h b/generic/tclInt.h index e153aa8..d397879 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -3004,7 +3004,6 @@ typedef struct ProcessGlobalValue { /* *---------------------------------------------------------------------- - * * Common functions for calculating overallocation. Trivial but allows for * experimenting with growth factors without having to change code in * multiple places. See TclAttemptAllocElemsEx and similar for usage -- cgit v0.12