Debian Codegraph @

doc/NRE.3

raw
1 .\"
2 .\" Copyright (c) 2008 Kevin B. Kenny.
3 .\" Copyright (c) 2018 Nathan Coulter.
4 .\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\"
8 .TH NRE 3 8.6 Tcl "Tcl Library Procedures"
9 .so man.macros
10 .BS
11 .SH NAME
12 Tcl_NRCreateCommand, Tcl_NRCallObjProc, Tcl_NREvalObj, Tcl_NREvalObjv, Tcl_NRCmdSwap, Tcl_NRExprObj, Tcl_NRAddCallback \- Non-Recursive (stackless) evaluation of Tcl scripts.
13 .SH SYNOPSIS
14 .nf
15 \fB#include <tcl.h>\fR
16 .sp
17 Tcl_Command
18 \fBTcl_NRCreateCommand\fR(\fIinterp, cmdName, proc, nreProc, clientData,
19 deleteProc\fR)
20 .sp
21 int
22 \fBTcl_NRCallObjProc\fR(\fIinterp, nreProc, clientData, objc, objv\fR)
23 .sp
24 int
25 \fBTcl_NREvalObj\fR(\fIinterp, objPtr, flags\fR)
26 .sp
27 int
28 \fBTcl_NREvalObjv\fR(\fIinterp, objc, objv, flags\fR)
29 .sp
30 int
31 \fBTcl_NRCmdSwap\fR(\fIinterp, cmd, objc, objv, flags\fR)
32 .sp
33 int
34 \fBTcl_NRExprObj\fR(\fIinterp, objPtr, resultPtr\fR)
35 .sp
36 void
37 \fBTcl_NRAddCallback\fR(\fIinterp, postProcPtr, data0, data1, data2, data3\fR)
38 .fi
39 .SH ARGUMENTS
40 .AS Tcl_CmdDeleteProc *interp in
41 .AP Tcl_Interp *interp in
42 The relevant Interpreter.
43 .AP "const char" *cmdName in
44 Name of the command to create.
45 .AP Tcl_ObjCmdProc *proc in
46 Called in order to evaluate a command. Is often just a small wrapper that uses
47 \fBTcl_NRCallObjProc\fR to call \fInreProc\fR using a new trampoline. Behaves
48 in the same way as the \fIproc\fR argument to \fBTcl_CreateObjCommand\fR(3)
49 (\fIq.v.\fR).
50 .AP Tcl_ObjCmdProc *nreProc in
51 Called instead of \fIproc\fR when a trampoline is already in use.
52 .AP ClientData clientData in
53 Arbitrary one-word value passed to \fIproc\fR, \fInreProc\fR, \fIdeleteProc\fR
54 and \fIobjProc\fR.
55 .AP Tcl_CmdDeleteProc *deleteProc in/out
56 Called before \fIcmdName\fR is deleted from the interpreter, allowing for
57 command-specific cleanup. May be NULL.
58 .AP int objc in
59 Number of items in \fIobjv\fR.
60 .AP Tcl_Obj **objv in
61 Words in the command.
62 .AP Tcl_Obj *objPtr in
63 A script or expression to evaluate.
64 .AP int flags in
65 As described for \fITcl_EvalObjv\fR.
66 .PP
67 .AP Tcl_Command cmd in
68 Token to use instead of one derived from the first word of \fIobjv\fR in order
69 to evaluate a command.
70 .AP Tcl_Obj *resultPtr out
71 Pointer to an unshared Tcl_Obj where the result of the evaluation is stored if
72 the return code is TCL_OK.
73 .AP Tcl_NRPostProc *postProcPtr in
74 A function to push.
75 .AP ClientData data0 in
76 .AP ClientData data1 in
77 .AP ClientData data2 in
78 .AP ClientData data3 in
79 \fIdata0\fR through \fIdata3\fR are four one-word values that will be passed
80 to the function designated by \fIpostProcPtr\fR when it is invoked.
81 .BE
82 .SH DESCRIPTION
83 .PP
84 These functions provide an interface to the function stack that an interpreter
85 iterates through to evaluate commands. The routine behind a command is
86 implemented by an initial function and any additional functions that the
87 routine pushes onto the stack as it progresses. The interpreter itself pushes
88 functions onto the stack to react to the end of a routine and to exercise other
89 forms of control such as switching between in-progress stacks and the
90 evaluation of other scripts at additional levels without adding frames to the C
91 stack. To execute a routine, the initial function for the routine is called
92 and then a small bit of code called a \fItrampoline\fR iteratively takes
93 functions off the stack and calls them, using the value of the last call as the
94 value of the routine.
95 .PP
96 \fBTcl_NRCallObjProc\fR calls \fInreProc\fR using a new trampoline.
97 .PP
98 \fBTcl_NRCreateCommand\fR, an alternative to \fBTcl_CreateObjCommand\fR,
99 resolves \fIcmdName\fR, which may contain namespace qualifiers, relative to the
100 current namespace, creates a command by that name, and returns a token for the
101 command which may be used in subsequent calls to \fBTcl_GetCommandName\fR.
102 Except for a few cases noted below any existing command by the same name is
103 first deleted. If \fIinterp\fR is in the process of being deleted
104 \fBTcl_NRCreateCommand\fR does not create any command, does not delete any
105 command, and returns NULL.
106 .PP
107 \fBTcl_NREvalObj\fR pushes a function that is like \fBTcl_EvalObjEx\fR but
108 consumes no space on the C stack.
109 .PP
110 \fBTcl_NREvalObjv\fR pushes a function that is like \fBTcl_EvalObjv\fR but
111 consumes no space on the C stack.
112 .PP
113 \fBTcl_NRCmdSwap\fR is like \fBTcl_NREvalObjv\fR, but uses \fIcmd\fR, a token
114 previously returned by \fBTcl_CreateObjCommand\fR or
115 \fBTcl_GetCommandFromObj\fR, instead of resolving the first word of \fIobjv\fR.
116 . The name of this command must be the same as \fIobjv[0]\fR.
117 .PP
118 \fBTcl_NRExprObj\fR pushes a function that evaluates \fIobjPtr\fR as an
119 expression in the same manner as \fBTcl_ExprObj\fR but without consuming space
120 on the C stack.
121 .PP
122 All of the functions return \fBTCL_OK\fR if the evaluation of the script,
123 command, or expression has been scheduled successfully. Otherwise (for example
124 if the command name cannot be resolved), they return \fBTCL_ERROR\fR and store
125 a message as the interpreter's result.
126 .PP
127 \fBTcl_NRAddCallback\fR pushes \fIpostProcPtr\fR. The signature for
128 \fBTcl_NRPostProc\fR is:
129 .PP
130 .CS
131 typedef int
132 \fBTcl_NRPostProc\fR(
133 \fBClientData\fR \fIdata\fR[],
134 \fBTcl_Interp\fR *\fIinterp\fR,
135 int \fIresult\fR);
136 .CE
137 .PP
138 \fIdata\fR is a pointer to an array containing \fIdata0\fR through \fIdata3\fR.
139 \fIresult\fR is the value returned by the previous function implementing part
140 the routine.
141 .SH EXAMPLE
142 .PP
143 The following command uses \fBTcl_EvalObjEx\fR, which consumes space on the C
144 stack, to evalute a script:
145 .PP
146 .CS
147 int
148 \fITheCmdOldObjProc\fR(
149 ClientData clientData,
150 Tcl_Interp *interp,
151 int objc,
152 Tcl_Obj *const objv[])
153 {
154 int result;
155 Tcl_Obj *objPtr;
156
157 \fI... preparation ...\fR
158
159 result = \fBTcl_EvalObjEx\fR(interp, objPtr, 0);
160
161 \fI... postprocessing ...\fR
162
163 return result;
164 }
165 \fBTcl_CreateObjCommand\fR(interp, "theCommand",
166 \fITheCmdOldObjProc\fR, clientData, TheCmdDeleteProc);
167 .CE
168 .PP
169 To avoid consuming space on the C stack, \fITheCmdOldObjProc\fR is renamed to
170 \fITheCmdNRObjProc\fR and the postprocessing step is split into a separate
171 function, \fITheCmdPostProc\fR, which is pushed onto the function stack.
172 \fITcl_EvalObjEx\fR is replaced with \fITcl_NREvalObj\fR, which uses a
173 trampoline instead of consuming space on the C stack. A new version of
174 \fITheCmdOldObjProc\fR is just a a wrapper that uses \fBTcl_NRCallObjProc\fR to
175 call \fITheCmdNRObjProc\fR:
176 .PP
177 .CS
178 int
179 \fITheCmdOldObjProc\fR(
180 ClientData clientData,
181 Tcl_Interp *interp,
182 int objc,
183 Tcl_Obj *const objv[])
184 {
185 return \fBTcl_NRCallObjProc\fR(interp, \fITheCmdNRObjProc\fR,
186 clientData, objc, objv);
187 }
188 .CE
189 .PP
190 .CS
191 int
192 \fITheCmdNRObjProc\fR
193 ClientData clientData,
194 Tcl_Interp *interp,
195 int objc,
196 Tcl_Obj *const objv[])
197 {
198 Tcl_Obj *objPtr;
199
200 \fI... preparation ...\fR
201
202 \fBTcl_NRAddCallback\fR(interp, \fITheCmdPostProc\fR,
203 data0, data1, data2, data3);
204 /* \fIdata0 .. data3\fR are up to four one-word items to
205 * pass to the postprocessing procedure */
206
207 return \fBTcl_NREvalObj\fR(interp, objPtr, 0);
208 }
209 .CE
210 .PP
211 .CS
212 int
213 \fITheCmdNRPostProc\fR(
214 ClientData data[],
215 Tcl_Interp *interp,
216 int result)
217 {
218 /* \fIdata[0] .. data[3]\fR are the four words of data
219 * passed to \fBTcl_NRAddCallback\fR */
220
221 \fI... postprocessing ...\fR
222
223 return result;
224 }
225 .CE
226 .PP
227 Any function comprising a routine can push other functions, making it possible
228 implement looping and sequencing constructs using the function stack.
229 .PP
230 .SH "SEE ALSO"
231 Tcl_CreateCommand(3), Tcl_CreateObjCommand(3), Tcl_EvalObjEx(3), Tcl_GetCommandFromObj(3), Tcl_ExprObj(3)
232 .SH KEYWORDS
233 stackless, nonrecursive, execute, command, global, value, result, script
234 .SH COPYRIGHT
235 Copyright \(co 2008 Kevin B. Kenny.
236 Copyright \(co 2018 Nathan Coulter.