Tcl_SetResult(3tcl)
π Tcl_SetResult β manipulate Tcl result π
okay bestie, welcome to the Tcl Library Procedures corner of the universe β¨ this whole page lives in Tcl_SetResult(3tcl) and it's literally all about one thing: the result of an interpreter. sit down, grab a strawberry π, we're doing this.
πΈ NAME πΈ
Tcl_SetObjResult, Tcl_GetObjResult, Tcl_SetResult, Tcl_GetStringResult, Tcl_AppendResult, Tcl_AppendResultVA, Tcl_AppendElement, Tcl_ResetResult, Tcl_TransferResult, Tcl_FreeResult β these are the girlies that manipulate Tcl result. that's the whole job description, periodt π
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_SetObjResult(interp, objPtr)
Tcl_Obj *
Tcl_GetObjResult(interp)
Tcl_SetResult(interp, result, freeProc)
const char *
Tcl_GetStringResult(interp)
Tcl_AppendResult(interp, result, result, ... , (char *)NULL)
Tcl_AppendResultVA(interp, argList)
Tcl_ResetResult(interp)
Tcl_TransferResult(sourceInterp, code, targetInterp)
Tcl_AppendElement(interp, element)
Tcl_FreeResult(interp)
π ARGUMENTS π
Tcl_Interp *interp(out) β the interpreter whose result is getting modified or read. she's the main character here, obviously πTcl_Obj *objPtr(in) β the Tcl value that is gonna become the result forinterp.char *result(in) β a string value that becomes the result forinterp, or gets appended to whatever result is already there.const char *element(in) β a string value to append as a list element tointerp's existing result.Tcl_FreeProc *freeProc(in) β address of a procedure to call to release the storage atresult, OR one ofTCL_STATIC,TCL_DYNAMIC, orTCL_VOLATILE. three moods, pick one π«Άva_list argList(in) β an argument list that must have been initialized usingva_start, and cleared usingva_end. don't skip that, fr fr.Tcl_Interp *sourceInterp(in) β interpreter that the result and return options should be transferred from.Tcl_Interp *targetInterp(in) β interpreter that the result and return options should be transferred to.int code(in) β return code value that controls transfer of return options.
π¦ DESCRIPTION π¦
So the procedures described here are utilities for manipulating the result value in a Tcl interpreter. and listen, the interpreter result may be either a Tcl value or a string β two forms, one queen π
for example, Tcl_SetObjResult and Tcl_SetResult set the interpreter result to, respectively, a value and a string. similarly, Tcl_GetObjResult and Tcl_GetStringResult return the interpreter result as a value and as a string. and the procedures always keep the string and value forms of the interpreter result consistent. like, they never let them drift apart, that's their whole thing. for example, if Tcl_SetObjResult is called to set the result to a value, then Tcl_GetStringResult is called, it will return the value's string representation. slay β¨
Tcl_SetObjResult arranges for objPtr to be the result for interp, replacing any existing result. the result is left pointing to the value referenced by objPtr. objPtr's reference count is incremented since there is now a new reference to it from interp. the reference count for any old result value is decremented, and the old result value is freed if no references to it remain. out with the old, in with the new πβ‘οΈπ
Tcl_GetObjResult returns the result for interp as a value. heads up tho: the value's reference count is not incremented. so if the caller needs to retain a long-term pointer to the value, they should use Tcl_IncrRefCount to increment its reference count in order to keep it from being freed too early or accidentally changed. protect her!! π‘οΈ
Tcl_SetResult arranges for result to be the result for the current Tcl command in interp, replacing any existing result. the freeProc argument specifies how to manage the storage for the result argument β and there's a whole section on that below, it's giving its own chapter, iykyk. if result is NULL, then freeProc is ignored and Tcl_SetResult re-initializes interp's result to point to an empty string. clean slate, no notes πΈ
Tcl_GetStringResult returns the result for interp as a string. if the result was set to a value by a Tcl_SetObjResult call, the value form will be converted to a string and returned. however β and this is the important part β if the value's string representation contains null bytes, this conversion will lose information π for this reason, programmers are encouraged to write their code to use the new value API procedures and to call Tcl_GetObjResult instead. do it for your own good bestie.
Tcl_ResetResult clears the result for interp and leaves the result in its normal empty initialized state. if the result is a value, its reference count is decremented and the result is left pointing to an unshared value representing an empty string. if the result is a dynamically allocated string, its memory is freed and the result is left as a empty string. Tcl_ResetResult also clears the error state managed by Tcl_AddErrorInfo, Tcl_AddObjErrorInfo, and Tcl_SetErrorCode. full factory reset energy π§Ό
Tcl_AppendResult makes it easy to build up Tcl results in pieces. it takes each of its result arguments and appends them in order to the current result associated with interp. if the result is in its initialized empty state (e.g. a command procedure was just invoked or Tcl_ResetResult was just called), then Tcl_AppendResult sets the result to the concatenation of its result arguments. Tcl_AppendResult may be called repeatedly as additional pieces of the result are produced. it takes care of all the storage management issues associated with managing interp's result, such as allocating a larger result area if necessary. it also manages conversion to and from the result field of the interp so as to handle backward-compatibility with old-style extensions. any number of result arguments may be passed in a single call; the last argument in the list must be (char *)NULL. don't forget the sentinel, she's load-bearing π«
Tcl_AppendResultVA is the same as Tcl_AppendResult except that instead of taking a variable number of arguments it takes an argument list.
Tcl_TransferResult transfers interpreter state from sourceInterp to targetInterp. the two interpreters must have been created in the same thread. if sourceInterp and targetInterp are the same, nothing is done. otherwise, Tcl_TransferResult moves the result from sourceInterp to targetInterp, and resets the result in sourceInterp. it also moves the return options dictionary as controlled by the return code value code in the same manner as Tcl_GetReturnOptions. it's giving relocation arc πβ¨
π DEPRECATED INTERFACES π
π OLD STRING PROCEDURES π
Use of the following procedures is deprecated since they manipulate the Tcl result as a string. procedures such as Tcl_SetObjResult that manipulate the result as a value can be significantly more efficient. like, glow-up-wise? string versions are not it anymore.
β οΈ bestie this is the deprecated section. if you're writing new code, this is not your era. it's giving 2004 flip phone. still documented, still here, but tread carefully.
Tcl_AppendElement is similar to Tcl_AppendResult in that it allows results to be built up in pieces. however, Tcl_AppendElement takes only a single element argument and it appends that argument to the current result as a proper Tcl list element. Tcl_AppendElement adds backslashes or braces if necessary to ensure that interp's result can be parsed as a list and that element will be extracted as a single element. under normal conditions, Tcl_AppendElement will add a space character to interp's result just before adding the new list element, so that the list elements in the result are properly separated. however if the new list element is the first in a list or sub-list (i.e. interp's current result is empty, or consists of the single character β{β, or ends in the characters β {β) then no space is added. she knows when to hold back π
Tcl_FreeResult performs part of the work of Tcl_ResetResult. it frees up the memory associated with interp's result. it also sets interp->freeProc to zero, but does not change interp->result or clear error state. Tcl_FreeResult is most commonly used when a procedure is about to replace one result value with another.
π DIRECT ACCESS TO INTERP->RESULT π
It used to be legal for programs to directly read and write interp->result to manipulate the interpreter result. the Tcl headers no longer permit this access by default, and C code still doing this must be updated to use supported routines Tcl_GetObjResult, Tcl_GetStringResult, Tcl_SetObjResult, and Tcl_SetResult. as a migration aid, access can be restored with the compiler directive
#define USE_INTERP_RESULT
π¨ but this is meant only to offer life support to otherwise dead code. it's not a comeback tour, it's a ventilator. do not build new things on it.
π§ THE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT π§
Tcl_SetResult's freeProc argument specifies how the Tcl system is to manage the storage for the result argument. if Tcl_SetResult or Tcl_SetObjResult are called at a time when interp holds a string result, they do whatever is necessary to dispose of the old string result (see the Tcl_Interp manual entry for details on this). bye old string, you were fun βοΈ
now here are the three iconic constants:
- if
freeProcisTCL_STATICit means thatresultrefers to an area of static storage that is guaranteed not to be modified until at least the next call toTcl_Eval. - if
freeProcisTCL_DYNAMICit means thatresultwas allocated with a call toTcl_Allocand is now the property of the Tcl system.Tcl_SetResultwill arrange for the string's storage to be released by callingTcl_Freewhen it is no longer needed. - if
freeProcisTCL_VOLATILEit means thatresultpoints to an area of memory that is likely to be overwritten whenTcl_SetResultreturns (e.g. it points to something in a stack frame). in this caseTcl_SetResultwill make a copy of the string in dynamically allocated storage and arrange for the copy to be the result for the current Tcl command. she's making a backup, she's not risking it π«Ά
and if freeProc is not one of the values TCL_STATIC, TCL_DYNAMIC, and TCL_VOLATILE, then it is the address of a procedure that Tcl should call to free the string. this allows applications to use non-standard storage allocators. when Tcl no longer needs the storage for the string, it will call freeProc. FreeProc should have arguments and result that match the type Tcl_FreeProc:
typedef void Tcl_FreeProc(
char *blockPtr);
when freeProc is called, its blockPtr will be set to the value of result passed to Tcl_SetResult. no cap, that's the whole handoff.
π SEE ALSO π
Tcl_AddErrorInfo, Tcl_CreateObjCommand, Tcl_SetErrorCode, Tcl_Interp, Tcl_GetReturnOptions
β KEYWORDS β
append, command, element, list, value, result, return value, interpreter
and that's the whole page bestie, you ate that π½οΈπ this was the Tcl 8.6 era of Tcl_SetResult(3tcl), so go forth and set your results responsibly. mwah π
πΈ
π¦ where this page comes from
- debian trixie: tcl8.6-doc 8.6.16+dfsg-1
- ubuntu questing: tcl8.6-doc 8.6.17+dfsg-1
- ubuntu resolute: tcl8.6-doc 8.6.17+dfsg-1build1
- ubuntu stonking: tcl8.6-doc 8.6.18+dfsg-1
- Copyright (c) 1989-1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms