#include <tcl.h> Tcl_SetObjResult(interp, objPtr) Tcl_Obj * Tcl_GetObjResult(interp) Tcl_SetResult(interp, string, freeProc) char * Tcl_GetStringResult(interp) Tcl_AppendResult(interp, string, string, ... , (char *) NULL) Tcl_AppendElement(interp, string) Tcl_ResetResult(interp) Tcl_FreeResult(interp)
Tcl_SetObjResult arranges for objPtr to be the result for the current Tcl command in interp, replacing any existing result. If objPtr is NULL, the interpreter's result is set to a new Tcl object containing an empty string. Otherwise, the result is left pointing to the object 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 object is decremented and the old result object is freed if no references to it remain.
Tcl_GetObjResult returns the result for interp as an object. The object's reference count is not incremented; if the caller needs to retain a long-term pointer to the object they should use Tcl_IncrRefCount to increment its reference count in order to keep it from being freed too early or accidently changed.
Tcl_SetResult arranges for string 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 string argument; it is discussed in the section THE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT below. If string 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 an string. If the result was set to an object by a Tcl_SetObjResult call, the object form will be converted to a string and returned. If the object'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 object API procedures and to call Tcl_GetObjResult instead.
Tcl_AppendResult makes it easy to build up Tcl results in pieces. It takes each of its string 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 string arguments. Tcl_AppendResult may be called repeatedly as additional pieces of the result are produced. Tcl_AppendResult 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 converts the current interpreter result from an object to a string, if necessary, before appending the argument strings. Any number of string arguments may be passed in a single call; the last argument in the list must be a NULL pointer.
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 string 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 string 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.
Tcl_ResetResult clears the result for interp and leaves the result in its normal empty initialized state. If the result is an object, its reference count is decremented and the result is left pointing to an unshared object 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.
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 doesn't 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.
Although the result procedures keep the string and object forms of the interpreter result consistent, this is not true when programs write interp->result directly. Furthermore, it is unsafe to get the interpreter result by reading interp->result directly; the result might be held internally by the Tcl interpreter as an object. Programs should read the result using the procedures Tcl_GetObjResult or Tcl_GetStringResult.
Programmers are strongly encouraged to use the result procedures to read and write the interpreter result. These handle all the details of correctly getting or setting the result. For example, Tcl_GetObjResult returns objResultPtr but only after first checking if result is non-empty. If so, objResultPtr is either empty or stale because some procedure set interp->result directly, so Tcl_GetObjResult makes objResultPtr hold the string in result then resets result to an empty string. Similarly, Tcl_GetStringResult returns result but only after first moving objResultPtr to result if result was empty.
The procedures that set the result also reset the result and objResultPtr fields as necessary in order to implement the result rule above. For example, after setting objResultPtr, Tcl_SetObjResult resets result to ensure that any string it points to won't be misinterpreted as the current result.
Because the Tcl system uses the result rule to get the result, setting the result by storing a string into interp->result usually works even though it is not recommended. Reading interp->result to get the result, however, is unsafe and will fail if the result was last set to an object.
Because Tcl_GetObjResult clears out any old string result, this code ensures that the integer object is treated as the result. Note that the Tcl_GetObjResult call must be made after any other procedure calls that might change the result. It does not work to call Tcl_GetObjResult once to get a pointer to the result object, make a number of calls, then set that object to the desired value. After those calls, the object pointer may no longer point to the correct interpreter result. Also, if one of the various calls set the interpreter result to a string, that string result would override the value in the object pointer; the result rule above would ensure that this string was treated as the result. It is generally best to use Tcl_SetObjResult to set the result to an object.Tcl_SetIntObj(Tcl_GetObjResult(interp), numMapEntries);
Object-based command procedures (i.e., those created using Tcl_CreateObjCommand) are expected to return an object in the interpreter's result. They can return a string directly without having to first create a new string object and then call Tcl_SetObjResult. This is because the Tcl interpreter always converts a non-empty string result to an object after calling a command procedure. This allows an object-based command procedure to return an error message from any procedure it calls without being concerned about whether the procedure set the interpreter result to a string or an object. It is not necessary for the command procedure to copy an error string to the object result. Note that the following code
is not necessary and, in any case, does not work correctly. The Tcl_GetObjResult call clears out a non-empty string result after moving it to the object result, so this code ends up clearing the interpreter result and discarding the string that was in interp->result.Tcl_SetStringObj(Tcl_GetObjResult(interp), interp->result, -1);
If freeProc is TCL_STATIC it means that string 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 string 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 is no longer needed. If freeProc is TCL_VOLATILE it means that string 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 isn't 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:
When freeProc is called, its blockPtr will be set to the value of string passed to Tcl_SetResult.typedef void Tcl_FreeProc(char *blockPtr);
Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1997 Sun Microsystems, Inc.