Tcl Source Code

Changes On Branch tip479
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch tip479 Excluding Merge-Ins

This is equivalent to a diff from 2bdec8b46d to 8db02d0c98

2018-02-14
12:31
Unit test for issue 3c32a3f8bd, Segmentation fault in TclOO.c/ReleaseClassContents() for a class mix... check-in: 42d9036077 user: pooryorick tags: pyk-backport-to-8-6
2017-11-03
12:15
merge core-8-6-branch check-in: ce4b043516 user: jan.nijtmans tags: trunk
10:34
First shot at proposed solution (with test-cases). Doesn't handle "end-<integer>" and "<integer>-<in... check-in: 226140f554 user: jan.nijtmans tags: bug-aaa02c55b9
04:09
Revised Tip#479. Renamed new commands, added an info argspec command. @proc -> procx. @args -> argsx... Leaf check-in: 8db02d0c98 user: hypnotoad tags: tip479
2017-11-02
20:02
Pulling changes from trunk check-in: ed05b17d1c user: hypnotoad tags: tip479
12:53
merge trunk check-in: 319a7765cd user: dgp tags: novem
2017-11-01
22:21
Unit test for issue 3c32a3f8bd, Segmentation fault in TclOO.c/ReleaseClassContents() for a class mix... check-in: 2bdec8b46d user: pooryorick tags: trunk
21:05
Fix bug 3c32a3f8bd, segmentation fault in TclOO.c/ReleaseClassContents() for a class mixed into one ... check-in: 5f178e7f03 user: pooryorick tags: trunk

Changes to doc/proc.n.

34
35
36
37
38
39
40









41
42
43
44
45
46
47
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56







+
+
+
+
+
+
+
+
+







then it is the name of the argument; if there are two fields, then
the first is the argument name and the second is its default value.
Arguments with default values that are followed by non-defaulted
arguments become required arguments; enough actual arguments must be
supplied to allow all arguments up to and including the last required
formal argument.
.PP
If the \fIargs\fR is the last formal argument, and it is given a default
value, that default value is interpreted to be the specification for
how named parameters beyond the last formal argument will be interpreted
(an argspec). An argspec is a dict where every key calls out a local variable
to be defined. The values are a dict which can contain one or more of the
following options: default (a default value), mandatory (boolean, when false
an error will not be thrown if the parameter is missing), and aliases (a
list of alternative names for this parameter.)
.PP
When \fIname\fR is invoked a local variable
will be created for each of the formal arguments to the procedure; its
value will be the value of corresponding argument in the invoking command
or the argument's default value.
Actual arguments are assigned to formal arguments strictly in order.
Arguments with default values need not be
specified in a procedure invocation.  However, there must be enough
57
58
59
60
61
62
63








64
65
66
67
68
69
70
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87







+
+
+
+
+
+
+
+







variable numbers of arguments.  If the last formal argument has the name
.QW \fBargs\fR ,
then a call to the procedure may contain more actual arguments
than the procedure has formal arguments.  In this case, all of the actual arguments
starting at the one that would be assigned to \fBargs\fR are combined into
a list (as if the \fBlist\fR command had been used); this combined value
is assigned to the local variable \fBargs\fR.
.PP
When an argspec is defined elements beyond the last formal
argument are a dict. A variable with the name of every field will be
fill with the contents of every value. An argspec can provide a default value
for missing named parameters, or state that parameter is non-mandatory.
Non-mandatory fields will not be mapped as local variables. The value of
\fIargs\fR is still a verbatim record of elements beyond the final formal
argument.
.PP
When \fIbody\fR is being executed, variable names normally refer to
local variables, which are created automatically when referenced and
deleted when the procedure returns.  One local variable is automatically
created for each of the procedure's arguments.
Other variables can only be accessed by invoking one of the \fBglobal\fR,
\fBvariable\fR, \fBupvar\fR or \fBnamespace upvar\fR commands.

Changes to generic/tcl.decls.

2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2330
2331
2332
2333
2334
2335
2336


2337
2338
2339
2340
2341
2342
2343







-
-







declare 631 {
    Tcl_Channel Tcl_OpenTcpServerEx(Tcl_Interp *interp, const char *service,
	    const char *host, unsigned int flags, Tcl_TcpAcceptProc *acceptProc,
	    ClientData callbackData)
}

# ----- BASELINE -- FOR -- 8.7.0 ----- #



##############################################################################

# Define the platform specific public Tcl interface. These functions are only
# available on the designated platform.

interface tclPlat

Changes to generic/tclBasic.c.

196
197
198
199
200
201
202

203
204
205
206
207
208
209
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210







+







 */

