Debian Codegraph @

pTk/mTk/tclGeneric/tclObj.c

raw symbols SCIP
1 /*
2 * tclObj.c --
3 *
4 * This file contains Tcl object-related procedures that are used by
5 * many Tcl commands.
6 *
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
8 * Copyright (c) 1999 by Scriptics Corporation.
9 * Copyright (c) 2001 by ActiveState Corporation.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tclObj.c,v 1.42.2.4 2003/05/23 21:29:11 dgp Exp $
15 */
16
17 #include "tclInt.h"
18 #include "tclCompile.h"
19 #include "tclPort.h"
20
21 /*
22 * Table of all object types.
23 */
24
25 static Tcl_HashTable typeTable;
26 static int typeTableInitialized = 0; /* 0 means not yet initialized. */
27 TCL_DECLARE_MUTEX(tableMutex)
28
29 /*
30 * Head of the list of free Tcl_Obj structs we maintain.
31 */
32
33 Tcl_Obj *tclFreeObjList = NULL;
34
35 /*
36 * The object allocator is single threaded. This mutex is referenced
37 * by the TclNewObj macro, however, so must be visible.
38 */
39
40 #ifdef TCL_THREADS
41 Tcl_Mutex tclObjMutex;
42 #endif
43
44 /*
45 * Pointer to a heap-allocated string of length zero that the Tcl core uses
46 * as the value of an empty string representation for an object. This value
47 * is shared by all new objects allocated by Tcl_NewObj.
48 */
49
50 char tclEmptyString = '\0';
51 char *tclEmptyStringRep = &tclEmptyString;
52
53 /*
54 * Prototypes for procedures defined later in this file:
55 */
56
57 static int SetBooleanFromAny _ANSI_ARGS_((Tcl_Interp *interp,
58 Tcl_Obj *objPtr));
59 static int SetDoubleFromAny _ANSI_ARGS_((Tcl_Interp *interp,
60 Tcl_Obj *objPtr));
61 static int SetIntFromAny _ANSI_ARGS_((Tcl_Interp *interp,
62 Tcl_Obj *objPtr));
63 static void UpdateStringOfBoolean _ANSI_ARGS_((Tcl_Obj *objPtr));
64 static void UpdateStringOfDouble _ANSI_ARGS_((Tcl_Obj *objPtr));
65 static void UpdateStringOfInt _ANSI_ARGS_((Tcl_Obj *objPtr));
66 static int SetWideIntFromAny _ANSI_ARGS_((Tcl_Interp *interp,
67 Tcl_Obj *objPtr));
68 #ifndef TCL_WIDE_INT_IS_LONG
69 static void UpdateStringOfWideInt _ANSI_ARGS_((Tcl_Obj *objPtr));
70 #endif
71
72 /*
73 * Prototypes for the array hash key methods.
74 */
75
76 static Tcl_HashEntry * AllocObjEntry _ANSI_ARGS_((
77 Tcl_HashTable *tablePtr, VOID *keyPtr));
78 static int CompareObjKeys _ANSI_ARGS_((
79 VOID *keyPtr, Tcl_HashEntry *hPtr));
80 static void FreeObjEntry _ANSI_ARGS_((
81 Tcl_HashEntry *hPtr));
82 static unsigned int HashObjKey _ANSI_ARGS_((
83 Tcl_HashTable *tablePtr,
84 VOID *keyPtr));
85
86 /*
87 * Prototypes for the CommandName object type.
88 */
89
90 static void DupCmdNameInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr,
91 Tcl_Obj *copyPtr));
92 static void FreeCmdNameInternalRep _ANSI_ARGS_((
93 Tcl_Obj *objPtr));
94 static int SetCmdNameFromAny _ANSI_ARGS_((Tcl_Interp *interp,
95 Tcl_Obj *objPtr));
96
97
98 /*
99 * The structures below defines the Tcl object types defined in this file by
100 * means of procedures that can be invoked by generic object code. See also
101 * tclStringObj.c, tclListObj.c, tclByteCode.c for other type manager
102 * implementations.
103 */
104
105 Tcl_ObjType tclBooleanType = {
106 "boolean", /* name */
107 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
108 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
109 UpdateStringOfBoolean, /* updateStringProc */
110 SetBooleanFromAny /* setFromAnyProc */
111 };
112
113 Tcl_ObjType tclDoubleType = {
114 "double", /* name */
115 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
116 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
117 UpdateStringOfDouble, /* updateStringProc */
118 SetDoubleFromAny /* setFromAnyProc */
119 };
120
121 Tcl_ObjType tclIntType = {
122 "int", /* name */
123 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
124 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
125 UpdateStringOfInt, /* updateStringProc */
126 SetIntFromAny /* setFromAnyProc */
127 };
128
129 Tcl_ObjType tclWideIntType = {
130 "wideInt", /* name */
131 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
132 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
133 #ifdef TCL_WIDE_INT_IS_LONG
134 UpdateStringOfInt, /* updateStringProc */
135 #else /* !TCL_WIDE_INT_IS_LONG */
136 UpdateStringOfWideInt, /* updateStringProc */
137 #endif
138 SetWideIntFromAny /* setFromAnyProc */
139 };
140
141 /*
142 * The structure below defines the Tcl obj hash key type.
143 */
144 Tcl_HashKeyType tclObjHashKeyType = {
145 TCL_HASH_KEY_TYPE_VERSION, /* version */
146 0, /* flags */
147 HashObjKey, /* hashKeyProc */
148 CompareObjKeys, /* compareKeysProc */
149 AllocObjEntry, /* allocEntryProc */
150 FreeObjEntry /* freeEntryProc */
151 };
152
153 /*
154 * The structure below defines the command name Tcl object type by means of
155 * procedures that can be invoked by generic object code. Objects of this
156 * type cache the Command pointer that results from looking up command names
157 * in the command hashtable. Such objects appear as the zeroth ("command
158 * name") argument in a Tcl command.
159 *
160 * NOTE: the ResolvedCmdName that gets cached is stored in the
161 * twoPtrValue.ptr1 field, and the twoPtrValue.ptr2 field is unused.
162 * You might think you could use the simpler otherValuePtr field to
163 * store the single ResolvedCmdName pointer, but DO NOT DO THIS. It
164 * seems that some extensions use the second internal pointer field
165 * of the twoPtrValue field for their own purposes.
166 */
167
168 static Tcl_ObjType tclCmdNameType = {
169 "cmdName", /* name */
170 FreeCmdNameInternalRep, /* freeIntRepProc */
171 DupCmdNameInternalRep, /* dupIntRepProc */
172 (Tcl_UpdateStringProc *) NULL, /* updateStringProc */
173 SetCmdNameFromAny /* setFromAnyProc */
174 };
175
176
177 /*
178 * Structure containing a cached pointer to a command that is the result
179 * of resolving the command's name in some namespace. It is the internal
180 * representation for a cmdName object. It contains the pointer along
181 * with some information that is used to check the pointer's validity.
182 */
183
184 typedef struct ResolvedCmdName {
185 Command *cmdPtr; /* A cached Command pointer. */
186 Namespace *refNsPtr; /* Points to the namespace containing the
187 * reference (not the namespace that
188 * contains the referenced command). */
189 long refNsId; /* refNsPtr's unique namespace id. Used to
190 * verify that refNsPtr is still valid
191 * (e.g., it's possible that the cmd's
192 * containing namespace was deleted and a
193 * new one created at the same address). */
194 int refNsCmdEpoch; /* Value of the referencing namespace's
195 * cmdRefEpoch when the pointer was cached.
196 * Before using the cached pointer, we check
197 * if the namespace's epoch was incremented;
198 * if so, this cached pointer is invalid. */
199 int cmdEpoch; /* Value of the command's cmdEpoch when this
200 * pointer was cached. Before using the
201 * cached pointer, we check if the cmd's
202 * epoch was incremented; if so, the cmd was
203 * renamed, deleted, hidden, or exposed, and
204 * so the pointer is invalid. */
205 int refCount; /* Reference count: 1 for each cmdName
206 * object that has a pointer to this
207 * ResolvedCmdName structure as its internal
208 * rep. This structure can be freed when
209 * refCount becomes zero. */
210 } ResolvedCmdName;
211
212
213 /*
214 *-------------------------------------------------------------------------
215 *
216 * TclInitObjectSubsystem --
217 *
218 * This procedure is invoked to perform once-only initialization of
219 * the type table. It also registers the object types defined in
220 * this file.
221 *
222 * Results:
223 * None.
224 *
225 * Side effects:
226 * Initializes the table of defined object types "typeTable" with
227 * builtin object types defined in this file.
228 *
229 *-------------------------------------------------------------------------
230 */
231
232 void
233 TclInitObjSubsystem()
234 {
235 Tcl_MutexLock(&tableMutex);
236 typeTableInitialized = 1;
237 Tcl_InitHashTable(&typeTable, TCL_STRING_KEYS);
238 Tcl_MutexUnlock(&tableMutex);
239
240 Tcl_RegisterObjType(&tclBooleanType);
241 Tcl_RegisterObjType(&tclByteArrayType);
242 Tcl_RegisterObjType(&tclDoubleType);
243 Tcl_RegisterObjType(&tclEndOffsetType);
244 Tcl_RegisterObjType(&tclIntType);
245 Tcl_RegisterObjType(&tclWideIntType);
246 Tcl_RegisterObjType(&tclStringType);
247 Tcl_RegisterObjType(&tclListType);
248 Tcl_RegisterObjType(&tclByteCodeType);
249 Tcl_RegisterObjType(&tclProcBodyType);
250 Tcl_RegisterObjType(&tclArraySearchType);
251 Tcl_RegisterObjType(&tclIndexType);
252 Tcl_RegisterObjType(&tclNsNameType);
253 Tcl_RegisterObjType(&tclCmdNameType);
254
255 #ifdef TCL_COMPILE_STATS
256 Tcl_MutexLock(&tclObjMutex);
257 tclObjsAlloced = 0;
258 tclObjsFreed = 0;
259 {
260 int i;
261 for (i = 0; i < TCL_MAX_SHARED_OBJ_STATS; i++) {
262 tclObjsShared[i] = 0;
263 }
264 }
265 Tcl_MutexUnlock(&tclObjMutex);
266 #endif
267 }
268
269 /*
270 *----------------------------------------------------------------------
271 *
272 * TclFinalizeCompExecEnv --
273 *
274 * This procedure is called by Tcl_Finalize to clean up the Tcl
275 * compilation and execution environment so it can later be properly
276 * reinitialized.
277 *
278 * Results:
279 * None.
280 *
281 * Side effects:
282 * Cleans up the compilation and execution environment
283 *
284 *----------------------------------------------------------------------
285 */
286
287 void
288 TclFinalizeCompExecEnv()
289 {
290 Tcl_MutexLock(&tableMutex);
291 if (typeTableInitialized) {
292 Tcl_DeleteHashTable(&typeTable);
293 typeTableInitialized = 0;
294 }
295 Tcl_MutexUnlock(&tableMutex);
296 Tcl_MutexLock(&tclObjMutex);
297 tclFreeObjList = NULL;
298 Tcl_MutexUnlock(&tclObjMutex);
299
300 TclFinalizeCompilation();
301 TclFinalizeExecution();
302 }
303
304 /*
305 *--------------------------------------------------------------
306 *
307 * Tcl_RegisterObjType --
308 *
309 * This procedure is called to register a new Tcl object type
310 * in the table of all object types supported by Tcl.
311 *
312 * Results:
313 * None.
314 *
315 * Side effects:
316 * The type is registered in the Tcl type table. If there was already
317 * a type with the same name as in typePtr, it is replaced with the
318 * new type.
319 *
320 *--------------------------------------------------------------
321 */
322
323 void
324 Tcl_RegisterObjType(typePtr)
325 Tcl_ObjType *typePtr; /* Information about object type;
326 * storage must be statically
327 * allocated (must live forever). */
328 {
329 register Tcl_HashEntry *hPtr;
330 int new;
331
332 /*
333 * If there's already an object type with the given name, remove it.
334 */
335 Tcl_MutexLock(&tableMutex);
336 hPtr = Tcl_FindHashEntry(&typeTable, typePtr->name);
337 if (hPtr != (Tcl_HashEntry *) NULL) {
338 Tcl_DeleteHashEntry(hPtr);
339 }
340
341 /*
342 * Now insert the new object type.
343 */
344
345 hPtr = Tcl_CreateHashEntry(&typeTable, typePtr->name, &new);
346 if (new) {
347 Tcl_SetHashValue(hPtr, typePtr);
348 }
349 Tcl_MutexUnlock(&tableMutex);
350 }
351
352 /*
353 *----------------------------------------------------------------------
354 *
355 * Tcl_AppendAllObjTypes --
356 *
357 * This procedure appends onto the argument object the name of each
358 * object type as a list element. This includes the builtin object
359 * types (e.g. int, list) as well as those added using
360 * Tcl_NewObj. These names can be used, for example, with
361 * Tcl_GetObjType to get pointers to the corresponding Tcl_ObjType
362 * structures.
363 *
364 * Results:
365 * The return value is normally TCL_OK; in this case the object
366 * referenced by objPtr has each type name appended to it. If an
367 * error occurs, TCL_ERROR is returned and the interpreter's result
368 * holds an error message.
369 *
370 * Side effects:
371 * If necessary, the object referenced by objPtr is converted into
372 * a list object.
373 *
374 *----------------------------------------------------------------------
375 */
376
377 int
378 Tcl_AppendAllObjTypes(interp, objPtr)
379 Tcl_Interp *interp; /* Interpreter used for error reporting. */
380 Tcl_Obj *objPtr; /* Points to the Tcl object onto which the
381 * name of each registered type is appended
382 * as a list element. */
383 {
384 register Tcl_HashEntry *hPtr;
385 Tcl_HashSearch search;
386 Tcl_ObjType *typePtr;
387 int result;
388
389 /*
390 * This code assumes that types names do not contain embedded NULLs.
391 */
392
393 Tcl_MutexLock(&tableMutex);
394 for (hPtr = Tcl_FirstHashEntry(&typeTable, &search);
395 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
396 typePtr = (Tcl_ObjType *) Tcl_GetHashValue(hPtr);
397 result = Tcl_ListObjAppendElement(interp, objPtr,
398 Tcl_NewStringObj(typePtr->name, -1));
399 if (result == TCL_ERROR) {
400 Tcl_MutexUnlock(&tableMutex);
401 return result;
402 }
403 }
404 Tcl_MutexUnlock(&tableMutex);
405 return TCL_OK;
406 }
407
408 /*
409 *----------------------------------------------------------------------
410 *
411 * Tcl_GetObjType --
412 *
413 * This procedure looks up an object type by name.
414 *
415 * Results:
416 * If an object type with name matching "typeName" is found, a pointer
417 * to its Tcl_ObjType structure is returned; otherwise, NULL is
418 * returned.
419 *
420 * Side effects:
421 * None.
422 *
423 *----------------------------------------------------------------------
424 */
425
426 Tcl_ObjType *
427 Tcl_GetObjType(typeName)
428 CONST char *typeName; /* Name of Tcl object type to look up. */
429 {
430 register Tcl_HashEntry *hPtr;
431 Tcl_ObjType *typePtr;
432
433 Tcl_MutexLock(&tableMutex);
434 hPtr = Tcl_FindHashEntry(&typeTable, typeName);
435 if (hPtr != (Tcl_HashEntry *) NULL) {
436 typePtr = (Tcl_ObjType *) Tcl_GetHashValue(hPtr);
437 Tcl_MutexUnlock(&tableMutex);
438 return typePtr;
439 }
440 Tcl_MutexUnlock(&tableMutex);
441 return NULL;
442 }
443
444 /*
445 *----------------------------------------------------------------------
446 *
447 * Tcl_ConvertToType --
448 *
449 * Convert the Tcl object "objPtr" to have type "typePtr" if possible.
450 *
451 * Results:
452 * The return value is TCL_OK on success and TCL_ERROR on failure. If
453 * TCL_ERROR is returned, then the interpreter's result contains an
454 * error message unless "interp" is NULL. Passing a NULL "interp"
455 * allows this procedure to be used as a test whether the conversion
456 * could be done (and in fact was done).
457 *
458 * Side effects:
459 * Any internal representation for the old type is freed.
460 *
461 *----------------------------------------------------------------------
462 */
463
464 int
465 Tcl_ConvertToType(interp, objPtr, typePtr)
466 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
467 Tcl_Obj *objPtr; /* The object to convert. */
468 Tcl_ObjType *typePtr; /* The target type. */
469 {
470 if (objPtr->typePtr == typePtr) {
471 return TCL_OK;
472 }
473
474 /*
475 * Use the target type's Tcl_SetFromAnyProc to set "objPtr"s internal
476 * form as appropriate for the target type. This frees the old internal
477 * representation.
478 */
479
480 return typePtr->setFromAnyProc(interp, objPtr);
481 }
482
483 /*
484 *----------------------------------------------------------------------
485 *
486 * Tcl_NewObj --
487 *
488 * This procedure is normally called when not debugging: i.e., when
489 * TCL_MEM_DEBUG is not defined. It creates new Tcl objects that denote
490 * the empty string. These objects have a NULL object type and NULL
491 * string representation byte pointer. Type managers call this routine
492 * to allocate new objects that they further initialize.
493 *
494 * When TCL_MEM_DEBUG is defined, this procedure just returns the
495 * result of calling the debugging version Tcl_DbNewObj.
496 *
497 * Results:
498 * The result is a newly allocated object that represents the empty
499 * string. The new object's typePtr is set NULL and its ref count
500 * is set to 0.
501 *
502 * Side effects:
503 * If compiling with TCL_COMPILE_STATS, this procedure increments
504 * the global count of allocated objects (tclObjsAlloced).
505 *
506 *----------------------------------------------------------------------
507 */
508
509 #ifdef TCL_MEM_DEBUG
510 #undef Tcl_NewObj
511
512 Tcl_Obj *
513 Tcl_NewObj()
514 {
515 return Tcl_DbNewObj("unknown", 0);
516 }
517
518 #else /* if not TCL_MEM_DEBUG */
519
520 Tcl_Obj *
521 Tcl_NewObj()
522 {
523 register Tcl_Obj *objPtr;
524
525 /*
526 * Use the macro defined in tclInt.h - it will use the
527 * correct allocator.
528 */
529
530 TclNewObj(objPtr);
531 return objPtr;
532 }
533 #endif /* TCL_MEM_DEBUG */
534
535 /*
536 *----------------------------------------------------------------------
537 *
538 * Tcl_DbNewObj --
539 *
540 * This procedure is normally called when debugging: i.e., when
541 * TCL_MEM_DEBUG is defined. It creates new Tcl objects that denote the
542 * empty string. It is the same as the Tcl_NewObj procedure above
543 * except that it calls Tcl_DbCkalloc directly with the file name and
544 * line number from its caller. This simplifies debugging since then
545 * the [memory active] command will report the correct file name and line
546 * number when reporting objects that haven't been freed.
547 *
548 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
549 * result of calling Tcl_NewObj.
550 *
551 * Results:
552 * The result is a newly allocated that represents the empty string.
553 * The new object's typePtr is set NULL and its ref count is set to 0.
554 *
555 * Side effects:
556 * If compiling with TCL_COMPILE_STATS, this procedure increments
557 * the global count of allocated objects (tclObjsAlloced).
558 *
559 *----------------------------------------------------------------------
560 */
561
562 #ifdef TCL_MEM_DEBUG
563
564 Tcl_Obj *
565 Tcl_DbNewObj(file, line)
566 register CONST char *file; /* The name of the source file calling this
567 * procedure; used for debugging. */
568 register int line; /* Line number in the source file; used
569 * for debugging. */
570 {
571 register Tcl_Obj *objPtr;
572
573 /*
574 * Use the macro defined in tclInt.h - it will use the
575 * correct allocator.
576 */
577
578 TclDbNewObj(objPtr, file, line);
579 return objPtr;
580 }
581 #else /* if not TCL_MEM_DEBUG */
582
583 Tcl_Obj *
584 Tcl_DbNewObj(file, line)
585 CONST char *file; /* The name of the source file calling this
586 * procedure; used for debugging. */
587 int line; /* Line number in the source file; used
588 * for debugging. */
589 {
590 return Tcl_NewObj();
591 }
592 #endif /* TCL_MEM_DEBUG */
593
594 /*
595 *----------------------------------------------------------------------
596 *
597 * TclAllocateFreeObjects --
598 *
599 * Procedure to allocate a number of free Tcl_Objs. This is done using
600 * a single ckalloc to reduce the overhead for Tcl_Obj allocation.
601 *
602 * Assumes mutex is held.
603 *
604 * Results:
605 * None.
606 *
607 * Side effects:
608 * tclFreeObjList, the head of the list of free Tcl_Objs, is set to the
609 * first of a number of free Tcl_Obj's linked together by their
610 * internalRep.otherValuePtrs.
611 *
612 *----------------------------------------------------------------------
613 */
614
615 #define OBJS_TO_ALLOC_EACH_TIME 100
616
617 void
618 TclAllocateFreeObjects()
619 {
620 size_t bytesToAlloc = (OBJS_TO_ALLOC_EACH_TIME * sizeof(Tcl_Obj));
621 char *basePtr;
622 register Tcl_Obj *prevPtr, *objPtr;
623 register int i;
624
625 /*
626 * This has been noted by Purify to be a potential leak. The problem is
627 * that Tcl, when not TCL_MEM_DEBUG compiled, keeps around all allocated
628 * Tcl_Obj's, pointed to by tclFreeObjList, when freed instead of
629 * actually freeing the memory. These never do get freed properly.
630 */
631
632 basePtr = (char *) ckalloc(bytesToAlloc);
633 memset(basePtr, 0, bytesToAlloc);
634
635 prevPtr = NULL;
636 objPtr = (Tcl_Obj *) basePtr;
637 for (i = 0; i < OBJS_TO_ALLOC_EACH_TIME; i++) {
638 objPtr->internalRep.otherValuePtr = (VOID *) prevPtr;
639 prevPtr = objPtr;
640 objPtr++;
641 }
642 tclFreeObjList = prevPtr;
643 }
644 #undef OBJS_TO_ALLOC_EACH_TIME
645
646 /*
647 *----------------------------------------------------------------------
648 *
649 * TclFreeObj --
650 *
651 * This procedure frees the memory associated with the argument
652 * object. It is called by the tcl.h macro Tcl_DecrRefCount when an
653 * object's ref count is zero. It is only "public" since it must
654 * be callable by that macro wherever the macro is used. It should not
655 * be directly called by clients.
656 *
657 * Results:
658 * None.
659 *
660 * Side effects:
661 * Deallocates the storage for the object's Tcl_Obj structure
662 * after deallocating the string representation and calling the
663 * type-specific Tcl_FreeInternalRepProc to deallocate the object's
664 * internal representation. If compiling with TCL_COMPILE_STATS,
665 * this procedure increments the global count of freed objects
666 * (tclObjsFreed).
667 *
668 *----------------------------------------------------------------------
669 */
670
671 void
672 TclFreeObj(objPtr)
673 register Tcl_Obj *objPtr; /* The object to be freed. */
674 {
675 register Tcl_ObjType *typePtr = objPtr->typePtr;
676
677 #ifdef TCL_MEM_DEBUG
678 if ((objPtr)->refCount < -1) {
679 panic("Reference count for %lx was negative", objPtr);
680 }
681 #endif /* TCL_MEM_DEBUG */
682
683 if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) {
684 typePtr->freeIntRepProc(objPtr);
685 }
686 Tcl_InvalidateStringRep(objPtr);
687
688 /*
689 * If debugging Tcl's memory usage, deallocate the object using ckfree.
690 * Otherwise, deallocate it by adding it onto the list of free
691 * Tcl_Obj structs we maintain.
692 */
693
694 #if defined(TCL_MEM_DEBUG) || defined(PURIFY)
695 Tcl_MutexLock(&tclObjMutex);
696 ckfree((char *) objPtr);
697 Tcl_MutexUnlock(&tclObjMutex);
698 #elif defined(TCL_THREADS) && defined(USE_THREAD_ALLOC)
699 TclThreadFreeObj(objPtr);
700 #else
701 Tcl_MutexLock(&tclObjMutex);
702 objPtr->internalRep.otherValuePtr = (VOID *) tclFreeObjList;
703 tclFreeObjList = objPtr;
704 Tcl_MutexUnlock(&tclObjMutex);
705 #endif /* TCL_MEM_DEBUG */
706
707 #ifdef TCL_COMPILE_STATS
708 tclObjsFreed++;
709 #endif /* TCL_COMPILE_STATS */
710 }
711
712 /*
713 *----------------------------------------------------------------------
714 *
715 * Tcl_DuplicateObj --
716 *
717 * Create and return a new object that is a duplicate of the argument
718 * object.
719 *
720 * Results:
721 * The return value is a pointer to a newly created Tcl_Obj. This
722 * object has reference count 0 and the same type, if any, as the
723 * source object objPtr. Also:
724 * 1) If the source object has a valid string rep, we copy it;
725 * otherwise, the duplicate's string rep is set NULL to mark
726 * it invalid.
727 * 2) If the source object has an internal representation (i.e. its
728 * typePtr is non-NULL), the new object's internal rep is set to
729 * a copy; otherwise the new internal rep is marked invalid.
730 *
731 * Side effects:
732 * What constitutes "copying" the internal representation depends on
733 * the type. For example, if the argument object is a list,
734 * the element objects it points to will not actually be copied but
735 * will be shared with the duplicate list. That is, the ref counts of
736 * the element objects will be incremented.
737 *
738 *----------------------------------------------------------------------
739 */
740
741 Tcl_Obj *
742 Tcl_DuplicateObj(objPtr)
743 register Tcl_Obj *objPtr; /* The object to duplicate. */
744 {
745 register Tcl_ObjType *typePtr = objPtr->typePtr;
746 register Tcl_Obj *dupPtr;
747
748 TclNewObj(dupPtr);
749
750 if (objPtr->bytes == NULL) {
751 dupPtr->bytes = NULL;
752 } else if (objPtr->bytes != tclEmptyStringRep) {
753 TclInitStringRep(dupPtr, objPtr->bytes, objPtr->length);
754 }
755
756 if (typePtr != NULL) {
757 if (typePtr->dupIntRepProc == NULL) {
758 dupPtr->internalRep = objPtr->internalRep;
759 dupPtr->typePtr = typePtr;
760 } else {
761 (*typePtr->dupIntRepProc)(objPtr, dupPtr);
762 }
763 }
764 return dupPtr;
765 }
766
767 /*
768 *----------------------------------------------------------------------
769 *
770 * Tcl_GetString --
771 *
772 * Returns the string representation byte array pointer for an object.
773 *
774 * Results:
775 * Returns a pointer to the string representation of objPtr. The byte
776 * array referenced by the returned pointer must not be modified by the
777 * caller. Furthermore, the caller must copy the bytes if they need to
778 * retain them since the object's string rep can change as a result of
779 * other operations.
780 *
781 * Side effects:
782 * May call the object's updateStringProc to update the string
783 * representation from the internal representation.
784 *
785 *----------------------------------------------------------------------
786 */
787
788 char *
789 Tcl_GetString(objPtr)
790 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer
791 * should be returned. */
792 {
793 if (objPtr->bytes != NULL) {
794 return objPtr->bytes;
795 }
796
797 if (objPtr->typePtr->updateStringProc == NULL) {
798 panic("UpdateStringProc should not be invoked for type %s",
799 objPtr->typePtr->name);
800 }
801 (*objPtr->typePtr->updateStringProc)(objPtr);
802 return objPtr->bytes;
803 }
804
805 /*
806 *----------------------------------------------------------------------
807 *
808 * Tcl_GetStringFromObj --
809 *
810 * Returns the string representation's byte array pointer and length
811 * for an object.
812 *
813 * Results:
814 * Returns a pointer to the string representation of objPtr. If
815 * lengthPtr isn't NULL, the length of the string representation is
816 * stored at *lengthPtr. The byte array referenced by the returned
817 * pointer must not be modified by the caller. Furthermore, the
818 * caller must copy the bytes if they need to retain them since the
819 * object's string rep can change as a result of other operations.
820 *
821 * Side effects:
822 * May call the object's updateStringProc to update the string
823 * representation from the internal representation.
824 *
825 *----------------------------------------------------------------------
826 */
827
828 char *
829 Tcl_GetStringFromObj(objPtr, lengthPtr)
830 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer should
831 * be returned. */
832 register int *lengthPtr; /* If non-NULL, the location where the string
833 * rep's byte array length should * be stored.
834 * If NULL, no length is stored. */
835 {
836 if (objPtr->bytes == NULL) {
837 if (objPtr->typePtr->updateStringProc == NULL) {
838 panic("UpdateStringProc should not be invoked for type %s",
839 objPtr->typePtr->name);
840 }
841 (*objPtr->typePtr->updateStringProc)(objPtr);
842 }
843
844 if (lengthPtr != NULL) {
845 *lengthPtr = objPtr->length;
846 }
847 return objPtr->bytes;
848 }
849
850 /*
851 *----------------------------------------------------------------------
852 *
853 * Tcl_InvalidateStringRep --
854 *
855 * This procedure is called to invalidate an object's string
856 * representation.
857 *
858 * Results:
859 * None.
860 *
861 * Side effects:
862 * Deallocates the storage for any old string representation, then
863 * sets the string representation NULL to mark it invalid.
864 *
865 *----------------------------------------------------------------------
866 */
867
868 void
869 Tcl_InvalidateStringRep(objPtr)
870 register Tcl_Obj *objPtr; /* Object whose string rep byte pointer
871 * should be freed. */
872 {
873 if (objPtr->bytes != NULL) {
874 if (objPtr->bytes != tclEmptyStringRep) {
875 ckfree((char *) objPtr->bytes);
876 }
877 objPtr->bytes = NULL;
878 }
879 }
880
881 /*
882 *----------------------------------------------------------------------
883 *
884 * Tcl_NewBooleanObj --
885 *
886 * This procedure is normally called when not debugging: i.e., when
887 * TCL_MEM_DEBUG is not defined. It creates a new boolean object and
888 * initializes it from the argument boolean value. A nonzero
889 * "boolValue" is coerced to 1.
890 *
891 * When TCL_MEM_DEBUG is defined, this procedure just returns the
892 * result of calling the debugging version Tcl_DbNewBooleanObj.
893 *
894 * Results:
895 * The newly created object is returned. This object will have an
896 * invalid string representation. The returned object has ref count 0.
897 *
898 * Side effects:
899 * None.
900 *
901 *----------------------------------------------------------------------
902 */
903
904 #ifdef TCL_MEM_DEBUG
905 #undef Tcl_NewBooleanObj
906
907 Tcl_Obj *
908 Tcl_NewBooleanObj(boolValue)
909 register int boolValue; /* Boolean used to initialize new object. */
910 {
911 return Tcl_DbNewBooleanObj(boolValue, "unknown", 0);
912 }
913
914 #else /* if not TCL_MEM_DEBUG */
915
916 Tcl_Obj *
917 Tcl_NewBooleanObj(boolValue)
918 register int boolValue; /* Boolean used to initialize new object. */
919 {
920 register Tcl_Obj *objPtr;
921
922 TclNewObj(objPtr);
923 objPtr->bytes = NULL;
924
925 objPtr->internalRep.longValue = (boolValue? 1 : 0);
926 objPtr->typePtr = &tclBooleanType;
927 return objPtr;
928 }
929 #endif /* TCL_MEM_DEBUG */
930
931 /*
932 *----------------------------------------------------------------------
933 *
934 * Tcl_DbNewBooleanObj --
935 *
936 * This procedure is normally called when debugging: i.e., when
937 * TCL_MEM_DEBUG is defined. It creates new boolean objects. It is the
938 * same as the Tcl_NewBooleanObj procedure above except that it calls
939 * Tcl_DbCkalloc directly with the file name and line number from its
940 * caller. This simplifies debugging since then the [memory active]
941 * command will report the correct file name and line number when
942 * reporting objects that haven't been freed.
943 *
944 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
945 * result of calling Tcl_NewBooleanObj.
946 *
947 * Results:
948 * The newly created object is returned. This object will have an
949 * invalid string representation. The returned object has ref count 0.
950 *
951 * Side effects:
952 * None.
953 *
954 *----------------------------------------------------------------------
955 */
956
957 #ifdef TCL_MEM_DEBUG
958
959 Tcl_Obj *
960 Tcl_DbNewBooleanObj(boolValue, file, line)
961 register int boolValue; /* Boolean used to initialize new object. */
962 CONST char *file; /* The name of the source file calling this
963 * procedure; used for debugging. */
964 int line; /* Line number in the source file; used
965 * for debugging. */
966 {
967 register Tcl_Obj *objPtr;
968
969 TclDbNewObj(objPtr, file, line);
970 objPtr->bytes = NULL;
971
972 objPtr->internalRep.longValue = (boolValue? 1 : 0);
973 objPtr->typePtr = &tclBooleanType;
974 return objPtr;
975 }
976
977 #else /* if not TCL_MEM_DEBUG */
978
979 Tcl_Obj *
980 Tcl_DbNewBooleanObj(boolValue, file, line)
981 register int boolValue; /* Boolean used to initialize new object. */
982 CONST char *file; /* The name of the source file calling this
983 * procedure; used for debugging. */
984 int line; /* Line number in the source file; used
985 * for debugging. */
986 {
987 return Tcl_NewBooleanObj(boolValue);
988 }
989 #endif /* TCL_MEM_DEBUG */
990
991 /*
992 *----------------------------------------------------------------------
993 *
994 * Tcl_SetBooleanObj --
995 *
996 * Modify an object to be a boolean object and to have the specified
997 * boolean value. A nonzero "boolValue" is coerced to 1.
998 *
999 * Results:
1000 * None.
1001 *
1002 * Side effects:
1003 * The object's old string rep, if any, is freed. Also, any old
1004 * internal rep is freed.
1005 *
1006 *----------------------------------------------------------------------
1007 */
1008
1009 void
1010 Tcl_SetBooleanObj(objPtr, boolValue)
1011 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1012 register int boolValue; /* Boolean used to set object's value. */
1013 {
1014 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1015
1016 if (Tcl_IsShared(objPtr)) {
1017 panic("Tcl_SetBooleanObj called with shared object");
1018 }
1019
1020 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1021 oldTypePtr->freeIntRepProc(objPtr);
1022 }
1023
1024 objPtr->internalRep.longValue = (boolValue? 1 : 0);
1025 objPtr->typePtr = &tclBooleanType;
1026 Tcl_InvalidateStringRep(objPtr);
1027 }
1028
1029 /*
1030 *----------------------------------------------------------------------
1031 *
1032 * Tcl_GetBooleanFromObj --
1033 *
1034 * Attempt to return a boolean from the Tcl object "objPtr". If the
1035 * object is not already a boolean, an attempt will be made to convert
1036 * it to one.
1037 *
1038 * Results:
1039 * The return value is a standard Tcl object result. If an error occurs
1040 * during conversion, an error message is left in the interpreter's
1041 * result unless "interp" is NULL.
1042 *
1043 * Side effects:
1044 * If the object is not already a boolean, the conversion will free
1045 * any old internal representation.
1046 *
1047 *----------------------------------------------------------------------
1048 */
1049
1050 int
1051 Tcl_GetBooleanFromObj(interp, objPtr, boolPtr)
1052 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1053 register Tcl_Obj *objPtr; /* The object from which to get boolean. */
1054 register int *boolPtr; /* Place to store resulting boolean. */
1055 {
1056 register int result;
1057
1058 if (objPtr->typePtr == &tclBooleanType) {
1059 result = TCL_OK;
1060 } else {
1061 result = SetBooleanFromAny(interp, objPtr);
1062 }
1063
1064 if (result == TCL_OK) {
1065 *boolPtr = (int) objPtr->internalRep.longValue;
1066 }
1067 return result;
1068 }
1069
1070 /*
1071 *----------------------------------------------------------------------
1072 *
1073 * SetBooleanFromAny --
1074 *
1075 * Attempt to generate a boolean internal form for the Tcl object
1076 * "objPtr".
1077 *
1078 * Results:
1079 * The return value is a standard Tcl result. If an error occurs during
1080 * conversion, an error message is left in the interpreter's result
1081 * unless "interp" is NULL.
1082 *
1083 * Side effects:
1084 * If no error occurs, an integer 1 or 0 is stored as "objPtr"s
1085 * internal representation and the type of "objPtr" is set to boolean.
1086 *
1087 *----------------------------------------------------------------------
1088 */
1089
1090 static int
1091 SetBooleanFromAny(interp, objPtr)
1092 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1093 register Tcl_Obj *objPtr; /* The object to convert. */
1094 {
1095 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1096 char *string, *end;
1097 register char c;
1098 char lowerCase[10];
1099 int newBool, length;
1100 register int i;
1101
1102 /*
1103 * Get the string representation. Make it up-to-date if necessary.
1104 */
1105
1106 string = Tcl_GetStringFromObj(objPtr, &length);
1107
1108 /*
1109 * Use the obvious shortcuts for numerical values; if objPtr is not
1110 * of numerical type, parse its string rep.
1111 */
1112
1113 if (objPtr->typePtr == &tclIntType) {
1114 newBool = (objPtr->internalRep.longValue != 0);
1115 } else if (objPtr->typePtr == &tclDoubleType) {
1116 newBool = (objPtr->internalRep.doubleValue != 0.0);
1117 } else if (objPtr->typePtr == &tclWideIntType) {
1118 #ifdef TCL_WIDE_INT_IS_LONG
1119 newBool = (objPtr->internalRep.longValue != 0);
1120 #else /* !TCL_WIDE_INT_IS_LONG */
1121 newBool = (objPtr->internalRep.wideValue != Tcl_LongAsWide(0));
1122 #endif /* TCL_WIDE_INT_IS_LONG */
1123 } else {
1124 /*
1125 * Copy the string converting its characters to lower case.
1126 */
1127
1128 for (i = 0; (i < 9) && (i < length); i++) {
1129 c = string[i];
1130 /*
1131 * Weed out international characters so we can safely operate
1132 * on single bytes.
1133 */
1134
1135 if (c & 0x80) {
1136 goto badBoolean;
1137 }
1138 if (Tcl_UniCharIsUpper(UCHAR(c))) {
1139 c = (char) Tcl_UniCharToLower(UCHAR(c));
1140 }
1141 lowerCase[i] = c;
1142 }
1143 lowerCase[i] = 0;
1144
1145 /*
1146 * Parse the string as a boolean. We use an implementation here that
1147 * doesn't report errors in interp if interp is NULL.
1148 */
1149
1150 c = lowerCase[0];
1151 if ((c == '0') && (lowerCase[1] == '\0')) {
1152 newBool = 0;
1153 } else if ((c == '1') && (lowerCase[1] == '\0')) {
1154 newBool = 1;
1155 } else if ((c == 'y') && (strncmp(lowerCase, "yes", (size_t) length) == 0)) {
1156 newBool = 1;
1157 } else if ((c == 'n') && (strncmp(lowerCase, "no", (size_t) length) == 0)) {
1158 newBool = 0;
1159 } else if ((c == 't') && (strncmp(lowerCase, "true", (size_t) length) == 0)) {
1160 newBool = 1;
1161 } else if ((c == 'f') && (strncmp(lowerCase, "false", (size_t) length) == 0)) {
1162 newBool = 0;
1163 } else if ((c == 'o') && (length >= 2)) {
1164 if (strncmp(lowerCase, "on", (size_t) length) == 0) {
1165 newBool = 1;
1166 } else if (strncmp(lowerCase, "off", (size_t) length) == 0) {
1167 newBool = 0;
1168 } else {
1169 goto badBoolean;
1170 }
1171 } else {
1172 double dbl;
1173 /*
1174 * Boolean values can be extracted from ints or doubles. Note
1175 * that we don't use strtoul or strtoull here because we don't
1176 * care about what the value is, just whether it is equal to
1177 * zero or not.
1178 */
1179 #ifdef TCL_WIDE_INT_IS_LONG
1180 newBool = strtol(string, &end, 0);
1181 if (end != string) {
1182 /*
1183 * Make sure the string has no garbage after the end of
1184 * the int.
1185 */
1186 while ((end < (string+length))
1187 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1188 end++;
1189 }
1190 if (end == (string+length)) {
1191 newBool = (newBool != 0);
1192 goto goodBoolean;
1193 }
1194 }
1195 #else /* !TCL_WIDE_INT_IS_LONG */
1196 Tcl_WideInt wide = strtoll(string, &end, 0);
1197 if (end != string) {
1198 /*
1199 * Make sure the string has no garbage after the end of
1200 * the wide int.
1201 */
1202 while ((end < (string+length))
1203 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1204 end++;
1205 }
1206 if (end == (string+length)) {
1207 newBool = (wide != Tcl_LongAsWide(0));
1208 goto goodBoolean;
1209 }
1210 }
1211 #endif /* TCL_WIDE_INT_IS_LONG */
1212 /*
1213 * Still might be a string containing the characters representing an
1214 * int or double that wasn't handled above. This would be a string
1215 * like "27" or "1.0" that is non-zero and not "1". Such a string
1216 * would result in the boolean value true. We try converting to
1217 * double. If that succeeds and the resulting double is non-zero, we
1218 * have a "true". Note that numbers can't have embedded NULLs.
1219 */
1220
1221 dbl = strtod(string, &end);
1222 if (end == string) {
1223 goto badBoolean;
1224 }
1225
1226 /*
1227 * Make sure the string has no garbage after the end of the double.
1228 */
1229
1230 while ((end < (string+length))
1231 && isspace(UCHAR(*end))) { /* INTL: ISO only */
1232 end++;
1233 }
1234 if (end != (string+length)) {
1235 goto badBoolean;
1236 }
1237 newBool = (dbl != 0.0);
1238 }
1239 }
1240
1241 /*
1242 * Free the old internalRep before setting the new one. We do this as
1243 * late as possible to allow the conversion code, in particular
1244 * Tcl_GetStringFromObj, to use that old internalRep.
1245 */
1246
1247 goodBoolean:
1248 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1249 oldTypePtr->freeIntRepProc(objPtr);
1250 }
1251
1252 objPtr->internalRep.longValue = newBool;
1253 objPtr->typePtr = &tclBooleanType;
1254 return TCL_OK;
1255
1256 badBoolean:
1257 if (interp != NULL) {
1258 /*
1259 * Must copy string before resetting the result in case a caller
1260 * is trying to convert the interpreter's result to a boolean.
1261 */
1262
1263 char buf[100];
1264 sprintf(buf, "expected boolean value but got \"%.50s\"", string);
1265 Tcl_ResetResult(interp);
1266 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1267 }
1268 return TCL_ERROR;
1269 }
1270
1271 /*
1272 *----------------------------------------------------------------------
1273 *
1274 * UpdateStringOfBoolean --
1275 *
1276 * Update the string representation for a boolean object.
1277 * Note: This procedure does not free an existing old string rep
1278 * so storage will be lost if this has not already been done.
1279 *
1280 * Results:
1281 * None.
1282 *
1283 * Side effects:
1284 * The object's string is set to a valid string that results from
1285 * the boolean-to-string conversion.
1286 *
1287 *----------------------------------------------------------------------
1288 */
1289
1290 static void
1291 UpdateStringOfBoolean(objPtr)
1292 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
1293 {
1294 char *s = ckalloc((unsigned) 2);
1295
1296 s[0] = (char) (objPtr->internalRep.longValue? '1' : '0');
1297 s[1] = '\0';
1298 objPtr->bytes = s;
1299 objPtr->length = 1;
1300 }
1301
1302 /*
1303 *----------------------------------------------------------------------
1304 *
1305 * Tcl_NewDoubleObj --
1306 *
1307 * This procedure is normally called when not debugging: i.e., when
1308 * TCL_MEM_DEBUG is not defined. It creates a new double object and
1309 * initializes it from the argument double value.
1310 *
1311 * When TCL_MEM_DEBUG is defined, this procedure just returns the
1312 * result of calling the debugging version Tcl_DbNewDoubleObj.
1313 *
1314 * Results:
1315 * The newly created object is returned. This object will have an
1316 * invalid string representation. The returned object has ref count 0.
1317 *
1318 * Side effects:
1319 * None.
1320 *
1321 *----------------------------------------------------------------------
1322 */
1323
1324 #ifdef TCL_MEM_DEBUG
1325 #undef Tcl_NewDoubleObj
1326
1327 Tcl_Obj *
1328 Tcl_NewDoubleObj(dblValue)
1329 register double dblValue; /* Double used to initialize the object. */
1330 {
1331 return Tcl_DbNewDoubleObj(dblValue, "unknown", 0);
1332 }
1333
1334 #else /* if not TCL_MEM_DEBUG */
1335
1336 Tcl_Obj *
1337 Tcl_NewDoubleObj(dblValue)
1338 register double dblValue; /* Double used to initialize the object. */
1339 {
1340 register Tcl_Obj *objPtr;
1341
1342 TclNewObj(objPtr);
1343 objPtr->bytes = NULL;
1344
1345 objPtr->internalRep.doubleValue = dblValue;
1346 objPtr->typePtr = &tclDoubleType;
1347 return objPtr;
1348 }
1349 #endif /* if TCL_MEM_DEBUG */
1350
1351 /*
1352 *----------------------------------------------------------------------
1353 *
1354 * Tcl_DbNewDoubleObj --
1355 *
1356 * This procedure is normally called when debugging: i.e., when
1357 * TCL_MEM_DEBUG is defined. It creates new double objects. It is the
1358 * same as the Tcl_NewDoubleObj procedure above except that it calls
1359 * Tcl_DbCkalloc directly with the file name and line number from its
1360 * caller. This simplifies debugging since then the [memory active]
1361 * command will report the correct file name and line number when
1362 * reporting objects that haven't been freed.
1363 *
1364 * When TCL_MEM_DEBUG is not defined, this procedure just returns the
1365 * result of calling Tcl_NewDoubleObj.
1366 *
1367 * Results:
1368 * The newly created object is returned. This object will have an
1369 * invalid string representation. The returned object has ref count 0.
1370 *
1371 * Side effects:
1372 * None.
1373 *
1374 *----------------------------------------------------------------------
1375 */
1376
1377 #ifdef TCL_MEM_DEBUG
1378
1379 Tcl_Obj *
1380 Tcl_DbNewDoubleObj(dblValue, file, line)
1381 register double dblValue; /* Double used to initialize the object. */
1382 CONST char *file; /* The name of the source file calling this
1383 * procedure; used for debugging. */
1384 int line; /* Line number in the source file; used
1385 * for debugging. */
1386 {
1387 register Tcl_Obj *objPtr;
1388
1389 TclDbNewObj(objPtr, file, line);
1390 objPtr->bytes = NULL;
1391
1392 objPtr->internalRep.doubleValue = dblValue;
1393 objPtr->typePtr = &tclDoubleType;
1394 return objPtr;
1395 }
1396
1397 #else /* if not TCL_MEM_DEBUG */
1398
1399 Tcl_Obj *
1400 Tcl_DbNewDoubleObj(dblValue, file, line)
1401 register double dblValue; /* Double used to initialize the object. */
1402 CONST char *file; /* The name of the source file calling this
1403 * procedure; used for debugging. */
1404 int line; /* Line number in the source file; used
1405 * for debugging. */
1406 {
1407 return Tcl_NewDoubleObj(dblValue);
1408 }
1409 #endif /* TCL_MEM_DEBUG */
1410
1411 /*
1412 *----------------------------------------------------------------------
1413 *
1414 * Tcl_SetDoubleObj --
1415 *
1416 * Modify an object to be a double object and to have the specified
1417 * double value.
1418 *
1419 * Results:
1420 * None.
1421 *
1422 * Side effects:
1423 * The object's old string rep, if any, is freed. Also, any old
1424 * internal rep is freed.
1425 *
1426 *----------------------------------------------------------------------
1427 */
1428
1429 void
1430 Tcl_SetDoubleObj(objPtr, dblValue)
1431 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1432 register double dblValue; /* Double used to set the object's value. */
1433 {
1434 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1435
1436 if (Tcl_IsShared(objPtr)) {
1437 panic("Tcl_SetDoubleObj called with shared object");
1438 }
1439
1440 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1441 oldTypePtr->freeIntRepProc(objPtr);
1442 }
1443
1444 objPtr->internalRep.doubleValue = dblValue;
1445 objPtr->typePtr = &tclDoubleType;
1446 Tcl_InvalidateStringRep(objPtr);
1447 }
1448
1449 /*
1450 *----------------------------------------------------------------------
1451 *
1452 * Tcl_GetDoubleFromObj --
1453 *
1454 * Attempt to return a double from the Tcl object "objPtr". If the
1455 * object is not already a double, an attempt will be made to convert
1456 * it to one.
1457 *
1458 * Results:
1459 * The return value is a standard Tcl object result. If an error occurs
1460 * during conversion, an error message is left in the interpreter's
1461 * result unless "interp" is NULL.
1462 *
1463 * Side effects:
1464 * If the object is not already a double, the conversion will free
1465 * any old internal representation.
1466 *
1467 *----------------------------------------------------------------------
1468 */
1469
1470 int
1471 Tcl_GetDoubleFromObj(interp, objPtr, dblPtr)
1472 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1473 register Tcl_Obj *objPtr; /* The object from which to get a double. */
1474 register double *dblPtr; /* Place to store resulting double. */
1475 {
1476 register int result;
1477
1478 if (objPtr->typePtr == &tclDoubleType) {
1479 *dblPtr = objPtr->internalRep.doubleValue;
1480 return TCL_OK;
1481 }
1482
1483 result = SetDoubleFromAny(interp, objPtr);
1484 if (result == TCL_OK) {
1485 *dblPtr = objPtr->internalRep.doubleValue;
1486 }
1487 return result;
1488 }
1489
1490 /*
1491 *----------------------------------------------------------------------
1492 *
1493 * SetDoubleFromAny --
1494 *
1495 * Attempt to generate an double-precision floating point internal form
1496 * for the Tcl object "objPtr".
1497 *
1498 * Results:
1499 * The return value is a standard Tcl object result. If an error occurs
1500 * during conversion, an error message is left in the interpreter's
1501 * result unless "interp" is NULL.
1502 *
1503 * Side effects:
1504 * If no error occurs, a double is stored as "objPtr"s internal
1505 * representation.
1506 *
1507 *----------------------------------------------------------------------
1508 */
1509
1510 static int
1511 SetDoubleFromAny(interp, objPtr)
1512 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1513 register Tcl_Obj *objPtr; /* The object to convert. */
1514 {
1515 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1516 char *string, *end;
1517 double newDouble;
1518 int length;
1519
1520 /*
1521 * Get the string representation. Make it up-to-date if necessary.
1522 */
1523
1524 string = Tcl_GetStringFromObj(objPtr, &length);
1525
1526 /*
1527 * Now parse "objPtr"s string as an double. Numbers can't have embedded
1528 * NULLs. We use an implementation here that doesn't report errors in
1529 * interp if interp is NULL.
1530 */
1531
1532 errno = 0;
1533 newDouble = strtod(string, &end);
1534 if (end == string) {
1535 badDouble:
1536 if (interp != NULL) {
1537 /*
1538 * Must copy string before resetting the result in case a caller
1539 * is trying to convert the interpreter's result to an int.
1540 */
1541
1542 char buf[100];
1543 sprintf(buf, "expected floating-point number but got \"%.50s\"",
1544 string);
1545 Tcl_ResetResult(interp);
1546 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1547 }
1548 return TCL_ERROR;
1549 }
1550 if (errno != 0) {
1551 if (interp != NULL) {
1552 TclExprFloatError(interp, newDouble);
1553 }
1554 return TCL_ERROR;
1555 }
1556
1557 /*
1558 * Make sure that the string has no garbage after the end of the double.
1559 */
1560
1561 while ((end < (string+length))
1562 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
1563 end++;
1564 }
1565 if (end != (string+length)) {
1566 goto badDouble;
1567 }
1568
1569 /*
1570 * The conversion to double succeeded. Free the old internalRep before
1571 * setting the new one. We do this as late as possible to allow the
1572 * conversion code, in particular Tcl_GetStringFromObj, to use that old
1573 * internalRep.
1574 */
1575
1576 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1577 oldTypePtr->freeIntRepProc(objPtr);
1578 }
1579
1580 objPtr->internalRep.doubleValue = newDouble;
1581 objPtr->typePtr = &tclDoubleType;
1582 return TCL_OK;
1583 }
1584
1585 /*
1586 *----------------------------------------------------------------------
1587 *
1588 * UpdateStringOfDouble --
1589 *
1590 * Update the string representation for a double-precision floating
1591 * point object. This must obey the current tcl_precision value for
1592 * double-to-string conversions. Note: This procedure does not free an
1593 * existing old string rep so storage will be lost if this has not
1594 * already been done.
1595 *
1596 * Results:
1597 * None.
1598 *
1599 * Side effects:
1600 * The object's string is set to a valid string that results from
1601 * the double-to-string conversion.
1602 *
1603 *----------------------------------------------------------------------
1604 */
1605
1606 static void
1607 UpdateStringOfDouble(objPtr)
1608 register Tcl_Obj *objPtr; /* Double obj with string rep to update. */
1609 {
1610 char buffer[TCL_DOUBLE_SPACE];
1611 register int len;
1612
1613 Tcl_PrintDouble((Tcl_Interp *) NULL, objPtr->internalRep.doubleValue,
1614 buffer);
1615 len = strlen(buffer);
1616
1617 objPtr->bytes = (char *) ckalloc((unsigned) len + 1);
1618 strcpy(objPtr->bytes, buffer);
1619 objPtr->length = len;
1620 }
1621
1622 /*
1623 *----------------------------------------------------------------------
1624 *
1625 * Tcl_NewIntObj --
1626 *
1627 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
1628 * Tcl_NewIntObj to create a new integer object end up calling the
1629 * debugging procedure Tcl_DbNewLongObj instead.
1630 *
1631 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
1632 * calls to Tcl_NewIntObj result in a call to one of the two
1633 * Tcl_NewIntObj implementations below. We provide two implementations
1634 * so that the Tcl core can be compiled to do memory debugging of the
1635 * core even if a client does not request it for itself.
1636 *
1637 * Integer and long integer objects share the same "integer" type
1638 * implementation. We store all integers as longs and Tcl_GetIntFromObj
1639 * checks whether the current value of the long can be represented by
1640 * an int.
1641 *
1642 * Results:
1643 * The newly created object is returned. This object will have an
1644 * invalid string representation. The returned object has ref count 0.
1645 *
1646 * Side effects:
1647 * None.
1648 *
1649 *----------------------------------------------------------------------
1650 */
1651
1652 #ifdef TCL_MEM_DEBUG
1653 #undef Tcl_NewIntObj
1654
1655 Tcl_Obj *
1656 Tcl_NewIntObj(intValue)
1657 register int intValue; /* Int used to initialize the new object. */
1658 {
1659 return Tcl_DbNewLongObj((long)intValue, "unknown", 0);
1660 }
1661
1662 #else /* if not TCL_MEM_DEBUG */
1663
1664 Tcl_Obj *
1665 Tcl_NewIntObj(intValue)
1666 register int intValue; /* Int used to initialize the new object. */
1667 {
1668 register Tcl_Obj *objPtr;
1669
1670 TclNewObj(objPtr);
1671 objPtr->bytes = NULL;
1672
1673 objPtr->internalRep.longValue = (long)intValue;
1674 objPtr->typePtr = &tclIntType;
1675 return objPtr;
1676 }
1677 #endif /* if TCL_MEM_DEBUG */
1678
1679 /*
1680 *----------------------------------------------------------------------
1681 *
1682 * Tcl_SetIntObj --
1683 *
1684 * Modify an object to be an integer and to have the specified integer
1685 * value.
1686 *
1687 * Results:
1688 * None.
1689 *
1690 * Side effects:
1691 * The object's old string rep, if any, is freed. Also, any old
1692 * internal rep is freed.
1693 *
1694 *----------------------------------------------------------------------
1695 */
1696
1697 void
1698 Tcl_SetIntObj(objPtr, intValue)
1699 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
1700 register int intValue; /* Integer used to set object's value. */
1701 {
1702 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1703
1704 if (Tcl_IsShared(objPtr)) {
1705 panic("Tcl_SetIntObj called with shared object");
1706 }
1707
1708 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1709 oldTypePtr->freeIntRepProc(objPtr);
1710 }
1711
1712 objPtr->internalRep.longValue = (long) intValue;
1713 objPtr->typePtr = &tclIntType;
1714 Tcl_InvalidateStringRep(objPtr);
1715 }
1716
1717 /*
1718 *----------------------------------------------------------------------
1719 *
1720 * Tcl_GetIntFromObj --
1721 *
1722 * Attempt to return an int from the Tcl object "objPtr". If the object
1723 * is not already an int, an attempt will be made to convert it to one.
1724 *
1725 * Integer and long integer objects share the same "integer" type
1726 * implementation. We store all integers as longs and Tcl_GetIntFromObj
1727 * checks whether the current value of the long can be represented by
1728 * an int.
1729 *
1730 * Results:
1731 * The return value is a standard Tcl object result. If an error occurs
1732 * during conversion or if the long integer held by the object
1733 * can not be represented by an int, an error message is left in
1734 * the interpreter's result unless "interp" is NULL.
1735 *
1736 * Side effects:
1737 * If the object is not already an int, the conversion will free
1738 * any old internal representation.
1739 *
1740 *----------------------------------------------------------------------
1741 */
1742
1743 int
1744 Tcl_GetIntFromObj(interp, objPtr, intPtr)
1745 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1746 register Tcl_Obj *objPtr; /* The object from which to get a int. */
1747 register int *intPtr; /* Place to store resulting int. */
1748 {
1749 register long l;
1750 int result;
1751
1752 if (objPtr->typePtr != &tclIntType) {
1753 result = SetIntFromAny(interp, objPtr);
1754 if (result != TCL_OK) {
1755 return result;
1756 }
1757 }
1758 l = objPtr->internalRep.longValue;
1759 if (((long)((int)l)) == l) {
1760 *intPtr = (int)objPtr->internalRep.longValue;
1761 return TCL_OK;
1762 }
1763 if (interp != NULL) {
1764 Tcl_ResetResult(interp);
1765 Tcl_AppendToObj(Tcl_GetObjResult(interp),
1766 "integer value too large to represent as non-long integer", -1);
1767 }
1768 return TCL_ERROR;
1769 }
1770
1771 /*
1772 *----------------------------------------------------------------------
1773 *
1774 * SetIntFromAny --
1775 *
1776 * Attempt to generate an integer internal form for the Tcl object
1777 * "objPtr".
1778 *
1779 * Results:
1780 * The return value is a standard object Tcl result. If an error occurs
1781 * during conversion, an error message is left in the interpreter's
1782 * result unless "interp" is NULL.
1783 *
1784 * Side effects:
1785 * If no error occurs, an int is stored as "objPtr"s internal
1786 * representation.
1787 *
1788 *----------------------------------------------------------------------
1789 */
1790
1791 static int
1792 SetIntFromAny(interp, objPtr)
1793 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
1794 register Tcl_Obj *objPtr; /* The object to convert. */
1795 {
1796 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
1797 char *string, *end;
1798 int length;
1799 register char *p;
1800 long newLong;
1801
1802 /*
1803 * Get the string representation. Make it up-to-date if necessary.
1804 */
1805
1806 p = string = Tcl_GetStringFromObj(objPtr, &length);
1807
1808 /*
1809 * Now parse "objPtr"s string as an int. We use an implementation here
1810 * that doesn't report errors in interp if interp is NULL. Note: use
1811 * strtoul instead of strtol for integer conversions to allow full-size
1812 * unsigned numbers, but don't depend on strtoul to handle sign
1813 * characters; it won't in some implementations.
1814 */
1815
1816 errno = 0;
1817 #ifdef TCL_STRTOUL_SIGN_CHECK
1818 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
1819 /* Empty loop body. */
1820 }
1821 if (*p == '-') {
1822 p++;
1823 newLong = -((long)strtoul(p, &end, 0));
1824 } else if (*p == '+') {
1825 p++;
1826 newLong = strtoul(p, &end, 0);
1827 } else
1828 #else
1829 newLong = strtoul(p, &end, 0);
1830 #endif
1831 if (end == p) {
1832 badInteger:
1833 if (interp != NULL) {
1834 /*
1835 * Must copy string before resetting the result in case a caller
1836 * is trying to convert the interpreter's result to an int.
1837 */
1838
1839 char buf[100];
1840 sprintf(buf, "expected integer but got \"%.50s\"", string);
1841 Tcl_ResetResult(interp);
1842 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
1843 TclCheckBadOctal(interp, string);
1844 }
1845 return TCL_ERROR;
1846 }
1847 if (errno == ERANGE) {
1848 if (interp != NULL) {
1849 char *s = "integer value too large to represent";
1850 Tcl_ResetResult(interp);
1851 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
1852 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *) NULL);
1853 }
1854 return TCL_ERROR;
1855 }
1856
1857 /*
1858 * Make sure that the string has no garbage after the end of the int.
1859 */
1860
1861 while ((end < (string+length))
1862 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
1863 end++;
1864 }
1865 if (end != (string+length)) {
1866 goto badInteger;
1867 }
1868
1869 /*
1870 * The conversion to int succeeded. Free the old internalRep before
1871 * setting the new one. We do this as late as possible to allow the
1872 * conversion code, in particular Tcl_GetStringFromObj, to use that old
1873 * internalRep.
1874 */
1875
1876 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
1877 oldTypePtr->freeIntRepProc(objPtr);
1878 }
1879
1880 objPtr->internalRep.longValue = newLong;
1881 objPtr->typePtr = &tclIntType;
1882 return TCL_OK;
1883 }
1884
1885 /*
1886 *----------------------------------------------------------------------
1887 *
1888 * UpdateStringOfInt --
1889 *
1890 * Update the string representation for an integer object.
1891 * Note: This procedure does not free an existing old string rep
1892 * so storage will be lost if this has not already been done.
1893 *
1894 * Results:
1895 * None.
1896 *
1897 * Side effects:
1898 * The object's string is set to a valid string that results from
1899 * the int-to-string conversion.
1900 *
1901 *----------------------------------------------------------------------
1902 */
1903
1904 static void
1905 UpdateStringOfInt(objPtr)
1906 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
1907 {
1908 char buffer[TCL_INTEGER_SPACE];
1909 register int len;
1910
1911 len = TclFormatInt(buffer, objPtr->internalRep.longValue);
1912
1913 objPtr->bytes = ckalloc((unsigned) len + 1);
1914 strcpy(objPtr->bytes, buffer);
1915 objPtr->length = len;
1916 }
1917
1918 /*
1919 *----------------------------------------------------------------------
1920 *
1921 * Tcl_NewLongObj --
1922 *
1923 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
1924 * Tcl_NewLongObj to create a new long integer object end up calling
1925 * the debugging procedure Tcl_DbNewLongObj instead.
1926 *
1927 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
1928 * calls to Tcl_NewLongObj result in a call to one of the two
1929 * Tcl_NewLongObj implementations below. We provide two implementations
1930 * so that the Tcl core can be compiled to do memory debugging of the
1931 * core even if a client does not request it for itself.
1932 *
1933 * Integer and long integer objects share the same "integer" type
1934 * implementation. We store all integers as longs and Tcl_GetIntFromObj
1935 * checks whether the current value of the long can be represented by
1936 * an int.
1937 *
1938 * Results:
1939 * The newly created object is returned. This object will have an
1940 * invalid string representation. The returned object has ref count 0.
1941 *
1942 * Side effects:
1943 * None.
1944 *
1945 *----------------------------------------------------------------------
1946 */
1947
1948 #ifdef TCL_MEM_DEBUG
1949 #undef Tcl_NewLongObj
1950
1951 Tcl_Obj *
1952 Tcl_NewLongObj(longValue)
1953 register long longValue; /* Long integer used to initialize the
1954 * new object. */
1955 {
1956 return Tcl_DbNewLongObj(longValue, "unknown", 0);
1957 }
1958
1959 #else /* if not TCL_MEM_DEBUG */
1960
1961 Tcl_Obj *
1962 Tcl_NewLongObj(longValue)
1963 register long longValue; /* Long integer used to initialize the
1964 * new object. */
1965 {
1966 register Tcl_Obj *objPtr;
1967
1968 TclNewObj(objPtr);
1969 objPtr->bytes = NULL;
1970
1971 objPtr->internalRep.longValue = longValue;
1972 objPtr->typePtr = &tclIntType;
1973 return objPtr;
1974 }
1975 #endif /* if TCL_MEM_DEBUG */
1976
1977 /*
1978 *----------------------------------------------------------------------
1979 *
1980 * Tcl_DbNewLongObj --
1981 *
1982 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
1983 * Tcl_NewIntObj and Tcl_NewLongObj to create new integer or
1984 * long integer objects end up calling the debugging procedure
1985 * Tcl_DbNewLongObj instead. We provide two implementations of
1986 * Tcl_DbNewLongObj so that whether the Tcl core is compiled to do
1987 * memory debugging of the core is independent of whether a client
1988 * requests debugging for itself.
1989 *
1990 * When the core is compiled with TCL_MEM_DEBUG defined,
1991 * Tcl_DbNewLongObj calls Tcl_DbCkalloc directly with the file name and
1992 * line number from its caller. This simplifies debugging since then
1993 * the [memory active] command will report the caller's file name and
1994 * line number when reporting objects that haven't been freed.
1995 *
1996 * Otherwise, when the core is compiled without TCL_MEM_DEBUG defined,
1997 * this procedure just returns the result of calling Tcl_NewLongObj.
1998 *
1999 * Results:
2000 * The newly created long integer object is returned. This object
2001 * will have an invalid string representation. The returned object has
2002 * ref count 0.
2003 *
2004 * Side effects:
2005 * Allocates memory.
2006 *
2007 *----------------------------------------------------------------------
2008 */
2009
2010 #ifdef TCL_MEM_DEBUG
2011
2012 Tcl_Obj *
2013 Tcl_DbNewLongObj(longValue, file, line)
2014 register long longValue; /* Long integer used to initialize the
2015 * new object. */
2016 CONST char *file; /* The name of the source file calling this
2017 * procedure; used for debugging. */
2018 int line; /* Line number in the source file; used
2019 * for debugging. */
2020 {
2021 register Tcl_Obj *objPtr;
2022
2023 TclDbNewObj(objPtr, file, line);
2024 objPtr->bytes = NULL;
2025
2026 objPtr->internalRep.longValue = longValue;
2027 objPtr->typePtr = &tclIntType;
2028 return objPtr;
2029 }
2030
2031 #else /* if not TCL_MEM_DEBUG */
2032
2033 Tcl_Obj *
2034 Tcl_DbNewLongObj(longValue, file, line)
2035 register long longValue; /* Long integer used to initialize the
2036 * new object. */
2037 CONST char *file; /* The name of the source file calling this
2038 * procedure; used for debugging. */
2039 int line; /* Line number in the source file; used
2040 * for debugging. */
2041 {
2042 return Tcl_NewLongObj(longValue);
2043 }
2044 #endif /* TCL_MEM_DEBUG */
2045
2046 /*
2047 *----------------------------------------------------------------------
2048 *
2049 * Tcl_SetLongObj --
2050 *
2051 * Modify an object to be an integer object and to have the specified
2052 * long integer value.
2053 *
2054 * Results:
2055 * None.
2056 *
2057 * Side effects:
2058 * The object's old string rep, if any, is freed. Also, any old
2059 * internal rep is freed.
2060 *
2061 *----------------------------------------------------------------------
2062 */
2063
2064 void
2065 Tcl_SetLongObj(objPtr, longValue)
2066 register Tcl_Obj *objPtr; /* Object whose internal rep to init. */
2067 register long longValue; /* Long integer used to initialize the
2068 * object's value. */
2069 {
2070 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2071
2072 if (Tcl_IsShared(objPtr)) {
2073 panic("Tcl_SetLongObj called with shared object");
2074 }
2075
2076 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2077 oldTypePtr->freeIntRepProc(objPtr);
2078 }
2079
2080 objPtr->internalRep.longValue = longValue;
2081 objPtr->typePtr = &tclIntType;
2082 Tcl_InvalidateStringRep(objPtr);
2083 }
2084
2085 /*
2086 *----------------------------------------------------------------------
2087 *
2088 * Tcl_GetLongFromObj --
2089 *
2090 * Attempt to return an long integer from the Tcl object "objPtr". If
2091 * the object is not already an int object, an attempt will be made to
2092 * convert it to one.
2093 *
2094 * Results:
2095 * The return value is a standard Tcl object result. If an error occurs
2096 * during conversion, an error message is left in the interpreter's
2097 * result unless "interp" is NULL.
2098 *
2099 * Side effects:
2100 * If the object is not already an int object, the conversion will free
2101 * any old internal representation.
2102 *
2103 *----------------------------------------------------------------------
2104 */
2105
2106 int
2107 Tcl_GetLongFromObj(interp, objPtr, longPtr)
2108 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2109 register Tcl_Obj *objPtr; /* The object from which to get a long. */
2110 register long *longPtr; /* Place to store resulting long. */
2111 {
2112 register int result;
2113
2114 if (objPtr->typePtr == &tclIntType) {
2115 *longPtr = objPtr->internalRep.longValue;
2116 return TCL_OK;
2117 }
2118 result = SetIntFromAny(interp, objPtr);
2119 if (result == TCL_OK) {
2120 *longPtr = objPtr->internalRep.longValue;
2121 }
2122 return result;
2123 }
2124
2125 /*
2126 *----------------------------------------------------------------------
2127 *
2128 * SetWideIntFromAny --
2129 *
2130 * Attempt to generate an integer internal form for the Tcl object
2131 * "objPtr".
2132 *
2133 * Results:
2134 * The return value is a standard object Tcl result. If an error occurs
2135 * during conversion, an error message is left in the interpreter's
2136 * result unless "interp" is NULL.
2137 *
2138 * Side effects:
2139 * If no error occurs, an int is stored as "objPtr"s internal
2140 * representation.
2141 *
2142 *----------------------------------------------------------------------
2143 */
2144
2145 static int
2146 SetWideIntFromAny(interp, objPtr)
2147 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2148 register Tcl_Obj *objPtr; /* The object to convert. */
2149 {
2150 #ifndef TCL_WIDE_INT_IS_LONG
2151 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2152 char *string, *end;
2153 int length;
2154 register char *p;
2155 Tcl_WideInt newWide;
2156
2157 /*
2158 * Get the string representation. Make it up-to-date if necessary.
2159 */
2160
2161 p = string = Tcl_GetStringFromObj(objPtr, &length);
2162
2163 /*
2164 * Now parse "objPtr"s string as an int. We use an implementation here
2165 * that doesn't report errors in interp if interp is NULL. Note: use
2166 * strtoull instead of strtoll for integer conversions to allow full-size
2167 * unsigned numbers, but don't depend on strtoull to handle sign
2168 * characters; it won't in some implementations.
2169 */
2170
2171 errno = 0;
2172 #ifdef TCL_STRTOUL_SIGN_CHECK
2173 for ( ; isspace(UCHAR(*p)); p++) { /* INTL: ISO space. */
2174 /* Empty loop body. */
2175 }
2176 if (*p == '-') {
2177 p++;
2178 newWide = -((Tcl_WideInt)strtoull(p, &end, 0));
2179 } else if (*p == '+') {
2180 p++;
2181 newWide = strtoull(p, &end, 0);
2182 } else
2183 #else
2184 newWide = strtoull(p, &end, 0);
2185 #endif
2186 if (end == p) {
2187 badInteger:
2188 if (interp != NULL) {
2189 /*
2190 * Must copy string before resetting the result in case a caller
2191 * is trying to convert the interpreter's result to an int.
2192 */
2193
2194 char buf[100];
2195 sprintf(buf, "expected integer but got \"%.50s\"", string);
2196 Tcl_ResetResult(interp);
2197 Tcl_AppendToObj(Tcl_GetObjResult(interp), buf, -1);
2198 TclCheckBadOctal(interp, string);
2199 }
2200 return TCL_ERROR;
2201 }
2202 if (errno == ERANGE) {
2203 if (interp != NULL) {
2204 char *s = "integer value too large to represent";
2205 Tcl_ResetResult(interp);
2206 Tcl_AppendToObj(Tcl_GetObjResult(interp), s, -1);
2207 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", s, (char *) NULL);
2208 }
2209 return TCL_ERROR;
2210 }
2211
2212 /*
2213 * Make sure that the string has no garbage after the end of the int.
2214 */
2215
2216 while ((end < (string+length))
2217 && isspace(UCHAR(*end))) { /* INTL: ISO space. */
2218 end++;
2219 }
2220 if (end != (string+length)) {
2221 goto badInteger;
2222 }
2223
2224 /*
2225 * The conversion to int succeeded. Free the old internalRep before
2226 * setting the new one. We do this as late as possible to allow the
2227 * conversion code, in particular Tcl_GetStringFromObj, to use that old
2228 * internalRep.
2229 */
2230
2231 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2232 oldTypePtr->freeIntRepProc(objPtr);
2233 }
2234
2235 objPtr->internalRep.wideValue = newWide;
2236 #else
2237 if (TCL_ERROR == SetIntFromAny(interp, objPtr)) {
2238 return TCL_ERROR;
2239 }
2240 #endif
2241 objPtr->typePtr = &tclWideIntType;
2242 return TCL_OK;
2243 }
2244
2245 /*
2246 *----------------------------------------------------------------------
2247 *
2248 * UpdateStringOfWideInt --
2249 *
2250 * Update the string representation for a wide integer object.
2251 * Note: This procedure does not free an existing old string rep
2252 * so storage will be lost if this has not already been done.
2253 *
2254 * Results:
2255 * None.
2256 *
2257 * Side effects:
2258 * The object's string is set to a valid string that results from
2259 * the wideInt-to-string conversion.
2260 *
2261 *----------------------------------------------------------------------
2262 */
2263
2264 #ifndef TCL_WIDE_INT_IS_LONG
2265 static void
2266 UpdateStringOfWideInt(objPtr)
2267 register Tcl_Obj *objPtr; /* Int object whose string rep to update. */
2268 {
2269 char buffer[TCL_INTEGER_SPACE+2];
2270 register unsigned len;
2271 register Tcl_WideInt wideVal = objPtr->internalRep.wideValue;
2272
2273 /*
2274 * Note that sprintf will generate a compiler warning under
2275 * Mingw claiming %I64 is an unknown format specifier.
2276 * Just ignore this warning. We can't use %L as the format
2277 * specifier since that gets printed as a 32 bit value.
2278 */
2279 sprintf(buffer, "%" TCL_LL_MODIFIER "d", wideVal);
2280 len = strlen(buffer);
2281 objPtr->bytes = ckalloc((unsigned) len + 1);
2282 memcpy(objPtr->bytes, buffer, len + 1);
2283 objPtr->length = len;
2284 }
2285 #endif /* TCL_WIDE_INT_IS_LONG */
2286
2287 /*
2288 *----------------------------------------------------------------------
2289 *
2290 * Tcl_NewWideIntObj --
2291 *
2292 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
2293 * Tcl_NewWideIntObj to create a new 64-bit integer object end up calling
2294 * the debugging procedure Tcl_DbNewWideIntObj instead.
2295 *
2296 * Otherwise, if the client is compiled without TCL_MEM_DEBUG defined,
2297 * calls to Tcl_NewWideIntObj result in a call to one of the two
2298 * Tcl_NewWideIntObj implementations below. We provide two implementations
2299 * so that the Tcl core can be compiled to do memory debugging of the
2300 * core even if a client does not request it for itself.
2301 *
2302 * Results:
2303 * The newly created object is returned. This object will have an
2304 * invalid string representation. The returned object has ref count 0.
2305 *
2306 * Side effects:
2307 * None.
2308 *
2309 *----------------------------------------------------------------------
2310 */
2311
2312 #ifdef TCL_MEM_DEBUG
2313 #undef Tcl_NewWideIntObj
2314
2315 Tcl_Obj *
2316 Tcl_NewWideIntObj(wideValue)
2317 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2318 * the new object. */
2319 {
2320 return Tcl_DbNewWideIntObj(wideValue, "unknown", 0);
2321 }
2322
2323 #else /* if not TCL_MEM_DEBUG */
2324
2325 Tcl_Obj *
2326 Tcl_NewWideIntObj(wideValue)
2327 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2328 * the new object. */
2329 {
2330 register Tcl_Obj *objPtr;
2331
2332 TclNewObj(objPtr);
2333 objPtr->bytes = NULL;
2334
2335 objPtr->internalRep.wideValue = wideValue;
2336 objPtr->typePtr = &tclWideIntType;
2337 return objPtr;
2338 }
2339 #endif /* if TCL_MEM_DEBUG */
2340
2341 /*
2342 *----------------------------------------------------------------------
2343 *
2344 * Tcl_DbNewWideIntObj --
2345 *
2346 * If a client is compiled with TCL_MEM_DEBUG defined, calls to
2347 * Tcl_NewWideIntObj to create new wide integer end up calling
2348 * the debugging procedure Tcl_DbNewWideIntObj instead. We
2349 * provide two implementations of Tcl_DbNewWideIntObj so that
2350 * whether the Tcl core is compiled to do memory debugging of the
2351 * core is independent of whether a client requests debugging for
2352 * itself.
2353 *
2354 * When the core is compiled with TCL_MEM_DEBUG defined,
2355 * Tcl_DbNewWideIntObj calls Tcl_DbCkalloc directly with the file
2356 * name and line number from its caller. This simplifies
2357 * debugging since then the checkmem command will report the
2358 * caller's file name and line number when reporting objects that
2359 * haven't been freed.
2360 *
2361 * Otherwise, when the core is compiled without TCL_MEM_DEBUG defined,
2362 * this procedure just returns the result of calling Tcl_NewWideIntObj.
2363 *
2364 * Results:
2365 * The newly created wide integer object is returned. This object
2366 * will have an invalid string representation. The returned object has
2367 * ref count 0.
2368 *
2369 * Side effects:
2370 * Allocates memory.
2371 *
2372 *----------------------------------------------------------------------
2373 */
2374
2375 #ifdef TCL_MEM_DEBUG
2376
2377 Tcl_Obj *
2378 Tcl_DbNewWideIntObj(wideValue, file, line)
2379 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2380 * the new object. */
2381 CONST char *file; /* The name of the source file
2382 * calling this procedure; used for
2383 * debugging. */
2384 int line; /* Line number in the source file;
2385 * used for debugging. */
2386 {
2387 register Tcl_Obj *objPtr;
2388
2389 TclDbNewObj(objPtr, file, line);
2390 objPtr->bytes = NULL;
2391
2392 objPtr->internalRep.wideValue = wideValue;
2393 objPtr->typePtr = &tclWideIntType;
2394 return objPtr;
2395 }
2396
2397 #else /* if not TCL_MEM_DEBUG */
2398
2399 Tcl_Obj *
2400 Tcl_DbNewWideIntObj(wideValue, file, line)
2401 register Tcl_WideInt wideValue; /* Long integer used to initialize
2402 * the new object. */
2403 CONST char *file; /* The name of the source file
2404 * calling this procedure; used for
2405 * debugging. */
2406 int line; /* Line number in the source file;
2407 * used for debugging. */
2408 {
2409 return Tcl_NewWideIntObj(wideValue);
2410 }
2411 #endif /* TCL_MEM_DEBUG */
2412
2413 /*
2414 *----------------------------------------------------------------------
2415 *
2416 * Tcl_SetWideIntObj --
2417 *
2418 * Modify an object to be a wide integer object and to have the
2419 * specified wide integer value.
2420 *
2421 * Results:
2422 * None.
2423 *
2424 * Side effects:
2425 * The object's old string rep, if any, is freed. Also, any old
2426 * internal rep is freed.
2427 *
2428 *----------------------------------------------------------------------
2429 */
2430
2431 void
2432 Tcl_SetWideIntObj(objPtr, wideValue)
2433 register Tcl_Obj *objPtr; /* Object w. internal rep to init. */
2434 register Tcl_WideInt wideValue; /* Wide integer used to initialize
2435 * the object's value. */
2436 {
2437 register Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2438
2439 if (Tcl_IsShared(objPtr)) {
2440 panic("Tcl_SetWideIntObj called with shared object");
2441 }
2442
2443 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2444 oldTypePtr->freeIntRepProc(objPtr);
2445 }
2446
2447 objPtr->internalRep.wideValue = wideValue;
2448 objPtr->typePtr = &tclWideIntType;
2449 Tcl_InvalidateStringRep(objPtr);
2450 }
2451
2452 /*
2453 *----------------------------------------------------------------------
2454 *
2455 * Tcl_GetWideIntFromObj --
2456 *
2457 * Attempt to return a wide integer from the Tcl object "objPtr". If
2458 * the object is not already a wide int object, an attempt will be made
2459 * to convert it to one.
2460 *
2461 * Results:
2462 * The return value is a standard Tcl object result. If an error occurs
2463 * during conversion, an error message is left in the interpreter's
2464 * result unless "interp" is NULL.
2465 *
2466 * Side effects:
2467 * If the object is not already an int object, the conversion will free
2468 * any old internal representation.
2469 *
2470 *----------------------------------------------------------------------
2471 */
2472
2473 int
2474 Tcl_GetWideIntFromObj(interp, objPtr, wideIntPtr)
2475 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
2476 register Tcl_Obj *objPtr; /* Object from which to get a wide int. */
2477 register Tcl_WideInt *wideIntPtr; /* Place to store resulting long. */
2478 {
2479 register int result;
2480
2481 if (objPtr->typePtr == &tclWideIntType) {
2482 *wideIntPtr = objPtr->internalRep.wideValue;
2483 return TCL_OK;
2484 }
2485 result = SetWideIntFromAny(interp, objPtr);
2486 if (result == TCL_OK) {
2487 *wideIntPtr = objPtr->internalRep.wideValue;
2488 }
2489 return result;
2490 }
2491
2492 /*
2493 *----------------------------------------------------------------------
2494 *
2495 * Tcl_DbIncrRefCount --
2496 *
2497 * This procedure is normally called when debugging: i.e., when
2498 * TCL_MEM_DEBUG is defined. This checks to see whether or not
2499 * the memory has been freed before incrementing the ref count.
2500 *
2501 * When TCL_MEM_DEBUG is not defined, this procedure just increments
2502 * the reference count of the object.
2503 *
2504 * Results:
2505 * None.
2506 *
2507 * Side effects:
2508 * The object's ref count is incremented.
2509 *
2510 *----------------------------------------------------------------------
2511 */
2512
2513 void
2514 Tcl_DbIncrRefCount(objPtr, file, line)
2515 register Tcl_Obj *objPtr; /* The object we are registering a
2516 * reference to. */
2517 CONST char *file; /* The name of the source file calling this
2518 * procedure; used for debugging. */
2519 int line; /* Line number in the source file; used
2520 * for debugging. */
2521 {
2522 #ifdef TCL_MEM_DEBUG
2523 if (objPtr->refCount == 0x61616161) {
2524 fprintf(stderr, "file = %s, line = %d\n", file, line);
2525 fflush(stderr);
2526 panic("Trying to increment refCount of previously disposed object.");
2527 }
2528 #endif
2529 ++(objPtr)->refCount;
2530 }
2531
2532 /*
2533 *----------------------------------------------------------------------
2534 *
2535 * Tcl_DbDecrRefCount --
2536 *
2537 * This procedure is normally called when debugging: i.e., when
2538 * TCL_MEM_DEBUG is defined. This checks to see whether or not
2539 * the memory has been freed before decrementing the ref count.
2540 *
2541 * When TCL_MEM_DEBUG is not defined, this procedure just decrements
2542 * the reference count of the object.
2543 *
2544 * Results:
2545 * None.
2546 *
2547 * Side effects:
2548 * The object's ref count is incremented.
2549 *
2550 *----------------------------------------------------------------------
2551 */
2552
2553 void
2554 Tcl_DbDecrRefCount(objPtr, file, line)
2555 register Tcl_Obj *objPtr; /* The object we are releasing a reference
2556 * to. */
2557 CONST char *file; /* The name of the source file calling this
2558 * procedure; used for debugging. */
2559 int line; /* Line number in the source file; used
2560 * for debugging. */
2561 {
2562 #ifdef TCL_MEM_DEBUG
2563 if (objPtr->refCount == 0x61616161) {
2564 fprintf(stderr, "file = %s, line = %d\n", file, line);
2565 fflush(stderr);
2566 panic("Trying to decrement refCount of previously disposed object.");
2567 }
2568 #endif
2569 if (--(objPtr)->refCount <= 0) {
2570 TclFreeObj(objPtr);
2571 }
2572 }
2573
2574 /*
2575 *----------------------------------------------------------------------
2576 *
2577 * Tcl_DbIsShared --
2578 *
2579 * This procedure is normally called when debugging: i.e., when
2580 * TCL_MEM_DEBUG is defined. It tests whether the object has a ref
2581 * count greater than one.
2582 *
2583 * When TCL_MEM_DEBUG is not defined, this procedure just tests
2584 * if the object has a ref count greater than one.
2585 *
2586 * Results:
2587 * None.
2588 *
2589 * Side effects:
2590 * None.
2591 *
2592 *----------------------------------------------------------------------
2593 */
2594
2595 int
2596 Tcl_DbIsShared(objPtr, file, line)
2597 register Tcl_Obj *objPtr; /* The object to test for being shared. */
2598 CONST char *file; /* The name of the source file calling this
2599 * procedure; used for debugging. */
2600 int line; /* Line number in the source file; used
2601 * for debugging. */
2602 {
2603 #ifdef TCL_MEM_DEBUG
2604 if (objPtr->refCount == 0x61616161) {
2605 fprintf(stderr, "file = %s, line = %d\n", file, line);
2606 fflush(stderr);
2607 panic("Trying to check whether previously disposed object is shared.");
2608 }
2609 #endif
2610 #ifdef TCL_COMPILE_STATS
2611 Tcl_MutexLock(&tclObjMutex);
2612 if ((objPtr)->refCount <= 1) {
2613 tclObjsShared[1]++;
2614 } else if ((objPtr)->refCount < TCL_MAX_SHARED_OBJ_STATS) {
2615 tclObjsShared[(objPtr)->refCount]++;
2616 } else {
2617 tclObjsShared[0]++;
2618 }
2619 Tcl_MutexUnlock(&tclObjMutex);
2620 #endif
2621 return ((objPtr)->refCount > 1);
2622 }
2623
2624 /*
2625 *----------------------------------------------------------------------
2626 *
2627 * Tcl_InitObjHashTable --
2628 *
2629 * Given storage for a hash table, set up the fields to prepare
2630 * the hash table for use, the keys are Tcl_Obj *.
2631 *
2632 * Results:
2633 * None.
2634 *
2635 * Side effects:
2636 * TablePtr is now ready to be passed to Tcl_FindHashEntry and
2637 * Tcl_CreateHashEntry.
2638 *
2639 *----------------------------------------------------------------------
2640 */
2641
2642 void
2643 Tcl_InitObjHashTable(tablePtr)
2644 register Tcl_HashTable *tablePtr; /* Pointer to table record, which
2645 * is supplied by the caller. */
2646 {
2647 Tcl_InitCustomHashTable(tablePtr, TCL_CUSTOM_PTR_KEYS,
2648 &tclObjHashKeyType);
2649 }
2650
2651 /*
2652 *----------------------------------------------------------------------
2653 *
2654 * AllocObjEntry --
2655 *
2656 * Allocate space for a Tcl_HashEntry containing the Tcl_Obj * key.
2657 *
2658 * Results:
2659 * The return value is a pointer to the created entry.
2660 *
2661 * Side effects:
2662 * Increments the reference count on the object.
2663 *
2664 *----------------------------------------------------------------------
2665 */
2666
2667 static Tcl_HashEntry *
2668 AllocObjEntry(tablePtr, keyPtr)
2669 Tcl_HashTable *tablePtr; /* Hash table. */
2670 VOID *keyPtr; /* Key to store in the hash table entry. */
2671 {
2672 Tcl_Obj *objPtr = (Tcl_Obj *) keyPtr;
2673 Tcl_HashEntry *hPtr;
2674
2675 hPtr = (Tcl_HashEntry *) ckalloc((unsigned) (sizeof(Tcl_HashEntry)));
2676 hPtr->key.oneWordValue = (char *) objPtr;
2677 Tcl_IncrRefCount (objPtr);
2678
2679 return hPtr;
2680 }
2681
2682 /*
2683 *----------------------------------------------------------------------
2684 *
2685 * CompareObjKeys --
2686 *
2687 * Compares two Tcl_Obj * keys.
2688 *
2689 * Results:
2690 * The return value is 0 if they are different and 1 if they are
2691 * the same.
2692 *
2693 * Side effects:
2694 * None.
2695 *
2696 *----------------------------------------------------------------------
2697 */
2698
2699 static int
2700 CompareObjKeys(keyPtr, hPtr)
2701 VOID *keyPtr; /* New key to compare. */
2702 Tcl_HashEntry *hPtr; /* Existing key to compare. */
2703 {
2704 Tcl_Obj *objPtr1 = (Tcl_Obj *) keyPtr;
2705 Tcl_Obj *objPtr2 = (Tcl_Obj *) hPtr->key.oneWordValue;
2706 register CONST char *p1, *p2;
2707 register int l1, l2;
2708
2709 /*
2710 * If the object pointers are the same then they match.
2711 */
2712 if (objPtr1 == objPtr2) {
2713 return 1;
2714 }
2715
2716 /*
2717 * Don't use Tcl_GetStringFromObj as it would prevent l1 and l2 being
2718 * in a register.
2719 */
2720 p1 = Tcl_GetString (objPtr1);
2721 l1 = objPtr1->length;
2722 p2 = Tcl_GetString (objPtr2);
2723 l2 = objPtr2->length;
2724
2725 /*
2726 * Only compare if the string representations are of the same length.
2727 */
2728 if (l1 == l2) {
2729 for (;; p1++, p2++, l1--) {
2730 if (*p1 != *p2) {
2731 break;
2732 }
2733 if (l1 == 0) {
2734 return 1;
2735 }
2736 }
2737 }
2738
2739 return 0;
2740 }
2741
2742 /*
2743 *----------------------------------------------------------------------
2744 *
2745 * FreeObjEntry --
2746 *
2747 * Frees space for a Tcl_HashEntry containing the Tcl_Obj * key.
2748 *
2749 * Results:
2750 * The return value is a pointer to the created entry.
2751 *
2752 * Side effects:
2753 * Decrements the reference count of the object.
2754 *
2755 *----------------------------------------------------------------------
2756 */
2757
2758 static void
2759 FreeObjEntry(hPtr)
2760 Tcl_HashEntry *hPtr; /* Hash entry to free. */
2761 {
2762 Tcl_Obj *objPtr = (Tcl_Obj *) hPtr->key.oneWordValue;
2763
2764 Tcl_DecrRefCount (objPtr);
2765 ckfree ((char *) hPtr);
2766 }
2767
2768 /*
2769 *----------------------------------------------------------------------
2770 *
2771 * HashObjKey --
2772 *
2773 * Compute a one-word summary of the string representation of the
2774 * Tcl_Obj, which can be used to generate a hash index.
2775 *
2776 * Results:
2777 * The return value is a one-word summary of the information in
2778 * the string representation of the Tcl_Obj.
2779 *
2780 * Side effects:
2781 * None.
2782 *
2783 *----------------------------------------------------------------------
2784 */
2785
2786 static unsigned int
2787 HashObjKey(tablePtr, keyPtr)
2788 Tcl_HashTable *tablePtr; /* Hash table. */
2789 VOID *keyPtr; /* Key from which to compute hash value. */
2790 {
2791 Tcl_Obj *objPtr = (Tcl_Obj *) keyPtr;
2792 register CONST char *string;
2793 register int length;
2794 register unsigned int result;
2795 register int c;
2796
2797 string = Tcl_GetString (objPtr);
2798 length = objPtr->length;
2799
2800 /*
2801 * I tried a zillion different hash functions and asked many other
2802 * people for advice. Many people had their own favorite functions,
2803 * all different, but no-one had much idea why they were good ones.
2804 * I chose the one below (multiply by 9 and add new character)
2805 * because of the following reasons:
2806 *
2807 * 1. Multiplying by 10 is perfect for keys that are decimal strings,
2808 * and multiplying by 9 is just about as good.
2809 * 2. Times-9 is (shift-left-3) plus (old). This means that each
2810 * character's bits hang around in the low-order bits of the
2811 * hash value for ever, plus they spread fairly rapidly up to
2812 * the high-order bits to fill out the hash value. This seems
2813 * works well both for decimal and non-decimal strings.
2814 */
2815
2816 result = 0;
2817 while (length) {
2818 c = *string;
2819 string++;
2820 length--;
2821 if (length == 0) {
2822 break;
2823 }
2824 result += (result<<3) + c;
2825 }
2826 return result;
2827 }
2828
2829 /*
2830 *----------------------------------------------------------------------
2831 *
2832 * Tcl_GetCommandFromObj --
2833 *
2834 * Returns the command specified by the name in a Tcl_Obj.
2835 *
2836 * Results:
2837 * Returns a token for the command if it is found. Otherwise, if it
2838 * can't be found or there is an error, returns NULL.
2839 *
2840 * Side effects:
2841 * May update the internal representation for the object, caching
2842 * the command reference so that the next time this procedure is
2843 * called with the same object, the command can be found quickly.
2844 *
2845 *----------------------------------------------------------------------
2846 */
2847
2848 Tcl_Command
2849 Tcl_GetCommandFromObj(interp, objPtr)
2850 Tcl_Interp *interp; /* The interpreter in which to resolve the
2851 * command and to report errors. */
2852 register Tcl_Obj *objPtr; /* The object containing the command's
2853 * name. If the name starts with "::", will
2854 * be looked up in global namespace. Else,
2855 * looked up first in the current namespace,
2856 * then in global namespace. */
2857 {
2858 Interp *iPtr = (Interp *) interp;
2859 register ResolvedCmdName *resPtr;
2860 register Command *cmdPtr;
2861 Namespace *currNsPtr;
2862 int result;
2863 CallFrame *savedFramePtr;
2864 char *name;
2865
2866 /*
2867 * If the variable name is fully qualified, do as if the lookup were
2868 * done from the global namespace; this helps avoid repeated lookups
2869 * of fully qualified names. It costs close to nothing, and may be very
2870 * helpful for OO applications which pass along a command name ("this"),
2871 * [Patch 456668]
2872 */
2873
2874 savedFramePtr = iPtr->varFramePtr;
2875 name = Tcl_GetString(objPtr);
2876 if ((*name++ == ':') && (*name == ':')) {
2877 iPtr->varFramePtr = NULL;
2878 }
2879
2880 /*
2881 * Get the internal representation, converting to a command type if
2882 * needed. The internal representation is a ResolvedCmdName that points
2883 * to the actual command.
2884 */
2885
2886 if (objPtr->typePtr != &tclCmdNameType) {
2887 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
2888 if (result != TCL_OK) {
2889 iPtr->varFramePtr = savedFramePtr;
2890 return (Tcl_Command) NULL;
2891 }
2892 }
2893 resPtr = (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
2894
2895 /*
2896 * Get the current namespace.
2897 */
2898
2899 if (iPtr->varFramePtr != NULL) {
2900 currNsPtr = iPtr->varFramePtr->nsPtr;
2901 } else {
2902 currNsPtr = iPtr->globalNsPtr;
2903 }
2904
2905 /*
2906 * Check the context namespace and the namespace epoch of the resolved
2907 * symbol to make sure that it is fresh. If not, then force another
2908 * conversion to the command type, to discard the old rep and create a
2909 * new one. Note that we verify that the namespace id of the context
2910 * namespace is the same as the one we cached; this insures that the
2911 * namespace wasn't deleted and a new one created at the same address
2912 * with the same command epoch.
2913 */
2914
2915 cmdPtr = NULL;
2916 if ((resPtr != NULL)
2917 && (resPtr->refNsPtr == currNsPtr)
2918 && (resPtr->refNsId == currNsPtr->nsId)
2919 && (resPtr->refNsCmdEpoch == currNsPtr->cmdRefEpoch)) {
2920 cmdPtr = resPtr->cmdPtr;
2921 if (cmdPtr->cmdEpoch != resPtr->cmdEpoch) {
2922 cmdPtr = NULL;
2923 }
2924 }
2925
2926 if (cmdPtr == NULL) {
2927 result = tclCmdNameType.setFromAnyProc(interp, objPtr);
2928 if (result != TCL_OK) {
2929 iPtr->varFramePtr = savedFramePtr;
2930 return (Tcl_Command) NULL;
2931 }
2932 resPtr = (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
2933 if (resPtr != NULL) {
2934 cmdPtr = resPtr->cmdPtr;
2935 }
2936 }
2937 iPtr->varFramePtr = savedFramePtr;
2938 return (Tcl_Command) cmdPtr;
2939 }
2940
2941 /*
2942 *----------------------------------------------------------------------
2943 *
2944 * TclSetCmdNameObj --
2945 *
2946 * Modify an object to be an CmdName object that refers to the argument
2947 * Command structure.
2948 *
2949 * Results:
2950 * None.
2951 *
2952 * Side effects:
2953 * The object's old internal rep is freed. It's string rep is not
2954 * changed. The refcount in the Command structure is incremented to
2955 * keep it from being freed if the command is later deleted until
2956 * TclExecuteByteCode has a chance to recognize that it was deleted.
2957 *
2958 *----------------------------------------------------------------------
2959 */
2960
2961 void
2962 TclSetCmdNameObj(interp, objPtr, cmdPtr)
2963 Tcl_Interp *interp; /* Points to interpreter containing command
2964 * that should be cached in objPtr. */
2965 register Tcl_Obj *objPtr; /* Points to Tcl object to be changed to
2966 * a CmdName object. */
2967 Command *cmdPtr; /* Points to Command structure that the
2968 * CmdName object should refer to. */
2969 {
2970 Interp *iPtr = (Interp *) interp;
2971 register ResolvedCmdName *resPtr;
2972 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
2973 register Namespace *currNsPtr;
2974
2975 if (oldTypePtr == &tclCmdNameType) {
2976 return;
2977 }
2978
2979 /*
2980 * Get the current namespace.
2981 */
2982
2983 if (iPtr->varFramePtr != NULL) {
2984 currNsPtr = iPtr->varFramePtr->nsPtr;
2985 } else {
2986 currNsPtr = iPtr->globalNsPtr;
2987 }
2988
2989 cmdPtr->refCount++;
2990 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
2991 resPtr->cmdPtr = cmdPtr;
2992 resPtr->refNsPtr = currNsPtr;
2993 resPtr->refNsId = currNsPtr->nsId;
2994 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
2995 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
2996 resPtr->refCount = 1;
2997
2998 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
2999 oldTypePtr->freeIntRepProc(objPtr);
3000 }
3001 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3002 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
3003 objPtr->typePtr = &tclCmdNameType;
3004 }
3005
3006 /*
3007 *----------------------------------------------------------------------
3008 *
3009 * FreeCmdNameInternalRep --
3010 *
3011 * Frees the resources associated with a cmdName object's internal
3012 * representation.
3013 *
3014 * Results:
3015 * None.
3016 *
3017 * Side effects:
3018 * Decrements the ref count of any cached ResolvedCmdName structure
3019 * pointed to by the cmdName's internal representation. If this is
3020 * the last use of the ResolvedCmdName, it is freed. This in turn
3021 * decrements the ref count of the Command structure pointed to by
3022 * the ResolvedSymbol, which may free the Command structure.
3023 *
3024 *----------------------------------------------------------------------
3025 */
3026
3027 static void
3028 FreeCmdNameInternalRep(objPtr)
3029 register Tcl_Obj *objPtr; /* CmdName object with internal
3030 * representation to free. */
3031 {
3032 register ResolvedCmdName *resPtr =
3033 (ResolvedCmdName *) objPtr->internalRep.twoPtrValue.ptr1;
3034
3035 if (resPtr != NULL) {
3036 /*
3037 * Decrement the reference count of the ResolvedCmdName structure.
3038 * If there are no more uses, free the ResolvedCmdName structure.
3039 */
3040
3041 resPtr->refCount--;
3042 if (resPtr->refCount == 0) {
3043 /*
3044 * Now free the cached command, unless it is still in its
3045 * hash table or if there are other references to it
3046 * from other cmdName objects.
3047 */
3048
3049 Command *cmdPtr = resPtr->cmdPtr;
3050 TclCleanupCommand(cmdPtr);
3051 ckfree((char *) resPtr);
3052 }
3053 }
3054 }
3055
3056 /*
3057 *----------------------------------------------------------------------
3058 *
3059 * DupCmdNameInternalRep --
3060 *
3061 * Initialize the internal representation of an cmdName Tcl_Obj to a
3062 * copy of the internal representation of an existing cmdName object.
3063 *
3064 * Results:
3065 * None.
3066 *
3067 * Side effects:
3068 * "copyPtr"s internal rep is set to point to the ResolvedCmdName
3069 * structure corresponding to "srcPtr"s internal rep. Increments the
3070 * ref count of the ResolvedCmdName structure pointed to by the
3071 * cmdName's internal representation.
3072 *
3073 *----------------------------------------------------------------------
3074 */
3075
3076 static void
3077 DupCmdNameInternalRep(srcPtr, copyPtr)
3078 Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
3079 register Tcl_Obj *copyPtr; /* Object with internal rep to set. */
3080 {
3081 register ResolvedCmdName *resPtr =
3082 (ResolvedCmdName *) srcPtr->internalRep.twoPtrValue.ptr1;
3083
3084 copyPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3085 copyPtr->internalRep.twoPtrValue.ptr2 = NULL;
3086 if (resPtr != NULL) {
3087 resPtr->refCount++;
3088 }
3089 copyPtr->typePtr = &tclCmdNameType;
3090 }
3091
3092 /*
3093 *----------------------------------------------------------------------
3094 *
3095 * SetCmdNameFromAny --
3096 *
3097 * Generate an cmdName internal form for the Tcl object "objPtr".
3098 *
3099 * Results:
3100 * The return value is a standard Tcl result. The conversion always
3101 * succeeds and TCL_OK is returned.
3102 *
3103 * Side effects:
3104 * A pointer to a ResolvedCmdName structure that holds a cached pointer
3105 * to the command with a name that matches objPtr's string rep is
3106 * stored as objPtr's internal representation. This ResolvedCmdName
3107 * pointer will be NULL if no matching command was found. The ref count
3108 * of the cached Command's structure (if any) is also incremented.
3109 *
3110 *----------------------------------------------------------------------
3111 */
3112
3113 static int
3114 SetCmdNameFromAny(interp, objPtr)
3115 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
3116 register Tcl_Obj *objPtr; /* The object to convert. */
3117 {
3118 Interp *iPtr = (Interp *) interp;
3119 char *name;
3120 Tcl_Command cmd;
3121 register Command *cmdPtr;
3122 Namespace *currNsPtr;
3123 register ResolvedCmdName *resPtr;
3124
3125 /*
3126 * Get "objPtr"s string representation. Make it up-to-date if necessary.
3127 */
3128
3129 name = objPtr->bytes;
3130 if (name == NULL) {
3131 name = Tcl_GetString(objPtr);
3132 }
3133
3134 /*
3135 * Find the Command structure, if any, that describes the command called
3136 * "name". Build a ResolvedCmdName that holds a cached pointer to this
3137 * Command, and bump the reference count in the referenced Command
3138 * structure. A Command structure will not be deleted as long as it is
3139 * referenced from a CmdName object.
3140 */
3141
3142 cmd = Tcl_FindCommand(interp, name, (Tcl_Namespace *) NULL,
3143 /*flags*/ 0);
3144 cmdPtr = (Command *) cmd;
3145 if (cmdPtr != NULL) {
3146 /*
3147 * Get the current namespace.
3148 */
3149
3150 if (iPtr->varFramePtr != NULL) {
3151 currNsPtr = iPtr->varFramePtr->nsPtr;
3152 } else {
3153 currNsPtr = iPtr->globalNsPtr;
3154 }
3155
3156 cmdPtr->refCount++;
3157 resPtr = (ResolvedCmdName *) ckalloc(sizeof(ResolvedCmdName));
3158 resPtr->cmdPtr = cmdPtr;
3159 resPtr->refNsPtr = currNsPtr;
3160 resPtr->refNsId = currNsPtr->nsId;
3161 resPtr->refNsCmdEpoch = currNsPtr->cmdRefEpoch;
3162 resPtr->cmdEpoch = cmdPtr->cmdEpoch;
3163 resPtr->refCount = 1;
3164 } else {
3165 resPtr = NULL; /* no command named "name" was found */
3166 }
3167
3168 /*
3169 * Free the old internalRep before setting the new one. We do this as
3170 * late as possible to allow the conversion code, in particular
3171 * GetStringFromObj, to use that old internalRep. If no Command
3172 * structure was found, leave NULL as the cached value.
3173 */
3174
3175 if ((objPtr->typePtr != NULL)
3176 && (objPtr->typePtr->freeIntRepProc != NULL)) {
3177 objPtr->typePtr->freeIntRepProc(objPtr);
3178 }
3179
3180 objPtr->internalRep.twoPtrValue.ptr1 = (VOID *) resPtr;
3181 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
3182 objPtr->typePtr = &tclCmdNameType;
3183 return TCL_OK;
3184 }