1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/*
* tclProc.c --
*
* This file contains routines that implement Tcl procedures,
* including the "proc" and "uplevel" commands.
*
* Copyright (c) 1987-1993 The Regents of the University of California.
* Copyright (c) 1994-1998 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclProc.c,v 1.73.2.4 2005/04/12 18:23:33 msofer Exp $
*/
#include "tclInt.h"
#include "tclCompile.h"
/*
* Prototypes for static functions in this file
*/
static void ProcBodyDup _ANSI_ARGS_((Tcl_Obj *srcPtr, Tcl_Obj *dupPtr));
static void ProcBodyFree _ANSI_ARGS_((Tcl_Obj *objPtr));
static int ProcessProcResultCode _ANSI_ARGS_((Tcl_Interp *interp,
char *procName, int nameLen, int returnCode));
static int TclCompileNoOp _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Parse *parsePtr, struct CompileEnv *envPtr));
static void InitCompiledLocals _ANSI_ARGS_((Tcl_Interp *interp,
ByteCode *codePtr, CompiledLocal *localPtr,
Var *varPtr, Namespace *nsPtr));
/*
* The ProcBodyObjType type
*/
Tcl_ObjType tclProcBodyType = {
"procbody", /* name for this type */
|
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/*
* tclProc.c --
*
* This file contains routines that implement Tcl procedures,
* including the "proc" and "uplevel" commands.
*
* Copyright (c) 1987-1993 The Regents of the University of California.
* Copyright (c) 1994-1998 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclProc.c,v 1.73.2.5 2005/04/12 21:10:02 msofer Exp $
*/
#include "tclInt.h"
#include "tclCompile.h"
/*
* Prototypes for static functions in this file
*/
static void ProcBodyDup _ANSI_ARGS_((Tcl_Obj *srcPtr, Tcl_Obj *dupPtr));
static void ProcBodyFree _ANSI_ARGS_((Tcl_Obj *objPtr));
static int ProcessProcResultCode _ANSI_ARGS_((Tcl_Interp *interp,
char *procName, int nameLen, int returnCode));
static int TclCompileNoOp _ANSI_ARGS_((Tcl_Interp *interp,
Tcl_Parse *parsePtr, struct CompileEnv *envPtr));
static void InitCompiledLocals _ANSI_ARGS_((Tcl_Interp *interp,
ByteCode *codePtr, CompiledLocal *localPtr,
ShortVar *varPtr, Namespace *nsPtr));
/*
* The ProcBodyObjType type
*/
Tcl_ObjType tclProcBodyType = {
"procbody", /* name for this type */
|
| ︙ | | | ︙ | |
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
* NOTE: code precompiled under older versions of Tcl will not
* work properly.
*/
if ((localPtr->nameLength != nameLength)
|| (strcmp(localPtr->name, fieldValues[0]))
|| (localPtr->frameIndex != i)
|| (localPtr->flags !=
(VAR_ARGUMENT|VAR_DIRECT_READABLE|VAR_DIRECT_WRITABLE))
|| (localPtr->defValuePtr == NULL && fieldCount == 2)
|| (localPtr->defValuePtr != NULL && fieldCount != 2)) {
char buf[40 + TCL_INTEGER_SPACE];
ckfree((char *) fieldValues);
sprintf(buf, "%d is inconsistent with precompiled body", i);
Tcl_AppendResult(interp, "procedure \"", procName,
|
|
|
|
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
* NOTE: code precompiled under older versions of Tcl will not
* work properly.
*/
if ((localPtr->nameLength != nameLength)
|| (strcmp(localPtr->name, fieldValues[0]))
|| (localPtr->frameIndex != i)
|| (localPtr->flags != (VAR_ARGUMENT|VAR_SHORT
|VAR_DIRECT_READABLE|VAR_DIRECT_WRITABLE))
|| (localPtr->defValuePtr == NULL && fieldCount == 2)
|| (localPtr->defValuePtr != NULL && fieldCount != 2)) {
char buf[40 + TCL_INTEGER_SPACE];
ckfree((char *) fieldValues);
sprintf(buf, "%d is inconsistent with precompiled body", i);
Tcl_AppendResult(interp, "procedure \"", procName,
|
| ︙ | | | ︙ | |
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
} else {
procPtr->lastLocalPtr->nextPtr = localPtr;
procPtr->lastLocalPtr = localPtr;
}
localPtr->nextPtr = NULL;
localPtr->nameLength = nameLength;
localPtr->frameIndex = i;
localPtr->flags = (VAR_ARGUMENT|VAR_DIRECT_READABLE|VAR_DIRECT_WRITABLE);
localPtr->resolveInfo = NULL;
if (fieldCount == 2) {
localPtr->defValuePtr =
Tcl_NewStringObj(fieldValues[1], valueLength);
Tcl_IncrRefCount(localPtr->defValuePtr);
} else {
|
|
>
|
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
} else {
procPtr->lastLocalPtr->nextPtr = localPtr;
procPtr->lastLocalPtr = localPtr;
}
localPtr->nextPtr = NULL;
localPtr->nameLength = nameLength;
localPtr->frameIndex = i;
localPtr->flags = (VAR_ARGUMENT|VAR_SHORT
|VAR_DIRECT_READABLE|VAR_DIRECT_WRITABLE);
localPtr->resolveInfo = NULL;
if (fieldCount == 2) {
localPtr->defValuePtr =
Tcl_NewStringObj(fieldValues[1], valueLength);
Tcl_IncrRefCount(localPtr->defValuePtr);
} else {
|
| ︙ | | | ︙ | |
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
|
*/
static void
InitCompiledLocals(interp, codePtr, localPtr, varPtr, nsPtr)
Tcl_Interp *interp; /* Current interpreter. */
ByteCode *codePtr;
CompiledLocal *localPtr;
Var *varPtr;
Namespace *nsPtr; /* Pointer to current namespace. */
{
Interp *iPtr = (Interp*) interp;
int haveResolvers = (nsPtr->compiledVarResProc || iPtr->resolverPtr);
CompiledLocal *firstLocalPtr;
if (codePtr->flags & TCL_BYTECODE_RESOLVE_VARS) {
|
|
|
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
|
*/
static void
InitCompiledLocals(interp, codePtr, localPtr, varPtr, nsPtr)
Tcl_Interp *interp; /* Current interpreter. */
ByteCode *codePtr;
CompiledLocal *localPtr;
ShortVar *varPtr;
Namespace *nsPtr; /* Pointer to current namespace. */
{
Interp *iPtr = (Interp*) interp;
int haveResolvers = (nsPtr->compiledVarResProc || iPtr->resolverPtr);
CompiledLocal *firstLocalPtr;
if (codePtr->flags & TCL_BYTECODE_RESOLVE_VARS) {
|
| ︙ | | | ︙ | |
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
* we call their "resolver" procs to get our hands on the variable,
* and we make the compiled local a link to the real variable.
*/
if (haveResolvers) {
Tcl_ResolvedVarInfo *resVarInfo;
for (; localPtr != NULL; varPtr++, localPtr = localPtr->nextPtr) {
varPtr->value.objPtr = NULL;
varPtr->id.name = localPtr->name; /* will be just '\0' if temp var */
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = localPtr->flags;
/*
* Now invoke the resolvers to determine the exact variables that
* should be used.
*/
resVarInfo = localPtr->resolveInfo;
if (resVarInfo && resVarInfo->fetchProc) {
Var *resolvedVarPtr = (Var*) (*resVarInfo->fetchProc)(interp,
resVarInfo);
if (resolvedVarPtr) {
resolvedVarPtr->refCount++;
varPtr->value.linkPtr = resolvedVarPtr;
varPtr->flags = VAR_LINK;
}
}
}
} else {
for (; localPtr != NULL; varPtr++, localPtr = localPtr->nextPtr) {
varPtr->value.objPtr = NULL;
varPtr->id.name = localPtr->name; /* will be just '\0' if temp var */
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = localPtr->flags;
}
}
}
/*
*----------------------------------------------------------------------
*
|
>
<
<
<
<
>
<
<
<
<
|
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
* we call their "resolver" procs to get our hands on the variable,
* and we make the compiled local a link to the real variable.
*/
if (haveResolvers) {
Tcl_ResolvedVarInfo *resVarInfo;
for (; localPtr != NULL; varPtr++, localPtr = localPtr->nextPtr) {
varPtr->flags = localPtr->flags;
varPtr->value.objPtr = NULL;
varPtr->id.name = localPtr->name; /* will be just '\0' if temp var */
/*
* Now invoke the resolvers to determine the exact variables that
* should be used.
*/
resVarInfo = localPtr->resolveInfo;
if (resVarInfo && resVarInfo->fetchProc) {
Var *resolvedVarPtr = (Var*) (*resVarInfo->fetchProc)(interp,
resVarInfo);
if (resolvedVarPtr) {
resolvedVarPtr->refCount++;
varPtr->value.linkPtr = resolvedVarPtr;
varPtr->flags = VAR_LINK;
}
}
}
} else {
for (; localPtr != NULL; varPtr++, localPtr = localPtr->nextPtr) {
varPtr->flags = localPtr->flags;
varPtr->value.objPtr = NULL;
varPtr->id.name = localPtr->name; /* will be just '\0' if temp var */
}
}
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
|
void
TclInitCompiledLocals(interp, framePtr, nsPtr)
Tcl_Interp *interp; /* Current interpreter. */
CallFrame *framePtr; /* Call frame to initialize. */
Namespace *nsPtr; /* Pointer to current namespace. */
{
Var *varPtr = framePtr->compiledLocals;
Tcl_Obj *bodyPtr;
ByteCode *codePtr;
CompiledLocal *localPtr = framePtr->procPtr->firstLocalPtr;
bodyPtr = framePtr->procPtr->bodyPtr;
if (bodyPtr->typePtr != &tclByteCodeType) {
Tcl_Panic("body object for proc attached to frame is not a byte code type");
|
|
|
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
|
void
TclInitCompiledLocals(interp, framePtr, nsPtr)
Tcl_Interp *interp; /* Current interpreter. */
CallFrame *framePtr; /* Call frame to initialize. */
Namespace *nsPtr; /* Pointer to current namespace. */
{
ShortVar *varPtr = framePtr->compiledLocals;
Tcl_Obj *bodyPtr;
ByteCode *codePtr;
CompiledLocal *localPtr = framePtr->procPtr->firstLocalPtr;
bodyPtr = framePtr->procPtr->bodyPtr;
if (bodyPtr->typePtr != &tclByteCodeType) {
Tcl_Panic("body object for proc attached to frame is not a byte code type");
|
| ︙ | | | ︙ | |
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
|
int objc; /* Count of number of arguments to this
* procedure. */
Tcl_Obj *CONST objv[]; /* Argument value objects. */
{
register Proc *procPtr = (Proc *) clientData;
Namespace *nsPtr = procPtr->cmdPtr->nsPtr;
CallFrame *framePtr, **framePtrPtr;
register Var *varPtr;
register CompiledLocal *localPtr;
char *procName;
int nameLen, localCt, numArgs, argCt, i, imax, result;
Var *compiledLocals;
/*
* Get the procedure's name.
*/
procName = Tcl_GetStringFromObj(objv[0], &nameLen);
|
|
|
|
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
|
int objc; /* Count of number of arguments to this
* procedure. */
Tcl_Obj *CONST objv[]; /* Argument value objects. */
{
register Proc *procPtr = (Proc *) clientData;
Namespace *nsPtr = procPtr->cmdPtr->nsPtr;
CallFrame *framePtr, **framePtrPtr;
register ShortVar *varPtr;
register CompiledLocal *localPtr;
char *procName;
int nameLen, localCt, numArgs, argCt, i, imax, result;
ShortVar *compiledLocals;
/*
* Get the procedure's name.
*/
procName = Tcl_GetStringFromObj(objv[0], &nameLen);
|
| ︙ | | | ︙ | |
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
|
/*
* Create the "compiledLocals" array. Make sure it is large enough to
* hold all the procedure's compiled local variables, including its
* formal parameters.
*/
localCt = procPtr->numCompiledLocals;
compiledLocals = (Var *) TclStackAlloc(interp, localCt*sizeof(Var));
framePtr->numCompiledLocals = localCt;
framePtr->compiledLocals = compiledLocals;
/*
* Match and assign the call's actual parameters to the procedure's
* formal arguments. The formal arguments are described by the first
* numArgs entries in both the Proc structure's local variable list and
|
|
|
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
|
/*
* Create the "compiledLocals" array. Make sure it is large enough to
* hold all the procedure's compiled local variables, including its
* formal parameters.
*/
localCt = procPtr->numCompiledLocals;
compiledLocals = (ShortVar *) TclStackAlloc(interp, localCt*sizeof(ShortVar));
framePtr->numCompiledLocals = localCt;
framePtr->compiledLocals = compiledLocals;
/*
* Match and assign the call's actual parameters to the procedure's
* formal arguments. The formal arguments are described by the first
* numArgs entries in both the Proc structure's local variable list and
|
| ︙ | | | ︙ | |
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
/*
* "Normal" arguments; last formal is special, depends on
* it being 'args'.
*/
Tcl_Obj *objPtr = objv[i];
varPtr->value.objPtr = objPtr;
Tcl_IncrRefCount(objPtr); /* local var is a reference */
varPtr->id.name = localPtr->name;
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = localPtr->flags;
varPtr++;
localPtr = localPtr->nextPtr;
}
for (; i < numArgs; i++) {
/*
* This loop is entered if argCt < (numArgs-1).
* Set default values; last formal is special.
*/
if (localPtr->defValuePtr != NULL) {
Tcl_Obj *objPtr = localPtr->defValuePtr;
varPtr->value.objPtr = objPtr;
Tcl_IncrRefCount(objPtr); /* local var is a reference */
varPtr->id.name = localPtr->name;
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = localPtr->flags;
varPtr++;
localPtr = localPtr->nextPtr;
} else {
goto incorrectArgs;
}
}
|
<
<
<
<
>
>
<
<
<
<
|
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
/*
* "Normal" arguments; last formal is special, depends on
* it being 'args'.
*/
Tcl_Obj *objPtr = objv[i];
varPtr->value.objPtr = objPtr;
Tcl_IncrRefCount(objPtr); /* local var is a reference */
varPtr->flags = localPtr->flags;
varPtr->id.name = localPtr->name;
varPtr++;
localPtr = localPtr->nextPtr;
}
for (; i < numArgs; i++) {
/*
* This loop is entered if argCt < (numArgs-1).
* Set default values; last formal is special.
*/
if (localPtr->defValuePtr != NULL) {
Tcl_Obj *objPtr = localPtr->defValuePtr;
varPtr->flags = localPtr->flags;
varPtr->value.objPtr = objPtr;
Tcl_IncrRefCount(objPtr); /* local var is a reference */
varPtr->id.name = localPtr->name;
varPtr++;
localPtr = localPtr->nextPtr;
} else {
goto incorrectArgs;
}
}
|
| ︙ | | | ︙ | |
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
|
TclDecrRefCount(desiredObjs[i]);
}
#endif /* AVOID_HACKS_FOR_ITCL */
ckfree((char *) desiredObjs);
goto procDone;
}
varPtr->id.name = localPtr->name;
varPtr->refCount = 0;
varPtr->tracePtr = NULL;
varPtr->searchPtr = NULL;
varPtr->flags = localPtr->flags;
localPtr = localPtr->nextPtr;
varPtr++;
runProc:
/*
* Initialise and resolve the remaining compiledLocals.
|
<
<
<
<
>
|
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
|
TclDecrRefCount(desiredObjs[i]);
}
#endif /* AVOID_HACKS_FOR_ITCL */
ckfree((char *) desiredObjs);
goto procDone;
}
varPtr->flags = localPtr->flags;
varPtr->id.name = localPtr->name;
localPtr = localPtr->nextPtr;
varPtr++;
runProc:
/*
* Initialise and resolve the remaining compiledLocals.
|
| ︙ | | | ︙ | |