NRE(3)
NRE(3) π the stackless era is HERE bestie
okay so before we even start: this is a page from the Tcl Library Procedures manual, and it's about doing Tcl evaluation without recursively eating your C stack. no cap, this is the good stuff. let's go π¦
β¨ NAME β¨
Tcl_NRCreateCommand, Tcl_NRCreateCommand2, Tcl_NRCallObjProc, Tcl_NRCallObjProc2, Tcl_NREvalObj, Tcl_NREvalObjv, Tcl_NRCmdSwap, Tcl_NRExprObj, Tcl_NRAddCallback β Non-Recursive (stackless) evaluation of Tcl scripts. periodt π
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_Command
Tcl_NRCreateCommand(interp, cmdName, proc, nreProc, clientData,
deleteProc)
Tcl_Command
Tcl_NRCreateCommand2(interp, cmdName, proc2, nreProc2, clientData,
deleteProc)
int
Tcl_NRCallObjProc(interp, nreProc, clientData, objc, objv)
int
Tcl_NRCallObjProc2(interp, nreProc2, clientData, objc, objv)
int
Tcl_NREvalObj(interp, objPtr, flags)
int
Tcl_NREvalObjv(interp, objc, objv, flags)
int
Tcl_NRCmdSwap(interp, cmd, objc, objv, flags)
int
Tcl_NRExprObj(interp, objPtr, resultPtr)
Tcl_NRAddCallback(interp, postProcPtr, data0, data1, data2, data3)
πΈ ARGUMENTS πΈ
like, every single one, bestie, no skipping π
Tcl_Interp *interp(in) β the relevant Interpreter. the main character fr.const char *cmdName(in) β name of the command to create.Tcl_ObjCmdProc *proc(in) β called in order to evaluate a command. it's often just a tiny little wrapper that usesTcl_NRCallObjProcto callnreProcusing a new trampoline. behaves in the same way as theprocargument toTcl_CreateObjCommand(3)(q.v.).Tcl_ObjCmdProc2 *proc2(in) β called in order to evaluate a command. often just a small wrapper that usesTcl_NRCallObjProc2to callnreProc2using a new trampoline. behaves in the same way as theproc2argument toTcl_CreateObjCommand2(3)(q.v.).Tcl_ObjCmdProc *nreProc(in) β called instead ofprocwhen a trampoline is already in use.Tcl_ObjCmdProc2 *nreProc2(in) β called instead ofproc2when a trampoline is already in use.void *clientData(in) β arbitrary one-word value passed toproc,nreProc,deleteProcandobjProc.Tcl_CmdDeleteProc *deleteProc(in/out) β called beforecmdNameis deleted from the interpreter, allowing for command-specific cleanup. may beNULL.Tcl_Size objc(in) β number of items inobjv.Tcl_Obj **objv(in) β words in the command.Tcl_Obj *objPtr(in) β a script or expression to evaluate.int flags(in) β as described forTcl_EvalObjv.Tcl_Command cmd(in) β token to use instead of one derived from the first word ofobjvin order to evaluate a command.Tcl_Obj *resultPtr(out) β pointer to an unsharedTcl_Objwhere the result of the evaluation is stored if the return code isTCL_OK.Tcl_NRPostProc *postProcPtr(in) β a function to push.void *data0(in)void *data1(in)void *data2(in)void *data3(in) βdata0throughdata3are four one-word values that will be passed to the function designated bypostProcPtrwhen it is invoked.
π DESCRIPTION π
so these functions give you an interface to the function stack that an interpreter iterates through to evaluate commands. the routine behind a command is implemented by an initial function plus any additional functions that the routine pushes onto the stack as it progresses. the interpreter itself pushes functions onto the stack to react to the end of a routine and to exercise other forms of control β like switching between in-progress stacks and evaluating other scripts at additional levels β all without adding frames to the C stack. iconic.
to execute a routine, the initial function for the routine is called, and then a small bit of code called a trampoline iteratively takes functions off the stack and calls them, using the value of the last call as the value of the routine. π§
Tcl_NRCallObjProc calls nreProc using a new trampoline.
Tcl_NRCreateCommand, an alternative to Tcl_CreateObjCommand, resolves cmdName β which may contain namespace qualifiers β relative to the current namespace, creates a command by that name, and returns a token for the command which may be used in subsequent calls to Tcl_GetCommandName. except for a few cases noted below, any existing command by the same name is first deleted. and ooh, important: if interp is in the process of being deleted, Tcl_NRCreateCommand does not create any command, does not delete any command, and returns NULL.
Tcl_NRCreateCommand2 is an alternative to Tcl_NRCreateCommand in the same way as Tcl_CreateObjCommand2.
Tcl_NREvalObj pushes a function that is like Tcl_EvalObjEx but consumes no space on the C stack. slay.
Tcl_NREvalObjv pushes a function that is like Tcl_EvalObjv but consumes no space on the C stack.
Tcl_NRCmdSwap is like Tcl_NREvalObjv, but uses cmd β a token previously returned by Tcl_CreateObjCommand or Tcl_GetCommandFromObj β instead of resolving the first word of objv.
Tcl_NRExprObj pushes a function that evaluates objPtr as an expression in the same manner as Tcl_ExprObj but without consuming space on the C stack.
all of the functions return TCL_OK if the evaluation of the script, command, or expression has been scheduled successfully. otherwise (for example if the command name cannot be resolved), they return TCL_ERROR and store a message as the interpreter's result.
Tcl_NRAddCallback pushes postProcPtr. the signature for Tcl_NRPostProc is:
typedef int
Tcl_NRPostProc(
void * data[],
Tcl_Interp *interp,
int result);
data is a pointer to an array containing data0 through data3. result is the value returned by the previous function implementing part the routine.
π EXAMPLE π
bestie, watch this glow-up. the following command uses Tcl_EvalObjEx, which consumes space on the C stack, to evaluate a script:
int
TheCmdOldObjProc(
void *clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
int result;
Tcl_Obj *objPtr;
... preparation ...
result = Tcl_EvalObjEx(interp, objPtr, 0);
... postprocessing ...
return result;
}
Tcl_CreateObjCommand(interp, "theCommand",
TheCmdOldObjProc, clientData, TheCmdDeleteProc);
to avoid consuming space on the C stack, TheCmdOldObjProc is renamed to TheCmdNRObjProc and the postprocessing step is split into a separate function, TheCmdPostProc, which is pushed onto the function stack. Tcl_EvalObjEx is replaced with Tcl_NREvalObj, which uses a trampoline instead of consuming space on the C stack. a new version of TheCmdOldObjProc is just a a wrapper that uses Tcl_NRCallObjProc to call TheCmdNRObjProc:
int
TheCmdOldObjProc(
void *clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
return Tcl_NRCallObjProc(interp, TheCmdNRObjProc,
clientData, objc, objv);
}
int
TheCmdNRObjProc
void *clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
Tcl_Obj *objPtr;
... preparation ...
Tcl_NRAddCallback(interp, TheCmdPostProc,
data0, data1, data2, data3);
/* data0 .. data3 are up to four one-word items to
* pass to the postprocessing procedure */
return Tcl_NREvalObj(interp, objPtr, 0);
}
int
TheCmdNRPostProc(
void *data[],
Tcl_Interp *interp,
int result)
{
/* data[0] .. data[3] are the four words of data
* passed to Tcl_NRAddCallback */
... postprocessing ...
return result;
}
any function comprising a routine can push other functions, making it possible implement looping and sequencing constructs using the function stack. the recursion could never π
π REFERENCE COUNT MANAGEMENT π
the first objc values in the objv array passed to the functions Tcl_NRCallObjProc, Tcl_NREvalObjv, and Tcl_NRCmdSwap should have a reference count of at least 1; they may have additional references taken during the execution.
the objPtr argument to Tcl_NREvalObj and Tcl_NRExprObj should have a reference count of at least 1, and may have additional references taken to it during execution.
the resultObj argument to Tcl_NRExprObj should be an unshared object.
use Tcl_NRAddCallback to schedule any required final decrementing of the reference counts of arguments to any of the other functions on this page, as with any other post-processing step in the non-recursive execution engine.
The
yes, that trailing "The" is really there. we don't know what she was gonna say. she left us hanging π iconic behaviour honestly.
π SEE ALSO π
Tcl_CreateCommand(3), Tcl_CreateObjCommand(3), Tcl_EvalObjEx(3), Tcl_GetCommandFromObj(3), Tcl_ExprObj(3)
β KEYWORDS β
stackless, nonrecursive, execute, command, global, value, result, script
π«Ά COPYRIGHT π«Ά
Copyright Β© 2008 Kevin B. Kenny. Copyright Β© 2018 Nathan Coulter.
and that's the whole page, bestie!! π this one's from the Tcl manual, version 8.6, and it lives at NRE(3). go forth and be stackless, you absolute legend π¦β¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 2008 Kevin B. Kenny
- Copyright (c) 2018 Nathan Coulter
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms