Tcl_SaveResult(3tcl)

πŸ“¦
version 8.6.18 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– Tcl_SaveResult β€” bestie, we're doing time travel (interpreter edition)

okay so this page lives in the Tcl Library Procedures section of the manual, and it's all about the six routines that let you hit save game on an interpreter and then load it back up: Tcl_SaveInterpState, Tcl_RestoreInterpState, Tcl_DiscardInterpState, Tcl_SaveResult, Tcl_RestoreResult, Tcl_DiscardResult. Snapshot and restore an interpreter's state, no cap. πŸŽ€


✨ 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)

🫢 ARGUMENTS 🫢

  • Tcl_Interp *interp (in) β€” the interpreter whose state we're saving. the main character, obviously.
  • int status (in) β€” the return code value you want to save as part of the interpreter state.
  • Tcl_InterpState state (in) β€” the saved state token that's getting restored or discarded.
  • Tcl_SavedResult *savedPtr (in) β€” a pointer to the place where the interpreter result should be saved to or restored from.

🌸 DESCRIPTION 🌸

These routines let a C procedure take a whole little snapshot of an interpreter's current state, so it can be put back exactly like that after a call to Tcl_Eval or literally any other routine that messes with the interpreter state. There are two triplets of routines, and they're designed to work together. πŸ’…

Triplet number one πŸ¦‹ stores the snapshot of interpreter state in an opaque token that Tcl_SaveInterpState hands back to you. That token value can then get passed back to either Tcl_RestoreInterpState or Tcl_DiscardInterpState, depending on whether you actually want the interp state restored or not. And as long as you call one of those latter two routines, Tcl handles all the memory management for you. iconic behaviour honestly.

Triplet number two πŸŽ€ stores a snapshot of only the interpreter result (not its complete state) in memory that the caller allocated. These routines get a pointer to Tcl_SavedResult, which is used to store enough info to restore the interpreter result. Tcl_SavedResult can be allocated right on the stack of the calling procedure β€” cute and cheap. But heads up bestie: 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). πŸ™ˆ

So here's the tea: because Tcl_SaveInterpState, Tcl_RestoreInterpState, and Tcl_DiscardInterpState do a superset of what the other routines do, any new code should only use the more powerful routines. The older, weaker trio β€” Tcl_SaveResult, Tcl_RestoreResult, and Tcl_DiscardResult β€” still exists purely for the sake of existing programs that may already be using them. legacy queen energy. πŸ‘‘


⭐ the powerful trio ⭐

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 comes back to you as an opaque token of type Tcl_InterpState. And no β€” calling Tcl_SaveInterpState does not itself change the state of the interpreter. Unlike Tcl_SaveResult, it does not reset the interpreter. periodt. ✨

Tcl_RestoreInterpState accepts a Tcl_InterpState token that Tcl_SaveInterpState previously returned, and restores the state of the interp to the state held in that snapshot. Its return value is the status value that was originally passed to Tcl_SaveInterpState when the snapshot token was created.

Tcl_DiscardInterpState is the one you call to release a Tcl_InterpState token previously returned by Tcl_SaveInterpState, when that snapshot is not going to be restored to an interp.

🚨 the Tcl_InterpState token returned by Tcl_SaveInterpState must eventually be passed to either Tcl_RestoreInterpState or Tcl_DiscardInterpState, or you get a memory leak. and once the token has been passed to one of them, it is no longer valid and should not be used anymore. it's giving expired, do not text back. πŸ’€

🌈 the legacy trio 🌈

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.

Tcl_RestoreResult moves the string and value results from statePtr back into interp. Any result or error that was already in the interpreter gets cleared. The statePtr is left in an uninitialized state and cannot be used until another call to Tcl_SaveResult.

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.

πŸ’– 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. don't leave it hanging, that's so not slay.

πŸ”₯ KEYWORDS πŸ”₯

result, state, interp


and that's the whole vibe, bestie πŸ¦‹ this page is from the Tcl manual, version 8.1, and it's filed under Tcl_SaveResult(3tcl). go forth and snapshot responsibly πŸ’œβœ¨

πŸ“¦ where this page comes from