os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclObj.c
Update contrib.
4 * This file contains Tcl object-related procedures that are used by
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
8 * Copyright (c) 1999 by Scriptics Corporation.
9 * Copyright (c) 2001 by ActiveState Corporation.
10 * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 * RCS: @(#) $Id: tclObj.c,v 1.42.2.14 2005/11/29 14:02:04 dkf Exp $
19 #include "tclCompile.h"
21 #if defined(__SYMBIAN32__)
22 #include "tclSymbianGlobals.h"
26 * Table of all object types.
28 #if !defined(__SYMBIAN32__) || !defined(__WINSCW__)
29 static Tcl_HashTable typeTable;
30 static int typeTableInitialized = 0; /* 0 means not yet initialized. */
32 TCL_DECLARE_MUTEX(tableMutex)
35 * Head of the list of free Tcl_Obj structs we maintain.
38 Tcl_Obj *tclFreeObjList = NULL;
41 * The object allocator is single threaded. This mutex is referenced
42 * by the TclNewObj macro, however, so must be visible.
46 Tcl_Mutex tclObjMutex;
50 * Pointer to a heap-allocated string of length zero that the Tcl core uses
51 * as the value of an empty string representation for an object. This value
52 * is shared by all new objects allocated by Tcl_NewObj.
55 char tclEmptyString = '\0';
56 char *tclEmptyStringRep = &tclEmptyString;
59 * Prototypes for procedures defined later in this file:
62 static int SetBooleanFromAny _ANSI_ARGS_((Tcl_Interp *interp,
64 static int SetDoubleFromAny _ANSI_ARGS_((Tcl_Interp *interp,
66 static int SetIntFromAny _ANSI_ARGS_((Tcl_Interp *interp,
68 static int SetIntOrWideFromAny _ANSI_ARGS_((Tcl_Interp* interp,
70 static void UpdateStringOfBoolean _ANSI_ARGS_((Tcl_Obj *objPtr));
71 static void UpdateStringOfDouble _ANSI_ARGS_((Tcl_Obj *objPtr));
72 static void UpdateStringOfInt _ANSI_ARGS_((Tcl_Obj *objPtr));
73 static int SetWideIntFromAny _ANSI_ARGS_((Tcl_Interp *interp,
76 #ifndef TCL_WIDE_INT_IS_LONG
77 static void UpdateStringOfWideInt _ANSI_ARGS_((Tcl_Obj *objPtr));
81 * Prototypes for the array hash key methods.
84 static Tcl_HashEntry * AllocObjEntry _ANSI_ARGS_((
85 Tcl_HashTable *tablePtr, VOID *keyPtr));
86 static int CompareObjKeys _ANSI_ARGS_((
87 VOID *keyPtr, Tcl_HashEntry *hPtr));
88 static void FreeObjEntry _ANSI_ARGS_((
89 Tcl_HashEntry *hPtr));
90 static unsigned int HashObjKey _ANSI_ARGS_((
91 Tcl_HashTable *tablePtr,
95 * Prototypes for the CommandName object type.
98 static void DupCmdNameInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr,
100 static void FreeCmdNameInternalRep _ANSI_ARGS_((
102 static int SetCmdNameFromAny _ANSI_ARGS_((Tcl_Interp *interp,
107 * The structures below defines the Tcl object types defined in this file by
108 * means of procedures that can be invoked by generic object code. See also
109 * tclStringObj.c, tclListObj.c, tclByteCode.c for other type manager
113 Tcl_ObjType tclBooleanType = {
114 "boolean", /* name */
115 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
116 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
117 UpdateStringOfBoolean, /* updateStringProc */
118 SetBooleanFromAny /* setFromAnyProc */
121 Tcl_ObjType tclDoubleType = {
123 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
124 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
125 UpdateStringOfDouble, /* updateStringProc */
126 SetDoubleFromAny /* setFromAnyProc */
129 Tcl_ObjType tclIntType = {
131 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
132 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
133 UpdateStringOfInt, /* updateStringProc */
134 SetIntFromAny /* setFromAnyProc */
137 Tcl_ObjType tclWideIntType = {
138 "wideInt", /* name */
139 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
140 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
141 #ifdef TCL_WIDE_INT_IS_LONG
142 UpdateStringOfInt, /* updateStringProc */
143 #else /* !TCL_WIDE_INT_IS_LONG */
144 UpdateStringOfWideInt, /* updateStringProc */
146 SetWideIntFromAny /* setFromAnyProc */
150 * The structure below defines the Tcl obj hash key type.
152 Tcl_HashKeyType tclObjHashKeyType = {
153 TCL_HASH_KEY_TYPE_VERSION, /* version */
155 HashObjKey, /* hashKeyProc */
156 CompareObjKeys, /* compareKeysProc */
157 AllocObjEntry, /* allocEntryProc */
158 FreeObjEntry /* freeEntryProc */
162 * The structure below defines the command name Tcl object type by means of
163 * procedures that can be invoked by generic object code. Objects of this
164 * type cache the Command pointer that results from looking up command names
165 * in the command hashtable. Such objects appear as the zeroth ("command
166 * name") argument in a Tcl command.
168 * NOTE: the ResolvedCmdName that gets cached is stored in the
169 * twoPtrValue.ptr1 field, and the twoPtrValue.ptr2 field is unused.
170 * You might think you could use the simpler otherValuePtr field to
171 * store the single ResolvedCmdName pointer, but DO NOT DO THIS. It
172 * seems that some extensions use the second internal pointer field
173 * of the twoPtrValue field for their own purposes.
176 static Tcl_ObjType tclCmdNameType = {
177 "cmdName", /* name */
178 FreeCmdNameInternalRep, /* freeIntRepProc */
179 DupCmdNameInternalRep, /* dupIntRepProc */
180 (Tcl_UpdateStringProc *) NULL, /* updateStringProc */
181 SetCmdNameFromAny /* setFromAnyProc */
186 * Structure containing a cached pointer to a command that is the result
187 * of resolving the command's name in some namespace. It is the internal
188 * representation for a cmdName object. It contains the pointer along
189 * with some information that is used to check the pointer's validity.
192 typedef struct ResolvedCmdName {
193 Command *cmdPtr; /* A cached Command pointer. */
194 Namespace *refNsPtr; /* Points to the namespace containing the
195 * reference (not the namespace that
196 * contains the referenced command). */
197 long refNsId; /* refNsPtr's unique namespace id. Used to
198 * verify that refNsPtr is still valid
199 * (e.g., it's possible that the cmd's
200 * containing namespace was deleted and a
201 * new one created at the same address). */
202 int refNsCmdEpoch; /* Value of the referencing namespace's
203 * cmdRefEpoch when the pointer was cached.
204 * Before using the cached pointer, we check
205 * if the namespace's epoch was incremented;
206 * if so, this cached pointer is invalid. */
207 int cmdEpoch; /* Value of the command's cmdEpoch when this
208 * pointer was cached. Before using the
209 * cached pointer, we check if the cmd's
210 * epoch was incremented; if so, the cmd was
211 * renamed, deleted, hidden, or exposed, and
212 * so the pointer is invalid. */
213 int refCount; /* Reference count: 1 for each cmdName
214 * object that has a pointer to this
215 * ResolvedCmdName structure as its internal
216 * rep. This structure can be freed when
217 * refCount becomes zero. */
222 *-------------------------------------------------------------------------
224 * TclInitObjectSubsystem --
226 * This procedure is invoked to perform once-only initialization of
227 * the type table. It also registers the object types defined in
234 * Initializes the table of defined object types "typeTable" with
235 * builtin object types defined in this file.
237 *-------------------------------------------------------------------------
241 TclInitObjSubsystem()
243 Tcl_MutexLock(&tableMutex);
244 typeTableInitialized = 1;
245 Tcl_InitHashTable(&typeTable, TCL_STRING_KEYS);
246 Tcl_MutexUnlock(&tableMutex);
248 Tcl_RegisterObjType(&tclBooleanType);
249 Tcl_RegisterObjType(&tclByteArrayType);
250 Tcl_RegisterObjType(&tclDoubleType);
251 Tcl_RegisterObjType(&tclEndOffsetType);
252 Tcl_RegisterObjType(&tclIntType);
253 Tcl_RegisterObjType(&tclWideIntType);
254 Tcl_RegisterObjType(&tclStringType);
255 Tcl_RegisterObjType(&tclListType);
256 Tcl_RegisterObjType(&tclByteCodeType);
257 Tcl_RegisterObjType(&tclProcBodyType);
258 Tcl_RegisterObjType(&tclArraySearchType);
259 Tcl_RegisterObjType(&tclIndexType);
260 Tcl_RegisterObjType(&tclNsNameType);
261 Tcl_RegisterObjType(&tclCmdNameType);
263 #ifdef TCL_COMPILE_STATS
264 Tcl_MutexLock(&tclObjMutex);
269 for (i = 0; i < TCL_MAX_SHARED_OBJ_STATS; i++) {
270 tclObjsShared[i] = 0;
273 Tcl_MutexUnlock(&tclObjMutex);
278 *----------------------------------------------------------------------
280 * TclFinalizeObjects --
282 * This procedure is called by Tcl_Finalize to clean up all
283 * registered Tcl_ObjType's and to reset the tclFreeObjList.
291 *----------------------------------------------------------------------
297 Tcl_MutexLock(&tableMutex);
298 if (typeTableInitialized) {
299 Tcl_DeleteHashTable(&typeTable);
300 typeTableInitialized = 0;
302 Tcl_MutexUnlock(&tableMutex);
305 * All we do here is reset the head pointer of the linked list of
306 * free Tcl_Obj's to NULL; the memory finalization will take care
307 * of releasing memory for us.
309 Tcl_MutexLock(&tclObjMutex);
310 tclFreeObjList = NULL;
311 Tcl_MutexUnlock(&tclObjMutex);
315 *--------------------------------------------------------------
317 * Tcl_RegisterObjType --
319 * This procedure is called to register a new Tcl object type
320 * in the table of all object types supported by Tcl.
326 * The type is registered in the Tcl type table. If there was already
327 * a type with the same name as in typePtr, it is replaced with the
330 *--------------------------------------------------------------
334 Tcl_RegisterObjType(typePtr)
335 Tcl_ObjType *typePtr; /* Information about object type;
336 * storage must be statically
337 * allocated (must live forever). */
340 Tcl_MutexLock(&tableMutex);
342 Tcl_CreateHashEntry(&typeTable, typePtr->name, &new), typePtr);
343 Tcl_MutexUnlock(&tableMutex);
347 *----------------------------------------------------------------------
349 * Tcl_AppendAllObjTypes --
351 * This procedure appends onto the argument object the name of each
352 * object type as a list element. This includes the builtin object
353 * types (e.g. int, list) as well as those added using
354 * Tcl_NewObj. These names can be used, for example, with
355 * Tcl_GetObjType to get pointers to the corresponding Tcl_ObjType
359 * The return value is normally TCL_OK; in this case the object
360 * referenced by objPtr has each type name appended to it. If an
361 * error occurs, TCL_ERROR is returned and the interpreter's result
362 * holds an error message.
365 * If necessary, the object referenced by objPtr is converted into
368 *----------------------------------------------------------------------
372 Tcl_AppendAllObjTypes(interp, objPtr)
373 Tcl_Interp *interp; /* Interpreter used for error reporting. */
374 Tcl_Obj *objPtr; /* Points to the Tcl object onto which the
375 * name of each registered type is appended
376 * as a list element. */
378 register Tcl_HashEntry *hPtr;
379 Tcl_HashSearch search;
384 * Get the test for a valid list out of the way first.
387 if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) {
392 * Type names are NUL-terminated, not counted strings.
393 * This code relies on that.
396 Tcl_MutexLock(&tableMutex);
397 for (hPtr = Tcl_FirstHashEntry(&typeTable, &search);
398 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
399 Tcl_ListObjAppendElement(NULL, objPtr,
400 Tcl_NewStringObj(Tcl_GetHashKey(&typeTable, hPtr), -1));
402 Tcl_MutexUnlock(&tableMutex);
407 *----------------------------------------------------------------------
411 * This procedure looks up an object type by name.
414 * If an object type with name matching "typeName" is found, a pointer
415 * to its Tcl_ObjType structure is returned; otherwise, NULL is
421 *----------------------------------------------------------------------
424 EXPORT_C Tcl_ObjType *
425 Tcl_GetObjType(typeName)
426 CONST char *typeName; /* Name of Tcl object type to look up. */
428 register Tcl_HashEntry *hPtr;
429 Tcl_ObjType *typePtr = NULL;
431 Tcl_MutexLock(&tableMutex);
432 hPtr = Tcl_FindHashEntry(&typeTable, typeName);
433 if (hPtr != (Tcl_HashEntry *) NULL) {
434 typePtr = (Tcl_ObjType *) Tcl_GetHashValue(hPtr);
436 Tcl_MutexUnlock(&tableMutex);
441 *----------------------------------------------------------------------
443 * Tcl_ConvertToType --
445 * Convert the Tcl object "objPtr" to have type "typePtr" if possible.
448 * The return value is TCL_OK on success and TCL_ERROR on failure. If
449 * TCL_ERROR is returned, then the interpreter's result contains an
450 * error message unless "interp" is NULL. Passing a NULL "interp"
451 * allows this procedure to be used as a test whether the conversion
452 * could be done (and in fact was done).
455 * Any internal representation for the old type is freed.
457 *----------------------------------------------------------------------
461 Tcl_ConvertToType(interp, objPtr, typePtr)
462 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
463 Tcl_Obj *objPtr; /* The object to convert. */
464 Tcl_ObjType *typePtr; /* The target type. */
466 if (objPtr->typePtr == typePtr) {
471 * Use the target type's Tcl_SetFromAnyProc to set "objPtr"s internal
472 * form as appropriate for the target type. This frees the old internal
476 return typePtr->setFromAnyProc(interp, objPtr);
480 *----------------------------------------------------------------------
484 * This procedure is normally called when not debugging: i.e., when
485 * TCL_MEM_DEBUG is not defined. It creates new Tcl objects that denote
486 * the empty string. These objects have a NULL object type and NULL
487 * string representation byte pointer. Type managers call this routine
488 * to allocate new objects that they further initialize.
490 * When TCL_MEM_DEBUG is defined, this procedure just returns the
491 * result of calling the debugging version Tcl_DbNewObj.
494 * The result is a newly allocated object that represents the empty
495 * string. The new object's typePtr is set NULL and its ref count
499 * If compiling with TCL_COMPILE_STATS, this procedure increments
500 * the global count of allocated objects (tclObjsAlloced).
502 *----------------------------------------------------------------------
511 return Tcl_DbNewObj("unknown", 0);
514 #else /* if not TCL_MEM_DEBUG */
519 register Tcl_Obj *objPtr;
522 * Use the macro defined in tclInt.h - it will use the
529 #endif /* TCL_MEM_DEBUG */
532 *----------------------------------------------------------------------
536 * This procedure is normally called when debugging: i.e., when
537 * TCL_MEM_DEBUG is defined. It creates new Tcl objects that denote the
538 * empty string. It is the same as the Tcl_NewObj procedure above
539 * except that it calls Tcl_DbCkalloc directly with the file name and
540 * line number from its caller. This simplifies debugging since then
541 * the [memory active] command will report the correct file name and line
542 * number when reporting objects that haven't been freed.
544 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
545 * result of calling Tcl_NewObj.
548 * The result is a newly allocated that represents the empty string.
549 * The new object's typePtr is set NULL and its ref count is set to 0.
552 * If compiling with TCL_COMPILE_STATS, this procedure increments
553 * the global count of allocated objects (tclObjsAlloced).
555 *----------------------------------------------------------------------
561 Tcl_DbNewObj(file, line)
562 register CONST char *file; /* The name of the source file calling this
563 * procedure; used for debugging. */
564 register int line; /* Line number in the source file; used
567 register Tcl_Obj *objPtr;
570 * Use the macro defined in tclInt.h - it will use the
574 TclDbNewObj(objPtr, file, line);
577 #else /* if not TCL_MEM_DEBUG */
580 Tcl_DbNewObj(file, line)
581 CONST char *file; /* The name of the source file calling this
582 * procedure; used for debugging. */
583 int line; /* Line number in the source file; used
588 #endif /* TCL_MEM_DEBUG */
591 *----------------------------------------------------------------------
593 * TclAllocateFreeObjects --
595 * Procedure to allocate a number of free Tcl_Objs. This is done using
596 * a single ckalloc to reduce the overhead for Tcl_Obj allocation.
598 * Assumes mutex is held.
604 * tclFreeObjList, the head of the list of free Tcl_Objs, is set to the
605 * first of a number of free Tcl_Obj's linked together by their
606 * internalRep.otherValuePtrs.
608 *----------------------------------------------------------------------
611 #define OBJS_TO_ALLOC_EACH_TIME 100
614 TclAllocateFreeObjects()
616 size_t bytesToAlloc = (OBJS_TO_ALLOC_EACH_TIME * sizeof(Tcl_Obj));
618 register Tcl_Obj *prevPtr, *objPtr;
622 * This has been noted by Purify to be a potential leak. The problem is
623 * that Tcl, when not TCL_MEM_DEBUG compiled, keeps around all allocated
624 * Tcl_Obj's, pointed to by tclFreeObjList, when freed instead of
625 * actually freeing the memory. TclFinalizeObjects() does not ckfree()
626 * this memory, but leaves it to Tcl's memory subsystem finalziation to
627 * release it. Purify apparently can't figure that out, and fires a
631 basePtr = (char *) ckalloc(bytesToAlloc);
632 memset(basePtr, 0, bytesToAlloc);
635 objPtr = (Tcl_Obj *) basePtr;
636 for (i = 0; i < OBJS_TO_ALLOC_EACH_TIME; i++) {
637 objPtr->internalRep.otherValuePtr = (VOID *) prevPtr;
641 tclFreeObjList = prevPtr;
643 #undef OBJS_TO_ALLOC_EACH_TIME
646 *----------------------------------------------------------------------
650 * This procedure frees the memory associated with the argument
651 * object. It is called by the tcl.h macro Tcl_DecrRefCount when an
652 * object's ref count is zero. It is only "public" since it must
653 * be callable by that macro wherever the macro is used. It should not
654 * be directly called by clients.
660 * Deallocates the storage for the object's Tcl_Obj structure
661 * after deallocating the string representation and calling the
662 * type-specific Tcl_FreeInternalRepProc to deallocate the object's
663 * internal representation. If compiling with TCL_COMPILE_STATS,
664 * this procedure increments the global count of freed objects
667 *----------------------------------------------------------------------
672 register Tcl_Obj *objPtr; /* The object to be freed. */
674 register Tcl_ObjType *typePtr = objPtr->typePtr;
677 if ((objPtr)->refCount < -1) {
678 panic("Reference count for %lx was negative", objPtr);
680 #endif /* TCL_MEM_DEBUG */
682 if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) {
683 typePtr->freeIntRepProc(objPtr);
685 Tcl_InvalidateStringRep(objPtr);
688 * If debugging Tcl's memory usage, deallocate the object using ckfree.
689 * Otherwise, deallocate it by adding it onto the list of free
690 * Tcl_Obj structs we maintain.
693 #if defined(TCL_MEM_DEBUG) || defined(PURIFY)
694 Tcl_MutexLock(&tclObjMutex);
695 ckfree((char *) objPtr);
696 Tcl_MutexUnlock(&tclObjMutex);
697 #elif defined(TCL_THREADS) && defined(USE_THREAD_ALLOC)
698 TclThreadFreeObj(objPtr);
700 Tcl_MutexLock(&tclObjMutex);
701 objPtr->internalRep.otherValuePtr = (VOID *) tclFreeObjList;
702 tclFreeObjList = objPtr;
703 Tcl_MutexUnlock(&tclObjMutex);
704 #endif /* TCL_MEM_DEBUG */
706 #ifdef TCL_COMPILE_STATS
708 #endif /* TCL_COMPILE_STATS */
712 *----------------------------------------------------------------------
714 * Tcl_DuplicateObj --
716 * Create and return a new object that is a duplicate of the argument
720 * The return value is a pointer to a newly created Tcl_Obj. This
721 * object has reference count 0 and the same type, if any, as the
722 * source object objPtr. Also:
723 * 1) If the source object has a valid string rep, we copy it;
724 * otherwise, the duplicate's string rep is set NULL to mark
726 * 2) If the source object has an internal representation (i.e. its
727 * typePtr is non-NULL), the new object's internal rep is set to
728 * a copy; otherwise the new internal rep is marked invalid.
731 * What constitutes "copying" the internal representation depends on
732 * the type. For example, if the argument object is a list,
733 * the element objects it points to will not actually be copied but
734 * will be shared with the duplicate list. That is, the ref counts of
735 * the element objects will be incremented.
737 *----------------------------------------------------------------------
741 Tcl_DuplicateObj(objPtr)
742 register Tcl_Obj *objPtr; /* The object to duplicate. */
744 register Tcl_ObjType *typePtr = objPtr->typePtr;
745 register Tcl_Obj *dupPtr;
749 if (objPtr->bytes == NULL) {
750 dupPtr->bytes = NULL;
751 } else if (objPtr->bytes != tclEmptyStringRep) {
752 TclInitStringRep(dupPtr, objPtr->bytes, objPtr->length);
755 if (typePtr != NULL) {
756 if (typePtr->dupIntRepProc == NULL) {
757 dupPtr->internalRep = objPtr->internalRep;
758 dupPtr->typePtr = typePtr;
760 (*typePtr->dupIntRepProc)(objPtr, dupPtr);
767 *----------------------------------------------------------------------
771 * Returns the string representation byte array pointer for an object.
774 * Returns a pointer to the string representation of objPtr. The byte
775 * array referenced by the returned pointer must not be modified by the
776 * caller. Furthermore, the caller must copy the bytes if they need to
777 * retain them since the object's string rep can change as a result of
781 * May call the object's updateStringProc to update the string
782 * representation from the internal representation.
784 *----------------------------------------------------------------------
788 Tcl_GetString(objPtr)
789 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer
790 * should be returned. */
792 if (objPtr->bytes != NULL) {
793 return objPtr->bytes;
796 if (objPtr->typePtr->updateStringProc == NULL) {
797 panic("UpdateStringProc should not be invoked for type %s",
798 objPtr->typePtr->name);
800 (*objPtr->typePtr->updateStringProc)(objPtr);
801 return objPtr->bytes;
805 *----------------------------------------------------------------------
807 * Tcl_GetStringFromObj --
809 * Returns the string representation's byte array pointer and length
813 * Returns a pointer to the string representation of objPtr. If
814 * lengthPtr isn't NULL, the length of the string representation is
815 * stored at *lengthPtr. The byte array referenced by the returned
816 * pointer must not be modified by the caller. Furthermore, the
817 * caller must copy the bytes if they need to retain them since the
818 * object's string rep can change as a result of other operations.
821 * May call the object's updateStringProc to update the string
822 * representation from the internal representation.
824 *----------------------------------------------------------------------
828 Tcl_GetStringFromObj(objPtr, lengthPtr)
829 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer should
831 register int *lengthPtr; /* If non-NULL, the location where the string
832 * rep's byte array length should * be stored.
833 * If NULL, no length is stored. */
835 if (objPtr->bytes == NULL) {
836 if (objPtr->typePtr->updateStringProc == NULL) {
837 panic("UpdateStringProc should not be invoked for type %s",
838 objPtr->typePtr->name);
840 (*objPtr->typePtr->updateStringProc)(objPtr);
843 if (lengthPtr != NULL) {
844 *lengthPtr = objPtr->length;
846 return objPtr->bytes;
850 *----------------------------------------------------------------------
852 * Tcl_InvalidateStringRep --
854 * This procedure is called to invalidate an object's string
861 * Deallocates the storage for any old string representation, then
862 * sets the string representation NULL to mark it invalid.
864 *----------------------------------------------------------------------
868 Tcl_InvalidateStringRep(objPtr)
869 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer
870 * should be freed. */
872 if (objPtr->bytes != NULL) {
873 if (objPtr->bytes != tclEmptyStringRep) {
874 ckfree((char *) objPtr->bytes);
876 objPtr->bytes = NULL;
881 *----------------------------------------------------------------------
883 * Tcl_NewBooleanObj --
885 * This procedure is normally called when not debugging: i.e., when
886 * TCL_MEM_DEBUG is not defined. It creates a new Tcl_Obj and
887 * initializes it from the argument boolean value. A nonzero
888 * "boolValue" is coerced to 1.
890 * When TCL_MEM_DEBUG is defined, this procedure just returns the
891 * result of calling the debugging version Tcl_DbNewBooleanObj.
894 * The newly created object is returned. This object will have an
895 * invalid string representation. The returned object has ref count 0.
900 *----------------------------------------------------------------------
904 #undef Tcl_NewBooleanObj
907 Tcl_NewBooleanObj(boolValue)
908 register int boolValue; /* Boolean used to initialize new object. */
910 return Tcl_DbNewBooleanObj(boolValue, "unknown", 0);
913 #else /* if not TCL_MEM_DEBUG */
916 Tcl_NewBooleanObj(boolValue)
917 register int boolValue; /* Boolean used to initialize new object. */
919 register Tcl_Obj *objPtr;
922 objPtr->bytes = NULL;
924 objPtr->internalRep.longValue = (boolValue? 1 : 0);
925 objPtr->typePtr = &tclBooleanType;
928 #endif /* TCL_MEM_DEBUG */
931 *----------------------------------------------------------------------
933 * Tcl_DbNewBooleanObj --
935 * This procedure is normally called when debugging: i.e., when
936 * TCL_MEM_DEBUG is defined. It creates new boolean objects. It is the
937 * same as the Tcl_NewBooleanObj procedure above except that it calls
938 * Tcl_DbCkalloc directly with the file name and line number from its
939 * caller. This simplifies debugging since then the [memory active]
940 * command will report the correct file name and line number when
941 * reporting objects that haven't been freed.
943 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
944 * result of calling Tcl_NewBooleanObj.
947 * The newly created object is returned. This object will have an
948 * invalid string representation. The returned object has ref count 0.
953 *----------------------------------------------------------------------
959 Tcl_DbNewBooleanObj(boolValue, file, line)
960 register int boolValue; /* Boolean used to initialize new object. */
961 CONST char *file; /* The name of the source file calling this
962 * procedure; used for debugging. */
963 int line; /* Line number in the source file; used
966 register Tcl_Obj *objPtr;
968 TclDbNewObj(objPtr, file, line);
969 objPtr->bytes = NULL;
971 objPtr->internalRep.longValue = (boolValue? 1 : 0);
972 objPtr->typePtr = &tclBooleanType;
976 #else /* if not TCL_MEM_DEBUG */
979 Tcl_DbNewBooleanObj(boolValue, file, line)
980 register int boolValue; /* Boolean used to initialize new object. */
981 CONST char *file; /* The name of the source file calling this
982 * procedure; used for debugging. */
983 int line; /* Line number in the source file; used
986 return Tcl_NewBooleanObj(boolValue);
988 #endif /* TCL_MEM_DEBUG */
991 *----------------------------------------------------------------------
993 * Tcl_SetBooleanObj --
995 * Modify an object to be a boolean object and to have the specified
996 * boolean value. A nonzero "boolValue" is coerced to 1.
1002 * The object's old string rep, if any, is freed. Also, any old
1003 * internal rep is freed.
1005 *----------------------------------------------------------------------
1009 Tcl_SetBooleanObj(objPtr, boolValue)
1010 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1011 register int boolValue; /* Boolean used to set object's value. */
1013 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1015 if (Tcl_IsShared(objPtr)) {
1016 panic("Tcl_SetBooleanObj called with shared object");
1019 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1020 oldTypePtr->freeIntRepProc(objPtr);
1023 objPtr->internalRep.longValue = (boolValue? 1 : 0);
1024 objPtr->typePtr = &tclBooleanType;
1025 Tcl_InvalidateStringRep(objPtr);
1029 *----------------------------------------------------------------------
1031 * Tcl_GetBooleanFromObj --
1033 * Attempt to return a boolean from the Tcl object "objPtr". If the
1034 * object is not already a boolean, an attempt will be made to convert
1038 * The return value is a standard Tcl object result. If an error occurs
1039 * during conversion, an error message is left in the interpreter's
1040 * result unless "interp" is NULL.
1043 * If the object is not already a boolean, the conversion will free
1044 * any old internal representation.
1046 *----------------------------------------------------------------------
1050 Tcl_GetBooleanFromObj(interp, objPtr, boolPtr)
1051 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1052 register Tcl_Obj *objPtr; /* The object from which to get boolean. */
1053 register int *boolPtr; /* Place to store resulting boolean. */
1055 register int result;
1057 if (objPtr->typePtr == &tclBooleanType) {
1060 result = SetBooleanFromAny(interp, objPtr);
1063 if (result == TCL_OK) {
1064 *boolPtr = (int) objPtr->internalRep.longValue;
1070 *----------------------------------------------------------------------
1072 * SetBooleanFromAny --
1074 * Attempt to generate a boolean internal form for the Tcl object
1078 * The return value is a standard Tcl result. If an error occurs during
1079 * conversion, an error message is left in the interpreter's result
1080 * unless "interp" is NULL.
1083 * If no error occurs, an integer 1 or 0 is stored as "objPtr"s
1084 * internal representation and the type of "objPtr" is set to boolean.
1086 *----------------------------------------------------------------------
1090 SetBooleanFromAny(interp, objPtr)
1091 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1092 register Tcl_Obj *objPtr; /* The object to convert. */
1094 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1098 int newBool, length;
1102 * Get the string representation. Make it up-to-date if necessary.
1105 string = Tcl_GetStringFromObj(objPtr, &length);
1108 * Use the obvious shortcuts for numerical values; if objPtr is not
1109 * of numerical type, parse its string rep.
1112 if (objPtr->typePtr == &tclIntType) {
1113 newBool = (objPtr->internalRep.longValue != 0);
1114 } else if (objPtr->typePtr == &tclDoubleType) {
1115 newBool = (objPtr->internalRep.doubleValue != 0.0);
1116 } else if (objPtr->typePtr == &tclWideIntType) {
1117 newBool = (objPtr->internalRep.wideValue != 0);
1120 * Copy the string converting its characters to lower case.
1123 for (i = 0; (i < 9) && (i < length); i++) {
1126 * Weed out international characters so we can safely operate
1133 if (Tcl_UniCharIsUpper(UCHAR(c))) {
1134 c = (char) Tcl_UniCharToLower(UCHAR(c));
1141 * Parse the string as a boolean. We use an implementation here that
1142 * doesn't report errors in interp if interp is NULL.
1146 if ((c == '0') && (lowerCase[1] == '\0')) {
1148 } else if ((c == '1') && (lowerCase[1] == '\0')) {
1150 } else if ((c == 'y') && (strncmp(lowerCase, "yes", (size_t) length) == 0)) {
1152 } else if ((c == 'n') && (strncmp(lowerCase, "no", (size_t) length) == 0)) {
1154 } else if ((c == 't') && (strncmp(lowerCase, "true", (size_t) length) == 0)) {
1156 } else if ((c == 'f') && (strncmp(lowerCase, "false", (size_t) length) == 0)) {
1158 } else if ((c == 'o') && (length >= 2)) {
1159 if (strncmp(lowerCase, "on", (size_t) length) == 0) {
1161 } else if (strncmp(lowerCase, "off", (size_t) length) == 0) {
1169 * Boolean values can be extracted from ints or doubles. Note
1170 * that we don't use strtoul or strtoull here because we don't
1171 * care about what the value is, just whether it is equal to
1174 #ifdef TCL_WIDE_INT_IS_LONG
1175 newBool = strtol(string, &end, 0);
1176 if (end != string) {
1178 * Make sure the string has no garbage after the end of
1181 while ((end < (string+length))
1182 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1185 if (end == (string+length)) {
1186 newBool = (newBool != 0);
1190 #else /* !TCL_WIDE_INT_IS_LONG */
1191 Tcl_WideInt wide = strtoll(string, &end, 0);
1192 if (end != string) {
1194 * Make sure the string has no garbage after the end of
1197 while ((end < (string+length))
1198 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1201 if (end == (string+length)) {
1202 newBool = (wide != Tcl_LongAsWide(0));
1206 #endif /* TCL_WIDE_INT_IS_LONG */
1208 * Still might be a string containing the characters representing an
1209 * int or double that wasn't handled above. This would be a string
1210 * like "27" or "1.0" that is non-zero and not "1". Such a string
1211 * would result in the boolean value true. We try converting to
1212 * double. If that succeeds and the resulting double is non-zero, we
1213 * have a "true". Note that numbers can't have embedded NULLs.
1216 dbl = strtod(string, &end);
1217 if (end == string) {
1222 * Make sure the string has no garbage after the end of the double.
1225 while ((end < (string+length))
1226 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1229 if (end != (string+length)) {
1232 newBool = (dbl != 0.0);
1237 * Free the old internalRep before setting the new one. We do this as
1238 * late as possible to allow the conversion code, in particular
1239 * Tcl_GetStringFromObj, to use that old internalRep.
1243 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1244 oldTypePtr->freeIntRepProc(objPtr);
1247 objPtr->internalRep.longValue = newBool;
1248 objPtr->typePtr = &tclBooleanType;
1252 if (interp != NULL) {
1254 * Must copy string before resetting the result in case a caller
1255 * is trying to convert the interpreter's result to a boolean.
1259 sprintf(buf, "expected boolean value but got \"%.50s\"", string);
1260 Tcl_ResetResult(interp);
1261 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1267 *----------------------------------------------------------------------
1269 * UpdateStringOfBoolean --
1271 * Update the string representation for a boolean object.
1272 * Note: This procedure does not free an existing old string rep
1273 * so storage will be lost if this has not already been done.
1279 * The object's string is set to a valid string that results from
1280 * the boolean-to-string conversion.
1282 *----------------------------------------------------------------------
1286 UpdateStringOfBoolean(objPtr)
1287 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
1289 char *s = ckalloc((unsigned) 2);
1291 s[0] = (char) (objPtr->internalRep.longValue? '1' : '0');
1298 *----------------------------------------------------------------------
1300 * Tcl_NewDoubleObj --
1302 * This procedure is normally called when not debugging: i.e., when
1303 * TCL_MEM_DEBUG is not defined. It creates a new double object and
1304 * initializes it from the argument double value.
1306 * When TCL_MEM_DEBUG is defined, this procedure just returns the
1307 * result of calling the debugging version Tcl_DbNewDoubleObj.
1310 * The newly created object is returned. This object will have an
1311 * invalid string representation. The returned object has ref count 0.
1316 *----------------------------------------------------------------------
1319 #ifdef TCL_MEM_DEBUG
1320 #undef Tcl_NewDoubleObj
1323 Tcl_NewDoubleObj(dblValue)
1324 register double dblValue; /* Double used to initialize the object. */
1326 return Tcl_DbNewDoubleObj(dblValue, "unknown", 0);
1329 #else /* if not TCL_MEM_DEBUG */
1332 Tcl_NewDoubleObj(dblValue)
1333 register double dblValue; /* Double used to initialize the object. */
1335 register Tcl_Obj *objPtr;
1338 objPtr->bytes = NULL;
1340 objPtr->internalRep.doubleValue = dblValue;
1341 objPtr->typePtr = &tclDoubleType;
1344 #endif /* if TCL_MEM_DEBUG */
1347 *----------------------------------------------------------------------
1349 * Tcl_DbNewDoubleObj --
1351 * This procedure is normally called when debugging: i.e., when
1352 * TCL_MEM_DEBUG is defined. It creates new double objects. It is the
1353 * same as the Tcl_NewDoubleObj procedure above except that it calls
1354 * Tcl_DbCkalloc directly with the file name and line number from its
1355 * caller. This simplifies debugging since then the [memory active]
1356 * command will report the correct file name and line number when
1357 * reporting objects that haven't been freed.
1359 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
1360 * result of calling Tcl_NewDoubleObj.
1363 * The newly created object is returned. This object will have an
1364 * invalid string representation. The returned object has ref count 0.
1369 *----------------------------------------------------------------------
1372 #ifdef TCL_MEM_DEBUG
1375 Tcl_DbNewDoubleObj(dblValue, file, line)
1376 register double dblValue; /* Double used to initialize the object. */
1377 CONST char *file; /* The name of the source file calling this
1378 * procedure; used for debugging. */
1379 int line; /* Line number in the source file; used
1382 register Tcl_Obj *objPtr;
1384 TclDbNewObj(objPtr, file, line);
1385 objPtr->bytes = NULL;
1387 objPtr->internalRep.doubleValue = dblValue;
1388 objPtr->typePtr = &tclDoubleType;
1392 #else /* if not TCL_MEM_DEBUG */
1395 Tcl_DbNewDoubleObj(dblValue, file, line)
1396 register double dblValue; /* Double used to initialize the object. */
1397 CONST char *file; /* The name of the source file calling this
1398 * procedure; used for debugging. */
1399 int line; /* Line number in the source file; used
1402 return Tcl_NewDoubleObj(dblValue);
1404 #endif /* TCL_MEM_DEBUG */
1407 *----------------------------------------------------------------------
1409 * Tcl_SetDoubleObj --
1411 * Modify an object to be a double object and to have the specified
1418 * The object's old string rep, if any, is freed. Also, any old
1419 * internal rep is freed.
1421 *----------------------------------------------------------------------
1425 Tcl_SetDoubleObj(objPtr, dblValue)
1426 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1427 register double dblValue; /* Double used to set the object's value. */
1429 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1431 if (Tcl_IsShared(objPtr)) {
1432 panic("Tcl_SetDoubleObj called with shared object");
1435 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1436 oldTypePtr->freeIntRepProc(objPtr);
1439 objPtr->internalRep.doubleValue = dblValue;
1440 objPtr->typePtr = &tclDoubleType;
1441 Tcl_InvalidateStringRep(objPtr);
1445 *----------------------------------------------------------------------
1447 * Tcl_GetDoubleFromObj --
1449 * Attempt to return a double from the Tcl object "objPtr". If the
1450 * object is not already a double, an attempt will be made to convert
1454 * The return value is a standard Tcl object result. If an error occurs
1455 * during conversion, an error message is left in the interpreter's
1456 * result unless "interp" is NULL.
1459 * If the object is not already a double, the conversion will free
1460 * any old internal representation.
1462 *----------------------------------------------------------------------
1466 Tcl_GetDoubleFromObj(interp, objPtr, dblPtr)
1467 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1468 register Tcl_Obj *objPtr; /* The object from which to get a double. */
1469 register double *dblPtr; /* Place to store resulting double. */
1471 register int result;
1473 if (objPtr->typePtr == &tclDoubleType) {
1474 *dblPtr = objPtr->internalRep.doubleValue;
1478 result = SetDoubleFromAny(interp, objPtr);
1479 if (result == TCL_OK) {
1480 *dblPtr = objPtr->internalRep.doubleValue;
1486 *----------------------------------------------------------------------
1488 * SetDoubleFromAny --
1490 * Attempt to generate an double-precision floating point internal form
1491 * for the Tcl object "objPtr".
1494 * The return value is a standard Tcl object result. If an error occurs
1495 * during conversion, an error message is left in the interpreter's
1496 * result unless "interp" is NULL.
1499 * If no error occurs, a double is stored as "objPtr"s internal
1502 *----------------------------------------------------------------------
1506 SetDoubleFromAny(interp, objPtr)
1507 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1508 register Tcl_Obj *objPtr; /* The object to convert. */
1510 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1516 * Get the string representation. Make it up-to-date if necessary.
1519 string = Tcl_GetStringFromObj(objPtr, &length);
1522 * Now parse "objPtr"s string as an double. Numbers can't have embedded
1523 * NULLs. We use an implementation here that doesn't report errors in
1524 * interp if interp is NULL.
1528 newDouble = strtod(string, &end);
1529 if (end == string) {
1531 if (interp != NULL) {
1533 * Must copy string before resetting the result in case a caller
1534 * is trying to convert the interpreter's result to an int.
1538 sprintf(buf, "expected floating-point number but got \"%.50s\"",
1540 Tcl_ResetResult(interp);
1541 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1546 if (interp != NULL) {
1547 TclExprFloatError(interp, newDouble);
1553 * Make sure that the string has no garbage after the end of the double.
1556 while ((end < (string+length))
1557 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
1560 if (end != (string+length)) {
1565 * The conversion to double succeeded. Free the old internalRep before
1566 * setting the new one. We do this as late as possible to allow the
1567 * conversion code, in particular Tcl_GetStringFromObj, to use that old
1571 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1572 oldTypePtr->freeIntRepProc(objPtr);
1575 objPtr->internalRep.doubleValue = newDouble;
1576 objPtr->typePtr = &tclDoubleType;
1581 *----------------------------------------------------------------------
1583 * UpdateStringOfDouble --
1585 * Update the string representation for a double-precision floating
1586 * point object. This must obey the current tcl_precision value for
1587 * double-to-string conversions. Note: This procedure does not free an
1588 * existing old string rep so storage will be lost if this has not
1589 * already been done.
1595 * The object's string is set to a valid string that results from
1596 * the double-to-string conversion.
1598 *----------------------------------------------------------------------
1602 UpdateStringOfDouble(objPtr)
1603 register Tcl_Obj *objPtr; /* Double obj with string rep to update. */
1605 char buffer[TCL_DOUBLE_SPACE];
1608 Tcl_PrintDouble((Tcl_Interp *) NULL, objPtr->internalRep.doubleValue,
1610 len = strlen(buffer);
1612 objPtr->bytes = (char *) ckalloc((unsigned) len + 1);
1613 strcpy(objPtr->bytes, buffer);
1614 objPtr->length = len;
1618 *----------------------------------------------------------------------
1622 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
1623 * Tcl_NewIntObj to create a new integer object end up calling the
1624 * debugging procedure Tcl_DbNewLongObj instead.
1626 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
1627 * calls to Tcl_NewIntObj result in a call to one of the two
1628 * Tcl_NewIntObj implementations below. We provide two implementations
1629 * so that the Tcl core can be compiled to do memory debugging of the
1630 * core even if a client does not request it for itself.
1632 * Integer and long integer objects share the same "integer" type
1633 * implementation. We store all integers as longs and Tcl_GetIntFromObj
1634 * checks whether the current value of the long can be represented by
1638 * The newly created object is returned. This object will have an
1639 * invalid string representation. The returned object has ref count 0.
1644 *----------------------------------------------------------------------
1647 #ifdef TCL_MEM_DEBUG
1648 #undef Tcl_NewIntObj
1651 Tcl_NewIntObj(intValue)
1652 register int intValue; /* Int used to initialize the new object. */
1654 return Tcl_DbNewLongObj((long)intValue, "unknown", 0);
1657 #else /* if not TCL_MEM_DEBUG */
1660 Tcl_NewIntObj(intValue)
1661 register int intValue; /* Int used to initialize the new object. */
1663 register Tcl_Obj *objPtr;
1666 objPtr->bytes = NULL;
1668 objPtr->internalRep.longValue = (long)intValue;
1669 objPtr->typePtr = &tclIntType;
1672 #endif /* if TCL_MEM_DEBUG */
1675 *----------------------------------------------------------------------
1679 * Modify an object to be an integer and to have the specified integer
1686 * The object's old string rep, if any, is freed. Also, any old
1687 * internal rep is freed.
1689 *----------------------------------------------------------------------
1693 Tcl_SetIntObj(objPtr, intValue)
1694 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1695 register int intValue; /* Integer used to set object's value. */
1697 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1699 if (Tcl_IsShared(objPtr)) {
1700 panic("Tcl_SetIntObj called with shared object");
1703 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1704 oldTypePtr->freeIntRepProc(objPtr);
1707 objPtr->internalRep.longValue = (long) intValue;
1708 objPtr->typePtr = &tclIntType;
1709 Tcl_InvalidateStringRep(objPtr);
1713 *----------------------------------------------------------------------
1715 * Tcl_GetIntFromObj --
1717 * Attempt to return an int from the Tcl object "objPtr". If the object
1718 * is not already an int, an attempt will be made to convert it to one.
1720 * Integer and long integer objects share the same "integer" type
1721 * implementation. We store all integers as longs and Tcl_GetIntFromObj
1722 * checks whether the current value of the long can be represented by
1726 * The return value is a standard Tcl object result. If an error occurs
1727 * during conversion or if the long integer held by the object
1728 * can not be represented by an int, an error message is left in
1729 * the interpreter's result unless "interp" is NULL.
1732 * If the object is not already an int, the conversion will free
1733 * any old internal representation.
1735 *----------------------------------------------------------------------
1739 Tcl_GetIntFromObj(interp, objPtr, intPtr)
1740 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1741 register Tcl_Obj *objPtr; /* The object from which to get a int. */
1742 register int *intPtr; /* Place to store resulting int. */
1748 * If the object isn't already an integer of any width, try to
1749 * convert it to one.
1752 if (objPtr->typePtr != &tclIntType && objPtr->typePtr != &tclWideIntType) {
1753 result = SetIntOrWideFromAny(interp, objPtr);
1754 if (result != TCL_OK) {
1760 * Object should now be either int or wide. Get its value.
1763 #ifndef TCL_WIDE_INT_IS_LONG
1764 if (objPtr->typePtr == &tclWideIntType) {
1765 w = objPtr->internalRep.wideValue;
1769 w = Tcl_LongAsWide(objPtr->internalRep.longValue);
1772 if ((LLONG_MAX > UINT_MAX)
1773 && ((w > UINT_MAX) || (w < -(Tcl_WideInt)UINT_MAX))) {
1774 if (interp != NULL) {
1775 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1776 "integer value too large to represent as non-long integer",
1786 *----------------------------------------------------------------------
1790 * Attempts to force the internal representation for a Tcl object
1791 * to tclIntType, specifically.
1794 * The return value is a standard object Tcl result. If an
1795 * error occurs during conversion, an error message is left in
1796 * the interpreter's result unless "interp" is NULL.
1798 *----------------------------------------------------------------------
1802 SetIntFromAny( Tcl_Interp* interp,
1803 /* Tcl interpreter */
1805 /* Pointer to the object to convert */
1809 result = SetIntOrWideFromAny( interp, objPtr );
1810 if ( result != TCL_OK ) {
1813 if ( objPtr->typePtr != &tclIntType ) {
1814 if ( interp != NULL ) {
1815 char *s = "integer value too large to represent";
1816 Tcl_ResetResult(interp);
1817 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
1818 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *) NULL);
1826 *----------------------------------------------------------------------
1828 * SetIntOrWideFromAny --
1830 * Attempt to generate an integer internal form for the Tcl object
1834 * The return value is a standard object Tcl result. If an error occurs
1835 * during conversion, an error message is left in the interpreter's
1836 * result unless "interp" is NULL.
1839 * If no error occurs, an int is stored as "objPtr"s internal
1842 *----------------------------------------------------------------------
1846 SetIntOrWideFromAny(interp, objPtr)
1847 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1848 register Tcl_Obj *objPtr; /* The object to convert. */
1850 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1854 unsigned long newLong;
1859 * Get the string representation. Make it up-to-date if necessary.
1862 p = string = Tcl_GetStringFromObj(objPtr, &length);
1865 * Now parse "objPtr"s string as an int. We use an implementation here
1866 * that doesn't report errors in interp if interp is NULL. Note: use
1867 * strtoul instead of strtol for integer conversions to allow full-size
1868 * unsigned numbers, but don't depend on strtoul to handle sign
1869 * characters; it won't in some implementations.
1873 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
1874 /* Empty loop body. */
1879 } else if (*p == '+') {
1882 if (!isdigit(UCHAR(*p))) {
1884 if (interp != NULL) {
1886 * Must copy string before resetting the result in case a caller
1887 * is trying to convert the interpreter's result to an int.
1891 sprintf(buf, "expected integer but got \"%.50s\"", string);
1892 Tcl_ResetResult(interp);
1893 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1894 TclCheckBadOctal(interp, string);
1898 newLong = strtoul(p, &end, 0);
1902 if (errno == ERANGE) {
1903 if (interp != NULL) {
1904 char *s = "integer value too large to represent";
1905 Tcl_ResetResult(interp);
1906 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
1907 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *) NULL);
1913 * Make sure that the string has no garbage after the end of the int.
1916 while ((end < (string+length))
1917 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
1920 if (end != (string+length)) {
1925 * If the resulting integer will exceed the range of a long,
1926 * put it into a wide instead. (Tcl Bug #868489)
1929 #ifndef TCL_WIDE_INT_IS_LONG
1930 if ((isNegative && newLong > (unsigned long) (LONG_MAX) + 1)
1931 || (!isNegative && newLong > LONG_MAX)) {
1937 * The conversion to int succeeded. Free the old internalRep before
1938 * setting the new one. We do this as late as possible to allow the
1939 * conversion code, in particular Tcl_GetStringFromObj, to use that old
1943 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1944 oldTypePtr->freeIntRepProc(objPtr);
1948 objPtr->internalRep.wideValue =
1949 (isNegative ? -(Tcl_WideInt)newLong : (Tcl_WideInt)newLong);
1950 objPtr->typePtr = &tclWideIntType;
1952 objPtr->internalRep.longValue =
1953 (isNegative ? -(long)newLong : (long)newLong);
1954 objPtr->typePtr = &tclIntType;
1960 *----------------------------------------------------------------------
1962 * UpdateStringOfInt --
1964 * Update the string representation for an integer object.
1965 * Note: This procedure does not free an existing old string rep
1966 * so storage will be lost if this has not already been done.
1972 * The object's string is set to a valid string that results from
1973 * the int-to-string conversion.
1975 *----------------------------------------------------------------------
1979 UpdateStringOfInt(objPtr)
1980 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
1982 char buffer[TCL_INTEGER_SPACE];
1985 len = TclFormatInt(buffer, objPtr->internalRep.longValue);
1987 objPtr->bytes = ckalloc((unsigned) len + 1);
1988 strcpy(objPtr->bytes, buffer);
1989 objPtr->length = len;
1993 *----------------------------------------------------------------------
1997 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
1998 * Tcl_NewLongObj to create a new long integer object end up calling
1999 * the debugging procedure Tcl_DbNewLongObj instead.
2001 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
2002 * calls to Tcl_NewLongObj result in a call to one of the two
2003 * Tcl_NewLongObj implementations below. We provide two implementations
2004 * so that the Tcl core can be compiled to do memory debugging of the
2005 * core even if a client does not request it for itself.
2007 * Integer and long integer objects share the same "integer" type
2008 * implementation. We store all integers as longs and Tcl_GetIntFromObj
2009 * checks whether the current value of the long can be represented by
2013 * The newly created object is returned. This object will have an
2014 * invalid string representation. The returned object has ref count 0.
2019 *----------------------------------------------------------------------
2022 #ifdef TCL_MEM_DEBUG
2023 #undef Tcl_NewLongObj
2026 Tcl_NewLongObj(longValue)
2027 register long longValue; /* Long integer used to initialize the
2030 return Tcl_DbNewLongObj(longValue, "unknown", 0);
2033 #else /* if not TCL_MEM_DEBUG */
2036 Tcl_NewLongObj(longValue)
2037 register long longValue; /* Long integer used to initialize the
2040 register Tcl_Obj *objPtr;
2043 objPtr->bytes = NULL;
2045 objPtr->internalRep.longValue = longValue;
2046 objPtr->typePtr = &tclIntType;
2049 #endif /* if TCL_MEM_DEBUG */
2052 *----------------------------------------------------------------------
2054 * Tcl_DbNewLongObj --
2056 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
2057 * Tcl_NewIntObj and Tcl_NewLongObj to create new integer or
2058 * long integer objects end up calling the debugging procedure
2059 * Tcl_DbNewLongObj instead. We provide two implementations of
2060 * Tcl_DbNewLongObj so that whether the Tcl core is compiled to do
2061 * memory debugging of the core is independent of whether a client
2062 * requests debugging for itself.
2064 * When the core is compiled with TCL_MEM_DEBUG defined,
2065 * Tcl_DbNewLongObj calls Tcl_DbCkalloc directly with the file name and
2066 * line number from its caller. This simplifies debugging since then
2067 * the [memory active] command will report the caller's file name and
2068 * line number when reporting objects that haven't been freed.
2070 * Otherwise, when the core is compiled without TCL_MEM_DEBUG defined,
2071 * this procedure just returns the result of calling Tcl_NewLongObj.
2074 * The newly created long integer object is returned. This object
2075 * will have an invalid string representation. The returned object has
2081 *----------------------------------------------------------------------
2084 #ifdef TCL_MEM_DEBUG
2087 Tcl_DbNewLongObj(longValue, file, line)
2088 register long longValue; /* Long integer used to initialize the
2090 CONST char *file; /* The name of the source file calling this
2091 * procedure; used for debugging. */
2092 int line; /* Line number in the source file; used
2095 register Tcl_Obj *objPtr;
2097 TclDbNewObj(objPtr, file, line);
2098 objPtr->bytes = NULL;
2100 objPtr->internalRep.longValue = longValue;
2101 objPtr->typePtr = &tclIntType;
2105 #else /* if not TCL_MEM_DEBUG */
2108 Tcl_DbNewLongObj(longValue, file, line)
2109 register long longValue; /* Long integer used to initialize the
2111 CONST char *file; /* The name of the source file calling this
2112 * procedure; used for debugging. */
2113 int line; /* Line number in the source file; used
2116 return Tcl_NewLongObj(longValue);
2118 #endif /* TCL_MEM_DEBUG */
2121 *----------------------------------------------------------------------
2125 * Modify an object to be an integer object and to have the specified
2126 * long integer value.
2132 * The object's old string rep, if any, is freed. Also, any old
2133 * internal rep is freed.
2135 *----------------------------------------------------------------------
2139 Tcl_SetLongObj(objPtr, longValue)
2140 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
2141 register long longValue; /* Long integer used to initialize the
2142 * object's value. */
2144 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2146 if (Tcl_IsShared(objPtr)) {
2147 panic("Tcl_SetLongObj called with shared object");
2150 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2151 oldTypePtr->freeIntRepProc(objPtr);
2154 objPtr->internalRep.longValue = longValue;
2155 objPtr->typePtr = &tclIntType;
2156 Tcl_InvalidateStringRep(objPtr);
2160 *----------------------------------------------------------------------
2162 * Tcl_GetLongFromObj --
2164 * Attempt to return an long integer from the Tcl object "objPtr". If
2165 * the object is not already an int object, an attempt will be made to
2166 * convert it to one.
2169 * The return value is a standard Tcl object result. If an error occurs
2170 * during conversion, an error message is left in the interpreter's
2171 * result unless "interp" is NULL.
2174 * If the object is not already an int object, the conversion will free
2175 * any old internal representation.
2177 *----------------------------------------------------------------------
2181 Tcl_GetLongFromObj(interp, objPtr, longPtr)
2182 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2183 register Tcl_Obj *objPtr; /* The object from which to get a long. */
2184 register long *longPtr; /* Place to store resulting long. */
2186 register int result;
2188 if (objPtr->typePtr != &tclIntType && objPtr->typePtr != &tclWideIntType) {
2189 result = SetIntOrWideFromAny(interp, objPtr);
2190 if (result != TCL_OK) {
2195 #ifndef TCL_WIDE_INT_IS_LONG
2196 if (objPtr->typePtr == &tclWideIntType) {
2198 * If the object is already a wide integer, don't convert it.
2199 * This code allows for any integer in the range -ULONG_MAX to
2200 * ULONG_MAX to be converted to a long, ignoring overflow.
2201 * The rule preserves existing semantics for conversion of
2202 * integers on input, but avoids inadvertent demotion of
2203 * wide integers to 32-bit ones in the internal rep.
2206 Tcl_WideInt w = objPtr->internalRep.wideValue;
2207 if (w >= -(Tcl_WideInt)(ULONG_MAX) && w <= (Tcl_WideInt)(ULONG_MAX)) {
2208 *longPtr = Tcl_WideAsLong(w);
2211 if (interp != NULL) {
2212 Tcl_ResetResult(interp);
2213 Tcl_AppendToObj(Tcl_GetObjResult(interp),
2214 "integer value too large to represent", -1);
2221 *longPtr = objPtr->internalRep.longValue;
2226 *----------------------------------------------------------------------
2228 * SetWideIntFromAny --
2230 * Attempt to generate an integer internal form for the Tcl object
2234 * The return value is a standard object Tcl result. If an error occurs
2235 * during conversion, an error message is left in the interpreter's
2236 * result unless "interp" is NULL.
2239 * If no error occurs, an int is stored as "objPtr"s internal
2242 *----------------------------------------------------------------------
2246 SetWideIntFromAny(interp, objPtr)
2247 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2248 register Tcl_Obj *objPtr; /* The object to convert. */
2250 #ifndef TCL_WIDE_INT_IS_LONG
2251 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2255 Tcl_WideInt newWide;
2258 * Get the string representation. Make it up-to-date if necessary.
2261 p = string = Tcl_GetStringFromObj(objPtr, &length);
2264 * Now parse "objPtr"s string as an int. We use an implementation here
2265 * that doesn't report errors in interp if interp is NULL. Note: use
2266 * strtoull instead of strtoll for integer conversions to allow full-size
2267 * unsigned numbers, but don't depend on strtoull to handle sign
2268 * characters; it won't in some implementations.
2272 #ifdef TCL_STRTOUL_SIGN_CHECK
2273 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
2274 /* Empty loop body. */
2278 newWide = -((Tcl_WideInt)strtoull(p, &end, 0));
2279 } else if (*p == '+') {
2281 newWide = strtoull(p, &end, 0);
2284 newWide = strtoull(p, &end, 0);
2288 if (interp != NULL) {
2290 * Must copy string before resetting the result in case a caller
2291 * is trying to convert the interpreter's result to an int.
2295 sprintf(buf, "expected integer but got \"%.50s\"", string);
2296 Tcl_ResetResult(interp);
2297 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
2298 TclCheckBadOctal(interp, string);
2302 if (errno == ERANGE) {
2303 if (interp != NULL) {
2304 char *s = "integer value too large to represent";
2305 Tcl_ResetResult(interp);
2306 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
2307 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *) NULL);
2313 * Make sure that the string has no garbage after the end of the int.
2316 while ((end < (string+length))
2317 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
2320 if (end != (string+length)) {
2325 * The conversion to int succeeded. Free the old internalRep before
2326 * setting the new one. We do this as late as possible to allow the
2327 * conversion code, in particular Tcl_GetStringFromObj, to use that old
2331 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2332 oldTypePtr->freeIntRepProc(objPtr);
2335 objPtr->internalRep.wideValue = newWide;
2337 if (TCL_ERROR == SetIntFromAny(interp, objPtr)) {
2341 objPtr->typePtr = &tclWideIntType;
2346 *----------------------------------------------------------------------
2348 * UpdateStringOfWideInt --
2350 * Update the string representation for a wide integer object.
2351 * Note: This procedure does not free an existing old string rep
2352 * so storage will be lost if this has not already been done.
2358 * The object's string is set to a valid string that results from
2359 * the wideInt-to-string conversion.
2361 *----------------------------------------------------------------------
2364 #ifndef TCL_WIDE_INT_IS_LONG
2366 UpdateStringOfWideInt(objPtr)
2367 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
2369 char buffer[TCL_INTEGER_SPACE+2];
2370 register unsigned len;
2371 register Tcl_WideInt wideVal = objPtr->internalRep.wideValue;
2374 * Note that sprintf will generate a compiler warning under
2375 * Mingw claiming %I64 is an unknown format specifier.
2376 * Just ignore this warning. We can't use %L as the format
2377 * specifier since that gets printed as a 32 bit value.
2379 sprintf(buffer, "%" TCL_LL_MODIFIER "d", wideVal);
2380 len = strlen(buffer);
2381 objPtr->bytes = ckalloc((unsigned) len + 1);
2382 memcpy(objPtr->bytes, buffer, len + 1);
2383 objPtr->length = len;
2385 #endif /* TCL_WIDE_INT_IS_LONG */
2388 *----------------------------------------------------------------------
2390 * Tcl_NewWideIntObj --
2392 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
2393 * Tcl_NewWideIntObj to create a new 64-bit integer object end up calling
2394 * the debugging procedure Tcl_DbNewWideIntObj instead.
2396 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
2397 * calls to Tcl_NewWideIntObj result in a call to one of the two
2398 * Tcl_NewWideIntObj implementations below. We provide two implementations
2399 * so that the Tcl core can be compiled to do memory debugging of the
2400 * core even if a client does not request it for itself.
2403 * The newly created object is returned. This object will have an
2404 * invalid string representation. The returned object has ref count 0.
2409 *----------------------------------------------------------------------
2412 #ifdef TCL_MEM_DEBUG
2413 #undef Tcl_NewWideIntObj
2416 Tcl_NewWideIntObj(wideValue)
2417 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2418 * the new object. */
2420 return Tcl_DbNewWideIntObj(wideValue, "unknown", 0);
2423 #else /* if not TCL_MEM_DEBUG */
2426 Tcl_NewWideIntObj(wideValue)
2427 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2428 * the new object. */
2430 register Tcl_Obj *objPtr;
2433 objPtr->bytes = NULL;
2435 objPtr->internalRep.wideValue = wideValue;
2436 objPtr->typePtr = &tclWideIntType;
2439 #endif /* if TCL_MEM_DEBUG */
2442 *----------------------------------------------------------------------
2444 * Tcl_DbNewWideIntObj --
2446 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
2447 * Tcl_NewWideIntObj to create new wide integer end up calling
2448 * the debugging procedure Tcl_DbNewWideIntObj instead. We
2449 * provide two implementations of Tcl_DbNewWideIntObj so that
2450 * whether the Tcl core is compiled to do memory debugging of the
2451 * core is independent of whether a client requests debugging for
2454 * When the core is compiled with TCL_MEM_DEBUG defined,
2455 * Tcl_DbNewWideIntObj calls Tcl_DbCkalloc directly with the file
2456 * name and line number from its caller. This simplifies
2457 * debugging since then the checkmem command will report the
2458 * caller's file name and line number when reporting objects that
2459 * haven't been freed.
2461 * Otherwise, when the core is compiled without TCL_MEM_DEBUG defined,
2462 * this procedure just returns the result of calling Tcl_NewWideIntObj.
2465 * The newly created wide integer object is returned. This object
2466 * will have an invalid string representation. The returned object has
2472 *----------------------------------------------------------------------
2475 #ifdef TCL_MEM_DEBUG
2478 Tcl_DbNewWideIntObj(wideValue, file, line)
2479 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2480 * the new object. */
2481 CONST char *file; /* The name of the source file
2482 * calling this procedure; used for
2484 int line; /* Line number in the source file;
2485 * used for debugging. */
2487 register Tcl_Obj *objPtr;
2489 TclDbNewObj(objPtr, file, line);
2490 objPtr->bytes = NULL;
2492 objPtr->internalRep.wideValue = wideValue;
2493 objPtr->typePtr = &tclWideIntType;
2497 #else /* if not TCL_MEM_DEBUG */
2500 Tcl_DbNewWideIntObj(wideValue, file, line)
2501 register Tcl_WideInt wideValue; /* Long integer used to initialize
2502 * the new object. */
2503 CONST char *file; /* The name of the source file
2504 * calling this procedure; used for
2506 int line; /* Line number in the source file;
2507 * used for debugging. */
2509 return Tcl_NewWideIntObj(wideValue);
2511 #endif /* TCL_MEM_DEBUG */
2514 *----------------------------------------------------------------------
2516 * Tcl_SetWideIntObj --
2518 * Modify an object to be a wide integer object and to have the
2519 * specified wide integer value.
2525 * The object's old string rep, if any, is freed. Also, any old
2526 * internal rep is freed.
2528 *----------------------------------------------------------------------
2532 Tcl_SetWideIntObj(objPtr, wideValue)
2533 register Tcl_Obj *objPtr; /* Object w. internal rep to init. */
2534 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2535 * the object's value. */
2537 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2539 if (Tcl_IsShared(objPtr)) {
2540 panic("Tcl_SetWideIntObj called with shared object");
2543 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2544 oldTypePtr->freeIntRepProc(objPtr);
2547 objPtr->internalRep.wideValue = wideValue;
2548 objPtr->typePtr = &tclWideIntType;
2549 Tcl_InvalidateStringRep(objPtr);
2553 *----------------------------------------------------------------------
2555 * Tcl_GetWideIntFromObj --
2557 * Attempt to return a wide integer from the Tcl object "objPtr". If
2558 * the object is not already a wide int object, an attempt will be made
2559 * to convert it to one.
2562 * The return value is a standard Tcl object result. If an error occurs
2563 * during conversion, an error message is left in the interpreter's
2564 * result unless "interp" is NULL.
2567 * If the object is not already an int object, the conversion will free
2568 * any old internal representation.
2570 *----------------------------------------------------------------------
2574 Tcl_GetWideIntFromObj(interp, objPtr, wideIntPtr)
2575 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2576 register Tcl_Obj *objPtr; /* Object from which to get a wide int. */
2577 register Tcl_WideInt *wideIntPtr; /* Place to store resulting long. */
2579 register int result;
2581 if (objPtr->typePtr == &tclWideIntType) {
2583 *wideIntPtr = objPtr->internalRep.wideValue;
2586 if (objPtr->typePtr == &tclIntType) {
2588 * This cast is safe; all valid ints/longs are wides.
2591 objPtr->internalRep.wideValue =
2592 Tcl_LongAsWide(objPtr->internalRep.longValue);
2593 objPtr->typePtr = &tclWideIntType;
2596 result = SetWideIntFromAny(interp, objPtr);
2597 if (result == TCL_OK) {
2598 *wideIntPtr = objPtr->internalRep.wideValue;
2604 *----------------------------------------------------------------------
2606 * Tcl_DbIncrRefCount --
2608 * This procedure is normally called when debugging: i.e., when
2609 * TCL_MEM_DEBUG is defined. This checks to see whether or not
2610 * the memory has been freed before incrementing the ref count.
2612 * When TCL_MEM_DEBUG is not defined, this procedure just increments
2613 * the reference count of the object.
2619 * The object's ref count is incremented.
2621 *----------------------------------------------------------------------
2625 Tcl_DbIncrRefCount(objPtr, file, line)
2626 register Tcl_Obj *objPtr; /* The object we are registering a
2628 CONST char *file; /* The name of the source file calling this
2629 * procedure; used for debugging. */
2630 int line; /* Line number in the source file; used
2633 #ifdef TCL_MEM_DEBUG
2634 if (objPtr->refCount == 0x61616161) {
2635 fprintf(stderr, "file = %s, line = %d\n", file, line);
2637 panic("Trying to increment refCount of previously disposed object.");
2640 ++(objPtr)->refCount;
2644 *----------------------------------------------------------------------
2646 * Tcl_DbDecrRefCount --
2648 * This procedure is normally called when debugging: i.e., when
2649 * TCL_MEM_DEBUG is defined. This checks to see whether or not
2650 * the memory has been freed before decrementing the ref count.
2652 * When TCL_MEM_DEBUG is not defined, this procedure just decrements
2653 * the reference count of the object.
2659 * The object's ref count is incremented.
2661 *----------------------------------------------------------------------
2665 Tcl_DbDecrRefCount(objPtr, file, line)
2666 register Tcl_Obj *objPtr; /* The object we are releasing a reference
2668 CONST char *file; /* The name of the source file calling this
2669 * procedure; used for debugging. */
2670 int line; /* Line number in the source file; used
2673 #ifdef TCL_MEM_DEBUG
2674 if (objPtr->refCount == 0x61616161) {
2675 fprintf(stderr, "file = %s, line = %d\n", file, line);
2677 panic("Trying to decrement refCount of previously disposed object.");
2680 if (--(objPtr)->refCount <= 0) {
2686 *----------------------------------------------------------------------
2690 * This procedure is normally called when debugging: i.e., when
2691 * TCL_MEM_DEBUG is defined. It tests whether the object has a ref
2692 * count greater than one.
2694 * When TCL_MEM_DEBUG is not defined, this procedure just tests
2695 * if the object has a ref count greater than one.
2703 *----------------------------------------------------------------------
2707 Tcl_DbIsShared(objPtr, file, line)
2708 register Tcl_Obj *objPtr; /* The object to test for being shared. */
2709 CONST char *file; /* The name of the source file calling this
2710 * procedure; used for debugging. */
2711 int line; /* Line number in the source file; used
2714 #ifdef TCL_MEM_DEBUG
2715 if (objPtr->refCount == 0x61616161) {
2716 fprintf(stderr, "file = %s, line = %d\n", file, line);
2718 panic("Trying to check whether previously disposed object is shared.");
2721 #ifdef TCL_COMPILE_STATS
2722 Tcl_MutexLock(&tclObjMutex);
2723 if ((objPtr)->refCount <= 1) {
2725 } else if ((objPtr)->refCount < TCL_MAX_SHARED_OBJ_STATS) {
2726 tclObjsShared[(objPtr)->refCount]++;
2730 Tcl_MutexUnlock(&tclObjMutex);
2732 return ((objPtr)->refCount > 1);
2736 *----------------------------------------------------------------------
2738 * Tcl_InitObjHashTable --
2740 * Given storage for a hash table, set up the fields to prepare
2741 * the hash table for use, the keys are Tcl_Obj *.
2747 * TablePtr is now ready to be passed to Tcl_FindHashEntry and
2748 * Tcl_CreateHashEntry.
2750 *----------------------------------------------------------------------
2754 Tcl_InitObjHashTable(tablePtr)
2755 register Tcl_HashTable *tablePtr; /* Pointer to table record, which
2756 * is supplied by the caller. */
2758 Tcl_InitCustomHashTable(tablePtr, TCL_CUSTOM_PTR_KEYS,
2759 &tclObjHashKeyType);
2763 *----------------------------------------------------------------------
2767 * Allocate space for a Tcl_HashEntry containing the Tcl_Obj * key.
2770 * The return value is a pointer to the created entry.
2773 * Increments the reference count on the object.
2775 *----------------------------------------------------------------------
2778 static Tcl_HashEntry *
2779 AllocObjEntry(tablePtr, keyPtr)
2780 Tcl_HashTable *tablePtr; /* Hash table. */
2781 VOID *keyPtr; /* Key to store in the hash table entry. */
2783 Tcl_Obj *objPtr = (Tcl_Obj *) keyPtr;
2784 Tcl_HashEntry *hPtr;
2786 hPtr = (Tcl_HashEntry *) ckalloc((unsigned) (sizeof(Tcl_HashEntry)));
2787 hPtr->key.oneWordValue = (char *) objPtr;
2788 Tcl_IncrRefCount (objPtr);
2794 *----------------------------------------------------------------------
2798 * Compares two Tcl_Obj * keys.
2801 * The return value is 0 if they are different and 1 if they are
2807 *----------------------------------------------------------------------
2811 CompareObjKeys(keyPtr, hPtr)
2812 VOID *keyPtr; /* New key to compare. */
2813 Tcl_HashEntry *hPtr; /* Existing key to compare. */
2815 Tcl_Obj *objPtr1 = (Tcl_Obj *) keyPtr;
2816 Tcl_Obj *objPtr2 = (Tcl_Obj *) hPtr->key.oneWordValue;
2817 register CONST char *p1, *p2;
2818 register int l1, l2;
2821 * If the object pointers are the same then they match.
2823 if (objPtr1 == objPtr2) {
2828 * Don't use Tcl_GetStringFromObj as it would prevent l1 and l2 being
2831 p1 = TclGetString(objPtr1);
2832 l1 = objPtr1->length;
2833 p2 = TclGetString(objPtr2);
2834 l2 = objPtr2->length;
2837 * Only compare if the string representations are of the same length.
2840 for (;; p1++, p2++, l1--) {
2854 *----------------------------------------------------------------------
2858 * Frees space for a Tcl_HashEntry containing the Tcl_Obj * key.
2861 * The return value is a pointer to the created entry.
2864 * Decrements the reference count of the object.
2866 *----------------------------------------------------------------------
2871 Tcl_HashEntry *hPtr; /* Hash entry to free. */
2873 Tcl_Obj *objPtr = (Tcl_Obj *) hPtr->key.oneWordValue;
2875 Tcl_DecrRefCount (objPtr);
2876 ckfree ((char *) hPtr);
2880 *----------------------------------------------------------------------
2884 * Compute a one-word summary of the string representation of the
2885 * Tcl_Obj, which can be used to generate a hash index.
2888 * The return value is a one-word summary of the information in
2889 * the string representation of the Tcl_Obj.
2894 *----------------------------------------------------------------------
2898 HashObjKey(tablePtr, keyPtr)
2899 Tcl_HashTable *tablePtr; /* Hash table. */
2900 VOID *keyPtr; /* Key from which to compute hash value. */
2902 Tcl_Obj *objPtr = (Tcl_Obj *) keyPtr;
2903 CONST char *string = TclGetString(objPtr);
2904 int length = objPtr->length;
2905 unsigned int result;
2909 * I tried a zillion different hash functions and asked many other
2910 * people for advice. Many people had their own favorite functions,
2911 * all different, but no-one had much idea why they were good ones.
2912 * I chose the one below (multiply by 9 and add new character)
2913 * because of the following reasons:
2915 * 1. Multiplying by 10 is perfect for keys that are decimal strings,
2916 * and multiplying by 9 is just about as good.
2917 * 2. Times-9 is (shift-left-3) plus (old). This means that each
2918 * character's bits hang around in the low-order bits of the
2919 * hash value for ever, plus they spread fairly rapidly up to
2920 * the high-order bits to fill out the hash value. This seems
2921 * works well both for decimal and non-decimal strings.
2925 for (i=0 ; i<length ; i++) {
2926 result += (result<<3) + string[i];
2932 *----------------------------------------------------------------------
2934 * Tcl_GetCommandFromObj --
2936 * Returns the command specified by the name in a Tcl_Obj.
2939 * Returns a token for the command if it is found. Otherwise, if it
2940 * can't be found or there is an error, returns NULL.
2943 * May update the internal representation for the object, caching
2944 * the command reference so that the next time this procedure is
2945 * called with the same object, the command can be found quickly.
2947 *----------------------------------------------------------------------
2951 Tcl_GetCommandFromObj(interp, objPtr)
2952 Tcl_Interp *interp; /* The interpreter in which to resolve the
2953 * command and to report errors. */
2954 register Tcl_Obj *objPtr; /* The object containing the command's
2955 * name. If the name starts with "::", will
2956 * be looked up in global namespace. Else,
2957 * looked up first in the current namespace,
2958 * then in global namespace. */
2960 Interp *iPtr = (Interp *) interp;
2961 register ResolvedCmdName *resPtr;
2962 register Command *cmdPtr;
2963 Namespace *currNsPtr;
2965 CallFrame *savedFramePtr;
2969 * If the variable name is fully qualified, do as if the lookup were
2970 * done from the global namespace; this helps avoid repeated lookups
2971 * of fully qualified names. It costs close to nothing, and may be very
2972 * helpful for OO applications which pass along a command name ("this"),
2976 savedFramePtr = iPtr->varFramePtr;
2977 name = Tcl_GetString(objPtr);
2978 if ((*name++ == ':') && (*name == ':')) {
2979 iPtr->varFramePtr = NULL;
2983 * Get the internal representation, converting to a command type if
2984 * needed. The internal representation is a ResolvedCmdName that points
2985 * to the actual command.
2988 if (objPtr->typePtr != &tclCmdNameType) {
2989 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
2990 if (result != TCL_OK) {
2991 iPtr->varFramePtr = savedFramePtr;
2992 return (Tcl_Command) NULL;
2995 resPtr = (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
2998 * Get the current namespace.
3001 if (iPtr->varFramePtr != NULL) {
3002 currNsPtr = iPtr->varFramePtr->nsPtr;
3004 currNsPtr = iPtr->globalNsPtr;
3008 * Check the context namespace and the namespace epoch of the resolved
3009 * symbol to make sure that it is fresh. If not, then force another
3010 * conversion to the command type, to discard the old rep and create a
3011 * new one. Note that we verify that the namespace id of the context
3012 * namespace is the same as the one we cached; this insures that the
3013 * namespace wasn't deleted and a new one created at the same address
3014 * with the same command epoch.
3018 if ((resPtr != NULL)
3019 && (resPtr->refNsPtr == currNsPtr)
3020 && (resPtr->refNsId == currNsPtr->nsId)
3021 && (resPtr->refNsCmdEpoch == currNsPtr->cmdRefEpoch)) {
3022 cmdPtr = resPtr->cmdPtr;
3023 if (cmdPtr->cmdEpoch != resPtr->cmdEpoch) {
3028 if (cmdPtr == NULL) {
3029 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
3030 if (result != TCL_OK) {
3031 iPtr->varFramePtr = savedFramePtr;
3032 return (Tcl_Command) NULL;
3034 resPtr = (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
3035 if (resPtr != NULL) {
3036 cmdPtr = resPtr->cmdPtr;
3039 iPtr->varFramePtr = savedFramePtr;
3040 return (Tcl_Command) cmdPtr;
3044 *----------------------------------------------------------------------
3046 * TclSetCmdNameObj --
3048 * Modify an object to be an CmdName object that refers to the argument
3049 * Command structure.
3055 * The object's old internal rep is freed. It's string rep is not
3056 * changed. The refcount in the Command structure is incremented to
3057 * keep it from being freed if the command is later deleted until
3058 * TclExecuteByteCode has a chance to recognize that it was deleted.
3060 *----------------------------------------------------------------------
3064 TclSetCmdNameObj(interp, objPtr, cmdPtr)
3065 Tcl_Interp *interp; /* Points to interpreter containing command
3066 * that should be cached in objPtr. */
3067 register Tcl_Obj *objPtr; /* Points to Tcl object to be changed to
3068 * a CmdName object. */
3069 Command *cmdPtr; /* Points to Command structure that the
3070 * CmdName object should refer to. */
3072 Interp *iPtr = (Interp *) interp;
3073 register ResolvedCmdName *resPtr;
3074 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
3075 register Namespace *currNsPtr;
3077 if (oldTypePtr == &tclCmdNameType) {
3082 * Get the current namespace.
3085 if (iPtr->varFramePtr != NULL) {
3086 currNsPtr = iPtr->varFramePtr->nsPtr;
3088 currNsPtr = iPtr->globalNsPtr;
3092 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
3093 resPtr->cmdPtr = cmdPtr;
3094 resPtr->refNsPtr = currNsPtr;
3095 resPtr->refNsId = currNsPtr->nsId;
3096 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
3097 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
3098 resPtr->refCount = 1;
3100 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
3101 oldTypePtr->freeIntRepProc(objPtr);
3103 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3104 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
3105 objPtr->typePtr = &tclCmdNameType;
3109 *----------------------------------------------------------------------
3111 * FreeCmdNameInternalRep --
3113 * Frees the resources associated with a cmdName object's internal
3120 * Decrements the ref count of any cached ResolvedCmdName structure
3121 * pointed to by the cmdName's internal representation. If this is
3122 * the last use of the ResolvedCmdName, it is freed. This in turn
3123 * decrements the ref count of the Command structure pointed to by
3124 * the ResolvedSymbol, which may free the Command structure.
3126 *----------------------------------------------------------------------
3130 FreeCmdNameInternalRep(objPtr)
3131 register Tcl_Obj *objPtr; /* CmdName object with internal
3132 * representation to free. */
3134 register ResolvedCmdName *resPtr =
3135 (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
3137 if (resPtr != NULL) {
3139 * Decrement the reference count of the ResolvedCmdName structure.
3140 * If there are no more uses, free the ResolvedCmdName structure.
3144 if (resPtr->refCount == 0) {
3146 * Now free the cached command, unless it is still in its
3147 * hash table or if there are other references to it
3148 * from other cmdName objects.
3151 Command *cmdPtr = resPtr->cmdPtr;
3152 TclCleanupCommand(cmdPtr);
3153 ckfree((char *) resPtr);
3159 *----------------------------------------------------------------------
3161 * DupCmdNameInternalRep --
3163 * Initialize the internal representation of an cmdName Tcl_Obj to a
3164 * copy of the internal representation of an existing cmdName object.
3170 * "copyPtr"s internal rep is set to point to the ResolvedCmdName
3171 * structure corresponding to "srcPtr"s internal rep. Increments the
3172 * ref count of the ResolvedCmdName structure pointed to by the
3173 * cmdName's internal representation.
3175 *----------------------------------------------------------------------
3179 DupCmdNameInternalRep(srcPtr, copyPtr)
3180 Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
3181 register Tcl_Obj *copyPtr; /* Object with internal rep to set. */
3183 register ResolvedCmdName *resPtr =
3184 (ResolvedCmdName *) srcPtr->internalRep.twoPtrValue.ptr1;
3186 copyPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3187 copyPtr->internalRep.twoPtrValue.ptr2 = NULL;
3188 if (resPtr != NULL) {
3191 copyPtr->typePtr = &tclCmdNameType;
3195 *----------------------------------------------------------------------
3197 * SetCmdNameFromAny --
3199 * Generate an cmdName internal form for the Tcl object "objPtr".
3202 * The return value is a standard Tcl result. The conversion always
3203 * succeeds and TCL_OK is returned.
3206 * A pointer to a ResolvedCmdName structure that holds a cached pointer
3207 * to the command with a name that matches objPtr's string rep is
3208 * stored as objPtr's internal representation. This ResolvedCmdName
3209 * pointer will be NULL if no matching command was found. The ref count
3210 * of the cached Command's structure (if any) is also incremented.
3212 *----------------------------------------------------------------------
3216 SetCmdNameFromAny(interp, objPtr)
3217 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
3218 register Tcl_Obj *objPtr; /* The object to convert. */
3220 Interp *iPtr = (Interp *) interp;
3223 register Command *cmdPtr;
3224 Namespace *currNsPtr;
3225 register ResolvedCmdName *resPtr;
3228 * Get "objPtr"s string representation. Make it up-to-date if necessary.
3231 name = objPtr->bytes;
3233 name = Tcl_GetString(objPtr);
3237 * Find the Command structure, if any, that describes the command called
3238 * "name". Build a ResolvedCmdName that holds a cached pointer to this
3239 * Command, and bump the reference count in the referenced Command
3240 * structure. A Command structure will not be deleted as long as it is
3241 * referenced from a CmdName object.
3244 cmd = Tcl_FindCommand(interp, name, (Tcl_Namespace *) NULL,
3246 cmdPtr = (Command *) cmd;
3247 if (cmdPtr != NULL) {
3249 * Get the current namespace.
3252 if (iPtr->varFramePtr != NULL) {
3253 currNsPtr = iPtr->varFramePtr->nsPtr;
3255 currNsPtr = iPtr->globalNsPtr;
3259 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
3260 resPtr->cmdPtr = cmdPtr;
3261 resPtr->refNsPtr = currNsPtr;
3262 resPtr->refNsId = currNsPtr->nsId;
3263 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
3264 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
3265 resPtr->refCount = 1;
3267 resPtr = NULL; /* no command named "name" was found */
3271 * Free the old internalRep before setting the new one. We do this as
3272 * late as possible to allow the conversion code, in particular
3273 * GetStringFromObj, to use that old internalRep. If no Command
3274 * structure was found, leave NULL as the cached value.
3277 if ((objPtr->typePtr != NULL)
3278 && (objPtr->typePtr->freeIntRepProc != NULL)) {
3279 objPtr->typePtr->freeIntRepProc(objPtr);
3282 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3283 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
3284 objPtr->typePtr = &tclCmdNameType;