static const CmdInfo builtInCmds[] = {
    /*
     * Commands in the generic core.
     */

    {"argsx",           Tcl_ArgsxObjCmd,       NULL,                   NULL,   CMD_IS_SAFE},
    {"append",		Tcl_AppendObjCmd,	TclCompileAppendCmd,	NULL,	CMD_IS_SAFE},
    {"apply",		Tcl_ApplyObjCmd,	NULL,			TclNRApplyObjCmd,	CMD_IS_SAFE},
    {"break",		Tcl_BreakObjCmd,	TclCompileBreakCmd,	NULL,	CMD_IS_SAFE},
#ifndef TCL_NO_DEPRECATED
    {"case",		Tcl_CaseObjCmd,		NULL,			NULL,	CMD_IS_SAFE},
#endif
    {"catch",		Tcl_CatchObjCmd,	TclCompileCatchCmd,	TclNRCatchObjCmd,	CMD_IS_SAFE},
817
818
819
820
821
822
823




824
825
826
827
828
829
830
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835







+
+
+
+







    /*
     * Register "clock" subcommands. These *do* go through
     * Tcl_CreateObjCommand, since they aren't in the global namespace and
     * involve ensembles.
     */

    TclClockInit(interp);

    /* Tip 479 - Register literals for the argsx command */
    TclProcInit(interp);


    /*
     * Register the built-in functions. This is empty now that they are
     * implemented as commands in the ::tcl::mathfunc namespace.
     */

    /*
7718
7719
7720
7721
7722
7723
7724
7725

7726
7727
7728
7729
7730
7731
7732
7723
7724
7725
7726
7727
7728
7729

7730
7731
7732
7733
7734
7735
7736
7737







-
+







	return TCL_ERROR;
    }

    if (!(iPtr->flags & RAND_SEED_INITIALIZED)) {
	iPtr->flags |= RAND_SEED_INITIALIZED;

	/*
	 * To ensure different seeds in different threads (bug #416643), 
	 * To ensure different seeds in different threads (bug #416643),
	 * take into consideration the thread this interp is running in.
	 */

	iPtr->randSeed = TclpGetClicks() + (PTR2INT(Tcl_GetCurrentThread())<<12);

	/*
	 * Make sure 1 <= randSeed <= (2^31) - 2. See below.

Changes to generic/tclCmdIL.c.

104
105
106
107
108
109
110


111
112
113
114
115
116
117
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119







+
+







 * Forward declarations for procedures defined in this file:
 */

static int		DictionaryCompare(const char *left, const char *right);
static Tcl_NRPostProc	IfConditionCallback;
static int		InfoArgsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoArgSpecCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoBodyCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCmdCountCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCommandsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCompleteCmd(ClientData dummy, Tcl_Interp *interp,
157
158
159
160
161
162
163

164
165
166
167
168
169
170
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173







+







/*
 * Array of values describing how to implement each standard subcommand of the
 * "info" command.
 */

static const EnsembleImplMap defaultInfoMap[] = {
    {"args",		   InfoArgsCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"argspec",		   InfoArgSpecCmd,	    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"body",		   InfoBodyCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"cmdcount",	   InfoCmdCountCmd,	    TclCompileBasic0ArgCmd, NULL, NULL, 0},
    {"commands",	   InfoCommandsCmd,	    TclCompileInfoCommandsCmd, NULL, NULL, 0},
    {"complete",	   InfoCompleteCmd,	    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"coroutine",	   TclInfoCoroutineCmd,     TclCompileInfoCoroutineCmd, NULL, NULL, 0},
    {"default",		   InfoDefaultCmd,	    TclCompileBasic3ArgCmd, NULL, NULL, 0},
    {"errorstack",	   InfoErrorStackCmd,	    TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
510
511
512
513
514
515
516


































































517
518
519
520
521
522
523
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    Tcl_ListObjAppendElement(interp, listObjPtr,
		    Tcl_NewStringObj(localPtr->name, -1));
	}
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * InfoArgspecCmd --
 *
 *	Called to implement the "info argspec" command that returns the argument
 *	list for a procedure. Handles the following syntax:
 *
 *	    info argspec procName
 *
 * Results:
 *	Returns TCL_OK if successful and TCL_ERROR if there is an error.
 *
 * Side effects:
 *	Returns a result in the interpreter's result object. If there is an
 *	error, the result is an error message.
 *
 *----------------------------------------------------------------------
 */

static int
InfoArgSpecCmd(
    ClientData dummy,		/* Not used. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    register Interp *iPtr = (Interp *) interp;
    const char *name;
    Proc *procPtr;
    CompiledLocal *localPtr;
    Tcl_Obj *resultObjPtr=NULL;

    if (objc != 1 && objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "procname");
	return TCL_ERROR;
    }
    if(objc==2) {
            name = TclGetString(objv[1]);
        procPtr = TclFindProc(iPtr, name);
    } else {
        procPtr = iPtr->framePtr->procPtr;
    }
    if (procPtr == NULL) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" isn't a procedure", name));
	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROCEDURE", name, NULL);
	return TCL_ERROR;
    }

    /*
     * Build a return list containing the arguments.
     */

    for (localPtr = procPtr->firstLocalPtr;  localPtr != NULL;
	    localPtr = localPtr->nextPtr) {
	if (localPtr->flags & VAR_IS_ARGS) {
	    if(localPtr->defValuePtr) {
  	        Tcl_IncrRefCount(localPtr->defValuePtr);
	        Tcl_SetObjResult(interp, localPtr->defValuePtr);
	    }
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * InfoBodyCmd --
 *
 *	Called to implement the "info body" command that returns the body for

Changes to generic/tclInt.h.

735
736
737
738
739
740
741

742
743
744
745
746
747
748
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749







+







	(VAR_TRACED_READ|VAR_TRACED_WRITE|VAR_TRACED_ARRAY|VAR_TRACED_UNSET)

/* Special handling on initialisation (only CompiledLocal). */
#define VAR_ARGUMENT		0x100	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_TEMPORARY		0x200	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_IS_ARGS		0x400
#define VAR_RESOLVED		0x8000
#define VAR_NAMED_ARGUMENT	0x10000

/*
 * Macros to ensure that various flag bits are set properly for variables.
 * The ANSI C "prototypes" for these macros are:
 *
 * MODULE_SCOPE void	TclSetVarScalar(Var *varPtr);
 * MODULE_SCOPE void	TclSetVarArray(Var *varPtr);
3279
3280
3281
3282
3283
3284
3285





3286
3287
3288
3289
3290
3291
3292
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298







+
+
+
+
+







			    Tcl_Obj *part2Ptr, int index, int pathc,
			    Tcl_Obj *const pathv[], Tcl_Obj *keysPtr);
MODULE_SCOPE Tcl_Obj *	TclDictWithInit(Tcl_Interp *interp, Tcl_Obj *dictPtr,
			    int pathc, Tcl_Obj *const pathv[]);
MODULE_SCOPE int	Tcl_DisassembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
/* Tip 479 */
MODULE_SCOPE int	TclProcInit(Tcl_Interp *interp);
MODULE_SCOPE int	Tcl_ArgsxObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);

/* Assemble command function */
MODULE_SCOPE int	Tcl_AssembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE int	TclNRAssembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,

Changes to generic/tclProc.c.

43
44
45
46
47
48
49

50
51
52
53
54
55
56
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57







+







static void		ProcBodyFree(Tcl_Obj *objPtr);
static int		ProcWrongNumArgs(Tcl_Interp *interp, int skip);
static void		MakeProcError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static void		MakeLambdaError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static int		SetLambdaFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static int              ProcEatArgs(Tcl_Interp *interp,CallFrame *framePtr,Tcl_Obj *argspec,Tcl_Obj *values);

static Tcl_NRPostProc ApplyNR2;
static Tcl_NRPostProc InterpProcNR2;
static Tcl_NRPostProc Uplevel_Callback;

/*
 * The ProcBodyObjType type
92
93
94
95
96
97
98



































































































































































































































99
100
101
102
103
104
105
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







const Tcl_ObjType tclLambdaType = {
    "lambdaExpr",		/* name */
    FreeLambdaInternalRep,	/* freeIntRepProc */
    DupLambdaInternalRep,	/* dupIntRepProc */
    NULL,			/* updateStringProc */
    SetLambdaFromAny		/* setFromAnyProc */
};

/* Literal Pool */
static Tcl_Obj *obj_const_aliases;
static Tcl_Obj *obj_const_args;
static Tcl_Obj *obj_const_default;
static Tcl_Obj *obj_const_mandatory;
static Tcl_Obj *obj_const_atspec;

int TclProcInit(Tcl_Interp *interp) {
  static int once=0;
  if(!once) {
    obj_const_aliases=Tcl_NewStringObj("aliases:",-1);
    Tcl_IncrRefCount(obj_const_aliases);
    obj_const_args=Tcl_NewStringObj("args",-1);
    Tcl_IncrRefCount(obj_const_args);
    obj_const_default=Tcl_NewStringObj("default:",-1);
    Tcl_IncrRefCount(obj_const_default);
    obj_const_mandatory=Tcl_NewStringObj("mandatory:",-1);
    Tcl_IncrRefCount(obj_const_mandatory);
    obj_const_atspec=Tcl_NewStringObj("argspec",-1);
    Tcl_IncrRefCount(obj_const_atspec);
    once=1;
  }
  return TCL_OK;
}

/*
** Code to support TIP 479
*/

/*
** TIP 479
** A simplified TclLookupSimpleVar
*/
/*
 * NOTE: VarHashCreateVar increments the recount of its key argument.
 * All callers that will call Tcl_DecrRefCount on that argument must
 * call Tcl_IncrRefCount on it before passing it in.  This requirement
 * can bubble up to callers of callers .... etc.
 */
#define VarHashGetValue(hPtr) \
    ((Var *) ((char *)hPtr - TclOffset(VarInHash, entry)))

static inline Var *
VarHashCreateVar(
    TclVarHashTable *tablePtr,
    Tcl_Obj *key,
    int *newPtr)
{
    Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(&tablePtr->table, key, newPtr);

    if (hPtr) {
	return VarHashGetValue(hPtr);
    } else {
	return NULL;
    }
}

void ProcSetLocalVar (
    CallFrame *framePtr,
    Tcl_Obj *varNamePtr, 	/* Scaler name */
    Tcl_Obj *valuePtr           /* Value to set */
) {
    int i, varLen, idx, isNew;
    const char *varName = TclGetStringFromObj(varNamePtr, &varLen);
    int localLen, localCt = framePtr->numCompiledLocals;
    Tcl_Obj **objPtrPtr = &framePtr->localCachePtr->varName0;
    const char *localNameStr;
    TclVarHashTable *tablePtr;	/* Points to the hashtable, if any, in which
				 * to look up the variable. */
    Var *varPtr;		/* Points to the Var structure returned for
				 * the variable. */
    varPtr = NULL;
    idx=-1;
    isNew=-1;
    for (i=0 ; i<localCt ; i++, objPtrPtr++) {
	register Tcl_Obj *objPtr = *objPtrPtr;

	if (objPtr) {
	    localNameStr = TclGetStringFromObj(objPtr, &localLen);
	    if ((varLen == localLen) && (varName[0] == localNameStr[0])
			&& !memcmp(varName, localNameStr, varLen)) {
		idx=i;
		varPtr=(Var *) &framePtr->compiledLocals[i];
		break;
	    }
	}
    }
    if(!varPtr) {
        tablePtr = framePtr->varTablePtr;
        if (tablePtr == NULL) {
            tablePtr = ckalloc(sizeof(TclVarHashTable));
            TclInitVarHashTable(tablePtr, NULL);
            framePtr->varTablePtr = tablePtr;
        }
        varPtr = VarHashCreateVar(tablePtr, varNamePtr, &isNew);
    }
    varPtr->flags=VAR_NAMED_ARGUMENT;
    varPtr->value.objPtr=valuePtr;
    Tcl_IncrRefCount(valuePtr);
}

int ProcEatArgs(
    Tcl_Interp *interp,
    CallFrame *framePtr,
    Tcl_Obj *argspec,
    Tcl_Obj *argsValuePtr
) {
    Tcl_DictSearch search;
    Tcl_Obj *fieldname, *fieldspec;
    int done=0;
    int isNew=0,idx=-1;
    if (Tcl_DictObjFirst(interp, argspec, &search,
        &fieldname, &fieldspec, &done) != TCL_OK) {
        return TCL_ERROR;
    }
    for (; !done ; Tcl_DictObjNext(&search, &fieldname, &fieldspec, &done)) {
        Tcl_Obj *iVal;
        Tcl_Obj *aliases,*defaultObj,*mandatoryObj;
        int mandatory=1;

        if(Tcl_DictObjGet(interp,argsValuePtr,fieldname,&iVal)!=TCL_OK) return TCL_ERROR;
        if(iVal) {
            ProcSetLocalVar(framePtr,fieldname,iVal);
            continue;
        }
        /* Fight for another day - Look through the aliases */
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_aliases,&aliases)!=TCL_OK) return TCL_ERROR;
        if(aliases) {
            Tcl_Obj **varv;
            int varc,vargi;
            if (Tcl_ListObjGetElements(interp, aliases, &varc, &varv) != TCL_OK) return TCL_ERROR;
            for(vargi=0;vargi<varc;vargi++) {
                if(Tcl_DictObjGet(interp,argsValuePtr,varv[vargi],&iVal)!=TCL_OK) return TCL_ERROR;
                if(iVal) {
                    ProcSetLocalVar(framePtr,fieldname,iVal);
                    break;
                }
            }
        }
        if(iVal) {
            continue;
        }
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_default,&defaultObj)!=TCL_OK) return TCL_ERROR;
        if(defaultObj) {
            ProcSetLocalVar(framePtr,fieldname,defaultObj);
            continue;
        }
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_mandatory,&mandatoryObj)!=TCL_OK) return TCL_ERROR;
        if(mandatoryObj) {
            if(Tcl_GetBooleanFromObj(interp,mandatoryObj,&mandatory)) return TCL_ERROR;
        }
        if(mandatory) {
            Tcl_AppendResult(interp,"Error: ", Tcl_GetString(fieldname), " is required", (char *)NULL);
            return TCL_ERROR;
        }
    }
    Tcl_DictObjDone(&search);
    return TCL_OK;
}

