diff options
Diffstat (limited to 'generic/tclBasic.c')
| -rw-r--r-- | generic/tclBasic.c | 1595 | 
1 files changed, 793 insertions, 802 deletions
| diff --git a/generic/tclBasic.c b/generic/tclBasic.c index 2da455b..2a334c4 100644 --- a/generic/tclBasic.c +++ b/generic/tclBasic.c @@ -81,8 +81,6 @@ TCL_DECLARE_MUTEX(cancelLock)   * are used to save the evaluation state between NR calls to each coro.   */ -static const CorContext NULL_CONTEXT = {NULL, NULL, NULL, NULL}; -  #define SAVE_CONTEXT(context)				\      (context).framePtr = iPtr->framePtr;		\      (context).varFramePtr = iPtr->varFramePtr;		\ @@ -129,15 +127,12 @@ static Tcl_ObjCmdProc	ExprSqrtFunc;  static Tcl_ObjCmdProc	ExprSrandFunc;  static Tcl_ObjCmdProc	ExprUnaryFunc;  static Tcl_ObjCmdProc	ExprWideFunc; -static Tcl_Obj *	GetCommandSource(Interp *iPtr, int objc, -			    Tcl_Obj *const objv[], int lookup);  static void		MathFuncWrongNumArgs(Tcl_Interp *interp, int expected,  			    int actual, Tcl_Obj *const *objv); -static Tcl_NRPostProc	NRCoroutineActivateCallback;  static Tcl_NRPostProc	NRCoroutineCallerCallback;  static Tcl_NRPostProc	NRCoroutineExitCallback; -static Tcl_NRPostProc	NRRunObjProc; -static Tcl_NRPostProc	NRTailcallEval; +static int NRCommand(ClientData data[], Tcl_Interp *interp, int result); +  static Tcl_ObjCmdProc	OldMathFuncProc;  static void		OldMathFuncDeleteProc(ClientData clientData);  static void		ProcessUnexpectedResult(Tcl_Interp *interp, @@ -151,8 +146,8 @@ static inline Command *	TEOV_LookupCmdFromObj(Tcl_Interp *interp,  static int		TEOV_NotFound(Tcl_Interp *interp, int objc,  			    Tcl_Obj *const objv[], Namespace *lookupNsPtr);  static int		TEOV_RunEnterTraces(Tcl_Interp *interp, -			    Command **cmdPtrPtr, int objc, -			    Tcl_Obj *const objv[], Namespace *lookupNsPtr); +			    Command **cmdPtrPtr, Tcl_Obj *commandPtr, int objc, +			    Tcl_Obj *const objv[]);  static Tcl_NRPostProc	RewindCoroutineCallback;  static Tcl_NRPostProc	TailcallCleanup;  static Tcl_NRPostProc	TEOEx_ByteCodeCallback; @@ -162,11 +157,11 @@ static Tcl_NRPostProc	TEOV_Exception;  static Tcl_NRPostProc	TEOV_NotFoundCallback;  static Tcl_NRPostProc	TEOV_RestoreVarFrame;  static Tcl_NRPostProc	TEOV_RunLeaveTraces; -static Tcl_NRPostProc	YieldToCallback; +static Tcl_NRPostProc	EvalObjvCore; +static Tcl_NRPostProc	Dispatch; -static void	        ClearTailcall(Tcl_Interp *interp, -			    struct NRE_callback *tailcallPtr);  static Tcl_ObjCmdProc NRCoroInjectObjCmd; +static Tcl_NRPostProc NRPostInvoke;  MODULE_SCOPE const TclStubs tclStubs; @@ -190,11 +185,16 @@ typedef struct {      Tcl_ObjCmdProc *objProc;	/* Object-based function for command. */      CompileProc *compileProc;	/* Function called to compile command. */      Tcl_ObjCmdProc *nreProc;	/* NR-based function for command */ -    int isSafe;			/* If non-zero, command will be present in -				 * safe interpreter. Otherwise it will be -				 * hidden. */ +    int flags;			/* Various flag bits, as defined below. */  } CmdInfo; +#define CMD_IS_SAFE         1   /* Whether this command is part of the set of +                                 * commands present by default in a safe +                                 * interpreter. */ +/* CMD_COMPILES_EXPANDED - Whether the compiler for this command can handle + * expansion for itself rather than needing the generic layer to take care of + * it for it. Defined in tclInt.h. */ +  /*   * The built-in commands, and the functions that implement them:   */ @@ -204,93 +204,95 @@ static const CmdInfo builtInCmds[] = {       * Commands in the generic core.       */ -    {"append",		Tcl_AppendObjCmd,	TclCompileAppendCmd,	NULL,	1}, -    {"apply",		Tcl_ApplyObjCmd,	NULL,			TclNRApplyObjCmd,	1}, -    {"break",		Tcl_BreakObjCmd,	TclCompileBreakCmd,	NULL,	1}, +    {"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 EXCLUDE_OBSOLETE_COMMANDS -    {"case",		Tcl_CaseObjCmd,		NULL,			NULL,	1}, +    {"case",		Tcl_CaseObjCmd,		NULL,			NULL,	CMD_IS_SAFE},  #endif -    {"catch",		Tcl_CatchObjCmd,	TclCompileCatchCmd,	TclNRCatchObjCmd,	1}, -    {"concat",		Tcl_ConcatObjCmd,	NULL,			NULL,	1}, -    {"continue",	Tcl_ContinueObjCmd,	TclCompileContinueCmd,	NULL,	1}, -    {"coroutine",	NULL,			NULL,			TclNRCoroutineObjCmd,	1}, -    {"error",		Tcl_ErrorObjCmd,	TclCompileErrorCmd,	NULL,	1}, -    {"eval",		Tcl_EvalObjCmd,		NULL,			NULL,	1}, -    {"expr",		Tcl_ExprObjCmd,		TclCompileExprCmd,	TclNRExprObjCmd,	1}, -    {"for",		Tcl_ForObjCmd,		TclCompileForCmd,	TclNRForObjCmd,	1}, -    {"foreach",		Tcl_ForeachObjCmd,	TclCompileForeachCmd,	TclNRForeachCmd,	1}, -    {"format",		Tcl_FormatObjCmd,	NULL,			NULL,	1}, -    {"global",		Tcl_GlobalObjCmd,	TclCompileGlobalCmd,	NULL,	1}, -    {"if",		Tcl_IfObjCmd,		TclCompileIfCmd,	TclNRIfObjCmd,	1}, -    {"incr",		Tcl_IncrObjCmd,		TclCompileIncrCmd,	NULL,	1}, -    {"join",		Tcl_JoinObjCmd,		NULL,			NULL,	1}, -    {"lappend",		Tcl_LappendObjCmd,	TclCompileLappendCmd,	NULL,	1}, -    {"lassign",		Tcl_LassignObjCmd,	TclCompileLassignCmd,	NULL,	1}, -    {"lindex",		Tcl_LindexObjCmd,	TclCompileLindexCmd,	NULL,	1}, -    {"linsert",		Tcl_LinsertObjCmd,	NULL,			NULL,	1}, -    {"list",		Tcl_ListObjCmd,		TclCompileListCmd,	NULL,	1}, -    {"llength",		Tcl_LlengthObjCmd,	TclCompileLlengthCmd,	NULL,	1}, -    {"lrange",		Tcl_LrangeObjCmd,	NULL,			NULL,	1}, -    {"lrepeat",		Tcl_LrepeatObjCmd,	NULL,			NULL,	1}, -    {"lreplace",	Tcl_LreplaceObjCmd,	NULL,			NULL,	1}, -    {"lreverse",	Tcl_LreverseObjCmd,	NULL,			NULL,	1}, -    {"lsearch",		Tcl_LsearchObjCmd,	NULL,			NULL,	1}, -    {"lset",		Tcl_LsetObjCmd,		TclCompileLsetCmd,	NULL,	1}, -    {"lsort",		Tcl_LsortObjCmd,	NULL,			NULL,	1}, -    {"package",		Tcl_PackageObjCmd,	NULL,			NULL,	1}, -    {"proc",		Tcl_ProcObjCmd,		NULL,			NULL,	1}, -    {"regexp",		Tcl_RegexpObjCmd,	TclCompileRegexpCmd,	NULL,	1}, -    {"regsub",		Tcl_RegsubObjCmd,	NULL,			NULL,	1}, -    {"rename",		Tcl_RenameObjCmd,	NULL,			NULL,	1}, -    {"return",		Tcl_ReturnObjCmd,	TclCompileReturnCmd,	NULL,	1}, -    {"scan",		Tcl_ScanObjCmd,		NULL,			NULL,	1}, -    {"set",		Tcl_SetObjCmd,		TclCompileSetCmd,	NULL,	1}, -    {"split",		Tcl_SplitObjCmd,	NULL,			NULL,	1}, -    {"subst",		Tcl_SubstObjCmd,	TclCompileSubstCmd,	TclNRSubstObjCmd,	1}, -    {"switch",		Tcl_SwitchObjCmd,	TclCompileSwitchCmd,	TclNRSwitchObjCmd, 1}, -    {"tailcall",	NULL,			NULL,			TclNRTailcallObjCmd,	1}, -    {"throw",		Tcl_ThrowObjCmd,	TclCompileThrowCmd,	NULL,	1}, -    {"trace",		Tcl_TraceObjCmd,	NULL,			NULL,	1}, -    {"try",		Tcl_TryObjCmd,		TclCompileTryCmd,	TclNRTryObjCmd,	1}, -    {"unset",		Tcl_UnsetObjCmd,	TclCompileUnsetCmd,	NULL,	1}, -    {"uplevel",		Tcl_UplevelObjCmd,	NULL,			TclNRUplevelObjCmd,	1}, -    {"upvar",		Tcl_UpvarObjCmd,	TclCompileUpvarCmd,	NULL,	1}, -    {"variable",	Tcl_VariableObjCmd,	TclCompileVariableCmd,	NULL,	1}, -    {"while",		Tcl_WhileObjCmd,	TclCompileWhileCmd,	TclNRWhileObjCmd,	1}, -    {"yield",		NULL,			NULL,			TclNRYieldObjCmd,	1}, +    {"catch",		Tcl_CatchObjCmd,	TclCompileCatchCmd,	TclNRCatchObjCmd,	CMD_IS_SAFE}, +    {"concat",		Tcl_ConcatObjCmd,	TclCompileConcatCmd,	NULL,	CMD_IS_SAFE}, +    {"continue",	Tcl_ContinueObjCmd,	TclCompileContinueCmd,	NULL,	CMD_IS_SAFE}, +    {"coroutine",	NULL,			NULL,			TclNRCoroutineObjCmd,	CMD_IS_SAFE}, +    {"error",		Tcl_ErrorObjCmd,	TclCompileErrorCmd,	NULL,	CMD_IS_SAFE}, +    {"eval",		Tcl_EvalObjCmd,		NULL,			TclNREvalObjCmd,	CMD_IS_SAFE}, +    {"expr",		Tcl_ExprObjCmd,		TclCompileExprCmd,	TclNRExprObjCmd,	CMD_IS_SAFE}, +    {"for",		Tcl_ForObjCmd,		TclCompileForCmd,	TclNRForObjCmd,	CMD_IS_SAFE}, +    {"foreach",		Tcl_ForeachObjCmd,	TclCompileForeachCmd,	TclNRForeachCmd,	CMD_IS_SAFE}, +    {"format",		Tcl_FormatObjCmd,	TclCompileFormatCmd,	NULL,	CMD_IS_SAFE}, +    {"global",		Tcl_GlobalObjCmd,	TclCompileGlobalCmd,	NULL,	CMD_IS_SAFE}, +    {"if",		Tcl_IfObjCmd,		TclCompileIfCmd,	TclNRIfObjCmd,	CMD_IS_SAFE}, +    {"incr",		Tcl_IncrObjCmd,		TclCompileIncrCmd,	NULL,	CMD_IS_SAFE}, +    {"join",		Tcl_JoinObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"lappend",		Tcl_LappendObjCmd,	TclCompileLappendCmd,	NULL,	CMD_IS_SAFE}, +    {"lassign",		Tcl_LassignObjCmd,	TclCompileLassignCmd,	NULL,	CMD_IS_SAFE}, +    {"lindex",		Tcl_LindexObjCmd,	TclCompileLindexCmd,	NULL,	CMD_IS_SAFE}, +    {"linsert",		Tcl_LinsertObjCmd,	TclCompileLinsertCmd,	NULL,	CMD_IS_SAFE}, +    {"list",		Tcl_ListObjCmd,		TclCompileListCmd,	NULL,	CMD_IS_SAFE|CMD_COMPILES_EXPANDED}, +    {"llength",		Tcl_LlengthObjCmd,	TclCompileLlengthCmd,	NULL,	CMD_IS_SAFE}, +    {"lmap",		Tcl_LmapObjCmd,		TclCompileLmapCmd,	TclNRLmapCmd,	CMD_IS_SAFE}, +    {"lrange",		Tcl_LrangeObjCmd,	TclCompileLrangeCmd,	NULL,	CMD_IS_SAFE}, +    {"lrepeat",		Tcl_LrepeatObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"lreplace",	Tcl_LreplaceObjCmd,	TclCompileLreplaceCmd,	NULL,	CMD_IS_SAFE}, +    {"lreverse",	Tcl_LreverseObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"lsearch",		Tcl_LsearchObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"lset",		Tcl_LsetObjCmd,		TclCompileLsetCmd,	NULL,	CMD_IS_SAFE}, +    {"lsort",		Tcl_LsortObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"package",		Tcl_PackageObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"proc",		Tcl_ProcObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"regexp",		Tcl_RegexpObjCmd,	TclCompileRegexpCmd,	NULL,	CMD_IS_SAFE}, +    {"regsub",		Tcl_RegsubObjCmd,	TclCompileRegsubCmd,	NULL,	CMD_IS_SAFE}, +    {"rename",		Tcl_RenameObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"return",		Tcl_ReturnObjCmd,	TclCompileReturnCmd,	NULL,	CMD_IS_SAFE}, +    {"scan",		Tcl_ScanObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"set",		Tcl_SetObjCmd,		TclCompileSetCmd,	NULL,	CMD_IS_SAFE}, +    {"split",		Tcl_SplitObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"subst",		Tcl_SubstObjCmd,	TclCompileSubstCmd,	TclNRSubstObjCmd,	CMD_IS_SAFE}, +    {"switch",		Tcl_SwitchObjCmd,	TclCompileSwitchCmd,	TclNRSwitchObjCmd, CMD_IS_SAFE}, +    {"tailcall",	NULL,			TclCompileTailcallCmd,	TclNRTailcallObjCmd,	CMD_IS_SAFE}, +    {"throw",		Tcl_ThrowObjCmd,	TclCompileThrowCmd,	NULL,	CMD_IS_SAFE}, +    {"trace",		Tcl_TraceObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"try",		Tcl_TryObjCmd,		TclCompileTryCmd,	TclNRTryObjCmd,	CMD_IS_SAFE}, +    {"unset",		Tcl_UnsetObjCmd,	TclCompileUnsetCmd,	NULL,	CMD_IS_SAFE}, +    {"uplevel",		Tcl_UplevelObjCmd,	NULL,			TclNRUplevelObjCmd,	CMD_IS_SAFE}, +    {"upvar",		Tcl_UpvarObjCmd,	TclCompileUpvarCmd,	NULL,	CMD_IS_SAFE}, +    {"variable",	Tcl_VariableObjCmd,	TclCompileVariableCmd,	NULL,	CMD_IS_SAFE}, +    {"while",		Tcl_WhileObjCmd,	TclCompileWhileCmd,	TclNRWhileObjCmd,	CMD_IS_SAFE}, +    {"yield",		NULL,			TclCompileYieldCmd,	TclNRYieldObjCmd,	CMD_IS_SAFE}, +    {"yieldto",		NULL,			TclCompileYieldToCmd,	TclNRYieldToObjCmd,	CMD_IS_SAFE},      /*       * Commands in the OS-interface. Note that many of these are unsafe.       */ -    {"after",		Tcl_AfterObjCmd,	NULL,			NULL,	1}, +    {"after",		Tcl_AfterObjCmd,	NULL,			NULL,	CMD_IS_SAFE},      {"cd",		Tcl_CdObjCmd,		NULL,			NULL,	0}, -    {"close",		Tcl_CloseObjCmd,	NULL,			NULL,	1}, -    {"eof",		Tcl_EofObjCmd,		NULL,			NULL,	1}, +    {"close",		Tcl_CloseObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"eof",		Tcl_EofObjCmd,		NULL,			NULL,	CMD_IS_SAFE},      {"encoding",	Tcl_EncodingObjCmd,	NULL,			NULL,	0},      {"exec",		Tcl_ExecObjCmd,		NULL,			NULL,	0},      {"exit",		Tcl_ExitObjCmd,		NULL,			NULL,	0}, -    {"fblocked",	Tcl_FblockedObjCmd,	NULL,			NULL,	1}, +    {"fblocked",	Tcl_FblockedObjCmd,	NULL,			NULL,	CMD_IS_SAFE},      {"fconfigure",	Tcl_FconfigureObjCmd,	NULL,			NULL,	0}, -    {"fcopy",		Tcl_FcopyObjCmd,	NULL,			NULL,	1}, -    {"fileevent",	Tcl_FileEventObjCmd,	NULL,			NULL,	1}, -    {"flush",		Tcl_FlushObjCmd,	NULL,			NULL,	1}, -    {"gets",		Tcl_GetsObjCmd,		NULL,			NULL,	1}, +    {"fcopy",		Tcl_FcopyObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"fileevent",	Tcl_FileEventObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"flush",		Tcl_FlushObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"gets",		Tcl_GetsObjCmd,		NULL,			NULL,	CMD_IS_SAFE},      {"glob",		Tcl_GlobObjCmd,		NULL,			NULL,	0},      {"load",		Tcl_LoadObjCmd,		NULL,			NULL,	0},      {"open",		Tcl_OpenObjCmd,		NULL,			NULL,	0}, -    {"pid",		Tcl_PidObjCmd,		NULL,			NULL,	1}, -    {"puts",		Tcl_PutsObjCmd,		NULL,			NULL,	1}, +    {"pid",		Tcl_PidObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"puts",		Tcl_PutsObjCmd,		NULL,			NULL,	CMD_IS_SAFE},      {"pwd",		Tcl_PwdObjCmd,		NULL,			NULL,	0}, -    {"read",		Tcl_ReadObjCmd,		NULL,			NULL,	1}, -    {"seek",		Tcl_SeekObjCmd,		NULL,			NULL,	1}, +    {"read",		Tcl_ReadObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"seek",		Tcl_SeekObjCmd,		NULL,			NULL,	CMD_IS_SAFE},      {"socket",		Tcl_SocketObjCmd,	NULL,			NULL,	0},      {"source",		Tcl_SourceObjCmd,	NULL,			TclNRSourceObjCmd,	0}, -    {"tell",		Tcl_TellObjCmd,		NULL,			NULL,	1}, -    {"time",		Tcl_TimeObjCmd,		NULL,			NULL,	1}, +    {"tell",		Tcl_TellObjCmd,		NULL,			NULL,	CMD_IS_SAFE}, +    {"time",		Tcl_TimeObjCmd,		NULL,			NULL,	CMD_IS_SAFE},      {"unload",		Tcl_UnloadObjCmd,	NULL,			NULL,	0}, -    {"update",		Tcl_UpdateObjCmd,	NULL,			NULL,	1}, -    {"vwait",		Tcl_VwaitObjCmd,	NULL,			NULL,	1}, +    {"update",		Tcl_UpdateObjCmd,	NULL,			NULL,	CMD_IS_SAFE}, +    {"vwait",		Tcl_VwaitObjCmd,	NULL,			NULL,	CMD_IS_SAFE},      {NULL,		NULL,			NULL,			NULL,	0}  }; @@ -484,6 +486,18 @@ Tcl_CreateInterp(void)  	Tcl_Panic("Tcl_CallFrame must not be smaller than CallFrame");      } +#if defined(_WIN32) && !defined(_WIN64) +    if (sizeof(time_t) != 4) { +	/*NOTREACHED*/ +	Tcl_Panic("<time.h> is not compatible with MSVC"); +    } +    if ((TclOffset(Tcl_StatBuf,st_atime) != 32) +	    || (TclOffset(Tcl_StatBuf,st_ctime) != 40)) { +	/*NOTREACHED*/ +	Tcl_Panic("<sys/stat.h> is not compatible with MSVC"); +    } +#endif +      if (cancelTableInitialized == 0) {  	Tcl_MutexLock(&cancelLock);  	if (cancelTableInitialized == 0) { @@ -512,6 +526,9 @@ Tcl_CreateInterp(void)      iPtr->hiddenCmdTablePtr = NULL;      iPtr->interpInfo = NULL; +    TCL_CT_ASSERT(sizeof(iPtr->extra) <= sizeof(Tcl_HashTable)); +    iPtr->extra.optimizer = TclOptimizeBytecode; +      iPtr->numLevels = 0;      iPtr->maxNestingDepth = MAX_NESTING_DEPTH;      iPtr->framePtr = NULL;	/* Initialise as soon as :: is available */ @@ -771,6 +788,9 @@ Tcl_CreateInterp(void)  	    cmdPtr->deleteProc = NULL;  	    cmdPtr->deleteData = NULL;  	    cmdPtr->flags = 0; +            if (cmdInfoPtr->flags & CMD_COMPILES_EXPANDED) { +                cmdPtr->flags |= CMD_COMPILES_EXPANDED; +            }  	    cmdPtr->importRefPtr = NULL;  	    cmdPtr->tracePtr = NULL;  	    cmdPtr->nreProc = cmdInfoPtr->nreProc; @@ -830,13 +850,9 @@ Tcl_CreateInterp(void)              TclNRAssembleObjCmd, NULL, NULL);      cmdPtr->compileProc = &TclCompileAssembleCmd; -    Tcl_NRCreateCommand(interp, "::tcl::unsupported::yieldTo", NULL, -	    TclNRYieldToObjCmd, NULL, NULL); -    Tcl_NRCreateCommand(interp, "::tcl::unsupported::yieldm", NULL, -	    TclNRYieldObjCmd, INT2PTR(CORO_ACTIVATE_YIELDM), NULL);      Tcl_NRCreateCommand(interp, "::tcl::unsupported::inject", NULL,  	    NRCoroInjectObjCmd, NULL, NULL); -     +  #ifdef USE_DTRACE      /*       * Register the tcl::dtrace command. @@ -1007,7 +1023,7 @@ TclHideUnsafeCommands(  	return TCL_ERROR;      }      for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) { -	if (!cmdInfoPtr->isSafe) { +	if (!(cmdInfoPtr->flags & CMD_IS_SAFE)) {  	    Tcl_HideCommand(interp, cmdInfoPtr->name, cmdInfoPtr->name);  	}      } @@ -1355,10 +1371,11 @@ DeleteInterpProc(      int i;      /* -     * Punt if there is an error in the Tcl_Release/Tcl_Preserve matchup. +     * Punt if there is an error in the Tcl_Release/Tcl_Preserve matchup, +	 * unless we are exiting.       */ -    if (iPtr->numLevels > 0) { +    if ((iPtr->numLevels > 0) && !TclInExit()) {  	Tcl_Panic("DeleteInterpProc called with active evals");      } @@ -1428,7 +1445,6 @@ DeleteInterpProc(       * table, as it will be freed later in this function without further use.       */ -    TclCleanupLiteralTable(interp, &iPtr->literalTable);      TclHandleFree(iPtr->handle);      TclTeardownNamespace(iPtr->globalNsPtr); @@ -1482,7 +1498,7 @@ DeleteInterpProc(       * namespace. The order is important [Bug 1658572].       */ -    if (iPtr->framePtr != iPtr->rootFramePtr) { +    if ((iPtr->framePtr != iPtr->rootFramePtr) && !TclInExit()) {  	Tcl_Panic("DeleteInterpProc: popping rootCallFrame with other frames on top");      }      Tcl_PopCallFrame(interp); @@ -1529,6 +1545,10 @@ DeleteInterpProc(      if (iPtr->execEnvPtr != NULL) {  	TclDeleteExecEnv(iPtr->execEnvPtr);      } +    if (iPtr->scriptFile) { +	Tcl_DecrRefCount(iPtr->scriptFile); +	iPtr->scriptFile = NULL; +    }      Tcl_DecrRefCount(iPtr->emptyObjPtr);      iPtr->emptyObjPtr = NULL; @@ -1556,12 +1576,16 @@ DeleteInterpProc(  	    hPtr != NULL;  	    hPtr = Tcl_NextHashEntry(&search)) {  	CmdFrame *cfPtr = Tcl_GetHashValue(hPtr); +	Proc *procPtr = (Proc *) Tcl_GetHashKey(iPtr->linePBodyPtr, hPtr); -	if (cfPtr->type == TCL_LOCATION_SOURCE) { -	    Tcl_DecrRefCount(cfPtr->data.eval.path); +	procPtr->iPtr = NULL; +	if (cfPtr) { +	    if (cfPtr->type == TCL_LOCATION_SOURCE) { +		Tcl_DecrRefCount(cfPtr->data.eval.path); +	    } +	    ckfree(cfPtr->line); +	    ckfree(cfPtr);  	} -	ckfree(cfPtr->line); -	ckfree(cfPtr);  	Tcl_DeleteHashEntry(hPtr);      }      Tcl_DeleteHashTable(iPtr->linePBodyPtr); @@ -1588,8 +1612,6 @@ DeleteInterpProc(  	    ckfree(eclPtr->loc);  	} -	Tcl_DeleteHashTable(&eclPtr->litInfo); -  	ckfree(eclPtr);  	Tcl_DeleteHashEntry(hPtr);      } @@ -1603,7 +1625,7 @@ DeleteInterpProc(       * know which arguments will be used as scripts and which will not.       */ -    if (iPtr->lineLAPtr->numEntries) { +    if (iPtr->lineLAPtr->numEntries && !TclInExit()) {  	/*  	 * When the interp goes away we have nothing on the stack, so there  	 * are no arguments, so this table has to be empty. @@ -1613,10 +1635,10 @@ DeleteInterpProc(      }      Tcl_DeleteHashTable(iPtr->lineLAPtr); -    ckfree(iPtr->lineLAPtr); +    ckfree((char *) iPtr->lineLAPtr);      iPtr->lineLAPtr = NULL; -    if (iPtr->lineLABCPtr->numEntries) { +    if (iPtr->lineLABCPtr->numEntries && !TclInExit()) {  	/*  	 * When the interp goes away we have nothing on the stack, so there  	 * are no arguments, so this table has to be empty. @@ -1703,9 +1725,9 @@ Tcl_HideCommand(       */      if (strstr(hiddenCmdToken, "::") != NULL) { -	Tcl_AppendResult(interp, +	Tcl_SetObjResult(interp, Tcl_NewStringObj(  		"cannot use namespace qualifiers in hidden command" -		" token (rename)", NULL); +		" token (rename)", -1));          Tcl_SetErrorCode(interp, "TCL", "VALUE", "HIDDENTOKEN", NULL);  	return TCL_ERROR;      } @@ -1728,8 +1750,9 @@ Tcl_HideCommand(       */      if (cmdPtr->nsPtr != iPtr->globalNsPtr) { -	Tcl_AppendResult(interp, "can only hide global namespace commands" -		" (use rename then hide)", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "can only hide global namespace commands (use rename then hide)", +                -1));          Tcl_SetErrorCode(interp, "TCL", "HIDE", "NON_GLOBAL", NULL);  	return TCL_ERROR;      } @@ -1753,8 +1776,9 @@ Tcl_HideCommand(      hPtr = Tcl_CreateHashEntry(hiddenCmdTablePtr, hiddenCmdToken, &isNew);      if (!isNew) { -	Tcl_AppendResult(interp, "hidden command named \"", hiddenCmdToken, -		"\" already exists", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "hidden command named \"%s\" already exists", +                hiddenCmdToken));          Tcl_SetErrorCode(interp, "TCL", "HIDE", "ALREADY_HIDDEN", NULL);  	return TCL_ERROR;      } @@ -1856,8 +1880,9 @@ Tcl_ExposeCommand(       */      if (strstr(cmdName, "::") != NULL) { -	Tcl_AppendResult(interp, "cannot expose to a namespace " -		"(use expose to toplevel, then rename)", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "cannot expose to a namespace (use expose to toplevel, then rename)", +                -1));          Tcl_SetErrorCode(interp, "TCL", "EXPOSE", "NON_GLOBAL", NULL);  	return TCL_ERROR;      } @@ -1872,8 +1897,8 @@ Tcl_ExposeCommand(  	hPtr = Tcl_FindHashEntry(hiddenCmdTablePtr, hiddenCmdToken);      }      if (hPtr == NULL) { -	Tcl_AppendResult(interp, "unknown hidden command \"", hiddenCmdToken, -		"\"", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "unknown hidden command \"%s\"", hiddenCmdToken));          Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "HIDDENTOKEN",                  hiddenCmdToken, NULL);  	return TCL_ERROR; @@ -1892,9 +1917,9 @@ Tcl_ExposeCommand(  	 * than 'nicely' erroring out ?  	 */ -	Tcl_AppendResult(interp, +	Tcl_SetObjResult(interp, Tcl_NewStringObj(  		"trying to expose a non-global command namespace command", -		NULL); +		-1));  	return TCL_ERROR;      } @@ -1911,13 +1936,24 @@ Tcl_ExposeCommand(      hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, cmdName, &isNew);      if (!isNew) { -	Tcl_AppendResult(interp, "exposed command \"", cmdName, -		"\" already exists", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "exposed command \"%s\" already exists", cmdName));          Tcl_SetErrorCode(interp, "TCL", "EXPOSE", "COMMAND_EXISTS", NULL);  	return TCL_ERROR;      }      /* +     * Command resolvers (per-interp, per-namespace) might have resolved to a +     * command for the given namespace scope with this command not being +     * registered with the namespace's command table. During BC compilation, +     * the so-resolved command turns into a CmdName literal. Without +     * invalidating a possible CmdName literal here explicitly, such literals +     * keep being reused while pointing to overhauled commands. +     */ + +    TclInvalidateCmdLiteral(interp, cmdName, nsPtr); + +    /*       * The list of command exported from the namespace might have changed.       * However, we do not need to recompute this just yet; next time we need       * the info will be soon enough. @@ -2049,10 +2085,19 @@ Tcl_CreateCommand(  	 */  	cmdPtr = Tcl_GetHashValue(hPtr); -	oldRefPtr = cmdPtr->importRefPtr; -	cmdPtr->importRefPtr = NULL; +	cmdPtr->refCount++; +	if (cmdPtr->importRefPtr) { +	    cmdPtr->flags |= CMD_REDEF_IN_PROGRESS; +	}  	Tcl_DeleteCommandFromToken(interp, (Tcl_Command) cmdPtr); + +	if (cmdPtr->flags & CMD_REDEF_IN_PROGRESS) { +	    oldRefPtr = cmdPtr->importRefPtr; +	    cmdPtr->importRefPtr = NULL; +	} +	TclCleanupCommandMacro(cmdPtr); +  	hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &isNew);  	if (!isNew) {  	    /* @@ -2065,6 +2110,18 @@ Tcl_CreateCommand(  	}      } else {  	/* +	 * Command resolvers (per-interp, per-namespace) might have resolved +	 * to a command for the given namespace scope with this command not +	 * being registered with the namespace's command table. During BC +	 * compilation, the so-resolved command turns into a CmdName literal. +	 * Without invalidating a possible CmdName literal here explicitly, +	 * such literals keep being reused while pointing to overhauled +	 * commands. +	 */ + +	TclInvalidateCmdLiteral(interp, tail, nsPtr); + +	/*  	 * The list of command exported from the namespace might have changed.  	 * However, we do not need to recompute this just yet; next time we  	 * need the info will be soon enough. @@ -2129,12 +2186,9 @@ Tcl_CreateCommand(   *	future calls to Tcl_GetCommandName.   *   * Side effects: - *	If no command named "cmdName" already exists for interp, one is - *	created. Otherwise, if a command does exist, then if the object-based - *	Tcl_ObjCmdProc is TclInvokeStringCommand, we assume Tcl_CreateCommand - *	was called previously for the same command and just set its - *	Tcl_ObjCmdProc to the argument "proc"; otherwise, we delete the old - *	command. + *	If a command named "cmdName" already exists for interp, it is + *	first deleted.  Then the new command is created from the arguments. + *	[***] (See below for exception).   *   *	In the future, during bytecode evaluation when "cmdName" is seen as   *	the name of a command by Tcl_EvalObj or Tcl_Eval, the object-based @@ -2201,17 +2255,22 @@ Tcl_CreateObjCommand(      if (!isNew) {  	cmdPtr = Tcl_GetHashValue(hPtr); +	/* Command already exists. */ +  	/* -	 * Command already exists. If its object-based Tcl_ObjCmdProc is -	 * TclInvokeStringCommand, we just set its Tcl_ObjCmdProc to the -	 * argument "proc". Otherwise, we delete the old command. +	 * [***] This is wrong.  See Tcl Bug a16752c252.   +	 * However, this buggy behavior is kept under particular +	 * circumstances to accommodate deployed binaries of the +	 * "tclcompiler" program. http://sourceforge.net/projects/tclpro/ +	 * that crash if the bug is fixed.  	 */ -	if (cmdPtr->objProc == TclInvokeStringCommand) { +	if (cmdPtr->objProc == TclInvokeStringCommand +		&& cmdPtr->clientData == clientData +		&& cmdPtr->deleteData == clientData +		&& cmdPtr->deleteProc == deleteProc) {  	    cmdPtr->objProc = proc;  	    cmdPtr->objClientData = clientData; -	    cmdPtr->deleteProc = deleteProc; -	    cmdPtr->deleteData = clientData;  	    return (Tcl_Command) cmdPtr;  	} @@ -2222,10 +2281,19 @@ Tcl_CreateObjCommand(  	 * intact.  	 */ -	oldRefPtr = cmdPtr->importRefPtr; -	cmdPtr->importRefPtr = NULL; +	cmdPtr->refCount++; +	if (cmdPtr->importRefPtr) { +	    cmdPtr->flags |= CMD_REDEF_IN_PROGRESS; +	}  	Tcl_DeleteCommandFromToken(interp, (Tcl_Command) cmdPtr); + +	if (cmdPtr->flags & CMD_REDEF_IN_PROGRESS) { +	    oldRefPtr = cmdPtr->importRefPtr; +	    cmdPtr->importRefPtr = NULL; +	} +	TclCleanupCommandMacro(cmdPtr); +  	hPtr = Tcl_CreateHashEntry(&nsPtr->cmdTable, tail, &isNew);  	if (!isNew) {  	    /* @@ -2238,6 +2306,18 @@ Tcl_CreateObjCommand(  	}      } else {  	/* +	 * Command resolvers (per-interp, per-namespace) might have resolved +	 * to a command for the given namespace scope with this command not +	 * being registered with the namespace's command table. During BC +	 * compilation, the so-resolved command turns into a CmdName literal. +	 * Without invalidating a possible CmdName literal here explicitly, +	 * such literals keep being reused while pointing to overhauled +	 * commands. +	 */ + +	TclInvalidateCmdLiteral(interp, tail, nsPtr); + +	/*  	 * The list of command exported from the namespace might have changed.  	 * However, we do not need to recompute this just yet; next time we  	 * need the info will be soon enough. @@ -2352,8 +2432,8 @@ TclInvokeStringCommand(   *	A standard Tcl string result value.   *   * Side effects: - *	Besides those side effects of the called Tcl_CmdProc, - *	TclInvokeStringCommand allocates and frees storage. + *	Besides those side effects of the called Tcl_ObjCmdProc, + *	TclInvokeObjectCommand allocates and frees storage.   *   *----------------------------------------------------------------------   */ @@ -2457,9 +2537,10 @@ TclRenameCommand(      cmd = Tcl_FindCommand(interp, oldName, NULL, /*flags*/ 0);      cmdPtr = (Command *) cmd;      if (cmdPtr == NULL) { -	Tcl_AppendResult(interp, "can't ", +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't %s \"%s\": command doesn't exist",  		((newName == NULL)||(*newName == '\0'))? "delete":"rename", -		" \"", oldName, "\": command doesn't exist", NULL); +		oldName));          Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "COMMAND", oldName, NULL);  	return TCL_ERROR;      } @@ -2489,15 +2570,15 @@ TclRenameCommand(  	    TCL_CREATE_NS_IF_UNKNOWN, &newNsPtr, &dummy1, &dummy2, &newTail);      if ((newNsPtr == NULL) || (newTail == NULL)) { -	Tcl_AppendResult(interp, "can't rename to \"", newName, -		"\": bad command name", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't rename to \"%s\": bad command name", newName));          Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMMAND", NULL);  	result = TCL_ERROR;  	goto done;      }      if (Tcl_FindHashEntry(&newNsPtr->cmdTable, newTail) != NULL) { -	Tcl_AppendResult(interp, "can't rename to \"", newName, -		 "\": command already exists", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't rename to \"%s\": command already exists", newName));          Tcl_SetErrorCode(interp, "TCL", "OPERATION", "RENAME",                  "TARGET_EXISTS", NULL);  	result = TCL_ERROR; @@ -2547,6 +2628,17 @@ TclRenameCommand(      TclInvalidateNsCmdLookup(cmdPtr->nsPtr);      /* +     * Command resolvers (per-interp, per-namespace) might have resolved to a +     * command for the given namespace scope with this command not being +     * registered with the namespace's command table. During BC compilation, +     * the so-resolved command turns into a CmdName literal. Without +     * invalidating a possible CmdName literal here explicitly, such literals +     * keep being reused while pointing to overhauled commands. +     */ + +    TclInvalidateCmdLiteral(interp, newTail, cmdPtr->nsPtr); + +    /*       * Script for rename traces can delete the command "oldName". Therefore       * increment the reference count for cmdPtr so that it's Command structure       * is freed only towards the end of this function by calling @@ -2561,7 +2653,7 @@ TclRenameCommand(      Tcl_DStringInit(&newFullName);      Tcl_DStringAppend(&newFullName, newNsPtr->fullName, -1);      if (newNsPtr != iPtr->globalNsPtr) { -	Tcl_DStringAppend(&newFullName, "::", 2); +	TclDStringAppendLiteral(&newFullName, "::");      }      Tcl_DStringAppend(&newFullName, newTail, -1);      cmdPtr->refCount++; @@ -3043,12 +3135,13 @@ Tcl_DeleteCommandFromToken(       * commands were created that refer back to this command. Delete these       * imported commands now.       */ - -    for (refPtr = cmdPtr->importRefPtr; refPtr != NULL; -	    refPtr = nextRefPtr) { -	nextRefPtr = refPtr->nextPtr; -	importCmd = (Tcl_Command) refPtr->importedCmdPtr; -	Tcl_DeleteCommandFromToken(interp, importCmd); +    if (!(cmdPtr->flags & CMD_REDEF_IN_PROGRESS)) { +	for (refPtr = cmdPtr->importRefPtr; refPtr != NULL; +		refPtr = nextRefPtr) { +	    nextRefPtr = refPtr->nextPtr; +	    importCmd = (Tcl_Command) refPtr->importedCmdPtr; +	    Tcl_DeleteCommandFromToken(interp, importCmd); +	}      }      /* @@ -3077,8 +3170,8 @@ Tcl_DeleteCommandFromToken(       * from a CmdName Tcl object in some ByteCode code sequence. In that case,       * delay the cleanup until all references are either discarded (when a       * ByteCode is freed) or replaced by a new reference (when a cached -     * CmdName Command reference is found to be invalid and TclNRExecuteByteCode -     * looks up the command in the command hashtable). +     * CmdName Command reference is found to be invalid and +     * TclNRExecuteByteCode looks up the command in the command hashtable).       */      TclCleanupCommandMacro(cmdPtr); @@ -3284,66 +3377,6 @@ CancelEvalProc(  /*   *----------------------------------------------------------------------   * - * GetCommandSource -- - * - *	This function returns a Tcl_Obj with the full source string for the - *	command. This insures that traces get a correct NUL-terminated command - *	string. The Tcl_Obj has refCount==1. - * - *	*** MAINTAINER WARNING *** - *	The returned Tcl_Obj is all wrong for any purpose but getting the - *	source string for an objc/objv command line in the stringRep (no - *	stringRep if no source is available) and the corresponding substituted - *	version in the List intrep. - *	This means that the intRep and stringRep DO NOT COINCIDE! Using these - *	Tcl_Objs normally is likely to break things. - * - *---------------------------------------------------------------------- - */ - -static Tcl_Obj * -GetCommandSource( -    Interp *iPtr, -    int objc, -    Tcl_Obj *const objv[], -    int lookup) -{ -    Tcl_Obj *objPtr, *obj2Ptr; -    CmdFrame *cfPtr = iPtr->cmdFramePtr; -    const char *command = NULL; -    int numChars; - -    objPtr = Tcl_NewListObj(objc, objv); -    if (lookup && cfPtr && (cfPtr->numLevels == iPtr->numLevels-1)) { -	switch (cfPtr->type) { -	case TCL_LOCATION_EVAL: -	case TCL_LOCATION_SOURCE: -	    command = cfPtr->cmd.str.cmd; -	    numChars = cfPtr->cmd.str.len; -	    break; -	case TCL_LOCATION_BC: -	case TCL_LOCATION_PREBC: -	    command = TclGetSrcInfoForCmd(iPtr, &numChars); -	    break; -	case TCL_LOCATION_EVAL_LIST: -	    /* Got it already */ -	    break; -	} -	if (command) { -	    obj2Ptr = Tcl_NewStringObj(command, numChars); -	    objPtr->bytes = obj2Ptr->bytes; -	    objPtr->length = numChars; -	    obj2Ptr->bytes = NULL; -	    Tcl_DecrRefCount(obj2Ptr); -	} -    } -    Tcl_IncrRefCount(objPtr); -    return objPtr; -} - -/* - *---------------------------------------------------------------------- - *   * TclCleanupCommand --   *   *	This function frees up a Command structure unless it is still @@ -3419,7 +3452,7 @@ Tcl_CreateMathFunc(      data->clientData = clientData;      Tcl_DStringInit(&bigName); -    Tcl_DStringAppend(&bigName, "::tcl::mathfunc::", -1); +    TclDStringAppendLiteral(&bigName, "::tcl::mathfunc::");      Tcl_DStringAppend(&bigName, name, -1);      Tcl_CreateObjCommand(interp, Tcl_DStringValue(&bigName), @@ -3487,9 +3520,9 @@ OldMathFuncProc(  	     * We have a non-numeric argument.  	     */ -	    Tcl_SetResult(interp, +	    Tcl_SetObjResult(interp, Tcl_NewStringObj(  		    "argument to math function didn't have numeric value", -		    TCL_STATIC); +		    -1));  	    TclCheckBadOctal(interp, Tcl_GetString(valuePtr));  	    ckfree(args);  	    return TCL_ERROR; @@ -3646,12 +3679,8 @@ Tcl_GetMathFuncInfo(       */      if (cmdPtr == NULL) { -	Tcl_Obj *message; - -	TclNewLiteralStringObj(message, "unknown math function \""); -	Tcl_AppendToObj(message, name, -1); -	Tcl_AppendToObj(message, "\"", 1); -	Tcl_SetObjResult(interp, message); +        Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "unknown math function \"%s\"", name));  	Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "MATHFUNC", name, NULL);  	*numArgsPtr = -1;  	*argTypesPtr = NULL; @@ -3706,41 +3735,28 @@ Tcl_ListMathFuncs(      Tcl_Interp *interp,      const char *pattern)  { -    Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp); -    Namespace *nsPtr; -    Namespace *dummy1NsPtr; -    Namespace *dummy2NsPtr; -    const char *dummyNamePtr; -    Tcl_Obj *result = Tcl_NewObj(); - -    TclGetNamespaceForQualName(interp, "::tcl::mathfunc", -	    globalNsPtr, TCL_FIND_ONLY_NS | TCL_GLOBAL_ONLY, -	    &nsPtr, &dummy1NsPtr, &dummy2NsPtr, &dummyNamePtr); -    if (nsPtr == NULL) { -	return result; +    Tcl_Obj *script = Tcl_NewStringObj("::info functions ", -1); +    Tcl_Obj *result; +    Tcl_InterpState state; + +    if (pattern) { +	Tcl_Obj *patternObj = Tcl_NewStringObj(pattern, -1); +	Tcl_Obj *arg = Tcl_NewListObj(1, &patternObj); + +	Tcl_AppendObjToObj(script, arg); +	Tcl_DecrRefCount(arg);	/* Should tear down patternObj too */      } -    if ((pattern != NULL) && TclMatchIsTrivial(pattern)) { -	if (Tcl_FindHashEntry(&nsPtr->cmdTable, pattern) != NULL) { -	    Tcl_ListObjAppendElement(NULL, result, -		    Tcl_NewStringObj(pattern, -1)); -	} +    state = Tcl_SaveInterpState(interp, TCL_OK); +    Tcl_IncrRefCount(script); +    if (TCL_OK == Tcl_EvalObjEx(interp, script, 0)) { +	result = Tcl_DuplicateObj(Tcl_GetObjResult(interp));      } else { -	Tcl_HashSearch cmdHashSearch; -	Tcl_HashEntry *cmdHashEntry = -		Tcl_FirstHashEntry(&nsPtr->cmdTable,&cmdHashSearch); - -	for (; cmdHashEntry != NULL; -		cmdHashEntry = Tcl_NextHashEntry(&cmdHashSearch)) { -	    const char *cmdNamePtr = -		    Tcl_GetHashKey(&nsPtr->cmdTable, cmdHashEntry); - -	    if (pattern == NULL || Tcl_StringMatch(cmdNamePtr, pattern)) { -		Tcl_ListObjAppendElement(NULL, result, -			Tcl_NewStringObj(cmdNamePtr, -1)); -	    } -	} +	result = Tcl_NewObj();      } +    Tcl_DecrRefCount(script); +    Tcl_RestoreInterpState(interp, state); +      return result;  } @@ -3780,9 +3796,8 @@ TclInterpReady(       */      if (iPtr->flags & DELETED) { -	/* JJM - Superfluous Tcl_ResetResult call removed. */ -	Tcl_AppendResult(interp, -		"attempt to call eval in deleted interpreter", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +		"attempt to call eval in deleted interpreter", -1));  	Tcl_SetErrorCode(interp, "TCL", "IDELETE",  		"attempt to call eval in deleted interpreter", NULL);  	return TCL_ERROR; @@ -3810,8 +3825,8 @@ TclInterpReady(  	return TCL_OK;      } -    Tcl_AppendResult(interp, -	    "too many nested evaluations (infinite loop?)", NULL); +    Tcl_SetObjResult(interp, Tcl_NewStringObj( +	    "too many nested evaluations (infinite loop?)", -1));      Tcl_SetErrorCode(interp, "TCL", "LIMIT", "STACK", NULL);      return TCL_ERROR;  } @@ -3945,8 +3960,7 @@ Tcl_Canceled(              }          } -        Tcl_ResetResult(interp); -        Tcl_AppendResult(interp, message, NULL); +        Tcl_SetObjResult(interp, Tcl_NewStringObj(message, -1));          Tcl_SetErrorCode(interp, "TCL", "CANCEL", id, message, NULL);      } @@ -4123,43 +4137,39 @@ TclNREvalObjv(  				 * requested Command struct to be invoked. */  {      Interp *iPtr = (Interp *) interp; -    int result; -    Namespace *lookupNsPtr = iPtr->lookupNsPtr; -    Command **cmdPtrPtr; - -    iPtr->lookupNsPtr = NULL;      /* -     * Push a callback with cleanup tasks for commands; the cmdPtr at data[0] -     * will be filled later when the command is found: save its address at -     * objProcPtr. -     *       * data[1] stores a marker for use by tailcalls; it will be set to 1 by       * command redirectors (imports, alias, ensembles) so that tailcalls       * finishes the source command and not just the target.       */ -    if (iPtr->evalFlags & TCL_EVAL_REDIRECT) { -	TclNRAddCallback(interp, NRCommand, NULL, INT2PTR(1), NULL, NULL); -	iPtr->evalFlags &= ~TCL_EVAL_REDIRECT; +    if (iPtr->deferredCallbacks) { +        iPtr->deferredCallbacks = NULL;      } else {  	TclNRAddCallback(interp, NRCommand, NULL, NULL, NULL, NULL);      } -    cmdPtrPtr = (Command **) &(TOP_CB(interp)->data[0]); - -    TclNRSpliceDeferred(interp);      iPtr->numLevels++; -    result = TclInterpReady(interp); - -    if ((result != TCL_OK) || (objc == 0)) { -	return result; -    } - -    if (cmdPtr) { -	goto commandFound; -    } +    TclNRAddCallback(interp, EvalObjvCore, cmdPtr, INT2PTR(flags), +	    INT2PTR(objc), objv); +    return TCL_OK; +} +static int +EvalObjvCore( +    ClientData data[], +    Tcl_Interp *interp, +    int result) +{ +    Command *cmdPtr = NULL, *preCmdPtr = data[0]; +    int flags = PTR2INT(data[1]); +    int objc = PTR2INT(data[2]); +    Tcl_Obj **objv = data[3]; +    Interp *iPtr = (Interp *) interp; +    Namespace *lookupNsPtr = NULL; +    int enterTracesDone = 0; +          /*       * Push records for task to be done on return, in INVERSE order. First, if       * needed, the exception handlers (as they should happen last). @@ -4169,61 +4179,150 @@ TclNREvalObjv(  	TEOV_PushExceptionHandlers(interp, objc, objv, flags);      } +    if (TCL_OK != TclInterpReady(interp)) { +	return TCL_ERROR; +    } + +    if (objc == 0) { +	return TCL_OK; +    } + +    if (TclLimitExceeded(iPtr->limit)) { +	return TCL_ERROR; +    } +      /*       * Configure evaluation context to match the requested flags.       */ -    if ((flags & TCL_EVAL_INVOKE) || lookupNsPtr) { -	if (!lookupNsPtr) { -	    lookupNsPtr = iPtr->globalNsPtr; -	} +    if (iPtr->lookupNsPtr) { + +	/* +	 * Capture the namespace we should do command name resolution in, as +	 * instructed by our caller sneaking it in to us in a private interp +	 * field.  Clear that field right away so we cannot possibly have its +	 * use leak where it should not.  The sneaky message pass is done. +	 * +	 * Use of this mechanism overrides the TCL_EVAL_GLOBAL flag. +	 * TODO: Is that a bug? +	 */ + +	lookupNsPtr = iPtr->lookupNsPtr; +	iPtr->lookupNsPtr = NULL; +    } else if (flags & TCL_EVAL_INVOKE) { +	lookupNsPtr = iPtr->globalNsPtr;      } else { -	if (flags & TCL_EVAL_GLOBAL) { -	    TEOV_SwitchVarFrame(interp); -	    lookupNsPtr = iPtr->globalNsPtr; -	}  	/*  	 * TCL_EVAL_INVOKE was not set: clear rewrite rules  	 */  	iPtr->ensembleRewrite.sourceObjs = NULL; + +	if (flags & TCL_EVAL_GLOBAL) { +	    TEOV_SwitchVarFrame(interp); +	    lookupNsPtr = iPtr->globalNsPtr; +	}      }      /* -     * Lookup the command +     * Lookup the Command to dispatch.       */ -    cmdPtr = TEOV_LookupCmdFromObj(interp, objv[0], lookupNsPtr); -    if (!cmdPtr) { -	return TEOV_NotFound(interp, objc, objv, lookupNsPtr); +    reresolve: +    assert(cmdPtr == NULL); +    if (preCmdPtr) { +	/* Caller gave it to us */ +	if (!(preCmdPtr->flags & CMD_IS_DELETED)) { +	    /* So long as it exists, use it. */ +	    cmdPtr = preCmdPtr; +	} else if (flags & TCL_EVAL_NORESOLVE) { +	    /* +	     * When it's been deleted, and we're told not to attempt +	     * resolving it ourselves, all we can do is raise an error. +	     */ +	    Tcl_SetObjResult(interp, Tcl_ObjPrintf( +		    "attempt to invoke a deleted command")); +	    Tcl_SetErrorCode(interp, "TCL", "EVAL", "DELETEDCOMMAND", NULL); +	    return TCL_ERROR; +	}      } - -    iPtr->cmdCount++; -    if (TclLimitExceeded(iPtr->limit)) { -	return TCL_ERROR; +    if (cmdPtr == NULL) { +	cmdPtr = TEOV_LookupCmdFromObj(interp, objv[0], lookupNsPtr); +	if (!cmdPtr) { +	    return TEOV_NotFound(interp, objc, objv, lookupNsPtr); +	}      } -    /* -     * Found a command! The real work begins now ... -     */ +    if (enterTracesDone || iPtr->tracePtr +	    || (cmdPtr->flags & CMD_HAS_EXEC_TRACES)) { -  commandFound: -    if (iPtr->tracePtr || (cmdPtr->flags & CMD_HAS_EXEC_TRACES)) { -	/* -	 * Call enter traces. They will schedule a call to the leave traces if -	 * necessary. -	 */ +	Tcl_Obj *commandPtr = TclGetSourceFromFrame( +		flags & TCL_EVAL_SOURCE_IN_FRAME ?  iPtr->cmdFramePtr : NULL, +		objc, objv); +	Tcl_IncrRefCount(commandPtr); -	result = TEOV_RunEnterTraces(interp, &cmdPtr, objc, objv, lookupNsPtr); -	if (!cmdPtr) { -	    return TEOV_NotFound(interp, objc, objv, lookupNsPtr); -	} -	if (result != TCL_OK) { -	    return result; +	if (!enterTracesDone) { + +	    int code = TEOV_RunEnterTraces(interp, &cmdPtr, commandPtr, +		    objc, objv); + +	    /* +	     * Send any exception from enter traces back as an exception +	     * raised by the traced command. +	     * TODO: Is this a bug?  Letting an execution trace BREAK or +	     * CONTINUE or RETURN in the place of the traced command? +	     * Would either converting all exceptions to TCL_ERROR, or +	     * just swallowing them be better?  (Swallowing them has the +	     * problem of permanently hiding program errors.) +	     */ + +	    if (code != TCL_OK) { +		Tcl_DecrRefCount(commandPtr); +		return code; +	    } + +	    /* +	     * If the enter traces made the resolved cmdPtr unusable, go +	     * back and resolve again, but next time don't run enter +	     * traces again. +	     */ + +	    if (cmdPtr == NULL) { +		enterTracesDone = 1; +		Tcl_DecrRefCount(commandPtr); +		goto reresolve; +	    }  	} + +	/*  +	 * Schedule leave traces.  Raise the refCount on the resolved +	 * cmdPtr, so that when it passes to the leave traces we know +	 * it's still valid. +	 */ + +	cmdPtr->refCount++; +	TclNRAddCallback(interp, TEOV_RunLeaveTraces, INT2PTR(objc), +		    commandPtr, cmdPtr, objv);      } +    TclNRAddCallback(interp, Dispatch, +	    cmdPtr->nreProc ? cmdPtr->nreProc : cmdPtr->objProc, +	    cmdPtr->objClientData, INT2PTR(objc), objv); +    return TCL_OK; +} + +static int +Dispatch( +    ClientData data[], +    Tcl_Interp *interp, +    int result) +{ +    Tcl_ObjCmdProc *objProc = data[0]; +    ClientData clientData = data[1]; +    int objc = PTR2INT(data[2]); +    Tcl_Obj **objv = data[3]; +    Interp *iPtr = (Interp *) interp;  #ifdef USE_DTRACE      if (TCL_DTRACE_CMD_ARGS_ENABLED()) { @@ -4244,42 +4343,18 @@ TclNREvalObjv(  	TCL_DTRACE_CMD_INFO(a[0], a[1], a[2], a[3], i[0], i[1], a[4], a[5]);  	TclDecrRefCount(info);      } -    if (TCL_DTRACE_CMD_RETURN_ENABLED() || TCL_DTRACE_CMD_RESULT_ENABLED()) { +    if ((TCL_DTRACE_CMD_RETURN_ENABLED() || TCL_DTRACE_CMD_RESULT_ENABLED()) +	    && objc) {  	TclNRAddCallback(interp, DTraceCmdReturn, objv[0], NULL, NULL, NULL);      } -    if (TCL_DTRACE_CMD_ENTRY_ENABLED()) { +    if (TCL_DTRACE_CMD_ENTRY_ENABLED() && objc) {  	TCL_DTRACE_CMD_ENTRY(TclGetString(objv[0]), objc - 1,  		(Tcl_Obj **)(objv + 1));      }  #endif /* USE_DTRACE */ -    /* -     * Fix the original callback to point to the now known cmdPtr. Insure that -     * the Command struct lives until the command returns. -     */ - -    *cmdPtrPtr = cmdPtr; -    cmdPtr->refCount++; - -    /* -     * Find the objProc to call: nreProc if available, objProc otherwise. Push -     * a callback to do the actual running. -     */ -    if (cmdPtr->nreProc) { -        TclNRAddCallback(interp, NRRunObjProc, cmdPtr, -                INT2PTR(objc), (ClientData) objv, NULL); -        return TCL_OK; -    } else { -	return cmdPtr->objProc(cmdPtr->objClientData, interp, objc, objv); -    }         -} - -void -TclPushTailcallPoint( -    Tcl_Interp *interp) -{ -    TclNRAddCallback(interp, NRCommand, NULL, NULL, NULL, NULL); -    ((Interp *) interp)->numLevels++; +    iPtr->cmdCount++; +    return objProc(clientData, interp, objc, objv);  }  int @@ -4318,20 +4393,23 @@ TclNRRunCallbacks(      return result;  } -int +static int  NRCommand(      ClientData data[],      Tcl_Interp *interp,      int result)  {      Interp *iPtr = (Interp *) interp; -    Command *cmdPtr = data[0]; -    /* int cmdStart = PTR2INT(data[1]); NOT USED HERE */ -    if (cmdPtr) { -	TclCleanupCommandMacro(cmdPtr); +    iPtr->numLevels--; + +     /* +      * If there is a tailcall, schedule it +      */ +  +    if (data[1] && (data[1] != INT2PTR(1))) { +        TclNRAddCallback(interp, TclNRTailcallEval, data[1], NULL, NULL, NULL);      } -    ((Interp *)interp)->numLevels--;      /* OPT ??       * Do not interrupt a series of cleanups with async or limit checks: @@ -4350,22 +4428,6 @@ NRCommand(      return result;  } - -static int -NRRunObjProc( -    ClientData data[], -    Tcl_Interp *interp, -    int result) -{ -    /* OPT: do not call? */ - -    Command* cmdPtr = data[0]; -    int objc = PTR2INT(data[1]); -    Tcl_Obj **objv = data[2]; - -    return cmdPtr->nreProc(cmdPtr->objClientData, interp, objc, objv); -} -  /*   *---------------------------------------------------------------------- @@ -4569,8 +4631,8 @@ TEOV_NotFound(      cmdPtr = TEOV_LookupCmdFromObj(interp, newObjv[0], lookupNsPtr);      if (cmdPtr == NULL) { -	Tcl_AppendResult(interp, "invalid command name \"", -		TclGetString(objv[0]), "\"", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "invalid command name \"%s\"", TclGetString(objv[0])));          Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "COMMAND",                  TclGetString(objv[0]), NULL); @@ -4590,9 +4652,9 @@ TEOV_NotFound(  	savedNsPtr = varFramePtr->nsPtr;  	varFramePtr->nsPtr = lookupNsPtr;      } -    TclNRDeferCallback(interp, TEOV_NotFoundCallback, INT2PTR(handlerObjc), +    TclSkipTailcall(interp); +    TclNRAddCallback(interp, TEOV_NotFoundCallback, INT2PTR(handlerObjc),  	    newObjv, savedNsPtr, NULL); -    iPtr->evalFlags |= TCL_EVAL_REDIRECT;      return TclNREvalObjv(interp, newObjc, newObjv, TCL_EVAL_NOERR, NULL);  } @@ -4629,27 +4691,21 @@ static int  TEOV_RunEnterTraces(      Tcl_Interp *interp,      Command **cmdPtrPtr, +    Tcl_Obj *commandPtr,      int objc, -    Tcl_Obj *const objv[], -    Namespace *lookupNsPtr) +    Tcl_Obj *const objv[])  {      Interp *iPtr = (Interp *) interp;      Command *cmdPtr = *cmdPtrPtr; -    int traceCode = TCL_OK; -    int cmdEpoch = cmdPtr->cmdEpoch; -    int newEpoch; -    const char *command; -    int length; -    Tcl_Obj *commandPtr; - -    commandPtr = GetCommandSource(iPtr, objc, objv, 1); -    command = Tcl_GetStringFromObj(commandPtr, &length); +    int newEpoch, cmdEpoch = cmdPtr->cmdEpoch; +    int length, traceCode = TCL_OK; +    const char *command = Tcl_GetStringFromObj(commandPtr, &length);      /*       * Call trace functions.       * Execute any command or execution traces. Note that we bump up the -     * command's reference count for the duration of the calling of the traces -     * so that the structure doesn't go away underneath our feet. +     * command's reference count for the duration of the calling of the +     * traces so that the structure doesn't go away underneath our feet.       */      cmdPtr->refCount++; @@ -4664,29 +4720,22 @@ TEOV_RunEnterTraces(      newEpoch = cmdPtr->cmdEpoch;      TclCleanupCommandMacro(cmdPtr); -    /* -     * If the traces modified/deleted the command or any existing traces, they -     * will update the command's epoch. We need to lookup again, but do not -     * run enter traces on the newly found cmdPtr. -     */ - -    if (cmdEpoch != newEpoch) { -	cmdPtr = TEOV_LookupCmdFromObj(interp, objv[0], lookupNsPtr); -	*cmdPtrPtr = cmdPtr; +    if (traceCode != TCL_OK) { +	if (traceCode == TCL_ERROR) { +	    Tcl_Obj *info; + +	    TclNewLiteralStringObj(info, "\n    (enter trace on \""); +	    Tcl_AppendLimitedToObj(info, command, length, 55, "..."); +	    Tcl_AppendToObj(info, "\")", 2); +	    Tcl_AppendObjToErrorInfo(interp, info); +	    iPtr->flags |= ERR_ALREADY_LOGGED; +	} +	return traceCode;      } - -    if (cmdPtr) { -	/* -	 * Command was found: push a record to schedule the leave traces. -	 */ - -	TclNRAddCallback(interp, TEOV_RunLeaveTraces, INT2PTR(traceCode), -		commandPtr, cmdPtr, NULL); -	cmdPtr->refCount++; -    } else { -	Tcl_DecrRefCount(commandPtr); +    if (cmdEpoch != newEpoch) { +	*cmdPtrPtr = NULL;      } -    return traceCode; +    return TCL_OK;  }  static int @@ -4696,20 +4745,16 @@ TEOV_RunLeaveTraces(      int result)  {      Interp *iPtr = (Interp *) interp; -    const char *command; -    int length, objc; -    Tcl_Obj **objv; -    int traceCode = PTR2INT(data[0]); +    int traceCode = TCL_OK; +    int objc = PTR2INT(data[0]);      Tcl_Obj *commandPtr = data[1];      Command *cmdPtr = data[2]; - -    command = Tcl_GetStringFromObj(commandPtr, &length); -    if (TCL_OK != Tcl_ListObjGetElements(interp, commandPtr, &objc, &objv)) { -	Tcl_Panic("Who messed with commandPtr?"); -    } +    Tcl_Obj **objv = data[3]; +    int length; +    const char *command = Tcl_GetStringFromObj(commandPtr, &length);      if (!(cmdPtr->flags & CMD_IS_DELETED)) { -	if ((cmdPtr->flags & CMD_HAS_EXEC_TRACES) && traceCode == TCL_OK){ +	if (cmdPtr->flags & CMD_HAS_EXEC_TRACES){  	    traceCode = TclCheckExecutionTraces(interp, command, length,  		    cmdPtr, result, TCL_TRACE_LEAVE_EXEC, objc, objv);  	} @@ -4718,7 +4763,6 @@ TEOV_RunLeaveTraces(  		    cmdPtr, result, TCL_TRACE_LEAVE_EXEC, objc, objv);  	}      } -    Tcl_DecrRefCount(commandPtr);      /*       * As cmdPtr is set, TclNRRunCallbacks is about to reduce the numlevels. @@ -4729,8 +4773,18 @@ TEOV_RunLeaveTraces(      TclCleanupCommandMacro(cmdPtr);      if (traceCode != TCL_OK) { -	return traceCode; +	if (traceCode == TCL_ERROR) { +	    Tcl_Obj *info; + +	    TclNewLiteralStringObj(info, "\n    (leave trace on \""); +	    Tcl_AppendLimitedToObj(info, command, length, 55, "..."); +	    Tcl_AppendToObj(info, "\")", 2); +	    Tcl_AppendObjToErrorInfo(interp, info); +	    iPtr->flags |= ERR_ALREADY_LOGGED; +	} +	result = traceCode;      } +    Tcl_DecrRefCount(commandPtr);      return result;  } @@ -4746,7 +4800,6 @@ TEOV_LookupCmdFromObj(      if (lookupNsPtr) {  	iPtr->varFramePtr->nsPtr = lookupNsPtr; -	iPtr->lookupNsPtr = NULL;      }      cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, namePtr);      iPtr->varFramePtr->nsPtr = savedNsPtr; @@ -4965,31 +5018,22 @@ TclEvalEx(      /*       * TIP #280 Initialize tracking. Do not push on the frame stack yet.       * -     * We may continue counting based on a specific context (CTX), or open a -     * new context, either for a sourced script, or 'eval'. For sourced files -     * we always have a path object, even if nothing was specified in the -     * interp itself. That makes code using it simpler as NULL checks can be -     * left out. Sourced file without path in the 'scriptFile' is possible -     * during Tcl initialization. +     * We open a new context, either for a sourced script, or 'eval'. +     * For sourced files we always have a path object, even if nothing was +     * specified in the interp itself. That makes code using it simpler as +     * NULL checks can be left out. Sourced file without path in the +     * 'scriptFile' is possible during Tcl initialization.       */      eeFramePtr->level = iPtr->cmdFramePtr ? iPtr->cmdFramePtr->level + 1 : 1; -    eeFramePtr->numLevels = iPtr->numLevels;      eeFramePtr->framePtr = iPtr->framePtr;      eeFramePtr->nextPtr = iPtr->cmdFramePtr;      eeFramePtr->nline = 0;      eeFramePtr->line = NULL; +    eeFramePtr->cmdObj = NULL;      iPtr->cmdFramePtr = eeFramePtr; -    if (iPtr->evalFlags & TCL_EVAL_CTX) { -	/* -	 * Path information comes out of the context. -	 */ - -	eeFramePtr->type = TCL_LOCATION_SOURCE; -	eeFramePtr->data.eval.path = iPtr->invokeCmdFramePtr->data.eval.path; -	Tcl_IncrRefCount(eeFramePtr->data.eval.path); -    } else if (iPtr->evalFlags & TCL_EVAL_FILE) { +    if (iPtr->evalFlags & TCL_EVAL_FILE) {  	/*  	 * Set up for a sourced file.  	 */ @@ -5032,7 +5076,9 @@ TclEvalEx(      do {  	if (Tcl_ParseCommand(interp, p, bytesLeft, 0, parsePtr) != TCL_OK) {  	    code = TCL_ERROR; -	    goto error; +	    Tcl_LogCommandInfo(interp, script, parsePtr->commandStart, +		    parsePtr->term + 1 - parsePtr->commandStart); +	    goto posterror;  	}  	/* @@ -5198,23 +5244,28 @@ TclEvalEx(  	     * have been executed.  	     */ -	    eeFramePtr->cmd.str.cmd = parsePtr->commandStart; -	    eeFramePtr->cmd.str.len = parsePtr->commandSize; +	    eeFramePtr->cmd = parsePtr->commandStart; +	    eeFramePtr->len = parsePtr->commandSize;  	    if (parsePtr->term ==  		    parsePtr->commandStart + parsePtr->commandSize - 1) { -		eeFramePtr->cmd.str.len--; +		eeFramePtr->len--;  	    }  	    eeFramePtr->nline = objectsUsed;  	    eeFramePtr->line = lines;  	    TclArgumentEnter(interp, objv, objectsUsed, eeFramePtr); -	    code = Tcl_EvalObjv(interp, objectsUsed, objv, TCL_EVAL_NOERR); +	    code = Tcl_EvalObjv(interp, objectsUsed, objv, +		    TCL_EVAL_NOERR | TCL_EVAL_SOURCE_IN_FRAME);  	    TclArgumentRelease(interp, objv, objectsUsed);  	    eeFramePtr->line = NULL;  	    eeFramePtr->nline = 0; +	    if (eeFramePtr->cmdObj) { +		Tcl_DecrRefCount(eeFramePtr->cmdObj); +		eeFramePtr->cmdObj = NULL; +	    }  	    if (code != TCL_OK) {  		goto error; @@ -5288,6 +5339,7 @@ TclEvalEx(  	Tcl_LogCommandInfo(interp, script, parsePtr->commandStart,  		commandLength);      } + posterror:      iPtr->flags &= ~ERR_ALREADY_LOGGED;      /* @@ -5562,72 +5614,88 @@ TclArgumentBCEnter(      int objc,      void *codePtr,      CmdFrame *cfPtr, +    int cmd,      int pc)  { +    ExtCmdLoc *eclPtr; +    int word; +    ECL *ePtr; +    CFWordBC *lastPtr = NULL;      Interp *iPtr = (Interp *) interp;      Tcl_HashEntry *hePtr =  	    Tcl_FindHashEntry(iPtr->lineBCPtr, (char *) codePtr); -    ExtCmdLoc *eclPtr;      if (!hePtr) {  	return;      }      eclPtr = Tcl_GetHashValue(hePtr); -    hePtr = Tcl_FindHashEntry(&eclPtr->litInfo, INT2PTR(pc)); -    if (hePtr) { -	int word; -	int cmd = PTR2INT(Tcl_GetHashValue(hePtr)); -	ECL *ePtr = &eclPtr->loc[cmd]; -	CFWordBC *lastPtr = NULL; +    ePtr = &eclPtr->loc[cmd]; -	/* -	 * A few truths ... -	 * (1) ePtr->nline == objc -	 * (2) (ePtr->line[word] < 0) => !literal, for all words -	 * (3) (word == 0) => !literal -	 * -	 * Item (2) is why we can use objv to get the literals, and do not -	 * have to save them at compile time. -	 */ +    /* +     * ePtr->nline is the number of words originally parsed. +     * +     * objc is the number of elements getting invoked. +     * +     * If they are not the same, we arrived here by compiling an +     * ensemble dispatch.  Ensemble subcommands that lead to script +     * evaluation are not supposed to get compiled, because a command +     * such as [info level] in the script can expose some of the dispatch +     * shenanigans.  This means that we don't have to tend to the  +     * housekeeping, and can escape now. +     */ +	 +    if (ePtr->nline != objc) { +        return; +    } -	for (word = 1; word < objc; word++) { -	    if (ePtr->line[word] >= 0) { -		int isnew; -		Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(iPtr->lineLABCPtr, -			objv[word], &isnew); -		CFWordBC *cfwPtr = ckalloc(sizeof(CFWordBC)); - -		cfwPtr->framePtr = cfPtr; -		cfwPtr->obj = objv[word]; -		cfwPtr->pc = pc; -		cfwPtr->word = word; -		cfwPtr->nextPtr = lastPtr; -		lastPtr = cfwPtr; - -		if (isnew) { -		    /* -		     * The word is not on the stack yet, remember the current -		     * location and initialize references. -		     */ - -		    cfwPtr->prevPtr = NULL; -		} else { -		    /* -		     * The object is already on the stack, however it may have -		     * a different location now (literal sharing may map -		     * multiple location to a single Tcl_Obj*. Save the old -		     * information in the new structure. -		     */ - -		    cfwPtr->prevPtr = Tcl_GetHashValue(hPtr); -		} +    /* +     * Having disposed of the ensemble cases, we can state... +     * A few truths ... +     * (1) ePtr->nline == objc +     * (2) (ePtr->line[word] < 0) => !literal, for all words +     * (3) (word == 0) => !literal +     * +     * Item (2) is why we can use objv to get the literals, and do not +     * have to save them at compile time. +     */ + +    for (word = 1; word < objc; word++) { +	if (ePtr->line[word] >= 0) { +	    int isnew; +	    Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(iPtr->lineLABCPtr, +		objv[word], &isnew); +	    CFWordBC *cfwPtr = ckalloc(sizeof(CFWordBC)); + +	    cfwPtr->framePtr = cfPtr; +	    cfwPtr->obj = objv[word]; +	    cfwPtr->pc = pc; +	    cfwPtr->word = word; +	    cfwPtr->nextPtr = lastPtr; +	    lastPtr = cfwPtr; + +	    if (isnew) { +		/* +		 * The word is not on the stack yet, remember the current +		 * location and initialize references. +		 */ + +		cfwPtr->prevPtr = NULL; +	    } else { +		/* +		 * The object is already on the stack, however it may have +		 * a different location now (literal sharing may map +		 * multiple location to a single Tcl_Obj*. Save the old +		 * information in the new structure. +		 */ -		Tcl_SetHashValue(hPtr, cfwPtr); +		cfwPtr->prevPtr = Tcl_GetHashValue(hPtr);  	    } -	} /* for */ -	cfPtr->litarg = lastPtr; -    } /* if */ +	    Tcl_SetHashValue(hPtr, cfwPtr); +	} +    } /* for */ + +    cfPtr->litarg = lastPtr;  }  /* @@ -5775,6 +5843,7 @@ TclArgumentGet(   *----------------------------------------------------------------------   */ +#undef Tcl_Eval  int  Tcl_Eval(      Tcl_Interp *interp,		/* Token for command interpreter (returned by @@ -5836,6 +5905,11 @@ Tcl_GlobalEvalObj(   *	compiled into bytecodes if necessary, unless TCL_EVAL_DIRECT is   *	specified.   * + *	If the flag TCL_EVAL_DIRECT is passed in, the value of invoker + *	must be NULL.  Support for non-NULL invokers in that mode has + *	been removed since it was unused and untested.  Failure to  + *	follow this limitation will lead to an assertion panic. + *   * Results:   *	The return value is one of the return codes defined in tcl.h (such as   *	TCL_OK), and the interpreter's result contains a value to supplement @@ -5899,18 +5973,17 @@ TclNREvalObjEx(      /*       * This function consists of three independent blocks for: direct -     * evaluation of canonical lists, compileation and bytecode execution and +     * evaluation of canonical lists, compilation and bytecode execution and       * finally direct evaluation. Precisely one of these blocks will be run.       */      if (TclListObjIsCanonical(objPtr)) { -	Tcl_Obj *listPtr = objPtr;  	CmdFrame *eoFramePtr = NULL;  	int objc; -	Tcl_Obj **objv; +	Tcl_Obj *listPtr, **objv;  	/* -	 * Pure List Optimization (no string representation). In this case, we +	 * Canonical List Optimization:  In this case, we  	 * can safely use Tcl_EvalObjv instead and get an appreciable  	 * improvement in execution speed. This is because it allows us to  	 * avoid a setFromAny step that would just pack everything into a @@ -5918,11 +5991,6 @@ TclNREvalObjEx(  	 *  	 * This also preserves any associations between list elements and  	 * location information for such elements. -	 * -	 * This restriction has been relaxed a bit by storing in lists whether -	 * they are "canonical" or not (a canonical list being one that is -	 * either pure or that has its string rep derived by -	 * UpdateStringOfList from the internal rep).  	 */  	/* @@ -5931,13 +5999,13 @@ TclNREvalObjEx(  	 * we always make a copy. The callback takes care od the refCounts for  	 * both listPtr and objPtr.  	 * +	 * TODO: Create a test to demo this need, or eliminate it.  	 * FIXME OPT: preserve just the internal rep?  	 */  	Tcl_IncrRefCount(objPtr);  	listPtr = TclListObjCopy(interp, objPtr);  	Tcl_IncrRefCount(listPtr); -	TclDecrRefCount(objPtr);  	if (word != INT_MIN) {  	    /* @@ -5960,21 +6028,25 @@ TclNREvalObjEx(  	    eoFramePtr->nline = 0;  	    eoFramePtr->line = NULL; -	    eoFramePtr->type = TCL_LOCATION_EVAL_LIST; +	    eoFramePtr->type = TCL_LOCATION_EVAL;  	    eoFramePtr->level = (iPtr->cmdFramePtr == NULL?  		    1 : iPtr->cmdFramePtr->level + 1); -	    eoFramePtr->numLevels = iPtr->numLevels;  	    eoFramePtr->framePtr = iPtr->framePtr;  	    eoFramePtr->nextPtr = iPtr->cmdFramePtr; -	    eoFramePtr->cmd.listPtr = listPtr; +	    eoFramePtr->cmdObj = objPtr; +	    eoFramePtr->cmd = NULL; +	    eoFramePtr->len = 0;  	    eoFramePtr->data.eval.path = NULL;  	    iPtr->cmdFramePtr = eoFramePtr; + +	    flags |= TCL_EVAL_SOURCE_IN_FRAME;  	} -	TclNRDeferCallback(interp, TEOEx_ListCallback, listPtr, eoFramePtr, -		NULL, NULL); +	TclMarkTailcall(interp); +        TclNRAddCallback(interp, TEOEx_ListCallback, listPtr, eoFramePtr, +		objPtr, NULL);  	ListObjGetElements(listPtr, objc, objv);  	return TclNREvalObjv(interp, objc, objv, flags, NULL); @@ -6014,14 +6086,6 @@ TclNREvalObjEx(  	 * We're not supposed to use the compiler or byte-code  	 * interpreter. Let Tcl_EvalEx evaluate the command directly (and  	 * probably more slowly). -	 * -	 * TIP #280. Propagate context as much as we can. Especially if the -	 * script to evaluate is a single literal it makes sense to look if -	 * our context is one with absolute line numbers we can then track -	 * into the literal itself too. -	 * -	 * See also tclCompile.c, TclInitCompileEnv, for the equivalent code -	 * in the bytecode compiler.  	 */  	const char *script; @@ -6045,92 +6109,19 @@ TclNREvalObjEx(  	 */  	ContLineLoc *saveCLLocPtr = iPtr->scriptCLLocPtr; -	ContLineLoc *clLocPtr = TclContinuationsGet(objPtr); - -	if (clLocPtr) { -	    iPtr->scriptCLLocPtr = clLocPtr; -	    Tcl_Preserve(iPtr->scriptCLLocPtr); -	} else { -	    iPtr->scriptCLLocPtr = NULL; -	} - -	Tcl_IncrRefCount(objPtr); -	if (invoker == NULL) { -	    /* -	     * No context, force opening of our own. -	     */ - -	    script = Tcl_GetStringFromObj(objPtr, &numSrcBytes); -	    result = Tcl_EvalEx(interp, script, numSrcBytes, flags); -	} else { -	    /* -	     * We have an invoker, describing the command asking for the -	     * evaluation of a subordinate script. This script may originate -	     * in a literal word, or from a variable, etc. Using the line -	     * array we now check if we have good line information for the -	     * relevant word. The type of context is relevant as well. In a -	     * non-'source' context we don't have to try tracking lines. -	     * -	     * First see if the word exists and is a literal. If not we go -	     * through the easy dynamic branch. No need to perform more -	     * complex invokations. -	     */ - -	    int pc = 0; -	    CmdFrame *ctxPtr = TclStackAlloc(interp, sizeof(CmdFrame)); - -	    *ctxPtr = *invoker; -	    if (invoker->type == TCL_LOCATION_BC) { -		/* -		 * Note: Type BC => ctxPtr->data.eval.path is not used. -		 * ctxPtr->data.tebc.codePtr is used instead. -		 */ - -		TclGetSrcInfoForPc(ctxPtr); -		pc = 1; -	    } - -	    script = Tcl_GetStringFromObj(objPtr, &numSrcBytes); - -	    if ((invoker->nline <= word) || -		    (invoker->line[word] < 0) || -		    (ctxPtr->type != TCL_LOCATION_SOURCE)) { -		/* -		 * Dynamic script, or dynamic context, force our own context. -		 */ -		result = Tcl_EvalEx(interp, script, numSrcBytes, flags); -	    } else { -		/* -		 * Absolute context to reuse. -		 */ +	assert(invoker == NULL); -		iPtr->invokeCmdFramePtr = ctxPtr; -		iPtr->evalFlags |= TCL_EVAL_CTX; +	iPtr->scriptCLLocPtr = TclContinuationsGet(objPtr); -		result = TclEvalEx(interp, script, numSrcBytes, flags, -			ctxPtr->line[word], NULL, script); -	    } -	    if (pc && (ctxPtr->type == TCL_LOCATION_SOURCE)) { -		/* -		 * Death of SrcInfo reference. -		 */ +	Tcl_IncrRefCount(objPtr); -		Tcl_DecrRefCount(ctxPtr->data.eval.path); -	    } -	    TclStackFree(interp, ctxPtr); -	} +	script = Tcl_GetStringFromObj(objPtr, &numSrcBytes); +	result = Tcl_EvalEx(interp, script, numSrcBytes, flags); -	/* -	 * Now release the lock on the continuation line information, if any, -	 * and restore the caller's settings. -	 */ +	TclDecrRefCount(objPtr); -	if (iPtr->scriptCLLocPtr) { -	    Tcl_Release(iPtr->scriptCLLocPtr); -	}  	iPtr->scriptCLLocPtr = saveCLLocPtr; -	TclDecrRefCount(objPtr);  	return result;      }  } @@ -6190,6 +6181,7 @@ TEOEx_ListCallback(      Interp *iPtr = (Interp *) interp;      Tcl_Obj *listPtr = data[0];      CmdFrame *eoFramePtr = data[1]; +    Tcl_Obj *objPtr = data[2];      /*       * Remove the cmdFrame @@ -6199,6 +6191,7 @@ TEOEx_ListCallback(  	iPtr->cmdFramePtr = eoFramePtr->nextPtr;  	TclStackFree(interp, eoFramePtr);      } +    TclDecrRefCount(objPtr);      TclDecrRefCount(listPtr);      return result; @@ -6234,11 +6227,11 @@ ProcessUnexpectedResult(      Tcl_ResetResult(interp);      if (returnCode == TCL_BREAK) { -	Tcl_AppendResult(interp, -		"invoked \"break\" outside of a loop", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +		"invoked \"break\" outside of a loop", -1));      } else if (returnCode == TCL_CONTINUE) { -	Tcl_AppendResult(interp, -		"invoked \"continue\" outside of a loop", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +		"invoked \"continue\" outside of a loop", -1));      } else {  	Tcl_SetObjResult(interp, Tcl_ObjPrintf(  		"command returned bad code: %d", returnCode)); @@ -6561,29 +6554,32 @@ TclObjInvoke(  				 * TCL_INVOKE_HIDDEN, TCL_INVOKE_NO_UNKNOWN,  				 * or TCL_INVOKE_NO_TRACEBACK. */  { -    register Interp *iPtr = (Interp *) interp; -    Tcl_HashTable *hTblPtr;	/* Table of hidden commands. */ -    const char *cmdName;	/* Name of the command from objv[0]. */ -    Tcl_HashEntry *hPtr = NULL; -    Command *cmdPtr; -    int result; -      if (interp == NULL) {  	return TCL_ERROR;      } -      if ((objc < 1) || (objv == NULL)) { -	Tcl_AppendResult(interp, "illegal argument vector", NULL); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "illegal argument vector", -1));  	return TCL_ERROR;      } -      if ((flags & TCL_INVOKE_HIDDEN) == 0) {  	Tcl_Panic("TclObjInvoke: called without TCL_INVOKE_HIDDEN");      } +    return Tcl_NRCallObjProc(interp, TclNRInvoke, NULL, objc, objv); +} -    if (TclInterpReady(interp) == TCL_ERROR) { -	return TCL_ERROR; -    } +int +TclNRInvoke( +    ClientData clientData, +    Tcl_Interp *interp, +    int objc, +    Tcl_Obj *const objv[]) +{ +    register Interp *iPtr = (Interp *) interp; +    Tcl_HashTable *hTblPtr;	/* Table of hidden commands. */ +    const char *cmdName;	/* Name of the command from objv[0]. */ +    Tcl_HashEntry *hPtr = NULL; +    Command *cmdPtr;      cmdName = TclGetString(objv[0]);      hTblPtr = iPtr->hiddenCmdTablePtr; @@ -6591,44 +6587,35 @@ TclObjInvoke(  	hPtr = Tcl_FindHashEntry(hTblPtr, cmdName);      }      if (hPtr == NULL) { -	Tcl_AppendResult(interp, "invalid hidden command name \"", -		cmdName, "\"", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "invalid hidden command name \"%s\"", cmdName));          Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "HIDDENTOKEN", cmdName,                  NULL);  	return TCL_ERROR;      }      cmdPtr = Tcl_GetHashValue(hPtr); -    /* -     * Invoke the command function. -     */ - -    iPtr->cmdCount++; -    if (cmdPtr->objProc != NULL) { -	result = cmdPtr->objProc(cmdPtr->objClientData, interp, objc, objv); -    } else { -	result = Tcl_NRCallObjProc(interp, cmdPtr->nreProc, -		cmdPtr->objClientData, objc, objv); -    } +    /* Avoid the exception-handling brain damage when numLevels == 0 . */ +    iPtr->numLevels++; +    Tcl_NRAddCallback(interp, NRPostInvoke, NULL, NULL, NULL, NULL);      /* -     * If an error occurred, record information about what was being executed -     * when the error occurred. +     * Normal command resolution of objv[0] isn't going to find cmdPtr. +     * That's the whole point of **hidden** commands.  So tell the +     * Eval core machinery not to even try (and risk finding something wrong).       */ -    if ((result == TCL_ERROR) -	    && ((flags & TCL_INVOKE_NO_TRACEBACK) == 0) -	    && ((iPtr->flags & ERR_ALREADY_LOGGED) == 0)) { -	int length; -	Tcl_Obj *command = Tcl_NewListObj(objc, objv); -	const char *cmdString; +    return TclNREvalObjv(interp, objc, objv, TCL_EVAL_NORESOLVE, cmdPtr); +} -	Tcl_IncrRefCount(command); -	cmdString = Tcl_GetStringFromObj(command, &length); -	Tcl_LogCommandInfo(interp, cmdString, cmdString, length); -	Tcl_DecrRefCount(command); -	iPtr->flags &= ~ERR_ALREADY_LOGGED; -    } +static int +NRPostInvoke( +    ClientData clientData[], +    Tcl_Interp *interp, +    int result) +{ +    Interp *iPtr = (Interp *)interp; +    iPtr->numLevels--;      return result;  } @@ -6705,6 +6692,7 @@ Tcl_ExprString(   *----------------------------------------------------------------------   */ +#undef Tcl_AddObjErrorInfo  void  Tcl_AppendObjToErrorInfo(      Tcl_Interp *interp,		/* Interpreter to which error information @@ -6738,6 +6726,7 @@ Tcl_AppendObjToErrorInfo(   *----------------------------------------------------------------------   */ +#undef Tcl_AddErrorInfo  void  Tcl_AddErrorInfo(      Tcl_Interp *interp,		/* Interpreter to which error information @@ -6918,6 +6907,7 @@ Tcl_VarEval(   *----------------------------------------------------------------------   */ +#undef Tcl_GlobalEval  int  Tcl_GlobalEval(      Tcl_Interp *interp,		/* Interpreter in which to evaluate @@ -7218,7 +7208,8 @@ ExprIsqrtFunc(      return TCL_OK;    negarg: -    Tcl_SetResult(interp, "square root of negative argument", TCL_STATIC); +    Tcl_SetObjResult(interp, Tcl_NewStringObj( +            "square root of negative argument", -1));      Tcl_SetErrorCode(interp, "ARITH", "DOMAIN",  	    "domain error: argument not in valid range", NULL);      return TCL_ERROR; @@ -7435,7 +7426,7 @@ ExprAbsFunc(  	return TCL_OK;      } -#ifndef NO_WIDE_TYPE +#ifndef TCL_WIDE_INT_IS_LONG      if (type == TCL_NUMBER_WIDE) {  	Tcl_WideInt w = *((const Tcl_WideInt *) ptr); @@ -8075,39 +8066,11 @@ Tcl_NRCallObjProc(      int objc,      Tcl_Obj *const objv[])  { -    int result = TCL_OK;      NRE_callback *rootPtr = TOP_CB(interp); -#ifdef USE_DTRACE -    if (TCL_DTRACE_CMD_ARGS_ENABLED()) { -	const char *a[10]; -	int i = 0; - -	while (i < 10) { -	    a[i] = i < objc ? TclGetString(objv[i]) : NULL; i++; -	} -	TCL_DTRACE_CMD_ARGS(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], -		a[8], a[9]); -    } -    if (TCL_DTRACE_CMD_INFO_ENABLED() && ((Interp *) interp)->cmdFramePtr) { -	Tcl_Obj *info = TclInfoFrame(interp, ((Interp *) interp)->cmdFramePtr); -	const char *a[6]; int i[2]; - -	TclDTraceInfo(info, a, i); -	TCL_DTRACE_CMD_INFO(a[0], a[1], a[2], a[3], i[0], i[1], a[4], a[5]); -	TclDecrRefCount(info); -    } -    if ((TCL_DTRACE_CMD_RETURN_ENABLED() || TCL_DTRACE_CMD_RESULT_ENABLED()) -	    && objc) { -	TclNRAddCallback(interp, DTraceCmdReturn, objv[0], NULL, NULL, NULL); -    } -    if (TCL_DTRACE_CMD_ENTRY_ENABLED() && objc) { -	TCL_DTRACE_CMD_ENTRY(TclGetString(objv[0]), objc - 1, -		(Tcl_Obj **)(objv + 1)); -    } -#endif /* USE_DTRACE */ -    result = objProc(clientData, interp, objc, objv); -    return TclNRRunCallbacks(interp, result, rootPtr); +    TclNRAddCallback(interp, Dispatch, objProc, clientData, +	    INT2PTR(objc), objv); +    return TclNRRunCallbacks(interp, TCL_OK, rootPtr);  }  /* @@ -8200,7 +8163,8 @@ Tcl_NRCmdSwap(      Tcl_Obj *const objv[],      int flags)  { -    return TclNREvalObjv(interp, objc, objv, flags, (Command *) cmd); +    return TclNREvalObjv(interp, objc, objv, flags|TCL_EVAL_NOERR, +	    (Command *) cmd);  }  /***************************************************************************** @@ -8228,29 +8192,58 @@ Tcl_NRCmdSwap(   */  void -TclSpliceTailcall( +TclMarkTailcall( +    Tcl_Interp *interp) +{ +    Interp *iPtr = (Interp *) interp; + +    if (iPtr->deferredCallbacks == NULL) { +	TclNRAddCallback(interp, NRCommand, NULL, NULL, +                NULL, NULL); +        iPtr->deferredCallbacks = TOP_CB(interp); +    } +} + +void +TclSkipTailcall( +    Tcl_Interp *interp) +{ +    Interp *iPtr = (Interp *) interp; + +    TclMarkTailcall(interp); +    iPtr->deferredCallbacks->data[1] = INT2PTR(1); +} + +void +TclPushTailcallPoint( +    Tcl_Interp *interp) +{ +    TclNRAddCallback(interp, NRCommand, NULL, NULL, NULL, NULL); +    ((Interp *) interp)->numLevels++; +} + +void +TclSetTailcall(      Tcl_Interp *interp, -    NRE_callback *tailcallPtr) +    Tcl_Obj *listPtr)  {      /*       * Find the splicing spot: right before the NRCommand of the thing -     * being tailcalled. Note that we skip NRCommands marked in data[1] +     * being tailcalled. Note that we skip NRCommands marked by a 1 in data[1]       * (used by command redirectors).       */      NRE_callback *runPtr;      for (runPtr = TOP_CB(interp); runPtr; runPtr = runPtr->nextPtr) { -	if (((runPtr->procPtr) == NRCommand) && !runPtr->data[1]) { +        if (((runPtr->procPtr) == NRCommand) && !runPtr->data[1]) {              break;          }      }      if (!runPtr) {          Tcl_Panic("tailcall cannot find the right splicing spot: should not happen!");      } - -    tailcallPtr->nextPtr = runPtr->nextPtr; -    runPtr->nextPtr = tailcallPtr; +    runPtr->data[1] = listPtr;  }  int @@ -8267,10 +8260,9 @@ TclNRTailcallObjCmd(  	return TCL_ERROR;      } -    if (!iPtr->varFramePtr->isProcCallFrame) {	/* or is upleveled */ -        Tcl_SetResult(interp, -                "tailcall can only be called from a proc or lambda", -                TCL_STATIC); +    if (!(iPtr->varFramePtr->isProcCallFrame & 1)) {	/* or is upleveled */ +        Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "tailcall can only be called from a proc or lambda", -1));          Tcl_SetErrorCode(interp, "TCL", "TAILCALL", "ILLEGAL", NULL);  	return TCL_ERROR;      } @@ -8281,7 +8273,7 @@ TclNRTailcallObjCmd(       */      if (iPtr->varFramePtr->tailcallPtr) { -        ClearTailcall(interp, iPtr->varFramePtr->tailcallPtr); +        Tcl_DecrRefCount(iPtr->varFramePtr->tailcallPtr);          iPtr->varFramePtr->tailcallPtr = NULL;      } @@ -8296,39 +8288,39 @@ TclNRTailcallObjCmd(          Tcl_Obj *listPtr, *nsObjPtr;          Tcl_Namespace *nsPtr = (Tcl_Namespace *) iPtr->varFramePtr->nsPtr;          Tcl_Namespace *ns1Ptr; -        NRE_callback *tailcallPtr; + +        /* The tailcall data is in a Tcl list: the first element is the +         * namespace, the rest the command to be tailcalled. */ -        listPtr = Tcl_NewListObj(objc-1, objv+1); -        Tcl_IncrRefCount(listPtr); +        listPtr = Tcl_NewListObj(objc, objv);          nsObjPtr = Tcl_NewStringObj(nsPtr->fullName, -1);          if ((TCL_OK != TclGetNamespaceFromObj(interp, nsObjPtr, &ns1Ptr))                  || (nsPtr != ns1Ptr)) {              Tcl_Panic("Tailcall failed to find the proper namespace");          } -        Tcl_IncrRefCount(nsObjPtr); - -        TclNRAddCallback(interp, NRTailcallEval, listPtr, nsObjPtr, NULL, NULL); -        tailcallPtr = TOP_CB(interp); -        TOP_CB(interp) = tailcallPtr->nextPtr; -        iPtr->varFramePtr->tailcallPtr = tailcallPtr; + 	TclListObjSetElement(interp, listPtr, 0, nsObjPtr); +         +        iPtr->varFramePtr->tailcallPtr = listPtr;      }      return TCL_RETURN;  }  int -NRTailcallEval( +TclNRTailcallEval(      ClientData data[],      Tcl_Interp *interp,      int result)  {      Interp *iPtr = (Interp *) interp; -    Tcl_Obj *listPtr = data[0]; -    Tcl_Obj *nsObjPtr = data[1]; +    Tcl_Obj *listPtr = data[0], *nsObjPtr;      Tcl_Namespace *nsPtr;      int objc;      Tcl_Obj **objv; +    Tcl_ListObjGetElements(interp, listPtr, &objc, &objv);  +    nsObjPtr = objv[0]; +          if (result == TCL_OK) {  	result = TclGetNamespaceFromObj(interp, nsObjPtr, &nsPtr);      } @@ -8338,7 +8330,7 @@ NRTailcallEval(           * Tailcall execution was preempted, eg by an intervening catch or by           * a now-gone namespace: cleanup and return.           */ -         +          TailcallCleanup(data, interp, result);          return result;      } @@ -8347,10 +8339,10 @@ NRTailcallEval(       * Perform the tailcall       */ -    TclNRDeferCallback(interp, TailcallCleanup, listPtr, nsObjPtr, NULL,NULL); +    TclMarkTailcall(interp); +    TclNRAddCallback(interp, TailcallCleanup, listPtr, NULL, NULL,NULL);      iPtr->lookupNsPtr = (Namespace *) nsPtr; -    ListObjGetElements(listPtr, objc, objv); -    return TclNREvalObjv(interp, objc, objv, 0, NULL); +    return TclNREvalObjv(interp, objc-1, objv+1, 0, NULL);  }  static int @@ -8360,19 +8352,9 @@ TailcallCleanup(      int result)  {      Tcl_DecrRefCount((Tcl_Obj *) data[0]); -    Tcl_DecrRefCount((Tcl_Obj *) data[1]);      return result;  } -static void -ClearTailcall( -    Tcl_Interp *interp, -    NRE_callback *tailcallPtr) -{ -    TailcallCleanup(tailcallPtr->data, interp, TCL_OK); -    TCLNR_FREE(interp, tailcallPtr); -} -  void  Tcl_NRAddCallback( @@ -8421,14 +8403,15 @@ TclNRYieldObjCmd(      Tcl_Obj *const objv[])  {      CoroutineData *corPtr = iPtr->execEnvPtr->corPtr; +      if (objc > 2) {  	Tcl_WrongNumArgs(interp, 1, objv, "?returnValue?");  	return TCL_ERROR;      }      if (!corPtr) { -	Tcl_SetResult(interp, "yield can only be called in a coroutine", -		TCL_STATIC); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "yield can only be called in a coroutine", -1));  	Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "ILLEGAL_YIELD", NULL);  	return TCL_ERROR;      } @@ -8438,7 +8421,7 @@ TclNRYieldObjCmd(      }      NRE_ASSERT(!COR_IS_SUSPENDED(corPtr)); -    TclNRAddCallback(interp, NRCoroutineActivateCallback, corPtr, +    TclNRAddCallback(interp, TclNRCoroutineActivateCallback, corPtr,              clientData, NULL, NULL);      return TCL_OK;  } @@ -8452,8 +8435,7 @@ TclNRYieldToObjCmd(  {      CoroutineData *corPtr = iPtr->execEnvPtr->corPtr;      Tcl_Obj *listPtr, *nsObjPtr; -    Tcl_Namespace *nsPtr = (Tcl_Namespace *) iPtr->varFramePtr->nsPtr; -    Tcl_Namespace *ns1Ptr; +    Tcl_Namespace *nsPtr = TclGetCurrentNamespace(interp);      if (objc < 2) {  	Tcl_WrongNumArgs(interp, 1, objv, "command ?arg ...?"); @@ -8461,61 +8443,39 @@ TclNRYieldToObjCmd(      }      if (!corPtr) { -	Tcl_SetResult(interp, "yieldTo can only be called in a coroutine", -		TCL_STATIC); +	Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "yieldto can only be called in a coroutine", -1));  	Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "ILLEGAL_YIELD", NULL);  	return TCL_ERROR;      } +    if (((Namespace *) nsPtr)->flags & NS_DYING) { +        Tcl_SetObjResult(interp, Tcl_NewStringObj( +		"yieldto called in deleted namespace", -1)); +        Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "YIELDTO_IN_DELETED", +		NULL); +        return TCL_ERROR; +    } +      /*       * Add the tailcall in the caller env, then just yield.       *       * This is essentially code from TclNRTailcallObjCmd       */ -    listPtr = Tcl_NewListObj(objc-1, objv+1); -    Tcl_IncrRefCount(listPtr); - +    listPtr = Tcl_NewListObj(objc, objv);      nsObjPtr = Tcl_NewStringObj(nsPtr->fullName, -1); -    if ((TCL_OK != TclGetNamespaceFromObj(interp, nsObjPtr, &ns1Ptr)) -	    || (nsPtr != ns1Ptr)) { -	Tcl_Panic("yieldTo failed to find the proper namespace"); -    } -    Tcl_IncrRefCount(nsObjPtr); +    TclListObjSetElement(interp, listPtr, 0, nsObjPtr);      /*       * Add the callback in the caller's env, then instruct TEBC to yield.       */      iPtr->execEnvPtr = corPtr->callerEEPtr; -    TclNRAddCallback(interp, YieldToCallback, corPtr, listPtr, nsObjPtr, -	    NULL); +    TclSetTailcall(interp, listPtr);      iPtr->execEnvPtr = corPtr->eePtr; -    return TclNRYieldObjCmd(clientData, interp, 1, objv); -} - -static int -YieldToCallback( -    ClientData data[], -    Tcl_Interp *interp, -    int result) -{ -    /* CoroutineData *corPtr = data[0];*/ -    Tcl_Obj *listPtr = data[1]; -    ClientData nsPtr = data[2]; -    NRE_callback *cbPtr; - -    /* -     * yieldTo: invoke the command using tailcall tech. -     */ - -    TclNRAddCallback(interp, NRTailcallEval, listPtr, nsPtr, NULL, NULL); -    cbPtr = TOP_CB(interp); -    TOP_CB(interp) = cbPtr->nextPtr; - -    TclSpliceTailcall(interp, cbPtr); -    return TCL_OK; +    return TclNRYieldObjCmd(INT2PTR(CORO_ACTIVATE_YIELDM), interp, 1, objv);  }  static int @@ -8542,7 +8502,7 @@ RewindCoroutine(      corPtr->eePtr->rewind = 1;      TclNRAddCallback(interp, RewindCoroutineCallback, state,  	    NULL, NULL, NULL); -    return NRInterpCoroutine(corPtr, interp, 0, NULL); +    return TclNRInterpCoroutine(corPtr, interp, 0, NULL);  }  static void @@ -8590,7 +8550,7 @@ NRCoroutineCallerCallback(      NRE_ASSERT(COR_IS_SUSPENDED(corPtr));      SAVE_CONTEXT(corPtr->running);      RESTORE_CONTEXT(corPtr->caller); -     +      if (cmdPtr->flags & CMD_IS_DELETED) {  	/*  	 * The command was deleted while it was running: wind down the @@ -8652,20 +8612,25 @@ NRCoroutineExitCallback(      return result;  } -  /* - * NRCoroutineActivateCallback -- + *----------------------------------------------------------------------   * - * This is the workhorse for coroutines: it implements both yield and resume. + * TclNRCoroutineActivateCallback --   * - * It is important that both be implemented in the same callback: the - * detection of the impossibility to suspend due to a busy C-stack relies on - * the precise position of a local variable in the stack. We do not want the - * compiler to play tricks on us, either by moving things around or inlining. + *      This is the workhorse for coroutines: it implements both yield and + *      resume. + * + *      It is important that both be implemented in the same callback: the + *      detection of the impossibility to suspend due to a busy C-stack relies + *      on the precise position of a local variable in the stack. We do not + *      want the compiler to play tricks on us, either by moving things around + *      or inlining. + * + *----------------------------------------------------------------------   */ -static int -NRCoroutineActivateCallback( +int +TclNRCoroutineActivateCallback(      ClientData data[],      Tcl_Interp *interp,      int result) @@ -8678,18 +8643,18 @@ NRCoroutineActivateCallback(      if (!corPtr->stackLevel) {          /*           * -- Coroutine is suspended -- -         * Push the callback to restore the caller's context on yield or return +         * Push the callback to restore the caller's context on yield or +         * return.           */ -        TclNRAddCallback(interp, NRCoroutineCallerCallback, corPtr, NULL, NULL, -                NULL); +        TclNRAddCallback(interp, NRCoroutineCallerCallback, corPtr, +                NULL, NULL, NULL);          /*           * Record the stackLevel at which the resume is happening, then swap -         * the interp's environment to make it suitable to run this -         * coroutine.  +         * the interp's environment to make it suitable to run this coroutine.           */ -         +          corPtr->stackLevel = stackLevel;          numLevels = corPtr->auxNumLevels;          corPtr->auxNumLevels = iPtr->numLevels; @@ -8699,29 +8664,27 @@ NRCoroutineActivateCallback(          RESTORE_CONTEXT(corPtr->running);          iPtr->execEnvPtr = corPtr->eePtr;          iPtr->numLevels += numLevels; -         -        return TCL_OK;      } else {          /*           * Coroutine is active: yield           */          if (corPtr->stackLevel != stackLevel) { -            Tcl_SetResult(interp, "cannot yield: C stack busy", -                    TCL_STATIC); +            Tcl_SetObjResult(interp, Tcl_NewStringObj( +                    "cannot yield: C stack busy", -1));              Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "CANT_YIELD",                      NULL);              return TCL_ERROR;          } -         -        if (type == CORO_ACTIVATE_YIELD) {  + +        if (type == CORO_ACTIVATE_YIELD) {              corPtr->nargs = COROUTINE_ARGUMENTS_SINGLE_OPTIONAL;          } else if (type == CORO_ACTIVATE_YIELDM) {              corPtr->nargs = COROUTINE_ARGUMENTS_ARBITRARY;          } else {              Tcl_Panic("Yield received an option which is not implemented");          } -         +          corPtr->stackLevel = NULL;          numLevels = iPtr->numLevels; @@ -8729,10 +8692,20 @@ NRCoroutineActivateCallback(          corPtr->auxNumLevels = numLevels - corPtr->auxNumLevels;          iPtr->execEnvPtr = corPtr->callerEEPtr; -        return TCL_OK;      } + +    return TCL_OK;  } +/* + *---------------------------------------------------------------------- + * + * NRCoroInjectObjCmd -- + * + *      Implementation of [::tcl::unsupported::inject] command. + * + *---------------------------------------------------------------------- + */  static int  NRCoroInjectObjCmd( @@ -8744,7 +8717,7 @@ NRCoroInjectObjCmd(      Command *cmdPtr;      CoroutineData *corPtr;      ExecEnv *savedEEPtr = iPtr->execEnvPtr; -     +      /*       * Usage more or less like tailcall:       *   inject coroName cmd ?arg1 arg2 ...? @@ -8756,31 +8729,36 @@ NRCoroInjectObjCmd(      }      cmdPtr = (Command *) Tcl_GetCommandFromObj(interp, objv[1]); -    if ((!cmdPtr) || (cmdPtr->nreProc != NRInterpCoroutine)) { -        Tcl_SetObjResult(interp, Tcl_NewStringObj("can only inject a command into a coroutine", -1)); +    if ((!cmdPtr) || (cmdPtr->nreProc != TclNRInterpCoroutine)) { +        Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "can only inject a command into a coroutine", -1)); +        Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "COROUTINE", +                TclGetString(objv[1]), NULL);          return TCL_ERROR;      } -    corPtr = (CoroutineData *) cmdPtr->objClientData; +    corPtr = cmdPtr->objClientData;      if (!COR_IS_SUSPENDED(corPtr)) { -        Tcl_SetObjResult(interp, Tcl_NewStringObj("can only inject a command into a suspended coroutine", -1)); +        Tcl_SetObjResult(interp, Tcl_NewStringObj( +                "can only inject a command into a suspended coroutine", -1)); +        Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "ACTIVE", NULL);          return TCL_ERROR;      }      /*       * Add the callback to the coro's execEnv, so that it is the first thing -     * to happen when the coro is resumed +     * to happen when the coro is resumed.       */ -     +      iPtr->execEnvPtr = corPtr->eePtr; -    Tcl_NREvalObj(interp, Tcl_NewListObj(objc-2, objv+2), 0);     +    TclNREvalObjEx(interp, Tcl_NewListObj(objc-2, objv+2), 0, NULL, INT_MIN);      iPtr->execEnvPtr = savedEEPtr; -     +      return TCL_OK;  } - +  int -NRInterpCoroutine( +TclNRInterpCoroutine(      ClientData clientData,      Tcl_Interp *interp,		/* Current interpreter. */      int objc,			/* Number of arguments. */ @@ -8789,9 +8767,9 @@ NRInterpCoroutine(      CoroutineData *corPtr = clientData;      if (!COR_IS_SUSPENDED(corPtr)) { -	Tcl_ResetResult(interp); -	Tcl_AppendResult(interp, "coroutine \"", Tcl_GetString(objv[0]), -		"\" is already running", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "coroutine \"%s\" is already running", +                Tcl_GetString(objv[0])));  	Tcl_SetErrorCode(interp, "TCL", "COROUTINE", "BUSY", NULL);  	return TCL_ERROR;      } @@ -8827,11 +8805,22 @@ NRInterpCoroutine(          break;      } -    TclNRAddCallback(interp, NRCoroutineActivateCallback, corPtr, +    TclNRAddCallback(interp, TclNRCoroutineActivateCallback, corPtr,              NULL, NULL, NULL);      return TCL_OK;  } +/* + *---------------------------------------------------------------------- + * + * TclNRCoroutineObjCmd -- + * + *      Implementation of [coroutine] command; see documentation for + *      description of what this does. + * + *---------------------------------------------------------------------- + */ +  int  TclNRCoroutineObjCmd(      ClientData dummy,		/* Not used. */ @@ -8845,7 +8834,7 @@ TclNRCoroutineObjCmd(      Namespace *nsPtr, *altNsPtr, *cxtNsPtr;      Tcl_DString ds;      Namespace *lookupNsPtr = iPtr->varFramePtr->nsPtr; -     +      if (objc < 3) {  	Tcl_WrongNumArgs(interp, 1, objv, "name cmd ?arg ...?");  	return TCL_ERROR; @@ -8861,22 +8850,24 @@ TclNRCoroutineObjCmd(  	    &nsPtr, &altNsPtr, &cxtNsPtr, &procName);      if (nsPtr == NULL) { -	Tcl_AppendResult(interp, "can't create procedure \"", fullName, -		"\": unknown namespace", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't create procedure \"%s\": unknown namespace", +                fullName));          Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NAMESPACE", NULL);  	return TCL_ERROR;      }      if (procName == NULL) { -	Tcl_AppendResult(interp, "can't create procedure \"", fullName, -		"\": bad procedure name", NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't create procedure \"%s\": bad procedure name", +                fullName));          Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMMAND", fullName, NULL);  	return TCL_ERROR;      }      if ((nsPtr != iPtr->globalNsPtr)  	    && (procName != NULL) && (procName[0] == ':')) { -	Tcl_AppendResult(interp, "can't create procedure \"", procName, -		"\" in non-global namespace with name starting with \":\"", -		NULL); +	Tcl_SetObjResult(interp, Tcl_ObjPrintf( +                "can't create procedure \"%s\" in non-global namespace with" +                " name starting with \":\"", procName));          Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMMAND", procName, NULL);  	return TCL_ERROR;      } @@ -8891,12 +8882,12 @@ TclNRCoroutineObjCmd(      Tcl_DStringInit(&ds);      if (nsPtr != iPtr->globalNsPtr) {  	Tcl_DStringAppend(&ds, nsPtr->fullName, -1); -	Tcl_DStringAppend(&ds, "::", 2); +	TclDStringAppendLiteral(&ds, "::");      }      Tcl_DStringAppend(&ds, procName, -1);      cmdPtr = (Command *) Tcl_NRCreateCommand(interp, Tcl_DStringValue(&ds), -	    /*objProc*/ NULL, NRInterpCoroutine, corPtr, DeleteCoroutine); +	    /*objProc*/ NULL, TclNRInterpCoroutine, corPtr, DeleteCoroutine);      Tcl_DStringFree(&ds);      corPtr->cmdPtr = cmdPtr; @@ -8940,17 +8931,16 @@ TclNRCoroutineObjCmd(      corPtr->running.lineLABCPtr = corPtr->lineLABCPtr;      corPtr->stackLevel = NULL;      corPtr->auxNumLevels = 0; -    iPtr->numLevels--; -     +      /*       * Create the coro's execEnv, switch to it to push the exit and coro -     * command callbacks, then switch back.  +     * command callbacks, then switch back.       */      corPtr->eePtr = TclCreateExecEnv(interp, CORO_STACK_INITIAL_SIZE);      corPtr->callerEEPtr = iPtr->execEnvPtr;      corPtr->eePtr->corPtr = corPtr; -     +      SAVE_CONTEXT(corPtr->caller);      corPtr->callerEEPtr = iPtr->execEnvPtr;      RESTORE_CONTEXT(corPtr->running); @@ -8959,19 +8949,20 @@ TclNRCoroutineObjCmd(      TclNRAddCallback(interp, NRCoroutineExitCallback, corPtr,  	    NULL, NULL, NULL); +    /* insure that the command is looked up in the correct namespace */      iPtr->lookupNsPtr = lookupNsPtr;      Tcl_NREvalObj(interp, Tcl_NewListObj(objc-2, objv+2), 0); +    iPtr->numLevels--;      SAVE_CONTEXT(corPtr->running);      RESTORE_CONTEXT(corPtr->caller);      iPtr->execEnvPtr = corPtr->callerEEPtr; -     +      /* -     * Now just resume the coroutine. Take care to insure that the command is -     * looked up in the correct namespace. +     * Now just resume the coroutine.       */ -    TclNRAddCallback(interp, NRCoroutineActivateCallback, corPtr, +    TclNRAddCallback(interp, TclNRCoroutineActivateCallback, corPtr,              NULL, NULL, NULL);      return TCL_OK;  } | 
