Ashot
Ashot

Reputation: 10959

Interpreter creates copy of object in the trace

Tcl: Interpreter creates copy of traced object whet it goes changed in this question I asked why does interpreter creates a copy of object in the trace? I understand that using handles is the right way here but it requires huge work, therefore I need workaround solution.

I need the code in the trace behave exactly the same as if it was written after set command. I need to change the object in the trace and I want the interpreter not to make a copy of object. It would be great if somehow disable the copying functionality in the trace function.

I have looked here http://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl/TraceVar.3.html but didn't find it.

Upvotes: 0

Views: 86

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

DON'T DO THIS!


Changing the Tcl_Obj itself when there are multiple references held open to it (i.e., when Tcl_IsShared reports that the value is shared) can cause all sorts of semantic problems (many functions — e.g., Tcl_SetIntObj — specifically check and will abort the process if you try) so you need to be exceptionally careful. You're moving outside Tcl's semantics; there are dragons here!

To directly modify a Tcl_Obj, look at the bytes field of the structure (the length field is supposed to be kept as the strlen of bytes). That's writable; just change it, bearing in mind that it is always encoded as pseudo-UTF-8 (which is very straight forward for ASCII characters other than \u0000). If you want to change what is allocated, that field must be allocated with Tcl_Alloc. If bytes is NULL, there is no string representation; look to the internal representation for meaning.

The internal representation of a Tcl_Obj is determined by the internalRep union and the typePtr field. Understanding the meaning of the internalRep depends on understanding what typePtr points to; usually, if you didn't implement it, you shouldn't poke around at all. We do change what internal representations are present in patch releases sometimes. If you defined the typePtr to point to a structure you own, you'll know how to understand the internal representation. When the typePtr field is NULL, there is no internal representation.

Sometimes you may encounter a Tcl_Obj with neither internal nor string representation. DO NOT CHANGE THIS if you find one. It is reserved for the (very highly-shared) empty value.

While is technically possible to alter a shared object by poking around inside, the rest of Tcl assumes that you're not going to do this so it's pretty random what might break. You might even get away with it! But be aware that we won't formally support you; you're on your own.

Upvotes: 2

Related Questions