int Tcl_ArgsxObjCmd(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *CONST objv[]
) {
    Tcl_DictSearch search;
    Tcl_Obj *fieldname, *fieldspec, *argspec, *argdat;
    int done=0;

    if(objc!=3) {
        Tcl_WrongNumArgs(interp, 1, objv, "argspec argdat");
        return TCL_ERROR;
    }
    argspec=objv[1];
    argdat=objv[2];
    Tcl_ObjSetVar2(interp,obj_const_atspec,NULL,argspec,0);

    if (Tcl_DictObjFirst(interp, argspec, &search,
        &fieldname, &fieldspec, &done) != TCL_OK) {
        return TCL_ERROR;
    }
    for (; !done ; Tcl_DictObjNext(&search, &fieldname, &fieldspec, &done)) {
        Tcl_Obj *iVal;
        Tcl_Obj *aliases,*defaultObj,*mandatoryObj;
        int mandatory=1;
        if(Tcl_DictObjGet(interp,argdat,fieldname,&iVal)!=TCL_OK) return TCL_ERROR;
        if(iVal) {
            Tcl_ObjSetVar2(interp,fieldname,NULL,iVal,0);
            continue;
        }
        /* Fight for another day - Look through the aliases */
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_aliases,&aliases)!=TCL_OK) return TCL_ERROR;
        if(aliases) {
            Tcl_Obj **varv;
            int varc,vargi;
            if (Tcl_ListObjGetElements(interp, aliases, &varc, &varv) != TCL_OK) return TCL_ERROR;
            for(vargi=0;vargi<varc;vargi++) {
                if(Tcl_DictObjGet(interp,argdat,varv[vargi],&iVal)!=TCL_OK) return TCL_ERROR;
                if(iVal) {
                      Tcl_ObjSetVar2(interp,fieldname,NULL,iVal,0);
                      break;
                }
            }
        }
        if(iVal) {
            continue;
        }
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_default,&defaultObj)!=TCL_OK) return TCL_ERROR;
        if(defaultObj) {
            Tcl_ObjSetVar2(interp,fieldname,NULL,defaultObj,0);
            continue;
        }
        if(Tcl_DictObjGet(interp,fieldspec,obj_const_mandatory,&mandatoryObj)!=TCL_OK) return TCL_ERROR;
        if(mandatoryObj) {
            if(Tcl_GetBooleanFromObj(interp,mandatoryObj,&mandatory)) return TCL_ERROR;
        }
        if(mandatory) {
            Tcl_AppendResult(interp,"Error: ", Tcl_GetString(fieldname), " is required", (char *)NULL);
            return TCL_ERROR;
        }
    }
    Tcl_DictObjDone(&search);
    return TCL_OK;
}


/*
 *----------------------------------------------------------------------
 *
 * Tcl_ProcObjCmd --
 *
 *	This object-based function is invoked to process the "proc" Tcl
355
356
357
358
359
360
361

362
363
364
365
366
367
368
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597







+







	((Command *) cmd)->compileProc = TclCompileNoOp;
    }

  done:
    return TCL_OK;
}


/*
 *----------------------------------------------------------------------
 *
 * TclCreateProc --
 *
 *	Creates the data associated with a Tcl procedure definition. This
 *	function knows how to handle two types of body objects: strings and
472
473
474
475
476
477
478
479
480
481
482
483

484
485
486
487
488
489
490
701
702
703
704
705
706
707

708
709
710

711
712
713
714
715
716
717
718







-



-
+







     * Break up the argument list into argument specifiers, then process each
     * argument specifier. If the body is precompiled, processing is limited
     * to checking that the parsed argument is consistent with the one stored
     * in the Proc.
     *
     * THIS FAILS IF THE ARG LIST OBJECT'S STRING REP CONTAINS NULS.
     */

    args = TclGetStringFromObj(argsPtr, &length);
    result = Tcl_SplitList(interp, args, &numArgs, &argArray);
    if (result != TCL_OK) {
	goto procError;
        goto procError;
    }

    if (precompiled) {
	if (numArgs > procPtr->numArgs) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "procedure \"%s\": arg list contains %d entries, "
		    "precompiled header expects %d", procName, numArgs,
654
655
656
657
658
659
660













661
662












663

































664
665
666
667
668
669
670
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901


902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954







+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    }
	    memcpy(localPtr->name, fieldValues[0], nameLength + 1);
	    if ((i == numArgs - 1)
		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (strcmp(localPtr->name, "args") == 0)) {
		localPtr->flags |= VAR_IS_ARGS;
		if(fieldCount == 2) {
		    /*
		    ** TIP 479
		    ** Use the otherwise ignored default field for the last
		    ** parameter named args as a dict-based specification for
		    ** named parameters
		    */
		    Tcl_DictSearch search;
                    Tcl_Obj *fieldname, *fieldspec;
                    int done=0;
                    char *Fieldname;
                    int FieldLen;
                    Tcl_Obj *argspec;
	    }
	}

                    /*
                    ** Create entries in runtime procedure frame's array for
                    ** each named parameter
                    */
                    argspec=localPtr->defValuePtr;
                    if(Tcl_DictObjFirst(NULL, argspec, &search,&fieldname, &fieldspec, &done) != TCL_OK) goto procError;

                    for (; !done ; Tcl_DictObjNext(&search, &fieldname, &fieldspec, &done)) {
                        /* Allocate one slot for each of the other named parameters */
                        Tcl_Obj *defaultObj,*mandatoryObj;
                        int mandatory=1;

                        defaultObj=NULL;
                        mandatoryObj=NULL;
                        Tcl_DictObjGet(NULL,fieldspec,obj_const_default,&defaultObj);
                        Tcl_DictObjGet(NULL,fieldspec,obj_const_mandatory,&mandatoryObj);
                        if(mandatoryObj) {
                            if(Tcl_GetBooleanFromObj(interp,mandatoryObj,&mandatory)) {
                                mandatory=1;
                            }
                        }
                        /* Non-mandatory fields are not tracked */
                        if(!defaultObj && !mandatory) continue;

                        procPtr->numCompiledLocals++;
                        Fieldname=Tcl_GetStringFromObj(fieldname,&FieldLen);
     	                localPtr = ckalloc(TclOffset(CompiledLocal, name) + FieldLen+1);
        	        procPtr->lastLocalPtr->nextPtr = localPtr;
 		        procPtr->lastLocalPtr = localPtr;
 		     	memcpy(localPtr->name, Fieldname, FieldLen + 1);
                        localPtr->nextPtr = NULL;
                        localPtr->nameLength = FieldLen;
                        localPtr->frameIndex = i;
	                localPtr->flags = VAR_NAMED_ARGUMENT;
                        localPtr->resolveInfo = NULL;
                        if(defaultObj) {
                            localPtr->defValuePtr=defaultObj;
                            Tcl_IncrRefCount(localPtr->defValuePtr);
                        } else {
                            localPtr->defValuePtr=NULL;
                        }
		    }
		}
	    }
	}
	ckfree(fieldValues);
    }

    *procPtrPtr = procPtr;
    ckfree(argArray);
    return TCL_OK;

