Tcl_SaveResult(3)

πŸ“¦
version 8.6.17 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, opensuse-leap 16.0

πŸ’… Tcl_SaveResult β€” the Tcl Library Procedures glow-up πŸŽ€

Okay bestie, so this is the Tcl_SaveResult(3) page from the Tcl Library Procedures, and it's basically about snapshotting an interpreter so you can put it back later. Like hitting save before you do something unhinged. No cap.


✨ NAME ✨

Tcl_SaveInterpState, Tcl_RestoreInterpState, Tcl_DiscardInterpState, Tcl_SaveResult, Tcl_RestoreResult, Tcl_DiscardResult β€” save and restore an interpreter's state. That's the whole vibe. Six little functions, two little trios, one big slay. πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <tcl.h>

Tcl_InterpState
Tcl_SaveInterpState(interp, status)

int
Tcl_RestoreInterpState(interp, state)

Tcl_DiscardInterpState(state)

Tcl_SaveResult(interp, savedPtr)

Tcl_RestoreResult(interp, savedPtr)

Tcl_DiscardResult(savedPtr)

Like, look at them. Little guys. All in a row. 🫢


🌸 ARGUMENTS 🌸

  • Tcl_Interp *interp (in) β€” the interpreter whose state you're trying to save. Your interpreter. The main character.
  • int status (in) β€” the return code value you wanna stash away as part of the interpreter state. It gets saved along for the ride.
  • Tcl_InterpState state (in) β€” the saved state token. Either you're restoring it or you're discarding it, bestie, pick one.
  • Tcl_SavedResult *savedPtr (in) β€” pointer to the spot where the interpreter result should be saved or restored. It's giving "storage unit for your result." πŸŽ€

πŸ’œ DESCRIPTION πŸ’œ

So these routines let a C procedure take a snapshot of the current state of an interpreter, so you can restore it after a call to Tcl_Eval or literally any other routine that messes with the interpreter state. And there are two triplets of routines meant to work together. Two. Triplets. Iconic.

Triplet number one 🧚 stores the snapshot of interpreter state in an opaque token, returned by Tcl_SaveInterpState. That token value can then be handed back to either Tcl_RestoreInterpState or Tcl_DiscardInterpState, depending on whether you actually want the interp state restored. And here's the good part: so long as you call one of those latter two routines, Tcl handles the memory management for you. Literally not your problem. 😌

Triplet number two 🌈 stores the snapshot of only the interpreter result (not its complete state, bestie, just the result) in memory allocated by the caller. These routines get a pointer to Tcl_SavedResult used to store enough info to restore the interpreter result. And good news: Tcl_SavedResult can be allocated right on the stack of the calling procedure. Very demure, very mindful. But real talk β€” these routines do not save the state of any error information in the interpreter. Like, the -errorcode or -errorinfo return options when an error is in progress? Not saved. Missed those. πŸ™ˆ

Because Tcl_SaveInterpState, Tcl_RestoreInterpState, and Tcl_DiscardInterpState perform a superset of what the other routines do, any new code should only use the more powerful routines. The older, weaker Tcl_SaveResult, Tcl_RestoreResult, and Tcl_DiscardResult only continue to exist for the sake of existing programs that may already be using them. They're the legacy besties. We respect them, but we don't build new stuff on them. πŸ’…

πŸ’« Tcl_SaveInterpState πŸ’«

Takes a snapshot of those portions of interpreter state that make up the full result of script evaluation. This includes the interpreter result, the return code (passed in as the status argument), and any return options β€” including -errorinfo and -errorcode when an error is in progress. This snapshot is returned as an opaque token of type Tcl_InterpState. The call to Tcl_SaveInterpState does not itself change the state of the interpreter. And unlike Tcl_SaveResult, it does not reset the interpreter. Big difference. Note it. πŸ“

πŸ“ Tcl_RestoreInterpState πŸ“

Accepts a Tcl_InterpState token previously returned by Tcl_SaveInterpState and restores the state of the interp to the state held in that snapshot. The return value of Tcl_RestoreInterpState is the status value originally passed to Tcl_SaveInterpState when the snapshot token was created. Cute little callback, honestly.

⭐ Tcl_DiscardInterpState ⭐

Called to release a Tcl_InterpState token previously returned by Tcl_SaveInterpState when that snapshot is not to be restored to an interp. Bye bestie, we're not using you. πŸ‘‹

⚠️ The Tcl_InterpState token returned by Tcl_SaveInterpState must eventually be passed to either Tcl_RestoreInterpState or Tcl_DiscardInterpState to avoid a memory leak. And once the Tcl_InterpState token is passed to one of them, the token is no longer valid and should not be used anymore. Dead token. Do not touch. πŸ’€

πŸŽ€ Tcl_SaveResult πŸŽ€

Moves the string and value results of interp into the location specified by statePtr. Tcl_SaveResult clears the result for interp and leaves the result in its normal empty initialized state. Fresh. Clean. Empty. ✨

πŸ¦‹ Tcl_RestoreResult πŸ¦‹

Moves the string and value results from statePtr back into interp. Any result or error that was already in the interpreter will be cleared β€” sorry to whoever was living there. And the statePtr is left in an uninitialized state and can't be used until another call to Tcl_SaveResult. So don't go poking it. πŸ™…

🌸 Tcl_DiscardResult 🌸

Releases the saved interpreter state stored at statePtr. The state structure is left in an uninitialized state and cannot be used until another call to Tcl_SaveResult. Same deal. It's spent. 🫠

⚠️ Once Tcl_SaveResult is called to save the interpreter result, either Tcl_RestoreResult or Tcl_DiscardResult must be called to properly clean up the memory associated with the saved state. No skipping. The memory will absolutely notice. 😭

πŸ‘‘ KEYWORDS πŸ‘‘

result, state, interp


Okay that's the whole page, you ate that πŸ’…βœ¨ This little masterpiece is from Tcl 8.1, dated way back, and it's the Tcl_SaveResult(3) manual page β€” so go forth and snapshot responsibly, bestie. πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from