Debian Codegraph @

pTk/mTk/tclGeneric/tclProc.c

raw symbols SCIP
1 /*
2 * tclProc.c --
3 *
4 * This file contains routines that implement Tcl procedures,
5 * including the "proc" and "uplevel" commands.
6 *
7 * Copyright (c) 1987-1993 The Regents of the University of California.
8 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tclProc.c,v 1.44.2.1 2003/07/18 23:35:39 dgp Exp $
14 */
15
16 #include "tclInt.h"
17 #include "tclCompile.h"
18
19 /*
20 * Prototypes for static functions in this file
21 */
22
23 static void ProcBodyDup _ANSI_ARGS_((Tcl_Obj *srcPtr, Tcl_Obj *dupPtr));
24 static void ProcBodyFree _ANSI_ARGS_((Tcl_Obj *objPtr));
25 static int ProcBodySetFromAny _ANSI_ARGS_((Tcl_Interp *interp,
26 Tcl_Obj *objPtr));
27 static void ProcBodyUpdateString _ANSI_ARGS_((Tcl_Obj *objPtr));
28 static int ProcessProcResultCode _ANSI_ARGS_((Tcl_Interp *interp,
29 char *procName, int nameLen, int returnCode));
30 static int TclCompileNoOp _ANSI_ARGS_((Tcl_Interp *interp,
31 Tcl_Parse *parsePtr, struct CompileEnv *envPtr));
32
33 /*
34 * The ProcBodyObjType type
35 */
36
37 Tcl_ObjType tclProcBodyType = {
38 "procbody", /* name for this type */
39 ProcBodyFree, /* FreeInternalRep procedure */
40 ProcBodyDup, /* DupInternalRep procedure */
41 ProcBodyUpdateString, /* UpdateString procedure */
42 ProcBodySetFromAny /* SetFromAny procedure */
43 };
44
45 /*
46 *----------------------------------------------------------------------
47 *
48 * Tcl_ProcObjCmd --
49 *
50 * This object-based procedure is invoked to process the "proc" Tcl
51 * command. See the user documentation for details on what it does.
52 *
53 * Results:
54 * A standard Tcl object result value.
55 *
56 * Side effects:
57 * A new procedure gets created.
58 *
59 *----------------------------------------------------------------------
60 */
61
62 /* ARGSUSED */
63 int
64 Tcl_ProcObjCmd(dummy, interp, objc, objv)
65 ClientData dummy; /* Not used. */
66 Tcl_Interp *interp; /* Current interpreter. */
67 int objc; /* Number of arguments. */
68 Tcl_Obj *CONST objv[]; /* Argument objects. */
69 {
70 register Interp *iPtr = (Interp *) interp;
71 Proc *procPtr;
72 char *fullName;
73 CONST char *procName, *procArgs, *procBody;
74 Namespace *nsPtr, *altNsPtr, *cxtNsPtr;
75 Tcl_Command cmd;
76 Tcl_DString ds;
77
78 if (objc != 4) {
79 Tcl_WrongNumArgs(interp, 1, objv, "name args body");
80 return TCL_ERROR;
81 }
82
83 /*
84 * Determine the namespace where the procedure should reside. Unless
85 * the command name includes namespace qualifiers, this will be the
86 * current namespace.
87 */
88
89 fullName = TclGetString(objv[1]);
90 TclGetNamespaceForQualName(interp, fullName, (Namespace *) NULL,
91 0, &nsPtr, &altNsPtr, &cxtNsPtr, &procName);
92
93 if (nsPtr == NULL) {
94 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
95 "can't create procedure \"", fullName,
96 "\": unknown namespace", (char *) NULL);
97 return TCL_ERROR;
98 }
99 if (procName == NULL) {
100 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
101 "can't create procedure \"", fullName,
102 "\": bad procedure name", (char *) NULL);
103 return TCL_ERROR;
104 }
105 if ((nsPtr != iPtr->globalNsPtr)
106 && (procName != NULL) && (procName[0] == ':')) {
107 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
108 "can't create procedure \"", procName,
109 "\" in non-global namespace with name starting with \":\"",
110 (char *) NULL);
111 return TCL_ERROR;
112 }
113
114 /*
115 * Create the data structure to represent the procedure.
116 */
117 if (TclCreateProc(interp, nsPtr, procName, objv[2], objv[3],
118 &procPtr) != TCL_OK) {
119 return TCL_ERROR;
120 }
121
122 /*
123 * Now create a command for the procedure. This will initially be in
124 * the current namespace unless the procedure's name included namespace
125 * qualifiers. To create the new command in the right namespace, we
126 * generate a fully qualified name for it.
127 */
128
129 Tcl_DStringInit(&ds);
130 if (nsPtr != iPtr->globalNsPtr) {
131 Tcl_DStringAppend(&ds, nsPtr->fullName, -1);
132 Tcl_DStringAppend(&ds, "::", 2);
133 }
134 Tcl_DStringAppend(&ds, procName, -1);
135
136 Tcl_CreateCommand(interp, Tcl_DStringValue(&ds), TclProcInterpProc,
137 (ClientData) procPtr, TclProcDeleteProc);
138 cmd = Tcl_CreateObjCommand(interp, Tcl_DStringValue(&ds),
139 TclObjInterpProc, (ClientData) procPtr, TclProcDeleteProc);
140
141 Tcl_DStringFree(&ds);
142 /*
143 * Now initialize the new procedure's cmdPtr field. This will be used
144 * later when the procedure is called to determine what namespace the
145 * procedure will run in. This will be different than the current
146 * namespace if the proc was renamed into a different namespace.
147 */
148
149 procPtr->cmdPtr = (Command *) cmd;
150
151
152 /*
153 * Optimize for noop procs: if the body is not precompiled (like a TclPro
154 * procbody), and the argument list is just "args" and the body is empty,
155 * define a compileProc to compile a noop.
156 *
157 * Notes:
158 * - cannot be done for any argument list without having different
159 * compiled/not-compiled behaviour in the "wrong argument #" case,
160 * or making this code much more complicated. In any case, it doesn't
161 * seem to make a lot of sense to verify the number of arguments we
162 * are about to ignore ...
163 * - could be enhanced to handle also non-empty bodies that contain
164 * only comments; however, parsing the body will slow down the
165 * compilation of all procs whose argument list is just _args_ */
166
167 if (objv[3]->typePtr == &tclProcBodyType) {
168 goto done;
169 }
170
171 procArgs = Tcl_GetString(objv[2]);
172
173 while (*procArgs == ' ') {
174 procArgs++;
175 }
176
177 if ((procArgs[0] == 'a') && (strncmp(procArgs, "args", 4) == 0)) {
178 procArgs +=4;
179 while(*procArgs != '\0') {
180 if (*procArgs != ' ') {
181 goto done;
182 }
183 procArgs++;
184 }
185
186 /*
187 * The argument list is just "args"; check the body
188 */
189
190 procBody = Tcl_GetString(objv[3]);
191 while (*procBody != '\0') {
192 if (!isspace(UCHAR(*procBody))) {
193 goto done;
194 }
195 procBody++;
196 }
197
198 /*
199 * The body is just spaces: link the compileProc
200 */
201
202 ((Command *) cmd)->compileProc = TclCompileNoOp;
203 }
204
205 done:
206 return TCL_OK;
207 }
208
209 /*
210 *----------------------------------------------------------------------
211 *
212 * TclCreateProc --
213 *
214 * Creates the data associated with a Tcl procedure definition.
215 * This procedure knows how to handle two types of body objects:
216 * strings and procbody. Strings are the traditional (and common) value
217 * for bodies, procbody are values created by extensions that have
218 * loaded a previously compiled script.
219 *
220 * Results:
221 * Returns TCL_OK on success, along with a pointer to a Tcl
222 * procedure definition in procPtrPtr. This definition should
223 * be freed by calling TclCleanupProc() when it is no longer
224 * needed. Returns TCL_ERROR if anything goes wrong.
225 *
226 * Side effects:
227 * If anything goes wrong, this procedure returns an error
228 * message in the interpreter.
229 *
230 *----------------------------------------------------------------------
231 */
232 int
233 TclCreateProc(interp, nsPtr, procName, argsPtr, bodyPtr, procPtrPtr)
234 Tcl_Interp *interp; /* interpreter containing proc */
235 Namespace *nsPtr; /* namespace containing this proc */
236 CONST char *procName; /* unqualified name of this proc */
237 Tcl_Obj *argsPtr; /* description of arguments */
238 Tcl_Obj *bodyPtr; /* command body */
239 Proc **procPtrPtr; /* returns: pointer to proc data */
240 {
241 Interp *iPtr = (Interp*)interp;
242 CONST char **argArray = NULL;
243
244 register Proc *procPtr;
245 int i, length, result, numArgs;
246 CONST char *args, *bytes, *p;
247 register CompiledLocal *localPtr = NULL;
248 Tcl_Obj *defPtr;
249 int precompiled = 0;
250
251 if (bodyPtr->typePtr == &tclProcBodyType) {
252 /*
253 * Because the body is a TclProProcBody, the actual body is already
254 * compiled, and it is not shared with anyone else, so it's OK not to
255 * unshare it (as a matter of fact, it is bad to unshare it, because
256 * there may be no source code).
257 *
258 * We don't create and initialize a Proc structure for the procedure;
259 * rather, we use what is in the body object. Note that
260 * we initialize its cmdPtr field below after we've created the command
261 * for the procedure. We increment the ref count of the Proc struct
262 * since the command (soon to be created) will be holding a reference
263 * to it.
264 */
265
266 procPtr = (Proc *) bodyPtr->internalRep.otherValuePtr;
267 procPtr->iPtr = iPtr;
268 procPtr->refCount++;
269 precompiled = 1;
270 } else {
271 /*
272 * If the procedure's body object is shared because its string value is
273 * identical to, e.g., the body of another procedure, we must create a
274 * private copy for this procedure to use. Such sharing of procedure
275 * bodies is rare but can cause problems. A procedure body is compiled
276 * in a context that includes the number of compiler-allocated "slots"
277 * for local variables. Each formal parameter is given a local variable
278 * slot (the "procPtr->numCompiledLocals = numArgs" assignment
279 * below). This means that the same code can not be shared by two
280 * procedures that have a different number of arguments, even if their
281 * bodies are identical. Note that we don't use Tcl_DuplicateObj since
282 * we would not want any bytecode internal representation.
283 */
284
285 if (Tcl_IsShared(bodyPtr)) {
286 bytes = Tcl_GetStringFromObj(bodyPtr, &length);
287 bodyPtr = Tcl_NewStringObj(bytes, length);
288 }
289
290 /*
291 * Create and initialize a Proc structure for the procedure. Note that
292 * we initialize its cmdPtr field below after we've created the command
293 * for the procedure. We increment the ref count of the procedure's
294 * body object since there will be a reference to it in the Proc
295 * structure.
296 */
297
298 Tcl_IncrRefCount(bodyPtr);
299
300 procPtr = (Proc *) ckalloc(sizeof(Proc));
301 procPtr->iPtr = iPtr;
302 procPtr->refCount = 1;
303 procPtr->bodyPtr = bodyPtr;
304 procPtr->numArgs = 0; /* actual argument count is set below. */
305 procPtr->numCompiledLocals = 0;
306 procPtr->firstLocalPtr = NULL;
307 procPtr->lastLocalPtr = NULL;
308 }
309
310 /*
311 * Break up the argument list into argument specifiers, then process
312 * each argument specifier.
313 * If the body is precompiled, processing is limited to checking that
314 * the the parsed argument is consistent with the one stored in the
315 * Proc.
316 * THIS FAILS IF THE ARG LIST OBJECT'S STRING REP CONTAINS NULLS.
317 */
318
319 args = Tcl_GetStringFromObj(argsPtr, &length);
320 result = Tcl_SplitList(interp, args, &numArgs, &argArray);
321 if (result != TCL_OK) {
322 goto procError;
323 }
324
325 if (precompiled) {
326 if (numArgs > procPtr->numArgs) {
327 char buf[64 + TCL_INTEGER_SPACE + TCL_INTEGER_SPACE];
328 sprintf(buf, "\": arg list contains %d entries, precompiled header expects %d",
329 numArgs, procPtr->numArgs);
330 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
331 "procedure \"", procName,
332 buf, (char *) NULL);
333 goto procError;
334 }
335 localPtr = procPtr->firstLocalPtr;
336 } else {
337 procPtr->numArgs = numArgs;
338 procPtr->numCompiledLocals = numArgs;
339 }
340 for (i = 0; i < numArgs; i++) {
341 int fieldCount, nameLength, valueLength;
342 CONST char **fieldValues;
343
344 /*
345 * Now divide the specifier up into name and default.
346 */
347
348 result = Tcl_SplitList(interp, argArray[i], &fieldCount,
349 &fieldValues);
350 if (result != TCL_OK) {
351 goto procError;
352 }
353 if (fieldCount > 2) {
354 ckfree((char *) fieldValues);
355 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
356 "too many fields in argument specifier \"",
357 argArray[i], "\"", (char *) NULL);
358 goto procError;
359 }
360 if ((fieldCount == 0) || (*fieldValues[0] == 0)) {
361 ckfree((char *) fieldValues);
362 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
363 "procedure \"", procName,
364 "\" has argument with no name", (char *) NULL);
365 goto procError;
366 }
367
368 nameLength = strlen(fieldValues[0]);
369 if (fieldCount == 2) {
370 valueLength = strlen(fieldValues[1]);
371 } else {
372 valueLength = 0;
373 }
374
375 /*
376 * Check that the formal parameter name is a scalar.
377 */
378
379 p = fieldValues[0];
380 while (*p != '\0') {
381 if (*p == '(') {
382 CONST char *q = p;
383 do {
384 q++;
385 } while (*q != '\0');
386 q--;
387 if (*q == ')') { /* we have an array element */
388 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
389 "procedure \"", procName,
390 "\" has formal parameter \"", fieldValues[0],
391 "\" that is an array element",
392 (char *) NULL);
393 ckfree((char *) fieldValues);
394 goto procError;
395 }
396 } else if ((*p == ':') && (*(p+1) == ':')) {
397 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
398 "procedure \"", procName,
399 "\" has formal parameter \"", fieldValues[0],
400 "\" that is not a simple name",
401 (char *) NULL);
402 ckfree((char *) fieldValues);
403 goto procError;
404 }
405 p++;
406 }
407
408 if (precompiled) {
409 /*
410 * Compare the parsed argument with the stored one.
411 * For the flags, we and out VAR_UNDEFINED to support bridging
412 * precompiled <= 8.3 code in 8.4 where this is now used as an
413 * optimization indicator. Yes, this is a hack. -- hobbs
414 */
415
416 if ((localPtr->nameLength != nameLength)
417 || (strcmp(localPtr->name, fieldValues[0]))
418 || (localPtr->frameIndex != i)
419 || ((localPtr->flags & ~VAR_UNDEFINED)
420 != (VAR_SCALAR | VAR_ARGUMENT))
421 || ((localPtr->defValuePtr == NULL)
422 && (fieldCount == 2))
423 || ((localPtr->defValuePtr != NULL)
424 && (fieldCount != 2))) {
425 char buf[80 + TCL_INTEGER_SPACE];
426 sprintf(buf, "\": formal parameter %d is inconsistent with precompiled body",
427 i);
428 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
429 "procedure \"", procName,
430 buf, (char *) NULL);
431 ckfree((char *) fieldValues);
432 goto procError;
433 }
434
435 /*
436 * compare the default value if any
437 */
438
439 if (localPtr->defValuePtr != NULL) {
440 int tmpLength;
441 char *tmpPtr = Tcl_GetStringFromObj(localPtr->defValuePtr,
442 &tmpLength);
443 if ((valueLength != tmpLength)
444 || (strncmp(fieldValues[1], tmpPtr,
445 (size_t) tmpLength))) {
446 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
447 "procedure \"", procName,
448 "\": formal parameter \"",
449 fieldValues[0],
450 "\" has default value inconsistent with precompiled body",
451 (char *) NULL);
452 ckfree((char *) fieldValues);
453 goto procError;
454 }
455 }
456
457 localPtr = localPtr->nextPtr;
458 } else {
459 /*
460 * Allocate an entry in the runtime procedure frame's array of
461 * local variables for the argument.
462 */
463
464 localPtr = (CompiledLocal *) ckalloc((unsigned)
465 (sizeof(CompiledLocal) - sizeof(localPtr->name)
466 + nameLength+1));
467 if (procPtr->firstLocalPtr == NULL) {
468 procPtr->firstLocalPtr = procPtr->lastLocalPtr = localPtr;
469 } else {
470 procPtr->lastLocalPtr->nextPtr = localPtr;
471 procPtr->lastLocalPtr = localPtr;
472 }
473 localPtr->nextPtr = NULL;
474 localPtr->nameLength = nameLength;
475 localPtr->frameIndex = i;
476 localPtr->flags = VAR_SCALAR | VAR_ARGUMENT;
477 localPtr->resolveInfo = NULL;
478
479 if (fieldCount == 2) {
480 localPtr->defValuePtr =
481 Tcl_NewStringObj(fieldValues[1], valueLength);
482 Tcl_IncrRefCount(localPtr->defValuePtr);
483 } else {
484 localPtr->defValuePtr = NULL;
485 }
486 strcpy(localPtr->name, fieldValues[0]);
487 }
488
489 ckfree((char *) fieldValues);
490 }
491
492 /*
493 * Now initialize the new procedure's cmdPtr field. This will be used
494 * later when the procedure is called to determine what namespace the
495 * procedure will run in. This will be different than the current
496 * namespace if the proc was renamed into a different namespace.
497 */
498
499 *procPtrPtr = procPtr;
500 ckfree((char *) argArray);
501 return TCL_OK;
502
503 procError:
504 if (precompiled) {
505 procPtr->refCount--;
506 } else {
507 Tcl_DecrRefCount(bodyPtr);
508 while (procPtr->firstLocalPtr != NULL) {
509 localPtr = procPtr->firstLocalPtr;
510 procPtr->firstLocalPtr = localPtr->nextPtr;
511
512 defPtr = localPtr->defValuePtr;
513 if (defPtr != NULL) {
514 Tcl_DecrRefCount(defPtr);
515 }
516
517 ckfree((char *) localPtr);
518 }
519 ckfree((char *) procPtr);
520 }
521 if (argArray != NULL) {
522 ckfree((char *) argArray);
523 }
524 return TCL_ERROR;
525 }
526
527 /*
528 *----------------------------------------------------------------------
529 *
530 * TclGetFrame --
531 *
532 * Given a description of a procedure frame, such as the first
533 * argument to an "uplevel" or "upvar" command, locate the
534 * call frame for the appropriate level of procedure.
535 *
536 * Results:
537 * The return value is -1 if an error occurred in finding the frame
538 * (in this case an error message is left in the interp's result).
539 * 1 is returned if string was either a number or a number preceded
540 * by "#" and it specified a valid frame. 0 is returned if string
541 * isn't one of the two things above (in this case, the lookup
542 * acts as if string were "1"). The variable pointed to by
543 * framePtrPtr is filled in with the address of the desired frame
544 * (unless an error occurs, in which case it isn't modified).
545 *
546 * Side effects:
547 * None.
548 *
549 *----------------------------------------------------------------------
550 */
551
552 int
553 TclGetFrame(interp, string, framePtrPtr)
554 Tcl_Interp *interp; /* Interpreter in which to find frame. */
555 CONST char *string; /* String describing frame. */
556 CallFrame **framePtrPtr; /* Store pointer to frame here (or NULL
557 * if global frame indicated). */
558 {
559 register Interp *iPtr = (Interp *) interp;
560 int curLevel, level, result;
561 CallFrame *framePtr;
562
563 /*
564 * Parse string to figure out which level number to go to.
565 */
566
567 result = 1;
568 curLevel = (iPtr->varFramePtr == NULL) ? 0 : iPtr->varFramePtr->level;
569 if (*string == '#') {
570 if (Tcl_GetInt(interp, string+1, &level) != TCL_OK) {
571 return -1;
572 }
573 if (level < 0) {
574 levelError:
575 Tcl_AppendResult(interp, "bad level \"", string, "\"",
576 (char *) NULL);
577 return -1;
578 }
579 } else if (isdigit(UCHAR(*string))) { /* INTL: digit */
580 if (Tcl_GetInt(interp, string, &level) != TCL_OK) {
581 return -1;
582 }
583 level = curLevel - level;
584 } else {
585 level = curLevel - 1;
586 result = 0;
587 }
588
589 /*
590 * Figure out which frame to use, and modify the interpreter so
591 * its variables come from that frame.
592 */
593
594 if (level == 0) {
595 framePtr = NULL;
596 } else {
597 for (framePtr = iPtr->varFramePtr; framePtr != NULL;
598 framePtr = framePtr->callerVarPtr) {
599 if (framePtr->level == level) {
600 break;
601 }
602 }
603 if (framePtr == NULL) {
604 goto levelError;
605 }
606 }
607 *framePtrPtr = framePtr;
608 return result;
609 }
610
611 /*
612 *----------------------------------------------------------------------
613 *
614 * Tcl_UplevelObjCmd --
615 *
616 * This object procedure is invoked to process the "uplevel" Tcl
617 * command. See the user documentation for details on what it does.
618 *
619 * Results:
620 * A standard Tcl object result value.
621 *
622 * Side effects:
623 * See the user documentation.
624 *
625 *----------------------------------------------------------------------
626 */
627
628 /* ARGSUSED */
629 int
630 Tcl_UplevelObjCmd(dummy, interp, objc, objv)
631 ClientData dummy; /* Not used. */
632 Tcl_Interp *interp; /* Current interpreter. */
633 int objc; /* Number of arguments. */
634 Tcl_Obj *CONST objv[]; /* Argument objects. */
635 {
636 register Interp *iPtr = (Interp *) interp;
637 char *optLevel;
638 int result;
639 CallFrame *savedVarFramePtr, *framePtr;
640
641 if (objc < 2) {
642 uplevelSyntax:
643 Tcl_WrongNumArgs(interp, 1, objv, "?level? command ?arg ...?");
644 return TCL_ERROR;
645 }
646
647 /*
648 * Find the level to use for executing the command.
649 */
650
651 optLevel = TclGetString(objv[1]);
652 result = TclGetFrame(interp, optLevel, &framePtr);
653 if (result == -1) {
654 return TCL_ERROR;
655 }
656 objc -= (result+1);
657 if (objc == 0) {
658 goto uplevelSyntax;
659 }
660 objv += (result+1);
661
662 /*
663 * Modify the interpreter state to execute in the given frame.
664 */
665
666 savedVarFramePtr = iPtr->varFramePtr;
667 iPtr->varFramePtr = framePtr;
668
669 /*
670 * Execute the residual arguments as a command.
671 */
672
673 if (objc == 1) {
674 result = Tcl_EvalObjEx(interp, objv[0], TCL_EVAL_DIRECT);
675 } else {
676 /*
677 * More than one argument: concatenate them together with spaces
678 * between, then evaluate the result. Tcl_EvalObjEx will delete
679 * the object when it decrements its refcount after eval'ing it.
680 */
681 Tcl_Obj *objPtr;
682
683 objPtr = Tcl_ConcatObj(objc, objv);
684 result = Tcl_EvalObjEx(interp, objPtr, TCL_EVAL_DIRECT);
685 }
686 if (result == TCL_ERROR) {
687 char msg[32 + TCL_INTEGER_SPACE];
688 sprintf(msg, "\n (\"uplevel\" body line %d)", interp->errorLine);
689 Tcl_AddObjErrorInfo(interp, msg, -1);
690 }
691
692 /*
693 * Restore the variable frame, and return.
694 */
695
696 iPtr->varFramePtr = savedVarFramePtr;
697 return result;
698 }
699
700 /*
701 *----------------------------------------------------------------------
702 *
703 * TclFindProc --
704 *
705 * Given the name of a procedure, return a pointer to the
706 * record describing the procedure. The procedure will be
707 * looked up using the usual rules: first in the current
708 * namespace and then in the global namespace.
709 *
710 * Results:
711 * NULL is returned if the name doesn't correspond to any
712 * procedure. Otherwise, the return value is a pointer to
713 * the procedure's record. If the name is found but refers
714 * to an imported command that points to a "real" procedure
715 * defined in another namespace, a pointer to that "real"
716 * procedure's structure is returned.
717 *
718 * Side effects:
719 * None.
720 *
721 *----------------------------------------------------------------------
722 */
723
724 Proc *
725 TclFindProc(iPtr, procName)
726 Interp *iPtr; /* Interpreter in which to look. */
727 CONST char *procName; /* Name of desired procedure. */
728 {
729 Tcl_Command cmd;
730 Tcl_Command origCmd;
731 Command *cmdPtr;
732
733 cmd = Tcl_FindCommand((Tcl_Interp *) iPtr, procName,
734 (Tcl_Namespace *) NULL, /*flags*/ 0);
735 if (cmd == (Tcl_Command) NULL) {
736 return NULL;
737 }
738 cmdPtr = (Command *) cmd;
739
740 origCmd = TclGetOriginalCommand(cmd);
741 if (origCmd != NULL) {
742 cmdPtr = (Command *) origCmd;
743 }
744 if (cmdPtr->proc != TclProcInterpProc) {
745 return NULL;
746 }
747 return (Proc *) cmdPtr->clientData;
748 }
749
750 /*
751 *----------------------------------------------------------------------
752 *
753 * TclIsProc --
754 *
755 * Tells whether a command is a Tcl procedure or not.
756 *
757 * Results:
758 * If the given command is actually a Tcl procedure, the
759 * return value is the address of the record describing
760 * the procedure. Otherwise the return value is 0.
761 *
762 * Side effects:
763 * None.
764 *
765 *----------------------------------------------------------------------
766 */
767
768 Proc *
769 TclIsProc(cmdPtr)
770 Command *cmdPtr; /* Command to test. */
771 {
772 Tcl_Command origCmd;
773
774 origCmd = TclGetOriginalCommand((Tcl_Command) cmdPtr);
775 if (origCmd != NULL) {
776 cmdPtr = (Command *) origCmd;
777 }
778 if (cmdPtr->proc == TclProcInterpProc) {
779 return (Proc *) cmdPtr->clientData;
780 }
781 return (Proc *) 0;
782 }
783
784 /*
785 *----------------------------------------------------------------------
786 *
787 * TclProcInterpProc --
788 *
789 * When a Tcl procedure gets invoked with an argc/argv array of
790 * strings, this routine gets invoked to interpret the procedure.
791 *
792 * Results:
793 * A standard Tcl result value, usually TCL_OK.
794 *
795 * Side effects:
796 * Depends on the commands in the procedure.
797 *
798 *----------------------------------------------------------------------
799 */
800
801 int
802 TclProcInterpProc(clientData, interp, argc, argv)
803 ClientData clientData; /* Record describing procedure to be
804 * interpreted. */
805 Tcl_Interp *interp; /* Interpreter in which procedure was
806 * invoked. */
807 int argc; /* Count of number of arguments to this
808 * procedure. */
809 register CONST char **argv; /* Argument values. */
810 {
811 register Tcl_Obj *objPtr;
812 register int i;
813 int result;
814
815 /*
816 * This procedure generates an objv array for object arguments that hold
817 * the argv strings. It starts out with stack-allocated space but uses
818 * dynamically-allocated storage if needed.
819 */
820
821 #define NUM_ARGS 20
822 Tcl_Obj *(objStorage[NUM_ARGS]);
823 register Tcl_Obj **objv = objStorage;
824
825 /*
826 * Create the object argument array "objv". Make sure objv is large
827 * enough to hold the objc arguments plus 1 extra for the zero
828 * end-of-objv word.
829 */
830
831 if ((argc + 1) > NUM_ARGS) {
832 objv = (Tcl_Obj **)
833 ckalloc((unsigned)(argc + 1) * sizeof(Tcl_Obj *));
834 }
835
836 for (i = 0; i < argc; i++) {
837 objv[i] = Tcl_NewStringObj(argv[i], -1);
838 Tcl_IncrRefCount(objv[i]);
839 }
840 objv[argc] = 0;
841
842 /*
843 * Use TclObjInterpProc to actually interpret the procedure.
844 */
845
846 result = TclObjInterpProc(clientData, interp, argc, objv);
847
848 /*
849 * Move the interpreter's object result to the string result,
850 * then reset the object result.
851 */
852
853 Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
854 TCL_VOLATILE);
855
856 /*
857 * Decrement the ref counts on the objv elements since we are done
858 * with them.
859 */
860
861 for (i = 0; i < argc; i++) {
862 objPtr = objv[i];
863 TclDecrRefCount(objPtr);
864 }
865
866 /*
867 * Free the objv array if malloc'ed storage was used.
868 */
869
870 if (objv != objStorage) {
871 ckfree((char *) objv);
872 }
873 return result;
874 #undef NUM_ARGS
875 }
876
877 /*
878 *----------------------------------------------------------------------
879 *
880 * TclObjInterpProc --
881 *
882 * When a Tcl procedure gets invoked during bytecode evaluation, this
883 * object-based routine gets invoked to interpret the procedure.
884 *
885 * Results:
886 * A standard Tcl object result value.
887 *
888 * Side effects:
889 * Depends on the commands in the procedure.
890 *
891 *----------------------------------------------------------------------
892 */
893
894 int
895 TclObjInterpProc(clientData, interp, objc, objv)
896 ClientData clientData; /* Record describing procedure to be
897 * interpreted. */
898 register Tcl_Interp *interp; /* Interpreter in which procedure was
899 * invoked. */
900 int objc; /* Count of number of arguments to this
901 * procedure. */
902 Tcl_Obj *CONST objv[]; /* Argument value objects. */
903 {
904 Interp *iPtr = (Interp *) interp;
905 register Proc *procPtr = (Proc *) clientData;
906 Namespace *nsPtr = procPtr->cmdPtr->nsPtr;
907 CallFrame frame;
908 register CallFrame *framePtr = &frame;
909 register Var *varPtr;
910 register CompiledLocal *localPtr;
911 char *procName;
912 int nameLen, localCt, numArgs, argCt, i, result;
913 Tcl_Obj *objResult = Tcl_GetObjResult(interp);
914
915 /*
916 * This procedure generates an array "compiledLocals" that holds the
917 * storage for local variables. It starts out with stack-allocated space
918 * but uses dynamically-allocated storage if needed.
919 */
920
921 #define NUM_LOCALS 20
922 Var localStorage[NUM_LOCALS];
923 Var *compiledLocals = localStorage;
924
925 /*
926 * Get the procedure's name.
927 */
928
929 procName = Tcl_GetStringFromObj(objv[0], &nameLen);
930
931 /*
932 * If necessary, compile the procedure's body. The compiler will
933 * allocate frame slots for the procedure's non-argument local
934 * variables. Note that compiling the body might increase
935 * procPtr->numCompiledLocals if new local variables are found
936 * while compiling.
937 */
938
939 result = TclProcCompileProc(interp, procPtr, procPtr->bodyPtr, nsPtr,
940 "body of proc", procName);
941
942 if (result != TCL_OK) {
943 return result;
944 }
945
946 /*
947 * Create the "compiledLocals" array. Make sure it is large enough to
948 * hold all the procedure's compiled local variables, including its
949 * formal parameters.
950 */
951
952 localCt = procPtr->numCompiledLocals;
953 if (localCt > NUM_LOCALS) {
954 compiledLocals = (Var *) ckalloc((unsigned) localCt * sizeof(Var));
955 }
956
957 /*
958 * Set up and push a new call frame for the new procedure invocation.
959 * This call frame will execute in the proc's namespace, which might
960 * be different than the current namespace. The proc's namespace is
961 * that of its command, which can change if the command is renamed
962 * from one namespace to another.
963 */
964
965 result = Tcl_PushCallFrame(interp, (Tcl_CallFrame *) framePtr,
966 (Tcl_Namespace *) nsPtr, /*isProcCallFrame*/ 1);
967
968 if (result != TCL_OK) {
969 return result;
970 }
971
972 framePtr->objc = objc;
973 framePtr->objv = objv; /* ref counts for args are incremented below */
974
975 /*
976 * Initialize and resolve compiled variable references.
977 */
978
979 framePtr->procPtr = procPtr;
980 framePtr->numCompiledLocals = localCt;
981 framePtr->compiledLocals = compiledLocals;
982
983 TclInitCompiledLocals(interp, framePtr, nsPtr);
984
985 /*
986 * Match and assign the call's actual parameters to the procedure's
987 * formal arguments. The formal arguments are described by the first
988 * numArgs entries in both the Proc structure's local variable list and
989 * the call frame's local variable array.
990 */
991
992 numArgs = procPtr->numArgs;
993 varPtr = framePtr->compiledLocals;
994 localPtr = procPtr->firstLocalPtr;
995 argCt = objc;
996 for (i = 1, argCt -= 1; i <= numArgs; i++, argCt--) {
997 if (!TclIsVarArgument(localPtr)) {
998 panic("TclObjInterpProc: local variable %s is not argument but should be",
999 localPtr->name);
1000 return TCL_ERROR;
1001 }
1002 if (TclIsVarTemporary(localPtr)) {
1003 panic("TclObjInterpProc: local variable %d is temporary but should be an argument", i);
1004 return TCL_ERROR;
1005 }
1006
1007 /*
1008 * Handle the special case of the last formal being "args". When
1009 * it occurs, assign it a list consisting of all the remaining
1010 * actual arguments.
1011 */
1012
1013 if ((i == numArgs) && ((localPtr->name[0] == 'a')
1014 && (strcmp(localPtr->name, "args") == 0))) {
1015 Tcl_Obj *listPtr = Tcl_NewListObj(argCt, &(objv[i]));
1016 varPtr->value.objPtr = listPtr;
1017 Tcl_IncrRefCount(listPtr); /* local var is a reference */
1018 TclClearVarUndefined(varPtr);
1019 argCt = 0;
1020 break; /* done processing args */
1021 } else if (argCt > 0) {
1022 Tcl_Obj *objPtr = objv[i];
1023 varPtr->value.objPtr = objPtr;
1024 TclClearVarUndefined(varPtr);
1025 Tcl_IncrRefCount(objPtr); /* since the local variable now has
1026 * another reference to object. */
1027 } else if (localPtr->defValuePtr != NULL) {
1028 Tcl_Obj *objPtr = localPtr->defValuePtr;
1029 varPtr->value.objPtr = objPtr;
1030 TclClearVarUndefined(varPtr);
1031 Tcl_IncrRefCount(objPtr); /* since the local variable now has
1032 * another reference to object. */
1033 } else {
1034 goto incorrectArgs;
1035 }
1036 varPtr++;
1037 localPtr = localPtr->nextPtr;
1038 }
1039 if (argCt > 0) {
1040 incorrectArgs:
1041 /*
1042 * Build up equivalent to Tcl_WrongNumArgs message for proc
1043 */
1044 Tcl_ResetResult(interp);
1045 Tcl_AppendStringsToObj(objResult,
1046 "wrong # args: should be \"", procName, (char *) NULL);
1047 localPtr = procPtr->firstLocalPtr;
1048 for (i = 1; i <= numArgs; i++) {
1049 if (localPtr->defValuePtr != NULL) {
1050 Tcl_AppendStringsToObj(objResult,
1051 " ?", localPtr->name, "?", (char *) NULL);
1052 } else {
1053 Tcl_AppendStringsToObj(objResult,
1054 " ", localPtr->name, (char *) NULL);
1055 }
1056 localPtr = localPtr->nextPtr;
1057 }
1058 Tcl_AppendStringsToObj(objResult, "\"", (char *) NULL);
1059
1060 result = TCL_ERROR;
1061 goto procDone;
1062 }
1063
1064 /*
1065 * Invoke the commands in the procedure's body.
1066 */
1067
1068 #ifdef TCL_COMPILE_DEBUG
1069 if (tclTraceExec >= 1) {
1070 fprintf(stdout, "Calling proc ");
1071 for (i = 0; i < objc; i++) {
1072 TclPrintObject(stdout, objv[i], 15);
1073 fprintf(stdout, " ");
1074 }
1075 fprintf(stdout, "\n");
1076 fflush(stdout);
1077 }
1078 #endif /*TCL_COMPILE_DEBUG*/
1079
1080 iPtr->returnCode = TCL_OK;
1081 procPtr->refCount++;
1082 result = TclCompEvalObj(interp, procPtr->bodyPtr);
1083 procPtr->refCount--;
1084 if (procPtr->refCount <= 0) {
1085 TclProcCleanupProc(procPtr);
1086 }
1087
1088 if (result != TCL_OK) {
1089 result = ProcessProcResultCode(interp, procName, nameLen, result);
1090 }
1091
1092 /*
1093 * Pop and free the call frame for this procedure invocation, then
1094 * free the compiledLocals array if malloc'ed storage was used.
1095 */
1096
1097 procDone:
1098 Tcl_PopCallFrame(interp);
1099 if (compiledLocals != localStorage) {
1100 ckfree((char *) compiledLocals);
1101 }
1102 return result;
1103 #undef NUM_LOCALS
1104 }
1105
1106 /*
1107 *----------------------------------------------------------------------
1108 *
1109 * TclProcCompileProc --
1110 *
1111 * Called just before a procedure is executed to compile the
1112 * body to byte codes. If the type of the body is not
1113 * "byte code" or if the compile conditions have changed
1114 * (namespace context, epoch counters, etc.) then the body
1115 * is recompiled. Otherwise, this procedure does nothing.
1116 *
1117 * Results:
1118 * None.
1119 *
1120 * Side effects:
1121 * May change the internal representation of the body object
1122 * to compiled code.
1123 *
1124 *----------------------------------------------------------------------
1125 */
1126
1127 int
1128 TclProcCompileProc(interp, procPtr, bodyPtr, nsPtr, description, procName)
1129 Tcl_Interp *interp; /* Interpreter containing procedure. */
1130 Proc *procPtr; /* Data associated with procedure. */
1131 Tcl_Obj *bodyPtr; /* Body of proc. (Usually procPtr->bodyPtr,
1132 * but could be any code fragment compiled
1133 * in the context of this procedure.) */
1134 Namespace *nsPtr; /* Namespace containing procedure. */
1135 CONST char *description; /* string describing this body of code. */
1136 CONST char *procName; /* Name of this procedure. */
1137 {
1138 Interp *iPtr = (Interp*)interp;
1139 int result;
1140 Tcl_CallFrame frame;
1141 Proc *saveProcPtr;
1142 ByteCode *codePtr = (ByteCode *) bodyPtr->internalRep.otherValuePtr;
1143
1144 /*
1145 * If necessary, compile the procedure's body. The compiler will
1146 * allocate frame slots for the procedure's non-argument local
1147 * variables. If the ByteCode already exists, make sure it hasn't been
1148 * invalidated by someone redefining a core command (this might make the
1149 * compiled code wrong). Also, if the code was compiled in/for a
1150 * different interpreter, we recompile it. Note that compiling the body
1151 * might increase procPtr->numCompiledLocals if new local variables are
1152 * found while compiling.
1153 *
1154 * Precompiled procedure bodies, however, are immutable and therefore
1155 * they are not recompiled, even if things have changed.
1156 */
1157
1158 if (bodyPtr->typePtr == &tclByteCodeType) {
1159 if (((Interp *) *codePtr->interpHandle != iPtr)
1160 || (codePtr->compileEpoch != iPtr->compileEpoch)
1161 || (codePtr->nsPtr != nsPtr)) {
1162 if (codePtr->flags & TCL_BYTECODE_PRECOMPILED) {
1163 if ((Interp *) *codePtr->interpHandle != iPtr) {
1164 Tcl_AppendResult(interp,
1165 "a precompiled script jumped interps", NULL);
1166 return TCL_ERROR;
1167 }
1168 codePtr->compileEpoch = iPtr->compileEpoch;
1169 codePtr->nsPtr = nsPtr;
1170 } else {
1171 (*tclByteCodeType.freeIntRepProc)(bodyPtr);
1172 bodyPtr->typePtr = (Tcl_ObjType *) NULL;
1173 }
1174 }
1175 }
1176 if (bodyPtr->typePtr != &tclByteCodeType) {
1177 int numChars;
1178 char *ellipsis;
1179
1180 #ifdef TCL_COMPILE_DEBUG
1181 if (tclTraceCompile >= 1) {
1182 /*
1183 * Display a line summarizing the top level command we
1184 * are about to compile.
1185 */
1186
1187 numChars = strlen(procName);
1188 ellipsis = "";
1189 if (numChars > 50) {
1190 numChars = 50;
1191 ellipsis = "...";
1192 }
1193 fprintf(stdout, "Compiling %s \"%.*s%s\"\n",
1194 description, numChars, procName, ellipsis);
1195 }
1196 #endif
1197
1198 /*
1199 * Plug the current procPtr into the interpreter and coerce
1200 * the code body to byte codes. The interpreter needs to
1201 * know which proc it's compiling so that it can access its
1202 * list of compiled locals.
1203 *
1204 * TRICKY NOTE: Be careful to push a call frame with the
1205 * proper namespace context, so that the byte codes are
1206 * compiled in the appropriate class context.
1207 */
1208
1209 saveProcPtr = iPtr->compiledProcPtr;
1210 iPtr->compiledProcPtr = procPtr;
1211
1212 result = Tcl_PushCallFrame(interp, &frame,
1213 (Tcl_Namespace*)nsPtr, /* isProcCallFrame */ 0);
1214
1215 if (result == TCL_OK) {
1216 result = tclByteCodeType.setFromAnyProc(interp, bodyPtr);
1217 Tcl_PopCallFrame(interp);
1218 }
1219
1220 iPtr->compiledProcPtr = saveProcPtr;
1221
1222 if (result != TCL_OK) {
1223 if (result == TCL_ERROR) {
1224 char buf[100 + TCL_INTEGER_SPACE];
1225
1226 numChars = strlen(procName);
1227 ellipsis = "";
1228 if (numChars > 50) {
1229 numChars = 50;
1230 ellipsis = "...";
1231 }
1232 while ( (procName[numChars] & 0xC0) == 0x80 ) {
1233 /*
1234 * Back up truncation point so that we don't truncate
1235 * in the middle of a multi-byte character (in UTF-8)
1236 */
1237 numChars--;
1238 ellipsis = "...";
1239 }
1240 sprintf(buf, "\n (compiling %s \"%.*s%s\", line %d)",
1241 description, numChars, procName, ellipsis,
1242 interp->errorLine);
1243 Tcl_AddObjErrorInfo(interp, buf, -1);
1244 }
1245 return result;
1246 }
1247 } else if (codePtr->nsEpoch != nsPtr->resolverEpoch) {
1248 register CompiledLocal *localPtr;
1249
1250 /*
1251 * The resolver epoch has changed, but we only need to invalidate
1252 * the resolver cache.
1253 */
1254
1255 for (localPtr = procPtr->firstLocalPtr; localPtr != NULL;
1256 localPtr = localPtr->nextPtr) {
1257 localPtr->flags &= ~(VAR_RESOLVED);
1258 if (localPtr->resolveInfo) {
1259 if (localPtr->resolveInfo->deleteProc) {
1260 localPtr->resolveInfo->deleteProc(localPtr->resolveInfo);
1261 } else {
1262 ckfree((char*)localPtr->resolveInfo);
1263 }
1264 localPtr->resolveInfo = NULL;
1265 }
1266 }
1267 }
1268 return TCL_OK;
1269 }
1270
1271 /*
1272 *----------------------------------------------------------------------
1273 *
1274 * ProcessProcResultCode --
1275 *
1276 * Procedure called by TclObjInterpProc to process a return code other
1277 * than TCL_OK returned by a Tcl procedure.
1278 *
1279 * Results:
1280 * Depending on the argument return code, the result returned is
1281 * another return code and the interpreter's result is set to a value
1282 * to supplement that return code.
1283 *
1284 * Side effects:
1285 * If the result returned is TCL_ERROR, traceback information about
1286 * the procedure just executed is appended to the interpreter's
1287 * "errorInfo" variable.
1288 *
1289 *----------------------------------------------------------------------
1290 */
1291
1292 static int
1293 ProcessProcResultCode(interp, procName, nameLen, returnCode)
1294 Tcl_Interp *interp; /* The interpreter in which the procedure
1295 * was called and returned returnCode. */
1296 char *procName; /* Name of the procedure. Used for error
1297 * messages and trace information. */
1298 int nameLen; /* Number of bytes in procedure's name. */
1299 int returnCode; /* The unexpected result code. */
1300 {
1301 Interp *iPtr = (Interp *) interp;
1302 char msg[100 + TCL_INTEGER_SPACE];
1303 char *ellipsis = "";
1304
1305 if (returnCode == TCL_OK) {
1306 return TCL_OK;
1307 }
1308 if ((returnCode > TCL_CONTINUE) || (returnCode < TCL_OK)) {
1309 return returnCode;
1310 }
1311 if (returnCode == TCL_RETURN) {
1312 return TclUpdateReturnInfo(iPtr);
1313 }
1314 if (returnCode != TCL_ERROR) {
1315 Tcl_ResetResult(interp);
1316 Tcl_AppendToObj(Tcl_GetObjResult(interp), ((returnCode == TCL_BREAK)
1317 ? "invoked \"break\" outside of a loop"
1318 : "invoked \"continue\" outside of a loop"), -1);
1319 }
1320 if (nameLen > 60) {
1321 nameLen = 60;
1322 ellipsis = "...";
1323 }
1324 while ( (procName[nameLen] & 0xC0) == 0x80 ) {
1325 /*
1326 * Back up truncation point so that we don't truncate in the
1327 * middle of a multi-byte character (in UTF-8)
1328 */
1329 nameLen--;
1330 ellipsis = "...";
1331 }
1332 sprintf(msg, "\n (procedure \"%.*s%s\" line %d)", nameLen, procName,
1333 ellipsis, iPtr->errorLine);
1334 Tcl_AddObjErrorInfo(interp, msg, -1);
1335 return TCL_ERROR;
1336 }
1337
1338 /*
1339 *----------------------------------------------------------------------
1340 *
1341 * TclProcDeleteProc --
1342 *
1343 * This procedure is invoked just before a command procedure is
1344 * removed from an interpreter. Its job is to release all the
1345 * resources allocated to the procedure.
1346 *
1347 * Results:
1348 * None.
1349 *
1350 * Side effects:
1351 * Memory gets freed, unless the procedure is actively being
1352 * executed. In this case the cleanup is delayed until the
1353 * last call to the current procedure completes.
1354 *
1355 *----------------------------------------------------------------------
1356 */
1357
1358 void
1359 TclProcDeleteProc(clientData)
1360 ClientData clientData; /* Procedure to be deleted. */
1361 {
1362 Proc *procPtr = (Proc *) clientData;
1363
1364 procPtr->refCount--;
1365 if (procPtr->refCount <= 0) {
1366 TclProcCleanupProc(procPtr);
1367 }
1368 }
1369
1370 /*
1371 *----------------------------------------------------------------------
1372 *
1373 * TclProcCleanupProc --
1374 *
1375 * This procedure does all the real work of freeing up a Proc
1376 * structure. It's called only when the structure's reference
1377 * count becomes zero.
1378 *
1379 * Results:
1380 * None.
1381 *
1382 * Side effects:
1383 * Memory gets freed.
1384 *
1385 *----------------------------------------------------------------------
1386 */
1387
1388 void
1389 TclProcCleanupProc(procPtr)
1390 register Proc *procPtr; /* Procedure to be deleted. */
1391 {
1392 register CompiledLocal *localPtr;
1393 Tcl_Obj *bodyPtr = procPtr->bodyPtr;
1394 Tcl_Obj *defPtr;
1395 Tcl_ResolvedVarInfo *resVarInfo;
1396
1397 if (bodyPtr != NULL) {
1398 Tcl_DecrRefCount(bodyPtr);
1399 }
1400 for (localPtr = procPtr->firstLocalPtr; localPtr != NULL; ) {
1401 CompiledLocal *nextPtr = localPtr->nextPtr;
1402
1403 resVarInfo = localPtr->resolveInfo;
1404 if (resVarInfo) {
1405 if (resVarInfo->deleteProc) {
1406 (*resVarInfo->deleteProc)(resVarInfo);
1407 } else {
1408 ckfree((char *) resVarInfo);
1409 }
1410 }
1411
1412 if (localPtr->defValuePtr != NULL) {
1413 defPtr = localPtr->defValuePtr;
1414 Tcl_DecrRefCount(defPtr);
1415 }
1416 ckfree((char *) localPtr);
1417 localPtr = nextPtr;
1418 }
1419 ckfree((char *) procPtr);
1420 }
1421
1422 /*
1423 *----------------------------------------------------------------------
1424 *
1425 * TclUpdateReturnInfo --
1426 *
1427 * This procedure is called when procedures return, and at other
1428 * points where the TCL_RETURN code is used. It examines fields
1429 * such as iPtr->returnCode and iPtr->errorCode and modifies
1430 * the real return status accordingly.
1431 *
1432 * Results:
1433 * The return value is the true completion code to use for
1434 * the procedure, instead of TCL_RETURN.
1435 *
1436 * Side effects:
1437 * The errorInfo and errorCode variables may get modified.
1438 *
1439 *----------------------------------------------------------------------
1440 */
1441
1442 int
1443 TclUpdateReturnInfo(iPtr)
1444 Interp *iPtr; /* Interpreter for which TCL_RETURN
1445 * exception is being processed. */
1446 {
1447 int code;
1448 char *errorCode;
1449
1450 code = iPtr->returnCode;
1451 iPtr->returnCode = TCL_OK;
1452 if (code == TCL_ERROR) {
1453 errorCode = ((iPtr->errorCode != NULL) ? iPtr->errorCode : "NONE");
1454 Tcl_ObjSetVar2((Tcl_Interp *) iPtr, iPtr->execEnvPtr->errorCode,
1455 NULL, Tcl_NewStringObj(errorCode, -1),
1456 TCL_GLOBAL_ONLY);
1457 iPtr->flags |= ERROR_CODE_SET;
1458 if (iPtr->errorInfo != NULL) {
1459 Tcl_ObjSetVar2((Tcl_Interp *) iPtr, iPtr->execEnvPtr->errorInfo,
1460 NULL, Tcl_NewStringObj(iPtr->errorInfo, -1),
1461 TCL_GLOBAL_ONLY);
1462 iPtr->flags |= ERR_IN_PROGRESS;
1463 }
1464 }
1465 return code;
1466 }
1467
1468 /*
1469 *----------------------------------------------------------------------
1470 *
1471 * TclGetInterpProc --
1472 *
1473 * Returns a pointer to the TclProcInterpProc procedure; this is different
1474 * from the value obtained from the TclProcInterpProc reference on systems
1475 * like Windows where import and export versions of a procedure exported
1476 * by a DLL exist.
1477 *
1478 * Results:
1479 * Returns the internal address of the TclProcInterpProc procedure.
1480 *
1481 * Side effects:
1482 * None.
1483 *
1484 *----------------------------------------------------------------------
1485 */
1486
1487 TclCmdProcType
1488 TclGetInterpProc()
1489 {
1490 return (TclCmdProcType) TclProcInterpProc;
1491 }
1492
1493 /*
1494 *----------------------------------------------------------------------
1495 *
1496 * TclGetObjInterpProc --
1497 *
1498 * Returns a pointer to the TclObjInterpProc procedure; this is different
1499 * from the value obtained from the TclObjInterpProc reference on systems
1500 * like Windows where import and export versions of a procedure exported
1501 * by a DLL exist.
1502 *
1503 * Results:
1504 * Returns the internal address of the TclObjInterpProc procedure.
1505 *
1506 * Side effects:
1507 * None.
1508 *
1509 *----------------------------------------------------------------------
1510 */
1511
1512 TclObjCmdProcType
1513 TclGetObjInterpProc()
1514 {
1515 return (TclObjCmdProcType) TclObjInterpProc;
1516 }
1517
1518 /*
1519 *----------------------------------------------------------------------
1520 *
1521 * TclNewProcBodyObj --
1522 *
1523 * Creates a new object, of type "procbody", whose internal
1524 * representation is the given Proc struct.
1525 * The newly created object's reference count is 0.
1526 *
1527 * Results:
1528 * Returns a pointer to a newly allocated Tcl_Obj, 0 on error.
1529 *
1530 * Side effects:
1531 * The reference count in the ByteCode attached to the Proc is bumped up
1532 * by one, since the internal rep stores a pointer to it.
1533 *
1534 *----------------------------------------------------------------------
1535 */
1536
1537 Tcl_Obj *
1538 TclNewProcBodyObj(procPtr)
1539 Proc *procPtr; /* the Proc struct to store as the internal
1540 * representation. */
1541 {
1542 Tcl_Obj *objPtr;
1543
1544 if (!procPtr) {
1545 return (Tcl_Obj *) NULL;
1546 }
1547
1548 objPtr = Tcl_NewStringObj("", 0);
1549
1550 if (objPtr) {
1551 objPtr->typePtr = &tclProcBodyType;
1552 objPtr->internalRep.otherValuePtr = (VOID *) procPtr;
1553
1554 procPtr->refCount++;
1555 }
1556
1557 return objPtr;
1558 }
1559
1560 /*
1561 *----------------------------------------------------------------------
1562 *
1563 * ProcBodyDup --
1564 *
1565 * Tcl_ObjType's Dup function for the proc body object.
1566 * Bumps the reference count on the Proc stored in the internal
1567 * representation.
1568 *
1569 * Results:
1570 * None.
1571 *
1572 * Side effects:
1573 * Sets up the object in dupPtr to be a duplicate of the one in srcPtr.
1574 *
1575 *----------------------------------------------------------------------
1576 */
1577
1578 static void ProcBodyDup(srcPtr, dupPtr)
1579 Tcl_Obj *srcPtr; /* object to copy */
1580 Tcl_Obj *dupPtr; /* target object for the duplication */
1581 {
1582 Proc *procPtr = (Proc *) srcPtr->internalRep.otherValuePtr;
1583
1584 dupPtr->typePtr = &tclProcBodyType;
1585 dupPtr->internalRep.otherValuePtr = (VOID *) procPtr;
1586 procPtr->refCount++;
1587 }
1588
1589 /*
1590 *----------------------------------------------------------------------
1591 *
1592 * ProcBodyFree --
1593 *
1594 * Tcl_ObjType's Free function for the proc body object.
1595 * The reference count on its Proc struct is decreased by 1; if the count
1596 * reaches 0, the proc is freed.
1597 *
1598 * Results:
1599 * None.
1600 *
1601 * Side effects:
1602 * If the reference count on the Proc struct reaches 0, the struct is freed.
1603 *
1604 *----------------------------------------------------------------------
1605 */
1606
1607 static void
1608 ProcBodyFree(objPtr)
1609 Tcl_Obj *objPtr; /* the object to clean up */
1610 {
1611 Proc *procPtr = (Proc *) objPtr->internalRep.otherValuePtr;
1612 procPtr->refCount--;
1613 if (procPtr->refCount <= 0) {
1614 TclProcCleanupProc(procPtr);
1615 }
1616 }
1617
1618 /*
1619 *----------------------------------------------------------------------
1620 *
1621 * ProcBodySetFromAny --
1622 *
1623 * Tcl_ObjType's SetFromAny function for the proc body object.
1624 * Calls panic.
1625 *
1626 * Results:
1627 * Theoretically returns a TCL result code.
1628 *
1629 * Side effects:
1630 * Calls panic, since we can't set the value of the object from a string
1631 * representation (or any other internal ones).
1632 *
1633 *----------------------------------------------------------------------
1634 */
1635
1636 static int
1637 ProcBodySetFromAny(interp, objPtr)
1638 Tcl_Interp *interp; /* current interpreter */
1639 Tcl_Obj *objPtr; /* object pointer */
1640 {
1641 panic("called ProcBodySetFromAny");
1642
1643 /*
1644 * this to keep compilers happy.
1645 */
1646
1647 return TCL_OK;
1648 }
1649
1650 /*
1651 *----------------------------------------------------------------------
1652 *
1653 * ProcBodyUpdateString --
1654 *
1655 * Tcl_ObjType's UpdateString function for the proc body object.
1656 * Calls panic.
1657 *
1658 * Results:
1659 * None.
1660 *
1661 * Side effects:
1662 * Calls panic, since we this type has no string representation.
1663 *
1664 *----------------------------------------------------------------------
1665 */
1666
1667 static void
1668 ProcBodyUpdateString(objPtr)
1669 Tcl_Obj *objPtr; /* the object to update */
1670 {
1671 panic("called ProcBodyUpdateString");
1672 }
1673
1674
1675 /*
1676 *----------------------------------------------------------------------
1677 *
1678 * TclCompileNoOp --
1679 *
1680 * Procedure called to compile noOp's
1681 *
1682 * Results:
1683 * The return value is TCL_OK, indicating successful compilation.
1684 *
1685 * Side effects:
1686 * Instructions are added to envPtr to execute a noOp at runtime.
1687 *
1688 *----------------------------------------------------------------------
1689 */
1690
1691 static int
1692 TclCompileNoOp(interp, parsePtr, envPtr)
1693 Tcl_Interp *interp; /* Used for error reporting. */
1694 Tcl_Parse *parsePtr; /* Points to a parse structure for the
1695 * command created by Tcl_ParseCommand. */
1696 CompileEnv *envPtr; /* Holds resulting instructions. */
1697 {
1698 Tcl_Token *tokenPtr;
1699 int i, code;
1700 int savedStackDepth = envPtr->currStackDepth;
1701
1702 tokenPtr = parsePtr->tokenPtr;
1703 for(i = 1; i < parsePtr->numWords; i++) {
1704 tokenPtr = tokenPtr + tokenPtr->numComponents + 1;
1705 envPtr->currStackDepth = savedStackDepth;
1706
1707 if (tokenPtr->type != TCL_TOKEN_SIMPLE_WORD) {
1708 code = TclCompileTokens(interp, tokenPtr+1,
1709 tokenPtr->numComponents, envPtr);
1710 if (code != TCL_OK) {
1711 return code;
1712 }
1713 TclEmitOpcode(INST_POP, envPtr);
1714 }
1715 }
1716 envPtr->currStackDepth = savedStackDepth;
1717 TclEmitPush(TclRegisterLiteral(envPtr, "", 0, /*onHeap*/ 0), envPtr);
1718 return TCL_OK;
1719 }
1720
1721
1722