Diff
Not logged in

Differences From Artifact [2f5ac5a80b]:

To Artifact [ab52d497ba]:


405
406
407
408
409
410
411







412
413
414
415
416
417
418
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425







+
+
+
+
+
+
+







    Interp *iPtr = (Interp *) interp;

    Proc *procPtr = NULL;
    Tcl_Size i, numArgs;
    CompiledLocal *localPtr = NULL;
    Tcl_Obj **argArray;
    int precompiled = 0, result;
    /*
     * To report on bad arglists:
     *  - set to 1 when 0 and optional/args is found
     *  - set to 2 when 1 and required is found
     *  - error when 2 and optional/args is found
     */
    int arglistShape = 0, isArgs, seenArgs = 0;

    ProcGetInternalRep(bodyPtr, procPtr);
    if (procPtr != NULL) {
	/*
	 * Because the body is a TclProProcBody, the actual body is already
	 * compiled, and it is not shared with anyone else, so it's OK not to
	 * unshare it (as a matter of fact, it is bad to unshare it, because
535
536
537
538
539
540
541




























542
543
544
545
546
547
548
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







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







		    "argument with no name", -1));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
		    "FORMALARGUMENTFORMAT", NULL);
	    goto procError;
	}

	argname = Tcl_GetStringFromObj(fieldValues[0], &nameLength);
	isArgs = (nameLength == 4) && !strcmp(argname, "args");

	/*
	 * Reject invalid argspecs early
	 */
	if (fieldCount == 2 || isArgs) {
	    if (isArgs && seenArgs) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(
			"repeated \"args\" in argument list", -1));
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"FORMALARGUMENTFORMAT", NULL);
		goto procError;
	    }
	    seenArgs = seenArgs || isArgs;
	    if (arglistShape == 0) {
		arglistShape = 1;
	    } else if (arglistShape == 2) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(
			"required arg may not be in the middle", -1));
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"FORMALARGUMENTFORMAT", NULL);
		goto procError;
	    }
	} else {
	    if (arglistShape == 1) {
		arglistShape = 2;
	    }
	}

	/*
	 * Check that the formal parameter name is a scalar.
	 */

	argnamei = argname;
	argnamelast = (nameLength > 0) ? (argname + nameLength - 1) : argname;
614
615
616
617
618
619
620
621

622
623
624
625
626
627
628
629
630
631
649
650
651
652
653
654
655

656



657
658
659
660
661
662
663







-
+
-
-
-







			"default value inconsistent with precompiled body", -1);
		    Tcl_SetObjResult(interp, errorObj);
		    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			    "BYTECODELIES", NULL);
		    goto procError;
		}
	    }
	    if ((i == numArgs - 1)
	    if (isArgs) {
		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (strcmp(localPtr->name, "args") == 0)) {
		localPtr->flags |= VAR_IS_ARGS;
	    }

	    localPtr = localPtr->nextPtr;
	} else {
	    /*
	     * Allocate an entry in the runtime procedure frame's array of
649
650
651
652
653
654
655
656

657
658
659
660
661
662
663
664
665
666
681
682
683
684
685
686
687

688



689
690
691
692
693
694
695







-
+
-
-
-







	    if (fieldCount == 2) {
		localPtr->defValuePtr = fieldValues[1];
		Tcl_IncrRefCount(localPtr->defValuePtr);
	    } else {
		localPtr->defValuePtr = NULL;
	    }
	    memcpy(localPtr->name, argname, fieldValues[0]->length + 1);
	    if ((i == numArgs - 1)
	    if (isArgs) {
		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (memcmp(localPtr->name, "args", 4) == 0)) {
		localPtr->flags |= VAR_IS_ARGS;
	    }
	}
    }

    *procPtrPtr = procPtr;
    return TCL_OK;
1061
1062
1063
1064
1065
1066
1067
1068

1069
1070
1071
1072
1073
1074
1075
1076
1077
1078

1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090

1091
1092
1093
1094
1095
1096
1097

1098
1099
1100
1101









1102
1103
1104
1105

1106
1107
1108
1109
1110

1111
1112

1113
1114
1115
1116
1117
1118
1119
1090
1091
1092
1093
1094
1095
1096

1097
1098
1099
1100
1101
1102
1103
1104
1105
1106

1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118

1119
1120
1121
1122
1123
1124
1125
1126
1127




1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139

1140
1141
1142
1143
1144

1145
1146

1147
1148
1149
1150
1151
1152
1153
1154







-
+









-
+











-
+







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



-
+




-
+

-
+







static int
ProcWrongNumArgs(
    Tcl_Interp *interp,
    int skip)
{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    Proc *procPtr = framePtr->procPtr;
    int localCt = procPtr->numCompiledLocals, numArgs, i;
    int localCt = procPtr->numCompiledLocals, numArgs, i, i2 = 1;
    Tcl_Obj **desiredObjs;
    const char *final = NULL;

    /*
     * Build up desired argument list for Tcl_WrongNumArgs
     */

    numArgs = framePtr->procPtr->numArgs;
    desiredObjs = (Tcl_Obj **)TclStackAlloc(interp,
	    sizeof(Tcl_Obj *) * (numArgs+1));
	    sizeof(Tcl_Obj *) * (numArgs+2));

    if (framePtr->isProcCallFrame & FRAME_IS_LAMBDA) {
	desiredObjs[0] = Tcl_NewStringObj("lambdaExpr", -1);
    } else {
	desiredObjs[0] = framePtr->objv[skip-1];
    }
    Tcl_IncrRefCount(desiredObjs[0]);

    if (localCt > 0) {
	Var *defPtr = (Var *)(&framePtr->localCachePtr->varName0 + localCt);

	for (i=1 ; i<=numArgs ; i++, defPtr++) {
	for (i=i2=1 ; i<=numArgs ; i++, i2++, defPtr++) {
	    Tcl_Obj *argObj;
	    Tcl_Obj *namePtr = localName(framePtr, i-1);

	    if (defPtr->value.objPtr != NULL) {
		TclNewObj(argObj);
		Tcl_AppendStringsToObj(argObj, "?", TclGetString(namePtr), "?", NULL);
	    } else if (defPtr->flags & VAR_IS_ARGS) {
		/*
		numArgs--;
		final = "?arg ...?";
		break;
	    } else {
		 * Work around the list quoting in WrongNumArgs which we
		 * do not want for ?arg ...?.
		 */

	        TclNewLiteralStringObj(argObj, "?arg");
		desiredObjs[i2] = argObj;
		i2++;
	        TclNewLiteralStringObj(argObj, "...?");
            } else {
		argObj = namePtr;
		Tcl_IncrRefCount(namePtr);
	    }
	    desiredObjs[i] = argObj;
	    desiredObjs[i2] = argObj;
	}
    }

    Tcl_ResetResult(interp);
    Tcl_WrongNumArgs(interp, numArgs+1, desiredObjs, final);
    Tcl_WrongNumArgs(interp, i2, desiredObjs, final);

    for (i=0 ; i<=numArgs ; i++) {
    for (i=0 ; i<i2 ; i++) {
	Tcl_DecrRefCount(desiredObjs[i]);
    }
    TclStackFree(interp, desiredObjs);
    return TCL_ERROR;
}

/*
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350









1351
1352
1353
1354
1355
1356
1357
1358
1359

1360
1361
1362
1363
1364
1365


1366
1367


1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378





1379
1380
1381
1382
1383
1384
1385
1386
1387
1388

1389
1390
1391
1392
1393
1394
1395
1396
1397
1398


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


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
1461
1462

1463
1464

1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478



1479
1480


1481
1482
1483
1484
1485
1486
1487
1376
1377
1378
1379
1380
1381
1382



1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
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
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
1461
1462
1463
1464
1465




1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480

1481




1482
1483
1484
1485
1486

1487

1488
1489
1490
1491
1492
1493
1494
1495



1496
1497
1498
1499
1500
1501


1502
1503






1504
1505
1506
1507
1508
1509
1510






1511
1512
1513
1514
1515
1516
1517
1518
1519


1520
1521

1522
1523
1524
1525
1526
1527
1528
1529

1530
1531
1532

1533
1534

1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552


1553
1554
1555
1556
1557
1558
1559
1560
1561







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








-
+





-
+
+

-
+
+








-
-
-
+
+
+
+
+








-
-
+
-







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

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

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

-
+
-

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

+
-
-
+
+
-
+







-
+


-
+

-
+














+
+
+
-
-
+
+







				 * invoked. */
    int skip)			/* Number of initial arguments to be skipped,
				 * i.e., words in the "command name". */
{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    Proc *procPtr = framePtr->procPtr;
    ByteCode *codePtr;
    Var *varPtr, *defPtr;
    int localCt = procPtr->numCompiledLocals, numArgs, argCt, i, imax;
    Tcl_Obj *const *argObjs;
    Var *nextVarPtr, *lastVarPtr, *localVarPtr, *nextDefPtr, *lastDefPtr;
    /* Total compiled locals, >= numArgs */
    int numLocals = procPtr->numCompiledLocals;
    /* Number of arguments taken */
    int numArgs = procPtr->numArgs;
    /* Number of arguments given */
    int argCt = framePtr->objc - skip;
    Tcl_Obj *const *nextArgObj;
    Tcl_Obj *const *lastArgObj;

    ByteCodeGetInternalRep(procPtr->bodyPtr, &tclByteCodeType, codePtr);

    /*
     * Make sure that the local cache of variable names and initial values has
     * been initialised properly .
     */

    if (localCt) {
    if (numLocals) {
	if (!codePtr->localCachePtr) {
	    InitLocalCache(procPtr) ;
	}
	framePtr->localCachePtr = codePtr->localCachePtr;
	framePtr->localCachePtr->refCount++;
	defPtr = (Var *) (&framePtr->localCachePtr->varName0 + localCt);
	nextDefPtr = (Var *) (&framePtr->localCachePtr->varName0 + numLocals);
	lastDefPtr = &nextDefPtr[numArgs-1];
    } else {
	defPtr = NULL;
	nextDefPtr = NULL;
	lastDefPtr = 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 = (Var *)TclStackAlloc(interp, localCt * sizeof(Var));
    framePtr->compiledLocals = varPtr;
    framePtr->numCompiledLocals = localCt;
    nextVarPtr = (Var *)TclStackAlloc(interp, numLocals * sizeof(Var));
    lastVarPtr = &nextVarPtr[numArgs-1];
    localVarPtr = &nextVarPtr[numArgs];
    framePtr->compiledLocals = nextVarPtr;
    framePtr->numCompiledLocals = numLocals;

    /*
     * 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 the call
     * frame's local variable array.
     */

    numArgs = procPtr->numArgs;
    argCt = framePtr->objc - skip;	/* Set it to the number of args to the
    nextArgObj = framePtr->objv + skip;
					 * procedure. */
    if (numArgs == 0) {
	if (argCt) {
	    goto incorrectArgs;
	} else {
	    goto correctArgs;
	}
    }
    argObjs = framePtr->objv + skip;
    imax = ((argCt < numArgs-1) ? argCt : numArgs-1);
    lastArgObj = &nextArgObj[argCt-1];

    for (i = 0; i < imax; i++, varPtr++, defPtr ? defPtr++ : defPtr) {
	/*
    /*
	 * "Normal" arguments; last formal is special, depends on it being
	 * 'args'.
	 */

	Tcl_Obj *objPtr = argObjs[i];

	varPtr->flags = 0;
	varPtr->value.objPtr = objPtr;
	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */
    }
    for (; i < numArgs-1; i++, varPtr++, defPtr ? defPtr++ : defPtr) {
	/*
	 * This loop is entered if argCt < (numArgs-1). Set default values;
     * Required args, LHS
     */
    while ( (nextVarPtr <= lastVarPtr)
	 && !(nextDefPtr->flags & VAR_IS_ARGS)
	 && (nextDefPtr->value.objPtr == NULL) ) {
	if (nextArgObj > lastArgObj) goto incorrectArgs;	/* not enough args */
	nextVarPtr->flags = 0;
	nextVarPtr->value.objPtr = *(nextArgObj++);
	Tcl_IncrRefCount(nextVarPtr->value.objPtr);	/* Local var is a reference. */

	++nextVarPtr; ++nextDefPtr;
    }
    /*
     * Required args, RHS
	 * last formal is special.
	 */
     */
    while ( (nextVarPtr <= lastVarPtr)
	 && !(lastDefPtr->flags & VAR_IS_ARGS)
	 && (lastDefPtr->value.objPtr == NULL) ) {
	if (nextArgObj > lastArgObj) goto incorrectArgs;	/* not enough args */
	lastVarPtr->flags = 0;
	lastVarPtr->value.objPtr = *(lastArgObj--);
	Tcl_IncrRefCount(lastVarPtr->value.objPtr);	/* Local var is a reference. */

	Tcl_Obj *objPtr = defPtr ? defPtr->value.objPtr : NULL;

	if (!objPtr) {
	    goto incorrectArgs;
	--lastVarPtr; --lastDefPtr;
    }
    /*
     * Optional args, LHS
     */
    while ( (nextVarPtr <= lastVarPtr)
	 && !(nextDefPtr->flags & VAR_IS_ARGS) ) {
	Tcl_Obj * objPtr;
	if (nextArgObj > lastArgObj) {
	    objPtr = nextDefPtr->value.objPtr;	/* take default */
	} else {
	    objPtr = *(nextArgObj++);
	}
	if (objPtr == NULL) Tcl_Panic("oops LHS!");
	nextVarPtr->value.objPtr = objPtr;
	varPtr->flags = 0;
	nextVarPtr->flags = 0;
	varPtr->value.objPtr = objPtr;
	Tcl_IncrRefCount(objPtr);	/* Local var reference. */
    }

	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */

	++nextVarPtr; ++nextDefPtr;
    }
    /*
     * When we get here, the last formal argument remains to be defined:
     * Optional args, RHS
     * defPtr and varPtr point to the last argument to be initialized.
     */
    while ( (nextVarPtr <= lastVarPtr)
	 && !(lastDefPtr->flags & VAR_IS_ARGS) ) {
	Tcl_Obj * objPtr;
	if (nextArgObj > lastArgObj) {
	    objPtr = lastDefPtr->value.objPtr;	/* take default */
	} else {
	    objPtr = *(lastArgObj--);

    varPtr->flags = 0;
    if (defPtr && defPtr->flags & VAR_IS_ARGS) {
	}
	if (objPtr == NULL) Tcl_Panic("oops RHS!");
	lastVarPtr->value.objPtr = objPtr;
	lastVarPtr->flags = 0;
	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */

	Tcl_Obj *listPtr = Tcl_NewListObj((argCt>i)? argCt-i : 0, argObjs+i);

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

	varPtr->value.objPtr = objPtr;
    /*
     * Args?
     */
    if (nextVarPtr < lastVarPtr) {
	Tcl_Panic("nextVarPtr < lastVarPtr!\n");
    }
    if (nextVarPtr == lastVarPtr) {
	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */
    } else if ((argCt < numArgs) && defPtr && defPtr->value.objPtr) {
	Tcl_Obj *objPtr = defPtr->value.objPtr;

	varPtr->value.objPtr = objPtr;
	Tcl_IncrRefCount(objPtr);	/* Local var is a reference. */
	if (!(nextDefPtr->flags & VAR_IS_ARGS)) {
	    goto incorrectArgs;
	}
	Tcl_Obj *listPtr = Tcl_NewListObj(1+lastArgObj-nextArgObj, nextArgObj);
	nextVarPtr->value.objPtr = listPtr;
	nextVarPtr->flags = 0;
	Tcl_IncrRefCount(listPtr);	/* Local var is a reference. */
    } else {
	if (nextArgObj <= lastArgObj) {
	goto incorrectArgs;
    }
	    goto incorrectArgs;
	}
    varPtr++;
    }

    /*
     * Initialise and resolve the remaining compiledLocals. In the absence of
     * resolvers, they are undefined local vars: (flags=0, value=NULL).
     */

  correctArgs:
    if (numArgs < localCt) {
    if (numArgs < numLocals) {
	if (!framePtr->nsPtr->compiledVarResProc
		&& !((Interp *)interp)->resolverPtr) {
	    memset(varPtr, 0, (localCt - numArgs)*sizeof(Var));
	    memset(localVarPtr, 0, (numLocals - numArgs)*sizeof(Var));
	} else {
	    InitResolvedLocals(interp, codePtr, varPtr, framePtr->nsPtr);
	    InitResolvedLocals(interp, codePtr, localVarPtr, framePtr->nsPtr);
	}
    }

    return TCL_OK;

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

  incorrectArgs:
    if ((skip != 1) &&
	    TclInitRewriteEnsemble(interp, skip-1, 0, framePtr->objv)) {
	TclNRAddCallback(interp, TclClearRootEnsemble, NULL, NULL, NULL, NULL);
    }
    /*
     * Ensure all un-assigned vars are zeroed
     */
    memset(varPtr, 0,
	    ((framePtr->compiledLocals + localCt)-varPtr) * sizeof(Var));
    memset(nextVarPtr, 0,
	    ((framePtr->compiledLocals + numLocals)-nextVarPtr) * sizeof(Var));
    return ProcWrongNumArgs(interp, skip);
}

/*
 *----------------------------------------------------------------------
 *
 * TclPushProcCallFrame --