Skip to content
Tcl Source Code Artifact [e19689b8ce]
Login

Artifact [e19689b8ce]

Artifact e19689b8ced0b1aebad9e896ba0a435d207f4799:

Wiki page [Variables] by dkf on 2016-08-20 17:59:52.
D 2016-08-20T17:59:52.569
L Variables
U dkf
W 3036
<h2>General Structure</h2>

Tcl internally has two main types of variables. These are variables that are in a Tcl_HashTable, and variables that are in a Local Variable Table (LVT) that is associated with a (procedure-like) stack frame. The variables in a hash table are larger than the variables in an LVT; they contain additional fields used to manage the variable in the context of the hash, but they share a common prefix structure; the <code>Var</code> struct. (A <code>Tcl_Var</code> is an opaque <code>Var*</code>.)

The <code>Var</code> has two fields: the <code>flags</code> and the <code>value</code>. The <code>flags</code> is an integer bit-set that's used to describe a whole bunch of things (such as whether the variable is a scalar, an array or a link to another variable). The <code>value</code> is a pointer, or rather more strictly it is a union of three pointers: a pointer to a <code>Tcl_Obj*</code> when the variable is a scalar, a pointer to a (specialisation of) <code>Tcl_HashTable</code> when the variable is an array, and a <code>Var*</code> when the variable is a link. When the pointer is NULL, the variable is considered to be unset. Variables in LVTs can have NULL values for as long as their stack frame persists, and variables in hash tables are usually removed as soon as their value becomes NULL (though not always; links and traces can keep them alive).

<h2>Access API</h2>

Internally, most of Tcl's considerable variable API funnels through a small set of core functions:

<code>TclObjLookupVarEx</code> is responsible for mapping a variable name (in one or two part form) to a <code>Var*</code> that can be accessed. It also does link traversal; the variable references it returns are to the variables that can actually be accessed, not their aliases. Where it can, it caches much of the interpretations of what it finds out in the <code>Tcl_Obj</code> references to the variable names; this is why it can be faster to use <code>Tcl_ObjGetVar2</code> than <code>Tcl_GetVar</code>, though it is only really faster if you can handle the lifetime of the variable names usefully.

The other core part of Tcl's variable API are <code>TclPtrGetVar</code>, <code>TclPtrSetVar</code>, <code>TclPtrIncrObjVar</code>, and <code>TclPtrUnsetVar</code>. These are what actually do the reading, writing (including both <code>append</code> and <code>lappend</code>), incrementing and unsetting of variables, respectively.

With bytecode-compiled procedures, most of the calls to <code>TclObjLookupVarEx</code> are omitted because the variable references are converted into integer lookups into the LVT, which is far faster. Moreover, in the simplest cases the accesses to the variables by the other core API functions are also bypassed and the contents of the variables are accessed directly. It's only when the variables have things like traces or are accessed from outside the local context that the full API described above is used. Dynamic variable names <i>always</i> use the full API path.


Z 5d94da8558a34c5cdd2d4bdc1ed5a323