1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1434
1435
1436
1437
1438
1439
1440

1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453

1454
1455
1456
1457
1458
1459
1460







-













-







    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;

    bodyPtr = framePtr->procPtr->bodyPtr;
    if (bodyPtr->typePtr != &tclByteCodeType) {
	Tcl_Panic("body object for proc attached to frame is not a byte code type");
    }
    codePtr = bodyPtr->internalRep.twoPtrValue.ptr1;

    if (framePtr->numCompiledLocals) {
	if (!codePtr->localCachePtr) {
	    InitLocalCache(framePtr->procPtr) ;
	}
	framePtr->localCachePtr = codePtr->localCachePtr;
	framePtr->localCachePtr->refCount++;
    }

    InitResolvedLocals(interp, codePtr, varPtr, nsPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * InitResolvedLocals --
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1480
1481
1482
1483
1484
1485
1486

1487
1488
1489
1490
1491
1492
1493







-







    Namespace *nsPtr)		/* Pointer to current namespace. */
{
    Interp *iPtr = (Interp *) interp;
    int haveResolvers = (nsPtr->compiledVarResProc || iPtr->resolverPtr);
    CompiledLocal *firstLocalPtr, *localPtr;
    int varNum;
    Tcl_ResolvedVarInfo *resVarInfo;

    /*
     * Find the localPtr corresponding to varPtr
     */

    varNum = varPtr - iPtr->framePtr->compiledLocals;
    localPtr = iPtr->framePtr->procPtr->firstLocalPtr;
    while (varNum--) {
1231
1232
1233
1234
1235
1236
1237
1238

1239
1240
1241
1242
1243
1244
1245
1512
1513
1514
1515
1516
1517
1518

1519
1520
1521
1522
1523
1524
1525
1526







-
+







		ckfree(localPtr->resolveInfo);
	    }
	    localPtr->resolveInfo = NULL;
	}
	localPtr->flags &= ~VAR_RESOLVED;

	if (haveResolvers &&
		!(localPtr->flags & (VAR_ARGUMENT|VAR_TEMPORARY))) {
		!(localPtr->flags & (VAR_ARGUMENT|VAR_TEMPORARY|VAR_NAMED_ARGUMENT))) {
	    ResolverScheme *resPtr = iPtr->resolverPtr;
	    Tcl_ResolvedVarInfo *vinfo;
	    int result;

	    if (nsPtr->compiledVarResProc) {
		result = nsPtr->compiledVarResProc(nsPtr->interp,
			localPtr->name, localPtr->nameLength,
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1631
1632
1633
1634
1635
1636
1637

1638
1639
1640
1641
1642
1643
1644







-







	    *namePtr = NULL;
	} else {
	    *namePtr = TclCreateLiteral(iPtr, localPtr->name,
		    localPtr->nameLength, /* hash */ (unsigned int) -1,
		    &new, /* nsPtr */ NULL, 0, NULL);
	    Tcl_IncrRefCount(*namePtr);
	}

	if (i < numArgs) {
	    varPtr->flags = (localPtr->flags & VAR_IS_ARGS);
	    varPtr->value.objPtr = localPtr->defValuePtr;
	    varPtr++;
	    i++;
	}
	namePtr++;
1399
1400
1401
1402
1403
1404
1405
1406

1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1679
1680
1681
1682
1683
1684
1685

1686
1687
1688
1689
1690

1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706

1707
1708
1709
1710
1711
1712
1713







-
+




-
















-







{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    register Proc *procPtr = framePtr->procPtr;
    ByteCode *codePtr = procPtr->bodyPtr->internalRep.twoPtrValue.ptr1;
    register Var *varPtr, *defPtr;
    int localCt = procPtr->numCompiledLocals, numArgs, argCt, i, imax;
    Tcl_Obj *const *argObjs;

    Tcl_Obj *argSpec=NULL,*argsValuePtr=NULL;
    /*
     * Make sure that the local cache of variable names and initial values has
     * been initialised properly .
     */

    if (localCt) {
	if (!codePtr->localCachePtr) {
	    InitLocalCache(procPtr) ;
	}
	framePtr->localCachePtr = codePtr->localCachePtr;
	framePtr->localCachePtr->refCount++;
	defPtr = (Var *) (&framePtr->localCachePtr->varName0 + localCt);
    } else {
	defPtr = NULL;
    }

    /*
     * Create the "compiledLocals" array. Make sure it is large enough to hold
     * all the procedure's compiled local variables, including its formal
     * parameters.
     */

    varPtr = TclStackAlloc(interp, (int)(localCt * sizeof(Var)));
    framePtr->compiledLocals = varPtr;
    framePtr->numCompiledLocals = localCt;

    /*
     * Match and assign the call's actual parameters to the procedure's formal
     * arguments. The formal arguments are described by the first numArgs
1480
1481
1482
1483
1484
1485
1486





1487

1488
1489
1490



1491
1492
1493
1494
1495
1496
1497
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769

1770



1771
1772
1773
1774
1775
1776
1777
1778
1779
1780







+
+
+
+
+
-
+
-
-
-
+
+
+







    /*
     * When we get here, the last formal argument remains to be defined:
     * defPtr and varPtr point to the last argument to be initialized.
     */

    varPtr->flags = 0;
    if (defPtr && defPtr->flags & VAR_IS_ARGS) {
        /*
        ** Build a conventional list of arguments
        ** past the last positional argument, and call it
        ** args
        */
	Tcl_Obj *listPtr = Tcl_NewListObj(argCt-i, argObjs+i);
        argsValuePtr = Tcl_NewListObj(argCt-i, argObjs+i);

	varPtr->value.objPtr = listPtr;
	Tcl_IncrRefCount(listPtr);	/* Local var is a reference. */
        varPtr->value.objPtr = argsValuePtr;
        argSpec=defPtr->value.objPtr;
        Tcl_IncrRefCount(argsValuePtr);	/* Local var is a reference. */
    } else if (argCt == numArgs) {
	Tcl_Obj *objPtr = argObjs[i];

	varPtr->value.objPtr = objPtr;
	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */
    } else if ((argCt < numArgs) && defPtr && defPtr->value.objPtr) {
	Tcl_Obj *objPtr = defPtr->value.objPtr;
1513
1514
1515
1516
1517
1518
1519









1520

1521
1522
1523
1524
1525
1526
1527
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811

1812
1813
1814
1815
1816
1817
1818
1819







+
+
+
+
+
+
+
+
+
-
+







	if (!framePtr->nsPtr->compiledVarResProc
		&& !((Interp *)interp)->resolverPtr) {
	    memset(varPtr, 0, (localCt - numArgs)*sizeof(Var));
	} else {
	    InitResolvedLocals(interp, codePtr, varPtr, framePtr->nsPtr);
	}
    }
    if(argSpec) {
        /*
        ** TIP 479
        ** Feed dict values into compiled locals
        ** If a required parameter is missing ProcEatArgs
        ** will return TCL_ERROR.
        */
        if(ProcEatArgs(interp,framePtr,argSpec,argsValuePtr)
           != TCL_OK) goto incorrectArgs;

    }
    return TCL_OK;

    /*
     * Initialise all compiled locals to avoid problems at DeleteLocalVars.
     */

  incorrectArgs:
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2281
2282
2283
2284
2285
2286
2287

2288
2289
2290
2291
2292
2293
2294







-







	 *
	 * TRICKY NOTE: Be careful to push a call frame with the proper
	 *   namespace context, so that the byte codes are compiled in the
	 *   appropriate class context.
	 */

	iPtr->compiledProcPtr = procPtr;

	if (procPtr->numCompiledLocals > procPtr->numArgs) {
	    CompiledLocal *clPtr = procPtr->firstLocalPtr;
	    CompiledLocal *lastPtr = NULL;
	    int i, numArgs = procPtr->numArgs;

	    for (i = 0; i < numArgs; i++) {
		lastPtr = clPtr;
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2312
2313
2314
2315
2316
2317
2318

2319
2320
2321
2322
2323
2324
2325







-







			ckfree(toFree->resolveInfo);
		    }
		}
		ckfree(toFree);
	    }
	    procPtr->numCompiledLocals = procPtr->numArgs;
	}

	(void) TclPushStackFrame(interp, &framePtr, (Tcl_Namespace *) nsPtr,
		/* isProcCallFrame */ 0);

	/*
	 * TIP #280: We get the invoking context from the cmdFrame which
	 * was saved by 'Tcl_ProcObjCmd' (using linePBodyPtr).
	 */

Changes to library/init.tcl.

832
833
834
835
836
837
838






832
833
834
835
836
837
838
839
840
841
842
843
844







+
+
+
+
+
+
    foreach s [lsort -unique $filelist] {
	if {[file tail $s] ni {. ..}} {
	    file copy -force -- $s [file join $dest [file tail $s]]
	}
    }
    return
}

