| 1 |
/* |
| 2 |
* tclListObj.c -- |
| 3 |
* |
| 4 |
* This file contains functions that implement the Tcl list object type. |
| 5 |
* |
| 6 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 7 |
* Copyright (c) 1998 by Scriptics Corporation. |
| 8 |
* Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* Prototypes for functions defined later in this file: |
| 18 |
*/ |
| 19 |
|
| 20 |
static List * AttemptNewList(Tcl_Interp *interp, int objc, |
| 21 |
Tcl_Obj *const objv[]); |
| 22 |
static List * NewListInternalRep(int objc, Tcl_Obj *const objv[], int p); |
| 23 |
static void DupListInternalRep(Tcl_Obj *srcPtr, Tcl_Obj *copyPtr); |
| 24 |
static void FreeListInternalRep(Tcl_Obj *listPtr); |
| 25 |
static int SetListFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 26 |
static void UpdateStringOfList(Tcl_Obj *listPtr); |
| 27 |
|
| 28 |
/* |
| 29 |
* The structure below defines the list Tcl object type by means of functions |
| 30 |
* that can be invoked by generic object code. |
| 31 |
* |
| 32 |
* The internal representation of a list object is a two-pointer |
| 33 |
* representation. The first pointer designates a List structure that contains |
| 34 |
* an array of pointers to the element objects, together with integers that |
| 35 |
* represent the current element count and the allocated size of the array. |
| 36 |
* The second pointer is normally NULL; during execution of functions in this |
| 37 |
* file that operate on nested sublists, it is occasionally used as working |
| 38 |
* storage to avoid an auxiliary stack. |
| 39 |
*/ |
| 40 |
|
| 41 |
const Tcl_ObjType tclListType = { |
| 42 |
"list", /* name */ |
| 43 |
FreeListInternalRep, /* freeIntRepProc */ |
| 44 |
DupListInternalRep, /* dupIntRepProc */ |
| 45 |
UpdateStringOfList, /* updateStringProc */ |
| 46 |
SetListFromAny /* setFromAnyProc */ |
| 47 |
}; |
| 48 |
|
| 49 |
/* Macros to manipulate the List internal rep */ |
| 50 |
|
| 51 |
#define ListSetInternalRep(objPtr, listRepPtr) \ |
| 52 |
(objPtr)->internalRep.twoPtrValue.ptr1 = (void *)(listRepPtr), \ |
| 53 |
(objPtr)->internalRep.twoPtrValue.ptr2 = NULL, \ |
| 54 |
(listRepPtr)->refCount++, \ |
| 55 |
(objPtr)->typePtr = &tclListType |
| 56 |
|
| 57 |
|
| 58 |
#ifndef TCL_MIN_ELEMENT_GROWTH |
| 59 |
#define TCL_MIN_ELEMENT_GROWTH TCL_MIN_GROWTH/sizeof(Tcl_Obj *) |
| 60 |
#endif |
| 61 |
|
| 62 |
/* |
| 63 |
*---------------------------------------------------------------------- |
| 64 |
* |
| 65 |
* NewListInternalRep -- |
| 66 |
* |
| 67 |
* Creates a 'List' structure with space for 'objc' elements. 'objc' must |
| 68 |
* be > 0. If 'objv' is not NULL, The list is initialized with first |
| 69 |
* 'objc' values in that array. Otherwise the list is initialized to have |
| 70 |
* 0 elements, with space to add 'objc' more. Flag value 'p' indicates |
| 71 |
* how to behave on failure. |
| 72 |
* |
| 73 |
* Value |
| 74 |
* |
| 75 |
* A new 'List' structure with refCount 0. If some failure |
| 76 |
* prevents this NULL is returned if 'p' is 0 , and 'Tcl_Panic' |
| 77 |
* is called if it is not. |
| 78 |
* |
| 79 |
* Effect |
| 80 |
* |
| 81 |
* The refCount of each value in 'objv' is incremented as it is added |
| 82 |
* to the list. |
| 83 |
* |
| 84 |
*---------------------------------------------------------------------- |
| 85 |
*/ |
| 86 |
|
| 87 |
static List * |
| 88 |
NewListInternalRep( |
| 89 |
int objc, |
| 90 |
Tcl_Obj *const objv[], |
| 91 |
int p) |
| 92 |
{ |
| 93 |
List *listRepPtr; |
| 94 |
|
| 95 |
if (objc <= 0) { |
| 96 |
Tcl_Panic("NewListInternalRep: expects positive element count"); |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* First check to see if we'd overflow and try to allocate an object |
| 101 |
* larger than our memory allocator allows. Note that this is actually a |
| 102 |
* fairly small value when you're on a serious 64-bit machine, but that |
| 103 |
* requires API changes to fix. See [Bug 219196] for a discussion. |
| 104 |
*/ |
| 105 |
|
| 106 |
if ((size_t)objc > LIST_MAX) { |
| 107 |
if (p) { |
| 108 |
Tcl_Panic("max length of a Tcl list (%d elements) exceeded", |
| 109 |
LIST_MAX); |
| 110 |
} |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
listRepPtr = (List *)attemptckalloc(LIST_SIZE(objc)); |
| 115 |
if (listRepPtr == NULL) { |
| 116 |
if (p) { |
| 117 |
Tcl_Panic("list creation failed: unable to alloc %u bytes", |
| 118 |
LIST_SIZE(objc)); |
| 119 |
} |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
listRepPtr->canonicalFlag = 0; |
| 124 |
listRepPtr->refCount = 0; |
| 125 |
listRepPtr->maxElemCount = objc; |
| 126 |
|
| 127 |
if (objv) { |
| 128 |
Tcl_Obj **elemPtrs; |
| 129 |
int i; |
| 130 |
|
| 131 |
listRepPtr->elemCount = objc; |
| 132 |
elemPtrs = &listRepPtr->elements; |
| 133 |
for (i = 0; i < objc; i++) { |
| 134 |
elemPtrs[i] = objv[i]; |
| 135 |
Tcl_IncrRefCount(elemPtrs[i]); |
| 136 |
} |
| 137 |
} else { |
| 138 |
listRepPtr->elemCount = 0; |
| 139 |
} |
| 140 |
return listRepPtr; |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
*---------------------------------------------------------------------- |
| 145 |
* |
| 146 |
* AttemptNewList -- |
| 147 |
* |
| 148 |
* Like NewListInternalRep, but additionally sets an error message on failure. |
| 149 |
* |
| 150 |
*---------------------------------------------------------------------- |
| 151 |
*/ |
| 152 |
|
| 153 |
static List * |
| 154 |
AttemptNewList( |
| 155 |
Tcl_Interp *interp, |
| 156 |
int objc, |
| 157 |
Tcl_Obj *const objv[]) |
| 158 |
{ |
| 159 |
List *listRepPtr = NewListInternalRep(objc, objv, 0); |
| 160 |
|
| 161 |
if (interp != NULL && listRepPtr == NULL) { |
| 162 |
if (objc > LIST_MAX) { |
| 163 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 164 |
"max length of a Tcl list (%d elements) exceeded", |
| 165 |
LIST_MAX)); |
| 166 |
} else { |
| 167 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 168 |
"list creation failed: unable to alloc %u bytes", |
| 169 |
LIST_SIZE(objc))); |
| 170 |
} |
| 171 |
Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL); |
| 172 |
} |
| 173 |
return listRepPtr; |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
*---------------------------------------------------------------------- |
| 178 |
* |
| 179 |
* Tcl_NewListObj -- |
| 180 |
* |
| 181 |
* Creates a new list object and adds values to it. When TCL_MEM_DEBUG is |
| 182 |
* defined, 'Tcl_DbNewListObj' is called instead. |
| 183 |
* |
| 184 |
* Value |
| 185 |
* |
| 186 |
* A new list 'Tcl_Obj' to which is appended values from 'objv', or if |
| 187 |
* 'objc' is less than or equal to zero, a list 'Tcl_Obj' having no |
| 188 |
* elements. The string representation of the new 'Tcl_Obj' is set to |
| 189 |
* NULL. The refCount of the list is 0. |
| 190 |
* |
| 191 |
* Effect |
| 192 |
* |
| 193 |
* The refCount of each elements in 'objv' is incremented as it is added |
| 194 |
* to the list. |
| 195 |
* |
| 196 |
*---------------------------------------------------------------------- |
| 197 |
*/ |
| 198 |
|
| 199 |
#ifdef TCL_MEM_DEBUG |
| 200 |
#undef Tcl_NewListObj |
| 201 |
|
| 202 |
Tcl_Obj * |
| 203 |
Tcl_NewListObj( |
| 204 |
int objc, /* Count of objects referenced by objv. */ |
| 205 |
Tcl_Obj *const objv[]) /* An array of pointers to Tcl objects. */ |
| 206 |
{ |
| 207 |
return Tcl_DbNewListObj(objc, objv, "unknown", 0); |
| 208 |
} |
| 209 |
|
| 210 |
#else /* if not TCL_MEM_DEBUG */ |
| 211 |
|
| 212 |
Tcl_Obj * |
| 213 |
Tcl_NewListObj( |
| 214 |
int objc, /* Count of objects referenced by objv. */ |
| 215 |
Tcl_Obj *const objv[]) /* An array of pointers to Tcl objects. */ |
| 216 |
{ |
| 217 |
List *listRepPtr; |
| 218 |
Tcl_Obj *listPtr; |
| 219 |
|
| 220 |
TclNewObj(listPtr); |
| 221 |
|
| 222 |
if (objc <= 0) { |
| 223 |
return listPtr; |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* Create the internal rep. |
| 228 |
*/ |
| 229 |
|
| 230 |
listRepPtr = NewListInternalRep(objc, objv, 1); |
| 231 |
|
| 232 |
/* |
| 233 |
* Now create the object. |
| 234 |
*/ |
| 235 |
|
| 236 |
TclInvalidateStringRep(listPtr); |
| 237 |
ListSetInternalRep(listPtr, listRepPtr); |
| 238 |
return listPtr; |
| 239 |
} |
| 240 |
#endif /* if TCL_MEM_DEBUG */ |
| 241 |
|
| 242 |
/* |
| 243 |
*---------------------------------------------------------------------- |
| 244 |
* |
| 245 |
* Tcl_DbNewListObj -- |
| 246 |
* |
| 247 |
* Like 'Tcl_NewListObj', but it calls Tcl_DbCkalloc directly with the |
| 248 |
* file name and line number from its caller. This simplifies debugging |
| 249 |
* since the [memory active] command will report the correct file |
| 250 |
* name and line number when reporting objects that haven't been freed. |
| 251 |
* |
| 252 |
* When TCL_MEM_DEBUG is not defined, 'Tcl_NewListObj' is called instead. |
| 253 |
* |
| 254 |
*---------------------------------------------------------------------- |
| 255 |
*/ |
| 256 |
|
| 257 |
#ifdef TCL_MEM_DEBUG |
| 258 |
|
| 259 |
Tcl_Obj * |
| 260 |
Tcl_DbNewListObj( |
| 261 |
int objc, /* Count of objects referenced by objv. */ |
| 262 |
Tcl_Obj *const objv[], /* An array of pointers to Tcl objects. */ |
| 263 |
const char *file, /* The name of the source file calling this |
| 264 |
* function; used for debugging. */ |
| 265 |
int line) /* Line number in the source file; used for |
| 266 |
* debugging. */ |
| 267 |
{ |
| 268 |
Tcl_Obj *listPtr; |
| 269 |
List *listRepPtr; |
| 270 |
|
| 271 |
TclDbNewObj(listPtr, file, line); |
| 272 |
|
| 273 |
if (objc <= 0) { |
| 274 |
return listPtr; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Create the internal rep. |
| 279 |
*/ |
| 280 |
|
| 281 |
listRepPtr = NewListInternalRep(objc, objv, 1); |
| 282 |
|
| 283 |
/* |
| 284 |
* Now create the object. |
| 285 |
*/ |
| 286 |
|
| 287 |
TclInvalidateStringRep(listPtr); |
| 288 |
ListSetInternalRep(listPtr, listRepPtr); |
| 289 |
|
| 290 |
return listPtr; |
| 291 |
} |
| 292 |
|
| 293 |
#else /* if not TCL_MEM_DEBUG */ |
| 294 |
|
| 295 |
Tcl_Obj * |
| 296 |
Tcl_DbNewListObj( |
| 297 |
int objc, /* Count of objects referenced by objv. */ |
| 298 |
Tcl_Obj *const objv[], /* An array of pointers to Tcl objects. */ |
| 299 |
const char *file, /* The name of the source file calling this |
| 300 |
* function; used for debugging. */ |
| 301 |
int line) /* Line number in the source file; used for |
| 302 |
* debugging. */ |
| 303 |
{ |
| 304 |
return Tcl_NewListObj(objc, objv); |
| 305 |
} |
| 306 |
#endif /* TCL_MEM_DEBUG */ |
| 307 |
|
| 308 |
/* |
| 309 |
*---------------------------------------------------------------------- |
| 310 |
* |
| 311 |
* Tcl_SetListObj -- |
| 312 |
* |
| 313 |
* Like 'Tcl_NewListObj', but operates on an existing 'Tcl_Obj'instead of |
| 314 |
* creating a new one. |
| 315 |
* |
| 316 |
*---------------------------------------------------------------------- |
| 317 |
*/ |
| 318 |
|
| 319 |
void |
| 320 |
Tcl_SetListObj( |
| 321 |
Tcl_Obj *objPtr, /* Object whose internal rep to init. */ |
| 322 |
int objc, /* Count of objects referenced by objv. */ |
| 323 |
Tcl_Obj *const objv[]) /* An array of pointers to Tcl objects. */ |
| 324 |
{ |
| 325 |
List *listRepPtr; |
| 326 |
|
| 327 |
if (Tcl_IsShared(objPtr)) { |
| 328 |
Tcl_Panic("%s called with shared object", "Tcl_SetListObj"); |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
* Free any old string rep and any internal rep for the old type. |
| 333 |
*/ |
| 334 |
|
| 335 |
TclFreeIntRep(objPtr); |
| 336 |
TclInvalidateStringRep(objPtr); |
| 337 |
|
| 338 |
/* |
| 339 |
* Set the object's type to "list" and initialize the internal rep. |
| 340 |
* However, if there are no elements to put in the list, just give the |
| 341 |
* object an empty string rep and a NULL type. |
| 342 |
*/ |
| 343 |
|
| 344 |
if (objc > 0) { |
| 345 |
listRepPtr = NewListInternalRep(objc, objv, 1); |
| 346 |
ListSetInternalRep(objPtr, listRepPtr); |
| 347 |
} else { |
| 348 |
objPtr->bytes = tclEmptyStringRep; |
| 349 |
objPtr->length = 0; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* |
| 354 |
*---------------------------------------------------------------------- |
| 355 |
* |
| 356 |
* TclListObjCopy -- |
| 357 |
* |
| 358 |
* Creates a new 'Tcl_Obj' which is a pure copy of a list value. This |
| 359 |
* provides for the C level a counterpart of the [lrange $list 0 end] |
| 360 |
* command, while using internals details to be as efficient as possible. |
| 361 |
* |
| 362 |
* Value |
| 363 |
* |
| 364 |
* The address of the new 'Tcl_Obj' which shares its internal |
| 365 |
* representation with 'listPtr', and whose refCount is 0. If 'listPtr' |
| 366 |
* is not actually a list, the value is NULL, and an error message is left |
| 367 |
* in 'interp' if it is not NULL. |
| 368 |
* |
| 369 |
* Effect |
| 370 |
* |
| 371 |
* 'listPtr' is converted to a list if it isn't one already. |
| 372 |
* |
| 373 |
*---------------------------------------------------------------------- |
| 374 |
*/ |
| 375 |
|
| 376 |
Tcl_Obj * |
| 377 |
TclListObjCopy( |
| 378 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 379 |
Tcl_Obj *listPtr) /* List object for which an element array is |
| 380 |
* to be returned. */ |
| 381 |
{ |
| 382 |
Tcl_Obj *copyPtr; |
| 383 |
|
| 384 |
if (listPtr->typePtr != &tclListType) { |
| 385 |
if (SetListFromAny(interp, listPtr) != TCL_OK) { |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
TclNewObj(copyPtr); |
| 391 |
TclInvalidateStringRep(copyPtr); |
| 392 |
DupListInternalRep(listPtr, copyPtr); |
| 393 |
return copyPtr; |
| 394 |
} |
| 395 |
|
| 396 |
/* |
| 397 |
*---------------------------------------------------------------------- |
| 398 |
* |
| 399 |
* Tcl_ListObjGetElements -- |
| 400 |
* |
| 401 |
* Retreive the elements in a list 'Tcl_Obj'. |
| 402 |
* |
| 403 |
* Value |
| 404 |
* |
| 405 |
* TCL_OK |
| 406 |
* |
| 407 |
* A count of list elements is stored, 'objcPtr', And a pointer to the |
| 408 |
* array of elements in the list is stored in 'objvPtr'. |
| 409 |
* |
| 410 |
* The elements accessible via 'objvPtr' should be treated as readonly |
| 411 |
* and the refCount for each object is _not_ incremented; the caller |
| 412 |
* must do that if it holds on to a reference. Furthermore, the |
| 413 |
* pointer and length returned by this function may change as soon as |
| 414 |
* any function is called on the list object. Be careful about |
| 415 |
* retaining the pointer in a local data structure. |
| 416 |
* |
| 417 |
* TCL_ERROR |
| 418 |
* |
| 419 |
* 'listPtr' is not a valid list. An error message is left in the |
| 420 |
* interpreter's result if 'interp' is not NULL. |
| 421 |
* |
| 422 |
* Effect |
| 423 |
* |
| 424 |
* 'listPtr' is converted to a list object if it isn't one already. |
| 425 |
* |
| 426 |
*---------------------------------------------------------------------- |
| 427 |
*/ |
| 428 |
|
| 429 |
int |
| 430 |
Tcl_ListObjGetElements( |
| 431 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 432 |
Tcl_Obj *listPtr, /* List object for which an element array is |
| 433 |
* to be returned. */ |
| 434 |
int *objcPtr, /* Where to store the count of objects |
| 435 |
* referenced by objv. */ |
| 436 |
Tcl_Obj ***objvPtr) /* Where to store the pointer to an array of |
| 437 |
* pointers to the list's objects. */ |
| 438 |
{ |
| 439 |
List *listRepPtr; |
| 440 |
|
| 441 |
if (listPtr->typePtr != &tclListType) { |
| 442 |
int result; |
| 443 |
|
| 444 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 445 |
*objcPtr = 0; |
| 446 |
*objvPtr = NULL; |
| 447 |
return TCL_OK; |
| 448 |
} |
| 449 |
result = SetListFromAny(interp, listPtr); |
| 450 |
if (result != TCL_OK) { |
| 451 |
return result; |
| 452 |
} |
| 453 |
} |
| 454 |
listRepPtr = ListRepPtr(listPtr); |
| 455 |
*objcPtr = listRepPtr->elemCount; |
| 456 |
*objvPtr = &listRepPtr->elements; |
| 457 |
return TCL_OK; |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
*---------------------------------------------------------------------- |
| 462 |
* |
| 463 |
* Tcl_ListObjAppendList -- |
| 464 |
* |
| 465 |
* Appends the elements of elemListPtr to those of listPtr. |
| 466 |
* |
| 467 |
* Value |
| 468 |
* |
| 469 |
* TCL_OK |
| 470 |
* |
| 471 |
* Success. |
| 472 |
* |
| 473 |
* TCL_ERROR |
| 474 |
* |
| 475 |
* 'listPtr' or 'elemListPtr' are not valid lists. An error |
| 476 |
* message is left in the interpreter's result if 'interp' is not NULL. |
| 477 |
* |
| 478 |
* Effect |
| 479 |
* |
| 480 |
* The reference count of each element of 'elemListPtr' as it is added to |
| 481 |
* 'listPtr'. 'listPtr' and 'elemListPtr' are converted to 'tclListType' |
| 482 |
* if they are not already. Appending the new elements may cause the |
| 483 |
* array of element pointers in 'listObj' to grow. If any objects are |
| 484 |
* appended to 'listPtr'. Any preexisting string representation of |
| 485 |
* 'listPtr' is invalidated. |
| 486 |
* |
| 487 |
*---------------------------------------------------------------------- |
| 488 |
*/ |
| 489 |
|
| 490 |
int |
| 491 |
Tcl_ListObjAppendList( |
| 492 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 493 |
Tcl_Obj *listPtr, /* List object to append elements to. */ |
| 494 |
Tcl_Obj *elemListPtr) /* List obj with elements to append. */ |
| 495 |
{ |
| 496 |
int objc; |
| 497 |
Tcl_Obj **objv; |
| 498 |
|
| 499 |
if (Tcl_IsShared(listPtr)) { |
| 500 |
Tcl_Panic("%s called with shared object", "Tcl_ListObjAppendList"); |
| 501 |
} |
| 502 |
|
| 503 |
/* |
| 504 |
* Pull the elements to append from elemListPtr. |
| 505 |
*/ |
| 506 |
|
| 507 |
if (TCL_OK != TclListObjGetElements(interp, elemListPtr, &objc, &objv)) { |
| 508 |
return TCL_ERROR; |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* Insert the new elements starting after the lists's last element. |
| 513 |
* Delete zero existing elements. |
| 514 |
*/ |
| 515 |
|
| 516 |
return Tcl_ListObjReplace(interp, listPtr, LIST_MAX, 0, objc, objv); |
| 517 |
} |
| 518 |
|
| 519 |
/* |
| 520 |
*---------------------------------------------------------------------- |
| 521 |
* |
| 522 |
* Tcl_ListObjAppendElement -- |
| 523 |
* |
| 524 |
* Like 'Tcl_ListObjAppendList', but Appends a single value to a list. |
| 525 |
* |
| 526 |
* Value |
| 527 |
* |
| 528 |
* TCL_OK |
| 529 |
* |
| 530 |
* 'objPtr' is appended to the elements of 'listPtr'. |
| 531 |
* |
| 532 |
* TCL_ERROR |
| 533 |
* |
| 534 |
* listPtr does not refer to a list object and the object can not be |
| 535 |
* converted to one. An error message will be left in the |
| 536 |
* interpreter's result if interp is not NULL. |
| 537 |
* |
| 538 |
* Effect |
| 539 |
* |
| 540 |
* If 'listPtr' is not already of type 'tclListType', it is converted. |
| 541 |
* The 'refCount' of 'objPtr' is incremented as it is added to 'listPtr'. |
| 542 |
* Appending the new element may cause the array of element pointers |
| 543 |
* in 'listObj' to grow. Any preexisting string representation of |
| 544 |
* 'listPtr' is invalidated. |
| 545 |
* |
| 546 |
*---------------------------------------------------------------------- |
| 547 |
*/ |
| 548 |
|
| 549 |
int |
| 550 |
Tcl_ListObjAppendElement( |
| 551 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 552 |
Tcl_Obj *listPtr, /* List object to append objPtr to. */ |
| 553 |
Tcl_Obj *objPtr) /* Object to append to listPtr's list. */ |
| 554 |
{ |
| 555 |
List *listRepPtr, *newPtr = NULL; |
| 556 |
int numElems, numRequired, needGrow, isShared, attempt; |
| 557 |
|
| 558 |
if (Tcl_IsShared(listPtr)) { |
| 559 |
Tcl_Panic("%s called with shared object", "Tcl_ListObjAppendElement"); |
| 560 |
} |
| 561 |
if (listPtr->typePtr != &tclListType) { |
| 562 |
int result; |
| 563 |
|
| 564 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 565 |
Tcl_SetListObj(listPtr, 1, &objPtr); |
| 566 |
return TCL_OK; |
| 567 |
} |
| 568 |
result = SetListFromAny(interp, listPtr); |
| 569 |
if (result != TCL_OK) { |
| 570 |
return result; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
listRepPtr = ListRepPtr(listPtr); |
| 575 |
numElems = listRepPtr->elemCount; |
| 576 |
numRequired = numElems + 1 ; |
| 577 |
needGrow = (numRequired > listRepPtr->maxElemCount); |
| 578 |
isShared = (listRepPtr->refCount > 1); |
| 579 |
|
| 580 |
if (numRequired > LIST_MAX) { |
| 581 |
if (interp != NULL) { |
| 582 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 583 |
"max length of a Tcl list (%d elements) exceeded", |
| 584 |
LIST_MAX)); |
| 585 |
Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL); |
| 586 |
} |
| 587 |
return TCL_ERROR; |
| 588 |
} |
| 589 |
|
| 590 |
if (needGrow && !isShared) { |
| 591 |
/* |
| 592 |
* Need to grow + unshared internalrep => try to realloc |
| 593 |
*/ |
| 594 |
|
| 595 |
attempt = 2 * numRequired; |
| 596 |
if (attempt <= LIST_MAX) { |
| 597 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 598 |
} |
| 599 |
if (newPtr == NULL) { |
| 600 |
attempt = numRequired + 1 + TCL_MIN_ELEMENT_GROWTH; |
| 601 |
if (attempt > LIST_MAX) { |
| 602 |
attempt = LIST_MAX; |
| 603 |
} |
| 604 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 605 |
} |
| 606 |
if (newPtr == NULL) { |
| 607 |
attempt = numRequired; |
| 608 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 609 |
} |
| 610 |
if (newPtr) { |
| 611 |
listRepPtr = newPtr; |
| 612 |
listRepPtr->maxElemCount = attempt; |
| 613 |
needGrow = 0; |
| 614 |
} |
| 615 |
} |
| 616 |
if (isShared || needGrow) { |
| 617 |
Tcl_Obj **dst, **src = &listRepPtr->elements; |
| 618 |
|
| 619 |
/* |
| 620 |
* Either we have a shared internalrep and we must copy to write, or we |
| 621 |
* need to grow and realloc attempts failed. Attempt internalrep copy. |
| 622 |
*/ |
| 623 |
|
| 624 |
attempt = 2 * numRequired; |
| 625 |
newPtr = AttemptNewList(NULL, attempt, NULL); |
| 626 |
if (newPtr == NULL) { |
| 627 |
attempt = numRequired + 1 + TCL_MIN_ELEMENT_GROWTH; |
| 628 |
if (attempt > LIST_MAX) { |
| 629 |
attempt = LIST_MAX; |
| 630 |
} |
| 631 |
newPtr = AttemptNewList(NULL, attempt, NULL); |
| 632 |
} |
| 633 |
if (newPtr == NULL) { |
| 634 |
attempt = numRequired; |
| 635 |
newPtr = AttemptNewList(interp, attempt, NULL); |
| 636 |
} |
| 637 |
if (newPtr == NULL) { |
| 638 |
/* |
| 639 |
* All growth attempts failed; throw the error. |
| 640 |
*/ |
| 641 |
|
| 642 |
return TCL_ERROR; |
| 643 |
} |
| 644 |
|
| 645 |
dst = &newPtr->elements; |
| 646 |
newPtr->refCount++; |
| 647 |
newPtr->canonicalFlag = listRepPtr->canonicalFlag; |
| 648 |
newPtr->elemCount = listRepPtr->elemCount; |
| 649 |
|
| 650 |
if (isShared) { |
| 651 |
/* |
| 652 |
* The original internalrep must remain undisturbed. Copy into the new |
| 653 |
* one and bump refcounts |
| 654 |
*/ |
| 655 |
while (numElems--) { |
| 656 |
*dst = *src++; |
| 657 |
Tcl_IncrRefCount(*dst++); |
| 658 |
} |
| 659 |
listRepPtr->refCount--; |
| 660 |
} else { |
| 661 |
/* |
| 662 |
* Old internalrep to be freed, re-use refCounts. |
| 663 |
*/ |
| 664 |
|
| 665 |
memcpy(dst, src, numElems * sizeof(Tcl_Obj *)); |
| 666 |
ckfree(listRepPtr); |
| 667 |
} |
| 668 |
listRepPtr = newPtr; |
| 669 |
} |
| 670 |
listPtr->internalRep.twoPtrValue.ptr1 = listRepPtr; |
| 671 |
|
| 672 |
/* |
| 673 |
* Add objPtr to the end of listPtr's array of element pointers. Increment |
| 674 |
* the ref count for the (now shared) objPtr. |
| 675 |
*/ |
| 676 |
|
| 677 |
*(&listRepPtr->elements + listRepPtr->elemCount) = objPtr; |
| 678 |
Tcl_IncrRefCount(objPtr); |
| 679 |
listRepPtr->elemCount++; |
| 680 |
|
| 681 |
/* |
| 682 |
* Invalidate any old string representation since the list's internal |
| 683 |
* representation has changed. |
| 684 |
*/ |
| 685 |
|
| 686 |
TclInvalidateStringRep(listPtr); |
| 687 |
return TCL_OK; |
| 688 |
} |
| 689 |
|
| 690 |
/* |
| 691 |
*---------------------------------------------------------------------- |
| 692 |
* |
| 693 |
* Tcl_ListObjIndex -- |
| 694 |
* |
| 695 |
* Retrieve a pointer to the element of 'listPtr' at 'index'. The index |
| 696 |
* of the first element is 0. |
| 697 |
* |
| 698 |
* Value |
| 699 |
* |
| 700 |
* TCL_OK |
| 701 |
* |
| 702 |
* A pointer to the element at 'index' is stored in 'objPtrPtr'. If |
| 703 |
* 'index' is out of range, NULL is stored in 'objPtrPtr'. This |
| 704 |
* object should be treated as readonly and its 'refCount' is _not_ |
| 705 |
* incremented. The caller must do that if it holds on to the |
| 706 |
* reference. |
| 707 |
* |
| 708 |
* TCL_ERROR |
| 709 |
* |
| 710 |
* 'listPtr' is not a valid list. An error message is left in the |
| 711 |
* interpreter's result if 'interp' is not NULL. |
| 712 |
* |
| 713 |
* Effect |
| 714 |
* |
| 715 |
* If 'listPtr' is not already of type 'tclListType', it is converted. |
| 716 |
* |
| 717 |
*---------------------------------------------------------------------- |
| 718 |
*/ |
| 719 |
|
| 720 |
int |
| 721 |
Tcl_ListObjIndex( |
| 722 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 723 |
Tcl_Obj *listPtr, /* List object to index into. */ |
| 724 |
int index, /* Index of element to return. */ |
| 725 |
Tcl_Obj **objPtrPtr) /* The resulting Tcl_Obj* is stored here. */ |
| 726 |
{ |
| 727 |
List *listRepPtr; |
| 728 |
|
| 729 |
if (listPtr->typePtr != &tclListType) { |
| 730 |
int result; |
| 731 |
|
| 732 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 733 |
*objPtrPtr = NULL; |
| 734 |
return TCL_OK; |
| 735 |
} |
| 736 |
result = SetListFromAny(interp, listPtr); |
| 737 |
if (result != TCL_OK) { |
| 738 |
return result; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
listRepPtr = ListRepPtr(listPtr); |
| 743 |
if ((index < 0) || (index >= listRepPtr->elemCount)) { |
| 744 |
*objPtrPtr = NULL; |
| 745 |
} else { |
| 746 |
*objPtrPtr = (&listRepPtr->elements)[index]; |
| 747 |
} |
| 748 |
|
| 749 |
return TCL_OK; |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
*---------------------------------------------------------------------- |
| 754 |
* |
| 755 |
* Tcl_ListObjLength -- |
| 756 |
* |
| 757 |
* Retrieve the number of elements in a list. |
| 758 |
* |
| 759 |
* Value |
| 760 |
* |
| 761 |
* TCL_OK |
| 762 |
* |
| 763 |
* A count of list elements is stored at the address provided by |
| 764 |
* 'intPtr'. If 'listPtr' is not already of type 'tclListPtr', it is |
| 765 |
* converted. |
| 766 |
* |
| 767 |
* TCL_ERROR |
| 768 |
* |
| 769 |
* 'listPtr' is not a valid list. An error message will be left in |
| 770 |
* the interpreter's result if 'interp' is not NULL. |
| 771 |
* |
| 772 |
*---------------------------------------------------------------------- |
| 773 |
*/ |
| 774 |
|
| 775 |
int |
| 776 |
Tcl_ListObjLength( |
| 777 |
Tcl_Interp *interp, /* Used to report errors if not NULL. */ |
| 778 |
Tcl_Obj *listPtr, /* List object whose #elements to return. */ |
| 779 |
int *intPtr) /* The resulting int is stored here. */ |
| 780 |
{ |
| 781 |
List *listRepPtr; |
| 782 |
|
| 783 |
if (listPtr->typePtr != &tclListType) { |
| 784 |
int result; |
| 785 |
|
| 786 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 787 |
*intPtr = 0; |
| 788 |
return TCL_OK; |
| 789 |
} |
| 790 |
result = SetListFromAny(interp, listPtr); |
| 791 |
if (result != TCL_OK) { |
| 792 |
return result; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
listRepPtr = ListRepPtr(listPtr); |
| 797 |
*intPtr = listRepPtr->elemCount; |
| 798 |
return TCL_OK; |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
*---------------------------------------------------------------------- |
| 803 |
* |
| 804 |
* Tcl_ListObjReplace -- |
| 805 |
* |
| 806 |
* Replace values in a list. |
| 807 |
* |
| 808 |
* If 'first' is zero or negative, it refers to the first element. If |
| 809 |
* 'first' outside the range of elements in the list, no elements are |
| 810 |
* deleted. |
| 811 |
* |
| 812 |
* If 'count' is zero or negative no elements are deleted, and any new |
| 813 |
* elements are inserted at the beginning of the list. |
| 814 |
* |
| 815 |
* Value |
| 816 |
* |
| 817 |
* TCL_OK |
| 818 |
* |
| 819 |
* The first 'objc' values of 'objv' replaced 'count' elements in 'listPtr' |
| 820 |
* starting at 'first'. If 'objc' 0, no new elements are added. |
| 821 |
* |
| 822 |
* TCL_ERROR |
| 823 |
* |
| 824 |
* 'listPtr' is not a valid list. An error message is left in the |
| 825 |
* interpreter's result if 'interp' is not NULL. |
| 826 |
* |
| 827 |
* Effect |
| 828 |
* |
| 829 |
* If 'listPtr' is not of type 'tclListType', it is converted if possible. |
| 830 |
* |
| 831 |
* The 'refCount' of each element appended to the list is incremented. |
| 832 |
* Similarly, the 'refCount' for each replaced element is decremented. |
| 833 |
* |
| 834 |
* If 'listPtr' is modified, any previous string representation is |
| 835 |
* invalidated. |
| 836 |
* |
| 837 |
*---------------------------------------------------------------------- |
| 838 |
*/ |
| 839 |
|
| 840 |
int |
| 841 |
Tcl_ListObjReplace( |
| 842 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 843 |
Tcl_Obj *listPtr, /* List object whose elements to replace. */ |
| 844 |
int first, /* Index of first element to replace. */ |
| 845 |
int count, /* Number of elements to replace. */ |
| 846 |
int objc, /* Number of objects to insert. */ |
| 847 |
Tcl_Obj *const objv[]) /* An array of objc pointers to Tcl objects to |
| 848 |
* insert. */ |
| 849 |
{ |
| 850 |
List *listRepPtr; |
| 851 |
Tcl_Obj **elemPtrs; |
| 852 |
int needGrow, numElems, numRequired, numAfterLast, start, i, j, isShared; |
| 853 |
|
| 854 |
if (Tcl_IsShared(listPtr)) { |
| 855 |
Tcl_Panic("%s called with shared object", "Tcl_ListObjReplace"); |
| 856 |
} |
| 857 |
if (listPtr->typePtr != &tclListType) { |
| 858 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 859 |
if (!objc) { |
| 860 |
return TCL_OK; |
| 861 |
} |
| 862 |
Tcl_SetListObj(listPtr, objc, NULL); |
| 863 |
} else { |
| 864 |
int result = SetListFromAny(interp, listPtr); |
| 865 |
|
| 866 |
if (result != TCL_OK) { |
| 867 |
return result; |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* Note that when count == 0 and objc == 0, this routine is logically a |
| 874 |
* no-op, removing and adding no elements to the list. However, by flowing |
| 875 |
* through this routine anyway, we get the important side effect that the |
| 876 |
* resulting listPtr is a list in canonical form. This is important. |
| 877 |
* Resist any temptation to optimize this case. |
| 878 |
*/ |
| 879 |
|
| 880 |
listRepPtr = ListRepPtr(listPtr); |
| 881 |
elemPtrs = &listRepPtr->elements; |
| 882 |
numElems = listRepPtr->elemCount; |
| 883 |
|
| 884 |
if (first < 0) { |
| 885 |
first = 0; |
| 886 |
} |
| 887 |
if (first >= numElems) { |
| 888 |
first = numElems; /* So we'll insert after last element. */ |
| 889 |
} |
| 890 |
if (count < 0) { |
| 891 |
count = 0; |
| 892 |
} else if (count > LIST_MAX /* Handle integer overflow */ |
| 893 |
|| numElems < first+count) { |
| 894 |
|
| 895 |
count = numElems - first; |
| 896 |
} |
| 897 |
|
| 898 |
if (objc > LIST_MAX - (numElems - count)) { |
| 899 |
if (interp != NULL) { |
| 900 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 901 |
"max length of a Tcl list (%d elements) exceeded", |
| 902 |
LIST_MAX)); |
| 903 |
} |
| 904 |
return TCL_ERROR; |
| 905 |
} |
| 906 |
isShared = (listRepPtr->refCount > 1); |
| 907 |
numRequired = numElems - count + objc; /* Known <= LIST_MAX */ |
| 908 |
needGrow = numRequired > listRepPtr->maxElemCount; |
| 909 |
|
| 910 |
for (i = 0; i < objc; i++) { |
| 911 |
Tcl_IncrRefCount(objv[i]); |
| 912 |
} |
| 913 |
|
| 914 |
if (needGrow && !isShared) { |
| 915 |
/* Try to use realloc */ |
| 916 |
List *newPtr = NULL; |
| 917 |
int attempt = 2 * numRequired; |
| 918 |
if (attempt <= LIST_MAX) { |
| 919 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 920 |
} |
| 921 |
if (newPtr == NULL) { |
| 922 |
attempt = numRequired + 1 + TCL_MIN_ELEMENT_GROWTH; |
| 923 |
if (attempt > LIST_MAX) { |
| 924 |
attempt = LIST_MAX; |
| 925 |
} |
| 926 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 927 |
} |
| 928 |
if (newPtr == NULL) { |
| 929 |
attempt = numRequired; |
| 930 |
newPtr = (List *)attemptckrealloc(listRepPtr, LIST_SIZE(attempt)); |
| 931 |
} |
| 932 |
if (newPtr) { |
| 933 |
listRepPtr = newPtr; |
| 934 |
listPtr->internalRep.twoPtrValue.ptr1 = listRepPtr; |
| 935 |
elemPtrs = &listRepPtr->elements; |
| 936 |
listRepPtr->maxElemCount = attempt; |
| 937 |
needGrow = numRequired > listRepPtr->maxElemCount; |
| 938 |
} |
| 939 |
} |
| 940 |
if (!needGrow && !isShared) { |
| 941 |
int shift; |
| 942 |
|
| 943 |
/* |
| 944 |
* Can use the current List struct. First "delete" count elements |
| 945 |
* starting at first. |
| 946 |
*/ |
| 947 |
|
| 948 |
for (j = first; j < first + count; j++) { |
| 949 |
Tcl_Obj *victimPtr = elemPtrs[j]; |
| 950 |
|
| 951 |
TclDecrRefCount(victimPtr); |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
* Shift the elements after the last one removed to their new |
| 956 |
* locations. |
| 957 |
*/ |
| 958 |
|
| 959 |
start = first + count; |
| 960 |
numAfterLast = numElems - start; |
| 961 |
shift = objc - count; /* numNewElems - numDeleted */ |
| 962 |
if ((numAfterLast > 0) && (shift != 0)) { |
| 963 |
Tcl_Obj **src = elemPtrs + start; |
| 964 |
|
| 965 |
memmove(src+shift, src, numAfterLast * sizeof(Tcl_Obj*)); |
| 966 |
} |
| 967 |
} else { |
| 968 |
/* |
| 969 |
* Cannot use the current List struct; it is shared, too small, or |
| 970 |
* both. Allocate a new struct and insert elements into it. |
| 971 |
*/ |
| 972 |
|
| 973 |
List *oldListRepPtr = listRepPtr; |
| 974 |
Tcl_Obj **oldPtrs = elemPtrs; |
| 975 |
int newMax; |
| 976 |
|
| 977 |
if (needGrow){ |
| 978 |
newMax = 2 * numRequired; |
| 979 |
} else { |
| 980 |
newMax = listRepPtr->maxElemCount; |
| 981 |
} |
| 982 |
|
| 983 |
listRepPtr = AttemptNewList(NULL, newMax, NULL); |
| 984 |
if (listRepPtr == NULL) { |
| 985 |
unsigned int limit = LIST_MAX - numRequired; |
| 986 |
unsigned int extra = numRequired - numElems |
| 987 |
+ TCL_MIN_ELEMENT_GROWTH; |
| 988 |
int growth = (int) ((extra > limit) ? limit : extra); |
| 989 |
|
| 990 |
listRepPtr = AttemptNewList(NULL, numRequired + growth, NULL); |
| 991 |
if (listRepPtr == NULL) { |
| 992 |
listRepPtr = AttemptNewList(interp, numRequired, NULL); |
| 993 |
if (listRepPtr == NULL) { |
| 994 |
for (i = 0; i < objc; i++) { |
| 995 |
/* See bug 3598580 */ |
| 996 |
objv[i]->refCount--; |
| 997 |
} |
| 998 |
return TCL_ERROR; |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
listPtr->internalRep.twoPtrValue.ptr1 = listRepPtr; |
| 1004 |
listRepPtr->refCount++; |
| 1005 |
|
| 1006 |
elemPtrs = &listRepPtr->elements; |
| 1007 |
|
| 1008 |
if (isShared) { |
| 1009 |
/* |
| 1010 |
* The old struct will remain in place; need new refCounts for the |
| 1011 |
* new List struct references. Copy over only the surviving |
| 1012 |
* elements. |
| 1013 |
*/ |
| 1014 |
|
| 1015 |
for (i=0; i < first; i++) { |
| 1016 |
elemPtrs[i] = oldPtrs[i]; |
| 1017 |
Tcl_IncrRefCount(elemPtrs[i]); |
| 1018 |
} |
| 1019 |
for (i = first + count, j = first + objc; |
| 1020 |
j < numRequired; i++, j++) { |
| 1021 |
elemPtrs[j] = oldPtrs[i]; |
| 1022 |
Tcl_IncrRefCount(elemPtrs[j]); |
| 1023 |
} |
| 1024 |
|
| 1025 |
oldListRepPtr->refCount--; |
| 1026 |
} else { |
| 1027 |
/* |
| 1028 |
* The old struct will be removed; use its inherited refCounts. |
| 1029 |
*/ |
| 1030 |
|
| 1031 |
if (first > 0) { |
| 1032 |
memcpy(elemPtrs, oldPtrs, first * sizeof(Tcl_Obj *)); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* |
| 1036 |
* "Delete" count elements starting at first. |
| 1037 |
*/ |
| 1038 |
|
| 1039 |
for (j = first; j < first + count; j++) { |
| 1040 |
Tcl_Obj *victimPtr = oldPtrs[j]; |
| 1041 |
|
| 1042 |
TclDecrRefCount(victimPtr); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* Copy the elements after the last one removed, shifted to their |
| 1047 |
* new locations. |
| 1048 |
*/ |
| 1049 |
|
| 1050 |
start = first + count; |
| 1051 |
numAfterLast = numElems - start; |
| 1052 |
if (numAfterLast > 0) { |
| 1053 |
memcpy(elemPtrs + first + objc, oldPtrs + start, |
| 1054 |
(size_t) numAfterLast * sizeof(Tcl_Obj *)); |
| 1055 |
} |
| 1056 |
|
| 1057 |
ckfree(oldListRepPtr); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* Insert the new elements into elemPtrs before "first". |
| 1063 |
*/ |
| 1064 |
|
| 1065 |
for (i=0,j=first ; i<objc ; i++,j++) { |
| 1066 |
elemPtrs[j] = objv[i]; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* |
| 1070 |
* Update the count of elements. |
| 1071 |
*/ |
| 1072 |
|
| 1073 |
listRepPtr->elemCount = numRequired; |
| 1074 |
|
| 1075 |
/* |
| 1076 |
* Invalidate and free any old string representation since it no longer |
| 1077 |
* reflects the list's internal representation. |
| 1078 |
*/ |
| 1079 |
|
| 1080 |
TclInvalidateStringRep(listPtr); |
| 1081 |
return TCL_OK; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* |
| 1085 |
*---------------------------------------------------------------------- |
| 1086 |
* |
| 1087 |
* TclLindexList -- |
| 1088 |
* |
| 1089 |
* Implements the 'lindex' command when objc==3. |
| 1090 |
* |
| 1091 |
* Implemented entirely as a wrapper around 'TclLindexFlat'. Reconfigures |
| 1092 |
* the argument format into required form while taking care to manage |
| 1093 |
* shimmering so as to tend to keep the most useful internalreps |
| 1094 |
* and/or avoid the most expensive conversions. |
| 1095 |
* |
| 1096 |
* Value |
| 1097 |
* |
| 1098 |
* A pointer to the specified element, with its 'refCount' incremented, or |
| 1099 |
* NULL if an error occurred. |
| 1100 |
* |
| 1101 |
* Notes |
| 1102 |
* |
| 1103 |
*---------------------------------------------------------------------- |
| 1104 |
*/ |
| 1105 |
|
| 1106 |
Tcl_Obj * |
| 1107 |
TclLindexList( |
| 1108 |
Tcl_Interp *interp, /* Tcl interpreter. */ |
| 1109 |
Tcl_Obj *listPtr, /* List being unpacked. */ |
| 1110 |
Tcl_Obj *argPtr) /* Index or index list. */ |
| 1111 |
{ |
| 1112 |
|
| 1113 |
int index; /* Index into the list. */ |
| 1114 |
Tcl_Obj *indexListCopy; |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Determine whether argPtr designates a list or a single index. We have |
| 1118 |
* to be careful about the order of the checks to avoid repeated |
| 1119 |
* shimmering; see TIP#22 and TIP#33 for the details. |
| 1120 |
*/ |
| 1121 |
|
| 1122 |
if (argPtr->typePtr != &tclListType |
| 1123 |
&& TclGetIntForIndexM(NULL , argPtr, 0, &index) == TCL_OK) { |
| 1124 |
/* |
| 1125 |
* argPtr designates a single index. |
| 1126 |
*/ |
| 1127 |
|
| 1128 |
return TclLindexFlat(interp, listPtr, 1, &argPtr); |
| 1129 |
} |
| 1130 |
|
| 1131 |
/* |
| 1132 |
* Here we make a private copy of the index list argument to avoid any |
| 1133 |
* shimmering issues that might invalidate the indices array below while |
| 1134 |
* we are still using it. This is probably unnecessary. It does not appear |
| 1135 |
* that any damaging shimmering is possible, and no test has been devised |
| 1136 |
* to show any error when this private copy is not made. But it's cheap, |
| 1137 |
* and it offers some future-proofing insurance in case the TclLindexFlat |
| 1138 |
* implementation changes in some unexpected way, or some new form of |
| 1139 |
* trace or callback permits things to happen that the current |
| 1140 |
* implementation does not. |
| 1141 |
*/ |
| 1142 |
|
| 1143 |
indexListCopy = TclListObjCopy(NULL, argPtr); |
| 1144 |
if (indexListCopy == NULL) { |
| 1145 |
/* |
| 1146 |
* argPtr designates something that is neither an index nor a |
| 1147 |
* well-formed list. Report the error via TclLindexFlat. |
| 1148 |
*/ |
| 1149 |
|
| 1150 |
return TclLindexFlat(interp, listPtr, 1, &argPtr); |
| 1151 |
} |
| 1152 |
|
| 1153 |
{ |
| 1154 |
int indexCount = -1; /* Size of the array of list indices. */ |
| 1155 |
Tcl_Obj **indices = NULL; /* Array of list indices. */ |
| 1156 |
|
| 1157 |
TclListObjGetElements(NULL, indexListCopy, &indexCount, &indices); |
| 1158 |
listPtr = TclLindexFlat(interp, listPtr, indexCount, indices); |
| 1159 |
} |
| 1160 |
Tcl_DecrRefCount(indexListCopy); |
| 1161 |
return listPtr; |
| 1162 |
} |
| 1163 |
|
| 1164 |
/* |
| 1165 |
*---------------------------------------------------------------------- |
| 1166 |
* |
| 1167 |
* TclLindexFlat -- |
| 1168 |
* |
| 1169 |
* The core of the 'lindex' command, with all index |
| 1170 |
* arguments presented as a flat list. |
| 1171 |
* |
| 1172 |
* Value |
| 1173 |
* |
| 1174 |
* A pointer to the object extracted, with its 'refCount' incremented, or |
| 1175 |
* NULL if an error occurred. Thus, the calling code will usually do |
| 1176 |
* something like: |
| 1177 |
* |
| 1178 |
* Tcl_SetObjResult(interp, result); |
| 1179 |
* Tcl_DecrRefCount(result); |
| 1180 |
* |
| 1181 |
* |
| 1182 |
*---------------------------------------------------------------------- |
| 1183 |
*/ |
| 1184 |
|
| 1185 |
Tcl_Obj * |
| 1186 |
TclLindexFlat( |
| 1187 |
Tcl_Interp *interp, /* Tcl interpreter. */ |
| 1188 |
Tcl_Obj *listPtr, /* Tcl object representing the list. */ |
| 1189 |
int indexCount, /* Count of indices. */ |
| 1190 |
Tcl_Obj *const indexArray[])/* Array of pointers to Tcl objects that |
| 1191 |
* represent the indices in the list. */ |
| 1192 |
{ |
| 1193 |
int i; |
| 1194 |
|
| 1195 |
Tcl_IncrRefCount(listPtr); |
| 1196 |
|
| 1197 |
for (i=0 ; i<indexCount && listPtr ; i++) { |
| 1198 |
int index, listLen = 0; |
| 1199 |
Tcl_Obj **elemPtrs = NULL, *sublistCopy; |
| 1200 |
|
| 1201 |
/* |
| 1202 |
* Here we make a private copy of the current sublist, so we avoid any |
| 1203 |
* shimmering issues that might invalidate the elemPtr array below |
| 1204 |
* while we are still using it. See test lindex-8.4. |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
sublistCopy = TclListObjCopy(interp, listPtr); |
| 1208 |
Tcl_DecrRefCount(listPtr); |
| 1209 |
listPtr = NULL; |
| 1210 |
|
| 1211 |
if (sublistCopy == NULL) { |
| 1212 |
/* |
| 1213 |
* The sublist is not a list at all => error. |
| 1214 |
*/ |
| 1215 |
|
| 1216 |
break; |
| 1217 |
} |
| 1218 |
TclListObjGetElements(NULL, sublistCopy, &listLen, &elemPtrs); |
| 1219 |
|
| 1220 |
if (TclGetIntForIndexM(interp, indexArray[i], /*endValue*/ listLen-1, |
| 1221 |
&index) == TCL_OK) { |
| 1222 |
if (index<0 || index>=listLen) { |
| 1223 |
/* |
| 1224 |
* Index is out of range. Break out of loop with empty result. |
| 1225 |
* First check remaining indices for validity |
| 1226 |
*/ |
| 1227 |
|
| 1228 |
while (++i < indexCount) { |
| 1229 |
if (TclGetIntForIndexM(interp, indexArray[i], -1, &index) |
| 1230 |
!= TCL_OK) { |
| 1231 |
Tcl_DecrRefCount(sublistCopy); |
| 1232 |
return NULL; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
TclNewObj(listPtr); |
| 1236 |
} else { |
| 1237 |
/* |
| 1238 |
* Extract the pointer to the appropriate element. |
| 1239 |
*/ |
| 1240 |
|
| 1241 |
listPtr = elemPtrs[index]; |
| 1242 |
} |
| 1243 |
Tcl_IncrRefCount(listPtr); |
| 1244 |
} |
| 1245 |
Tcl_DecrRefCount(sublistCopy); |
| 1246 |
} |
| 1247 |
|
| 1248 |
return listPtr; |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* |
| 1252 |
*---------------------------------------------------------------------- |
| 1253 |
* |
| 1254 |
* TclLsetList -- |
| 1255 |
* |
| 1256 |
* The core of [lset] when objc == 4. Objv[2] may be either a |
| 1257 |
* scalar index or a list of indices. |
| 1258 |
* |
| 1259 |
* Implemented entirely as a wrapper around 'TclLindexFlat', as described |
| 1260 |
* for 'TclLindexList'. |
| 1261 |
* |
| 1262 |
* Value |
| 1263 |
* |
| 1264 |
* The new list, with the 'refCount' of 'valuPtr' incremented, or NULL if |
| 1265 |
* there was an error. |
| 1266 |
* |
| 1267 |
*---------------------------------------------------------------------- |
| 1268 |
*/ |
| 1269 |
|
| 1270 |
Tcl_Obj * |
| 1271 |
TclLsetList( |
| 1272 |
Tcl_Interp *interp, /* Tcl interpreter. */ |
| 1273 |
Tcl_Obj *listPtr, /* Pointer to the list being modified. */ |
| 1274 |
Tcl_Obj *indexArgPtr, /* Index or index-list arg to 'lset'. */ |
| 1275 |
Tcl_Obj *valuePtr) /* Value arg to 'lset'. */ |
| 1276 |
{ |
| 1277 |
int indexCount = 0; /* Number of indices in the index list. */ |
| 1278 |
Tcl_Obj **indices = NULL; /* Vector of indices in the index list. */ |
| 1279 |
Tcl_Obj *retValuePtr; /* Pointer to the list to be returned. */ |
| 1280 |
int index; /* Current index in the list - discarded. */ |
| 1281 |
Tcl_Obj *indexListCopy; |
| 1282 |
|
| 1283 |
/* |
| 1284 |
* Determine whether the index arg designates a list or a single index. |
| 1285 |
* We have to be careful about the order of the checks to avoid repeated |
| 1286 |
* shimmering; see TIP #22 and #23 for details. |
| 1287 |
*/ |
| 1288 |
|
| 1289 |
if (indexArgPtr->typePtr != &tclListType |
| 1290 |
&& TclGetIntForIndexM(NULL, indexArgPtr, 0, &index) == TCL_OK) { |
| 1291 |
/* |
| 1292 |
* indexArgPtr designates a single index. |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
return TclLsetFlat(interp, listPtr, 1, &indexArgPtr, valuePtr); |
| 1296 |
|
| 1297 |
} |
| 1298 |
|
| 1299 |
indexListCopy = TclListObjCopy(NULL, indexArgPtr); |
| 1300 |
if (indexListCopy == NULL) { |
| 1301 |
/* |
| 1302 |
* indexArgPtr designates something that is neither an index nor a |
| 1303 |
* well formed list. Report the error via TclLsetFlat. |
| 1304 |
*/ |
| 1305 |
|
| 1306 |
return TclLsetFlat(interp, listPtr, 1, &indexArgPtr, valuePtr); |
| 1307 |
} |
| 1308 |
TclListObjGetElements(NULL, indexArgPtr, &indexCount, &indices); |
| 1309 |
|
| 1310 |
/* |
| 1311 |
* Let TclLsetFlat perform the actual lset operation. |
| 1312 |
*/ |
| 1313 |
|
| 1314 |
retValuePtr = TclLsetFlat(interp, listPtr, indexCount, indices, valuePtr); |
| 1315 |
|
| 1316 |
Tcl_DecrRefCount(indexListCopy); |
| 1317 |
return retValuePtr; |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* |
| 1321 |
*---------------------------------------------------------------------- |
| 1322 |
* |
| 1323 |
* TclLsetFlat -- |
| 1324 |
* |
| 1325 |
* Core engine of the 'lset' command. |
| 1326 |
* |
| 1327 |
* Value |
| 1328 |
* |
| 1329 |
* The resulting list |
| 1330 |
* |
| 1331 |
* The 'refCount' of 'valuePtr' is incremented. If 'listPtr' was not |
| 1332 |
* duplicated, its 'refCount' is incremented. The reference count of |
| 1333 |
* an unduplicated object is therefore 2 (one for the returned pointer |
| 1334 |
* and one for the variable that holds it). The reference count of a |
| 1335 |
* duplicate object is 1, reflecting that result is the only active |
| 1336 |
* reference. The caller is expected to store the result in the |
| 1337 |
* variable and decrement its reference count. (INST_STORE_* does |
| 1338 |
* exactly this.) |
| 1339 |
* |
| 1340 |
* NULL |
| 1341 |
* |
| 1342 |
* An error occurred. If 'listPtr' was duplicated, the reference |
| 1343 |
* count on the duplicate is decremented so that it is 0, causing any |
| 1344 |
* memory allocated by this function to be freed. |
| 1345 |
* |
| 1346 |
* |
| 1347 |
* Effect |
| 1348 |
* |
| 1349 |
* On entry, the reference count of 'listPtr' does not reflect any |
| 1350 |
* references held on the stack. The first action of this function is to |
| 1351 |
* determine whether 'listPtr' is shared and to create a duplicate |
| 1352 |
* unshared copy if it is. The reference count of the duplicate is |
| 1353 |
* incremented. At this point, the reference count is 1 in either case so |
| 1354 |
* that the object is considered unshared. |
| 1355 |
* |
| 1356 |
* The unshared list is altered directly to produce the result. |
| 1357 |
* 'TclLsetFlat' maintains a linked list of 'Tcl_Obj' values whose string |
| 1358 |
* representations must be spoilt by threading via 'ptr2' of the |
| 1359 |
* two-pointer internal representation. On entry to 'TclLsetFlat', the |
| 1360 |
* values of 'ptr2' are immaterial; on exit, the 'ptr2' field of any |
| 1361 |
* Tcl_Obj that has been modified is set to NULL. |
| 1362 |
* |
| 1363 |
*---------------------------------------------------------------------- |
| 1364 |
*/ |
| 1365 |
|
| 1366 |
Tcl_Obj * |
| 1367 |
TclLsetFlat( |
| 1368 |
Tcl_Interp *interp, /* Tcl interpreter. */ |
| 1369 |
Tcl_Obj *listPtr, /* Pointer to the list being modified. */ |
| 1370 |
int indexCount, /* Number of index args. */ |
| 1371 |
Tcl_Obj *const indexArray[], |
| 1372 |
/* Index args. */ |
| 1373 |
Tcl_Obj *valuePtr) /* Value arg to 'lset'. */ |
| 1374 |
{ |
| 1375 |
int index, result, len; |
| 1376 |
Tcl_Obj *subListPtr, *retValuePtr, *chainPtr; |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* If there are no indices, simply return the new value. (Without |
| 1380 |
* indices, [lset] is a synonym for [set]. |
| 1381 |
*/ |
| 1382 |
|
| 1383 |
if (indexCount == 0) { |
| 1384 |
Tcl_IncrRefCount(valuePtr); |
| 1385 |
return valuePtr; |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* |
| 1389 |
* If the list is shared, make a copy we can modify (copy-on-write). We |
| 1390 |
* use Tcl_DuplicateObj() instead of TclListObjCopy() for a few reasons: |
| 1391 |
* 1) we have not yet confirmed listPtr is actually a list; 2) We make a |
| 1392 |
* verbatim copy of any existing string rep, and when we combine that with |
| 1393 |
* the delayed invalidation of string reps of modified Tcl_Obj's |
| 1394 |
* implemented below, the outcome is that any error condition that causes |
| 1395 |
* this routine to return NULL, will leave the string rep of listPtr and |
| 1396 |
* all elements to be unchanged. |
| 1397 |
*/ |
| 1398 |
|
| 1399 |
subListPtr = Tcl_IsShared(listPtr) ? Tcl_DuplicateObj(listPtr) : listPtr; |
| 1400 |
|
| 1401 |
/* |
| 1402 |
* Anchor the linked list of Tcl_Obj's whose string reps must be |
| 1403 |
* invalidated if the operation succeeds. |
| 1404 |
*/ |
| 1405 |
|
| 1406 |
retValuePtr = subListPtr; |
| 1407 |
chainPtr = NULL; |
| 1408 |
result = TCL_OK; |
| 1409 |
|
| 1410 |
/* |
| 1411 |
* Loop through all the index arguments, and for each one dive into the |
| 1412 |
* appropriate sublist. |
| 1413 |
*/ |
| 1414 |
|
| 1415 |
do { |
| 1416 |
int elemCount; |
| 1417 |
Tcl_Obj *parentList, **elemPtrs; |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* Check for the possible error conditions... |
| 1421 |
*/ |
| 1422 |
|
| 1423 |
if (TclListObjGetElements(interp, subListPtr, &elemCount, &elemPtrs) |
| 1424 |
!= TCL_OK) { |
| 1425 |
/* ...the sublist we're indexing into isn't a list at all. */ |
| 1426 |
result = TCL_ERROR; |
| 1427 |
break; |
| 1428 |
} |
| 1429 |
|
| 1430 |
/* |
| 1431 |
* WARNING: the macro TclGetIntForIndexM is not safe for |
| 1432 |
* post-increments, avoid '*indexArray++' here. |
| 1433 |
*/ |
| 1434 |
|
| 1435 |
if (TclGetIntForIndexM(interp, *indexArray, elemCount - 1, &index) |
| 1436 |
!= TCL_OK) { |
| 1437 |
/* ...the index we're trying to use isn't an index at all. */ |
| 1438 |
result = TCL_ERROR; |
| 1439 |
indexArray++; |
| 1440 |
break; |
| 1441 |
} |
| 1442 |
indexArray++; |
| 1443 |
|
| 1444 |
if (index < 0 || index > elemCount) { |
| 1445 |
/* ...the index points outside the sublist. */ |
| 1446 |
if (interp != NULL) { |
| 1447 |
Tcl_SetObjResult(interp, |
| 1448 |
Tcl_NewStringObj("list index out of range", -1)); |
| 1449 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSET", |
| 1450 |
"BADINDEX", (char *)NULL); |
| 1451 |
} |
| 1452 |
result = TCL_ERROR; |
| 1453 |
break; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/* |
| 1457 |
* No error conditions. As long as we're not yet on the last index, |
| 1458 |
* determine the next sublist for the next pass through the loop, and |
| 1459 |
* take steps to make sure it is an unshared copy, as we intend to |
| 1460 |
* modify it. |
| 1461 |
*/ |
| 1462 |
|
| 1463 |
if (--indexCount) { |
| 1464 |
parentList = subListPtr; |
| 1465 |
if (index == elemCount) { |
| 1466 |
TclNewObj(subListPtr); |
| 1467 |
} else { |
| 1468 |
subListPtr = elemPtrs[index]; |
| 1469 |
} |
| 1470 |
if (Tcl_IsShared(subListPtr)) { |
| 1471 |
subListPtr = Tcl_DuplicateObj(subListPtr); |
| 1472 |
} |
| 1473 |
|
| 1474 |
/* |
| 1475 |
* Replace the original elemPtr[index] in parentList with a copy |
| 1476 |
* we know to be unshared. This call will also deal with the |
| 1477 |
* situation where parentList shares its internalrep with other |
| 1478 |
* Tcl_Obj's. Dealing with the shared internalrep case can cause |
| 1479 |
* subListPtr to become shared again, so detect that case and make |
| 1480 |
* and store another copy. |
| 1481 |
*/ |
| 1482 |
|
| 1483 |
if (index == elemCount) { |
| 1484 |
Tcl_ListObjAppendElement(NULL, parentList, subListPtr); |
| 1485 |
} else { |
| 1486 |
TclListObjSetElement(NULL, parentList, index, subListPtr); |
| 1487 |
} |
| 1488 |
if (Tcl_IsShared(subListPtr)) { |
| 1489 |
subListPtr = Tcl_DuplicateObj(subListPtr); |
| 1490 |
TclListObjSetElement(NULL, parentList, index, subListPtr); |
| 1491 |
} |
| 1492 |
|
| 1493 |
/* |
| 1494 |
* The TclListObjSetElement() calls do not spoil the string rep of |
| 1495 |
* parentList, and that's fine for now, since all we've done so |
| 1496 |
* far is replace a list element with an unshared copy. The list |
| 1497 |
* value remains the same, so the string rep. is still valid, and |
| 1498 |
* unchanged, which is good because if this whole routine returns |
| 1499 |
* NULL, we'd like to leave no change to the value of the lset |
| 1500 |
* variable. Later on, when we set valuePtr in its proper place, |
| 1501 |
* then all containing lists will have their values changed, and |
| 1502 |
* will need their string reps spoiled. We maintain a list of all |
| 1503 |
* those Tcl_Obj's (via a little internalrep surgery) so we can spoil |
| 1504 |
* them at that time. |
| 1505 |
*/ |
| 1506 |
|
| 1507 |
parentList->internalRep.twoPtrValue.ptr2 = chainPtr; |
| 1508 |
chainPtr = parentList; |
| 1509 |
} |
| 1510 |
} while (indexCount > 0); |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* Either we've detected and error condition, and exited the loop with |
| 1514 |
* result == TCL_ERROR, or we've successfully reached the last index, and |
| 1515 |
* we're ready to store valuePtr. In either case, we need to clean up our |
| 1516 |
* string spoiling list of Tcl_Obj's. |
| 1517 |
*/ |
| 1518 |
|
| 1519 |
while (chainPtr) { |
| 1520 |
Tcl_Obj *objPtr = chainPtr; |
| 1521 |
|
| 1522 |
if (result == TCL_OK) { |
| 1523 |
/* |
| 1524 |
* We're going to store valuePtr, so spoil string reps of all |
| 1525 |
* containing lists. |
| 1526 |
*/ |
| 1527 |
|
| 1528 |
TclInvalidateStringRep(objPtr); |
| 1529 |
} |
| 1530 |
|
| 1531 |
/* |
| 1532 |
* Clear away our internalrep surgery mess. |
| 1533 |
*/ |
| 1534 |
|
| 1535 |
chainPtr = (Tcl_Obj *)objPtr->internalRep.twoPtrValue.ptr2; |
| 1536 |
objPtr->internalRep.twoPtrValue.ptr2 = NULL; |
| 1537 |
} |
| 1538 |
|
| 1539 |
if (result != TCL_OK) { |
| 1540 |
/* |
| 1541 |
* Error return; message is already in interp. Clean up any excess |
| 1542 |
* memory. |
| 1543 |
*/ |
| 1544 |
|
| 1545 |
if (retValuePtr != listPtr) { |
| 1546 |
Tcl_DecrRefCount(retValuePtr); |
| 1547 |
} |
| 1548 |
return NULL; |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* |
| 1552 |
* Store valuePtr in proper sublist and return. The -1 is to avoid a |
| 1553 |
* compiler warning (not a problem because we checked that we have a |
| 1554 |
* proper list - or something convertible to one - above). |
| 1555 |
*/ |
| 1556 |
|
| 1557 |
len = -1; |
| 1558 |
TclListObjLength(NULL, subListPtr, &len); |
| 1559 |
if (index == len) { |
| 1560 |
Tcl_ListObjAppendElement(NULL, subListPtr, valuePtr); |
| 1561 |
} else { |
| 1562 |
TclListObjSetElement(NULL, subListPtr, index, valuePtr); |
| 1563 |
} |
| 1564 |
TclInvalidateStringRep(subListPtr); |
| 1565 |
Tcl_IncrRefCount(retValuePtr); |
| 1566 |
return retValuePtr; |
| 1567 |
} |
| 1568 |
|
| 1569 |
/* |
| 1570 |
*---------------------------------------------------------------------- |
| 1571 |
* |
| 1572 |
* TclListObjSetElement -- |
| 1573 |
* |
| 1574 |
* Set a single element of a list to a specified value. |
| 1575 |
* |
| 1576 |
* It is the caller's responsibility to invalidate the string |
| 1577 |
* representation of the 'listPtr'. |
| 1578 |
* |
| 1579 |
* Value |
| 1580 |
* |
| 1581 |
* TCL_OK |
| 1582 |
* |
| 1583 |
* Success. |
| 1584 |
* |
| 1585 |
* TCL_ERROR |
| 1586 |
* |
| 1587 |
* 'listPtr' does not refer to a list object and cannot be converted |
| 1588 |
* to one. An error message will be left in the interpreter result if |
| 1589 |
* interp is not NULL. |
| 1590 |
* |
| 1591 |
* TCL_ERROR |
| 1592 |
* |
| 1593 |
* An index designates an element outside the range [0..listLength-1], |
| 1594 |
* where 'listLength' is the count of elements in the list object |
| 1595 |
* designated by 'listPtr'. An error message is left in the |
| 1596 |
* interpreter result. |
| 1597 |
* |
| 1598 |
* Effect |
| 1599 |
* |
| 1600 |
* If 'listPtr' designates a shared object, 'Tcl_Panic' is called. If |
| 1601 |
* 'listPtr' is not already of type 'tclListType', it is converted and the |
| 1602 |
* internal representation is unshared. The 'refCount' of the element at |
| 1603 |
* 'index' is decremented and replaced in the list with the 'valuePtr', |
| 1604 |
* whose 'refCount' in turn is incremented. |
| 1605 |
* |
| 1606 |
* |
| 1607 |
*---------------------------------------------------------------------- |
| 1608 |
*/ |
| 1609 |
|
| 1610 |
int |
| 1611 |
TclListObjSetElement( |
| 1612 |
Tcl_Interp *interp, /* Tcl interpreter; used for error reporting |
| 1613 |
* if not NULL. */ |
| 1614 |
Tcl_Obj *listPtr, /* List object in which element should be |
| 1615 |
* stored. */ |
| 1616 |
int index, /* Index of element to store. */ |
| 1617 |
Tcl_Obj *valuePtr) /* Tcl object to store in the designated list |
| 1618 |
* element. */ |
| 1619 |
{ |
| 1620 |
List *listRepPtr; /* Internal representation of the list being |
| 1621 |
* modified. */ |
| 1622 |
Tcl_Obj **elemPtrs; /* Pointers to elements of the list. */ |
| 1623 |
int elemCount; /* Number of elements in the list. */ |
| 1624 |
|
| 1625 |
/* |
| 1626 |
* Ensure that the listPtr parameter designates an unshared list. |
| 1627 |
*/ |
| 1628 |
|
| 1629 |
if (Tcl_IsShared(listPtr)) { |
| 1630 |
Tcl_Panic("%s called with shared object", "TclListObjSetElement"); |
| 1631 |
} |
| 1632 |
if (listPtr->typePtr != &tclListType) { |
| 1633 |
int result; |
| 1634 |
|
| 1635 |
if (listPtr->bytes == tclEmptyStringRep) { |
| 1636 |
if (interp != NULL) { |
| 1637 |
Tcl_SetObjResult(interp, |
| 1638 |
Tcl_NewStringObj("list index out of range", -1)); |
| 1639 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSET", |
| 1640 |
"BADINDEX", (char *)NULL); |
| 1641 |
} |
| 1642 |
return TCL_ERROR; |
| 1643 |
} |
| 1644 |
result = SetListFromAny(interp, listPtr); |
| 1645 |
if (result != TCL_OK) { |
| 1646 |
return result; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
listRepPtr = ListRepPtr(listPtr); |
| 1651 |
elemCount = listRepPtr->elemCount; |
| 1652 |
|
| 1653 |
/* |
| 1654 |
* Ensure that the index is in bounds. |
| 1655 |
*/ |
| 1656 |
|
| 1657 |
if (index<0 || index>=elemCount) { |
| 1658 |
if (interp != NULL) { |
| 1659 |
Tcl_SetObjResult(interp, |
| 1660 |
Tcl_NewStringObj("list index out of range", -1)); |
| 1661 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSET", "BADINDEX", |
| 1662 |
(char *)NULL); |
| 1663 |
} |
| 1664 |
return TCL_ERROR; |
| 1665 |
} |
| 1666 |
|
| 1667 |
/* |
| 1668 |
* If the internal rep is shared, replace it with an unshared copy. |
| 1669 |
*/ |
| 1670 |
|
| 1671 |
if (listRepPtr->refCount > 1) { |
| 1672 |
Tcl_Obj **dst, **src = &listRepPtr->elements; |
| 1673 |
List *newPtr = AttemptNewList(NULL, listRepPtr->maxElemCount, NULL); |
| 1674 |
|
| 1675 |
if (newPtr == NULL) { |
| 1676 |
newPtr = AttemptNewList(interp, elemCount, NULL); |
| 1677 |
if (newPtr == NULL) { |
| 1678 |
return TCL_ERROR; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
newPtr->refCount++; |
| 1682 |
newPtr->elemCount = elemCount; |
| 1683 |
newPtr->canonicalFlag = listRepPtr->canonicalFlag; |
| 1684 |
|
| 1685 |
dst = &newPtr->elements; |
| 1686 |
while (elemCount--) { |
| 1687 |
*dst = *src++; |
| 1688 |
Tcl_IncrRefCount(*dst++); |
| 1689 |
} |
| 1690 |
|
| 1691 |
listRepPtr->refCount--; |
| 1692 |
|
| 1693 |
listPtr->internalRep.twoPtrValue.ptr1 = listRepPtr = newPtr; |
| 1694 |
} |
| 1695 |
elemPtrs = &listRepPtr->elements; |
| 1696 |
|
| 1697 |
/* |
| 1698 |
* Add a reference to the new list element. |
| 1699 |
*/ |
| 1700 |
|
| 1701 |
Tcl_IncrRefCount(valuePtr); |
| 1702 |
|
| 1703 |
/* |
| 1704 |
* Remove a reference from the old list element. |
| 1705 |
*/ |
| 1706 |
|
| 1707 |
Tcl_DecrRefCount(elemPtrs[index]); |
| 1708 |
|
| 1709 |
/* |
| 1710 |
* Stash the new object in the list. |
| 1711 |
*/ |
| 1712 |
|
| 1713 |
elemPtrs[index] = valuePtr; |
| 1714 |
|
| 1715 |
return TCL_OK; |
| 1716 |
} |
| 1717 |
|
| 1718 |
/* |
| 1719 |
*---------------------------------------------------------------------- |
| 1720 |
* |
| 1721 |
* FreeListInternalRep -- |
| 1722 |
* |
| 1723 |
* Deallocate the storage associated with the internal representation of a |
| 1724 |
* a list object. |
| 1725 |
* |
| 1726 |
* Effect |
| 1727 |
* |
| 1728 |
* The storage for the internal 'List' pointer of 'listPtr' is freed, the |
| 1729 |
* 'internalRep.twoPtrValue.ptr1' of 'listPtr' is set to NULL, and the 'refCount' |
| 1730 |
* of each element of the list is decremented. |
| 1731 |
* |
| 1732 |
*---------------------------------------------------------------------- |
| 1733 |
*/ |
| 1734 |
|
| 1735 |
static void |
| 1736 |
FreeListInternalRep( |
| 1737 |
Tcl_Obj *listPtr) /* List object with internal rep to free. */ |
| 1738 |
{ |
| 1739 |
List *listRepPtr = ListRepPtr(listPtr); |
| 1740 |
|
| 1741 |
if (listRepPtr->refCount-- <= 1) { |
| 1742 |
Tcl_Obj **elemPtrs = &listRepPtr->elements; |
| 1743 |
int i, numElems = listRepPtr->elemCount; |
| 1744 |
|
| 1745 |
for (i = 0; i < numElems; i++) { |
| 1746 |
Tcl_DecrRefCount(elemPtrs[i]); |
| 1747 |
} |
| 1748 |
ckfree(listRepPtr); |
| 1749 |
} |
| 1750 |
|
| 1751 |
listPtr->typePtr = NULL; |
| 1752 |
} |
| 1753 |
|
| 1754 |
/* |
| 1755 |
*---------------------------------------------------------------------- |
| 1756 |
* |
| 1757 |
* DupListInternalRep -- |
| 1758 |
* |
| 1759 |
* Initialize the internal representation of a list 'Tcl_Obj' to share the |
| 1760 |
* internal representation of an existing list object. |
| 1761 |
* |
| 1762 |
* Effect |
| 1763 |
* |
| 1764 |
* The 'refCount' of the List internal rep is incremented. |
| 1765 |
* |
| 1766 |
*---------------------------------------------------------------------- |
| 1767 |
*/ |
| 1768 |
|
| 1769 |
static void |
| 1770 |
DupListInternalRep( |
| 1771 |
Tcl_Obj *srcPtr, /* Object with internal rep to copy. */ |
| 1772 |
Tcl_Obj *copyPtr) /* Object with internal rep to set. */ |
| 1773 |
{ |
| 1774 |
List *listRepPtr = ListRepPtr(srcPtr); |
| 1775 |
|
| 1776 |
ListSetInternalRep(copyPtr, listRepPtr); |
| 1777 |
} |
| 1778 |
|
| 1779 |
/* |
| 1780 |
*---------------------------------------------------------------------- |
| 1781 |
* |
| 1782 |
* SetListFromAny -- |
| 1783 |
* |
| 1784 |
* Convert any object to a list. |
| 1785 |
* |
| 1786 |
* Value |
| 1787 |
* |
| 1788 |
* TCL_OK |
| 1789 |
* |
| 1790 |
* Success. The internal representation of 'objPtr' is set, and the type |
| 1791 |
* of 'objPtr' is 'tclListType'. |
| 1792 |
* |
| 1793 |
* TCL_ERROR |
| 1794 |
* |
| 1795 |
* An error occured during conversion. An error message is left in the |
| 1796 |
* interpreter's result if 'interp' is not NULL. |
| 1797 |
* |
| 1798 |
* |
| 1799 |
*---------------------------------------------------------------------- |
| 1800 |
*/ |
| 1801 |
|
| 1802 |
static int |
| 1803 |
SetListFromAny( |
| 1804 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 1805 |
Tcl_Obj *objPtr) /* The object to convert. */ |
| 1806 |
{ |
| 1807 |
List *listRepPtr; |
| 1808 |
Tcl_Obj **elemPtrs; |
| 1809 |
|
| 1810 |
/* |
| 1811 |
* Dictionaries are a special case; they have a string representation such |
| 1812 |
* that *all* valid dictionaries are valid lists. Hence we can convert |
| 1813 |
* more directly. Only do this when there's no existing string rep; if |
| 1814 |
* there is, it is the string rep that's authoritative (because it could |
| 1815 |
* describe duplicate keys). |
| 1816 |
*/ |
| 1817 |
|
| 1818 |
if (objPtr->typePtr == &tclDictType && !objPtr->bytes) { |
| 1819 |
Tcl_Obj *keyPtr, *valuePtr; |
| 1820 |
Tcl_DictSearch search; |
| 1821 |
int done, size; |
| 1822 |
|
| 1823 |
/* |
| 1824 |
* Create the new list representation. Note that we do not need to do |
| 1825 |
* anything with the string representation as the transformation (and |
| 1826 |
* the reverse back to a dictionary) are both order-preserving. Also |
| 1827 |
* note that since we know we've got a valid dictionary (by |
| 1828 |
* representation) we also know that fetching the size of the |
| 1829 |
* dictionary or iterating over it will not fail. |
| 1830 |
*/ |
| 1831 |
|
| 1832 |
Tcl_DictObjSize(NULL, objPtr, &size); |
| 1833 |
listRepPtr = AttemptNewList(interp, size > 0 ? 2*size : 1, NULL); |
| 1834 |
if (!listRepPtr) { |
| 1835 |
return TCL_ERROR; |
| 1836 |
} |
| 1837 |
listRepPtr->elemCount = 2 * size; |
| 1838 |
|
| 1839 |
/* |
| 1840 |
* Populate the list representation. |
| 1841 |
*/ |
| 1842 |
|
| 1843 |
elemPtrs = &listRepPtr->elements; |
| 1844 |
Tcl_DictObjFirst(NULL, objPtr, &search, &keyPtr, &valuePtr, &done); |
| 1845 |
while (!done) { |
| 1846 |
*elemPtrs++ = keyPtr; |
| 1847 |
*elemPtrs++ = valuePtr; |
| 1848 |
Tcl_IncrRefCount(keyPtr); |
| 1849 |
Tcl_IncrRefCount(valuePtr); |
| 1850 |
Tcl_DictObjNext(&search, &keyPtr, &valuePtr, &done); |
| 1851 |
} |
| 1852 |
} else { |
| 1853 |
int estCount, length; |
| 1854 |
const char *limit, *nextElem = TclGetStringFromObj(objPtr, &length); |
| 1855 |
|
| 1856 |
/* |
| 1857 |
* Allocate enough space to hold a (Tcl_Obj *) for each |
| 1858 |
* (possible) list element. |
| 1859 |
*/ |
| 1860 |
|
| 1861 |
estCount = TclMaxListLength(nextElem, length, &limit); |
| 1862 |
estCount += (estCount == 0); /* Smallest list struct holds 1 |
| 1863 |
* element. */ |
| 1864 |
listRepPtr = AttemptNewList(interp, estCount, NULL); |
| 1865 |
if (listRepPtr == NULL) { |
| 1866 |
return TCL_ERROR; |
| 1867 |
} |
| 1868 |
elemPtrs = &listRepPtr->elements; |
| 1869 |
|
| 1870 |
/* |
| 1871 |
* Each iteration, parse and store a list element. |
| 1872 |
*/ |
| 1873 |
|
| 1874 |
while (nextElem < limit) { |
| 1875 |
const char *elemStart; |
| 1876 |
int elemSize, literal; |
| 1877 |
|
| 1878 |
if (TCL_OK != TclFindElement(interp, nextElem, limit - nextElem, |
| 1879 |
&elemStart, &nextElem, &elemSize, &literal)) { |
| 1880 |
while (--elemPtrs >= &listRepPtr->elements) { |
| 1881 |
Tcl_DecrRefCount(*elemPtrs); |
| 1882 |
} |
| 1883 |
ckfree((char *) listRepPtr); |
| 1884 |
return TCL_ERROR; |
| 1885 |
} |
| 1886 |
if (elemStart == limit) { |
| 1887 |
break; |
| 1888 |
} |
| 1889 |
|
| 1890 |
/* TODO: replace panic with error on alloc failure? */ |
| 1891 |
if (literal) { |
| 1892 |
TclNewStringObj(*elemPtrs, elemStart, elemSize); |
| 1893 |
} else { |
| 1894 |
TclNewObj(*elemPtrs); |
| 1895 |
(*elemPtrs)->bytes = (char *)ckalloc((unsigned) elemSize + 1); |
| 1896 |
(*elemPtrs)->length = TclCopyAndCollapse(elemSize, elemStart, |
| 1897 |
(*elemPtrs)->bytes); |
| 1898 |
} |
| 1899 |
|
| 1900 |
Tcl_IncrRefCount(*elemPtrs++);/* Since list now holds ref to it. */ |
| 1901 |
} |
| 1902 |
|
| 1903 |
listRepPtr->elemCount = elemPtrs - &listRepPtr->elements; |
| 1904 |
} |
| 1905 |
|
| 1906 |
/* |
| 1907 |
* Free the old internalRep before setting the new one. We do this as late |
| 1908 |
* as possible to allow the conversion code, in particular |
| 1909 |
* Tcl_GetStringFromObj, to use that old internalRep. |
| 1910 |
*/ |
| 1911 |
|
| 1912 |
TclFreeIntRep(objPtr); |
| 1913 |
ListSetInternalRep(objPtr, listRepPtr); |
| 1914 |
return TCL_OK; |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* |
| 1918 |
*---------------------------------------------------------------------- |
| 1919 |
* |
| 1920 |
* UpdateStringOfList -- |
| 1921 |
* |
| 1922 |
* Update the string representation for a list object. |
| 1923 |
* |
| 1924 |
* Any previously-existing string representation is not invalidated, so |
| 1925 |
* storage is lost if this has not been taken care of. |
| 1926 |
* |
| 1927 |
* Effect |
| 1928 |
* |
| 1929 |
* The string representation of 'listPtr' is set to the resulting string. |
| 1930 |
* This string will be empty if the list has no elements. It is assumed |
| 1931 |
* that the list internal representation is not NULL. |
| 1932 |
* |
| 1933 |
*---------------------------------------------------------------------- |
| 1934 |
*/ |
| 1935 |
|
| 1936 |
static void |
| 1937 |
UpdateStringOfList( |
| 1938 |
Tcl_Obj *listPtr) /* List object with string rep to update. */ |
| 1939 |
{ |
| 1940 |
# define LOCAL_SIZE 64 |
| 1941 |
char localFlags[LOCAL_SIZE], *flagPtr = NULL; |
| 1942 |
List *listRepPtr = ListRepPtr(listPtr); |
| 1943 |
int numElems = listRepPtr->elemCount; |
| 1944 |
int i, length; |
| 1945 |
unsigned int bytesNeeded = 0; |
| 1946 |
const char *elem; |
| 1947 |
char *dst; |
| 1948 |
Tcl_Obj **elemPtrs; |
| 1949 |
|
| 1950 |
/* |
| 1951 |
* Mark the list as being canonical; although it will now have a string |
| 1952 |
* rep, it is one we derived through proper "canonical" quoting and so |
| 1953 |
* it's known to be free from nasties relating to [concat] and [eval]. |
| 1954 |
*/ |
| 1955 |
|
| 1956 |
listRepPtr->canonicalFlag = 1; |
| 1957 |
|
| 1958 |
/* |
| 1959 |
* Handle empty list case first, so rest of the routine is simpler. |
| 1960 |
*/ |
| 1961 |
|
| 1962 |
if (numElems == 0) { |
| 1963 |
listPtr->bytes = tclEmptyStringRep; |
| 1964 |
listPtr->length = 0; |
| 1965 |
return; |
| 1966 |
} |
| 1967 |
|
| 1968 |
/* |
| 1969 |
* Pass 1: estimate space, gather flags. |
| 1970 |
*/ |
| 1971 |
|
| 1972 |
if (numElems <= LOCAL_SIZE) { |
| 1973 |
flagPtr = localFlags; |
| 1974 |
} else { |
| 1975 |
/* |
| 1976 |
* We know numElems <= LIST_MAX, so this is safe. |
| 1977 |
*/ |
| 1978 |
|
| 1979 |
flagPtr = (char *)ckalloc(numElems); |
| 1980 |
} |
| 1981 |
elemPtrs = &listRepPtr->elements; |
| 1982 |
for (i = 0; i < numElems; i++) { |
| 1983 |
flagPtr[i] = (i ? TCL_DONT_QUOTE_HASH : 0); |
| 1984 |
elem = TclGetStringFromObj(elemPtrs[i], &length); |
| 1985 |
bytesNeeded += TclScanElement(elem, length, flagPtr+i); |
| 1986 |
if (bytesNeeded > INT_MAX) { |
| 1987 |
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); |
| 1988 |
} |
| 1989 |
} |
| 1990 |
if (bytesNeeded + numElems > INT_MAX + 1U) { |
| 1991 |
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); |
| 1992 |
} |
| 1993 |
bytesNeeded += numElems; |
| 1994 |
|
| 1995 |
/* |
| 1996 |
* Pass 2: copy into string rep buffer. |
| 1997 |
*/ |
| 1998 |
|
| 1999 |
/* |
| 2000 |
* We used to set the string length here, relying on a presumed |
| 2001 |
* guarantee that the number of bytes TclScanElement() calls reported |
| 2002 |
* to be needed was a precise count and not an over-estimate, so long |
| 2003 |
* as the same flag values were passed to TclConvertElement(). |
| 2004 |
* |
| 2005 |
* Then we saw [35a8f1c04a], where a bug in TclScanElement() caused |
| 2006 |
* that guarantee to fail. Rather than trust there are no more bugs, |
| 2007 |
* we set the length after the loop based on what was actually written, |
| 2008 |
* an not on what was predicted. |
| 2009 |
* |
| 2010 |
listPtr->length = bytesNeeded - 1; |
| 2011 |
* |
| 2012 |
*/ |
| 2013 |
|
| 2014 |
listPtr->bytes = (char *)ckalloc(bytesNeeded); |
| 2015 |
dst = listPtr->bytes; |
| 2016 |
for (i = 0; i < numElems; i++) { |
| 2017 |
flagPtr[i] |= (i ? TCL_DONT_QUOTE_HASH : 0); |
| 2018 |
elem = TclGetStringFromObj(elemPtrs[i], &length); |
| 2019 |
dst += TclConvertElement(elem, length, dst, flagPtr[i]); |
| 2020 |
*dst++ = ' '; |
| 2021 |
} |
| 2022 |
dst[-1] = '\0'; |
| 2023 |
|
| 2024 |
/* Here is the safe setting of the string length. */ |
| 2025 |
listPtr->length = dst - 1 - listPtr->bytes; |
| 2026 |
|
| 2027 |
if (flagPtr != localFlags) { |
| 2028 |
ckfree(flagPtr); |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
/* |
| 2033 |
* Local Variables: |
| 2034 |
* mode: c |
| 2035 |
* c-basic-offset: 4 |
| 2036 |
* fill-column: 78 |
| 2037 |
* End: |
| 2038 |
*/ |