Module tcl::obj

source ·
Expand description

§An overview of Tcl values, aka Objs.

Tcl’s dual-ported values provide a general-purpose mechanism for storing and exchanging Tcl values. They largely replace the use of strings in Tcl. For example, they are used to store variable values, command arguments, command results, and scripts. Tcl values behave like strings but also hold an internal representation that can be manipulated more efficiently. For example, a Tcl list is now represented as a value that holds the list’s string representation as well as an array of pointers to the values for each list element. Dual-ported values avoid most runtime type conversions. They also improve the speed of many operations since an appropriate representation is immediately available. The interpreter itself uses Tcl values to cache the instruction bytecodes resulting from compiling scripts.

The two representations are a cache of each other and are computed lazily. That is, each representation is only computed when necessary, it is computed from the other representation, and, once computed, it is saved. In addition, a change in one representation invalidates the other one. As an example, a Tcl program doing integer calculations can operate directly on a variable’s internal machine integer representation without having to constantly convert between integers and strings. Only when it needs a string representing the variable’s value, say to print it, will the program regenerate the string representation from the integer. Although values contain an internal representation, their semantics are defined in terms of strings: an up-to-date string can always be obtained, and any change to the value will be reflected in that string when the value’s string representation is fetched.

Values are allocated on the heap and are referenced using a smart pointer Obj. Values are shared as much as possible. This significantly reduces storage requirements because some values such as long lists are very large. Also, most Tcl values are only read and never modified. This is especially true for procedure arguments, which can be shared between the caller and the called procedure. Assignment and argument binding is done by simply assigning a pointer to the value. Reference counting is used to determine when it is safe to reclaim a value’s storage.

Tcl values are typed. A value’s internal representation is controlled by its type. Several types are predefined in the Tcl core including integer, double, list, and bytecode. Extension writers can extend the set of types by defining their own Tcl_ObjType structs. This crate provides a registered type Tcl<T>.

Structs§

  • A smart pointer that points to a referece-counted, heap-allocated Tcl value.

Functions§

  • Decrease the reference count of the Tcl obj which the tcl_obj points to.
  • Increase the reference count of the Tcl obj which the tcl_obj points to.