proc procx {name argspec body} { proc $name [list [list args $argspec]] $body }
proc ::oo::define::methodx {name argspec body} {
  set class [lindex [::info level -1] 1]
  oo::define $class method $name [list [list args $argspec]] $body
}

Changes to tests/proc.test.

379
380
381
382
383
384
385
386


387
388
389





































































































390
391
392
393
394
395
396
379
380
381
382
383
384
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498







-
+
+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    set lambda x
    lappend lambda {set a 1}
    interp create slave
    slave eval [list apply $lambda foo]
    interp delete slave
    unset lambda
} {}



# cleanup
catch {rename p ""}
catch {rename t ""}


test proc-8.0 {Named parameters tip-479 map named parameters to local vars} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [list $color $flavor]
    }
    p
} -result {red strawberry} -cleanup {
  rename p ""
}

test proc-8.1 {Named parameters tip-479 map named parameters to args} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return $args
    }
    p
} -result {} -cleanup {
  rename p ""
}

test proc-8.2 {Named parameters tip-479 present in info locals} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [info locals]
    }
    p
} -result {args flavor color} -cleanup {
  rename p ""
}

test proc-8.3 {Named parameters tip-479 fail on lack of a named parameter} -body {
    proc p {{args {color {} flavor {default: strawberry}}}} {
       return $args
    }
    p
} -returnCodes 1 -result {wrong # args: should be "p ?args?"} -cleanup {
  rename p ""
}

