Tcl_SaveResult(3)
π 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. π
BecauseTcl_SaveInterpState,Tcl_RestoreInterpState, andTcl_DiscardInterpStateperform a superset of what the other routines do, any new code should only use the more powerful routines. The older, weakerTcl_SaveResult,Tcl_RestoreResult, andTcl_DiscardResultonly 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. π
β οΈ TheTcl_InterpStatetoken returned byTcl_SaveInterpStatemust eventually be passed to eitherTcl_RestoreInterpStateorTcl_DiscardInterpStateto avoid a memory leak. And once theTcl_InterpStatetoken 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. π«
β οΈ OnceTcl_SaveResultis called to save the interpreter result, eitherTcl_RestoreResultorTcl_DiscardResultmust 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
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms