Tcl_SetResult(3tcl)

πŸ“¦
version 9.0.4 Β· 4 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay bestie, welcome to the Tcl_SetResult(3tcl) page from the Tcl Library Procedures β€” this is THE spot for manipulating the Tcl result. the whole squad is here: Tcl_SetObjResult, Tcl_GetObjResult, Tcl_SetResult, Tcl_GetStringResult, Tcl_AppendResult, Tcl_AppendElement, Tcl_ResetResult, and Tcl_TransferResult. they all do one thing and one thing only: manipulate Tcl result. 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_ResetResult(interp)

Tcl_TransferResult(sourceInterp, code, targetInterp)

Tcl_AppendElement(interp, element)

🫢 ARGUMENTS 🫢

  • Tcl_Interp *interp (out) β€” the interpreter whose result is getting modified or read, okay?
  • Tcl_Obj *objPtr (in) β€” the Tcl value that's about to become the result for interp.
  • char *result (in) β€” a string value that becomes the result for interp, or gets appended onto whatever result was already there.
  • const char *element (in) β€” a string value to append as a list element onto interp's existing result.
  • Tcl_FreeProc *freeProc (in) β€” the address of a procedure to call to release storage at result, or one of TCL_STATIC, TCL_DYNAMIC, or TCL_VOLATILE. (iykyk)
  • Tcl_Interp *sourceInterp (in) β€” the interpreter that the result and return options should be transferred from.
  • Tcl_Interp *targetInterp (in) β€” the interpreter that the result and return options should be transferred to.
  • int code (in) β€” a return code value that controls the transfer of return options.

🌸 DESCRIPTION 🌸

so like, all the procedures below? they're utilities for manipulating the result value in a Tcl interpreter. the interpreter result can be either a Tcl value or a string. for example, Tcl_SetObjResult and Tcl_SetResult set the interpreter result to, respectively, a value and a string. similarly, Tcl_GetObjResult and Tcl_GetStringResult hand you back the interpreter result as a value and as a string. and the way these procedures always keep the string and value forms of the interpreter result consistent?? iconic behaviour honestly. like, if Tcl_SetObjResult is called to set the result to a value, and then Tcl_GetStringResult is called, it'll return that value's string representation. no notes. πŸ’―

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's now a new reference to it from interp. and the reference count for any old result value is decremented, and the old result value gets freed if no references to it remain. bye girl πŸ‘‹

Tcl_GetObjResult returns the result for interp as a value. heads up though: the value's reference count is not incremented. so if the caller needs to retain a long-term pointer to that value, they should use Tcl_IncrRefCount to increment its reference count, so it doesn't get freed too early or accidentally changed on you. 😭

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 β€” it gets discussed down in the section THE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT below, don't worry. if result is NULL, then freeProc is ignored and Tcl_SetResult re-initializes interp's result to point to an empty string.

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. but β€” and this is the tea β€” 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 that, 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 an empty string. Tcl_ResetResult also clears the error state managed by Tcl_AddErrorInfo, Tcl_AddObjErrorInfo, and Tcl_SetErrorCode. full factory reset vibes ✨

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 β€” but the last argument in the list must be (char *)NULL. no cap, don't forget it. πŸ’€

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✨


πŸ™ˆ 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. so like... maybe don't? πŸ‘‘

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. the way that detail lives rent free in my head fr fr 🧚


πŸ’… 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).

okay so here's the breakdown, bestie:

  • if freeProc is TCL_STATIC it means that result refers to an area of static storage that is guaranteed not to be modified until at least the next call to Tcl_Eval.
  • if freeProc is TCL_DYNAMIC it means that result was allocated with a call to Tcl_Alloc and is now the property of the Tcl system. Tcl_SetResult will arrange for the string's storage to be released by calling Tcl_Free when it's no longer needed.
  • if freeProc is TCL_VOLATILE it means that result points to an area of memory that is likely to be overwritten when Tcl_SetResult returns (e.g. it points to something in a stack frame). in this case Tcl_SetResult will make a copy of the string in dynamically allocated storage and arrange for the copy to be the result for the current Tcl command.

if freeProc is not one of the values TCL_STATIC, TCL_DYNAMIC, and TCL_VOLATILE, then it's 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(
        void *blockPtr);

when freeProc is called, its blockPtr will be set to the value of result passed to Tcl_SetResult.


⭐ REFERENCE COUNT MANAGEMENT ⭐

the interpreter result is one of the main places that owns references to values, along with the bytecode execution stack, argument lists, variables, and the list and dictionary collection values. main character energy honestly πŸ‘‘

Tcl_SetObjResult takes a value with an arbitrary reference count (specifically including zero) and guarantees to increment the reference count. if code wishes to continue using the value after setting it as the result, it should add its own reference to it with Tcl_IncrRefCount.

Tcl_GetObjResult returns the current interpreter result value. this will have a reference count of at least 1. if the caller wishes to keep the interpreter result value, it should increment its reference count.

Tcl_GetStringResult does not manipulate reference counts, but the string it returns is owned by (and has a lifetime controlled by) the current interpreter result value. it should be copied instead of being relied upon to persist after the next Tcl API call, as most Tcl operations can modify the interpreter result. 😭 protect your pointers, girlies.

Tcl_SetResult, Tcl_AppendResult, Tcl_AppendElement, and Tcl_ResetResult all modify the interpreter result. they may cause the old interpreter result to have its reference count decremented and a new interpreter result to be allocated. after they've been called, the reference count of the interpreter result is guaranteed to be 1. slay.


πŸ¦‹ 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!! πŸ’– this was Tcl version 9.0, the Tcl_SetResult(3tcl) manual page β€” go forth and manipulate those results responsibly, ily, mwah 🫢✨🌈

πŸ“¦ where this page comes from