test proc-8.4 {Named parameters tip-479 accept additional named parameters} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return $args
    }
    p shape oval
} -result {shape oval} -cleanup {
  rename p ""
}

test proc-8.5 {Named parameters tip-479 do not map fields outside of the spec} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [info locals]
    }
    p shape oval
} -result {args flavor color} -cleanup {
  rename p ""
}

test proc-8.6 {Named parameters tip-479 non-manditory fields are not mapped} -body {
    proc p {{args {color {default: red} flavor {mandatory: 0}}}} {
       return [info locals]
    }
    p
} -result {args color} -cleanup {
  rename p ""
}

test proc-8.7 {Named parameters tip-479 named parameters can be in any order} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [list $color $flavor]
    }
    p flavor cherry color blue
} -result {blue cherry} -cleanup {
  rename p ""
}

test proc-8.8 {Named parameters tip-479 order of parameters preserved in args} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return $args
    }
    p flavor cherry color blue
} -result {flavor cherry color blue} -cleanup {
  rename p ""
}

test proc-8.9 {Named parameters tip-479 spec available in [info argspec]} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [info argspec]
    }
    info argspec p
} -result {color {default: red} flavor {default: strawberry}} -cleanup {
  rename p ""
}

test proc-8.10 {Named parameters tip-479 spec available in [info argspec]} -body {
    proc p {{args {color {default: red} flavor {default: strawberry}}}} {
       return [info argspec]
    }
    p
} -result {color {default: red} flavor {default: strawberry}} -cleanup {
  rename p ""
}

::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# fill-column: 78
# End: